Random data inside device codes?

Hi all,

I would like to ask for your suggestions on generating random data inside device function codes.

I need to convert this function from C++ to device function. The function is something like this:

void MyFunction()
{

while ( value > threshold )
{
value1 = rand();
value2 = rand();
}
}

However, I couldn’t figure out how to get a lot of random data inside device codes. I’ve checked the sample codes using curand library as listed here: https://developer.nvidia.com/curand and tested in my codes. It seems that I keep getting same “random data” in some of the threads.

Could any one give any hints? Thanks a lot!

Have you tried Mersenne Twister sample in CUDA SDK? I hope it is still there.
Also, You can always generate random data in CPU and copy them over to GPU.
You can consider overlapping that time with other kernel executions…

Hi Sarnath,

Thanks for your reply! I just took a look at the Mersenne Twister sample you mentioned. If I understand the codes correctly, it generates a batch of random numbers in the device memory.

I think it is possible to get different random numbers if I use the thread index as an index. But I’m still not sure how to get different numbers in the while loop, as I don’t know how many random numbers I actually need. But I will try out using this sample first. Thanks!

I guess CURAND is the way to go.

I’ve had positive experiences with this LIB.

I use my own implementation based on infromation from a chapter of GPU Gems 3

MK