OpenCL random numbers

88dc730f0f71e55be39de0ad103bd9ff
0
Alienizer 109 Jan 10, 2012 at 19:05

Is there a good random number generator available for OpenCL? I’ve looked at many, all are complicated, and I don’t know which to use and which are good?

Thanks.

9 Replies

Please log in or register to post a reply.

340bf64ac6abda6e40f7e860279823cb
0
_oisyn 101 Jan 11, 2012 at 11:41

What we usually did in shaders for rendering was using noise textures. Basically just a buffer of precomputed random values. Can’t you use something similar?

6837d514b487de395be51432d9cdd078
0
TheNut 179 Jan 11, 2012 at 13:14

If you want a “stable” RNG that you can reproduce solutions with, then following .oisyn’s suggestion is the way to go. I commonly use simplex noise due to its versatility. Another approach you can take is to implement a XORshift RNG (FYI I haven’t tried this in OpenCL). It’s simple to implement, but also unstable in a parallel environment. You would need to assign a seed to each work item to maintain stability.

B20d81438814b6ba7da7ff8eb502d039
0
Vilem_Otte 117 Jan 11, 2012 at 13:37

Just a note, I’ve tried and successfully implemented XORshift RNG, that has been mentioned by The Nut in OpenCL - it works quite well ;). Though as mentioned, you need to assign a seed to each work item (didn’t do that first and it didn’t work quite well).

88dc730f0f71e55be39de0ad103bd9ff
0
Alienizer 109 Jan 11, 2012 at 20:22

Is there a way to use atom or anything so we don’t have to pass a seed?

6eaf0e08fe36b2c23ca096562dd7a8b7
0
__________Smile_ 101 Jan 12, 2012 at 04:10

Without seed you lose parallelism. Well, you can reduce size of seed array from one per work item to one per workgroup (local thread group, 1 compute unit, about 256 threads) and regenerate seeds with atomics, but it’s nontrivial algorithm and you may lose performance and random quality.

88dc730f0f71e55be39de0ad103bd9ff
0
Alienizer 109 Jan 12, 2012 at 18:04

@}:+()___ (Smile)

Without seed you lose parallelism. Well, you can reduce size of seed array from one per work item to one per workgroup (local thread group, 1 compute unit, about 256 threads) and regenerate seeds with atomics, but it’s nontrivial algorithm and you may lose performance and random quality.

I see what you mean, but what I meant to say was, if there is a way to pass a seed on every call to the kernel so that the seed is global to all the work items and workgroup, and updated everytime the random function is called? More like a GPU thread safe seed update?

6eaf0e08fe36b2c23ca096562dd7a8b7
0
__________Smile_ 101 Jan 12, 2012 at 18:49

It’s possible but too slow. Work items in GPU execute single instruction at the exact same time. So it’s easy to reach slowdown by factor \~100.

B5262118b588a5a420230bfbef4a2cdf
0
Stainless 151 Jan 13, 2012 at 11:33

Or you can build the seed into the structure you pass to the shaders.

It depends what you want to do, when I do my cloud simulations I use the old trick of using floating point textures and swapping buffers.

So you populate a texture with variables, run the shader on it and write the results to another texture. Then swap the textures.

You are limited in that you can only have 4 variables per pixel, so your equations have to be broken down into separate passes, but it’s effective.

88dc730f0f71e55be39de0ad103bd9ff
0
Alienizer 109 Jan 13, 2012 at 16:24

I now know the reason why GPUs don’t have a random() function!

Thanks everyone.