Skip to Content
人工智能系统性能工程 (Chinese Edition)
book

人工智能系统性能工程 (Chinese Edition)

by Chris Fregly
November 2025
Intermediate to advanced
1060 pages
14h 20m
Chinese
O'Reilly Media, Inc.
Content preview from 人工智能系统性能工程 (Chinese Edition)

第11章. 核间 流水线处理、同步机制与CUDA流序内存分配

本作品已使用人工智能进行翻译。欢迎您提供反馈和意见:translation-feedback@oreilly.com

迄今为止,我们主要探讨了单内核工具——cuda::pipeline 双缓冲、波束特化(加载/计算/存储波束)、持久内核以及带DSMEM/TMA的线程块集群——以保持单个内核在SM中的持续运行。本章将延续这些内核,展示如何通过CUDA流、事件及流顺序内存分配器实现跨内核与批次的流水线处理。简言之,第10章侧重于隐藏内核内部的延迟。本章则展示如何隐藏内核间以及GPU与主机间的延迟。

这种跨内核并行机制对于在实际工作负载中保持GPU所有引擎持续运转至关重要。要使现代GPU达到峰值利用率,必须让GPU的计算引擎与直接内存访问(DMA)引擎保持并行运行。

CUDA流为这种跨内核并行提供了基础。通过结合异步内存操作、精细同步机制以及CUDA图(本章简要介绍,下章详述),可构建高效管道以避免主机端阻塞。

利用CUDA流实现内核执行重叠

CUDA流是一系列 操作(内核启动、内存复制和内存分配)的序列,按发出顺序执行。如图11-1所示,设想从CPU向GPU启动5个内核,使用2个流。

Diagram showing the sequence of five kernels launched from the CPU to two GPU streams, illustrating the concurrent execution of operations in CUDA streams.
图11-1. 从CPU向GPU上运行的两个流启动 五个内核

此时可见:ker_A 与ker_B 在流2上运行,而ker_1 、ker_2 及ker_3 在流1上运行。只要硬件资源允许,所有内核均可相互重叠——甚至跨CUDA流重叠。

CPU可在流异步执行内核操作期间继续处理任务(cpu_code_1 和cpu_code_2) )。在两个CUDA流上启动这五个内核的代码如下所示:

#include <cstdio>
#include <cuda_runtime.h>

__global__ void ker_A()  { /* ... do some work ... */ }
__global__ void ker_B()  { /* ... do some work ... */ }

__global__ void ker_1()  { /* ... do some work ... */ }
__global__ void ker_2()  { /* ... do some work ... */ }
__global__ void ker_3()  { /* ... do some work ... */ }

int main() {
    // 1) Create two CUDA streams
    cudaStream_t stream1, stream2;
    cudaStreamCreateWithFlags(&stream1, cudaStreamNonBlocking);
    cudaStreamCreateWithFlags(&stream2, cudaStreamNonBlocking);

    // 2) Define your grid/block sizes
    dim3 grid(128);
    dim3 block(256);

    // 3) Launch ker_1 on stream1
    ker_1<<<grid, block, 0, stream1>>>();

    // 4) CPU code 1 runs ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

向量数据库 (Chinese Edition)

向量数据库 (Chinese Edition)

Nitin Borwankar

Publisher Resources

ISBN: 0642572281557