Skip to content

Commit 4dfde5f

Browse files
authored
Throw when device does not support USM allocation (#12446)
The USM allocation functions are changed to conform with the spec and throw when certain usm allocation modes are not supported. malloc_shared is left incomplete because it breaks a lot of tests and its more reasonable to handle that in a later PR. In the meantime, malloc_device and malloc_host have been changed to conform with the spec and throw an exception when the corresponding USM aspect is not supported. The test usm_pooling.cpp has also been updated to reflect this query about the memory access properties in the L0 API so that it knows to expect it in the output.
1 parent 2e4916a commit 4dfde5f

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

sycl/source/detail/usm/usm_impl.cpp

100644100755
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,14 @@ void *alignedAllocHost(size_t Alignment, size_t Size, const context &Ctxt,
5959
PrepareNotify.scopedNotify(
6060
(uint16_t)xpti::trace_point_type_t::mem_alloc_begin);
6161
#endif
62+
const auto &devices = Ctxt.get_devices();
63+
if (!std::any_of(devices.begin(), devices.end(), [&](const auto &device) {
64+
return device.has(sycl::aspect::usm_host_allocations);
65+
})) {
66+
throw sycl::exception(
67+
sycl::errc::feature_not_supported,
68+
"No device in this context supports USM host allocations!");
69+
}
6270
void *RetVal = nullptr;
6371
if (Size == 0)
6472
return nullptr;
@@ -131,6 +139,19 @@ void *alignedAllocInternal(size_t Alignment, size_t Size,
131139
const context_impl *CtxImpl,
132140
const device_impl *DevImpl, alloc Kind,
133141
const property_list &PropList) {
142+
if (Kind == alloc::device &&
143+
!DevImpl->has(sycl::aspect::usm_device_allocations)) {
144+
throw sycl::exception(sycl::errc::feature_not_supported,
145+
"Device does not support USM device allocations!");
146+
}
147+
if (Kind == alloc::shared &&
148+
!DevImpl->has(sycl::aspect::usm_shared_allocations)) {
149+
// TODO:: Throw an exception to conform with the specification.
150+
// Note that many tests will have to be changed to conform with the spec
151+
// before completing this. That is, the tests will now have to expect
152+
// exceptions as a result of failed allocations in addition to nullptr
153+
// being returned depending on the reason why allocation failed.
154+
}
134155
void *RetVal = nullptr;
135156
if (Size == 0)
136157
return nullptr;

sycl/test-e2e/USM/usm_pooling.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ int main(int argc, char *argv[]) {
102102
}
103103

104104
// CHECK-NOPOOL: Test [[API:zeMemAllocHost|zeMemAllocDevice|zeMemAllocShared]]
105+
// CHECK-NOPOOL-NEXT: ZE ---> zeDeviceGetMemoryAccessProperties
105106
// CHECK-NOPOOL-NEXT: ZE ---> [[API]](
106107
// CHECK-NOPOOL-NEXT: ZE ---> [[API]](
107108
// CHECK-NOPOOL-NEXT: ZE ---> zeMemFree
@@ -111,6 +112,7 @@ int main(int argc, char *argv[]) {
111112
// CHECK-NOPOOL-NEXT: ZE ---> [[API]](
112113

113114
// CHECK-12345: Test [[API:zeMemAllocHost|zeMemAllocDevice|zeMemAllocShared]]
115+
// CHECK-12345-NEXT: ZE ---> zeDeviceGetMemoryAccessProperties
114116
// CHECK-12345-NEXT: ZE ---> [[API]](
115117
// CHECK-12345-NEXT: ZE ---> [[API]](
116118
// CHECK-12345-NEXT: ZE ---> zeMemFree
@@ -120,13 +122,15 @@ int main(int argc, char *argv[]) {
120122
// CHECK-12345-NEXT: ZE ---> [[API]](
121123

122124
// CHECK-1245: Test [[API:zeMemAllocHost|zeMemAllocDevice|zeMemAllocShared]]
125+
// CHECK-1245-NEXT: ZE ---> zeDeviceGetMemoryAccessProperties
123126
// CHECK-1245-NEXT: ZE ---> [[API]](
124127
// CHECK-1245-NEXT: ZE ---> [[API]](
125128
// CHECK-1245-NEXT: ZE ---> zeMemFree
126129
// CHECK-1245-NEXT: ZE ---> [[API]](
127130
// CHECK-1245-NEXT: ZE ---> [[API]](
128131

129132
// CHECK-15: Test [[API:zeMemAllocHost|zeMemAllocDevice|zeMemAllocShared]]
133+
// CHECK-15-NEXT: ZE ---> zeDeviceGetMemoryAccessProperties
130134
// CHECK-15-NEXT: ZE ---> [[API]](
131135
// CHECK-15-NEXT: ZE ---> [[API]](
132136
// CHECK-15-NEXT: ZE ---> zeMemFree

0 commit comments

Comments
 (0)