How to separate the kernel file CUDA with the main .cpp file

前端 未结 2 585
谎友^
谎友^ 2021-01-06 00:26

When I build the code with kernelAdd() function and main() function in the same file mainFunc.cu, it\'s ok.

But when I separate the kernelAdd() function in the kerne

2条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-01-06 01:18

    The only addition to C to make CUDA C is the triple angle-bracket syntax to launch a kernel (<<<>>>). Everything else uses existing C features. Specifying a function as __global__ will cause nvcc to compile it for the device and create the symbols etc. so that it can be called from the host.

    This means that:

    1. The device code (__global__ function etc.) must be in a .cu file.
    2. The host code that uses the <<<>>> syntax to launch a kernel must be in a .cu file.

    You can still have all your other host code in .cpp files, you just need to put a stub in the .cu file to call the kernel, e.g. void launch_add(...) { add<<<...>>>(...); }.

提交回复
热议问题