DPC++ Runtime
Runtime libraries for oneAPI DPC++
usm_impl.cpp
Go to the documentation of this file.
1 //==---------------- usm_impl.cpp - USM API Utils -------------*- C++ -*---==//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 // ===--------------------------------------------------------------------=== //
8 
9 #include <CL/sycl/context.hpp>
12 #include <CL/sycl/detail/pi.hpp>
13 #include <CL/sycl/device.hpp>
14 #include <CL/sycl/usm.hpp>
15 #include <detail/queue_impl.hpp>
16 
17 #include <cstdlib>
18 #include <memory>
19 
20 #ifdef XPTI_ENABLE_INSTRUMENTATION
21 // Include the headers necessary for emitting
22 // traces using the trace framework
23 #include "xpti/xpti_trace_framework.hpp"
24 
25 #define XPTI_CREATE_TRACEPOINT(CL) \
26  std::unique_ptr<xpti::framework::tracepoint_t> _TP(nullptr); \
27  if (xptiTraceEnabled()) { \
28  xpti::payload_t Payload{CL.functionName(), CL.fileName(), \
29  static_cast<int>(CL.lineNumber()), \
30  static_cast<int>(CL.columnNumber()), nullptr}; \
31  _TP = std::make_unique<xpti::framework::tracepoint_t>(&Payload); \
32  } \
33  (void)_TP;
34 #else
35 #define XPTI_CREATE_TRACEPOINT(CL)
36 #endif
37 
39 namespace sycl {
40 
42 
43 namespace detail {
44 namespace usm {
45 
46 void *alignedAllocHost(size_t Alignment, size_t Size, const context &Ctxt,
47  alloc Kind, const detail::code_location &CL) {
49  void *RetVal = nullptr;
50  if (Size == 0)
51  return nullptr;
52  if (Ctxt.is_host()) {
53  if (!Alignment) {
54  // worst case default
55  Alignment = 128;
56  }
57 
58  aligned_allocator<char> Alloc(Alignment);
59  try {
60  RetVal = Alloc.allocate(Size);
61  } catch (const std::bad_alloc &) {
62  // Conform with Specification behavior
63  RetVal = nullptr;
64  }
65  } else {
66  std::shared_ptr<context_impl> CtxImpl = detail::getSyclObjImpl(Ctxt);
67  pi_context C = CtxImpl->getHandleRef();
68  const detail::plugin &Plugin = CtxImpl->getPlugin();
69  pi_result Error;
70 
71  switch (Kind) {
72  case alloc::host: {
74  &RetVal, C, nullptr, Size, Alignment);
75  break;
76  }
77  case alloc::device:
78  case alloc::shared:
79  case alloc::unknown: {
80  RetVal = nullptr;
81  Error = PI_INVALID_VALUE;
82  break;
83  }
84  }
85 
86  // Error is for debugging purposes.
87  // The spec wants a nullptr returned, not an exception.
88  if (Error != PI_SUCCESS)
89  return nullptr;
90  }
91  return RetVal;
92 }
93 
94 void *alignedAlloc(size_t Alignment, size_t Size, const context &Ctxt,
95  const device &Dev, alloc Kind,
96  const detail::code_location &CL,
97  const property_list &PropList = {}) {
99  void *RetVal = nullptr;
100  if (Size == 0)
101  return nullptr;
102 
103  if (Ctxt.is_host()) {
104  if (Kind == alloc::unknown) {
105  RetVal = nullptr;
106  } else {
107  if (!Alignment) {
108  // worst case default
109  Alignment = 128;
110  }
111 
112  aligned_allocator<char> Alloc(Alignment);
113  try {
114  RetVal = Alloc.allocate(Size);
115  } catch (const std::bad_alloc &) {
116  // Conform with Specification behavior
117  RetVal = nullptr;
118  }
119  }
120  } else {
121  std::shared_ptr<context_impl> CtxImpl = detail::getSyclObjImpl(Ctxt);
122  pi_context C = CtxImpl->getHandleRef();
123  const detail::plugin &Plugin = CtxImpl->getPlugin();
124  pi_result Error;
125  pi_device Id;
126 
127  switch (Kind) {
128  case alloc::device: {
129  Id = detail::getSyclObjImpl(Dev)->getHandleRef();
130  // Parse out buffer location property
131  // Buffer location is only supported on FPGA devices
132  bool IsBufferLocSupported =
133  Dev.has_extension("cl_intel_mem_alloc_buffer_location");
134  if (IsBufferLocSupported &&
135  PropList.has_property<cl::sycl::ext::intel::experimental::property::
136  usm::buffer_location>()) {
137  auto location = PropList
138  .get_property<cl::sycl::ext::intel::experimental::
139  property::usm::buffer_location>()
140  .get_buffer_location();
142  location, 0};
143  Error = Plugin.call_nocheck<PiApiKind::piextUSMDeviceAlloc>(
144  &RetVal, C, Id, props, Size, Alignment);
145  } else {
146  Error = Plugin.call_nocheck<PiApiKind::piextUSMDeviceAlloc>(
147  &RetVal, C, Id, nullptr, Size, Alignment);
148  }
149  break;
150  }
151  case alloc::shared: {
152  Id = detail::getSyclObjImpl(Dev)->getHandleRef();
153  if (PropList.has_property<
157  Error = Plugin.call_nocheck<PiApiKind::piextUSMSharedAlloc>(
158  &RetVal, C, Id, Props, Size, Alignment);
159  } else {
160  Error = Plugin.call_nocheck<PiApiKind::piextUSMSharedAlloc>(
161  &RetVal, C, Id, nullptr, Size, Alignment);
162  }
163  break;
164  }
165  case alloc::host:
166  case alloc::unknown: {
167  RetVal = nullptr;
168  Error = PI_INVALID_VALUE;
169  break;
170  }
171  }
172 
173  // Error is for debugging purposes.
174  // The spec wants a nullptr returned, not an exception.
175  if (Error != PI_SUCCESS)
176  return nullptr;
177  }
178  return RetVal;
179 }
180 
181 void free(void *Ptr, const context &Ctxt, const detail::code_location &CL) {
183  if (Ptr == nullptr)
184  return;
185  if (Ctxt.is_host()) {
186  // need to use alignedFree here for Windows
187  detail::OSUtil::alignedFree(Ptr);
188  } else {
189  std::shared_ptr<context_impl> CtxImpl = detail::getSyclObjImpl(Ctxt);
190  pi_context C = CtxImpl->getHandleRef();
191  const detail::plugin &Plugin = CtxImpl->getPlugin();
192  Plugin.call<PiApiKind::piextUSMFree>(C, Ptr);
193  }
194 }
195 
196 // For ABI compatibility
197 // TODO remove once ABI breakages are allowed.
198 __SYCL_EXPORT void *alignedAllocHost(size_t Alignment, size_t Size,
199  const context &Ctxt, alloc Kind) {
200  return alignedAllocHost(Alignment, Size, Ctxt, Kind, detail::code_location{});
201 }
202 
203 __SYCL_EXPORT void free(void *Ptr, const context &Ctxt) {
205 }
206 
207 __SYCL_EXPORT void *alignedAlloc(size_t Alignment, size_t Size,
208  const context &Ctxt, const device &Dev,
209  alloc Kind) {
210  return alignedAlloc(Alignment, Size, Ctxt, Dev, Kind,
212 }
213 
214 } // namespace usm
215 } // namespace detail
216 
217 void *malloc_device(size_t Size, const device &Dev, const context &Ctxt,
218  const detail::code_location CL) {
219  return detail::usm::alignedAlloc(0, Size, Ctxt, Dev, alloc::device, CL);
220 }
221 
222 void *malloc_device(size_t Size, const device &Dev, const context &Ctxt,
223  const property_list &PropList,
224  const detail::code_location CL) {
225  return detail::usm::alignedAlloc(0, Size, Ctxt, Dev, alloc::device, CL,
226  PropList);
227 }
228 
229 void *malloc_device(size_t Size, const queue &Q,
230  const detail::code_location CL) {
231  return malloc_device(Size, Q.get_device(), Q.get_context(), CL);
232 }
233 
234 void *malloc_device(size_t Size, const queue &Q, const property_list &PropList,
235  const detail::code_location CL) {
236  return malloc_device(Size, Q.get_device(), Q.get_context(), PropList, CL);
237 }
238 
239 void *aligned_alloc_device(size_t Alignment, size_t Size, const device &Dev,
240  const context &Ctxt,
241  const detail::code_location CL) {
242  return detail::usm::alignedAlloc(Alignment, Size, Ctxt, Dev, alloc::device,
243  CL);
244 }
245 
246 void *aligned_alloc_device(size_t Alignment, size_t Size, const device &Dev,
247  const context &Ctxt, const property_list &,
248  const detail::code_location CL) {
249  return aligned_alloc_device(Alignment, Size, Dev, Ctxt, CL);
250 }
251 
252 void *aligned_alloc_device(size_t Alignment, size_t Size, const queue &Q,
253  const detail::code_location CL) {
254  return aligned_alloc_device(Alignment, Size, Q.get_device(), Q.get_context(),
255  CL);
256 }
257 
258 void *aligned_alloc_device(size_t Alignment, size_t Size, const queue &Q,
259  const property_list &PropList,
260  const detail::code_location CL) {
261  return aligned_alloc_device(Alignment, Size, Q.get_device(), Q.get_context(),
262  PropList, CL);
263 }
264 
265 void free(void *ptr, const context &Ctxt, const detail::code_location CL) {
266  return detail::usm::free(ptr, Ctxt, CL);
267 }
268 
269 void free(void *ptr, const queue &Q, const detail::code_location CL) {
270  return free(ptr, Q.get_context(), CL);
271 }
272 
274 // Restricted USM
276 void *malloc_host(size_t Size, const context &Ctxt,
277  const detail::code_location CL) {
278  return detail::usm::alignedAllocHost(0, Size, Ctxt, alloc::host, CL);
279 }
280 
281 void *malloc_host(size_t Size, const context &Ctxt, const property_list &,
282  const detail::code_location CL) {
283  return malloc_host(Size, Ctxt, CL);
284 }
285 
286 void *malloc_host(size_t Size, const queue &Q, const detail::code_location CL) {
287  return malloc_host(Size, Q.get_context(), CL);
288 }
289 
290 void *malloc_host(size_t Size, const queue &Q, const property_list &PropList,
291  const detail::code_location CL) {
292  return malloc_host(Size, Q.get_context(), PropList, CL);
293 }
294 
295 void *malloc_shared(size_t Size, const device &Dev, const context &Ctxt,
296  const detail::code_location CL) {
297  return detail::usm::alignedAlloc(0, Size, Ctxt, Dev, alloc::shared, CL);
298 }
299 
300 void *malloc_shared(size_t Size, const device &Dev, const context &Ctxt,
301  const property_list &PropList,
302  const detail::code_location CL) {
303  return detail::usm::alignedAlloc(0, Size, Ctxt, Dev, alloc::shared, CL,
304  PropList);
305 }
306 
307 void *malloc_shared(size_t Size, const queue &Q,
308  const detail::code_location CL) {
309  return malloc_shared(Size, Q.get_device(), Q.get_context(), CL);
310 }
311 
312 void *malloc_shared(size_t Size, const queue &Q, const property_list &PropList,
313  const detail::code_location CL) {
314  return malloc_shared(Size, Q.get_device(), Q.get_context(), PropList, CL);
315 }
316 
317 void *aligned_alloc_host(size_t Alignment, size_t Size, const context &Ctxt,
318  const detail::code_location CL) {
319  return detail::usm::alignedAllocHost(Alignment, Size, Ctxt, alloc::host, CL);
320 }
321 
322 void *aligned_alloc_host(size_t Alignment, size_t Size, const context &Ctxt,
323  const property_list &,
324  const detail::code_location CL) {
325  return aligned_alloc_host(Alignment, Size, Ctxt, CL);
326 }
327 
328 void *aligned_alloc_host(size_t Alignment, size_t Size, const queue &Q,
329  const detail::code_location CL) {
330  return aligned_alloc_host(Alignment, Size, Q.get_context(), CL);
331 }
332 
333 void *aligned_alloc_host(size_t Alignment, size_t Size, const queue &Q,
334  const property_list &PropList,
335  const detail::code_location CL) {
336  return aligned_alloc_host(Alignment, Size, Q.get_context(), PropList, CL);
337 }
338 
339 void *aligned_alloc_shared(size_t Alignment, size_t Size, const device &Dev,
340  const context &Ctxt,
341  const detail::code_location CL) {
342  return detail::usm::alignedAlloc(Alignment, Size, Ctxt, Dev, alloc::shared,
343  CL);
344 }
345 
346 void *aligned_alloc_shared(size_t Alignment, size_t Size, const device &Dev,
347  const context &Ctxt, const property_list &PropList,
348  const detail::code_location CL) {
349  return detail::usm::alignedAlloc(Alignment, Size, Ctxt, Dev, alloc::shared,
350  CL, PropList);
351 }
352 
353 void *aligned_alloc_shared(size_t Alignment, size_t Size, const queue &Q,
354  const detail::code_location CL) {
355  return aligned_alloc_shared(Alignment, Size, Q.get_device(), Q.get_context(),
356  CL);
357 }
358 
359 void *aligned_alloc_shared(size_t Alignment, size_t Size, const queue &Q,
360  const property_list &PropList,
361  const detail::code_location CL) {
362  return aligned_alloc_shared(Alignment, Size, Q.get_device(), Q.get_context(),
363  PropList, CL);
364 }
365 
366 // single form
367 
368 void *malloc(size_t Size, const device &Dev, const context &Ctxt, alloc Kind,
369  const detail::code_location CL) {
370  void *RetVal = nullptr;
371 
372  if (Kind == alloc::host) {
373  RetVal = detail::usm::alignedAllocHost(0, Size, Ctxt, Kind, CL);
374  } else {
375  RetVal = detail::usm::alignedAlloc(0, Size, Ctxt, Dev, Kind, CL);
376  }
377 
378  return RetVal;
379 }
380 
381 void *malloc(size_t Size, const device &Dev, const context &Ctxt, alloc Kind,
382  const property_list &, const detail::code_location CL) {
383  return malloc(Size, Dev, Ctxt, Kind, CL);
384 }
385 
386 void *malloc(size_t Size, const queue &Q, alloc Kind,
387  const detail::code_location CL) {
388  return malloc(Size, Q.get_device(), Q.get_context(), Kind, CL);
389 }
390 
391 void *malloc(size_t Size, const queue &Q, alloc Kind,
392  const property_list &PropList, const detail::code_location CL) {
393  return malloc(Size, Q.get_device(), Q.get_context(), Kind, PropList, CL);
394 }
395 
396 void *aligned_alloc(size_t Alignment, size_t Size, const device &Dev,
397  const context &Ctxt, alloc Kind,
398  const detail::code_location CL) {
399  void *RetVal = nullptr;
400 
401  if (Kind == alloc::host) {
402  RetVal = detail::usm::alignedAllocHost(Alignment, Size, Ctxt, Kind, CL);
403  } else {
404  RetVal = detail::usm::alignedAlloc(Alignment, Size, Ctxt, Dev, Kind, CL);
405  }
406 
407  return RetVal;
408 }
409 
410 void *aligned_alloc(size_t Alignment, size_t Size, const device &Dev,
411  const context &Ctxt, alloc Kind, const property_list &,
412  const detail::code_location CL) {
413  return aligned_alloc(Alignment, Size, Dev, Ctxt, Kind, CL);
414 }
415 
416 void *aligned_alloc(size_t Alignment, size_t Size, const queue &Q, alloc Kind,
417  const detail::code_location CL) {
418  return aligned_alloc(Alignment, Size, Q.get_device(), Q.get_context(), Kind,
419  CL);
420 }
421 
422 void *aligned_alloc(size_t Alignment, size_t Size, const queue &Q, alloc Kind,
423  const property_list &PropList,
424  const detail::code_location CL) {
425  return aligned_alloc(Alignment, Size, Q.get_device(), Q.get_context(), Kind,
426  PropList, CL);
427 }
428 
429 // Pointer queries
435 alloc get_pointer_type(const void *Ptr, const context &Ctxt) {
436  if (!Ptr)
437  return alloc::unknown;
438 
439  // Everything on a host device is just system malloc so call it host
440  if (Ctxt.is_host())
441  return alloc::host;
442 
443  std::shared_ptr<detail::context_impl> CtxImpl = detail::getSyclObjImpl(Ctxt);
444  pi_context PICtx = CtxImpl->getHandleRef();
445  pi_usm_type AllocTy;
446 
447  // query type using PI function
448  const detail::plugin &Plugin = CtxImpl->getPlugin();
449  RT::PiResult Err =
451  PICtx, Ptr, PI_MEM_ALLOC_TYPE, sizeof(pi_usm_type), &AllocTy,
452  nullptr);
453 
454  // PI_INVALID_VALUE means USM doesn't know about this ptr
455  if (Err == PI_INVALID_VALUE)
456  return alloc::unknown;
457  // otherwise PI_SUCCESS is expected
458  if (Err != PI_SUCCESS) {
459  Plugin.reportPiError(Err, "get_pointer_type()");
460  }
461 
462  alloc ResultAlloc;
463  switch (AllocTy) {
464  case PI_MEM_TYPE_HOST:
465  ResultAlloc = alloc::host;
466  break;
467  case PI_MEM_TYPE_DEVICE:
468  ResultAlloc = alloc::device;
469  break;
470  case PI_MEM_TYPE_SHARED:
471  ResultAlloc = alloc::shared;
472  break;
473  default:
474  ResultAlloc = alloc::unknown;
475  break;
476  }
477 
478  return ResultAlloc;
479 }
480 
485 device get_pointer_device(const void *Ptr, const context &Ctxt) {
486  // Check if ptr is a valid USM pointer
487  if (get_pointer_type(Ptr, Ctxt) == alloc::unknown)
488  throw runtime_error("Ptr not a valid USM allocation!", PI_INVALID_VALUE);
489 
490  // Just return the host device in the host context
491  if (Ctxt.is_host())
492  return Ctxt.get_devices()[0];
493 
494  std::shared_ptr<detail::context_impl> CtxImpl = detail::getSyclObjImpl(Ctxt);
495 
496  // Check if ptr is a host allocation
497  if (get_pointer_type(Ptr, Ctxt) == alloc::host) {
498  auto Devs = CtxImpl->getDevices();
499  if (Devs.size() == 0)
500  throw runtime_error("No devices in passed context!", PI_INVALID_VALUE);
501 
502  // Just return the first device in the context
503  return Devs[0];
504  }
505 
506  pi_context PICtx = CtxImpl->getHandleRef();
507  pi_device DeviceId;
508 
509  // query device using PI function
510  const detail::plugin &Plugin = CtxImpl->getPlugin();
512  PICtx, Ptr, PI_MEM_ALLOC_DEVICE, sizeof(pi_device), &DeviceId, nullptr);
513 
514  for (const device &Dev : CtxImpl->getDevices()) {
515  // Try to find the real sycl device used in the context
516  if (detail::getSyclObjImpl(Dev)->getHandleRef() == DeviceId)
517  return Dev;
518  }
519 
520  throw runtime_error("Cannot find device associated with USM allocation!",
522 }
523 
524 // For ABI compatibility
525 
526 __SYCL_EXPORT void *malloc_device(size_t Size, const device &Dev,
527  const context &Ctxt) {
528  return detail::usm::alignedAlloc(0, Size, Ctxt, Dev, alloc::device,
530 }
531 
532 __SYCL_EXPORT void *malloc_device(size_t Size, const device &Dev,
533  const context &Ctxt, const property_list &) {
534  return malloc_device(Size, Dev, Ctxt, detail::code_location{});
535 }
536 
537 __SYCL_EXPORT void *malloc_device(size_t Size, const queue &Q) {
538  return malloc_device(Size, Q.get_device(), Q.get_context(),
540 }
541 
542 __SYCL_EXPORT void *malloc_device(size_t Size, const queue &Q,
543  const property_list &PropList) {
544  return malloc_device(Size, Q.get_device(), Q.get_context(), PropList,
546 }
547 
548 __SYCL_EXPORT void *aligned_alloc_device(size_t Alignment, size_t Size,
549  const device &Dev,
550  const context &Ctxt) {
551  return detail::usm::alignedAlloc(Alignment, Size, Ctxt, Dev, alloc::device,
553 }
554 
555 __SYCL_EXPORT void *aligned_alloc_device(size_t Alignment, size_t Size,
556  const device &Dev, const context &Ctxt,
557  const property_list &) {
558  return aligned_alloc_device(Alignment, Size, Dev, Ctxt,
560 }
561 
562 __SYCL_EXPORT void *aligned_alloc_device(size_t Alignment, size_t Size,
563  const queue &Q) {
564  return aligned_alloc_device(Alignment, Size, Q.get_device(), Q.get_context(),
566 }
567 
568 __SYCL_EXPORT void *aligned_alloc_device(size_t Alignment, size_t Size,
569  const queue &Q,
570  const property_list &PropList) {
571  return aligned_alloc_device(Alignment, Size, Q.get_device(), Q.get_context(),
572  PropList, detail::code_location{});
573 }
574 
575 __SYCL_EXPORT void free(void *ptr, const context &Ctxt) {
576  return detail::usm::free(ptr, Ctxt, detail::code_location{});
577 }
578 
579 __SYCL_EXPORT void free(void *ptr, const queue &Q) {
580  return free(ptr, Q.get_context(), detail::code_location{});
581 }
582 
584 // Restricted USM
586 __SYCL_EXPORT void *malloc_host(size_t Size, const context &Ctxt) {
587  return detail::usm::alignedAllocHost(0, Size, Ctxt, alloc::host,
589 }
590 
591 __SYCL_EXPORT void *malloc_host(size_t Size, const context &Ctxt,
592  const property_list &) {
593  return malloc_host(Size, Ctxt, detail::code_location{});
594 }
595 
596 __SYCL_EXPORT void *malloc_host(size_t Size, const queue &Q) {
597  return malloc_host(Size, Q.get_context(), detail::code_location{});
598 }
599 
600 __SYCL_EXPORT void *malloc_host(size_t Size, const queue &Q,
601  const property_list &PropList) {
602  return malloc_host(Size, Q.get_context(), PropList, detail::code_location{});
603 }
604 
605 __SYCL_EXPORT void *malloc_shared(size_t Size, const device &Dev,
606  const context &Ctxt) {
607  return detail::usm::alignedAlloc(0, Size, Ctxt, Dev, alloc::shared,
609 }
610 
611 __SYCL_EXPORT void *malloc_shared(size_t Size, const device &Dev,
612  const context &Ctxt, const property_list &) {
613  return malloc_shared(Size, Dev, Ctxt, detail::code_location{});
614 }
615 
616 __SYCL_EXPORT void *malloc_shared(size_t Size, const queue &Q) {
617  return malloc_shared(Size, Q.get_device(), Q.get_context(),
619 }
620 
621 __SYCL_EXPORT void *malloc_shared(size_t Size, const queue &Q,
622  const property_list &PropList) {
623  return malloc_shared(Size, Q.get_device(), Q.get_context(), PropList,
625 }
626 
627 __SYCL_EXPORT void *aligned_alloc_host(size_t Alignment, size_t Size,
628  const context &Ctxt) {
629  return detail::usm::alignedAllocHost(Alignment, Size, Ctxt, alloc::host,
631 }
632 
633 __SYCL_EXPORT void *aligned_alloc_host(size_t Alignment, size_t Size,
634  const context &Ctxt,
635  const property_list &) {
636  return aligned_alloc_host(Alignment, Size, Ctxt, detail::code_location{});
637 }
638 
639 __SYCL_EXPORT void *aligned_alloc_host(size_t Alignment, size_t Size,
640  const queue &Q) {
641  return aligned_alloc_host(Alignment, Size, Q.get_context(),
643 }
644 
645 __SYCL_EXPORT void *aligned_alloc_host(size_t Alignment, size_t Size,
646  const queue &Q,
647  const property_list &PropList) {
648  return aligned_alloc_host(Alignment, Size, Q.get_context(), PropList,
650 }
651 
652 __SYCL_EXPORT void *aligned_alloc_shared(size_t Alignment, size_t Size,
653  const device &Dev,
654  const context &Ctxt) {
655  return detail::usm::alignedAlloc(Alignment, Size, Ctxt, Dev, alloc::shared,
657 }
658 
659 __SYCL_EXPORT void *aligned_alloc_shared(size_t Alignment, size_t Size,
660  const device &Dev, const context &Ctxt,
661  const property_list &) {
662  return aligned_alloc_shared(Alignment, Size, Dev, Ctxt,
664 }
665 
666 __SYCL_EXPORT void *aligned_alloc_shared(size_t Alignment, size_t Size,
667  const queue &Q) {
668  return aligned_alloc_shared(Alignment, Size, Q.get_device(), Q.get_context(),
670 }
671 
672 __SYCL_EXPORT void *aligned_alloc_shared(size_t Alignment, size_t Size,
673  const queue &Q,
674  const property_list &PropList) {
675  return aligned_alloc_shared(Alignment, Size, Q.get_device(), Q.get_context(),
676  PropList, detail::code_location{});
677 }
678 
679 // single form
680 
681 __SYCL_EXPORT void *malloc(size_t Size, const device &Dev, const context &Ctxt,
682  alloc Kind) {
683  void *RetVal = nullptr;
684 
685  if (Kind == alloc::host) {
686  RetVal = detail::usm::alignedAllocHost(0, Size, Ctxt, Kind,
688  } else {
689  RetVal = detail::usm::alignedAlloc(0, Size, Ctxt, Dev, Kind,
691  }
692 
693  return RetVal;
694 }
695 
696 __SYCL_EXPORT void *malloc(size_t Size, const device &Dev, const context &Ctxt,
697  alloc Kind, const property_list &) {
698  return malloc(Size, Dev, Ctxt, Kind, detail::code_location{});
699 }
700 
701 __SYCL_EXPORT void *malloc(size_t Size, const queue &Q, alloc Kind) {
702  return malloc(Size, Q.get_device(), Q.get_context(), Kind,
704 }
705 
706 __SYCL_EXPORT void *malloc(size_t Size, const queue &Q, alloc Kind,
707  const property_list &PropList) {
708  return malloc(Size, Q.get_device(), Q.get_context(), Kind, PropList,
710 }
711 
712 __SYCL_EXPORT void *aligned_alloc(size_t Alignment, size_t Size,
713  const device &Dev, const context &Ctxt,
714  alloc Kind) {
715  void *RetVal = nullptr;
716 
717  if (Kind == alloc::host) {
718  RetVal = detail::usm::alignedAllocHost(Alignment, Size, Ctxt, Kind,
720  } else {
721  RetVal = detail::usm::alignedAlloc(Alignment, Size, Ctxt, Dev, Kind,
723  }
724 
725  return RetVal;
726 }
727 
728 __SYCL_EXPORT void *aligned_alloc(size_t Alignment, size_t Size,
729  const device &Dev, const context &Ctxt,
730  alloc Kind, const property_list &) {
731  return aligned_alloc(Alignment, Size, Dev, Ctxt, Kind,
733 }
734 
735 __SYCL_EXPORT void *aligned_alloc(size_t Alignment, size_t Size, const queue &Q,
736  alloc Kind) {
737  return aligned_alloc(Alignment, Size, Q.get_device(), Q.get_context(), Kind,
739 }
740 
741 __SYCL_EXPORT void *aligned_alloc(size_t Alignment, size_t Size, const queue &Q,
742  alloc Kind, const property_list &PropList) {
743  return aligned_alloc(Alignment, Size, Q.get_device(), Q.get_context(), Kind,
744  PropList, detail::code_location{});
745 }
746 } // namespace sycl
747 } // __SYCL_INLINE_NAMESPACE(cl)
cl::sycl::aligned_alloc
void * aligned_alloc(size_t Alignment, size_t Size, const queue &Q, alloc Kind, const property_list &PropList)
Definition: usm_impl.cpp:741
PI_SUCCESS
@ PI_SUCCESS
Definition: pi.h:86
cl::sycl::detail::aligned_allocator
Definition: aligned_allocator.hpp:23
XPTI_CREATE_TRACEPOINT
#define XPTI_CREATE_TRACEPOINT(CL)
Definition: usm_impl.cpp:35
piextUSMFree
pi_result piextUSMFree(pi_context context, void *ptr)
Indicates that the allocated USM memory is no longer needed on the runtime side.
Definition: pi_esimd_emulator.cpp:1889
cl::sycl::info::device
device
Definition: info_desc.hpp:53
cl::sycl::malloc_host
void * malloc_host(size_t Size, const queue &Q, const property_list &PropList)
Definition: usm_impl.cpp:600
cl::sycl::ext::intel::experimental::property
Definition: usm_properties.hpp:25
PI_INVALID_OPERATION
@ PI_INVALID_OPERATION
Definition: pi.h:88
cl::sycl::malloc
void * malloc(size_t Size, const queue &Q, alloc Kind, const property_list &PropList)
Definition: usm_impl.cpp:706
_pi_usm_type
_pi_usm_type
Definition: pi.h:1648
device.hpp
cl::sycl::aligned_alloc_shared
void * aligned_alloc_shared(size_t Alignment, size_t Size, const queue &Q, const property_list &PropList)
Definition: usm_impl.cpp:672
_pi_result
_pi_result
Definition: pi.h:85
cl::sycl::device::has_extension
bool has_extension(const std::string &extension_name) const
Check SYCL extension support by device.
Definition: device.cpp:141
context.hpp
os_util.hpp
sycl
Definition: invoke_simd.hpp:68
usm.hpp
cl::sycl::property_list
Objects of the property_list class are containers for the SYCL properties.
Definition: property_list.hpp:26
queue_impl.hpp
cl::sycl::queue
Encapsulates a single SYCL queue which schedules kernels on a SYCL device.
Definition: queue.hpp:103
cl::sycl::detail::code_location
Definition: common.hpp:54
cl::sycl::detail::usm::alignedAllocHost
void * alignedAllocHost(size_t Alignment, size_t Size, const context &Ctxt, alloc Kind)
Definition: usm_impl.cpp:198
pi.hpp
piextUSMSharedAlloc
pi_result piextUSMSharedAlloc(void **result_ptr, pi_context context, pi_device device, pi_usm_mem_properties *properties, size_t size, pi_uint32 alignment)
Allocates memory accessible on both host and device.
Definition: pi_esimd_emulator.cpp:1850
PI_MEM_TYPE_DEVICE
@ PI_MEM_TYPE_DEVICE
Definition: pi.h:1651
cl::sycl::info::queue
queue
Definition: info_desc.hpp:229
cl::sycl::detail::plugin::call_nocheck
RT::PiResult call_nocheck(ArgsT... Args) const
Calls the PiApi, traces the call, and returns the result.
Definition: plugin.hpp:170
piextUSMDeviceAlloc
pi_result piextUSMDeviceAlloc(void **result_ptr, pi_context context, pi_device device, pi_usm_mem_properties *properties, size_t size, pi_uint32 alignment)
Allocates device memory.
Definition: pi_esimd_emulator.cpp:1845
cl::sycl::aligned_alloc_host
void * aligned_alloc_host(size_t Alignment, size_t Size, const queue &Q, const property_list &PropList)
Definition: usm_impl.cpp:645
cl::sycl::malloc_device
void * malloc_device(size_t Size, const queue &Q, const property_list &PropList)
Definition: usm_impl.cpp:542
PI_MEM_ALLOC_DEVICE_READ_ONLY
constexpr pi_usm_mem_properties PI_MEM_ALLOC_DEVICE_READ_ONLY
Definition: pi.h:616
cl::sycl::ext::oneapi::property::usm::device_read_only
Definition: usm_properties.hpp:14
pi_usm_type
_pi_usm_type pi_usm_type
Definition: pi.h:1665
cl::sycl::device
The SYCL device class encapsulates a single SYCL device on which kernels may be executed.
Definition: device.hpp:35
cl::sycl::context::is_host
bool is_host() const
Gets OpenCL interoperability context.
Definition: context.cpp:119
PI_MEM_TYPE_SHARED
@ PI_MEM_TYPE_SHARED
Definition: pi.h:1652
cl::sycl::detail::aligned_allocator::allocate
pointer allocate(size_t Size)
Definition: aligned_allocator.hpp:51
cl::sycl::detail::plugin::call
void call(ArgsT... Args) const
Calls the API, traces the call, checks the result.
Definition: plugin.hpp:217
cl
We provide new interfaces for matrix muliply in this patch:
Definition: access.hpp:13
cl::sycl::get_pointer_device
device get_pointer_device(const void *ptr, const context &ctxt)
Queries the device against which the pointer was allocated Throws an invalid_object_error if ptr is a...
Definition: usm_impl.cpp:485
cl::sycl::alloc
cl::sycl::usm::alloc alloc
Definition: usm_impl.cpp:41
piextUSMHostAlloc
pi_result piextUSMHostAlloc(void **result_ptr, pi_context context, pi_usm_mem_properties *properties, size_t size, pi_uint32 alignment)
Allocates host memory accessible by the device.
Definition: pi_esimd_emulator.cpp:1840
cl::sycl::detail::plugin
The plugin class provides a unified interface to the underlying low-level runtimes for the device-agn...
Definition: plugin.hpp:90
piextUSMGetMemAllocInfo
pi_result piextUSMGetMemAllocInfo(pi_context context, const void *ptr, pi_mem_alloc_info param_name, size_t param_value_size, void *param_value, size_t *param_value_size_ret)
API to query information about USM allocated pointers Valid Queries: PI_MEM_ALLOC_TYPE returns host/d...
Definition: pi_esimd_emulator.cpp:1932
aligned_allocator.hpp
cl::sycl::context::get_devices
std::vector< device > get_devices() const
Gets devices associated with this SYCL context.
Definition: context.cpp:127
cl::sycl::queue::get_device
device get_device() const
Definition: queue.cpp:75
cl::sycl::get_pointer_type
usm::alloc get_pointer_type(const void *ptr, const context &ctxt)
Query the allocation type from a USM pointer.
Definition: usm_impl.cpp:435
PI_MEM_ALLOC_FLAGS
constexpr pi_usm_mem_properties PI_MEM_ALLOC_FLAGS
Definition: pi.h:608
cl::sycl::malloc_shared
void * malloc_shared(size_t Size, const queue &Q, const property_list &PropList)
Definition: usm_impl.cpp:621
cl::sycl::queue::get_context
context get_context() const
Definition: queue.cpp:73
PI_INVALID_VALUE
@ PI_INVALID_VALUE
Definition: pi.h:91
cl::sycl::detail::getSyclObjImpl
decltype(Obj::impl) getSyclObjImpl(const Obj &SyclObject)
Definition: common.hpp:204
cl::sycl::aligned_alloc_device
void * aligned_alloc_device(size_t Alignment, size_t Size, const queue &Q, const property_list &PropList)
Definition: usm_impl.cpp:568
cl::sycl::detail::usm::alignedAlloc
void * alignedAlloc(size_t Alignment, size_t Size, const context &Ctxt, const device &Dev, alloc Kind)
Definition: usm_impl.cpp:207
cl::sycl::context
The context class represents a SYCL context on which kernel functions may be executed.
Definition: context.hpp:35
PI_MEM_ALLOC_DEVICE
@ PI_MEM_ALLOC_DEVICE
Definition: pi.h:1645
cl::sycl::free
void free(void *ptr, const queue &Q)
Definition: usm_impl.cpp:579
PI_MEM_TYPE_HOST
@ PI_MEM_TYPE_HOST
Definition: pi.h:1650
PI_MEM_USM_ALLOC_BUFFER_LOCATION
constexpr pi_usm_mem_properties PI_MEM_USM_ALLOC_BUFFER_LOCATION
Definition: pi.h:618
cl::sycl::detail::plugin::reportPiError
void reportPiError(RT::PiResult pi_result, const char *context) const
Definition: plugin.hpp:149
pi_usm_mem_properties
pi_bitfield pi_usm_mem_properties
Definition: pi.h:607
PI_MEM_ALLOC_TYPE
@ PI_MEM_ALLOC_TYPE
Definition: pi.h:1642
cl::sycl::usm::alloc
alloc
Definition: usm_enums.hpp:14
pi_device
_pi_device * pi_device
Definition: pi.h:922
_pi_context
PI context mapping to a CUDA context object.
Definition: pi_cuda.hpp:150
_pi_device
PI device mapping to a CUdevice.
Definition: pi_cuda.hpp:73
__SYCL_INLINE_NAMESPACE
#define __SYCL_INLINE_NAMESPACE(X)
Definition: defines_elementary.hpp:12