Function: set_device

Applicable Products

  • Ascend 950PR&950DT products: Supported
  • Atlas A3 products: Supported
  • Atlas A2 products: Supported
  • Atlas 200I/500 A2 inference products: Supported
  • Atlas inference products: Supported
  • Atlas training products: Supported

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

  • C Prototype

    aclError aclrtSetDevice(int32_t deviceId)
    
  • Python Function

    ret = acl.rt.set_device(device_id)
    

Parameters

Parameter Description
device_id Int, device ID.
Must be in the range of [0, Device count – 1]. Call acl.rt.get_device_count to obtain the device count.

Return Values

Return Value Description
ret Int, error code. If 0 is returned, the operation is successful. If other values are returned, the operation fails.

Constraints

If acl.rt.set_device is called for multiple times in a process, acl.rt.reset_device does not need to be called to reset the device resources, because the device resources will be reset when the process exits. You are advised to use acl.rt.set_device and acl.rt.reset_device in pairs. When the device resources are no longer needed, call acl.rt.reset_device to reset the device resources used by the current process promptly.

You can call acl.rt.set_device to specify the same device for computation in different processes or threads. In a multithreaded process, if acl.rt.set_device is called to specify the same compute device, they use the same default context.

Reference

For details about the API call process and example, see "Runtime Resource Allocation and Deallocation" and "Synchronous Wait" in "Application Development (Python)".