API Call Sequence

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 deallocation workflows in this section.

Runtime resources include the device, context, and stream.

Runtime Resource Allocation

Allocate the device, context, and stream resources in sequence. The context and stream can be created implicitly or explicitly.

Figure 1 Runtime resource allocation workflow

The key APIs are described as follows:

  1. Allocate the device, context, and stream resources in sequence.
    • Call aclrtSetDevice to explicitly set the compute device.
      • Call aclrtCreateContext to explicitly create a context and call aclrtCreateStream to explicitly create a stream.
      • If the context and stream are not created explicitly, the system uses the default context and stream, which are created implicitly with the aclrtSetDevice call.

        To pass the default stream to any API call, pass NULL directly.

    • Implicitly set the compute device.

      Call aclrtCreateContext to explicitly create a context and call aclrtCreateStream to explicitly create a stream. When a context is explicitly created, the compute device is set by the call to aclrtSetDevice. The device ID is passed to the aclrtCreateContext call.

  2. (Optional) Call aclrtGetRunMode 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 Transfer.

Runtime Resource Deallocation

Destroy the runtime allocations, including the stream, context, and device in sequence.

Figure 2 Runtime resource deallocation workflow

The key APIs are described as follows: