cuda.core.experimental.Device

class cuda.core.experimental.Device(device_id=None)

Represent a GPU and act as an entry point for cuda.core features.

This is a singleton object that helps ensure interoperability across multiple libraries imported in the process to both see and use the same GPU device.

While acting as the entry point, many other CUDA resources can be allocated such as streams and buffers. Any Context dependent resource created through this device, will continue to refer to this device’s context.

Newly returend Device object are is a thread-local singleton for a specified device.

Note

Will not initialize the GPU.

Parameters:

device_id (int, optional) – Device ordinal to return a Device object for. Default value of None return the currently used device.

Methods

__init__()
allocate(size, stream=None) Buffer

Allocate device memory from a specified stream.

Allocates device memory of size bytes on the specified stream using the memory resource currently associated with this Device.

Parameter stream is optional, using a default stream by default.

Note

Device must be initialized.

Parameters:
  • size (int) – Number of bytes to allocate.

  • stream (Stream, optional) – The stream establishing the stream ordering semantic. Default value of None uses default stream.

Returns:

Newly created buffer object.

Return type:

Buffer

create_context(options: ContextOptions = None) Context

Create a new Context object.

Note

The newly context will not be set as current.

Parameters:

options (ContextOptions, optional) – Customizable dataclass for context creation options.

Returns:

Newly created context object.

Return type:

Context

create_stream(obj=None, options: StreamOptions = None) Stream

Create a Stream object.

New stream objects can be created in two different ways:

  1. Create a new CUDA stream with customizable options.

  2. Wrap an existing foreign obj supporting the __cuda_stream__ protocol.

Option (2) internally holds a reference to the foreign object such that the lifetime is managed.

Note

Device must be initialized.

Parameters:
  • obj (Any, optional) – Any object supporting the __cuda_stream__ protocol.

  • options (StreamOptions, optional) – Customizable dataclass for stream creation options.

Returns:

Newly created stream object.

Return type:

Stream

set_current(ctx: Context = None) Context | None

Set device to be used for GPU executions.

Initializes CUDA and sets the calling thread to a valid CUDA context. By default the primary context is used, but optional ctx parameter can be used to explicitly supply a Context object.

Providing a ctx causes the previous set context to be popped and returned.

Parameters:

ctx (Context, optional) – Optional context to push onto this device’s current thread stack.

Returns:

Popped context.

Return type:

Union[Context, None], optional

Examples

Acts as an entry point of this object. Users always start a code by calling this method, e.g.

>>> from cuda.core.experimental import Device
>>> dev0 = Device(0)
>>> dev0.set_current()
>>> # ... do work on device 0 ...
sync()

Synchronize the device.

Note

Device must be initialized.

Attributes

compute_capability

major and minor.

Type:

Return a named tuple with 2 fields

context

Return the current Context associated with this device.

Note

Device must be initialized.

default_stream

Return default CUDA Stream associated with this device.

The type of default stream returned depends on if the environment variable CUDA_PYTHON_CUDA_PER_THREAD_DEFAULT_STREAM is set.

If set, returns a per-thread default stream. Otherwise returns the legacy stream.

device_id

Return device ordinal.

memory_resource

Return MemoryResource associated with this device.

name

Return the device name.

pci_bus_id

Return a PCI Bus Id string for this device.

properties

Return information about the compute-device.

uuid

Return a UUID for the device.

Returns 16-octets identifying the device. If the device is in MIG mode, returns its MIG UUID which uniquely identifies the subscribed MIG compute instance.

Note

MIG UUID is only returned when device is in MIG mode and the driver is older than CUDA 11.4.