device-management.rst 2.37 KB
Newer Older
dugupeiwen's avatar
dugupeiwen committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92

Device management
=================

For multi-GPU machines, users may want to select which GPU to use.
By default the CUDA driver selects the fastest GPU as the device 0,
which is the default device used by Numba.

The features introduced on this page are generally not of interest
unless working with systems hosting/offering more than one CUDA-capable GPU.

Device Selection
----------------

If at all required, device selection must be done before any CUDA feature is
used.

::

    from numba import cuda
    cuda.select_device(0)

The device can be closed by:

::

    cuda.close()

Users can then create a new context with another device.

::

    cuda.select_device(1)  # assuming we have 2 GPUs


.. function:: numba.cuda.select_device(device_id)
   :noindex:

   Create a new CUDA context for the selected *device_id*.  *device_id*
   should be the number of the device (starting from 0; the device order
   is determined by the CUDA libraries).  The context is associated with
   the current thread.  Numba currently allows only one context per thread.

   If successful, this function returns a device instance.

   .. XXX document device instances?


.. function:: numba.cuda.close
   :noindex:

   Explicitly close all contexts in the current thread.

   .. note::
      Compiled functions are associated with the CUDA context.
      This makes it not very useful to close and create new devices, though it
      is certainly useful for choosing which device to use when the machine
      has multiple GPUs.

The Device List
===============

The Device List is a list of all the GPUs in the system, and can be indexed to
obtain a context manager that ensures execution on the selected GPU.

.. attribute:: numba.cuda.gpus
   :noindex:
.. attribute:: numba.cuda.cudadrv.devices.gpus

:py:data:`numba.cuda.gpus` is an instance of the ``_DeviceList`` class, from
which the current GPU context can also be retrieved:

.. autoclass:: numba.cuda.cudadrv.devices._DeviceList
    :members: current
    :noindex:


Device UUIDs
============

The UUID of a device (equal to that returned by ``nvidia-smi -L``) is available
in the :attr:`uuid <numba.cuda.cudadrv.driver.Device.uuid>` attribute of a CUDA
device object.

For example, to obtain the UUID of the current device:

.. code-block:: python

   dev = cuda.current_context().device
   # prints e.g. "GPU-e6489c45-5b68-3b03-bab7-0e7c8e809643"
   print(dev.uuid)