Runtime Resource Allocation and Deallocation
The app you develop must contain the code logic for runtime resource allocation. See API Call Sequence to learn about the overall API call sequence before viewing the resource allocation and deallocate workflows in this section.
Principles
Allocate runtime resources including the device and stream in sequence to support the execution of computing and management tasks. After all data is processed, deallocate these runtime resources in sequence.
Streams can be created implicitly or explicitly based on the application scenarios.
- Implicit stream creation: applies to simple apps with low complicity of interaction logic. However, in multithreaded programming, each thread uses the default stream. The execution sequence of tasks in the default stream depends on the thread scheduling sequence of the operating system.
- Explicit stream creation: applies to large apps with complex interaction logic, offering better app readability and maintainability.
- The multithreading, multistreaming scenarios are described in Stream Management.
Runtime Resource Allocation
The key APIs are described as follows:
Allocate the devices and streams in sequence.
- Call acl.rt.set_device to specify the device used for compute. This API also implicitly creates the default context and stream. However, the following constraints must be observed:
- A device corresponds to a default context, which is released when the acl.rt.destroy_context API is called. Therefore, you do not need to release the default context by explicit call.
- A device corresponds to a default stream, which is destroyed when the acl.rt.destroy_stream API is called. Therefore, you do not need to release the default stream by explicit call.
- The default context and stream are automatically destroyed with the reset of device using acl.rt.reset_device.
- To use the default stream as the input parameter, pass 0. Alternatively, you can call acl.rt.create_stream to explicitly create a stream instead of using the default stream.
- (Optional) Call acl.rt.get_run_mode to obtain the run mode of the software stack and determine the logic of the memory allocation API call based on the run mode.
If the query result is ACL_HOST, host memory needs to be allocated for data transfer.
If the query result is ACL_DEVICE, only device memory needs to be allocated for data transfer.
For details, see Data Copy.
Runtime Resource Deallocation
The key APIs are described as follows:
- If acl.rt.create_stream is called to explicitly create a stream, acl.rt.destroy_stream needs to be called to destroy the stream. Otherwise, acl.rt.destroy_stream does not need to be called.
- If you call acl.rt.reset_device to destroy resources on the device. This API also implicitly destroy the default context and stream. You do not need to destroy the default context and stream separately.
Sample Code
You can view the complete code in Sample Overview.
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 |
import acl #...... # =====Runtime resource allocation===== # 1. Specify a compute device. ret = acl.rt.set_device(device_id) # 2. Explicitly create a stream. # Reserve the execution order of asynchronous tasks. stream, ret = acl.rt.create_stream() # =====Runtime resource allocation===== #...... # =====Runtime resource deallocation===== ret = acl.rt.destroy_stream(stream) ret = acl.rt.reset_device(device_id) # =====Runtime resource deallocation===== #...... |