Link kernels together

故事扮演 提交于 2019-12-12 03:09:05

问题


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.


回答1:


Yes, you can.

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.

Here's a simple example:

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();
}

compile with:

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


来源:https://stackoverflow.com/questions/20636800/link-kernels-together

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!