0Pricing
Mojo Academy · 课时

减少内存流量

让数据靠近计算过程

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

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

Why Memory Matters

Modern CPUs compute far faster than they can fetch data. Often a kernel waits on memory, not on the math itself.

What Is Memory Traffic?

Memory traffic is the total bytes your kernel reads and writes. Less traffic per result usually means a faster kernel.

Touch Data Once

Reading the same value many times wastes bandwidth. Load it once, do all the work, then reuse it from a register.

var x = a[i]
var y = x * x + x

Fuse Your Loops

Two separate loops over the same array read it twice. Fusing them into one pass reads each element only once.

for i in range(n):
    out[i] = a[i] * 2 + a[i]

Keep Values in Registers

A value held in a CPU register needs no memory access. Reuse intermediate results instead of writing them out and back.

Avoid Temp Buffers

Extra temporary arrays add both stores and loads. Skip them when you can and compute straight into the final output.

Stream Sequentially

Reading memory in order lets the CPU prefetch ahead. Jumping around defeats prefetching and stalls the loop.

for i in range(n):
    total += a[i]

Cache Lines Travel Together

Memory arrives in fixed-size cache lines. Using every byte of a line you fetched gives you free, already-loaded data.

Compute More per Byte

Arithmetic intensity is work done per byte loaded. Raising it means each fetched value earns more compute before you move on.

Write Once, If You Can

Stores cost bandwidth too. Accumulate in a local and write the final result once rather than updating memory repeatedly.

var acc = Float32(0)
for i in range(n):
    acc += a[i]
out[0] = acc

Less Traffic, More Speed

When the kernel waits on data, cutting reads and writes is the biggest win, often beating clever arithmetic tweaks.

Quick Check

Your kernel reads the same array in two separate loops. What single change cuts its memory traffic most?

Recap

Cut memory traffic by touching data once, fusing loops, reusing registers, streaming in order, and writing results just once. 💾

常见问题解答

「减少内存流量」课时是免费的吗?

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

「减少内存流量」这节课中我会学到什么?

让数据靠近计算过程 你通过在浏览器中直接运行的动手代码来练习 Mojo Academy,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 Mojo Academy 需要有经验吗?

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

「减少内存流量」课时需要多长时间?

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

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

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

此课程中的所有课时

  1. 计算内核的组成
  2. 结合 SIMD 与循环
  3. 减少内存流量
  4. 通过分块提升缓存局部性
← 返回 Mojo Academy