MUSA Compute Sanitizer 快速入门
本文用最短路径说明如何使用 mt-compute-sanitizer 包装原始 MUSA 程序、保存日志并判断结果。
前置条件
确保已经安装 MUSA Driver、MUSA Toolkit 和 MUSA Compute Sanitizer。
# 查看 GPU 和 Driver 信息
mthreads-gmi
# 查看 Toolkit / 编译器版本
/usr/local/musa/bin/mcc --version
# 查看 Compute Sanitizer 版本和帮助
/usr/local/musa/bin/mt-compute-sanitizer --version
/usr/local/musa/bin/mt-compute-sanitizer -h
如果 shell 中找不到 mt-compute-sanitizer,请先确认 /usr/local/musa/bin 是否在 PATH 中,或直接使用完整路径 /usr/local/musa/bin/mt-compute-sanitizer。
第一次运行
1. 准备示例程序
假设您已有一个 MUSA 程序 vectorAdd:
# -g 生成调试信息,方便 sanitizer 输出更有用的定位信息
/usr/local/musa/bin/mcc -g -O2 vectorAdd.cu -o vectorAdd
如果没有现成的 MUSA 程序,可以参考 MUSA SDK 文档中的示例代码。
2. 普通运行原始程序
先确认原始程序自身可以运行,并保存应用输出和退出码:
./vectorAdd > vectorAdd.plain.out 2>&1
echo PLAIN_STATUS=$?
3. 使用 sanitizer 运行
推荐总是使用 --log-file 保存 sanitizer 日志:
/usr/local/musa/bin/mt-compute-sanitizer \
--log-file vectorAdd.san.log \
./vectorAdd \
> vectorAdd.san.out 2>&1
| 文件 | 内容 |
|---|---|
vectorAdd.san.log | sanitizer 检查到的 OOB、API error 和 ERROR SUMMARY。 |
vectorAdd.san.out | 程序自己的输出、stderr、测试框架日志。 |
4. 查看结果
grep "ERROR SUMMARY" vectorAdd.san.log
grep -n "Out of bounds memory access\|Program hit musaError\|ERROR SUMMARY" vectorAdd.san.log
无错误时应看到:
========= COMPUTE-SANITIZER
========= ERROR SUMMARY: 0 error
有错误时,ERROR SUMMARY 会非 0。例如 OOB 输出可能为:
========= COMPUTE-SANITIZER
========= ERROR SUMMARY: 448 errors
========= ERROR SUMMARY: 448 errors were not printed. Use --print-limit option to adjust the number of printed errors
该示例说明:错误数量很多时,summary 仍会统计总错误数;如果详细错误被截断,可通过 --print-limit 调整打印数量。
检查显存泄漏
显存泄漏检查默认关闭。怀疑 musaMalloc() 后未释放时,必须显式加 --leak-check=full:
/usr/local/musa/bin/mt-compute-sanitizer \
--leak-check=full \
--log-file leak.san.log \
./vectorAdd \
> leak.san.out 2>&1
查看结果:
grep "ERROR SUMMARY" leak.san.log
grep -n "Leaked\|musaMalloc\|ERROR SUMMARY" leak.san.log
leak 输出示例:
========= COMPUTE-SANITIZER
========= Leaked 4096 bytes at 0x1006b600000
========= Saved host backtrace up to driver entry point at allocation time
========= Host Frame: main [0x1155] in your_program
========= ERROR SUMMARY: 1 error
检查 API error
MUSA API error 默认报告。API error 输出示例:
========= COMPUTE-SANITIZER
========= Program hit musaErrorInvalidDevice (error 101) due to "invalid device ordinal" on MUSA API call to musaSetDevice.
========= Saved host backtrace up to driver entry point at error
========= Host Frame: main [0x1149] in your_program
========= ERROR SUMMARY: 1 error
如果明确不关注 API error,可以关闭:
/usr/local/musa/bin/mt-compute-sanitizer \
--report-api-errors=no \
--log-file no_api_error.san.log \
./vectorAdd
关闭后,sanitizer 不再把 API error 计入 ERROR SUMMARY。被测程序自己的 API 返回值仍可能出现在应用输出中。
下一步
如果无错误(ERROR SUMMARY: 0 error):
- 对同一 case 加
--leak-check=full检查显存泄漏。 - 扩大输入规模或覆盖更多 case。
- 正确性问题收敛后,再使用 Moore Perf Compute 做性能分析。
如果有错误(ERROR SUMMARY 非 0):
- 查看错误类型:OOB、leak 或 API error。
- 根据 host frame、kernel 名称、API 名称定位问题代码。
- 修复后重新运行原始程序和 sanitizer。
- 保存普通运行日志、sanitizer 日志、环境信息和复现命令。

