简体   繁体   中英

How to separate kernels from C file

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?

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
$

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM