将内核链接在一起 [英] Link kernels together

查看:119
本文介绍了将内核链接在一起的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在.cu文件中有一个CUDA内核,在另一个.cu文件中有另一个CUDA内核。我知道通过动态并行性,我可以从父内核调用另一个CUDA内核,但是我想知道是否有任何方法可以将子内核驻留在另一个.cu文件中。

I have a CUDA kernel in a .cu file and another CUDA kernel in another .cu file. I know that with dynamic parallelism I can call another CUDA kernel from a parent kernel but I'd like to know if there's any way to do this with a child kernel residing in another .cu file.

推荐答案

是的。

关键是使用带有设备代码链接的单独编译,这是 nvcc可用。由于使用 dynamic,这已经是必需的并行性,这里确实没有什么新东西。

The key is to use separate compilation with device code linking, which is available with nvcc. Since this is already required for usage of dynamic parallelism, there's really nothing new here.

这里有个简单的例子:

ch_kernel.cu :

ch_kernel.cu:

#include <stdio.h>

__global__ void ch_kernel(){

  printf("hello from child kernel\n");
}

main.cu:

#include <stdio.h>

extern __global__ void ch_kernel();

__global__ void kernel(){

  ch_kernel<<<1,1>>>();
}

int main(){

  kernel<<<1,1>>>();
  cudaDeviceSynchronize();
}

编译为:

nvcc -arch=sm_35 -rdc=true -o test ch_kernel.cu main.cu -lcudadevrt

这篇关于将内核链接在一起的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆