0Pricing
CUDA Academy · 课时

测量您的首次加速效果

在相同任务上测量 GPU 与 CPU 的性能

测量您的首次加速效果 是 CoddyKit 上的免费 CUDA Academy 课时。 这是第 4 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 CUDA Academy 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 CUDA Academy 课程共包含 4 节课。

本课时的部分内容尚未翻译,以英文显示。

Now Measure the Win

You have a correct kernel, so the fun question is how much faster it is than the CPU. Measuring turns a guess into a real number. ⏱️

Time the CPU Baseline

Start with a fair baseline: time the same add running in a plain CPU loop. You need something to compare the GPU against.

Don't Time with the Clock Wrong

Avoid timing across a cudaMemcpy you forgot to wait for. The launch is asynchronous, so naive timers can report nonsense.

Use CUDA Events

The right tool is a pair of cudaEvent objects. They record GPU timestamps directly on the device for accurate kernel timing.

cudaEvent_t start, stop;
cudaEventCreate(&start);
cudaEventCreate(&stop);

Bracket the Kernel

Record start just before the launch and stop just after. The events queue in the stream alongside your kernel.

cudaEventRecord(start);
vecAdd<<<blocks, threads>>>(d_A, d_B, d_C, n);
cudaEventRecord(stop);

Wait, Then Read

Call cudaEventSynchronize on stop so the CPU waits for the GPU. Only then is the elapsed time ready to read.

cudaEventSynchronize(stop);
float ms = 0;
cudaEventElapsedTime(&ms, start, stop);

Warm Up First

The very first launch pays one-time setup costs. Run a throwaway warm-up launch before timing so those costs do not skew your number.

Average Several Runs

A single sample is noisy, so time the kernel a few times and take the average. Stable numbers make speedup claims trustworthy.

Count the Copies

Be honest about what you measure. Kernel-only time looks amazing, but real speedup must include the PCIe transfers too.

Compute the Speedup

Speedup is simply CPU time divided by GPU time. A small array may even be slower on the GPU once copies are counted.

float speedup = cpu_ms / gpu_ms;

Bigger Arrays Win More

The GPU shines when there is enough work to hide transfer cost. Grow n and watch the speedup climb as parallelism dominates. 📈

Quick Check

Which tool gives accurate GPU kernel timing?

Recap

You measured your first speedup with CUDA events: warm up, bracket the kernel, sync, and divide CPU by GPU time. Bigger problems, bigger wins. 🎉

常见问题解答

「测量您的首次加速效果」课时是免费的吗?

是的 — 「测量您的首次加速效果」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 CUDA Academy 课程的其余内容,请升级到 CoddyKit PRO。 CUDA Academy 课程共包含 4 节课。

「测量您的首次加速效果」这节课中我会学到什么?

在相同任务上测量 GPU 与 CPU 的性能 你通过在浏览器中直接运行的动手代码来练习 CUDA Academy,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 CUDA Academy 需要有经验吗?

无需任何先前经验。CoddyKit 上的 CUDA Academy 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 4 节课,共 4 节。

「测量您的首次加速效果」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 CUDA Academy 课中编写并运行代码吗?

能。每节 CUDA Academy 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. 向量加法内核
  2. 连接主机端代码
  3. 在 CPU 上验证结果
  4. 测量您的首次加速效果
← 返回 CUDA Academy