CANN运行时设备到主机同步内存复制示例
2026/6/24 6:17:39 网站建设 项目流程

3_d2h_sync_memory_copy

【免费下载链接】runtime本项目提供CANN运行时组件和维测功能组件。项目地址: https://gitcode.com/cann/runtime

Description

This sample demonstrates synchronous memory copy from Device to Host using theaclrtMemcpyAPI for data transfer.

Product Support

This sample supports the following products:

ProductSupported
Ascend 950PR/Ascend 950DT
Atlas A3 Training Series Products/Atlas A3 Inference Series Products
Atlas A2 Training Series Products/Atlas A2 Inference Series Products

Build and Run

  1. Download the sample code to an environment where CANN software is installed, and switch to the sample directory.
cd ${git_clone_path}/example/1_basic_features/memory/3_d2h_sync_memory_copy
  1. Set environment variables.
# Replace ${install_root} with the CANN installation root directory, which is installed in `/usr/local/Ascend` by default source ${install_root}/cann/set_env.sh export ASCEND_INSTALL_PATH=${install_root}/cann # Set SOC_VERSION and ASCENDC_CMAKE_DIR # -SOC_VERSION: The model of the Ascend AI processor, such as Ascend910_9362, Ascend910B2, and so on # -ASCENDC_CMAKE_DIR: The sample involves calling AscendC operators. Configure the AscendC compiler ascendc.cmake path, such as /usr/local/Ascend/cann/x86_64-linux/tikcpp/ascendc_kernel_cmake source ${git_clone_path}/example/set_sample_env.sh
  1. Run the following command to execute the sample.
bash run.sh

CANN RUNTIME API

The key functionality points and their corresponding APIs in this sample are as follows:

  • Initialization
    • CallaclInitto perform initialization configuration.
    • CallaclFinalizeto perform deinitialization.
  • Device Management
    • CallaclrtSetDeviceto specify the Device for computation.
    • CallaclrtResetDeviceForceto forcibly reset the current Device and reclaim Device resources.
  • Stream Management
    • CallaclrtCreateStreamto create a Stream.
    • CallaclrtDestroyStreamForceto forcibly destroy a Stream and discard all tasks.
  • Memory Management
    • CallaclrtMallocHostto allocate memory on the Host.
    • CallaclrtMallocto allocate memory on the Device.
    • CallaclrtFreeHostto free memory on the Host.
    • CallaclrtFreeto free memory on the Device.
  • Data Transfer
    • CallaclrtMemcpyto implement Device-to-Host data transfer using memory copy.

Sample Output

[INFO] Allocate memory on the host memory 0x... successfully [INFO] Allocate memory on the device memory 0x... successfully [INFO] Write the data 123 to the virtual memory 0x... [INFO] Copy memory from memory 0x... to memory 0x... [INFO] Destination data: 123 CANN Version: 1.0.0, TimeStamp: ... Source data: 123

【免费下载链接】runtime本项目提供CANN运行时组件和维测功能组件。项目地址: https://gitcode.com/cann/runtime

创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考

需要专业的网站建设服务?

联系我们获取免费的网站建设咨询和方案报价,让我们帮助您实现业务目标

立即咨询