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
  • 18,949
  • 31
  • 125
  • 230
user570593
  • 3,210
  • 9
  • 49
  • 84

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
  • 120,849
  • 8
  • 160
  • 206
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,248
  • 14
  • 26