How to separate CUDA core file with main .cpp file

When I create the code with the kernelAdd () and main () function in the same mainFunc.cu file, this is normal.

But when I separate the kernelAdd () function in the kernelAdd.cu file and the main file in the main.cpp file, it builds with two errors:

"C2065:" add ": undeclared identifier"

and "error C2059: syntax error: '<'"

I built them in Visual Studio 2008 and Cuda v5.0.

And how can I fix his mistakes?

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; } 
+4
source share
2 answers

The only addition to C is that CUDA C be the triple-angle bracket syntax for starting the kernel ( <<<>>> ). Everything else uses the existing C functions. Specifying the function as __global__ will cause nvcc to compile it for the device and create characters, etc., so that it can be called from the host.

It means that:

  • The device code ( __global__ , etc.) must be in the .cu file.
  • The host code that uses the syntax <<<>>> to start the kernel must be in the .cu file.

You still have all the other host codes in .cpp files, you just need to put a stub in the .cu file to call the kernel, for example. void launch_add(...) { add<<<...>>>(...); } void launch_add(...) { add<<<...>>>(...); } .

+2
source

error C2065: 'add': undeclared identifier

This error is not related to CUDA. The add function belongs to one compilation unit ( kernelAdd.cu ), and the other compilation unit ( mainFunc.cpp ) knows nothing about this. To provide this information, you must create an additional kernelAdd.h header kernelAdd.h with a function declaration:

 __global__ void add(int a, int b, int *c); 

And include it in mainFunc.cpp :

 #include "kernelAdd.h" 

Each cu or cpp file is compiled separately and knows only about the functions that it sees in the header files that it includes.

error C2059: syntax error: '<'

Now I'm guessing here (don't check VS or even Windows), but it looks like VS selects a compiler for each of the files in the project based on its extension. Thus, mainFunc.cpp compiled using the general C ++ compiler, but the syntax <<<>>> for kernel calls does not apply to standard C ++ - it is from CUDA. All CUDA syntax should be used only in files that are going to be compiled using nvcc .

Thus, one way to solve your problem is to rename mainFunc.cpp to mainFunc.cu . You can still save your main .cpp file, of course, but then you have to move the kernel call to some normal C ++ function inside the cu file and put it in the standard C ++ header file that your .cpp file will include.

+5
source

Source: https://habr.com/ru/post/1496710/


All Articles