Reputation: 45
I know the method of calling the .cu files from .c files. But now I want to call the .cu files from a C header file. Is it possible to do it ? If so how should I make the settings of my project ? please help.....
Upvotes: 2
Views: 8020
Reputation: 151799
Here's a worked 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;
}
build and test:
$ 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 are intending to include your file2.h
into a cpp file, you cannot call a cuda kernel directly from that header and use it in a cpp file. You must put a wrapper around the cuda kernel, and call the wrapper, as I have indicated. This is because your cpp file will get compiled by the host compiler, which doesn't know anything about cuda syntax (e.g. mykernel<<<1,1>>>();
)
Also, as indicated in the comments, it may make more sense to reserve the header file file2.h
just for needed prototypes, and put the actual function definition of myfunc
into a cpp file somewhere.
Upvotes: 9