连接主机端代码
分配、复制、启动、复制回来、释放
连接主机端代码 是 CoddyKit 上的免费 CUDA Academy 课时。 这是第 2 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 CUDA Academy 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 CUDA Academy 课程共包含 4 节课。
本课时的部分内容尚未翻译,以英文显示。
The Host Has a Job Too
The kernel does the math, but the host CPU sets everything up. Its job is allocate, copy in, launch, copy back, and free. 🧩
Two Sets of Pointers
You keep host pointers for CPU arrays and separate device pointers for GPU buffers. A common habit is naming them h_A and d_A.
float *h_A, *h_B, *h_C;
float *d_A, *d_B, *d_C;Fill the Input on the CPU
First prepare your data in normal host memory. Here you just initialize h_A and h_B with values the GPU will add later.
for (int i = 0; i < n; i++) {
h_A[i] = i; h_B[i] = 2 * i;
}Allocate on the Device
The GPU needs its own buffers, so call cudaMalloc for each array. Note the size is in bytes, computed as count times sizeof(float).
size_t bytes = n * sizeof(float);
cudaMalloc(&d_A, bytes);Copy Inputs Up
Move your input arrays to the GPU with cudaMemcpy and the HostToDevice direction. The kernel can only see data that lives on the device.
cudaMemcpy(d_A, h_A, bytes, cudaMemcpyHostToDevice);
cudaMemcpy(d_B, h_B, bytes, cudaMemcpyHostToDevice);Decide the Launch Shape
Pick a thread count per block, then compute how many blocks cover all elements. Rounding up guarantees every element gets a thread.
int threads = 256;
int blocks = (n + threads - 1) / threads;Launch the Kernel
Now fire the kernel with the triple-angle-bracket syntax, passing your device pointers. This call returns immediately while the GPU works.
vecAdd<<<blocks, threads>>>(d_A, d_B, d_C, n);Copy the Result Down
When the kernel finishes, bring C back with cudaMemcpyDeviceToHost. This copy also waits for the launch to complete first.
cudaMemcpy(h_C, d_C, bytes, cudaMemcpyDeviceToHost);Free Device Memory
GPU memory is not garbage collected, so release every buffer with cudaFree. Skipping this leaks memory that lasts until the process exits.
cudaFree(d_A); cudaFree(d_B); cudaFree(d_C);The Order Is Sacred
Always follow the same sequence: allocate, copy in, launch, copy back, free. Swap steps and the kernel reads stale or invalid data.
Host Code Is Plain C++
Notice the host side is ordinary C++ plus a handful of cuda calls. There is no special compiler magic beyond the kernel launch.
Quick Check
What must happen before you can launch the vecAdd kernel?
Recap
You wired the host side end to end: two pointer sets, cudaMalloc, copy up, launch, copy back, and cudaFree. The boilerplate that powers every kernel. ✅
常见问题解答
「连接主机端代码」课时是免费的吗?
是的 — 「连接主机端代码」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 CUDA Academy 课程的其余内容,请升级到 CoddyKit PRO。 CUDA Academy 课程共包含 4 节课。
「连接主机端代码」这节课中我会学到什么?
分配、复制、启动、复制回来、释放 你通过在浏览器中直接运行的动手代码来练习 CUDA Academy,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 CUDA Academy 需要有经验吗?
无需任何先前经验。CoddyKit 上的 CUDA Academy 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 2 节课,共 4 节。
「连接主机端代码」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 CUDA Academy 课中编写并运行代码吗?
能。每节 CUDA Academy 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。
此课程中的所有课时
- 向量加法内核
- 连接主机端代码
- 在 CPU 上验证结果
- 测量您的首次加速效果