1. Introduction
CUDA 10.1推出了新的API:The Compute Sanitizer API,提供了更底层更丰富的Instrumentation API。
https://docs.nvidia.com/cuda/sanitizer-docs/SanitizerApiGuide/index.html
目前相关文档还比较简单,本文记录下官方Sample:Memory Tracker 的使用方法。
2. 开启GPU的开发模式
Linux 418.43 or later driver会默认开启保护模式,最简单的验证方法是使用nvprof来测试程序,如果出现了ERR_NVGPUCTRPERM: Permission issue with Performance Counters
,则说明保护模式已经开启。关闭的方法见官方的说明:
3. Sanitizer API的sample
CUDA官方给了一个查看cuda程序memory访问的sample:
https://github.com/NVIDIA/compute-sanitizer-samples
以memory tracker为例,
3.1 编译sample
进入MemoryTracker目录make即可编译生成libMemoryTracker.so MemoryTrackerPatches.fatbin
两个文件。
你可能需要constexpr size_t MemAccessDefaultSize
为更大的数值,否则程序可能会卡住无法进行。
3.2 修改目标程序的运行环境变量
为了能够统计目标程序在运行时的memory访问,需要修改目标程序的运行环境变量,在shell中运行:
export LD_PRELOAD=~/compute-sanitizer-samples/MemoryTracker/libMemoryTracker.so
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/cuda/extras/Sanitizer/
将编译出的MemoryTrackerPatches.fatbin
文件拷贝到目标程序的所在目录。
4. 获得运行结果
直接执行目标程序,你会获得类似如下的输出:
[63723] Read access of global memory by thread (0,0,411) at address 0x7025c966c (size is 4 bytes)
[63724] Read access of global memory by thread (0,0,252) at address 0x7025c9bf0 (size is 4 bytes)
[63725] Read access of global memory by thread (0,0,253) at address 0x7025c9bf4 (size is 4 bytes)
[63726] Read access of global memory by thread (0,0,254) at address 0x7025c9bf8 (size is 4 bytes)
[63727] Read access of global memory by thread (0,0,388) at address 0x7025c9610 (size is 4 bytes)
具体输出可以参考memory tracker的源码进行修改或分析。
Comments