aclrtSetDevice

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

Sets the compute device in the calling thread. aclrtSetDevice can be called to specify the same device for computation in different threads.

Calling this API implicitly creates a default context that contains a default stream. In multiple threads of the same process, if aclrtSetDevice is called and the same device is specified for computation, these threads share the same default context.

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

Prototype

1
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; otherwise, 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 neither of the two APIs are called, the function will still be normal because the device resources used by the process are 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 is incremented 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.

  • In the multi-device scenario, aclrtSetDevice can be called in the process to switch to another device.

API Call Example

For the API call example, see Initialization.