aclrtSetDevice

Description

Sets the compute device in the calling thread.

For the Atlas 200/300/500 Inference Product , 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.

For the Atlas Training Series Product , calling this API implicitly creates a default context that contains a default stream.

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 reference counting. It is recommended that aclrtResetDevice and aclrtSetDevice be used 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 this API once to release the resources on the device.

  • aclrtSetDevice can be called to specify the same device for computation in different processes or threads. In a multithreaded process, if aclrtSetDevice is called to specify the same compute device, they use the same default context created implicitly.
  • In the multi-device scenario, you can switch to another device by calling the aclrtSetDevice API in the process or by calling the aclrtSetCurrentContext API to switch the context.

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

The value 0 indicates success, and other values indicate failure. For details, see aclError.

See Also

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