Skip to content

Commit ad13912

Browse files
Add USMAllocationError exception, use it for USM allocation failures
This PR addresses concern about use of non-descriptive RuntimeError exception in earlier versions raised in issue gh-1158 ``` In [1]: import dpctl In [2]: import dpctl.memory as dpm In [3]: dpm.MemoryUSMDevice( 1024 * 1024 * 1024 * 8) --------------------------------------------------------------------------- USMAllocationError Traceback (most recent call last) Input In [3], in <cell line: 1>() ----> 1 dpm.MemoryUSMDevice( 1024 * 1024 * 1024 * 8) File ~/repos/dpctl/dpctl/memory/_memory.pyx:783, in dpctl.memory._memory.MemoryUSMDevice.__cinit__() 781 SyclQueue queue=None, int copy=False): 782 if (isinstance(other, numbers.Integral)): --> 783 self._cinit_alloc(alignment, <Py_ssize_t>other, b"device", queue) 784 else: 785 self._cinit_other(other) File ~/repos/dpctl/dpctl/memory/_memory.pyx:201, in dpctl.memory._memory._Memory._cinit_alloc() 199 self.queue = queue 200 else: --> 201 raise USMAllocationError( 202 "USM allocation failed" 203 ) USMAllocationError: USM allocation failed In [4]: quit ```
1 parent 7bbfce1 commit ad13912

File tree

2 files changed

+21
-4
lines changed

2 files changed

+21
-4
lines changed

dpctl/memory/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,14 @@
3232
MemoryUSMDevice,
3333
MemoryUSMHost,
3434
MemoryUSMShared,
35+
USMAllocationError,
3536
as_usm_memory,
3637
)
3738

3839
__all__ = [
3940
"MemoryUSMDevice",
4041
"MemoryUSMHost",
4142
"MemoryUSMShared",
43+
"USMAllocationError",
4244
"as_usm_memory",
4345
]

dpctl/memory/_memory.pyx

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,11 +71,22 @@ import numpy as np
7171
__all__ = [
7272
"MemoryUSMShared",
7373
"MemoryUSMHost",
74-
"MemoryUSMDevice"
74+
"MemoryUSMDevice",
75+
"USMAllocationError",
7576
]
7677

7778
include "_sycl_usm_array_interface_utils.pxi"
7879

80+
class USMAllocationError(Exception):
81+
"""
82+
An exception raised when Universal Shared Memory (USM) allocation
83+
call returns a null pointer, signaling a failure to perform the allocation.
84+
Some common reasons for allocation failure are:
85+
* insufficient free memory to perform the allocation request
86+
* allocation size exceeds the maximum supported by targeted backend
87+
"""
88+
pass
89+
7990

8091
cdef void copy_via_host(void *dest_ptr, SyclQueue dest_queue,
8192
void *src_ptr, SyclQueue src_queue, size_t nbytes):
@@ -177,7 +188,7 @@ cdef class _Memory:
177188
with nogil: p = DPCTLmalloc_device(nbytes, QRef)
178189
else:
179190
raise RuntimeError(
180-
"Pointer type is unknown: {}".format(
191+
"Pointer type '{}' is not recognized".format(
181192
ptr_type.decode("UTF-8")
182193
)
183194
)
@@ -187,9 +198,13 @@ cdef class _Memory:
187198
self.nbytes = nbytes
188199
self.queue = queue
189200
else:
190-
raise RuntimeError("Null memory pointer returned")
201+
raise USMAllocationError(
202+
"USM allocation failed"
203+
)
191204
else:
192-
raise ValueError("Non-positive number of bytes found.")
205+
raise ValueError(
206+
"Number of bytes of request allocation must be positive."
207+
)
193208

194209
cdef _cinit_other(self, object other):
195210
cdef _Memory other_mem

0 commit comments

Comments
 (0)