MarvelousLim
MarvelousLim

Reputation: 1

How to initialize and run Mersenne Twister Random Generator inside kernels in PyCuda

I wanted to use Mersenne Twister random generator inside pyCuda kernels for numerical experiment. Via Internet I found no simple examples of how to do it, so, I tried to construct something from Cuda documentation and pyCuda examples (pyCuda code below).

How it can be done correctly?

Thank you.

code = """
    #include <curand_kernel.h>
    #include <curand_mtgp32_host.h>
    #include <curand_mtgp32dc_p_11213.h>

    const int nstates = %(NGENERATORS)s;

    __device__ curandStateMtgp32 *devMTGPStates[nstates];
    __device__ mtgp32_kernel_params *devKernelParams;

    curandMakeMTGP32Constants(mtgp32dc_params_fast_11213, devKernelParams);
    curandMakeMTGP32KernelState(devMTGPStates, mtgp32dc_params_fast_11213, devKernelParams, 64, %(seed)s);

    extern "C"
    {
        __global__ void generate_uniform(int N, int *result)
        {
            int tidx = threadIdx.x + blockIdx.x * blockDim.x;

            if (tidx < nstates) 
            {
                curandState_t s = *states[tidx];
                for(int i = tidx; i < N; i += blockDim.x * gridDim.x) 
                {
                    result[i] = curand_uniform(&s);
                }
                *states[tidx] = s;
            }
        }
    }
"""

seed = 0
N = 256 * 64
nvalues = int(10**3)
mod = SourceModule(code % { "NGENERATORS" : N, "seed": seed}, no_extern_c=True)
CompileError: nvcc compilation of C:\Users\limen\AppData\Local\Temp\tmpspxyn4h9\kernel.cu failed
[command: nvcc --cubin -arch sm_50 -m64 -Ic:\program files (x86)\microsoft visual studio\shared\anaconda3_64\lib\site-packages\pycuda\cuda kernel.cu]
[stdout:
kernel.cu
]
[stderr:
kernel.cu(10): error: this declaration has no storage class or type specifier

kernel.cu(10): error: declaration is incompatible with "curandStatus_t curandMakeMTGP32Constants(const mtgp32_params_fast_t *, mtgp32_kernel_params_t *)"
C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v10.1\bin/../include\curand_mtgp32_host.h(367): here

kernel.cu(10): error: a value of type "mtgp32_params_fast_t *" cannot be used to initialize an entity of type "int"

kernel.cu(10): error: expected a ")"

kernel.cu(11): error: this declaration has no storage class or type specifier

kernel.cu(11): error: declaration is incompatible with "curandStatus_t curandMakeMTGP32KernelState(curandStateMtgp32_t *, mtgp32_params_fast_t *, mtgp32_kernel_params_t *, int, unsigned long long)"
C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v10.1\bin/../include\curand_mtgp32_host.h(481): here

kernel.cu(11): error: a value of type "curandStateMtgp32 **" cannot be used to initialize an entity of type "int"

kernel.cu(11): error: expected a ")"

kernel.cu(21): error: identifier "states" is undefined

9 errors detected in the compilation of "C:/Users/limen/AppData/Local/Temp/tmpxft_00001aa8_00000000-10_kernel.cpp1.ii".
]

Upvotes: 0

Views: 266

Answers (1)

Saswat K. Levin
Saswat K. Levin

Reputation: 27

I did the following:

import pycuda.curandom
print(dir(pycuda.curandom))

This gives all attributes that are present in the Python module pycuda.curandom. There was no mention of any Mersenne-Twister(MT) or MT-based Pseudo Random Number Generator. This indicates that MT isn't implemented in PyCUDA.

Upvotes: 0

Related Questions