没有合适的资源?快使用搜索试试~ 我知道了~
CUDA C++ Programming Guide
需积分: 2 1 下载量 184 浏览量
2023-12-19
13:38:11
上传
评论
收藏 2.53MB PDF 举报
温馨提示
试读
118页
CUDA C++ Programming Guide Release 12.3 CUDA 编程指南
资源推荐
资源详情
资源评论
CUDA C++ Best Practices Guide
Release 12.3
NVIDIA
Nov 14, 2023
Contents
1 What Is This Document? 3
2 Who Should Read This Guide? 5
3 Assess, Parallelize, Optimize, Deploy 7
3.1 Assess . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
3.2 Parallelize . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
3.3 Optimize . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
3.4 Deploy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
4 Recommendations and Best Practices 11
5 Assessing Your Application 13
6 Heterogeneous Computing 15
6.1 Dierences between Host and Device . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
6.2 What Runs on a CUDA-Enabled Device? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
7 Application Proling 19
7.1 Prole . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
7.1.1 Creating the Prole . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
7.1.2 Identifying Hotspots . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
7.1.3 Understanding Scaling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
7.1.3.1 Strong Scaling and Amdahl’s Law . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
7.1.3.2 Weak Scaling and Gustafson’s Law . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
7.1.3.3 Applying Strong and Weak Scaling . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
8 Parallelizing Your Application 23
9 Getting Started 25
9.1 Parallel Libraries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
9.2 Parallelizing Compilers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
9.3 Coding to Expose Parallelism . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
10 Getting the Right Answer 27
10.1 Verication . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
10.1.1 Reference Comparison . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
10.1.2 Unit Testing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
10.2 Debugging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
10.3 Numerical Accuracy and Precision . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
10.3.1 Single vs. Double Precision . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
10.3.2 Floating Point Math Is not Associative . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
10.3.3 IEEE 754 Compliance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
10.3.4 x86 80-bit Computations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
i
11 Optimizing CUDA Applications 31
12 Performance Metrics 33
12.1 Timing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
12.1.1 Using CPU Timers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
12.1.2 Using CUDA GPU Timers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
12.2 Bandwidth . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
12.2.1 Theoretical Bandwidth Calculation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
12.2.2 Eective Bandwidth Calculation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
12.2.3 Throughput Reported by Visual Proler . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
13 Memory Optimizations 37
13.1 Data Transfer Between Host and Device . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
13.1.1 Pinned Memory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38
13.1.2 Asynchronous and Overlapping Transfers with Computation . . . . . . . . . . . . . . . 38
13.1.3 Zero Copy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40
13.1.4 Unied Virtual Addressing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
13.2 Device Memory Spaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42
13.2.1 Coalesced Access to Global Memory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44
13.2.1.1 A Simple Access Pattern . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44
13.2.1.2 A Sequential but Misaligned Access Pattern . . . . . . . . . . . . . . . . . . . . . . 45
13.2.1.3 Eects of Misaligned Accesses . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
13.2.1.4 Strided Accesses . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
13.2.2 L2 Cache . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
13.2.2.1 L2 Cache Access Window . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
13.2.2.2 Tuning the Access Window Hit-Ratio . . . . . . . . . . . . . . . . . . . . . . . . . . . 49
13.2.3 Shared Memory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51
13.2.3.1 Shared Memory and Memory Banks . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51
13.2.3.2 Shared Memory in Matrix Multiplication (C=AB) . . . . . . . . . . . . . . . . . . . . 52
13.2.3.3 Shared Memory in Matrix Multiplication (C=AAT) . . . . . . . . . . . . . . . . . . . . 55
13.2.3.4 Asynchronous Copy from Global Memory to Shared Memory . . . . . . . . . . . . 57
13.2.4 Local Memory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59
13.2.5 Texture Memory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59
13.2.5.1 Additional Texture Capabilities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59
13.2.6 Constant Memory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
13.2.7 Registers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
13.2.7.1 Register Pressure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
13.3 Allocation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
13.4 NUMA Best Practices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
14 Execution Conguration Optimizations 63
14.1 Occupancy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63
14.1.1 Calculating Occupancy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64
14.2 Hiding Register Dependencies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64
14.3 Thread and Block Heuristics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65
14.4 Eects of Shared Memory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66
14.5 Concurrent Kernel Execution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67
14.6 Multiple contexts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67
15 Instruction Optimization 69
15.1 Arithmetic Instructions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69
15.1.1 Division Modulo Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69
15.1.2 Loop Counters Signed vs. Unsigned . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69
15.1.3 Reciprocal Square Root . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70
15.1.4 Other Arithmetic Instructions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70
ii
15.1.5 Exponentiation With Small Fractional Arguments . . . . . . . . . . . . . . . . . . . . . . 70
15.1.6 Math Libraries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71
15.1.7 Precision-related Compiler Flags . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73
15.2 Memory Instructions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73
16 Control Flow 75
16.1 Branching and Divergence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75
16.2 Branch Predication . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75
17 Deploying CUDA Applications 77
18 Understanding the Programming Environment 79
18.1 CUDA Compute Capability . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79
18.2 Additional Hardware Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79
18.3 Which Compute Capability Target . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80
18.4 CUDA Runtime . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80
19 CUDA Compatibility Developer’s Guide 83
19.1 CUDA Toolkit Versioning . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84
19.2 Source Compatibility . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85
19.3 Binary Compatibility . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85
19.3.1 CUDA Binary (cubin) Compatibility . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87
19.4 CUDA Compatibility Across Minor Releases . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88
19.4.1 Existing CUDA Applications within Minor Versions of CUDA . . . . . . . . . . . . . . . . 88
19.4.1.1 Handling New CUDA Features and Driver APIs . . . . . . . . . . . . . . . . . . . . . 89
19.4.1.2 Using PTX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90
19.4.1.3 Dynamic Code Generation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91
19.4.1.4 Recommendations for building a minor-version compatible library . . . . . . . . 93
19.4.1.5 Recommendations for taking advantage of minor version compatibility in your
application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94
20 Preparing for Deployment 95
20.1 Testing for CUDA Availability . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95
20.2 Error Handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96
20.3 Building for Maximum Compatibility . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96
20.4 Distributing the CUDA Runtime and Libraries . . . . . . . . . . . . . . . . . . . . . . . . . . . 97
20.4.1 CUDA Toolkit Library Redistribution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 98
20.4.1.1 Which Files to Redistribute . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99
20.4.1.2 Where to Install Redistributed CUDA Libraries . . . . . . . . . . . . . . . . . . . . . 100
21 Deployment Infrastructure Tools 103
21.1 Nvidia-SMI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103
21.1.1 Queryable state . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103
21.1.2 Modiable state . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104
21.2 NVML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104
21.3 Cluster Management Tools . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104
21.4 Compiler JIT Cache Management Tools . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104
21.5 CUDA_VISIBLE_DEVICES . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105
22 Recommendations and Best Practices 107
22.1 Overall Performance Optimization Strategies . . . . . . . . . . . . . . . . . . . . . . . . . . . 107
23 nvcc Compiler Switches 109
23.1 nvcc . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109
iii
剩余117页未读,继续阅读
资源评论
Dream_Ross
- 粉丝: 16
- 资源: 9
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 基于matlab实现文档+程序边缘计算任务卸载与资源调度的算法,是论文的源代码,具有价值.rar
- 什么是学生成绩管理系统c++以及学习学生成绩管理系统的意义
- 什么是词向量-以及学习关于了解词向量的意义
- 什么是mybatis动态sql以及学习mybatis动态sql的意义
- 华为数据治理方法论,包括:数据治理框架、数据治理组织架构、数据治理度量评估体系以及华为数据治理案例分享
- 基于matlab实现对表面肌电信号进行归一化处理,并对归一化后的图形显示 .rar
- 基于matlab实现单级倒立摆的 T-S 模型 包括 LMI 程序源码
- 图书管理系统(struts+hibernate+spring+ext).rar
- 基于matlab实现此压缩包包含语音信号处理中的语音变声代码加音频.rar
- STM32使用PWM驱动舵机并通过OLED显示
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功