0Pricing
CUDA Academy · 课时

设计处理流水线

阶段、缓冲区与数据流

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

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

Think in Stages

A real image pipeline is a chain of stages: load, blur, sharpen, color-correct, save. Each stage is one clear transformation you can reason about alone. 🧩

Data Flows One Way

Pixels move forward through the pipeline: each stage reads the previous output and produces the next input. This one-way data flow keeps the design simple to follow.

Buffers Hold the In-Between

Between two stages you need a place to park pixels. A buffer is just a device array that one kernel writes and the next kernel reads. Plan a buffer per boundary.

float* d_stage1;
cudaMalloc(&d_stage1, width * height * sizeof(float));

Ping-Pong Two Buffers

You rarely need a fresh buffer per stage. Ping-pong between two buffers: read from one, write to the other, then swap. Two buffers serve a whole chain.

std::swap(d_in, d_out);

One Kernel Per Stage, For Now

Start with one kernel per stage. It is the clearest design and the easiest to verify. You will fuse stages later once each one is correct.

Map Pixels to Threads

The natural mapping is one thread per pixel. A 2D grid covers width and height, so each thread owns exactly one (x, y) location to process.

int x = blockIdx.x * blockDim.x + threadIdx.x;
int y = blockIdx.y * blockDim.y + threadIdx.y;

Index With Row Pitch

Images are stored row by row. Turn (x, y) into a flat offset with row-major indexing so every thread reads the right pixel.

int idx = y * width + x;

Guard the Image Borders

Your grid is rounded up, so some threads fall outside the image. A simple bounds check keeps them from touching memory they should not.

if (x >= width || y >= height) return;

Choose a 2D Block Shape

A block like 16x16 or 32x8 gives good coverage and warp-friendly rows. Pick a 2D block shape that divides the image cleanly when you can.

dim3 block(16, 16);
dim3 grid((width+15)/16, (height+15)/16);

Allocate Once, Reuse Often

Allocating device memory is costly, so do it once before the loop. Reuse the same buffers for every frame instead of malloc and free each time.

Sketch Before You Code

Draw the stages, their buffers, and the arrows between them first. A clear diagram of data flow catches design mistakes long before any kernel runs. ✏️

Quick Check

You have a chain of stages. How do you avoid allocating a new buffer for every stage?

Recap

You designed a pipeline as one-way stages joined by buffers, mapped one thread per pixel with a 2D grid, and learned to ping-pong buffers and sketch the flow first. 🎯

常见问题解答

「设计处理流水线」课时是免费的吗?

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

「设计处理流水线」这节课中我会学到什么?

阶段、缓冲区与数据流 你通过在浏览器中直接运行的动手代码来练习 CUDA Academy,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 CUDA Academy 需要有经验吗?

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

「设计处理流水线」课时需要多长时间?

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

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

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

此课程中的所有课时

  1. 设计处理流水线
  2. 将过滤器融合到一个内核中
  3. 为大图像流式处理分块
  4. 分析、优化、交付
← 返回 CUDA Academy