cancel
Showing results for 
Search instead for 
Did you mean: 

OpenCL

rsacker
Journeyman III

Random numbers in OpenCL

Does anyone have a simple random # generator for OpenCL kernel? The links on http://devgurus.amd.com/message/1178689 are both broken. Uploading them from the host is not practical.

Tags (1)
0 Kudos
Reply
20 Replies
jtrudeau
Staff
Staff

Re: Random numbers in OpenCL

I hope people have suggestions. All I can do is a search, but it turned up some possibilities.

A good discussion of multiple algorithms/approaches on Stack Overflow

http://stackoverflow.com/questions/9912143/how-to-get-a-random-number-in-opencl

A PRNGCL implementatino with links to source code

MWC64X - Uniform random number generator for OpenCL.

Hope this helps

0 Kudos
Reply
nan
Adept II

Re: Random numbers in OpenCL

Hi,

I've implemented a GPL licensed PRNG library for use in scientific simulations nearly two years ago. With the restrictions of OpenCL 1.1 it was one hack after another. It implements a classical Mersenne Twister PRNG, one MTGP PRNG and Multiply with Carry generator with a state of 5 uints. The Mersenne Twister PRNG is fastest but it does not support a work group size of 256 work items. The untempered MTGP generator is fast, too. With some optimizations some cycles in the evaluation of MT generator can be saved. It can output normally and uniformly distributed random numbers. Hopefully, that helps you. If I'm able to install a working OpenCL 2.0 driver I could cleanup the whole library. Here is the link: http://theorie.physik.uni-wuerzburg.de/~hinrichsen/software/

If you need a PRNG for graphics than simply use a linear congruential generator or a small MWC once and do some rotates and XORs.

Edit: Someone built a simple comparison of some OpenCL PRNGs: mcopik/OpenCLPRNGs · GitHub

-- NaN

0 Kudos
Reply
ddemidov
Adept II

Re: Random numbers in OpenCL

There is a couple of counter-based RNG from Random123 suite (D. E. Shaw Research) implemented in VexCL library (ddemidov/vexcl · GitHub). The advantage of counter-based generators is that they are stateless and hence you don't need to access global memory which makes them work fast on GPUs. If you don't want to depend on a library, you can run a simple simulation and ask VexCL to dump the generated OpenCL kernel to stdout and then use it. See a simple example here: https://gist.github.com/ddemidov/8ed870ffa0cb409ae6a1

0 Kudos
Reply
rsacker
Journeyman III

Re: Random numbers in OpenCL

#########################

Thank you for the info. I’m having a bit of trouble with the code. First the file “pi.cpp” contains "#include <vexcl/vexcl.hpp>” but I cannot find a header of such name. I see "vexcl<https://github.com/ddemidov/vexcl> / CL<https://github.com/ddemidov/vexcl/tree/master/CL> / cl.hpp” (only 11.45 K lines long). In addition the “Clang” compiler (on a Mac) keeps telling me " OpenCL does not support the 'static' storage class specifier”. Do you know of anyone who is implementing OpenCL on a Mac?

0 Kudos
Reply
ddemidov
Adept II

Re: Random numbers in OpenCL

<vexcl/vexcl.hpp> is located here: vexcl/vexcl.hpp at master · ddemidov/vexcl · GitHub.

I don't have much experience with MacOS, but VexCL has been reported to work there. See vexcl/INSTALL.md at master · ddemidov/vexcl · GitHub

<CL/cl.hpp> in VexCL tree is just a copy of Khronos-supplied C++ bindings header (https://www.khronos.org/registry/cl/api/1.2/cl.hpp). If Apple provides its own version of that, you could use it.

> In addition the “Clang” compiler (on a Mac) keeps telling me  " OpenCL does not support the 'static' storage class specifier”.

Could you clarify that a bit? What exactly is the source of the problem here?

0 Kudos
Reply
rsacker
Journeyman III

Re: Random numbers in OpenCL

#########################

Thank you ddemidov,

I’m sure the problem is my own ignorance but I seem to recall reading in several places “cross-platform” so I naively assumed the code would work on the Mac but I can’t even get this stuff running even from the command line. Here are two of the complaints I get from the Clang compiler when I use the command: clang -S -emit-llvm -o sum.ll -x cl sum.c

/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/clang/6.0/include/mmintrin.h:82:43: error:

      invalid conversion between vector type '__v4hi' and integer type '__m64'

      (aka 'long long') of different size

    return (__m64)__builtin_ia32_packuswb((__v4hi)__m1, (__v4hi)__m2);

                                          ^~~~~~~~~~~~

/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/clang/6.0/include/mmintrin.h:85:1: error:

      OpenCL does not support the 'static' storage class specifier

static __inline__ __m64 __attribute__((__always_inline__, __nodebug__))

Am I supposed to build the kernel “sum.cl” first or what? Thank you for your patience.

                                                  1. @

@Edit: Upon further search I find that the "static" complaint is coming from a file that is NOT part of the MTGP software but rather a file that is part of GCC. Exactly why Clang is using it is way above my head.

0 Kudos
Reply
titanius
Adept II

Re: Random numbers in OpenCL

i had good success with Mersenne Twister for Graphic Processors (MTGP)

and Tiny Mersenne Twister (TinyMT)

and TinyMT doesnot use that much memory.

if you really want a very simple random number generator (there are some issues w.r.t. correlation between some values How to get a "random" number in OpenCL - Stack Overflow)

Random Number Generators (using the 16807)

float get_random_0_1_ul (unsigned int* seed) //uniform between 0-1

{

  *seed = ((*seed) * 16807 ) % 2147483647;

  return  (float)(*seed) * 4.6566129e-10; //(4.6566129e-10 = 1/(2^31-1) = 1/2147483647)

}

0 Kudos
Reply
ddemidov
Adept II

Re: Random numbers in OpenCL

I think you need to use libc++ with clang on MacOS. This works for me (although I am on linux):

clang++ -o pi pi.cpp -std=c++11 -stdlib=libc++ -lc++abi -I${VEXCL_ROOT} -lOpenCL -lboost_system
0 Kudos
Reply
rsacker
Journeyman III

Re: Random numbers in OpenCL

#########################

Thank you titanius,

That looks like a variation of the Park Miller generator. I’ll have to use that one and the integer version until something more exotic comes along that I can implement. For that reason I will leave the question marked as “unanswered” for now. Thanks again, Bob

0 Kudos
Reply