| 1 |
# SPDX-License-Identifier: (GPL-2.0 OR Linux-OpenIB) |
| 2 |
# Copyright (c) 2022 Nvidia Inc. All rights reserved. See COPYING file |
| 3 |
|
| 4 |
""" |
| 5 |
This module provides utilities and auxiliary functions for CUDA related tests |
| 6 |
including the initialization/tear down needed and some error handlers. |
| 7 |
""" |
| 8 |
|
| 9 |
import unittest |
| 10 |
|
| 11 |
try: |
| 12 |
from cuda import cuda, cudart, nvrtc |
| 13 |
CUDA_FOUND = True |
| 14 |
except ImportError: |
| 15 |
CUDA_FOUND = False |
| 16 |
|
| 17 |
|
| 18 |
def requires_cuda(func): |
| 19 |
def inner(instance): |
| 20 |
if not CUDA_FOUND: |
| 21 |
raise unittest.SkipTest( |
| 22 |
'cuda-python 12.0+ must be installed to run CUDA tests') |
| 23 |
res = cudart.cudaGetDeviceCount() |
| 24 |
if res[0].value == cuda.CUresult.CUDA_ERROR_NO_DEVICE or res[1] == 0: |
| 25 |
raise unittest.SkipTest('No CUDA-capable devices were detected') |
| 26 |
return func(instance) |
| 27 |
return inner |
| 28 |
|
| 29 |
|
| 30 |
def _cuda_get_error_enum(error): |
| 31 |
if isinstance(error, cuda.CUresult): |
| 32 |
err, name = cuda.cuGetErrorName(error) |
| 33 |
return name if err == cuda.CUresult.CUDA_SUCCESS else "<unknown>" |
| 34 |
elif isinstance(error, cudart.cudaError_t): |
| 35 |
return cudart.cudaGetErrorName(error)[1] |
| 36 |
elif isinstance(error, nvrtc.nvrtcResult): |
| 37 |
return nvrtc.nvrtcGetErrorString(error)[1] |
| 38 |
else: |
| 39 |
raise RuntimeError(f'Unknown error type: {error}') |
| 40 |
|
| 41 |
|
| 42 |
def check_cuda_errors(result): |
| 43 |
""" |
| 44 |
CUDA error handler. |
| 45 |
If the CUDA result is success it returns the remaining objects that |
| 46 |
originally returned by the CUDA function call (if any). |
| 47 |
Otherwise and exception is raised. |
| 48 |
:param result: CUDA function result (CUresult) |
| 49 |
:return: The CUDA function results in case of success |
| 50 |
""" |
| 51 |
if result[0].value: |
| 52 |
raise RuntimeError( |
| 53 |
f'CUDA error code = {result[0].value} ({_cuda_get_error_enum(result[0])})') |
| 54 |
if len(result) == 1: |
| 55 |
return None |
| 56 |
elif len(result) == 2: |
| 57 |
return result[1] |
| 58 |
else: |
| 59 |
return result[1:] |
| 60 |
|
| 61 |
|
| 62 |
# The following functions should not be used directly, instead they should |
| 63 |
# replace/extend unittest.TestCase's derived methods in CUDA tests. |
| 64 |
|
| 65 |
@requires_cuda |
| 66 |
def setUp(obj): |
| 67 |
super(obj.__class__, obj).setUp() |
| 68 |
obj.iters = 10 |
| 69 |
obj.traffic_args = None |
| 70 |
obj.cuda_ctx = None |
| 71 |
obj.init_cuda() |
| 72 |
|
| 73 |
|
| 74 |
def tearDown(obj): |
| 75 |
if obj.server and obj.server.cuda_addr: |
| 76 |
check_cuda_errors(cuda.cuMemFree(obj.server.cuda_addr)) |
| 77 |
if obj.client and obj.client.cuda_addr: |
| 78 |
check_cuda_errors(cuda.cuMemFree(obj.client.cuda_addr)) |
| 79 |
if obj.cuda_ctx: |
| 80 |
check_cuda_errors(cuda.cuCtxDestroy(obj.cuda_ctx)) |
| 81 |
super(obj.__class__, obj).tearDown() |
| 82 |
|
| 83 |
|
| 84 |
def init_cuda(obj): |
| 85 |
cuda_dev_id = obj.config['gpu'] |
| 86 |
if cuda_dev_id is None: |
| 87 |
raise unittest.SkipTest('GPU device ID must be passed') |
| 88 |
check_cuda_errors(cuda.cuInit(0)) |
| 89 |
cuda_device = check_cuda_errors(cuda.cuDeviceGet(cuda_dev_id)) |
| 90 |
obj.cuda_ctx = check_cuda_errors( |
| 91 |
cuda.cuCtxCreate(cuda.CUctx_flags.CU_CTX_MAP_HOST, cuda_device)) |
| 92 |
check_cuda_errors(cuda.cuCtxSetCurrent(obj.cuda_ctx)) |
| 93 |
|
| 94 |
|
| 95 |
def set_init_cuda_methods(cls): |
| 96 |
""" |
| 97 |
Replaces the setUp and tearDown methods of any unittest.TestCase derived |
| 98 |
class. Can be useful as a decorator for CUDA related tests. |
| 99 |
:param cls: Test class of unittest.TestCase |
| 100 |
""" |
| 101 |
cls.setUp = setUp |
| 102 |
cls.tearDown = tearDown |
| 103 |
cls.init_cuda = init_cuda |
| 104 |
cls.mem_write = mem_write |
| 105 |
cls.mem_read = mem_read |
| 106 |
return cls |
| 107 |
|
| 108 |
|
| 109 |
# The following functions should be used by CUDA resources objects |
| 110 |
|
| 111 |
def mem_write(obj, data, size, offset=0): |
| 112 |
cuda_addr = cuda.CUdeviceptr(init_value=int(obj.cuda_addr) + offset) |
| 113 |
check_cuda_errors(cuda.cuMemcpyHtoD(cuda_addr, data.encode(), size)) |
| 114 |
|
| 115 |
|
| 116 |
def mem_read(obj, size=None, offset=0): |
| 117 |
size_ = obj.msg_size if size is None else size |
| 118 |
data_read = bytearray(size_) |
| 119 |
cuda_addr = cuda.CUdeviceptr(init_value=int(obj.cuda_addr) + offset) |
| 120 |
check_cuda_errors(cuda.cuMemcpyDtoH(data_read, cuda_addr, size_)) |
| 121 |
return data_read |
| 122 |
|
| 123 |
|
| 124 |
def set_mem_io_cuda_methods(cls): |
| 125 |
""" |
| 126 |
Replaces the mem_write/mem_read methods of any class derived from |
| 127 |
BaseResources. |
| 128 |
:param cls: Test class of BaseResources |
| 129 |
""" |
| 130 |
cls.mem_write = mem_write |
| 131 |
cls.mem_read = mem_read |
| 132 |
return cls |