Как инициализировать и запустить Mersenne Twister Random Generator внутри ядер в PyCuda - PullRequest
0 голосов
/ 06 октября 2019

Я хотел использовать генератор случайных чисел Мерсенна Твистера в ядрах pyCuda для численного эксперимента. Через Интернет я не нашел простых примеров того, как это сделать, поэтому я попытался создать что-то из документации Cuda и примеров pyCuda (код pyCuda ниже).

Как это можно сделать правильно?

Спасибо.

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".
]
...