已合并
fix: 初始化random_num_task样例counter(#827) #4295
fix: 初始化random_num_task样例counter(#827) #4295
已合并
jiaxingru创建于 22 天前
jiaxingru
jiaxingru成员
22 天前

Pull Request

描述

random_num_task 样例申请 16 字节 Device 内存作为随机数 counter 后,未写入明确的初始偏移量。由于 aclrtMalloc 不保证初始化内存,首个随机数任务的结果可能受到 Device 内存历史数据影响。

本次修改在 counter 分配成功后调用 aclrtMemset,将 128 位 counter 初始化为 0,再提交首个随机数任务。后续随机数任务继续共享并推进同一 counter,保持样例原有状态管理方式不变。

变更类型

关联的Issue

https://gitcode.com/cann/runtime/issues/827

如何测试

  1. 执行 clang-format --dry-run --Werror -style=file example/2_advanced_features/built_in_task/1_random_num_task/main.cpp,检查通过。
  2. 使用 -std=c++17 -Wall -Werror -fsyntax-only 进行语法检查,检查通过。
  3. 设置 ASCEND_INSTALL_PATH=/home/developer/Ascend/cann-9.1.0,执行样例目录下的 bash run.sh,CMake 构建成功,Ascend 实机运行成功。
  4. 使用相同 seed=0 多次运行,五组随机数输出逐项一致,程序均打印 Sample Run Successfully.

核对清单

其他信息

本次仅修复样例中的 counter 初始化,不修改 Runtime 接口或随机数任务实现。

likedislike
Pull Request已成功合入, 合并人@CANN-robot
(感谢 jiaxingru 的贡献)
jiaxingrujiaxingru成员
22 天前 创建了 pull request,commit cd101b1f
jiaxingrujiaxingru成员
22 天前 关联了issue:random_num_task 样例未初始化 randomCounterAddr,导致随机数结果受 Device 内存历史数据影响
atomgit-bot
atomgit-bot
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,并用于 aclrtMallocaclrtMemset 的内存长度参数。
  • 指针与类型规范化:将 counterAddr 的初始值由 NULL 改为 nullptr,并将 aclrtMalloc((void**)&counterAddr, ...) 简化为 aclrtMalloc(&counterAddr, ...)
  • 更新注释与文件格式:将注释由"申请存放随机数状态 counter 的device内存"更新为"申请并初始化存放随机数状态 counter 的 Device 内存",并补充文件末尾换行符。
likedislike
不准确?
atomgit-bot
atomgit-bot
22 天前 评论:

代码审查

✅ 未发现问题

likedislike
不准确?
CANN-robotCANN-robot成员
22 天前 添加了label:cann-cla/yes
CANN-robot
CANN-robot成员
22 天前 评论:

CLA Signature Pass

m0_60337577, thanks for your pull request. All authors of the commits have signed the CLA. 👍

likedislike
CANN-robot
CANN-robot成员
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 /approve or /lgtm
  • Commenting /approve implies both code review (lgtm) and intent to merge (approve)
likedislike
CANN-robotCANN-robot成员
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设为评审人
CANN-robotCANN-robot成员
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
jiaxingru成员
22 天前 评论:

/compile

likedislike
jiaxingrujiaxingru成员
22 天前 update merge request[project id: 8810053, iid: 4295, commit_id: 2987c360bacd87a59fa2ed9e39558c15a6a47cf6] virtual merging success
CANN-robotCANN-robot成员
22 天前 添加了label:ci-pipeline-running
CANN-robot
CANN-robot成员
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执行结束

likedislike
CANN-robot
CANN-robot成员
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执行结束

likedislike
CANN-robotCANN-robot成员
22 天前 添加了label:api-check-pass
ykl999
ykl999成员
22 天前 评论:

/approve
/lgtm

likedislike
CANN-robotCANN-robot成员
22 天前 添加了label:approved
CANN-robotCANN-robot成员
22 天前 删除了label:ci-pipeline-running
CANN-robotCANN-robot成员
22 天前 添加了label:ci-pipeline-passed
王璐瑶成员
22 天前 评论:

/lgtm

likedislike
CANN-robotCANN-robot成员
22 天前 添加了label:lgtm
CANN-robotCANN-robot成员
22 天前 合入了pull request