aclrtSetDevice

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

Sets the compute device in the calling thread.

Prototype

aclError aclrtSetDevice(int32_t deviceId)

Parameters

Parameter

Input/Output

Description

deviceId

Input

Device ID.

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

  • After aclrtSetDevice is called to specify a device for computation, if the resources on the device are not used, you can call aclrtResetDevice or aclrtResetDeviceForce to release the device resources used by the process in a timely manner. (If the Reset API is not called, the device resources used by the process are still released when the process exits.)
    • If aclrtResetDevice is called to release device resources:

      aclrtResetDevice involves the implementation of the reference count. It is recommended that you use aclrtResetDevice and aclrtSetDevice in pairs. Each time aclrtSetDevice is called, the reference count increases by 1. Each time aclrtResetDevice is called, the reference count decreases by 1. Resources on the device are released only when the reference count decreases to 0.

    • If aclrtResetDeviceForce is called to release device resources:

      aclrtResetDeviceForce can either be used with aclrtSetDevice or not. If aclrtResetDeviceForce is not used with aclrtSetDevice, after aclrtSetDevice is called once or multiple times for the same device in a process, you only need to call aclrtResetDeviceForce once to release the resources on the device.

  • When aclrtSetDevice is called, a default context is implicitly created. If aclrtSetDevice is called in multiple threads of the same process and the same device is specified for computation, these threads share the same default context.
  • In the multi-device scenario, aclrtSetDevice can be called in the process to switch to another device.
  • The number of streams created implicitly using this API varies according to the product model, as shown in the table.

    Model

    Description of Default Context and Default Stream

    Atlas inference products

    Calling this API implicitly creates a default context. The default context 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

    Calling this API implicitly creates a default context that contains a default stream.

See Also

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