gpujs / gpu.js

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

Examples don't run on codepen #648

Open ecotner opened 3 years ago

ecotner commented 3 years ago

A GIF or MEME to give some spice of the internet

What is wrong?

Running the introductory matrix multiplication example does not work on codepen.

Where does it happen?

In gpu-browser.min.js, when launching the kernel, it throws an error saying Uncaught Error: Unhandled function, couldn't find name on line 5, position 1: window.CP.shouldStopExecution(0). I have tried this on Firefox version 83.0 and Chrome 83.0.4103.61 and get the same error. My OS is Ubuntu 18.04 LTS. I did run the same script locally and it worked fine though, so the problem is specific to execution in codepen.

How do we replicate the issue?

Go to this example and click "Run", then pull up the dev tools and look in the console. You'll see the error. I'll paste the code below as well just in case something happens to the link. I did some googling and it seems that codepen sometimes has troubles with loops, and sometimes changing things to work with array methods will fix things.

How important is this (1-5)?

Definitely low priority... 1? It would be nice for people who just want to try out gpu.js super quick but not go through installing it locally just to see if it's what they need. If fixing this would break other things or decrease performance, I totally understand. It also could potentially just be completely unresolvable unless codepen were to change the way they execute JS haha. Just thought I should bring this to your attention

Expected behavior (i.e. solution)

The demo should have run without error on codepen

Other Comments

Here's the code being run in the codepen example:

HTML:

<h1 id="text">Let's do a GPU calculation!</h1>
<script src="https://cdn.jsdelivr.net/npm/gpu.js@latest/dist/gpu-browser.min.js"></script>

CSS:

h1 {
  color: white;
}

html {
  background-color: black;
}

Javascript:

const gpu = new GPU();
const multiplyMatrix = gpu.createKernel(function(a, b) {
    let sum = 0;
    for (let i = 0; i < 512; i++) {
        sum += a[this.thread.y][i] * b[i][this.thread.x];
    }
    return sum;
}).setOutput([512, 512]);

let [a, b] = [[], []]
for (var i=0; i<512; i++) {
  a.push([]); b.push([]);
  for (var j=0; j<512; j++) {
    a[i].push(Math.random()); b[i].push(Math.random());
  }
}

let x = document.getElementById("text");
x.innerText = "Starting matrix multiplication...";
const c = multiplyMatrix(a, b);
x.innerText = "The first element of c is: " + c[0][0];

BTW, I also noticed that in your example scripts, you never define the matrices a and b, so a simple copy/paste is not enough to get the examples running (hence why I have initialized them here).

harshkhandeparkar commented 3 years ago

Looks like your matrices are wrong...

Compare this image

To this image

You haven't used j anywhere...

Where did you get the example code from? The readme or the website?

Thank you!

harshkhandeparkar commented 3 years ago

If you are not using the example on the website, try it out, it's slightly new.

ecotner commented 3 years ago

Looks like your matrices are wrong... You haven't used j anywhere...

The matrices are not wrong; they are generated in exactly the same way as the snippet you posted (I might as well ask you why you haven't used x anywhere).

Where did you get the example code from? The readme or the website?

From the README.md on github. I was not aware of the website example, but it seems that it has the exact same problem.

harshkhandeparkar commented 3 years ago

The matrices are not wrong; they are generated in exactly the same way as the snippet you posted (I might as well ask you why you haven't used x anywhere).

Ok, lol. I did not realize that you defined a and b separately :sweat_smile:

but it seems that it has the exact same problem.

I never faced such an issue. Which version of gpu.js are you using?

harshkhandeparkar commented 3 years ago

I don't think there is an error in the code ¯\_(ツ)_/¯

ecotner commented 3 years ago

I am using the version that is being returned by the CDN at https://cdn.jsdelivr.net/npm/gpu.js@latest/dist/gpu-browser.min.js, which appears to be 2.10.5

I don't think there is an error in the code ¯_(ツ)_/¯

Can you not reproduce the error? Follow this link to my implementation of the website example. Does it work for you?

harshkhandeparkar commented 3 years ago

I managed to reproduce, but couldn't find the error. The error message is also cryptic.

ecotner commented 3 years ago

Ok, so does that mean you don't think this is worth fixing? @robertleeplummerjr do you think there is anything that can be done about this or should I close the issue?

robertleeplummerjr commented 3 years ago

For sure this is something that needs handled, I've just been very short on time. I'll see about looking into it this week.