aclrtSetDevice

Applicable Products

Product

Supported

Ascend 950PR / Ascend 950DT

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

Function

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

If there are multiple devices, aclrtSetDevice can be called in the process to switch to another device.

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.

Prototype

1
aclError aclrtSetDevice(int32_t deviceId)

Parameters

Parameter

Input/Output

Description

deviceId

Input

Device ID.

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

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 device resources will be released when the process exits.)
  • If aclrtResetDevice is called to release device resources:

    aclrtResetDevice involves reference counting. You are advised to 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.

API Call Example

For the API call example, see Initialization.