Runtime Resource Allocation and Deallocation
During application development, the application must contain the code logic for resource allocation during running. For details about the API call sequence for resource allocation during running, see API Call Sequence. Then, see the description of the resource allocation and release processes 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 application process
The key APIs are described as follows:
Allocate runtime resources, including the device and stream in sequence.
- Call the acl.rt.set_device API to specify the computation device. 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 destroyed when the acl.rt.destroy_context API is called. Therefore, you do not need to destroy 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 destroy the default stream by explicit call.
- The default context and stream are automatically released after the acl.rt.reset_device API is called.
- If the default stream is used as the input parameter of the API, set the value to 0. If the default stream is not used, call the acl.rt.create_stream API to explicitly create a stream.
- (Optional) Call the acl.rt.get_run_mode API to obtain the running mode of the software stack and determine the subsequent memory allocation API calling logic based on the running 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.
Runtime Resource Deallocation
The key APIs are described as follows:
- If the acl.rt.create_stream API is called to explicitly create a stream, the acl.rt.destroy_stream API needs to be called to release the stream. Otherwise, the acl.rt.destroy_stream API does not need to be called.
- Call the acl.rt.reset_device API to release the resources on the device. In addition, this API implicitly releases the default context and stream. You do not need to release the default context and stream separately.
Sample Code
You can obtain the complete sample code from Sample Introduction.
Following the API calls, add exception handling branches and specify log printing of error and information levels. 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===== #...... |