高效的使用搬运API【免费下载链接】asc-devkit本项目是CANN 推出的昇腾AI处理器专用的算子程序开发语言原生支持C和C标准规范主要由类库和语言扩展层构成提供多层级API满足多维场景算子开发诉求。项目地址: https://gitcode.com/cann/asc-devkit【优先级】高【描述】在使用搬运API时应该尽可能地通过配置搬运控制参数实现连续搬运或者固定间隔搬运避免使用for循环二者效率差距极大。如下图示例图片的每一行为16KB需要从每一行中搬运前2KB针对这种场景使用for循环遍历每行每次仅能搬运2KB。若直接配置DataCopyParams参数包含srcStride/dstStride/blockLen/blockCount则可以达到一次搬完的效果每次搬运32KB参考尽量一次搬运较大的数据块章节介绍的搬运数据量和实际带宽的关系建议一次搬完。图1待搬运数据排布【反例】// 搬运数据存在间隔从GM上每行16KB中搬运2KB数据,共16行 LocalTensorfloat tensorIn; GlobalTensorfloat tensorGM; ... constexpr int32_t copyWidth 2 * 1024 / sizeof(float); constexpr int32_t imgWidth 16 * 1024 / sizeof(float); constexpr int32_t imgHeight 16; // 使用for循环每次只能搬运2K重复16次 for (int i 0; i imgHeight; i) { DataCopy(tensorIn[i * copyWidth], tensorGM[i * imgWidth], copyWidth); }【正例】LocalTensorfloat tensorIn; GlobalTensorfloat tensorGM; ... constexpr int32_t copyWidth 2 * 1024 / sizeof(float); constexpr int32_t imgWidth 16 * 1024 / sizeof(float); constexpr int32_t imgHeight 16; // 通过DataCopy包含DataCopyParams的接口一次搬完 DataCopyParams copyParams; copyParams.blockCount imgHeight; copyParams.blockLen copyWidth / 8; // 搬运的单位为DataBlock(32Byte)每个DataBlock内有8个float copyParams.srcStride (imgWidth - copyWidth) / 8; // 表示两次搬运src之间的间隔单位为DataBlock copyParams.dstStride 0; // 连续写两次搬运之间dst的间隔为0单位为DataBlock DataCopy(tensorGM, tensorIn, copyParams);【免费下载链接】asc-devkit本项目是CANN 推出的昇腾AI处理器专用的算子程序开发语言原生支持C和C标准规范主要由类库和语言扩展层构成提供多层级API满足多维场景算子开发诉求。项目地址: https://gitcode.com/cann/asc-devkit创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考
Ascend C高效使用搬运API指南
发布时间:2026/7/18 10:19:25
高效的使用搬运API【免费下载链接】asc-devkit本项目是CANN 推出的昇腾AI处理器专用的算子程序开发语言原生支持C和C标准规范主要由类库和语言扩展层构成提供多层级API满足多维场景算子开发诉求。项目地址: https://gitcode.com/cann/asc-devkit【优先级】高【描述】在使用搬运API时应该尽可能地通过配置搬运控制参数实现连续搬运或者固定间隔搬运避免使用for循环二者效率差距极大。如下图示例图片的每一行为16KB需要从每一行中搬运前2KB针对这种场景使用for循环遍历每行每次仅能搬运2KB。若直接配置DataCopyParams参数包含srcStride/dstStride/blockLen/blockCount则可以达到一次搬完的效果每次搬运32KB参考尽量一次搬运较大的数据块章节介绍的搬运数据量和实际带宽的关系建议一次搬完。图1待搬运数据排布【反例】// 搬运数据存在间隔从GM上每行16KB中搬运2KB数据,共16行 LocalTensorfloat tensorIn; GlobalTensorfloat tensorGM; ... constexpr int32_t copyWidth 2 * 1024 / sizeof(float); constexpr int32_t imgWidth 16 * 1024 / sizeof(float); constexpr int32_t imgHeight 16; // 使用for循环每次只能搬运2K重复16次 for (int i 0; i imgHeight; i) { DataCopy(tensorIn[i * copyWidth], tensorGM[i * imgWidth], copyWidth); }【正例】LocalTensorfloat tensorIn; GlobalTensorfloat tensorGM; ... constexpr int32_t copyWidth 2 * 1024 / sizeof(float); constexpr int32_t imgWidth 16 * 1024 / sizeof(float); constexpr int32_t imgHeight 16; // 通过DataCopy包含DataCopyParams的接口一次搬完 DataCopyParams copyParams; copyParams.blockCount imgHeight; copyParams.blockLen copyWidth / 8; // 搬运的单位为DataBlock(32Byte)每个DataBlock内有8个float copyParams.srcStride (imgWidth - copyWidth) / 8; // 表示两次搬运src之间的间隔单位为DataBlock copyParams.dstStride 0; // 连续写两次搬运之间dst的间隔为0单位为DataBlock DataCopy(tensorGM, tensorIn, copyParams);【免费下载链接】asc-devkit本项目是CANN 推出的昇腾AI处理器专用的算子程序开发语言原生支持C和C标准规范主要由类库和语言扩展层构成提供多层级API满足多维场景算子开发诉求。项目地址: https://gitcode.com/cann/asc-devkit创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考