CUDA内核执行全链路拆解
推荐指数 65.0 NO. 013 · 2026.06.30
发布2026/06/29Score162Comments13
为什么值得看
从Host代码到GPU执行的完整流程解析,覆盖内存分配、数据传输、线程网格调度到实际指令执行。对写CUDA算子或做性能调优的工程师有直接参考价值,能帮你定位瓶颈到底在PCIe带宽、kernel launch还是内存对齐。
编辑判断
HN评论区有个关键细节被很多人忽略:现代CUDA驱动其实会缓存编译好的PTX,但首次launch的JIT编译延迟仍可达数十毫秒,这也是很多"第一个kernel特别慢"的根源。如果你在做推理服务冷启动优化,需要显式做warm-up launch或者直接用cuModuleLoad预加载cubin。
这篇文章没讲的是NVCC编译器的实际工作流——它先把CUDA C++转成PTX(类似GPU汇编),再由driver JIT成特定架构的SASS。这意味着你编译时指定的-arch参数直接影响可移植性和性能,比如sm_80的代码在Hopper上能跑但会用兼容模式,损失部分新指令加速。做跨代GPU部署的团队建议研究一下fatbinary机制。
社区反馈
意见分歧 13 条评论
核心争论:CUDA driver API vs runtime API 之争,及 kernel 优化服务是否会被 AI 自动化取代
The hardware has some open documentation. You don't actually need to read the kernel source to find some of the method documentation or qmd formats. See https://github.com/NVIDIA/open-gpu-doc/blob/master/classes/c...
First - nice writeup which goes into a lot of nooks and crannies. That said, a lot of the user-space "voodoo" is gone if you don't go through CUDA's "runtime API". If you use the driver API, take your kernel source as a string and compile it with NVIDIA's run-time compiler, you'll have better visibi
I like the driver API because it allows treating Cuda kernels like hot-reloadable shaders. It's fun to develop while being able to change the code at runtime.