简体   繁体   English

如何从C文件中分离内核

[英]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. 嗨,我想将我的一些CUDA内核函数放在一个单独的文件中,以便我可以重复使用它们。

Let say I have two files: 假设我有两个文件:

  1. A.cu contains the reusable CUDA kernels. A.cu包含可重用的CUDA内核。
  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. B.cu包含一些内核以及宿主函数,在这里我想从A.cu文件中调用一些内核。

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++. 对于您描述的情况,您可以采用与在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. 您可以做的是将内核原型放入.cuh文件中,然后将其包含在第二个文件中。 Here is a way of organizing your CUDA code. 是一种组织CUDA代码的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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