0

I'm currently working with ManagedCuda, and want to generate random numbers on the device. However I can't seem to find a simple example how to do this (browsing through objects in the ManagedCuda.CudaRand namespace and comparing with the C++ equivalent doesn't get me any further).

Actual question: How can I generate random numbers in a kernel when using managedCuda instead of the regular C++ API?

MarijnS95
  • 4,703
  • 3
  • 23
  • 47

1 Answers1

2

As it seems, you only want to use the device side API of CURAND, you will be then entirely independent of managedCuda: All you need to do in managedCuda is to allocate a large enough chunk of memory to save the current curandStates. You don’t even need a reference to managedCuda's CudaRand.dll.

Then you create an init kernel that calls for each thread curand_init() and then in your actual kernel you use curand_normal() or any of the other rand-functions. A step-by-step example is given in the curand manual in chapter 3.6.

kunzmi
  • 1,024
  • 1
  • 6
  • 8
  • Thanks, I'll try that later. However, guessing from the example the ammount of memory allocated is determined by the size of a curandState struct. There's probably no way to figure that out through managedCuda? (other than getting the sizeof on the device, storing that in device memory and querying from it in C#) – MarijnS95 Feb 19 '16 at 15:58
  • Apparently it's 48 bytes (in CUDA 7.5), figured that out with aformentioned sizeof method. – MarijnS95 Feb 19 '16 at 16:15
  • Thanks a bunch! It was indeed as easy as allocating enough memory (kernelIterations * 48 bytes), setting the global (`__device__ curandState* randomState;`) to point to that data, calling a setup kernel to set it up (`curand_init(1234, i, 0, &randomState[i]);`) and then generate random floats (using `float f = curand_uniform(&randomState[i]);`). – MarijnS95 Feb 19 '16 at 17:08
  • I've been trying to get this to work, to no avail. Are there any examples out there that I can actually load into Visual Studio with ManagedCuda? – BJury May 12 '19 at 14:29