已合并
fix: 初始化random_num_task样例counter(#827) #4295
jiaxingru创建于 22 天前
fix: 初始化random_num_task样例counter(#827) #4295
已合并
Pull Request已成功合入, 合并人@CANN-robot
(感谢 jiaxingru 的贡献)22 天前 关联了issue:random_num_task 样例未初始化 randomCounterAddr,导致随机数结果受 Device 内存历史数据影响
atomgit-bot
22 天前 评论:
22 天前 评论:
变更摘要
本 PR 修复了 example/2_advanced_features/built_in_task/1_random_num_task/main.cpp 样例中随机数状态 counter 未初始化的问题。由于 aclrtMalloc 不保证分配后的 Device 内存内容,原先申请 16 字节 counter 后未写入初始值,首个随机数任务的结果可能受到 Device 内存历史数据影响。本次改动在分配成功后新增 aclrtMemset 调用,将 128 位 counter 清零后再提交随机数任务,同时引入 kCounterSize 常量统一表示 counter 大小,保持样例原有状态管理方式不变。
主要改动
- 新增 counter 清零初始化:在
aclrtMalloc成功后调用CHECK_ERROR(aclrtMemset(counterAddr, kCounterSize, 0, kCounterSize));,将 16 字节(128 位)随机数 counter 初始化为 0,避免首任务受 Device 内存残留数据影响。 - 引入 counter 大小常量:新增
constexpr size_t kCounterSize = 16U;,替代原先硬编码的16,并用于aclrtMalloc和aclrtMemset的内存长度参数。 - 指针与类型规范化:将
counterAddr的初始值由NULL改为nullptr,并将aclrtMalloc((void**)&counterAddr, ...)简化为aclrtMalloc(&counterAddr, ...)。 - 更新注释与文件格式:将注释由"申请存放随机数状态 counter 的device内存"更新为"申请并初始化存放随机数状态 counter 的 Device 内存",并补充文件末尾换行符。


不准确?
atomgit-bot
22 天前 评论:
22 天前 评论:
22 天前 添加了label:cann-cla/yes
CANN-robot
22 天前 评论:
22 天前 评论:
CANN-robot
22 天前 评论:
22 天前 评论:
Thanks for your pull-request.
The full list of commands accepted by me can be found at here.
You can get sig-info at here.
For more, you also can visit HICANN.
PR Approval Progress
✅ Congratulations! All modules have met the lgtm and approve requirements.
Module Approval Details
| module | lgtm status | approve status |
|---|---|---|
| example | ✅ 卢煜坤, wangluyao17 (2/2) | ✅ 卢煜坤 (1/1) |
💡 Tip:
- Committer can comment
/approveor/lgtm- Commenting
/approveimplies both code review (lgtm) and intent to merge (approve)


22 天前 将zhaozhixuan,wda1991,wangtao43,zhangpengpeng8,changhaixun,vaceciliachen,yanmingxiang,turing_yhy,Henry_ascend,Reyn52166,Turing_JasonWen,phy-zhao,WangHuiH,Axiaolei,ykl999,sunnana_004434229,YzQnWyx,houyanbao,wangluyao17,zhou-wuxiao,tingwood,liubin1986设为评审人
22 天前 将zhaozhixuan,wda1991,wangtao43,zhangpengpeng8,changhaixun,vaceciliachen,yanmingxiang,turing_yhy,Henry_ascend,Reyn52166,Turing_JasonWen,phy-zhao,WangHuiH,Axiaolei,ykl999,sunnana_004434229,YzQnWyx,houyanbao,wangluyao17,zhou-wuxiao,tingwood,liubin1986设为评审人
22 天前 将zhaozhixuan,wda1991,wangtao43,zhangpengpeng8,changhaixun,vaceciliachen,yanmingxiang,turing_yhy,Henry_ascend,Reyn52166,Turing_JasonWen,phy-zhao,WangHuiH,Axiaolei,ykl999,sunnana_004434229,YzQnWyx,houyanbao,wangluyao17,zhou-wuxiao,liubin1986设为审查人
22 天前 将zhaozhixuan,wda1991,wangtao43,zhangpengpeng8,changhaixun,vaceciliachen,yanmingxiang,turing_yhy,Henry_ascend,Reyn52166,Turing_JasonWen,phy-zhao,WangHuiH,Axiaolei,ykl999,sunnana_004434229,YzQnWyx,houyanbao,wangluyao17,zhou-wuxiao,liubin1986设为审查人
jiaxingru
22 天前 评论:
22 天前 评论:
/compile


22 天前 update merge request[project id: 8810053, iid: 4295, commit_id: 2987c360bacd87a59fa2ed9e39558c15a6a47cf6] virtual merging success
22 天前 update merge request[project id: 8810053, iid: 4295, commit_id: 2987c360bacd87a59fa2ed9e39558c15a6a47cf6] virtual merging success
22 天前 添加了label:ci-pipeline-running
CANN-robot
22 天前 评论:
22 天前 评论:
流水线任务触发成功
任务链接 [4ecddff9049f405b962e999f26e0dd34][流水线指导]
| 任务名称 | 状态 | 日志 | 下载链接 |
|---|---|---|---|
| Compile_Ascend_X86 | ✅ SUCCESS | >>>>> | >>>>> |
| Compile_Ascend_ARM | ✅ SUCCESS | >>>>> | >>>>> |
| pre_comment | ✅ SUCCESS | >>>>> | |
| Compile_Ascend_X86_ubuntu24 | ✅ SUCCESS | >>>>> | >>>>> |
| Compile_Ascend_ARM_ubuntu24 | ✅ SUCCESS | >>>>> | >>>>> |
| UT_Test_acl | ✅ SUCCESS | >>>>> | |
| UT_Test_rts_v201 | ✅ SUCCESS | >>>>> | |
| UT_Test_rts_c | ✅ SUCCESS | >>>>> | |
| UT_Test_rts_david | ✅ SUCCESS | >>>>> | |
| UT_Test_rts_910b | ✅ SUCCESS | >>>>> | |
| UT_Test_rts_common | ✅ SUCCESS | >>>>> | |
| UT_Test_platform | ✅ SUCCESS | >>>>> | |
| UT_Test_qs | ✅ SUCCESS | >>>>> | |
| UT_Test_aicpusd | ✅ SUCCESS | >>>>> | |
| UT_Test_tsd | ✅ SUCCESS | >>>>> | |
| UT_Test_error_manager | ✅ SUCCESS | >>>>> | |
| UT_Test_slog | ✅ SUCCESS | >>>>> | |
| UT_Test_atrace | ✅ SUCCESS | >>>>> | |
| UT_Test_msprof_part1 | ✅ SUCCESS | ||
| UT_Test_msprof_part2 | ✅ SUCCESS | ||
| UT_Test_adump | ✅ SUCCESS | >>>>> | |
| UT_Test_mmpa | ✅ SUCCESS | >>>>> | |
| UT_Test_camodel_check | ✅ SUCCESS | ||
| API_Check | ✅ SUCCESS | >>>>> | |
| PreSmoke_A900_npupool | ✅ SUCCESS | >>>>> | |
| UT_Test_rts_report | ✅ SUCCESS | >>>>> | |
| UT_Test_msprof_report | ✅ SUCCESS | >>>>> |
[2026-08-17 16:47:11] CI执行结束


CANN-robot
22 天前 评论:
22 天前 评论:
流水线任务触发成功
任务链接 [14d430c38d874e30b940574210b45542][流水线指导]
| 任务名称 | 状态 | 日志 | 下载链接 |
|---|---|---|---|
| codecheck | ✅ SUCCESS | >>>>> | |
| SCA | ✅ SUCCESS | >>>>> | |
| antipoison | ✅ SUCCESS | >>>>> | |
| codecheck_Check_Pr | ✅ SUCCESS | ||
| codecheck_style | ✅ SUCCESS | >>>>> | |
| codecheck_precommit | ✅ SUCCESS | >>>>> | |
| StaticCheck_codespell | ✅ SUCCESS | ||
| StaticCheck_link_validity | ✅ SUCCESS | ||
| StaticCheck_resource_existence | ✅ SUCCESS | ||
| StaticCheck_tag_closed | ✅ SUCCESS | ||
| StaticCheck_markdownlint | ✅ SUCCESS |
[2026-08-17 16:43:07] CI执行结束


22 天前 添加了label:api-check-pass
ykl999
22 天前 评论:
22 天前 评论:
/approve
/lgtm


22 天前 添加了label:approved
22 天前 删除了label:ci-pipeline-running
22 天前 添加了label:ci-pipeline-passed
22 天前 添加了label:lgtm
22 天前 合入了pull request
Pull Request
描述
random_num_task样例申请 16 字节 Device 内存作为随机数 counter 后,未写入明确的初始偏移量。由于aclrtMalloc不保证初始化内存,首个随机数任务的结果可能受到 Device 内存历史数据影响。本次修改在 counter 分配成功后调用
aclrtMemset,将 128 位 counter 初始化为 0,再提交首个随机数任务。后续随机数任务继续共享并推进同一 counter,保持样例原有状态管理方式不变。变更类型
关联的Issue
https://gitcode.com/cann/runtime/issues/827
如何测试
clang-format --dry-run --Werror -style=file example/2_advanced_features/built_in_task/1_random_num_task/main.cpp,检查通过。-std=c++17 -Wall -Werror -fsyntax-only进行语法检查,检查通过。ASCEND_INSTALL_PATH=/home/developer/Ascend/cann-9.1.0,执行样例目录下的bash run.sh,CMake 构建成功,Ascend 实机运行成功。seed=0多次运行,五组随机数输出逐项一致,程序均打印Sample Run Successfully.。核对清单
其他信息
本次仅修复样例中的 counter 初始化,不修改 Runtime 接口或随机数任务实现。