forked from intel/llvm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathusm_impl.cpp
More file actions
764 lines (657 loc) · 28.9 KB
/
usm_impl.cpp
File metadata and controls
764 lines (657 loc) · 28.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
//==---------------- usm_impl.cpp - USM API Utils -------------*- C++ -*---==//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
// ===--------------------------------------------------------------------=== //
#include <detail/queue_impl.hpp>
#include <detail/usm/usm_impl.hpp>
#include <sycl/context.hpp>
#include <sycl/detail/aligned_allocator.hpp>
#include <sycl/detail/os_util.hpp>
#include <sycl/detail/ur.hpp>
#include <sycl/device.hpp>
#include <sycl/ext/intel/experimental/usm_properties.hpp>
#include <sycl/ext/oneapi/memcpy2d.hpp>
#include <sycl/usm.hpp>
#include <array>
#include <cassert>
#include <cstdlib>
#include <memory>
#ifdef XPTI_ENABLE_INSTRUMENTATION
// Include the headers necessary for emitting
// traces using the trace framework
#include "xpti/xpti_trace_framework.hpp"
#include <detail/xpti_registry.hpp>
#endif
namespace {
void *alignedAllocHost(size_t Alignment, size_t Size, const sycl::context &Ctxt,
const sycl::property_list &PropList,
const sycl::detail::code_location &CodeLoc) {
#ifdef XPTI_ENABLE_INSTRUMENTATION
// Stash the code location information and propagate
sycl::detail::tls_code_loc_t CL(CodeLoc);
const char *UserData = "malloc_host";
xpti::framework::tracepoint_scope_t TP(
CodeLoc.fileName(), CodeLoc.functionName(), CodeLoc.lineNumber(),
CodeLoc.columnNumber(), nullptr);
TP.stream(sycl::detail::GMemAllocStreamID)
.traceType(xpti::trace_point_type_t::node_create)
.parentEvent(sycl::detail::GSYCLGraphEvent);
TP.addMetadata([&](auto TEvent) {
xpti::addMetadata(TEvent, "sycl_device_name", std::string("Host"));
xpti::addMetadata(TEvent, "sycl_device", 0);
xpti::addMetadata(TEvent, "memory_size", Size);
});
// Notify XPTI about the allocation submission
TP.notify(UserData);
// Emit a begin/end scope for this call
TP.scopedNotify((uint16_t)xpti::trace_point_type_t::mem_alloc_begin,
UserData);
#endif
const auto &devices = Ctxt.get_devices();
if (!std::any_of(devices.begin(), devices.end(), [&](const auto &device) {
return device.has(sycl::aspect::usm_host_allocations);
})) {
throw sycl::exception(
sycl::errc::feature_not_supported,
"No device in this context supports USM host allocations!");
}
void *RetVal = nullptr;
if (Size == 0)
return nullptr;
auto [urCtx, Adapter] = get_ur_handles(Ctxt);
ur_result_t Error = UR_RESULT_ERROR_INVALID_VALUE;
ur_usm_desc_t UsmDesc{};
UsmDesc.align = Alignment;
ur_usm_alloc_location_desc_t UsmLocationDesc{};
UsmLocationDesc.stype = UR_STRUCTURE_TYPE_USM_ALLOC_LOCATION_DESC;
if (PropList.has_property<
sycl::ext::intel::experimental::property::usm::buffer_location>() &&
Ctxt.get_platform().has_extension("cl_intel_mem_alloc_buffer_location")) {
UsmLocationDesc.location = static_cast<uint32_t>(
PropList
.get_property<sycl::ext::intel::experimental::property::usm::
buffer_location>()
.get_buffer_location());
UsmDesc.pNext = &UsmLocationDesc;
}
Error = Adapter->call_nocheck<sycl::detail::UrApiKind::urUSMHostAlloc>(
urCtx, &UsmDesc,
/* pool= */ nullptr, Size, &RetVal);
// Error is for debugging purposes.
// The spec wants a nullptr returned, not an exception.
if (Error != UR_RESULT_SUCCESS)
return nullptr;
#ifdef XPTI_ENABLE_INSTRUMENTATION
// Once the allocation is complete, update metadata with the memory pointer
// before the mem_alloc_end event is sent
xpti::addMetadata(TP.traceEvent(), "memory_ptr",
reinterpret_cast<size_t>(RetVal));
#endif
return RetVal;
}
} // namespace
namespace sycl {
inline namespace _V1 {
using alloc = sycl::usm::alloc;
namespace detail {
namespace usm {
void *alignedAllocInternal(size_t Alignment, size_t Size,
const context_impl *CtxImpl,
const device_impl *DevImpl, alloc Kind,
const property_list &PropList) {
if (Kind == alloc::device &&
!DevImpl->has(sycl::aspect::usm_device_allocations)) {
throw sycl::exception(sycl::errc::feature_not_supported,
"Device does not support USM device allocations!");
}
if (Kind == alloc::shared &&
!DevImpl->has(sycl::aspect::usm_shared_allocations)) {
throw sycl::exception(sycl::errc::feature_not_supported,
"Device does not support shared USM allocations!");
}
void *RetVal = nullptr;
if (Size == 0)
return nullptr;
ur_context_handle_t C = CtxImpl->getHandleRef();
adapter_impl &Adapter = CtxImpl->getAdapter();
ur_result_t Error = UR_RESULT_ERROR_INVALID_VALUE;
ur_device_handle_t Dev;
switch (Kind) {
case alloc::device: {
Dev = DevImpl->getHandleRef();
ur_usm_desc_t UsmDesc{};
UsmDesc.align = Alignment;
ur_usm_alloc_location_desc_t UsmLocationDesc{};
UsmLocationDesc.stype = UR_STRUCTURE_TYPE_USM_ALLOC_LOCATION_DESC;
// Buffer location is only supported on FPGA devices
if (PropList.has_property<
sycl::ext::intel::experimental::property::usm::buffer_location>() &&
DevImpl->has_extension("cl_intel_mem_alloc_buffer_location")) {
UsmLocationDesc.location = static_cast<uint32_t>(
PropList
.get_property<sycl::ext::intel::experimental::property::usm::
buffer_location>()
.get_buffer_location());
UsmDesc.pNext = &UsmLocationDesc;
}
Error = Adapter.call_nocheck<detail::UrApiKind::urUSMDeviceAlloc>(
C, Dev, &UsmDesc,
/*pool=*/nullptr, Size, &RetVal);
break;
}
case alloc::shared: {
Dev = DevImpl->getHandleRef();
ur_usm_desc_t UsmDesc{};
UsmDesc.align = Alignment;
ur_usm_alloc_location_desc_t UsmLocationDesc{};
UsmLocationDesc.stype = UR_STRUCTURE_TYPE_USM_ALLOC_LOCATION_DESC;
ur_usm_device_desc_t UsmDeviceDesc{};
UsmDeviceDesc.stype = UR_STRUCTURE_TYPE_USM_DEVICE_DESC;
UsmDeviceDesc.flags = 0;
UsmDesc.pNext = &UsmDeviceDesc;
if (PropList.has_property<
sycl::ext::oneapi::property::usm::device_read_only>()) {
UsmDeviceDesc.flags |= UR_USM_DEVICE_MEM_FLAG_DEVICE_READ_ONLY;
}
if (PropList.has_property<
sycl::ext::intel::experimental::property::usm::buffer_location>() &&
DevImpl->has_extension("cl_intel_mem_alloc_buffer_location")) {
UsmLocationDesc.location = static_cast<uint32_t>(
PropList
.get_property<sycl::ext::intel::experimental::property::usm::
buffer_location>()
.get_buffer_location());
UsmDeviceDesc.pNext = &UsmLocationDesc;
}
Error = Adapter.call_nocheck<detail::UrApiKind::urUSMSharedAlloc>(
C, Dev, &UsmDesc,
/*pool=*/nullptr, Size, &RetVal);
break;
}
case alloc::host:
case alloc::unknown: {
RetVal = nullptr;
Error = UR_RESULT_ERROR_INVALID_VALUE;
break;
}
}
// Error is for debugging purposes.
// The spec wants a nullptr returned, not an exception.
if (Error != UR_RESULT_SUCCESS)
return nullptr;
return RetVal;
}
void *alignedAlloc(size_t Alignment, size_t Size, const context &Ctxt,
const device &Dev, alloc Kind, const property_list &PropList,
const detail::code_location &CodeLoc) {
#ifdef XPTI_ENABLE_INSTRUMENTATION
// Stash the code location information and propagate
detail::tls_code_loc_t CL(CodeLoc);
const char *UserData = "usm::alignedAlloc";
xpti::framework::tracepoint_scope_t TP(
CodeLoc.fileName(), CodeLoc.functionName(), CodeLoc.lineNumber(),
CodeLoc.columnNumber(), nullptr);
TP.stream(sycl::detail::GMemAllocStreamID)
.traceType(xpti::trace_point_type_t::node_create)
.parentEvent(sycl::detail::GSYCLGraphEvent);
TP.addMetadata([&](auto TEvent) {
xpti::addMetadata(TEvent, "sycl_device_name",
Dev.get_info<info::device::name>());
// Need to determine how to get the device handle reference
// xpti::addMetadata(TEvent, "sycl_device", Dev.getHandleRef()));
xpti::addMetadata(TEvent, "memory_size", Size);
});
// Notify XPTI about the allocation submission
TP.notify(UserData);
// Emit a begin/end scope for this call
TP.scopedNotify((uint16_t)xpti::trace_point_type_t::mem_alloc_begin,
UserData);
#endif
void *RetVal =
alignedAllocInternal(Alignment, Size, getSyclObjImpl(Ctxt).get(),
getSyclObjImpl(Dev).get(), Kind, PropList);
#ifdef XPTI_ENABLE_INSTRUMENTATION
// Once the allocation is complete, update metadata with the memory pointer
// before the mem_alloc_end event is sent
xpti::addMetadata(TP.traceEvent(), "memory_ptr",
reinterpret_cast<size_t>(RetVal));
#endif
return RetVal;
}
void freeInternal(void *Ptr, const context_impl *CtxImpl) {
if (Ptr == nullptr)
return;
ur_context_handle_t C = CtxImpl->getHandleRef();
adapter_impl &Adapter = CtxImpl->getAdapter();
Adapter.call<detail::UrApiKind::urUSMFree>(C, Ptr);
}
void free(void *Ptr, const context &Ctxt,
const detail::code_location &CodeLoc) {
#ifdef XPTI_ENABLE_INSTRUMENTATION
// Stash the code location information and propagate
detail::tls_code_loc_t CL(CodeLoc);
const char *UserData = "usm::free";
xpti::framework::tracepoint_scope_t TP(
CodeLoc.fileName(), CodeLoc.functionName(), CodeLoc.lineNumber(),
CodeLoc.columnNumber(), nullptr);
TP.stream(sycl::detail::GMemAllocStreamID)
.traceType(xpti::trace_point_type_t::node_create)
.parentEvent(sycl::detail::GSYCLGraphEvent);
TP.addMetadata([&](auto TEvent) {
xpti::addMetadata(TEvent, "memory_ptr", reinterpret_cast<size_t>(Ptr));
});
// Notify XPTI about the memset submission
TP.notify(UserData);
// Emit a begin/end scope for this call
TP.scopedNotify((uint16_t)xpti::trace_point_type_t::mem_release_begin,
UserData);
#endif
freeInternal(Ptr, detail::getSyclObjImpl(Ctxt).get());
}
} // namespace usm
} // namespace detail
void *malloc_device(size_t Size, const device &Dev, const context &Ctxt,
const detail::code_location &CodeLoc) {
return detail::usm::alignedAlloc(0, Size, Ctxt, Dev, alloc::device,
property_list{}, CodeLoc);
}
void *malloc_device(size_t Size, const device &Dev, const context &Ctxt,
const property_list &PropList,
const detail::code_location &CodeLoc) {
return detail::usm::alignedAlloc(0, Size, Ctxt, Dev, alloc::device, PropList,
CodeLoc);
}
void *malloc_device(size_t Size, const queue &Q,
const detail::code_location &CodeLoc) {
return detail::usm::alignedAlloc(0, Size, Q.get_context(), Q.get_device(),
alloc::device, property_list{}, CodeLoc);
}
void *malloc_device(size_t Size, const queue &Q, const property_list &PropList,
const detail::code_location &CodeLoc) {
return detail::usm::alignedAlloc(0, Size, Q.get_context(), Q.get_device(),
alloc::device, PropList, CodeLoc);
}
void *aligned_alloc_device(size_t Alignment, size_t Size, const device &Dev,
const context &Ctxt,
const detail::code_location &CodeLoc) {
return detail::usm::alignedAlloc(Alignment, Size, Ctxt, Dev, alloc::device,
property_list{}, CodeLoc);
}
void *aligned_alloc_device(size_t Alignment, size_t Size, const device &Dev,
const context &Ctxt, const property_list &PropList,
const detail::code_location &CodeLoc) {
return detail::usm::alignedAlloc(Alignment, Size, Ctxt, Dev, alloc::device,
PropList, CodeLoc);
}
void *aligned_alloc_device(size_t Alignment, size_t Size, const queue &Q,
const detail::code_location &CodeLoc) {
return detail::usm::alignedAlloc(Alignment, Size, Q.get_context(),
Q.get_device(), alloc::device,
property_list{}, CodeLoc);
}
void *aligned_alloc_device(size_t Alignment, size_t Size, const queue &Q,
const property_list &PropList,
const detail::code_location &CodeLoc) {
return detail::usm::alignedAlloc(Alignment, Size, Q.get_context(),
Q.get_device(), alloc::device, PropList,
CodeLoc);
}
void free(void *ptr, const context &Ctxt,
const detail::code_location &CodeLoc) {
return detail::usm::free(ptr, Ctxt, CodeLoc);
}
void free(void *ptr, const queue &Q, const detail::code_location &CodeLoc) {
return detail::usm::free(ptr, Q.get_context(), CodeLoc);
}
void *malloc_host(size_t Size, const context &Ctxt,
const detail::code_location &CodeLoc) {
return alignedAllocHost(0, Size, Ctxt, property_list{}, CodeLoc);
}
void *malloc_host(size_t Size, const context &Ctxt,
const property_list &PropList,
const detail::code_location &CodeLoc) {
return alignedAllocHost(0, Size, Ctxt, PropList, CodeLoc);
}
void *malloc_host(size_t Size, const queue &Q,
const detail::code_location &CodeLoc) {
return alignedAllocHost(0, Size, Q.get_context(), property_list{}, CodeLoc);
}
void *malloc_host(size_t Size, const queue &Q, const property_list &PropList,
const detail::code_location &CodeLoc) {
return alignedAllocHost(0, Size, Q.get_context(), PropList, CodeLoc);
}
void *malloc_shared(size_t Size, const device &Dev, const context &Ctxt,
const detail::code_location &CodeLoc) {
return detail::usm::alignedAlloc(0, Size, Ctxt, Dev, alloc::shared,
property_list{}, CodeLoc);
}
void *malloc_shared(size_t Size, const device &Dev, const context &Ctxt,
const property_list &PropList,
const detail::code_location &CodeLoc) {
return detail::usm::alignedAlloc(0, Size, Ctxt, Dev, alloc::shared, PropList,
CodeLoc);
}
void *malloc_shared(size_t Size, const queue &Q,
const detail::code_location &CodeLoc) {
return detail::usm::alignedAlloc(0, Size, Q.get_context(), Q.get_device(),
alloc::shared, property_list{}, CodeLoc);
}
void *malloc_shared(size_t Size, const queue &Q, const property_list &PropList,
const detail::code_location &CodeLoc) {
return detail::usm::alignedAlloc(0, Size, Q.get_context(), Q.get_device(),
alloc::shared, PropList, CodeLoc);
}
void *aligned_alloc_host(size_t Alignment, size_t Size, const context &Ctxt,
const detail::code_location &CodeLoc) {
return alignedAllocHost(Alignment, Size, Ctxt, property_list{}, CodeLoc);
}
void *aligned_alloc_host(size_t Alignment, size_t Size, const context &Ctxt,
const property_list &PropList,
const detail::code_location &CodeLoc) {
return alignedAllocHost(Alignment, Size, Ctxt, PropList, CodeLoc);
}
void *aligned_alloc_host(size_t Alignment, size_t Size, const queue &Q,
const detail::code_location &CodeLoc) {
return alignedAllocHost(Alignment, Size, Q.get_context(), property_list{},
CodeLoc);
}
void *aligned_alloc_host(size_t Alignment, size_t Size, const queue &Q,
const property_list &PropList,
const detail::code_location &CodeLoc) {
return alignedAllocHost(Alignment, Size, Q.get_context(), PropList, CodeLoc);
}
void *aligned_alloc_shared(size_t Alignment, size_t Size, const device &Dev,
const context &Ctxt,
const detail::code_location &CodeLoc) {
return detail::usm::alignedAlloc(Alignment, Size, Ctxt, Dev, alloc::shared,
property_list{}, CodeLoc);
}
void *aligned_alloc_shared(size_t Alignment, size_t Size, const device &Dev,
const context &Ctxt, const property_list &PropList,
const detail::code_location &CodeLoc) {
return detail::usm::alignedAlloc(Alignment, Size, Ctxt, Dev, alloc::shared,
PropList, CodeLoc);
}
void *aligned_alloc_shared(size_t Alignment, size_t Size, const queue &Q,
const detail::code_location &CodeLoc) {
return detail::usm::alignedAlloc(Alignment, Size, Q.get_context(),
Q.get_device(), alloc::shared,
property_list{}, CodeLoc);
}
void *aligned_alloc_shared(size_t Alignment, size_t Size, const queue &Q,
const property_list &PropList,
const detail::code_location &CodeLoc) {
return detail::usm::alignedAlloc(Alignment, Size, Q.get_context(),
Q.get_device(), alloc::shared, PropList,
CodeLoc);
}
// single form
void *malloc(size_t Size, const device &Dev, const context &Ctxt, alloc Kind,
const property_list &PropList,
const detail::code_location &CodeLoc) {
if (Kind == alloc::host)
return alignedAllocHost(0, Size, Ctxt, PropList, CodeLoc);
return detail::usm::alignedAlloc(0, Size, Ctxt, Dev, Kind, PropList, CodeLoc);
}
void *malloc(size_t Size, const device &Dev, const context &Ctxt, alloc Kind,
const detail::code_location &CodeLoc) {
if (Kind == alloc::host)
return alignedAllocHost(0, Size, Ctxt, property_list{}, CodeLoc);
return detail::usm::alignedAlloc(0, Size, Ctxt, Dev, Kind, property_list{},
CodeLoc);
}
void *malloc(size_t Size, const queue &Q, alloc Kind,
const detail::code_location &CodeLoc) {
if (Kind == alloc::host)
return alignedAllocHost(0, Size, Q.get_context(), property_list{}, CodeLoc);
return detail::usm::alignedAlloc(0, Size, Q.get_context(), Q.get_device(),
Kind, property_list{}, CodeLoc);
}
void *malloc(size_t Size, const queue &Q, alloc Kind,
const property_list &PropList,
const detail::code_location &CodeLoc) {
if (Kind == alloc::host)
return alignedAllocHost(0, Size, Q.get_context(), PropList, CodeLoc);
return detail::usm::alignedAlloc(0, Size, Q.get_context(), Q.get_device(),
Kind, PropList, CodeLoc);
}
void *aligned_alloc(size_t Alignment, size_t Size, const device &Dev,
const context &Ctxt, alloc Kind,
const detail::code_location &CodeLoc) {
if (Kind == alloc::host)
return alignedAllocHost(Alignment, Size, Ctxt, property_list{}, CodeLoc);
return detail::usm::alignedAlloc(Alignment, Size, Ctxt, Dev, Kind,
property_list{}, CodeLoc);
}
void *aligned_alloc(size_t Alignment, size_t Size, const device &Dev,
const context &Ctxt, alloc Kind,
const property_list &PropList,
const detail::code_location &CodeLoc) {
if (Kind == alloc::host)
return alignedAllocHost(Alignment, Size, Ctxt, PropList, CodeLoc);
return detail::usm::alignedAlloc(Alignment, Size, Ctxt, Dev, Kind, PropList,
CodeLoc);
}
void *aligned_alloc(size_t Alignment, size_t Size, const queue &Q, alloc Kind,
const detail::code_location &CodeLoc) {
if (Kind == alloc::host)
return alignedAllocHost(Alignment, Size, Q.get_context(), property_list{},
CodeLoc);
return detail::usm::alignedAlloc(Alignment, Size, Q.get_context(),
Q.get_device(), Kind, property_list{},
CodeLoc);
}
void *aligned_alloc(size_t Alignment, size_t Size, const queue &Q, alloc Kind,
const property_list &PropList,
const detail::code_location &CodeLoc) {
if (Kind == alloc::host)
return alignedAllocHost(Alignment, Size, Q.get_context(), PropList,
CodeLoc);
return detail::usm::alignedAlloc(Alignment, Size, Q.get_context(),
Q.get_device(), Kind, PropList, CodeLoc);
}
// Pointer queries
/// Query the allocation type from a USM pointer
/// Returns alloc::host for all pointers in a host context.
///
/// \param Ptr is the USM pointer to query
/// \param Ctxt is the sycl context the ptr was allocated in
namespace detail {
alloc get_pointer_type(const void *Ptr, context_impl &Ctxt) {
if (!Ptr)
return alloc::unknown;
auto [urCtx, Adapter] = get_ur_handles(Ctxt);
ur_usm_type_t AllocTy;
// query type using UR function
ur_result_t Err =
Adapter->call_nocheck<detail::UrApiKind::urUSMGetMemAllocInfo>(
urCtx, Ptr, UR_USM_ALLOC_INFO_TYPE, sizeof(ur_usm_type_t), &AllocTy,
nullptr);
// UR_RESULT_ERROR_INVALID_VALUE means USM doesn't know about this ptr
if (Err == UR_RESULT_ERROR_INVALID_VALUE)
return alloc::unknown;
// otherwise UR_RESULT_SUCCESS is expected
if (Err != UR_RESULT_SUCCESS) {
throw detail::set_ur_error(
exception(make_error_code(errc::runtime), "get_pointer_type() failed"),
Err);
}
alloc ResultAlloc;
switch (AllocTy) {
case UR_USM_TYPE_HOST:
ResultAlloc = alloc::host;
break;
case UR_USM_TYPE_DEVICE:
ResultAlloc = alloc::device;
break;
case UR_USM_TYPE_SHARED:
ResultAlloc = alloc::shared;
break;
default:
ResultAlloc = alloc::unknown;
break;
}
return ResultAlloc;
}
} // namespace detail
/// Queries the device against which the pointer was allocated
///
/// \param Ptr is the USM pointer to query
/// \param Ctxt is the sycl context the ptr was allocated in
device get_pointer_device(const void *Ptr, const context &Ctxt) {
// Check if ptr is a valid USM pointer
if (get_pointer_type(Ptr, Ctxt) == alloc::unknown)
throw exception(make_error_code(errc::invalid),
"Ptr not a valid USM allocation!");
auto [urCtx, Adapter] = get_ur_handles(Ctxt);
// Check if ptr is a host allocation
if (get_pointer_type(Ptr, Ctxt) == alloc::host) {
detail::devices_range Devs = detail::getSyclObjImpl(Ctxt)->getDevices();
if (Devs.size() == 0)
throw exception(make_error_code(errc::invalid),
"No devices in passed context!");
// Just return the first device in the context
return detail::createSyclObjFromImpl<device>(Devs.front());
}
ur_device_handle_t DeviceId;
// query device using UR function
Adapter->call<detail::UrApiKind::urUSMGetMemAllocInfo>(
urCtx, Ptr, UR_USM_ALLOC_INFO_DEVICE, sizeof(ur_device_handle_t),
&DeviceId, nullptr);
// The device is not necessarily a member of the context, it could be a
// member's descendant instead. Fetch the corresponding device from the cache.
if (detail::device_impl *DevImpl =
detail::getSyclObjImpl(Ctxt)->getPlatformImpl().getDeviceImpl(
DeviceId))
return detail::createSyclObjFromImpl<device>(*DevImpl);
throw exception(make_error_code(errc::runtime),
"Cannot find device associated with USM allocation!");
}
// Device copy enhancement APIs, prepare_for and release_from USM.
static void prepare_for_usm_device_copy(const void *Ptr, size_t Size,
const context &Ctxt) {
auto [urCtx, Adapter] = get_ur_handles(Ctxt);
// Call the UR function
Adapter->call<detail::UrApiKind::urUSMImportExp>(
urCtx, const_cast<void *>(Ptr), Size);
}
static void release_from_usm_device_copy(const void *Ptr, const context &Ctxt) {
auto [urCtx, Adapter] = get_ur_handles(Ctxt);
// Call the UR function
Adapter->call<detail::UrApiKind::urUSMReleaseExp>(urCtx,
const_cast<void *>(Ptr));
}
namespace ext::oneapi::experimental {
void prepare_for_device_copy(const void *Ptr, size_t Size,
const context &Ctxt) {
prepare_for_usm_device_copy(Ptr, Size, Ctxt);
}
void prepare_for_device_copy(const void *Ptr, size_t Size, const queue &Queue) {
prepare_for_usm_device_copy(Ptr, Size, Queue.get_context());
}
void release_from_device_copy(const void *Ptr, const context &Ctxt) {
release_from_usm_device_copy(Ptr, Ctxt);
}
void release_from_device_copy(const void *Ptr, const queue &Queue) {
release_from_usm_device_copy(Ptr, Queue.get_context());
}
void *malloc_device(size_t numBytes, const device &syclDevice,
const property_list &propList) {
sycl::context ctxt = syclDevice.get_platform().khr_get_default_context();
return sycl::malloc_device(numBytes, syclDevice, ctxt, propList);
}
template <typename T>
T *malloc_device(size_t count, const device &syclDevice,
const property_list &propList) {
sycl::context ctxt = syclDevice.get_platform().khr_get_default_context();
return sycl::malloc_device<T>(count, syclDevice, ctxt, propList);
}
void *aligned_alloc_device(size_t alignment, size_t numBytes,
const device &syclDevice,
const property_list &propList) {
sycl::context ctxt = syclDevice.get_platform().khr_get_default_context();
return sycl::aligned_alloc_device(alignment, numBytes, syclDevice, ctxt,
propList);
}
template <typename T>
T *aligned_alloc_device(size_t alignment, size_t count,
const device &syclDevice,
const property_list &propList) {
sycl::context ctxt = syclDevice.get_platform().khr_get_default_context();
return sycl::aligned_alloc_device<T>(alignment, count, syclDevice, ctxt,
propList);
}
void *malloc_shared(size_t numBytes, const device &syclDevice,
const property_list &propList) {
sycl::context ctxt = syclDevice.get_platform().khr_get_default_context();
return sycl::malloc_shared(numBytes, syclDevice, ctxt, propList);
}
template <typename T>
T *malloc_shared(size_t count, const device &syclDevice,
const property_list &propList) {
sycl::context ctxt = syclDevice.get_platform().khr_get_default_context();
return sycl::malloc_shared<T>(count, syclDevice, ctxt, propList);
}
void *aligned_alloc_shared(size_t alignment, size_t numBytes,
const device &syclDevice,
const property_list &propList) {
sycl::context ctxt = syclDevice.get_platform().khr_get_default_context();
return sycl::aligned_alloc_shared(alignment, numBytes, syclDevice, ctxt,
propList);
}
template <typename T>
T *aligned_alloc_shared(size_t alignment, size_t count,
const device &syclDevice,
const property_list &propList) {
sycl::context ctxt = syclDevice.get_platform().khr_get_default_context();
return sycl::aligned_alloc_shared<T>(alignment, count, syclDevice, ctxt,
propList);
}
void *malloc(size_t numBytes, const device &syclDevice, usm::alloc kind,
const property_list &propList) {
sycl::context ctxt = syclDevice.get_platform().khr_get_default_context();
return sycl::malloc(numBytes, syclDevice, ctxt, kind, propList);
}
template <typename T>
T *malloc(size_t count, const device &syclDevice, usm::alloc kind,
const property_list &propList) {
sycl::context ctxt = syclDevice.get_platform().khr_get_default_context();
return sycl::malloc_shared<T>(count, syclDevice, ctxt, kind, propList);
}
void *aligned_alloc(size_t alignment, size_t numBytes, const device &syclDevice,
usm::alloc kind, const property_list &propList) {
sycl::context ctxt = syclDevice.get_platform().khr_get_default_context();
return sycl::aligned_alloc(alignment, numBytes, syclDevice, ctxt, kind,
propList);
}
template <typename T>
T *aligned_alloc(size_t alignment, size_t count, const device &syclDevice,
usm::alloc kind, const property_list &propList) {
sycl::context ctxt = syclDevice.get_platform().khr_get_default_context();
return sycl::aligned_alloc<T>(alignment, count, syclDevice, ctxt, kind,
propList);
}
} // namespace ext::oneapi::experimental
__SYCL_EXPORT void verifyUSMAllocatorProperties(const property_list &PropList) {
auto DataLessCheck = [](int Kind) {
switch (Kind) {
case detail::DeviceReadOnly:
return true;
}
return false;
};
auto WithDataCheck = [](int Kind) {
switch (Kind) {
case detail::PropWithDataKind::AccPropBufferLocation:
return true;
}
return false;
};
detail::PropertyValidator::checkPropsAndThrow(PropList, DataLessCheck,
WithDataCheck);
}
} // namespace _V1
} // namespace sycl