aclrtCreateContext
Applicable Products
Product |
Supported |
|---|---|
√ |
|
√ |
|
√ |
|
√ |
|
√ |
|
√ |
Function
Explicitly creates a context in the current thread and associates the current thread with the created context.
If the aclrtCreateContext API is not called to explicitly create a context, the system uses the default context, which is implicitly created when aclrtSetDevice is called. The default context is suitable for simple applications without complex interaction logic. However, in multi-thread programming, the execution result depends on the thread scheduling sequence. Explicitly created contexts are suitable for large-scale applications with complex interaction logic to improve program 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. The value must be in the range of [0, (Available device count – 1)]. Call aclrtGetDeviceCount to obtain the device count. |
Returns
0 on success; otherwise, failure. For details, see aclError.
Restrictions
- If a device is specified in a process, multiple threads in the process can share the context explicitly created on the device.
- If multiple contexts are created in a process, the number of contexts is related to the number of streams. The number of streams is limited. For details, see the API for explicitly creating a stream. The current thread can use only one context at a time. You are advised to specify a context for the current thread by calling aclrtSetCurrentContext to improve program maintainability.
- The context created by calling this API contains a default stream.
However, the EP standard form of the
Atlas inference products is an exception. The context contains two streams: a default stream and a stream for internal synchronization. - If aclrtSetDevice is not called in an 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 (only one default stream is bound to a device). 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.