0

Hi I would like to separate some of my CUDA kernel functions in a separate file so that I can reuse them.

Let say I have two files:

  1. A.cu contains the reusable CUDA kernels.
  2. B.cu contains some kernels as well as the host function, where I would like to call some of the kernels from my A.cu file.

How can I do it?

Tae-Sung Shin
  • 20,215
  • 33
  • 138
  • 240
user570593
  • 3,420
  • 12
  • 56
  • 91

2 Answers2

3

For the case you have described, you can do this in a fashion nearly identical to how you would do it in C/C++. Here's a fully worked example:

$ cat B.cu
#include "myheader.h"

__global__ void kernel1(){
  printf("Hello 1\n");
}

int main(){

  kernel1<<<1,1>>>();
  cudaDeviceSynchronize();
  kernel2<<<1,1>>>();
  cudaDeviceSynchronize();
  return 0;
}

$ cat A.cu
#include "myheader.h"

__global__ void kernel2(){
  printf("Hello 2\n");
}

$ cat myheader.h
#include <stdio.h>
__global__ void kernel2();

$ nvcc -arch=sm_20 -o test A.cu B.cu
$ cuda-memcheck ./test
========= CUDA-MEMCHECK
Hello 1
Hello 2
========= ERROR SUMMARY: 0 errors
$
Robert Crovella
  • 143,785
  • 11
  • 213
  • 257
1

What you can do is put your kernels prototypes in a .cuh file, and then include it in your second file. Here is a way of organizing your CUDA code.

Community
  • 1
  • 1
Jeff Bencteux
  • 1,406
  • 16
  • 27