Bob Haslett
Bob Haslett

Reputation: 1103

Simple average calculation using GPU.js

I’m trying to adapt a fairly simple old function to use the GPU.js library and make it faster. Hopefully, once I’ve got my head around some of the concepts I can build on it to speed up the functionality of overall code.

For the moment I just want to get this calculation to work Given the following data structure of withWeighting how do adapt th below function correctly;

0
: 
date
: 
Wed Feb 24 2021 00:00:00 GMT+0000 (Greenwich Mean Time) {}
party
: 
"Macron"
pollster
: 
"Ifop-Fiducial"
value
: 
31
weight
: 
100
[[Prototype]]
: 
Object```

My current function looks like this:

const average =
      d3.sum(withWeighting.map(({ value, weight }) => value * weight)) /
      d3.sum(withWeighting.map(({ weight }) => weight));

I’ve tried variations on this:

const calculateAverageKernel = gpu.createKernel(function (withWeighting) {
  let sumValueWeight = 0;
  let sumWeight = 0;

  for (let i = 0; i < withWeighting.length; i++) {
    const { value, weight } = withWeighting[i];
    sumValueWeight += value * weight;
    sumWeight += weight;
  }

  return sumValueWeight / sumWeight;
}).setOutput([1]);

But this is the error message I am getting

comms.js:99 Uncaught TypeError: Cannot read properties of undefined (reading 'start')

I suspect that gpu.js can’t accept an object but any advice is gratefully received, Thanks.

Upvotes: 1

Views: 101

Answers (0)

Related Questions