cntk.device module

Utilities to specify device on which CNTK computation can be executed.

class DeviceDescriptor(*args, **kwargs)[source]

Bases: cntk.cntk_py.DeviceDescriptor

Describes a device by a unique id and its type. If the device corresponds to a GPU its type is 1, otherwise, it is 0

id()[source]

Returns id of device descriptor

Returns:id
Return type:int
is_locked()[source]

Returns True if another CNTK process already holds an exclusive lock on this device.

type()[source]

Returns type of device descriptor. 1 if it is a GPU device or 0 if CPU.

Returns:type
Return type:int
class DeviceKind[source]

Bases: enum.Enum

Describes different device kinds like CPU or GPU.

CPU = 0
GPU = 1
all_devices()[source]

Returns a device descriptor list with all the available devices

Returns:all device descriptors
Return type:DeviceDescriptor list
cpu()[source]

Returns CPU device descriptor

Returns:CPU device descriptor
Return type:DeviceDescriptor
get_gpu_properties(device)[source]

Retrieves and returns additional properties (total memory, number of CUDA cores, etc.) for the specified GPU device. This method will raise an exception if a CPU device is specified as an argument.

Parameters:device – a GPU device descriptor.
gpu(device_id)[source]

Returns GPU device descriptor

Returns:GPU device descriptor
Return type:DeviceDescriptor
set_excluded_devices(excluded_devices)[source]

Allows to specify a list of excluded devices that cannot be used as globally default (neither auto-selected nor explicitly specified by try_set_default_device()). For example, to avoid auto-selecting the CPU, invoke set_excluded_devices([cpu()]). However, after the default device has been selected and frozen (by a call to use_default_device()), invoking this methods has no effect, it becomes essentially a no-op.

Parameters:excluded_devices (list of DeviceDescriptor) – a list of device descriptors to exclude.
try_set_default_device(new_default_device, acquire_device_lock=False)[source]

Tries to set the specified device as the globally default, optionally acquiring an exclusive (cooperative) lock on the device (only a GPU device can be locked).

Parameters:
  • new_default_device (DeviceDescriptor) – a descriptor of the device to be used as a globally default.
  • acquire_device_lock (bool, defaults to False) – whether or not a lock should be acquired for the specified device.

The default device can only be changed if it has not yet been frozen by being implicitly used in any previous CNTK operation.

CNTK uses cooperative locking for the device access, whereby only a single process can acquire a device lock. This locking mechanism allows CNTK processes to avoid device oversubscription only if they collectively choose so. In other words, the device locked by one CNTK process, can still be accessed by another CNTK process without acquiring any locks (i.e, the existing device lock can be ignored by other CNTK processes). This cooperative locking mechanism does not guarantee any kind of exclusive access to the device. The proper way to ensure exclusivity is to use tools provided by NVIDIA (nvidia smi).

Returns: False if
  • the specified device appears in the list of excluded devices;
  • acquire_device_lock is True and another process already holds a lock on the device;
  • acquire_device_lock is True and new_default_device corresponds to a CPU device (which cannot be locked).
use_default_device()[source]

Freezes the default device of the current CNTK process disallowing further changes through calls to try_set_default_device(). This default device will used for all CNTK operations where a device needs to be specified and where none was explicitly provided. If no device has been specified with a call to try_set_default_device(), on the first invocation, this methods will auto-select one of the available (non-locked) devices as the default.

Returns:descriptor of the globally default device
Return type:DeviceDescriptor