怎么解决这个错error C2065:'blockIdx':undeclared identifier

hhsherrill 2009-10-26 02:38:02
我之前用MFC做了一个程序,现在想把其中一部分计算放到GPU上去做,我把cuda函数和kernel函数放在一个cu文件中,cuda函数调用kernel函数,然后工程中某个类的成员函数调用这个cuda函数,但是编译时一直有这个错error C2065:'blockIdx':undeclared identifier
网上各种配置版本我都试过了,这个错一直都在,跪求解决方法,已经困扰我很久了
...全文
905 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
Double_Lan_2975 2012-08-20
  • 打赏
  • 举报
回复
路径没配置对哦
fengye553 2012-02-20
  • 打赏
  • 举报
回复
1.确认编译器
2..cu不能在.cpp中包含
wanggangkahn 2010-04-26
  • 打赏
  • 举报
回复
有谁能说一下这个问题是怎么解决的吗?非常感谢~~~
hhsherrill 2009-11-16
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 l7331014 的回复:]
还有编译器要用nvcc.
[/Quote]

我是把主机端代码和设备端代码写在一个.cu文件中的,我感觉是没用nvcc编译,难道一定要分开写吗
该设置我都设置了,为什么不是用nvcc编译的呢
hhsherrill 2009-11-11
  • 打赏
  • 举报
回复
已经加了这三个了:
cuda.h
cutil.h
cuda_runtime.h
  • 打赏
  • 举报
回复
.cu中cuda的头文件include了吗?
hhsherrill 2009-11-11
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 l7331014 的回复:]
还有编译器要用nvcc.
[/Quote]

是kernel函数要在一个独立的.cu文件中吗?我把kernel函数和调用它的那个函数放在一个.cu文件中了,这样不行吗?
编译器用nvcc怎么设置,是不是就是命令行那块?
  • 打赏
  • 举报
回复
还有编译器要用nvcc.
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 hhsherrill 的回复:]
我讲错了不是在编译时 是在生成时
代码有点长
__global__ static void rayKernel(BYTE *datad,RGBA *colord,RGB *imgd,Vector raydir,int cy,Vector screen_ld,Vector X,Vector Y,double step,int line,int point,int sample,int byte)
{
    int i=blockIdx.x*blockDim.x+threadIdx.x;
    int j=blockIdx.y*blockDim.y+threadIdx.y;
    int offset=i*cy+j;
    (*(imgd+offset)).red=0;
    (*(imgd+offset)).green=0;
    (*(imgd+offset)).blue=0;
    float opacity=0.0f;
    //中间计算过程就省略了
    (*(imgd+offset)).red=r;
    (*(imgd+offset)).green=g;
    (*(imgd+offset)).blue=b;
}

extern "C" void
rayCUDA(int line,int point,int sample,int byte,BYTE *data,RGB *img,RGBA color[],Vector raydir,int cx,int cy,Vector X,Vector Y,double step)
{
    int nGPU = 0;
    if (!InitCUDA(&nGPU))
    {
        return ;
    }

    BYTE *datad;
    RGBA *colord;
    RGB *imgd;

    double lengthScreen;
    lengthScreen=sqrt(double(line*line+point*point+sample*sample));
    double dist=lengthScreen;

    Vector datacenter;
    datacenter.x=(line-1)/2;
    datacenter.y=(point-1)/2;
    datacenter.z=(sample-1)/2;

    Vector screencenter;
    screencenter=VecAdd(datacenter,VecMult(dist,raydir));
    Vector screen_ld;
    screen_ld=VecSub(screencenter,VecAdd(VecMult(cx/2,X),VecMult(cy/2,Y)));

    int sized=line*point*sample*byte;
    int sizei=cx*cy*sizeof(RGB);

    cudaMalloc(&datad,sized);
    cudaMemcpy(datad,data,sized,cudaMemcpyHostToDevice);
    cudaMalloc(&colord,256*sizeof(RGBA));
    cudaMemcpy(colord,(RGBA*)color,256*sizeof(RGBA),cudaMemcpyHostToDevice);
    cudaMalloc(&imgd,sizei);

    dim3 dimBlock(16,16);
    dim3 dimGrid((cx+15)/16,(cy+15)/16);
    rayKernel < < <dimGrid,dimBlock>>>(datad,colord,imgd,raydir,cy,screen_ld,X,Y,step,line,point,sample,byte);

    cudaMemcpy(img,imgd,sizei,cudaMemcpyDeviceToHost);
    cudaFree(datad);
    cudaFree(colord);
    cudaFree(imgd);
}

类成员函数就只是调用了一下这个rayCUDA函数

我才接触CUDA 不知道是不是这样写的
[/Quote]

要在一个独立的.cu文件中.
lood339 2009-11-02
  • 打赏
  • 举报
回复
你是否在.c 或.cpp中用了#include " xxx.cu"
这样是不对的,会把.cu中的代码包含在cpp中,然后用cpp的编译器编译.cu文件,这样就出现了语法错误
hhsherrill 2009-10-26
  • 打赏
  • 举报
回复
我讲错了不是在编译时 是在生成时
代码有点长
__global__ static void rayKernel(BYTE *datad,RGBA *colord,RGB *imgd,Vector raydir,int cy,Vector screen_ld,Vector X,Vector Y,double step,int line,int point,int sample,int byte)
{
int i=blockIdx.x*blockDim.x+threadIdx.x;
int j=blockIdx.y*blockDim.y+threadIdx.y;
int offset=i*cy+j;
(*(imgd+offset)).red=0;
(*(imgd+offset)).green=0;
(*(imgd+offset)).blue=0;
float opacity=0.0f;
//中间计算过程就省略了
(*(imgd+offset)).red=r;
(*(imgd+offset)).green=g;
(*(imgd+offset)).blue=b;
}

extern "C" void
rayCUDA(int line,int point,int sample,int byte,BYTE *data,RGB *img,RGBA color[],Vector raydir,int cx,int cy,Vector X,Vector Y,double step)
{
int nGPU = 0;
if (!InitCUDA(&nGPU))
{
return ;
}

BYTE *datad;
RGBA *colord;
RGB *imgd;

double lengthScreen;
lengthScreen=sqrt(double(line*line+point*point+sample*sample));
double dist=lengthScreen;

Vector datacenter;
datacenter.x=(line-1)/2;
datacenter.y=(point-1)/2;
datacenter.z=(sample-1)/2;

Vector screencenter;
screencenter=VecAdd(datacenter,VecMult(dist,raydir));
Vector screen_ld;
screen_ld=VecSub(screencenter,VecAdd(VecMult(cx/2,X),VecMult(cy/2,Y)));

int sized=line*point*sample*byte;
int sizei=cx*cy*sizeof(RGB);

cudaMalloc(&datad,sized);
cudaMemcpy(datad,data,sized,cudaMemcpyHostToDevice);
cudaMalloc(&colord,256*sizeof(RGBA));
cudaMemcpy(colord,(RGBA*)color,256*sizeof(RGBA),cudaMemcpyHostToDevice);
cudaMalloc(&imgd,sizei);

dim3 dimBlock(16,16);
dim3 dimGrid((cx+15)/16,(cy+15)/16);
rayKernel<<<dimGrid,dimBlock>>>(datad,colord,imgd,raydir,cy,screen_ld,X,Y,step,line,point,sample,byte);

cudaMemcpy(img,imgd,sizei,cudaMemcpyDeviceToHost);
cudaFree(datad);
cudaFree(colord);
cudaFree(imgd);
}

类成员函数就只是调用了一下这个rayCUDA函数

我才接触CUDA 不知道是不是这样写的
tossense 2009-10-26
  • 打赏
  • 举报
回复
贴代码?
hhsherrill 2009-10-26
  • 打赏
  • 举报
回复
我觉得正确了啊
C:\CUDA\include
C:Documents and Settings\....\SDK\..\common\inc
tossense 2009-10-26
  • 打赏
  • 举报
回复
include 路径确定设置正确了?
hhsherrill 2009-10-26
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 tossense 的回复:]
我个人猜测是cu文件的编译规则调用了c++的,没用cuda build rule
在工程,右键里选择custom build rules,然后导入cuda.rules文件,为cu文件添加规则
cuda.rules在sdk目录\C\common文件夹下
[/Quote]
这个也加了
tossense 2009-10-26
  • 打赏
  • 举报
回复
我个人猜测是cu文件的编译规则调用了c++的,没用cuda build rule
在工程,右键里选择custom build rules,然后导入cuda.rules文件,为cu文件添加规则
cuda.rules在sdk目录\C\common文件夹下

581

社区成员

发帖
与我相关
我的任务
社区描述
CUDA™是一种由NVIDIA推出的通用并行计算架构,该架构使GPU能够解决复杂的计算问题。 它包含了CUDA指令集架构(ISA)以及GPU内部的并行计算引擎。
社区管理员
  • CUDA编程社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧