从c文件中调用cuda - 如何操作参数

时间:2014-02-14 13:26:01

标签: c cuda

我有一个cu文件:

__device__ float generate( curandState* globalState, int ind ) 
{
    curandState localState = globalState[ind];
    float RANDOM = curand_uniform( &localState );
    globalState[ind] = localState;
    return RANDOM;
}

__global__ void setup_kernel ( curandState * state, unsigned long seed )
{
    int id = threadIdx.x+blockIdx.x*blockDim.x;
    curand_init ( seed, id, 0, &state[id] );
}

__global__ void kernel(float* A,float *B, curandState* globalState, int Asize,int Bsize)
{
     // generate random numbers
     for(int i=0;i<Asize;i++){
       .....
     for (int i=0 ;i<Bsize;i++){
    float k = generate(globalState, i);
    B[i]=k;
    }
}

void kernel_wrapper(float* A,float* B, curandState* globalState, int Asize ,int Bsize)
{
....
    //create random states  
    curandState* devStates;
    cudaMalloc ( &devStates, N*sizeof( curandState ) );

    // setup seeds
    setup_kernel <<< 1, N >>> ( devStates,unsigned(time(NULL)) );

    // copy arrays from host to device
    cudaMemcpy(A_dev, A_host,Asize* sizeof(float), cudaMemcpyHostToDevice);
    cudaMemcpy(B_dev, B_host,Bsize* sizeof(float), cudaMemcpyHostToDevice);

    //generate random numbers
    kernel<<<1,1>>> (A_dev,B_dev, devStates, Asize , Bsize);

    // copy result from device to host
    cudaMemcpy(A_host, A_dev,Asize* sizeof(float), cudaMemcpyDeviceToHost);
    cudaMemcpy(B_host, B_dev,Bsize* sizeof(float), cudaMemcpyDeviceToHost);
 ...
}
  

现在,在c代码中:

...
extern void kernel_wrapper(float* A,float* B, curandState* globalState, int Asize ,int Bsize)
....
int main()
{
...
 kernel_wrapper(&A,&B, curandState* globalState,Asize ,Bsize)
    ....

我不确定如何处理参数curandState* globalState。 我该怎么用? 因为我没有使用任何

  

curandState

c代码中的

值。

1 个答案:

答案 0 :(得分:1)

[假设其他一切都正确]

看来你的kernel_wrapper根本不需要那个参数。 devStates 指针是指向setup_kernel()中生成的随机值种子的指针,该随机值稍后会传递到应用分布的kernel()

简短回答:从发布的代码中可以安全地删除globalState

中的kernel_wrapper参数