1

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.

1
  • What are you putting in the withWeighting parameter? Commented Jun 5, 2024 at 19:41

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.