Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Math.random in mode:gpu #498

Closed
marcelloexp386 opened this issue Sep 18, 2019 · 19 comments
Closed

Math.random in mode:gpu #498

marcelloexp386 opened this issue Sep 18, 2019 · 19 comments

Comments

@marcelloexp386
Copy link

marcelloexp386 commented Sep 18, 2019

A GIF or MEME to give some spice of the internet

What is wrong?

The random number distribution is Normal instead of Uniform[0,1]

Where does it happen?

Ex. In GPU.js when I run Math.random inside a gpu.createKernel(function() in Node.js on my pc windows 10 and Nvidia GPU
NB If mode is set to cpu, all else equal, the numbers are Unifom

How do we replicate the issue?

const { GPU } = require('gpu.js');
const gpu = new GPU({ mode: 'gpu' });
const nobs=10000;

const kernel = gpu.createKernel(function() {
const y=Math.random();
return y;
}, { output: [nobs] });
const data = kernel();

How important is this (1-5)?

5

Expected behavior (i.e. solution)

the data should be uniform

Other Comments

Probably, in the plugin you should have chosen nrandom instead of n4random.

@robertleeplummerjr
Copy link
Member

Do you have a link to that function? It was super difficult to find any insight when implementing.

@marcelloexp386
Copy link
Author

Hi Robert, I am quite new to gpu.js and zero-knowledge of WebGL, shaders ... so I think to be quite hopeless in finding a solution. To be totally honest I just had the intuition that the problem was in the choice of n4rand.

In https://www.shadertoy.com/view/4t2SDh, you can find the file generating the noise with the 4 different functions. One can see that nrand produce a uniform histogram, while the others n1rand, n2rand, n3rand and n4 rand produces triangle and bell shaped histograms.

In your plugin triangle-noise.js, you make a functionReplace of Math.random() with n4rand(vTexCoord). If I interpreted correctly your code, the right function should be nrand.

My script for testing the statistical properties of the random numbers is attached here (as I said, I run it with node and one can alternatively select "cpu" or "gpu" in line 10). If you take the data saved in the txt file and plot the histogram you will immediately see the bell shaped curve for the data generated with the "gpu" mode.
stampa_randnumbers_GPU_v01.js.txt

@robertleeplummerjr
Copy link
Member

Fantastic! Ty for not attacking this, it was quite the thing to figure out. I'll review asap, and get it corrected. Again TY!

@marcelloexp386
Copy link
Author

Thanks to you and the gpu team for the fantastic job you are doing with gpu.js!

@robertleeplummerjr
Copy link
Member

ty for trying it!

@marcelloexp386
Copy link
Author

marcelloexp386 commented Sep 20, 2019 via email

@marcelloexp386
Copy link
Author

marcelloexp386 commented Oct 16, 2019 via email

@robertleeplummerjr
Copy link
Member

Hi Robert, did you succeed in changing algorithm?>

I tried some changes out this morning on this branch: https://github.com/gpujs/gpu.js/tree/498-uniformly-distributed
Can you take look and see if that is good enough for your needs?
I can see a noticeable improvement when comparing them on the ./examples/random.html page.

@robertleeplummerjr
Copy link
Member

FYI: https://github.com/gpujs/gpu.js/tree/498-uniformly-distributed will be merged soon, and addresses the issue brought up here.

@marcelloexp386
Copy link
Author

Hi Robert, tremendous improvement! I run a "pi" test (i.e. estimation of the pi value) and now the results do not show anymore the systematic bias. Still, the GPU method produces worse estimates (in the sense of higher stdev) than the CPU method, but this was expected. If, somewhere in your blog, you can mention RandomPower for having contributed to your project, it would be very much appreciated. RandomPower is a university spin-off where we use a quantistic effect of the light in order to generate True Random Numbers .

@robertleeplummerjr
Copy link
Member

Do you have a link to RandomPower?

@marcelloexp386
Copy link
Author

@robertleeplummerjr
Copy link
Member

Can you get me more details on the contribution?

@marcelloexp386
Copy link
Author

? In reality, my contribution was just about "testing" your random number generator algorithm. So, I would simply mention randompower.eu among the contributors to the gpujs project.

@robertleeplummerjr
Copy link
Member

Awesome! Is there any sort of qualification that our random generates that we can make mention of?

@marcelloexp386
Copy link
Author

We can analyze their statistical properties but it will take a bit of time because at the moment we are working on the hardware of our project. As soon as we have a couple of days free, we can come back with some stats.
If/when you put a link to our website somewhere in your blog, let me know.

@robertleeplummerjr
Copy link
Member

I'll just put a shout out to RandomPower in the readme if that is ok. That way everyone sees it. Also, I very much look forward to those test results!

@robertleeplummerjr
Copy link
Member

@marcelloexp386
Copy link
Author

marcelloexp386 commented Oct 29, 2019 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants