Function: set_device

Applicable Products

Product

Supported (√/x)

Ascend 950PR / Ascend 950DT

Atlas A3 training products / Atlas A3 inference products

Atlas A2 training products / Atlas A2 inference products

Atlas training products

Atlas inference products

Atlas 200I/500 A2 inference products

Description

Sets the compute device in the calling thread. The call implicitly creates the default context.

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 products 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
    aclError aclrtSetDevice(int32_t deviceId)
    
  • 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 Values

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.