numba-cuda 0.0.0__py3-none-any.whl → 0.0.12__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- _numba_cuda_redirector.pth +1 -0
- _numba_cuda_redirector.py +74 -0
- numba_cuda/VERSION +1 -0
- numba_cuda/__init__.py +5 -0
- numba_cuda/_version.py +19 -0
- numba_cuda/numba/cuda/__init__.py +22 -0
- numba_cuda/numba/cuda/api.py +526 -0
- numba_cuda/numba/cuda/api_util.py +30 -0
- numba_cuda/numba/cuda/args.py +77 -0
- numba_cuda/numba/cuda/cg.py +62 -0
- numba_cuda/numba/cuda/codegen.py +378 -0
- numba_cuda/numba/cuda/compiler.py +422 -0
- numba_cuda/numba/cuda/cpp_function_wrappers.cu +47 -0
- numba_cuda/numba/cuda/cuda_fp16.h +3631 -0
- numba_cuda/numba/cuda/cuda_fp16.hpp +2465 -0
- numba_cuda/numba/cuda/cuda_paths.py +258 -0
- numba_cuda/numba/cuda/cudadecl.py +806 -0
- numba_cuda/numba/cuda/cudadrv/__init__.py +9 -0
- numba_cuda/numba/cuda/cudadrv/devicearray.py +904 -0
- numba_cuda/numba/cuda/cudadrv/devices.py +248 -0
- numba_cuda/numba/cuda/cudadrv/driver.py +3201 -0
- numba_cuda/numba/cuda/cudadrv/drvapi.py +398 -0
- numba_cuda/numba/cuda/cudadrv/dummyarray.py +452 -0
- numba_cuda/numba/cuda/cudadrv/enums.py +607 -0
- numba_cuda/numba/cuda/cudadrv/error.py +36 -0
- numba_cuda/numba/cuda/cudadrv/libs.py +176 -0
- numba_cuda/numba/cuda/cudadrv/ndarray.py +20 -0
- numba_cuda/numba/cuda/cudadrv/nvrtc.py +260 -0
- numba_cuda/numba/cuda/cudadrv/nvvm.py +707 -0
- numba_cuda/numba/cuda/cudadrv/rtapi.py +10 -0
- numba_cuda/numba/cuda/cudadrv/runtime.py +142 -0
- numba_cuda/numba/cuda/cudaimpl.py +1055 -0
- numba_cuda/numba/cuda/cudamath.py +140 -0
- numba_cuda/numba/cuda/decorators.py +189 -0
- numba_cuda/numba/cuda/descriptor.py +33 -0
- numba_cuda/numba/cuda/device_init.py +89 -0
- numba_cuda/numba/cuda/deviceufunc.py +908 -0
- numba_cuda/numba/cuda/dispatcher.py +1057 -0
- numba_cuda/numba/cuda/errors.py +59 -0
- numba_cuda/numba/cuda/extending.py +7 -0
- numba_cuda/numba/cuda/initialize.py +13 -0
- numba_cuda/numba/cuda/intrinsic_wrapper.py +77 -0
- numba_cuda/numba/cuda/intrinsics.py +198 -0
- numba_cuda/numba/cuda/kernels/__init__.py +0 -0
- numba_cuda/numba/cuda/kernels/reduction.py +262 -0
- numba_cuda/numba/cuda/kernels/transpose.py +65 -0
- numba_cuda/numba/cuda/libdevice.py +3382 -0
- numba_cuda/numba/cuda/libdevicedecl.py +17 -0
- numba_cuda/numba/cuda/libdevicefuncs.py +1057 -0
- numba_cuda/numba/cuda/libdeviceimpl.py +83 -0
- numba_cuda/numba/cuda/mathimpl.py +448 -0
- numba_cuda/numba/cuda/models.py +48 -0
- numba_cuda/numba/cuda/nvvmutils.py +235 -0
- numba_cuda/numba/cuda/printimpl.py +86 -0
- numba_cuda/numba/cuda/random.py +292 -0
- numba_cuda/numba/cuda/simulator/__init__.py +38 -0
- numba_cuda/numba/cuda/simulator/api.py +110 -0
- numba_cuda/numba/cuda/simulator/compiler.py +9 -0
- numba_cuda/numba/cuda/simulator/cudadrv/__init__.py +2 -0
- numba_cuda/numba/cuda/simulator/cudadrv/devicearray.py +432 -0
- numba_cuda/numba/cuda/simulator/cudadrv/devices.py +117 -0
- numba_cuda/numba/cuda/simulator/cudadrv/driver.py +62 -0
- numba_cuda/numba/cuda/simulator/cudadrv/drvapi.py +4 -0
- numba_cuda/numba/cuda/simulator/cudadrv/dummyarray.py +4 -0
- numba_cuda/numba/cuda/simulator/cudadrv/error.py +6 -0
- numba_cuda/numba/cuda/simulator/cudadrv/libs.py +2 -0
- numba_cuda/numba/cuda/simulator/cudadrv/nvvm.py +29 -0
- numba_cuda/numba/cuda/simulator/cudadrv/runtime.py +19 -0
- numba_cuda/numba/cuda/simulator/kernel.py +308 -0
- numba_cuda/numba/cuda/simulator/kernelapi.py +495 -0
- numba_cuda/numba/cuda/simulator/reduction.py +15 -0
- numba_cuda/numba/cuda/simulator/vector_types.py +58 -0
- numba_cuda/numba/cuda/simulator_init.py +17 -0
- numba_cuda/numba/cuda/stubs.py +902 -0
- numba_cuda/numba/cuda/target.py +440 -0
- numba_cuda/numba/cuda/testing.py +202 -0
- numba_cuda/numba/cuda/tests/__init__.py +58 -0
- numba_cuda/numba/cuda/tests/cudadrv/__init__.py +8 -0
- numba_cuda/numba/cuda/tests/cudadrv/test_array_attr.py +145 -0
- numba_cuda/numba/cuda/tests/cudadrv/test_context_stack.py +145 -0
- numba_cuda/numba/cuda/tests/cudadrv/test_cuda_array_slicing.py +375 -0
- numba_cuda/numba/cuda/tests/cudadrv/test_cuda_auto_context.py +21 -0
- numba_cuda/numba/cuda/tests/cudadrv/test_cuda_devicerecord.py +179 -0
- numba_cuda/numba/cuda/tests/cudadrv/test_cuda_driver.py +235 -0
- numba_cuda/numba/cuda/tests/cudadrv/test_cuda_libraries.py +22 -0
- numba_cuda/numba/cuda/tests/cudadrv/test_cuda_memory.py +193 -0
- numba_cuda/numba/cuda/tests/cudadrv/test_cuda_ndarray.py +547 -0
- numba_cuda/numba/cuda/tests/cudadrv/test_deallocations.py +249 -0
- numba_cuda/numba/cuda/tests/cudadrv/test_detect.py +81 -0
- numba_cuda/numba/cuda/tests/cudadrv/test_emm_plugins.py +192 -0
- numba_cuda/numba/cuda/tests/cudadrv/test_events.py +38 -0
- numba_cuda/numba/cuda/tests/cudadrv/test_host_alloc.py +65 -0
- numba_cuda/numba/cuda/tests/cudadrv/test_init.py +139 -0
- numba_cuda/numba/cuda/tests/cudadrv/test_inline_ptx.py +37 -0
- numba_cuda/numba/cuda/tests/cudadrv/test_is_fp16.py +12 -0
- numba_cuda/numba/cuda/tests/cudadrv/test_linker.py +317 -0
- numba_cuda/numba/cuda/tests/cudadrv/test_managed_alloc.py +127 -0
- numba_cuda/numba/cuda/tests/cudadrv/test_mvc.py +54 -0
- numba_cuda/numba/cuda/tests/cudadrv/test_nvvm_driver.py +199 -0
- numba_cuda/numba/cuda/tests/cudadrv/test_pinned.py +37 -0
- numba_cuda/numba/cuda/tests/cudadrv/test_profiler.py +20 -0
- numba_cuda/numba/cuda/tests/cudadrv/test_ptds.py +149 -0
- numba_cuda/numba/cuda/tests/cudadrv/test_reset_device.py +36 -0
- numba_cuda/numba/cuda/tests/cudadrv/test_runtime.py +85 -0
- numba_cuda/numba/cuda/tests/cudadrv/test_select_device.py +41 -0
- numba_cuda/numba/cuda/tests/cudadrv/test_streams.py +122 -0
- numba_cuda/numba/cuda/tests/cudapy/__init__.py +8 -0
- numba_cuda/numba/cuda/tests/cudapy/cache_usecases.py +234 -0
- numba_cuda/numba/cuda/tests/cudapy/cache_with_cpu_usecases.py +41 -0
- numba_cuda/numba/cuda/tests/cudapy/extensions_usecases.py +58 -0
- numba_cuda/numba/cuda/tests/cudapy/jitlink.ptx +30 -0
- numba_cuda/numba/cuda/tests/cudapy/recursion_usecases.py +100 -0
- numba_cuda/numba/cuda/tests/cudapy/test_alignment.py +42 -0
- numba_cuda/numba/cuda/tests/cudapy/test_array.py +260 -0
- numba_cuda/numba/cuda/tests/cudapy/test_array_args.py +201 -0
- numba_cuda/numba/cuda/tests/cudapy/test_array_methods.py +35 -0
- numba_cuda/numba/cuda/tests/cudapy/test_atomics.py +1620 -0
- numba_cuda/numba/cuda/tests/cudapy/test_blackscholes.py +120 -0
- numba_cuda/numba/cuda/tests/cudapy/test_boolean.py +24 -0
- numba_cuda/numba/cuda/tests/cudapy/test_caching.py +545 -0
- numba_cuda/numba/cuda/tests/cudapy/test_casting.py +257 -0
- numba_cuda/numba/cuda/tests/cudapy/test_cffi.py +33 -0
- numba_cuda/numba/cuda/tests/cudapy/test_compiler.py +276 -0
- numba_cuda/numba/cuda/tests/cudapy/test_complex.py +296 -0
- numba_cuda/numba/cuda/tests/cudapy/test_complex_kernel.py +20 -0
- numba_cuda/numba/cuda/tests/cudapy/test_const_string.py +129 -0
- numba_cuda/numba/cuda/tests/cudapy/test_constmem.py +176 -0
- numba_cuda/numba/cuda/tests/cudapy/test_cooperative_groups.py +147 -0
- numba_cuda/numba/cuda/tests/cudapy/test_cuda_array_interface.py +435 -0
- numba_cuda/numba/cuda/tests/cudapy/test_cuda_jit_no_types.py +90 -0
- numba_cuda/numba/cuda/tests/cudapy/test_datetime.py +94 -0
- numba_cuda/numba/cuda/tests/cudapy/test_debug.py +101 -0
- numba_cuda/numba/cuda/tests/cudapy/test_debuginfo.py +221 -0
- numba_cuda/numba/cuda/tests/cudapy/test_device_func.py +222 -0
- numba_cuda/numba/cuda/tests/cudapy/test_dispatcher.py +700 -0
- numba_cuda/numba/cuda/tests/cudapy/test_enums.py +121 -0
- numba_cuda/numba/cuda/tests/cudapy/test_errors.py +79 -0
- numba_cuda/numba/cuda/tests/cudapy/test_exception.py +174 -0
- numba_cuda/numba/cuda/tests/cudapy/test_extending.py +155 -0
- numba_cuda/numba/cuda/tests/cudapy/test_fastmath.py +244 -0
- numba_cuda/numba/cuda/tests/cudapy/test_forall.py +52 -0
- numba_cuda/numba/cuda/tests/cudapy/test_freevar.py +29 -0
- numba_cuda/numba/cuda/tests/cudapy/test_frexp_ldexp.py +66 -0
- numba_cuda/numba/cuda/tests/cudapy/test_globals.py +60 -0
- numba_cuda/numba/cuda/tests/cudapy/test_gufunc.py +456 -0
- numba_cuda/numba/cuda/tests/cudapy/test_gufunc_scalar.py +159 -0
- numba_cuda/numba/cuda/tests/cudapy/test_gufunc_scheduling.py +95 -0
- numba_cuda/numba/cuda/tests/cudapy/test_idiv.py +37 -0
- numba_cuda/numba/cuda/tests/cudapy/test_inspect.py +165 -0
- numba_cuda/numba/cuda/tests/cudapy/test_intrinsics.py +1106 -0
- numba_cuda/numba/cuda/tests/cudapy/test_ipc.py +318 -0
- numba_cuda/numba/cuda/tests/cudapy/test_iterators.py +99 -0
- numba_cuda/numba/cuda/tests/cudapy/test_lang.py +64 -0
- numba_cuda/numba/cuda/tests/cudapy/test_laplace.py +119 -0
- numba_cuda/numba/cuda/tests/cudapy/test_libdevice.py +187 -0
- numba_cuda/numba/cuda/tests/cudapy/test_lineinfo.py +199 -0
- numba_cuda/numba/cuda/tests/cudapy/test_localmem.py +164 -0
- numba_cuda/numba/cuda/tests/cudapy/test_mandel.py +37 -0
- numba_cuda/numba/cuda/tests/cudapy/test_math.py +786 -0
- numba_cuda/numba/cuda/tests/cudapy/test_matmul.py +74 -0
- numba_cuda/numba/cuda/tests/cudapy/test_minmax.py +113 -0
- numba_cuda/numba/cuda/tests/cudapy/test_montecarlo.py +22 -0
- numba_cuda/numba/cuda/tests/cudapy/test_multigpu.py +140 -0
- numba_cuda/numba/cuda/tests/cudapy/test_multiprocessing.py +46 -0
- numba_cuda/numba/cuda/tests/cudapy/test_multithreads.py +101 -0
- numba_cuda/numba/cuda/tests/cudapy/test_nondet.py +49 -0
- numba_cuda/numba/cuda/tests/cudapy/test_operator.py +401 -0
- numba_cuda/numba/cuda/tests/cudapy/test_optimization.py +86 -0
- numba_cuda/numba/cuda/tests/cudapy/test_overload.py +335 -0
- numba_cuda/numba/cuda/tests/cudapy/test_powi.py +124 -0
- numba_cuda/numba/cuda/tests/cudapy/test_print.py +128 -0
- numba_cuda/numba/cuda/tests/cudapy/test_py2_div_issue.py +33 -0
- numba_cuda/numba/cuda/tests/cudapy/test_random.py +104 -0
- numba_cuda/numba/cuda/tests/cudapy/test_record_dtype.py +610 -0
- numba_cuda/numba/cuda/tests/cudapy/test_recursion.py +125 -0
- numba_cuda/numba/cuda/tests/cudapy/test_reduction.py +76 -0
- numba_cuda/numba/cuda/tests/cudapy/test_retrieve_autoconverted_arrays.py +83 -0
- numba_cuda/numba/cuda/tests/cudapy/test_serialize.py +85 -0
- numba_cuda/numba/cuda/tests/cudapy/test_slicing.py +37 -0
- numba_cuda/numba/cuda/tests/cudapy/test_sm.py +444 -0
- numba_cuda/numba/cuda/tests/cudapy/test_sm_creation.py +205 -0
- numba_cuda/numba/cuda/tests/cudapy/test_sync.py +271 -0
- numba_cuda/numba/cuda/tests/cudapy/test_transpose.py +80 -0
- numba_cuda/numba/cuda/tests/cudapy/test_ufuncs.py +277 -0
- numba_cuda/numba/cuda/tests/cudapy/test_userexc.py +47 -0
- numba_cuda/numba/cuda/tests/cudapy/test_vector_type.py +307 -0
- numba_cuda/numba/cuda/tests/cudapy/test_vectorize.py +283 -0
- numba_cuda/numba/cuda/tests/cudapy/test_vectorize_complex.py +20 -0
- numba_cuda/numba/cuda/tests/cudapy/test_vectorize_decor.py +69 -0
- numba_cuda/numba/cuda/tests/cudapy/test_vectorize_device.py +36 -0
- numba_cuda/numba/cuda/tests/cudapy/test_vectorize_scalar_arg.py +37 -0
- numba_cuda/numba/cuda/tests/cudapy/test_warning.py +139 -0
- numba_cuda/numba/cuda/tests/cudapy/test_warp_ops.py +276 -0
- numba_cuda/numba/cuda/tests/cudasim/__init__.py +6 -0
- numba_cuda/numba/cuda/tests/cudasim/support.py +6 -0
- numba_cuda/numba/cuda/tests/cudasim/test_cudasim_issues.py +102 -0
- numba_cuda/numba/cuda/tests/data/__init__.py +0 -0
- numba_cuda/numba/cuda/tests/data/cuda_include.cu +5 -0
- numba_cuda/numba/cuda/tests/data/error.cu +7 -0
- numba_cuda/numba/cuda/tests/data/jitlink.cu +23 -0
- numba_cuda/numba/cuda/tests/data/jitlink.ptx +51 -0
- numba_cuda/numba/cuda/tests/data/warn.cu +7 -0
- numba_cuda/numba/cuda/tests/doc_examples/__init__.py +6 -0
- numba_cuda/numba/cuda/tests/doc_examples/ffi/__init__.py +0 -0
- numba_cuda/numba/cuda/tests/doc_examples/ffi/functions.cu +49 -0
- numba_cuda/numba/cuda/tests/doc_examples/test_cg.py +77 -0
- numba_cuda/numba/cuda/tests/doc_examples/test_cpu_gpu_compat.py +76 -0
- numba_cuda/numba/cuda/tests/doc_examples/test_ffi.py +82 -0
- numba_cuda/numba/cuda/tests/doc_examples/test_laplace.py +155 -0
- numba_cuda/numba/cuda/tests/doc_examples/test_matmul.py +173 -0
- numba_cuda/numba/cuda/tests/doc_examples/test_montecarlo.py +109 -0
- numba_cuda/numba/cuda/tests/doc_examples/test_random.py +59 -0
- numba_cuda/numba/cuda/tests/doc_examples/test_reduction.py +76 -0
- numba_cuda/numba/cuda/tests/doc_examples/test_sessionize.py +130 -0
- numba_cuda/numba/cuda/tests/doc_examples/test_ufunc.py +50 -0
- numba_cuda/numba/cuda/tests/doc_examples/test_vecadd.py +73 -0
- numba_cuda/numba/cuda/tests/nocuda/__init__.py +8 -0
- numba_cuda/numba/cuda/tests/nocuda/test_dummyarray.py +359 -0
- numba_cuda/numba/cuda/tests/nocuda/test_function_resolution.py +36 -0
- numba_cuda/numba/cuda/tests/nocuda/test_import.py +49 -0
- numba_cuda/numba/cuda/tests/nocuda/test_library_lookup.py +238 -0
- numba_cuda/numba/cuda/tests/nocuda/test_nvvm.py +54 -0
- numba_cuda/numba/cuda/types.py +37 -0
- numba_cuda/numba/cuda/ufuncs.py +662 -0
- numba_cuda/numba/cuda/vector_types.py +209 -0
- numba_cuda/numba/cuda/vectorizers.py +252 -0
- numba_cuda-0.0.12.dist-info/LICENSE +25 -0
- numba_cuda-0.0.12.dist-info/METADATA +68 -0
- numba_cuda-0.0.12.dist-info/RECORD +231 -0
- {numba_cuda-0.0.0.dist-info → numba_cuda-0.0.12.dist-info}/WHEEL +1 -1
- numba_cuda-0.0.0.dist-info/METADATA +0 -6
- numba_cuda-0.0.0.dist-info/RECORD +0 -5
- {numba_cuda-0.0.0.dist-info → numba_cuda-0.0.12.dist-info}/top_level.txt +0 -0
@@ -0,0 +1,248 @@
|
|
1
|
+
"""
|
2
|
+
Expose each GPU devices directly.
|
3
|
+
|
4
|
+
This module implements a API that is like the "CUDA runtime" context manager
|
5
|
+
for managing CUDA context stack and clean up. It relies on thread-local globals
|
6
|
+
to separate the context stack management of each thread. Contexts are also
|
7
|
+
shareable among threads. Only the main thread can destroy Contexts.
|
8
|
+
|
9
|
+
Note:
|
10
|
+
- This module must be imported by the main-thread.
|
11
|
+
|
12
|
+
"""
|
13
|
+
import functools
|
14
|
+
import threading
|
15
|
+
from contextlib import contextmanager
|
16
|
+
|
17
|
+
from .driver import driver, USE_NV_BINDING
|
18
|
+
|
19
|
+
|
20
|
+
class _DeviceList(object):
|
21
|
+
def __getattr__(self, attr):
|
22
|
+
# First time looking at "lst" attribute.
|
23
|
+
if attr == "lst":
|
24
|
+
# Device list is not initialized.
|
25
|
+
# Query all CUDA devices.
|
26
|
+
numdev = driver.get_device_count()
|
27
|
+
gpus = [_DeviceContextManager(driver.get_device(devid))
|
28
|
+
for devid in range(numdev)]
|
29
|
+
# Define "lst" to avoid re-initialization
|
30
|
+
self.lst = gpus
|
31
|
+
return gpus
|
32
|
+
|
33
|
+
# Other attributes
|
34
|
+
return super(_DeviceList, self).__getattr__(attr)
|
35
|
+
|
36
|
+
def __getitem__(self, devnum):
|
37
|
+
'''
|
38
|
+
Returns the context manager for device *devnum*.
|
39
|
+
'''
|
40
|
+
return self.lst[devnum]
|
41
|
+
|
42
|
+
def __str__(self):
|
43
|
+
return ', '.join([str(d) for d in self.lst])
|
44
|
+
|
45
|
+
def __iter__(self):
|
46
|
+
return iter(self.lst)
|
47
|
+
|
48
|
+
def __len__(self):
|
49
|
+
return len(self.lst)
|
50
|
+
|
51
|
+
@property
|
52
|
+
def current(self):
|
53
|
+
"""Returns the active device or None if there's no active device
|
54
|
+
"""
|
55
|
+
with driver.get_active_context() as ac:
|
56
|
+
devnum = ac.devnum
|
57
|
+
if devnum is not None:
|
58
|
+
return self[devnum]
|
59
|
+
|
60
|
+
|
61
|
+
class _DeviceContextManager(object):
|
62
|
+
"""
|
63
|
+
Provides a context manager for executing in the context of the chosen
|
64
|
+
device. The normal use of instances of this type is from
|
65
|
+
``numba.cuda.gpus``. For example, to execute on device 2::
|
66
|
+
|
67
|
+
with numba.cuda.gpus[2]:
|
68
|
+
d_a = numba.cuda.to_device(a)
|
69
|
+
|
70
|
+
to copy the array *a* onto device 2, referred to by *d_a*.
|
71
|
+
"""
|
72
|
+
|
73
|
+
def __init__(self, device):
|
74
|
+
self._device = device
|
75
|
+
|
76
|
+
def __getattr__(self, item):
|
77
|
+
return getattr(self._device, item)
|
78
|
+
|
79
|
+
def __enter__(self):
|
80
|
+
_runtime.get_or_create_context(self._device.id)
|
81
|
+
|
82
|
+
def __exit__(self, exc_type, exc_val, exc_tb):
|
83
|
+
# this will verify that we are popping the right device context.
|
84
|
+
self._device.get_primary_context().pop()
|
85
|
+
|
86
|
+
def __str__(self):
|
87
|
+
return "<Managed Device {self.id}>".format(self=self)
|
88
|
+
|
89
|
+
|
90
|
+
class _Runtime(object):
|
91
|
+
"""Emulate the CUDA runtime context management.
|
92
|
+
|
93
|
+
It owns all Devices and Contexts.
|
94
|
+
Keeps at most one Context per Device
|
95
|
+
"""
|
96
|
+
|
97
|
+
def __init__(self):
|
98
|
+
self.gpus = _DeviceList()
|
99
|
+
|
100
|
+
# For caching the attached CUDA Context
|
101
|
+
self._tls = threading.local()
|
102
|
+
|
103
|
+
# Remember the main thread
|
104
|
+
# Only the main thread can *actually* destroy
|
105
|
+
self._mainthread = threading.current_thread()
|
106
|
+
|
107
|
+
# Avoid mutation of runtime state in multithreaded programs
|
108
|
+
self._lock = threading.RLock()
|
109
|
+
|
110
|
+
@contextmanager
|
111
|
+
def ensure_context(self):
|
112
|
+
"""Ensure a CUDA context is available inside the context.
|
113
|
+
|
114
|
+
On entrance, queries the CUDA driver for an active CUDA context and
|
115
|
+
attaches it in TLS for subsequent calls so they do not need to query
|
116
|
+
the CUDA driver again. On exit, detach the CUDA context from the TLS.
|
117
|
+
|
118
|
+
This will allow us to pickup thirdparty activated CUDA context in
|
119
|
+
any top-level Numba CUDA API.
|
120
|
+
"""
|
121
|
+
with driver.get_active_context():
|
122
|
+
oldctx = self._get_attached_context()
|
123
|
+
newctx = self.get_or_create_context(None)
|
124
|
+
self._set_attached_context(newctx)
|
125
|
+
try:
|
126
|
+
yield
|
127
|
+
finally:
|
128
|
+
self._set_attached_context(oldctx)
|
129
|
+
|
130
|
+
def get_or_create_context(self, devnum):
|
131
|
+
"""Returns the primary context and push+create it if needed
|
132
|
+
for *devnum*. If *devnum* is None, use the active CUDA context (must
|
133
|
+
be primary) or create a new one with ``devnum=0``.
|
134
|
+
"""
|
135
|
+
if devnum is None:
|
136
|
+
attached_ctx = self._get_attached_context()
|
137
|
+
if attached_ctx is None:
|
138
|
+
return self._get_or_create_context_uncached(devnum)
|
139
|
+
else:
|
140
|
+
return attached_ctx
|
141
|
+
else:
|
142
|
+
if USE_NV_BINDING:
|
143
|
+
devnum = int(devnum)
|
144
|
+
return self._activate_context_for(devnum)
|
145
|
+
|
146
|
+
def _get_or_create_context_uncached(self, devnum):
|
147
|
+
"""See also ``get_or_create_context(devnum)``.
|
148
|
+
This version does not read the cache.
|
149
|
+
"""
|
150
|
+
with self._lock:
|
151
|
+
# Try to get the active context in the CUDA stack or
|
152
|
+
# activate GPU-0 with the primary context
|
153
|
+
with driver.get_active_context() as ac:
|
154
|
+
if not ac:
|
155
|
+
return self._activate_context_for(0)
|
156
|
+
else:
|
157
|
+
# Get primary context for the active device
|
158
|
+
ctx = self.gpus[ac.devnum].get_primary_context()
|
159
|
+
# Is active context the primary context?
|
160
|
+
if USE_NV_BINDING:
|
161
|
+
ctx_handle = int(ctx.handle)
|
162
|
+
ac_ctx_handle = int(ac.context_handle)
|
163
|
+
else:
|
164
|
+
ctx_handle = ctx.handle.value
|
165
|
+
ac_ctx_handle = ac.context_handle.value
|
166
|
+
if ctx_handle != ac_ctx_handle:
|
167
|
+
msg = ('Numba cannot operate on non-primary'
|
168
|
+
' CUDA context {:x}')
|
169
|
+
raise RuntimeError(msg.format(ac_ctx_handle))
|
170
|
+
# Ensure the context is ready
|
171
|
+
ctx.prepare_for_use()
|
172
|
+
return ctx
|
173
|
+
|
174
|
+
def _activate_context_for(self, devnum):
|
175
|
+
with self._lock:
|
176
|
+
gpu = self.gpus[devnum]
|
177
|
+
newctx = gpu.get_primary_context()
|
178
|
+
# Detect unexpected context switch
|
179
|
+
cached_ctx = self._get_attached_context()
|
180
|
+
if cached_ctx is not None and cached_ctx is not newctx:
|
181
|
+
raise RuntimeError('Cannot switch CUDA-context.')
|
182
|
+
newctx.push()
|
183
|
+
return newctx
|
184
|
+
|
185
|
+
def _get_attached_context(self):
|
186
|
+
return getattr(self._tls, 'attached_context', None)
|
187
|
+
|
188
|
+
def _set_attached_context(self, ctx):
|
189
|
+
self._tls.attached_context = ctx
|
190
|
+
|
191
|
+
def reset(self):
|
192
|
+
"""Clear all contexts in the thread. Destroy the context if and only
|
193
|
+
if we are in the main thread.
|
194
|
+
"""
|
195
|
+
# Pop all active context.
|
196
|
+
while driver.pop_active_context() is not None:
|
197
|
+
pass
|
198
|
+
|
199
|
+
# If it is the main thread
|
200
|
+
if threading.current_thread() == self._mainthread:
|
201
|
+
self._destroy_all_contexts()
|
202
|
+
|
203
|
+
def _destroy_all_contexts(self):
|
204
|
+
# Reset all devices
|
205
|
+
for gpu in self.gpus:
|
206
|
+
gpu.reset()
|
207
|
+
|
208
|
+
|
209
|
+
_runtime = _Runtime()
|
210
|
+
|
211
|
+
# ================================ PUBLIC API ================================
|
212
|
+
|
213
|
+
gpus = _runtime.gpus
|
214
|
+
|
215
|
+
|
216
|
+
def get_context(devnum=None):
|
217
|
+
"""Get the current device or use a device by device number, and
|
218
|
+
return the CUDA context.
|
219
|
+
"""
|
220
|
+
return _runtime.get_or_create_context(devnum)
|
221
|
+
|
222
|
+
|
223
|
+
def require_context(fn):
|
224
|
+
"""
|
225
|
+
A decorator that ensures a CUDA context is available when *fn* is executed.
|
226
|
+
|
227
|
+
Note: The function *fn* cannot switch CUDA-context.
|
228
|
+
"""
|
229
|
+
@functools.wraps(fn)
|
230
|
+
def _require_cuda_context(*args, **kws):
|
231
|
+
with _runtime.ensure_context():
|
232
|
+
return fn(*args, **kws)
|
233
|
+
|
234
|
+
return _require_cuda_context
|
235
|
+
|
236
|
+
|
237
|
+
def reset():
|
238
|
+
"""Reset the CUDA subsystem for the current thread.
|
239
|
+
|
240
|
+
In the main thread:
|
241
|
+
This removes all CUDA contexts. Only use this at shutdown or for
|
242
|
+
cleaning up between tests.
|
243
|
+
|
244
|
+
In non-main threads:
|
245
|
+
This clear the CUDA context stack only.
|
246
|
+
|
247
|
+
"""
|
248
|
+
_runtime.reset()
|