aclrtCreateContext

Applicability

Product

Supported

Atlas A3 training products/Atlas A3 inference products

Atlas A2 training products/Atlas A2 inference products

Atlas 200I/500 A2 inference products

Atlas inference products

Atlas training products

Description

Explicitly creates a context in the current thread and associates the current thread with the newly created context.

Prototype

aclError aclrtCreateContext(aclrtContext *context, int32_t deviceId)

Parameters

Parameter

Input/Output

Description

context

Output

Context pointer.

deviceId

Input

Device on which to create a context.

Must be in the range of [0, Device count – 1]. Call aclrtGetDeviceCount to obtain the device count.

Returns

0 on success; else, failure. For details, see aclError.

Restrictions

  • If aclrtCreateContext is not called to explicitly create a context, the system uses the default context, which is created implicitly with the aclrtSetDevice call.
    • Implicit context creation: applies to simple apps with low complicity of interaction logic. However, in multithreaded programming, the execution result depends on the thread scheduling sequence.
    • Explicit context creation: applies to large apps with complex interaction logic, offering better app readability and maintainability.
  • If a device is specified in a process, multiple threads in the process can share the context explicitly created on the device by calling aclrtCreateContext.
  • If multiple contexts are created in a process, the current thread can use only one context at a time. It is recommended that aclrtSetCurrentContext be used to specify the context of the current thread to improve program maintainability. (The number of contexts depends on the number of streams. For details, see aclrtCreateStream.)
  • The number of streams created implicitly using this API varies according to the product model, as shown in the table.

    Model

    Description of Default Stream

    Atlas inference products

    The context created by calling this API contains two streams, one as the default stream and the other for internal synchronization.

    Atlas A3 training products/Atlas A3 inference products

    Atlas A2 training products/Atlas A2 inference products

    Atlas 200I/500 A2 inference products

    Atlas training products

    The context created by calling this API contains one default stream.

  • If aclrtSetDevice is not called in the application, when aclrtCreateContext is called for the first time, the system binds a default stream to the device based on the device ID passed by the API (one device is bound to only one default stream). Therefore, when aclrtCreateContext is called for the first time, the number of occupied streams is calculated as follows: Number of occupied streams = Number of default streams bound to the device + Number of streams contained in the context.