gpujs / gpu.js

GPU Accelerated JavaScript
https://gpu.rocks
MIT License
15.1k stars 652 forks source link

Example of adding a custom function to gpu/kernel instance (node env) #635

Closed JThobbyist closed 4 years ago

JThobbyist commented 4 years ago

A GIF or MEME to give some spice of the internet image (it me)

What is wrong?

I have tried and failed like a hundred times to figure out how to get even a simple custom function to work in a kernel. I've tried adding it to the kernel instance, and I've tried ading it to the gpu instance. Nothing works. Example Plz?

Where does it happen?

when trying to add any custom function in node.js on my surface pro 4 running windows 10 and the latest versions of node.js and GPU.js

How do we replicate the issue?

My code looks like:

` const _gpu = new GPU();

const sqre = function (x) {return (x)**2};

_gpu.addFunction(sqre, {argumentTypes: {x:'Number'}, returnType: 'Number'})

const seed = [1, 2, 3];

const kernel = _gpu.createKernel(function(a){ return sqre(a[this.thread.x]); }, { output: [3] });

const res = kernel(seed); console.log(res);`

I've also tried making the function a string, using addNativeFunction instead of addFunction, etc.

How important is this (1-5)?

5 (for me as a GPU.js beginner)

Expected behavior (i.e. solution)

the function sqre should have been accessible from within the kernel function

Other Comments

harshkhandeparkar commented 4 years ago

What's the error?

harshkhandeparkar commented 4 years ago

Anonymous functions may not work, try function sqre(x) {}

JThobbyist commented 4 years ago

Thanks! It works as expected when it's a named function. Seems like a detail that might be good to note in the docs?

harshkhandeparkar commented 4 years ago

True. Mind opening an issue for that specifically?

JThobbyist commented 4 years ago

Sure