0Pricing
CUDA Academy · 课时

常量内存及其缓存

低成本广播只读数值

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

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

A Space for Read-Only Values

Constant memory is a small region built for data that every thread reads but none of them ever writes during a kernel.

Declaring It

You mark a global-scope array with the __constant__ qualifier. It lives outside any function, visible to all your kernels.

__constant__ float weights[256];

It Is Genuinely Small

Total constant memory is only 64 KB on typical GPUs. It is meant for coefficients and parameters, not for large datasets.

Filling It from the Host

You write to constant memory from the CPU using cudaMemcpyToSymbol, since kernels themselves cannot modify it.

cudaMemcpyToSymbol(weights, h_w,
    256 * sizeof(float));

Backed by a Special Cache

Reads flow through a dedicated constant cache on each multiprocessor. A cached value comes back almost as fast as a register.

The Broadcast Superpower

When a whole warp reads the same address, the hardware does one fetch and broadcasts it to all 32 threads in a single step. 📡

Same Address Is the Key

The win depends on uniform access. If threads in a warp read different constant addresses, those reads serialize and the speedup is lost.

Reading It Looks Normal

Inside a kernel you read constant memory like any array. The compiler quietly routes the access through the fast constant cache.

__global__ void k(float *out, int i) {
    out[i] = weights[0] * 2.0f;
}

A Perfect Fit for Filters

Convolution kernels and lookup tables shine here, because every thread reuses the same small set of coefficients over and over.

Set Once, Reuse Often

You typically upload to constant memory a single time, then launch many kernels that all read those unchanging values cheaply.

When Not to Use It

Skip constant memory if your data is large or if threads read scattered addresses. In those cases plain global memory serves you better.

Quick Check

When does constant memory give its biggest speedup?

Recap: Small, Cached, Broadcast

You learned constant memory is a tiny read-only space whose cache broadcasts uniform reads to a whole warp. Use it for small, shared values. ✨

常见问题解答

「常量内存及其缓存」课时是免费的吗?

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

「常量内存及其缓存」这节课中我会学到什么?

低成本广播只读数值 你通过在浏览器中直接运行的动手代码来练习 CUDA Academy,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 CUDA Academy 需要有经验吗?

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

「常量内存及其缓存」课时需要多长时间?

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

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

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

此课程中的所有课时

  1. 寄存器与本地内存
  2. 全局内存的权衡
  3. 常量内存及其缓存
  4. 建立层次结构的心智模型
← 返回 CUDA Academy