我们正带领大家开始阅读英文的《CUDA C Programming Guide》,今天是第72天,我们正在讲解CUDA 动态并行,希望在接下来的28天里,您可以学习到原汁原味的CUDA,同时能养成英文阅读的习惯。
关注微信公众号,查看历史信息,可以看到之前的阅读
本文共计219字,阅读时间15分钟
DAY65:阅读Device-Side Kernel Launch
DAY67:阅读阅读Events、Synchronization和Device Management
DAY69:阅读API Errors and Launch Failures
DAY71:阅读Device-side Launch from PTX
Similar to the host-side runtime API, prototypes for the CUDA device runtime API are included automatically during program compilation. There is no need to includecuda_device_runtime_api.h explicitly.
CUDA programs are automatically linked with the host runtime library when compiled with nvcc, but the device runtime is shipped as a static library which must explicitly be linked with a program which wishes to use it.
The device runtime is offered as a static library (cudadevrt.lib on Windows, libcudadevrt.a under Linux and MacOS), against which a GPU application that uses the device runtime must be linked. Linking of device libraries can be accomplished through nvcc and/or nvlink. Two simple examples are shown below.
A device runtime program may be compiled and linked in a single step, if all required source files can be specified from the command line:
$ nvcc -arch=sm_35 -rdc=true hello_world.cu -o hello -lcudadevrt
It is also possible to compile CUDA .cu source files first to object files, and then link these together in a two-stage process:
$ nvcc -arch=sm_35 -dc hello_world.cu -o hello_world.o
$ nvcc -arch=sm_35 -rdc=true hello_world.o -o hello -lcudadevrt
Please see the Using Separate Compilation section of The CUDA Driver Compiler NVCC guide for more details.
本文备注/经验分享:
主要是说, 用户在使用动态并行的时候, 需要在头文件中包含哪些头文件(设备端Runtime的头文件), 以及, 连接的时候需要使用哪些库(设备端的库), 还有连接的时候所需要的特殊参数(rdc, Relocatable Device Code)。因为CUDA C相当于C语言的扩展, 所以常见的C语言对一些库的用法, 这里也适合.也就是说, 你需要额外指定一些头文件, 再指定一些链接的库这两个步骤, 部分特别的库还需要特别的编译参数.但CUDA是一种易用为主的语言, 在现在为主的开发包版本中(CUDA 9.2),该头文件和该库都会被自动被链接上去.也就是说, 本章节给出的头文件和库均不需要你记忆和指定了.全部现在都已经是自动的了.唯一需要注意的则是rdc选项(设备端代码重定位), 这个还是需要的.也就是这是为了的本章节的还需要的内容(指定在你的命令行上). 这是对于命令行用户说的. 对于常见的Windows上的VS用户, 如果是使用的默认的安装CUDA时候的CUDA自定义模板,直接在Solution Explorer里面, 右键属性中, 选择rdc打开即可,也就是说, VS点击几下鼠标, 就可以打开开发环境的动态并行支持了. 这个非常简单. 这就是本章节的全部内容.常见的使用动态并行不能, VS用户都是忘记打开了这个选项, 打开即可.需要说明的是, 链接动态并行的库会可能带来额外的性能负担. 这个以后再说.以及, 在你的代码调用动态并行的相关东西, 例如动态并行所需要的设备端库的一些函数的时候,实际在生成PTX这一步, 都是.extern或者.weak形式的占位的, 这里看不到这些代码.
有不明白的地方,请在本文后留言
或者在我们的技术论坛bbs.gpuworld.cn上发帖