Jump to content


OpenCL random numbers


9 replies to this topic

#1 Alienizer

    Member

  • Members
  • PipPipPipPip
  • 435 posts

Posted 10 January 2012 - 07:05 PM

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.

#2 .oisyn

    DevMaster Staff

  • Moderators
  • 1842 posts

Posted 11 January 2012 - 11:41 AM

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?
C++ addict
-
Currently working on: the 3D engine for Tomb Raider.

#3 TheNut

    Senior Member

  • Moderators
  • 1701 posts
  • LocationCyberspace

Posted 11 January 2012 - 01:14 PM

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.
http://www.nutty.ca - Being a nut has its advantages.

#4 Vilem Otte

    Valued Member

  • Members
  • PipPipPipPip
  • 345 posts

Posted 11 January 2012 - 01:37 PM

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).
My blog about game development (and not just game development) - http://gameprogramme...y.blogspot.com/

If you don't know how to speed up application, go "roarrrrrr!", hit the compiler with the club and use -O3 :D

#5 Alienizer

    Member

  • Members
  • PipPipPipPip
  • 435 posts

Posted 11 January 2012 - 08:22 PM

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

#6 }:+()___ (Smile)

    Member

  • Members
  • PipPipPip
  • 169 posts

Posted 12 January 2012 - 04:10 AM

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.
Sorry my broken english!

#7 Alienizer

    Member

  • Members
  • PipPipPipPip
  • 435 posts

Posted 12 January 2012 - 06:04 PM

View Post}:+()___ (Smile), on 12 January 2012 - 04:10 AM, said:

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?

#8 }:+()___ (Smile)

    Member

  • Members
  • PipPipPip
  • 169 posts

Posted 12 January 2012 - 06:49 PM

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.
Sorry my broken english!

#9 Stainless

    Member

  • Members
  • PipPipPipPip
  • 582 posts
  • LocationSouthampton

Posted 13 January 2012 - 11:33 AM

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.

#10 Alienizer

    Member

  • Members
  • PipPipPipPip
  • 435 posts

Posted 13 January 2012 - 04:24 PM

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

Thanks everyone.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users