2013-04-10 268 views
1

我有一個CUDA C代碼,當我嘗試編譯它時,nvcc抱怨一個未定義的標識符錯誤,但它真的退出變量!nvcc:錯誤:標識符未定義,但它的定義

extern "C" 
void vRand3f_cuda (vec3f *d_v, int n) 
{ 

    curandState *d_states; 
    CUDA_CALL (cudaMalloc ((void **)&d_states, n * sizeof (curandState))); 

    dim3 gDim (1); 
    dim3 bDim (n); 

    set_seed<<<gDim, bDim>>> (d_states, time (NULL), n); 
    vRand3f_cuda_generate<<<gDim, bDim>>> (d_v, d_states, n); 

    CUDA_CALL (cudaFree (d_states)); 

} 

__global__ void set_seed (curandState *states, unsigned long seed, int n) 
{ 
    int idx = threadIdx.x + blockIdx.x * gridDim.x; 
    if (idx >= n) 
     return ; 

    curand_init (seed, idx, 0, &states[idx]); 

} 

__global__ void vRand3f_cuda_generate (vec3f *v, curandState *states, int n) 
{ 
    int idx = threadIdx.x + blockIdx.x * gridDim.x; 
    if (idx >= n) 

    curandState localS = states[idx]; 
    double s, x, y; 
    s = 2.; 

    while (s > 1.) 
    { 
     x = 2. - curand_uniform_double (&localS) - 1.; 
     y = 2. - curand_uniform_double (&localS) - 1.; 
     s = x * x + y * y; 
    } 

    v[idx].z = 1. - 2. * s; 
    s = 2. * sqrt (1. - s); 
    v[idx].x = s * x; 
    v[idx].y = s * y; 

    states[idx] = localS; 
} 

我使用下面的行編譯:

nvcc -m64 -g -G `pkg-config --cflags glib-2.0` -gencode arch=compute_20,code=sm_20 -gencode arch=compute_30,code=sm_30 -gencode arch=compute_35,code=sm_35 -I/Developer/NVIDIA/CUDA-5.0/include -I. -o vec3-cuda.o -c vec3-cuda.cu 

,我得到以下幾點:

vec3-cuda.cu(58): warning: variable "localS" was declared but never referenced

vec3-cuda.cu(64): error: identifier "localS" is undefined

vec3-cuda.cu(74): error: identifier "localS" is undefined

任何想法,這裏發生了什麼?我在OSX上使用CUDA 5。

謝謝。

回答

3

這是錯誤的:

if (idx >= n) 

curandState localS = states[idx]; 

你大概意思是這樣的:

if (idx >= n) return; 

curandState localS = states[idx]; 

正如你這寫的,如果子句中定義的變量的作用域是局部的如果子句。既然你從不在if子句中引用它,你會得到第一個警告。在其他地方,當您嘗試將其引用到if-子句的範圍之外時,您會遇到錯誤。

+0

哦!,你說得對,我忘記了'回來'。 謝謝@Robert Crovella – crosvera 2013-04-10 03:41:55

相關問題