Function: set_device

Applicability

Product

Supported (√/x)

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

Function Usage

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

For the Atlas 200I/500 A2 inference products , the default context contains one default stream.

For the Atlas training products , the default context contains one default stream.

For the Atlas A2 training products / Atlas A2 inference products , the default context contains one default stream.

For the Atlas inference products , in standard form, the context contains two streams: one default stream and one stream for internal synchronization.

For the Atlas A3 training products / Atlas A3 inference products , calling this API implicitly creates a default context that contains a default stream.

Prototype

  • C Prototype
    1
    aclError aclrtSetDevice(int32_t deviceId)
    
  • Python Function
    1
    ret = acl.rt.set_device(device_id)
    

Parameter Description

Parameter

Description

device_id

Int, device ID.

After acl.rt.get_device_count is called to obtain the number of available devices, the value range of device ID is [0, Number of available devices – 1].

Return Value Description

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 resources on the device are not used, you can call acl.rt.reset_device to free the device resources used by the current 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 example, see Runtime Resource Allocation and Deallocation and Synchronous Wait.