Function: create_context

Applicability

Product

Supported (√/x)

Atlas A3 training products / Atlas A3 inference products

Atlas A2 training products / Atlas A2 inference products

Atlas training products

Atlas inference products

Atlas 200I/500 A2 inference products

Function Usage

Explicitly creates a context in the current process or thread.

For the Atlas 200I/500 A2 inference products , the context contains two streams: one default stream and one stream for internal synchronization.

For the Atlas training products , the context contains one default stream.

For the Atlas A2 training products / Atlas A2 inference products , the context contains one default stream.

For the Atlas inference products , in standard form, the context contains two streams: one default stream and one stream for internal synchronization.

For the Atlas A3 training products / Atlas A3 inference products , the context contains one default stream.

If acl.rt.set_device is not called in the app, when acl.rt.create_context 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 acl.rt.create_context 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.

Prototype

  • C Prototype
    1
    aclError aclrtCreateContext(aclrtContext *context, int32_t deviceId)
    
  • Python Function
    1
    context, ret = acl.rt.create_context(device_id)
    

Parameter Description

Parameter

Description

device_id

Int, ID of the device for which the context is to be created.

Return Value Description

Return Value

Description

context

Int, pointer address of the created context.

ret

Int, error code: 0 on success; else, failure.

Restrictions

The following use cases are supported:

  • If the acl.rt.create_context API is not called to display the created context, the system uses the default context, which is created implicitly when acl.rt.set_device is called.
    • 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 acl.rt.create_context.
  • If multiple contexts are created in a process, the current thread can use only one context at a time. It is recommended that acl.rt.set_context 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 acl.rt.create_stream.

Reference

For details about the API call sequence and example, see Runtime Resource Allocation and Deallocation.