问题描述
当我在同一个文件mainFunc.cu中使用kernelAdd()函数和main()函数构建代码时,可以。
但是当我分离kernelAdd ()函数在kernelAdd.cu文件和主文件在main.cpp文件,它是与2错误:
错误C2065:'add':undeclared标识符
和error C2059:syntax error:'<''
Studio 2008和Cuda v5.0。
如何更正错误?
谢谢! p>
kernelAdd.cu
__ global__ void add(int a,int b, int * c){
* c = a + b;
}
mainFunc.cpp
#includecuda_runtime.h
#includedevice_launch_parameters.h
#include< stdio.h>
#include< conio.h>
int main(void){
int c;
int * devC;
cudaMalloc((void **)& devC,sizeof(int));
add<<< 1,1>>>(2,7,devC);
cudaMemcpy(& c,devC,sizeof(int),cudaMemcpyDeviceToHost);
printf(2 + 7 =%d\\\
,c);
cudaFree(devC);
getch();
return 0;
}
make CUDA C是启动内核(<<<<>>
)的三角括号语法。一切都使用现有的C功能。将函数指定为 __ global __
将使nvcc为设备编译它,并创建符号等,以便可以从主机调用。
这意味着:
- 设备代码(
__ global __
函数等)必须在.cu文件中。 - 使用
<< / code>启动内核的语法必须在.cu文件中。
您仍然可以拥有所有其他主机代码在.cpp文件中,你只需要在.cu文件中放一个存根来调用内核,例如
void launch_add(...){add<<< ...>>>(...) }
。
When I build the code with kernelAdd() function and main() function in the same file mainFunc.cu, it's ok.
But when I separate the kernelAdd() function in the kernelAdd.cu file and the main file in main.cpp file, it's built with the 2 errors:
"error C2065: 'add' : undeclared identifier"
and "error C2059: syntax error : '<'"
I built them in Visual Studio 2008 and Cuda v5.0.
And how do I correct its errors?
Thanks!
kernelAdd.cu
__global__ void add(int a, int b, int *c) {
*c = a + b;
}
mainFunc.cpp
#include "cuda_runtime.h"
#include "device_launch_parameters.h"
#include <stdio.h>
#include <conio.h>
int main(void) {
int c;
int *devC;
cudaMalloc((void**) &devC, sizeof(int));
add<<<1,1>>>(2,7,devC);
cudaMemcpy(&c, devC, sizeof(int), cudaMemcpyDeviceToHost);
printf("2+7=%d\n", c);
cudaFree(devC);
getch();
return 0;
}
解决方案 The only addition to C to make CUDA C is the triple angle-bracket syntax to launch a kernel (<<<>>>
). Everything else uses existing C features. Specifying a function as __global__
will cause nvcc to compile it for the device and create the symbols etc. so that it can be called from the host.
This means that:
- The device code (
__global__
function etc.) must be in a .cu file. - The host code that uses the
<<<>>>
syntax to launch a kernel must be in a .cu file.
You can still have all your other host code in .cpp files, you just need to put a stub in the .cu file to call the kernel, e.g. void launch_add(...) { add<<<...>>>(...); }
.
这篇关于如何将内核文件CUDA与主.cpp文件分离的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!