Function: set_device

Description

When this API is called, the default context is created implicitly. The default context 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

  • C Prototype
    1
    aclErroraclrtSetDevice(int32_tdeviceId)
    
  • Python Function
    1
    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 Value

Return Value

Description

ret

Int, error code. 0 on success; else, failure.

Restrictions

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. It is recommended that the acl.rt.set_device and acl.rt.reset_device APIs be used in pairs. When the device resources are not used, call the acl.rt.reset_device API to release the device resources used by the process in a timely manner.

acl.rt.set_device can be called 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 sequence and examples, see Runtime Resource Allocation and Deallocation and Synchronous Wait.