Intra-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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | import acl # ...... # 1. Allocate memory. size = 1 * 1024 * 1024 dev_ptr_a, ret = acl.rt.malloc(size, ACL_MEM_MALLOC_NORMAL_ONLY) 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, dev_ptr_a, size) # 3. Perform synchronous memory copy. # In synchronous memory copy, dev_ptr_a indicates the address of the pointer to the source memory address on the device, 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_DEVICE_TO_DEVICE = 3 ret = acl.rt.memcpy(dev_ptr_b, size, dev_ptr_a, size, ACL_MEMCPY_DEVICE_TO_DEVICE) # 4. Destroy allocations in a timely manner. ret = acl.rt.free(dev_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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | import acl # ...... # 1. Allocate memory. size = 1 * 1024 * 1024 dev_ptr_a, ret = acl.rt.malloc(size, ACL_MEM_MALLOC_NORMAL_ONLY) 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, dev_ptr_a, size) # 3. Perform asynchronous memory copy. # In asynchronous memory copy, dev_ptr_a indicates the address of the pointer to the source memory address on the device, 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_DEVICE_TO_DEVICE = 3 stream, ret = acl.rt.create_stream() ret = acl.rt.memcpy_async(dev_ptr_b, size, dev_ptr_a, size, ACL_MEMCPY_DEVICE_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(dev_ptr_a) ret = acl.rt.free(dev_ptr_b) # ...... |
Parent topic: Data Copy