Host-to-Device Data Transfer

Synchronous Memory Copy

After APIs are called, add an exception handling branch, and record error logs and warning logs. The following is a code snippet of key steps only, which is not ready to use.

import acl
# ......

# 1. Allocate memory.
size = 1 * 1024 * 1024
host_ptr_a, ret = acl.rt.malloc_host(size)
dev_ptr_b, ret = acl.rt.malloc(size, ACL_MEM_MALLOC_NORMAL_ONLY)

# 2. After the memory is allocated, read data into the memory and implement the customized function read_file.
read_file(fileName, host_ptr_a, size)

# 3. Perform synchronous memory copy.
# In synchronous memory copy, host_ptr_a indicates the address of the pointer to the source memory address on the host, dev_ptr_b indicates the address of the pointer to the destination memory address on the device, and size indicates the memory size.
# ACL_MEMCPY_HOST_TO_DEVICE = 1
ret = acl.rt.memcpy(dev_ptr_b, size, host_ptr_a, size, ACL_MEMCPY_HOST_TO_DEVICE)

# 4. Destroy allocations in a timely manner.
ret = acl.rt.free_host(host_ptr_a)
ret = acl.rt.free(dev_ptr_b)

# ......

Asynchronous Memory Copy

After APIs are called, add an exception handling branch, and record error logs and warning logs. The following is a code snippet of key steps only, which is not ready to use.
import acl
# ......

# 1. Allocate memory.
size = 1 * 1024 * 1024
host_ptr_a, ret = acl.rt.malloc_host(size)
dev_ptr_b, ret = acl.rt.malloc(size, ACL_MEM_MALLOC_NORMAL_ONLY)

# 2. After the memory is allocated, read data into the memory and implement the customized function read_file.
read_file(fileName, host_ptr_a, size)

# 3. Perform asynchronous memory copy.
# In asynchronous memory copy, host_ptr_a indicates the address of the pointer to the source memory address on the host, dev_ptr_b indicates the address of the pointer to the destination memory address on the device, and size indicates the memory size.
# ACL_MEMCPY_HOST_TO_DEVICE = 1
stream = acl.rt.create_stream()
ret = acl.rt.memcpy_async(dev_ptr_b, size , host_ptr_a, size, ACL_MEMCPY_HOST_TO_DEVICE, stream)
ret = acl.rt.synchronize_stream(stream)

# 4. Destroy allocations in a timely manner.
ret = acl.rt.destroy_stream(stream)
ret = acl.rt.free_host(host_ptr_a)
ret = acl.rt.free(dev_ptr_b)

# ......