You do this essentially the same as with regular cpp files / modules. In C ++, you usually don't include one .cpp file in another if you want to access functions from another file. You include headers that usually only contain function prototypes.
Here is one example:
test.h:
void my_cuda_func();
main.cpp:
#include <stdio.h>
#include "test.h"
int main(){
my_cuda_func();
return 0;
}
test.cu:
#include <stdio.h>
#include "test.h"
__global__ void my_kernel(){
printf("Hello!\n");
}
void my_cuda_func(){
my_kernel<<<1,1>>>();
cudaDeviceSynchronize();
}
:
g++ -c main.cpp
nvcc -arch=sm_20 -c test.cu
g++ -L/usr/local/cuda/lib64 -lcudart -o test main.o test.o
, . C ++, . -, nvcc g++ ( .cu ). , GPU (, ),
.
, , , , , :
test.h:
__global__ void my_kernel();
main.cu:
#include <stdio.h>
#include "test.h"
int main(){
my_kernel<<<1,1>>>();
cudaDeviceSynchronize();
return 0;
}
test.cu:
#include <stdio.h>
#include "test.h"
__global__ void my_kernel(){
printf("Hello!\n");
}
:
nvcc -arch=sm_20 -c main.cu
nvcc -arch=sm_20 -c test.cu
nvcc -arch=sm_20 -o test main.o test.o