How do I call a CUDA file from a C ++ header file?
Here's an example:
file1.h:
int hello();
file2.h:
#include <stdio.h>
#include "file1.h"
int myfunc(){
hello();
return 0;
}
file1.cu:
#include <stdio.h>
#include "file1.h"
__global__ void mykernel(){
printf("Hello from mykernel\n");
}
int hello(){
mykernel<<<1,1>>>();
cudaDeviceSynchronize();
return 0;
}
file2.cpp:
#include "file2.h"
int main(){
myfunc();
return 0;
}
assembly and testing:
$ nvcc -arch=sm_20 -c file1.cu
$ g++ -c file2.cpp
$ g++ -o test file1.o file2.o -L/usr/local/cuda/lib64 -lcudart
$ ./test
Hello from mykernel
$
Assuming you intend to include yours file2.h
in the cpp file, you cannot call the core cuda directly from that header and use it in the cpp file. You have to put a wrapper around the cuda kernel and invoke the wrapper as I pointed out. This is because your cpp file will be compiled by the host compiler, which knows nothing about the cuda syntax (for example mykernel<<<1,1>>>();
)
Also, as pointed out in the comments, it makes sense to reserve the header file file2.h
only for the prototypes needed and put the actual function definition myfunc
in a cpp file somewhere.
source to share