@@ -44,24 +44,48 @@ DEFINE_SIMPLE_CONVERSION_FUNCTIONS(void, DPCTLSyclUSMRef)
44
44
__dpctl_give DPCTLSyclUSMRef
45
45
DPCTLmalloc_shared (size_t size, __dpctl_keep const DPCTLSyclQueueRef QRef)
46
46
{
47
- auto Q = unwrap (QRef);
48
- auto Ptr = malloc_shared (size, *Q);
49
- return wrap (Ptr);
47
+ if (!QRef) {
48
+ std::cerr << " Input QRef is nullptr\n " ;
49
+ return nullptr ;
50
+ }
51
+ try {
52
+ auto Q = unwrap (QRef);
53
+ auto Ptr = malloc_shared (size, *Q);
54
+ return wrap (Ptr);
55
+ } catch (feature_not_supported const &fns) {
56
+ std::cerr << fns.what () << ' \n ' ;
57
+ return nullptr ;
58
+ }
50
59
}
51
60
52
61
__dpctl_give DPCTLSyclUSMRef
53
62
DPCTLaligned_alloc_shared (size_t alignment,
54
63
size_t size,
55
64
__dpctl_keep const DPCTLSyclQueueRef QRef)
56
65
{
57
- auto Q = unwrap (QRef);
58
- auto Ptr = aligned_alloc_shared (alignment, size, *Q);
59
- return wrap (Ptr);
66
+ if (!QRef) {
67
+ std::cerr << " Input QRef is nullptr\n " ;
68
+ return nullptr ;
69
+ }
70
+ try {
71
+ auto Q = unwrap (QRef);
72
+ auto Ptr = aligned_alloc_shared (alignment, size, *Q);
73
+ return wrap (Ptr);
74
+ } catch (feature_not_supported const &fns) {
75
+ std::cerr << fns.what () << ' \n ' ;
76
+ return nullptr ;
77
+ }
60
78
}
61
79
62
80
__dpctl_give DPCTLSyclUSMRef
63
81
DPCTLmalloc_host (size_t size, __dpctl_keep const DPCTLSyclQueueRef QRef)
64
82
{
83
+ if (!QRef) {
84
+ std::cerr << " Input QRef is nullptr\n " ;
85
+ return nullptr ;
86
+ }
87
+ // SYCL 2020 spec: for devices without aspect::usm_host_allocations:
88
+ // undefined behavior
65
89
auto Q = unwrap (QRef);
66
90
auto Ptr = malloc_host (size, *Q);
67
91
return wrap (Ptr);
@@ -72,6 +96,12 @@ DPCTLaligned_alloc_host(size_t alignment,
72
96
size_t size,
73
97
__dpctl_keep const DPCTLSyclQueueRef QRef)
74
98
{
99
+ if (!QRef) {
100
+ std::cerr << " Input QRef is nullptr\n " ;
101
+ return nullptr ;
102
+ }
103
+ // SYCL 2020 spec: for devices without aspect::usm_host_allocations:
104
+ // undefined behavior
75
105
auto Q = unwrap (QRef);
76
106
auto Ptr = aligned_alloc_host (alignment, size, *Q);
77
107
return wrap (Ptr);
@@ -80,24 +110,50 @@ DPCTLaligned_alloc_host(size_t alignment,
80
110
__dpctl_give DPCTLSyclUSMRef
81
111
DPCTLmalloc_device (size_t size, __dpctl_keep const DPCTLSyclQueueRef QRef)
82
112
{
83
- auto Q = unwrap (QRef);
84
- auto Ptr = malloc_device (size, *Q);
85
- return wrap (Ptr);
113
+ if (!QRef) {
114
+ std::cerr << " Input QRef is nullptr\n " ;
115
+ return nullptr ;
116
+ }
117
+ try {
118
+ auto Q = unwrap (QRef);
119
+ auto Ptr = malloc_device (size, *Q);
120
+ return wrap (Ptr);
121
+ } catch (feature_not_supported const &fns) {
122
+ std::cerr << fns.what () << ' \n ' ;
123
+ return nullptr ;
124
+ }
86
125
}
87
126
88
127
__dpctl_give DPCTLSyclUSMRef
89
128
DPCTLaligned_alloc_device (size_t alignment,
90
129
size_t size,
91
130
__dpctl_keep const DPCTLSyclQueueRef QRef)
92
131
{
93
- auto Q = unwrap (QRef);
94
- auto Ptr = aligned_alloc_device (alignment, size, *Q);
95
- return wrap (Ptr);
132
+ if (!QRef) {
133
+ std::cerr << " Input QRef is nullptr\n " ;
134
+ return nullptr ;
135
+ }
136
+ try {
137
+ auto Q = unwrap (QRef);
138
+ auto Ptr = aligned_alloc_device (alignment, size, *Q);
139
+ return wrap (Ptr);
140
+ } catch (feature_not_supported const &fns) {
141
+ std::cerr << fns.what () << ' \n ' ;
142
+ return nullptr ;
143
+ }
96
144
}
97
145
98
146
void DPCTLfree_with_queue (__dpctl_take DPCTLSyclUSMRef MRef,
99
147
__dpctl_keep const DPCTLSyclQueueRef QRef)
100
148
{
149
+ if (!QRef) {
150
+ std::cerr << " Input QRef is nullptr\n " ;
151
+ return ;
152
+ }
153
+ if (!MRef) {
154
+ std::cerr << " Input MRef is nullptr, nothing to free\n " ;
155
+ return ;
156
+ }
101
157
auto Ptr = unwrap (MRef);
102
158
auto Q = unwrap (QRef);
103
159
free (Ptr, *Q);
@@ -106,6 +162,14 @@ void DPCTLfree_with_queue(__dpctl_take DPCTLSyclUSMRef MRef,
106
162
void DPCTLfree_with_context (__dpctl_take DPCTLSyclUSMRef MRef,
107
163
__dpctl_keep const DPCTLSyclContextRef CRef)
108
164
{
165
+ if (!CRef) {
166
+ std::cerr << " Input CRef is nullptr\n " ;
167
+ return ;
168
+ }
169
+ if (!MRef) {
170
+ std::cerr << " Input MRef is nullptr, nothing to free\n " ;
171
+ return ;
172
+ }
109
173
auto Ptr = unwrap (MRef);
110
174
auto C = unwrap (CRef);
111
175
free (Ptr, *C);
@@ -114,6 +178,14 @@ void DPCTLfree_with_context(__dpctl_take DPCTLSyclUSMRef MRef,
114
178
const char *DPCTLUSM_GetPointerType (__dpctl_keep const DPCTLSyclUSMRef MRef,
115
179
__dpctl_keep const DPCTLSyclContextRef CRef)
116
180
{
181
+ if (!CRef) {
182
+ std::cerr << " Input CRef is nullptr\n " ;
183
+ return " unknown" ;
184
+ }
185
+ if (!MRef) {
186
+ std::cerr << " Input MRef is nullptr\n " ;
187
+ return " unknown" ;
188
+ }
117
189
auto Ptr = unwrap (MRef);
118
190
auto C = unwrap (CRef);
119
191
@@ -134,6 +206,15 @@ DPCTLSyclDeviceRef
134
206
DPCTLUSM_GetPointerDevice (__dpctl_keep const DPCTLSyclUSMRef MRef,
135
207
__dpctl_keep const DPCTLSyclContextRef CRef)
136
208
{
209
+ if (!CRef) {
210
+ std::cerr << " Input CRef is nullptr\n " ;
211
+ return nullptr ;
212
+ }
213
+ if (!MRef) {
214
+ std::cerr << " Input MRef is nullptr\n " ;
215
+ return nullptr ;
216
+ }
217
+
137
218
auto Ptr = unwrap (MRef);
138
219
auto C = unwrap (CRef);
139
220
0 commit comments