aclrtCreateContext

Applicability

Product

Supported

Atlas 350 Accelerator Card

Atlas A3 training product/Atlas A3 inference product

Atlas A2 training product/Atlas A2 inference product

Atlas 200I/500 A2 inference product

Atlas inference product

Atlas training product

Description

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

If the aclrtCreateContext interface is not called to display the created context, the system uses the default context, which is created implicitly when aclrtSetDevice is called. Default contexts apply to simple apps with low complexity of interaction logic. However, in multithreaded programming, the execution result depends on the thread scheduling sequence. Explicit context creation applies to large-scale applications with complex interaction logic, improving application readability and maintainability.

Prototype

1
aclError aclrtCreateContext(aclrtContext *context, int32_t deviceId)

Parameters

Parameter

Input/Output

Description

context

Output

Context pointer. For details about the type definition, see aclrtContext.

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; otherwise, failure. For details, see aclError.

Restrictions

  • If a compute device is specified in a process, threads in the process can share the device to explicitly create a context.
  • 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 the API for explicitly creating streams.)
  • The context created by calling this API contains one default stream.

    However, the EP standard form of the Atlas inference product is an exception. The context created by the EP standard form contains two streams: a default stream and a stream for internal synchronization.

  • 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.