0Pricing
CUDA Academy · 课时

避免存储体冲突

了解填充为何能够加快共享内存访问

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

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

Shared Memory Has Banks

Shared memory is split into 32 banks, one for each thread in a warp. Spreading accesses across them lets all 32 threads read at full speed.

How Addresses Map

Consecutive 4-byte words land in consecutive banks, wrapping around after 32. So word 0 is bank 0, word 32 is bank 0 again.

What a Bank Conflict Is

A bank conflict happens when two threads in a warp hit the same bank but different words. The hardware must serialize those accesses.

Serialization Costs Speed

An N-way conflict turns one fast access into N slow ones. A 32-way conflict can make shared memory as slow as if it were single-file. 🐢

The Happy Case: Stride One

When each thread reads tile[threadIdx.x], every thread hits a distinct bank. That is conflict-free and runs at full bandwidth.

float v = tile[threadIdx.x];

The Trap: Stride of 32

Reading with a stride of 32 sends every thread to the same bank. That is the worst case, a full 32-way conflict.

float v = tile[threadIdx.x * 32];

Even Strides Hurt Too

Any even stride that shares a factor with 32 causes partial conflicts. A stride of 2, for example, gives a 2-way conflict across the warp.

The Broadcast Exception

Good news: if all threads read the same address, the hardware broadcasts it in one shot. Same word is fine, only same bank with different words conflicts.

Padding to the Rescue

For 2D tiles, add one extra column with +1 padding. This shifts each row so column accesses no longer all land in one bank.

__shared__ float tile[32][33];

Why +1 Works

The extra column makes the row length coprime with 32. Now stepping down a column visits a different bank each time, killing the conflict.

Profile, Do Not Guess

Bank conflicts are invisible in source code. Use Nsight Compute to measure shared-memory conflicts before spending effort fixing them.

Quick Check

Let us check what makes shared access fast.

Recap

You learned that shared memory has 32 banks, that same-bank different-word access serializes, and that +1 padding fixes column conflicts. Next: dynamic shared memory. 🎯

常见问题解答

「避免存储体冲突」课时是免费的吗?

是的 — 「避免存储体冲突」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 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. 声明 __shared__ 数组
  2. 使用 __syncthreads 同步
  3. 避免存储体冲突
  4. 动态共享内存
← 返回 CUDA Academy