【问题标题】:nvcc: error: identifier undefined, but it's definednvcc:错误:标识符未定义,但已定义
【发布时间】:2013-04-01 17:42:45
【问题描述】:

我有一个 CUDA C 代码,当我尝试编译它时,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):警告:变量“localS”已声明但从未声明 引用

vec3-cuda.cu(64):错误:标识符“localS”未定义

vec3-cuda.cu(74):错误:标识符“localS”未定义

知道这里发生了什么吗?我在 OSX 上使用 CUDA 5。

谢谢。

【问题讨论】:

    标签: cuda compiler-errors nvcc


    【解决方案1】:

    这是错误的:

    if (idx >= n)
    
    curandState localS = states[idx];
    

    你的意思可能是这样的:

    if (idx >= n) return;
    
    curandState localS = states[idx];
    

    正如您所写的,在 if 子句中定义的变量的范围是该 if 子句的本地变量。由于您从未在 if 子句中引用它,因此您会收到第一个警告。在其他地方,当您尝试在 if 子句范围之外引用它时会出错。

    【讨论】:

    • 哦!,你说得对,我忘记了'return'。谢谢@Robert Crovella
    猜你喜欢
    • 2014-06-05
    • 2015-05-12
    • 2012-02-26
    • 2011-10-23
    • 1970-01-01
    • 1970-01-01
    • 2020-10-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多