Skip to content

Commit 2320c92

Browse files
Merge pull request #1161 from IntelPython/better-usm-allocation-exception
Add USMAllocationError exception, use it for USM allocation failures
2 parents a504e96 + ad13912 commit 2320c92

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)