How to generate random number inside pyCUDA kernel?

前端 未结 2 608
北恋
北恋 2021-01-13 16:45

I am using pyCUDA for CUDA programming. I need to use random number inside kernel function. CURAND library doesn\'t work inside it (pyCUDA). Since, there is lot of work to b

2条回答
  •  时光取名叫无心
    2021-01-13 17:50

    There is one problem I have with the accepted answer. We have a name mangling there which is sort of nasty (these _Z10initkerneli and _Z14randfillkernelPfi). To avoid that we can wrap the code in the extern "C" {...} clause manually.

    code = """
        #include 
    
        const int nstates = %(NGENERATORS)s;
        __device__ curandState_t* states[nstates];
        extern "C" {
    
        __global__ void initkernel(int seed)
        { .... }
    
        __global__ void randfillkernel(float *values, int N)
        { .... }
        }
    """
    

    Then the code is still compiled with no_extern_c=True:

    mod = SourceModule(code % { "NGENERATORS" : N }, no_extern_c=True)
    

    and this should work with

    init_func = mod.get_function("initkernel")
    fill_func = mod.get_function("randfillkernel")
    

    Hope that helps.

提交回复
热议问题