
aclrtMemcpyAsync在错误的Stream上下发失败【免费下载链接】runtime本项目提供CANN运行时组件和维测功能组件。项目地址: https://gitcode.com/cann/runtime问题现象描述现象1调用aclrtMemcpyAsync接口返回设备不匹配错误码调用 aclrtMemcpyAsync 异步内存复制接口时返回错误码 107003ACL_ERROR_RT_STREAM_CONTEXT表示Stream不在当前Device的Context中。典型错误场景aclrtSetDevice(0); // 指定 Device 0 aclrtStream s0; aclrtCreateStream(s0); // 在 Device 0 上创建 Stream s0 aclrtSetDevice(1); // 指定 Device 1 aclrtStream s1; aclrtCreateStream(s1); // 在 Device 1 上创建 Stream s1 // 错误在 Device 1 上通过 Device 0 的 Stream s0 下发任务 aclrtMemcpyAsync(dst, size, src, size, ACL_MEMCPY_DEVICE_TO_DEVICE, s0); // 失败报错日志示例如下aclrtMemcpyAsync failed, ret 107003, stream not in current context [ERROR] RUNTIME: Stream not in current context, stream device mismatch可能原因Stream 与当前 Device 不属于同一 DeviceStream 在创建时绑定到特定 Device不能在另一个 Device 上下发任务。处理步骤原因1Stream 与当前 Device 不属于同一 Device解决方法理解 Stream 归属概念Stream 属于创建时的 Device与 Device 绑定在正确 Device 上下发切换 Device 后使用属于该 Device 的 Stream每个 Device 创建独立 Stream避免跨 Device 混用 Stream正确使用示例// Device 0 的操作 aclrtSetDevice(0); aclrtStream s0; aclrtCreateStream(s0); // s0 属于 Device 0 // Device 0 上使用 s0 下发任务 aclrtMemcpyAsync(dst0, size, src0, size, ACL_MEMCPY_DEVICE_TO_DEVICE, s0); // 正确 // Device 1 的操作 aclrtSetDevice(1); aclrtStream s1; aclrtCreateStream(s1); // s1 属于 Device 1 // Device 1 上使用 s1 下发任务 aclrtMemcpyAsync(dst1, size, src1, size, ACL_MEMCPY_DEVICE_TO_DEVICE, s1); // 正确 // 错误示例在 Device 1 上使用 Device 0 的 Stream s0 aclrtMemcpyAsync(dst1, size, src1, size, ACL_MEMCPY_DEVICE_TO_DEVICE, s0); // 失败多 Device 编程最佳实践// 为每个 Device 创建独立的 Stream aclrtStream deviceStreams[2]; aclrtSetDevice(0); aclrtCreateStream(deviceStreams[0]); // Device 0 的 Stream aclrtSetDevice(1); aclrtCreateStream(deviceStreams[1]); // Device 1 的 Stream // 切换 Device 后使用对应的 Stream aclrtSetDevice(0); aclrtMemcpyAsync(dst, size, src, size, ACL_MEMCPY_DEVICE_TO_DEVICE, deviceStreams[0]); // 正确 aclrtSetDevice(1); aclrtMemcpyAsync(dst, size, src, size, ACL_MEMCPY_DEVICE_TO_DEVICE, deviceStreams[1]); // 正确相关 issue暂无相关Issue。【免费下载链接】runtime本项目提供CANN运行时组件和维测功能组件。项目地址: https://gitcode.com/cann/runtime创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考