-
Notifications
You must be signed in to change notification settings - Fork 123
/
Copy pathmemory.cpp
2408 lines (2143 loc) · 104 KB
/
memory.cpp
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
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//===--------- memory.cpp - Level Zero Adapter ----------------------------===//
//
// Copyright (C) 2023 Intel Corporation
//
// Part of the Unified-Runtime Project, under the Apache License v2.0 with LLVM
// Exceptions. See LICENSE.TXT
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include <algorithm>
#include <climits>
#include <string.h>
#include <ur/ur.hpp>
#include "context.hpp"
#include "event.hpp"
#include "helpers/memory_helpers.hpp"
#include "image.hpp"
#include "logger/ur_logger.hpp"
#include "queue.hpp"
#include "ur_interface_loader.hpp"
#include "ur_level_zero.hpp"
// Default to using compute engine for fill operation, but allow to
// override this with an environment variable.
static bool PreferCopyEngine = [] {
const char *UrRet = std::getenv("UR_L0_USE_COPY_ENGINE_FOR_FILL");
const char *PiRet =
std::getenv("SYCL_PI_LEVEL_ZERO_USE_COPY_ENGINE_FOR_FILL");
return (UrRet ? std::stoi(UrRet) : (PiRet ? std::stoi(PiRet) : 0));
}();
// Helper function to check if a pointer is a device pointer.
bool IsDevicePointer(ur_context_handle_t Context, const void *Ptr) {
ze_device_handle_t ZeDeviceHandle;
ZeStruct<ze_memory_allocation_properties_t> ZeMemoryAllocationProperties;
// Query memory type of the pointer
ZE2UR_CALL(zeMemGetAllocProperties,
(Context->ZeContext, Ptr, &ZeMemoryAllocationProperties,
&ZeDeviceHandle));
return (ZeMemoryAllocationProperties.type == ZE_MEMORY_TYPE_DEVICE);
}
// Helper function to check if a pointer is a shared pointer.
bool IsSharedPointer(ur_context_handle_t Context, const void *Ptr) {
ze_device_handle_t ZeDeviceHandle;
ZeStruct<ze_memory_allocation_properties_t> ZeMemoryAllocationProperties;
// Query memory type of the pointer
ZE2UR_CALL(zeMemGetAllocProperties,
(Context->ZeContext, Ptr, &ZeMemoryAllocationProperties,
&ZeDeviceHandle));
return (ZeMemoryAllocationProperties.type == ZE_MEMORY_TYPE_SHARED);
}
// Helper Function to check if the Copy Engine should be preferred given the
// types of memory used.
bool PreferCopyEngineUsage(ur_device_handle_t Device,
ur_context_handle_t Context, const void *Src,
void *Dst) {
bool PreferCopyEngine = false;
// Given Integrated Devices, Copy Engines are not preferred for any Copy
// operations.
if (!Device->isIntegrated()) {
// Given non D2D Copies, for better performance, Copy Engines are preferred
// only if one has both the Main and Link Copy Engines.
if (Device->hasLinkCopyEngine() && Device->hasMainCopyEngine() &&
(!IsDevicePointer(Context, Src) || !IsDevicePointer(Context, Dst))) {
PreferCopyEngine = true;
}
}
// Temporary option added to use force engine for D2D copy
PreferCopyEngine |= UseCopyEngineForD2DCopy;
return PreferCopyEngine;
}
// Shared by all memory read/write/copy PI interfaces.
// PI interfaces must have queue's and destination buffer's mutexes locked for
// exclusive use and source buffer's mutex locked for shared use on entry.
ur_result_t enqueueMemCopyHelper(ur_command_t CommandType,
ur_queue_handle_t Queue, void *Dst,
ur_bool_t BlockingWrite, size_t Size,
const void *Src, uint32_t NumEventsInWaitList,
const ur_event_handle_t *EventWaitList,
ur_event_handle_t *OutEvent,
bool PreferCopyEngine) {
bool UseCopyEngine = Queue->useCopyEngine(PreferCopyEngine);
_ur_ze_event_list_t TmpWaitList;
UR_CALL(TmpWaitList.createAndRetainUrZeEventList(
NumEventsInWaitList, EventWaitList, Queue, UseCopyEngine));
// We want to batch these commands to avoid extra submissions (costly)
bool OkToBatch = true;
// Get a new command list to be used on this call
ur_command_list_ptr_t CommandList{};
UR_CALL(Queue->Context->getAvailableCommandList(
Queue, CommandList, UseCopyEngine, NumEventsInWaitList, EventWaitList,
OkToBatch, nullptr /*ForcedCmdQueue*/));
ze_event_handle_t ZeEvent = nullptr;
ur_event_handle_t InternalEvent;
bool IsInternal = OutEvent == nullptr;
ur_event_handle_t *Event = OutEvent ? OutEvent : &InternalEvent;
UR_CALL(createEventAndAssociateQueue(Queue, Event, CommandType, CommandList,
IsInternal, false));
UR_CALL(setSignalEvent(Queue, UseCopyEngine, &ZeEvent, Event,
NumEventsInWaitList, EventWaitList,
CommandList->second.ZeQueue));
(*Event)->WaitList = TmpWaitList;
const auto &ZeCommandList = CommandList->first;
const auto &WaitList = (*Event)->WaitList;
logger::debug("calling zeCommandListAppendMemoryCopy() with"
" ZeEvent {}",
ur_cast<std::uintptr_t>(ZeEvent));
printZeEventList(WaitList);
ZE2UR_CALL(zeCommandListAppendMemoryCopy,
(ZeCommandList, Dst, Src, Size, ZeEvent, WaitList.Length,
WaitList.ZeEventList));
UR_CALL(Queue->executeCommandList(CommandList, BlockingWrite, OkToBatch));
return UR_RESULT_SUCCESS;
}
// Shared by all memory read/write/copy rect PI interfaces.
// PI interfaces must have queue's and destination buffer's mutexes locked for
// exclusive use and source buffer's mutex locked for shared use on entry.
ur_result_t enqueueMemCopyRectHelper(
ur_command_t CommandType, ur_queue_handle_t Queue, const void *SrcBuffer,
void *DstBuffer, ur_rect_offset_t SrcOrigin, ur_rect_offset_t DstOrigin,
ur_rect_region_t Region, size_t SrcRowPitch, size_t DstRowPitch,
size_t SrcSlicePitch, size_t DstSlicePitch, ur_bool_t Blocking,
uint32_t NumEventsInWaitList, const ur_event_handle_t *EventWaitList,
ur_event_handle_t *OutEvent, bool PreferCopyEngine) {
bool UseCopyEngine = Queue->useCopyEngine(PreferCopyEngine);
_ur_ze_event_list_t TmpWaitList;
UR_CALL(TmpWaitList.createAndRetainUrZeEventList(
NumEventsInWaitList, EventWaitList, Queue, UseCopyEngine));
// We want to batch these commands to avoid extra submissions (costly)
bool OkToBatch = true;
// Get a new command list to be used on this call
ur_command_list_ptr_t CommandList{};
UR_CALL(Queue->Context->getAvailableCommandList(
Queue, CommandList, UseCopyEngine, NumEventsInWaitList, EventWaitList,
OkToBatch, nullptr /*ForcedCmdQueue*/));
ze_event_handle_t ZeEvent = nullptr;
ur_event_handle_t InternalEvent;
bool IsInternal = OutEvent == nullptr;
ur_event_handle_t *Event = OutEvent ? OutEvent : &InternalEvent;
UR_CALL(createEventAndAssociateQueue(Queue, Event, CommandType, CommandList,
IsInternal, false));
UR_CALL(setSignalEvent(Queue, UseCopyEngine, &ZeEvent, Event,
NumEventsInWaitList, EventWaitList,
CommandList->second.ZeQueue));
(*Event)->WaitList = TmpWaitList;
const auto &ZeCommandList = CommandList->first;
const auto &WaitList = (*Event)->WaitList;
logger::debug("calling zeCommandListAppendMemoryCopy() with"
" ZeEvent {}",
ur_cast<std::uintptr_t>(ZeEvent));
printZeEventList(WaitList);
auto ZeParams = ur2zeRegionParams(SrcOrigin, DstOrigin, Region, SrcRowPitch,
DstRowPitch, SrcSlicePitch, DstSlicePitch);
ZE2UR_CALL(zeCommandListAppendMemoryCopyRegion,
(ZeCommandList, DstBuffer, &ZeParams.dstRegion, ZeParams.dstPitch,
ZeParams.dstSlicePitch, SrcBuffer, &ZeParams.srcRegion,
ZeParams.srcPitch, ZeParams.srcSlicePitch, ZeEvent,
WaitList.Length, WaitList.ZeEventList));
logger::debug("calling zeCommandListAppendMemoryCopyRegion()");
UR_CALL(Queue->executeCommandList(CommandList, Blocking, OkToBatch));
return UR_RESULT_SUCCESS;
}
// PI interfaces must have queue's and buffer's mutexes locked on entry.
static ur_result_t enqueueMemFillHelper(ur_command_t CommandType,
ur_queue_handle_t Queue, void *Ptr,
const void *Pattern, size_t PatternSize,
size_t Size,
uint32_t NumEventsInWaitList,
const ur_event_handle_t *EventWaitList,
ur_event_handle_t *OutEvent) {
auto &Device = Queue->Device;
// Make sure that pattern size matches the capability of the copy queues.
// Check both main and link groups as we don't known which one will be used.
//
if (PreferCopyEngine && Device->hasCopyEngine()) {
if (Device->hasMainCopyEngine() &&
Device->QueueGroup[ur_device_handle_t_::queue_group_info_t::MainCopy]
.ZeProperties.maxMemoryFillPatternSize < PatternSize) {
PreferCopyEngine = false;
}
if (Device->hasLinkCopyEngine() &&
Device->QueueGroup[ur_device_handle_t_::queue_group_info_t::LinkCopy]
.ZeProperties.maxMemoryFillPatternSize < PatternSize) {
PreferCopyEngine = false;
}
}
bool UseCopyEngine = Queue->useCopyEngine(PreferCopyEngine);
if (!UseCopyEngine) {
// Pattern size must fit the compute queue capabilities.
UR_ASSERT(
PatternSize <=
Device->QueueGroup[ur_device_handle_t_::queue_group_info_t::Compute]
.ZeProperties.maxMemoryFillPatternSize,
UR_RESULT_ERROR_INVALID_VALUE);
}
_ur_ze_event_list_t TmpWaitList;
UR_CALL(TmpWaitList.createAndRetainUrZeEventList(
NumEventsInWaitList, EventWaitList, Queue, UseCopyEngine));
ur_command_list_ptr_t CommandList{};
// We want to batch these commands to avoid extra submissions (costly)
bool OkToBatch = true;
UR_CALL(Queue->Context->getAvailableCommandList(
Queue, CommandList, UseCopyEngine, NumEventsInWaitList, EventWaitList,
OkToBatch, nullptr /*ForcedCmdQueue*/));
ze_event_handle_t ZeEvent = nullptr;
ur_event_handle_t InternalEvent;
bool IsInternal = OutEvent == nullptr;
ur_event_handle_t *Event = OutEvent ? OutEvent : &InternalEvent;
UR_CALL(createEventAndAssociateQueue(Queue, Event, CommandType, CommandList,
IsInternal, false));
UR_CALL(setSignalEvent(Queue, UseCopyEngine, &ZeEvent, Event,
NumEventsInWaitList, EventWaitList,
CommandList->second.ZeQueue));
(*Event)->WaitList = TmpWaitList;
const auto &ZeCommandList = CommandList->first;
const auto &WaitList = (*Event)->WaitList;
// PatternSize must be a power of two for zeCommandListAppendMemoryFill.
// When it's not, the fill is emulated with zeCommandListAppendMemoryCopy.
if (isPowerOf2(PatternSize)) {
ZE2UR_CALL(zeCommandListAppendMemoryFill,
(ZeCommandList, Ptr, Pattern, PatternSize, Size, ZeEvent,
WaitList.Length, WaitList.ZeEventList));
logger::debug("calling zeCommandListAppendMemoryFill() with"
" ZeEvent {}",
ur_cast<uint64_t>(ZeEvent));
printZeEventList(WaitList);
// Execute command list asynchronously, as the event will be used
// to track down its completion.
UR_CALL(Queue->executeCommandList(CommandList, false /*IsBlocking*/,
OkToBatch));
} else {
// Copy pattern into every entry in memory array pointed by Ptr.
uint32_t NumOfCopySteps = Size / PatternSize;
const void *Src = Pattern;
for (uint32_t step = 0; step < NumOfCopySteps; ++step) {
void *Dst = reinterpret_cast<void *>(reinterpret_cast<uint8_t *>(Ptr) +
step * PatternSize);
ZE2UR_CALL(zeCommandListAppendMemoryCopy,
(ZeCommandList, Dst, Src, PatternSize, ZeEvent,
WaitList.Length, WaitList.ZeEventList));
}
logger::debug("calling zeCommandListAppendMemoryCopy() with"
" ZeEvent {}",
ur_cast<uint64_t>(ZeEvent));
printZeEventList(WaitList);
// Execute command list synchronously.
UR_CALL(
Queue->executeCommandList(CommandList, true /*IsBlocking*/, OkToBatch));
}
return UR_RESULT_SUCCESS;
}
// If indirect access tracking is enabled then performs reference counting,
// otherwise just calls zeMemAllocHost.
static ur_result_t ZeHostMemAllocHelper(void **ResultPtr,
ur_context_handle_t UrContext,
size_t Size) {
ur_platform_handle_t Plt = UrContext->getPlatform();
std::unique_lock<ur_shared_mutex> ContextsLock(Plt->ContextsMutex,
std::defer_lock);
if (IndirectAccessTrackingEnabled) {
// Lock the mutex which is guarding contexts container in the platform.
// This prevents new kernels from being submitted in any context while
// we are in the process of allocating a memory, this is needed to
// properly capture allocations by kernels with indirect access.
ContextsLock.lock();
// We are going to defer memory release if there are kernels with
// indirect access, that is why explicitly retain context to be sure
// that it is released after all memory allocations in this context are
// released.
UR_CALL(ur::level_zero::urContextRetain(UrContext));
}
ZeStruct<ze_host_mem_alloc_desc_t> ZeDesc;
ZeDesc.flags = 0;
ZE2UR_CALL(zeMemAllocHost,
(UrContext->ZeContext, &ZeDesc, Size, 1, ResultPtr));
if (IndirectAccessTrackingEnabled) {
// Keep track of all memory allocations in the context
UrContext->MemAllocs.emplace(
std::piecewise_construct, std::forward_as_tuple(*ResultPtr),
std::forward_as_tuple(
reinterpret_cast<ur_context_handle_t>(UrContext)));
}
return UR_RESULT_SUCCESS;
}
// Helper function to implement image read/write/copy.
// PI interfaces must have queue's and destination image's mutexes locked for
// exclusive use and source image's mutex locked for shared use on entry.
static ur_result_t enqueueMemImageCommandHelper(
ur_command_t CommandType, ur_queue_handle_t Queue,
const void *Src, // image or ptr
void *Dst, // image or ptr
ur_bool_t IsBlocking, ur_rect_offset_t *SrcOrigin,
ur_rect_offset_t *DstOrigin, ur_rect_region_t *Region, size_t RowPitch,
size_t SlicePitch, uint32_t NumEventsInWaitList,
const ur_event_handle_t *EventWaitList, ur_event_handle_t *OutEvent,
bool PreferCopyEngine = false) {
bool UseCopyEngine = Queue->useCopyEngine(PreferCopyEngine);
_ur_ze_event_list_t TmpWaitList;
UR_CALL(TmpWaitList.createAndRetainUrZeEventList(
NumEventsInWaitList, EventWaitList, Queue, UseCopyEngine));
// We want to batch these commands to avoid extra submissions (costly)
bool OkToBatch = true;
// Get a new command list to be used on this call
ur_command_list_ptr_t CommandList{};
UR_CALL(Queue->Context->getAvailableCommandList(
Queue, CommandList, UseCopyEngine, NumEventsInWaitList, EventWaitList,
OkToBatch, nullptr /*ForcedCmdQueue*/));
ze_event_handle_t ZeEvent = nullptr;
ur_event_handle_t InternalEvent;
bool IsInternal = OutEvent == nullptr;
ur_event_handle_t *Event = OutEvent ? OutEvent : &InternalEvent;
UR_CALL(createEventAndAssociateQueue(Queue, Event, CommandType, CommandList,
IsInternal, false));
UR_CALL(setSignalEvent(Queue, UseCopyEngine, &ZeEvent, Event,
NumEventsInWaitList, EventWaitList,
CommandList->second.ZeQueue));
(*Event)->WaitList = TmpWaitList;
const auto &ZeCommandList = CommandList->first;
const auto &WaitList = (*Event)->WaitList;
if (CommandType == UR_COMMAND_MEM_IMAGE_READ) {
_ur_image *SrcMem = ur_cast<_ur_image *>(const_cast<void *>(Src));
ze_image_region_t ZeSrcRegion;
UR_CALL(getImageRegionHelper(SrcMem->ZeImageDesc, SrcOrigin, Region,
ZeSrcRegion));
// TODO: Level Zero does not support row_pitch/slice_pitch for images yet.
// Check that SYCL RT did not want pitch larger than default.
std::ignore = RowPitch;
std::ignore = SlicePitch;
UR_ASSERT(SrcMem->isImage(), UR_RESULT_ERROR_INVALID_MEM_OBJECT);
#ifndef NDEBUG
auto SrcImage = SrcMem;
const ze_image_desc_t &ZeImageDesc = SrcImage->ZeImageDesc;
UR_ASSERT(
RowPitch == 0 ||
// special case RGBA image pitch equal to region's width
(ZeImageDesc.format.layout == ZE_IMAGE_FORMAT_LAYOUT_32_32_32_32 &&
RowPitch == 4 * 4 * ZeSrcRegion.width) ||
(ZeImageDesc.format.layout == ZE_IMAGE_FORMAT_LAYOUT_16_16_16_16 &&
RowPitch == 4 * 2 * ZeSrcRegion.width) ||
(ZeImageDesc.format.layout == ZE_IMAGE_FORMAT_LAYOUT_8_8_8_8 &&
RowPitch == 4 * ZeSrcRegion.width),
UR_RESULT_ERROR_INVALID_IMAGE_SIZE);
#endif
UR_ASSERT(SlicePitch == 0 || SlicePitch == RowPitch * ZeSrcRegion.height,
UR_RESULT_ERROR_INVALID_IMAGE_SIZE);
char *ZeHandleSrc = nullptr;
UR_CALL(SrcMem->getZeHandle(ZeHandleSrc, ur_mem_handle_t_::read_only,
Queue->Device, EventWaitList,
NumEventsInWaitList));
ZE2UR_CALL(zeCommandListAppendImageCopyToMemory,
(ZeCommandList, Dst, ur_cast<ze_image_handle_t>(ZeHandleSrc),
&ZeSrcRegion, ZeEvent, WaitList.Length, WaitList.ZeEventList));
} else if (CommandType == UR_COMMAND_MEM_IMAGE_WRITE) {
_ur_image *DstMem = ur_cast<_ur_image *>(Dst);
ze_image_region_t ZeDstRegion;
UR_CALL(getImageRegionHelper(DstMem->ZeImageDesc, DstOrigin, Region,
ZeDstRegion));
// TODO: Level Zero does not support row_pitch/slice_pitch for images yet.
// Check that SYCL RT did not want pitch larger than default.
UR_ASSERT(DstMem->isImage(), UR_RESULT_ERROR_INVALID_MEM_OBJECT);
#ifndef NDEBUG
auto DstImage = static_cast<_ur_image *>(DstMem);
const ze_image_desc_t &ZeImageDesc = DstImage->ZeImageDesc;
UR_ASSERT(
RowPitch == 0 ||
// special case RGBA image pitch equal to region's width
(ZeImageDesc.format.layout == ZE_IMAGE_FORMAT_LAYOUT_32_32_32_32 &&
RowPitch == 4 * 4 * ZeDstRegion.width) ||
(ZeImageDesc.format.layout == ZE_IMAGE_FORMAT_LAYOUT_16_16_16_16 &&
RowPitch == 4 * 2 * ZeDstRegion.width) ||
(ZeImageDesc.format.layout == ZE_IMAGE_FORMAT_LAYOUT_8_8_8_8 &&
RowPitch == 4 * ZeDstRegion.width),
UR_RESULT_ERROR_INVALID_IMAGE_SIZE);
#endif
UR_ASSERT(SlicePitch == 0 || SlicePitch == RowPitch * ZeDstRegion.height,
UR_RESULT_ERROR_INVALID_IMAGE_SIZE);
char *ZeHandleDst = nullptr;
UR_CALL(DstMem->getZeHandle(ZeHandleDst, ur_mem_handle_t_::write_only,
Queue->Device, EventWaitList,
NumEventsInWaitList));
ZE2UR_CALL(zeCommandListAppendImageCopyFromMemory,
(ZeCommandList, ur_cast<ze_image_handle_t>(ZeHandleDst), Src,
&ZeDstRegion, ZeEvent, WaitList.Length, WaitList.ZeEventList));
} else if (CommandType == UR_COMMAND_MEM_IMAGE_COPY) {
_ur_image *SrcImage = ur_cast<_ur_image *>(const_cast<void *>(Src));
_ur_image *DstImage = ur_cast<_ur_image *>(Dst);
ze_image_region_t ZeSrcRegion;
UR_CALL(getImageRegionHelper(SrcImage->ZeImageDesc, SrcOrigin, Region,
ZeSrcRegion));
ze_image_region_t ZeDstRegion;
UR_CALL(getImageRegionHelper(DstImage->ZeImageDesc, DstOrigin, Region,
ZeDstRegion));
char *ZeHandleSrc = nullptr;
char *ZeHandleDst = nullptr;
UR_CALL(SrcImage->getZeHandle(ZeHandleSrc, ur_mem_handle_t_::read_only,
Queue->Device, EventWaitList,
NumEventsInWaitList));
UR_CALL(DstImage->getZeHandle(ZeHandleDst, ur_mem_handle_t_::write_only,
Queue->Device, EventWaitList,
NumEventsInWaitList));
ZE2UR_CALL(zeCommandListAppendImageCopyRegion,
(ZeCommandList, ur_cast<ze_image_handle_t>(ZeHandleDst),
ur_cast<ze_image_handle_t>(ZeHandleSrc), &ZeDstRegion,
&ZeSrcRegion, ZeEvent, 0, nullptr));
} else {
logger::error("enqueueMemImageUpdate: unsupported image command type");
return UR_RESULT_ERROR_INVALID_OPERATION;
}
UR_CALL(Queue->executeCommandList(CommandList, IsBlocking, OkToBatch));
return UR_RESULT_SUCCESS;
}
namespace ur::level_zero {
ur_result_t urEnqueueMemBufferRead(
ur_queue_handle_t Queue, ///< [in] handle of the queue object
ur_mem_handle_t hBuffer, ///< [in] handle of the buffer object
bool blockingRead, ///< [in] indicates blocking (true), non-blocking (false)
size_t offset, ///< [in] offset in bytes in the buffer object
size_t size, ///< [in] size in bytes of data being read
void *pDst, ///< [in] pointer to host memory where data is to be read into
uint32_t numEventsInWaitList, ///< [in] size of the event wait list
const ur_event_handle_t
*phEventWaitList, ///< [in][optional][range(0, numEventsInWaitList)]
///< pointer to a list of events that must be complete
///< before this command can be executed. If nullptr,
///< the numEventsInWaitList must be 0, indicating
///< that this command does not wait on any event to
///< complete.
ur_event_handle_t
*phEvent ///< [in,out][optional] return an event object that identifies
///< this particular command instance.
) {
ur_mem_handle_t_ *Src = ur_cast<ur_mem_handle_t_ *>(hBuffer);
std::shared_lock<ur_shared_mutex> SrcLock(Src->Mutex, std::defer_lock);
std::scoped_lock<std::shared_lock<ur_shared_mutex>, ur_shared_mutex> LockAll(
SrcLock, Queue->Mutex);
char *ZeHandleSrc = nullptr;
UR_CALL(Src->getZeHandle(ZeHandleSrc, ur_mem_handle_t_::read_only,
Queue->Device, phEventWaitList,
numEventsInWaitList));
return enqueueMemCopyHelper(UR_COMMAND_MEM_BUFFER_READ, Queue, pDst,
blockingRead, size, ZeHandleSrc + offset,
numEventsInWaitList, phEventWaitList, phEvent,
true /* PreferCopyEngine */);
}
ur_result_t urEnqueueMemBufferWrite(
ur_queue_handle_t Queue, ///< [in] handle of the queue object
ur_mem_handle_t hBuffer, ///< [in] handle of the buffer object
bool
blockingWrite, ///< [in] indicates blocking (true), non-blocking (false)
size_t offset, ///< [in] offset in bytes in the buffer object
size_t size, ///< [in] size in bytes of data being written
const void
*pSrc, ///< [in] pointer to host memory where data is to be written from
uint32_t numEventsInWaitList, ///< [in] size of the event wait list
const ur_event_handle_t
*phEventWaitList, ///< [in][optional][range(0, numEventsInWaitList)]
///< pointer to a list of events that must be complete
///< before this command can be executed. If nullptr,
///< the numEventsInWaitList must be 0, indicating
///< that this command does not wait on any event to
///< complete.
ur_event_handle_t
*phEvent ///< [in,out][optional] return an event object that identifies
///< this particular command instance.
) {
ur_mem_handle_t_ *Buffer = ur_cast<ur_mem_handle_t_ *>(hBuffer);
std::scoped_lock<ur_shared_mutex, ur_shared_mutex> Lock(Queue->Mutex,
Buffer->Mutex);
char *ZeHandleDst = nullptr;
UR_CALL(Buffer->getZeHandle(ZeHandleDst, ur_mem_handle_t_::write_only,
Queue->Device, phEventWaitList,
numEventsInWaitList));
return enqueueMemCopyHelper(UR_COMMAND_MEM_BUFFER_WRITE, Queue,
ZeHandleDst + offset, // dst
blockingWrite, size,
pSrc, // src
numEventsInWaitList, phEventWaitList, phEvent,
true /* PreferCopyEngine */);
}
ur_result_t urEnqueueMemBufferReadRect(
ur_queue_handle_t Queue, ///< [in] handle of the queue object
ur_mem_handle_t hBuffer, ///< [in] handle of the buffer object
bool blockingRead, ///< [in] indicates blocking (true), non-blocking (false)
ur_rect_offset_t bufferOffset, ///< [in] 3D offset in the buffer
ur_rect_offset_t hostOffset, ///< [in] 3D offset in the host region
ur_rect_region_t
region, ///< [in] 3D rectangular region descriptor: width, height, depth
size_t bufferRowPitch, ///< [in] length of each row in bytes in the buffer
///< object
size_t bufferSlicePitch, ///< [in] length of each 2D slice in bytes in the
///< buffer object being read
size_t hostRowPitch, ///< [in] length of each row in bytes in the host
///< memory region pointed by dst
size_t hostSlicePitch, ///< [in] length of each 2D slice in bytes in the
///< host memory region pointed by dst
void *pDst, ///< [in] pointer to host memory where data is to be read into
uint32_t numEventsInWaitList, ///< [in] size of the event wait list
const ur_event_handle_t
*phEventWaitList, ///< [in][optional][range(0, numEventsInWaitList)]
///< pointer to a list of events that must be complete
///< before this command can be executed. If nullptr,
///< the numEventsInWaitList must be 0, indicating
///< that this command does not wait on any event to
///< complete.
ur_event_handle_t
*phEvent ///< [in,out][optional] return an event object that identifies
///< this particular command instance.
) {
ur_mem_handle_t_ *Buffer = ur_cast<ur_mem_handle_t_ *>(hBuffer);
std::shared_lock<ur_shared_mutex> SrcLock(Buffer->Mutex, std::defer_lock);
std::scoped_lock<std::shared_lock<ur_shared_mutex>, ur_shared_mutex> LockAll(
SrcLock, Queue->Mutex);
char *ZeHandleSrc;
UR_CALL(Buffer->getZeHandle(ZeHandleSrc, ur_mem_handle_t_::read_only,
Queue->Device, phEventWaitList,
numEventsInWaitList));
return enqueueMemCopyRectHelper(
UR_COMMAND_MEM_BUFFER_READ_RECT, Queue, ZeHandleSrc, pDst, bufferOffset,
hostOffset, region, bufferRowPitch, hostRowPitch, bufferSlicePitch,
hostSlicePitch, blockingRead, numEventsInWaitList, phEventWaitList,
phEvent);
}
ur_result_t urEnqueueMemBufferWriteRect(
ur_queue_handle_t Queue, ///< [in] handle of the queue object
ur_mem_handle_t hBuffer, ///< [in] handle of the buffer object
bool
blockingWrite, ///< [in] indicates blocking (true), non-blocking (false)
ur_rect_offset_t bufferOffset, ///< [in] 3D offset in the buffer
ur_rect_offset_t hostOffset, ///< [in] 3D offset in the host region
ur_rect_region_t
region, ///< [in] 3D rectangular region descriptor: width, height, depth
size_t bufferRowPitch, ///< [in] length of each row in bytes in the buffer
///< object
size_t bufferSlicePitch, ///< [in] length of each 2D slice in bytes in the
///< buffer object being written
size_t hostRowPitch, ///< [in] length of each row in bytes in the host
///< memory region pointed by src
size_t hostSlicePitch, ///< [in] length of each 2D slice in bytes in the
///< host memory region pointed by src
void
*pSrc, ///< [in] pointer to host memory where data is to be written from
uint32_t numEventsInWaitList, ///< [in] size of the event wait list
const ur_event_handle_t
*phEventWaitList, ///< [in][optional][range(0, numEventsInWaitList)]
///< points to a list of events that must be complete
///< before this command can be executed. If nullptr,
///< the numEventsInWaitList must be 0, indicating
///< that this command does not wait on any event to
///< complete.
ur_event_handle_t
*phEvent ///< [in,out][optional] return an event object that identifies
///< this particular command instance.
) {
ur_mem_handle_t_ *Buffer = ur_cast<ur_mem_handle_t_ *>(hBuffer);
std::scoped_lock<ur_shared_mutex, ur_shared_mutex> Lock(Queue->Mutex,
Buffer->Mutex);
char *ZeHandleDst = nullptr;
UR_CALL(Buffer->getZeHandle(ZeHandleDst, ur_mem_handle_t_::write_only,
Queue->Device, phEventWaitList,
numEventsInWaitList));
return enqueueMemCopyRectHelper(
UR_COMMAND_MEM_BUFFER_WRITE_RECT, Queue,
const_cast<char *>(static_cast<const char *>(pSrc)), ZeHandleDst,
hostOffset, bufferOffset, region, hostRowPitch, bufferRowPitch,
hostSlicePitch, bufferSlicePitch, blockingWrite, numEventsInWaitList,
phEventWaitList, phEvent);
}
ur_result_t urEnqueueMemBufferCopy(
ur_queue_handle_t Queue, ///< [in] handle of the queue object
ur_mem_handle_t BufferSrc, ///< [in] handle of the src buffer object
ur_mem_handle_t BufferDst, ///< [in] handle of the dest buffer object
size_t SrcOffset, ///< [in] offset into hBufferSrc to begin copying from
size_t DstOffset, ///< [in] offset info hBufferDst to begin copying into
size_t Size, ///< [in] size in bytes of data being copied
uint32_t NumEventsInWaitList, ///< [in] size of the event wait list
const ur_event_handle_t
*EventWaitList, ///< [in][optional][range(0, numEventsInWaitList)]
///< pointer to a list of events that must be complete
///< before this command can be executed. If nullptr,
///< the numEventsInWaitList must be 0, indicating
///< that this command does not wait on any event to
///< complete.
ur_event_handle_t
*OutEvent ///< [in,out][optional] return an event object that identifies
///< this particular command instance.
) {
_ur_buffer *SrcBuffer = ur_cast<_ur_buffer *>(BufferSrc);
_ur_buffer *DstBuffer = ur_cast<_ur_buffer *>(BufferDst);
UR_ASSERT(!SrcBuffer->isImage(), UR_RESULT_ERROR_INVALID_MEM_OBJECT);
UR_ASSERT(!DstBuffer->isImage(), UR_RESULT_ERROR_INVALID_MEM_OBJECT);
std::shared_lock<ur_shared_mutex> SrcLock(SrcBuffer->Mutex, std::defer_lock);
std::scoped_lock<std::shared_lock<ur_shared_mutex>, ur_shared_mutex,
ur_shared_mutex>
LockAll(SrcLock, DstBuffer->Mutex, Queue->Mutex);
// Copy engine is preferred only for host to device transfer.
// Device to device transfers run faster on compute engines.
bool PreferCopyEngine = (SrcBuffer->OnHost || DstBuffer->OnHost);
// Temporary option added to use copy engine for D2D copy
PreferCopyEngine |= UseCopyEngineForD2DCopy;
char *ZeHandleSrc = nullptr;
UR_CALL(SrcBuffer->getZeHandle(ZeHandleSrc, ur_mem_handle_t_::read_only,
Queue->Device, EventWaitList,
NumEventsInWaitList));
char *ZeHandleDst = nullptr;
UR_CALL(DstBuffer->getZeHandle(ZeHandleDst, ur_mem_handle_t_::write_only,
Queue->Device, EventWaitList,
NumEventsInWaitList));
return enqueueMemCopyHelper(
UR_COMMAND_MEM_BUFFER_COPY, Queue, ZeHandleDst + DstOffset,
false, // blocking
Size, ZeHandleSrc + SrcOffset, NumEventsInWaitList, EventWaitList,
OutEvent, PreferCopyEngine);
}
ur_result_t urEnqueueMemBufferCopyRect(
ur_queue_handle_t Queue, ///< [in] handle of the queue object
ur_mem_handle_t BufferSrc, ///< [in] handle of the source buffer object
ur_mem_handle_t BufferDst, ///< [in] handle of the dest buffer object
ur_rect_offset_t SrcOrigin, ///< [in] 3D offset in the source buffer
ur_rect_offset_t DstOrigin, ///< [in] 3D offset in the destination buffer
ur_rect_region_t SrcRegion, ///< [in] source 3D rectangular region
///< descriptor: width, height, depth
size_t SrcRowPitch, ///< [in] length of each row in bytes in the source
///< buffer object
size_t SrcSlicePitch, ///< [in] length of each 2D slice in bytes in the
///< source buffer object
size_t DstRowPitch, ///< [in] length of each row in bytes in the destination
///< buffer object
size_t DstSlicePitch, ///< [in] length of each 2D slice in bytes in the
///< destination buffer object
uint32_t NumEventsInWaitList, ///< [in] size of the event wait list
const ur_event_handle_t
*EventWaitList, ///< [in][optional][range(0, numEventsInWaitList)]
///< pointer to a list of events that must be complete
///< before this command can be executed. If nullptr,
///< the numEventsInWaitList must be 0, indicating
///< that this command does not wait on any event to
///< complete.
ur_event_handle_t
*OutEvent ///< [in,out][optional] return an event object that identifies
///< this particular command instance.
) {
_ur_buffer *SrcBuffer = ur_cast<_ur_buffer *>(BufferSrc);
_ur_buffer *DstBuffer = ur_cast<_ur_buffer *>(BufferDst);
UR_ASSERT(!SrcBuffer->isImage(), UR_RESULT_ERROR_INVALID_MEM_OBJECT);
UR_ASSERT(!DstBuffer->isImage(), UR_RESULT_ERROR_INVALID_MEM_OBJECT);
std::shared_lock<ur_shared_mutex> SrcLock(SrcBuffer->Mutex, std::defer_lock);
std::scoped_lock<std::shared_lock<ur_shared_mutex>, ur_shared_mutex,
ur_shared_mutex>
LockAll(SrcLock, DstBuffer->Mutex, Queue->Mutex);
// Copy engine is preferred only for host to device transfer.
// Device to device transfers run faster on compute engines.
bool PreferCopyEngine = (SrcBuffer->OnHost || DstBuffer->OnHost);
char *ZeHandleSrc = nullptr;
UR_CALL(SrcBuffer->getZeHandle(ZeHandleSrc, ur_mem_handle_t_::read_only,
Queue->Device, EventWaitList,
NumEventsInWaitList));
char *ZeHandleDst = nullptr;
UR_CALL(DstBuffer->getZeHandle(ZeHandleDst, ur_mem_handle_t_::write_only,
Queue->Device, EventWaitList,
NumEventsInWaitList));
return enqueueMemCopyRectHelper(
UR_COMMAND_MEM_BUFFER_COPY_RECT, Queue, ZeHandleSrc, ZeHandleDst,
SrcOrigin, DstOrigin, SrcRegion, SrcRowPitch, DstRowPitch, SrcSlicePitch,
DstSlicePitch,
false, // blocking
NumEventsInWaitList, EventWaitList, OutEvent, PreferCopyEngine);
}
ur_result_t urEnqueueMemBufferFill(
ur_queue_handle_t Queue, ///< [in] handle of the queue object
ur_mem_handle_t Buffer, ///< [in] handle of the buffer object
const void *Pattern, ///< [in] pointer to the fill pattern
size_t PatternSize, ///< [in] size in bytes of the pattern
size_t Offset, ///< [in] offset into the buffer
size_t Size, ///< [in] fill size in bytes, must be a multiple of patternSize
uint32_t NumEventsInWaitList, ///< [in] size of the event wait list
const ur_event_handle_t
*EventWaitList, ///< [in][optional][range(0, numEventsInWaitList)]
///< pointer to a list of events that must be complete
///< before this command can be executed. If nullptr,
///< the numEventsInWaitList must be 0, indicating
///< that this command does not wait on any event to
///< complete.
ur_event_handle_t
*OutEvent ///< [in,out][optional] return an event object that identifies
///< this particular command instance.
) {
std::scoped_lock<ur_shared_mutex, ur_shared_mutex> Lock(Queue->Mutex,
Buffer->Mutex);
char *ZeHandleDst = nullptr;
_ur_buffer *UrBuffer = reinterpret_cast<_ur_buffer *>(Buffer);
UR_CALL(UrBuffer->getZeHandle(ZeHandleDst, ur_mem_handle_t_::write_only,
Queue->Device, EventWaitList,
NumEventsInWaitList));
return enqueueMemFillHelper(
UR_COMMAND_MEM_BUFFER_FILL, Queue, ZeHandleDst + Offset,
Pattern, // It will be interpreted as an 8-bit value,
PatternSize, // which is indicated with this pattern_size==1
Size, NumEventsInWaitList, EventWaitList, OutEvent);
}
ur_result_t urEnqueueMemImageRead(
ur_queue_handle_t Queue, ///< [in] handle of the queue object
ur_mem_handle_t Image, ///< [in] handle of the image object
bool BlockingRead, ///< [in] indicates blocking (true), non-blocking (false)
ur_rect_offset_t Origin, ///< [in] defines the (x,y,z) offset in pixels in
///< the 1D, 2D, or 3D image
ur_rect_region_t Region, ///< [in] defines the (width, height, depth) in
///< pixels of the 1D, 2D, or 3D image
size_t RowPitch, ///< [in] length of each row in bytes
size_t SlicePitch, ///< [in] length of each 2D slice of the 3D image
void *Dst, ///< [in] pointer to host memory where image is to be read into
uint32_t NumEventsInWaitList, ///< [in] size of the event wait list
const ur_event_handle_t
*EventWaitList, ///< [in][optional][range(0, numEventsInWaitList)]
///< pointer to a list of events that must be complete
///< before this command can be executed. If nullptr,
///< the numEventsInWaitList must be 0, indicating
///< that this command does not wait on any event to
///< complete.
ur_event_handle_t
*OutEvent ///< [in,out][optional] return an event object that identifies
///< this particular command instance.
) {
std::scoped_lock<ur_shared_mutex, ur_shared_mutex> Lock(Queue->Mutex,
Image->Mutex);
return enqueueMemImageCommandHelper(
UR_COMMAND_MEM_IMAGE_READ, Queue, Image, Dst, BlockingRead, &Origin,
nullptr, &Region, RowPitch, SlicePitch, NumEventsInWaitList,
EventWaitList, OutEvent);
}
ur_result_t urEnqueueMemImageWrite(
ur_queue_handle_t Queue, ///< [in] handle of the queue object
ur_mem_handle_t Image, ///< [in] handle of the image object
bool
BlockingWrite, ///< [in] indicates blocking (true), non-blocking (false)
ur_rect_offset_t Origin, ///< [in] defines the (x,y,z) offset in pixels in
///< the 1D, 2D, or 3D image
ur_rect_region_t Region, ///< [in] defines the (width, height, depth) in
///< pixels of the 1D, 2D, or 3D image
size_t RowPitch, ///< [in] length of each row in bytes
size_t SlicePitch, ///< [in] length of each 2D slice of the 3D image
void *Src, ///< [in] pointer to host memory where image is to be read into
uint32_t NumEventsInWaitList, ///< [in] size of the event wait list
const ur_event_handle_t
*EventWaitList, ///< [in][optional][range(0, numEventsInWaitList)]
///< pointer to a list of events that must be complete
///< before this command can be executed. If nullptr,
///< the numEventsInWaitList must be 0, indicating
///< that this command does not wait on any event to
///< complete.
ur_event_handle_t
*OutEvent ///< [in,out][optional] return an event object that identifies
///< this particular command instance.
) {
std::scoped_lock<ur_shared_mutex, ur_shared_mutex> Lock(Queue->Mutex,
Image->Mutex);
return enqueueMemImageCommandHelper(
UR_COMMAND_MEM_IMAGE_WRITE, Queue, Src, Image, BlockingWrite, nullptr,
&Origin, &Region, RowPitch, SlicePitch, NumEventsInWaitList,
EventWaitList, OutEvent);
}
ur_result_t urEnqueueMemImageCopy(
ur_queue_handle_t Queue, ///< [in] handle of the queue object
ur_mem_handle_t ImageSrc, ///< [in] handle of the src image object
ur_mem_handle_t ImageDst, ///< [in] handle of the dest image object
ur_rect_offset_t SrcOrigin, ///< [in] defines the (x,y,z) offset in pixels
///< in the source 1D, 2D, or 3D image
ur_rect_offset_t DstOrigin, ///< [in] defines the (x,y,z) offset in pixels
///< in the destination 1D, 2D, or 3D image
ur_rect_region_t Region, ///< [in] defines the (width, height, depth) in
///< pixels of the 1D, 2D, or 3D image
uint32_t NumEventsInWaitList, ///< [in] size of the event wait list
const ur_event_handle_t
*EventWaitList, ///< [in][optional][range(0, numEventsInWaitList)]
///< pointer to a list of events that must be complete
///< before this command can be executed. If nullptr,
///< the numEventsInWaitList must be 0, indicating
///< that this command does not wait on any event to
///< complete.
ur_event_handle_t
*OutEvent ///< [in,out][optional] return an event object that identifies
///< this particular command instance.
) {
std::shared_lock<ur_shared_mutex> SrcLock(ImageSrc->Mutex, std::defer_lock);
std::scoped_lock<std::shared_lock<ur_shared_mutex>, ur_shared_mutex,
ur_shared_mutex>
LockAll(SrcLock, ImageDst->Mutex, Queue->Mutex);
// Copy engine is preferred only for host to device transfer.
// Device to device transfers run faster on compute engines.
// Images are always allocated on device.
bool PreferCopyEngine = false;
return enqueueMemImageCommandHelper(
UR_COMMAND_MEM_IMAGE_COPY, Queue, ImageSrc, ImageDst,
false, // is_blocking
&SrcOrigin, &DstOrigin, &Region,
0, // row pitch
0, // slice pitch
NumEventsInWaitList, EventWaitList, OutEvent, PreferCopyEngine);
}
ur_result_t urEnqueueMemBufferMap(
ur_queue_handle_t Queue, ///< [in] handle of the queue object
ur_mem_handle_t Buf, ///< [in] handle of the buffer object
bool BlockingMap, ///< [in] indicates blocking (true), non-blocking (false)
ur_map_flags_t MapFlags, ///< [in] flags for read, write, readwrite mapping
size_t Offset, ///< [in] offset in bytes of the buffer region being mapped
size_t Size, ///< [in] size in bytes of the buffer region being mapped
uint32_t NumEventsInWaitList, ///< [in] size of the event wait list
const ur_event_handle_t
*EventWaitList, ///< [in][optional][range(0, numEventsInWaitList)]
///< pointer to a list of events that must be complete
///< before this command can be executed. If nullptr,
///< the numEventsInWaitList must be 0, indicating
///< that this command does not wait on any event to
///< complete.
ur_event_handle_t
*OutEvent, ///< [in,out][optional] return an event object that
///< identifies this particular command instance.
void **RetMap ///< [in,out] return mapped pointer. TODO: move it before
///< numEventsInWaitList?
) {
auto Buffer = ur_cast<_ur_buffer *>(Buf);
UR_ASSERT(!Buffer->isImage(), UR_RESULT_ERROR_INVALID_MEM_OBJECT);
ur_event_handle_t InternalEvent;
bool IsInternal = OutEvent == nullptr;
ur_event_handle_t *Event = OutEvent ? OutEvent : &InternalEvent;
ze_event_handle_t ZeEvent = nullptr;
bool UseCopyEngine = false;
{
// Lock automatically releases when this goes out of scope.
std::scoped_lock<ur_shared_mutex> lock(Queue->Mutex);
_ur_ze_event_list_t TmpWaitList;
UR_CALL(TmpWaitList.createAndRetainUrZeEventList(
NumEventsInWaitList, EventWaitList, Queue, UseCopyEngine));
UR_CALL(createEventAndAssociateQueue(
Queue, Event, UR_COMMAND_MEM_BUFFER_MAP, Queue->CommandListMap.end(),
IsInternal, false));
ZeEvent = (*Event)->ZeEvent;
(*Event)->WaitList = TmpWaitList;
}
// Translate the host access mode info.
ur_mem_handle_t_::access_mode_t AccessMode = ur_mem_handle_t_::unknown;
if (MapFlags & UR_MAP_FLAG_WRITE_INVALIDATE_REGION)
AccessMode = ur_mem_handle_t_::write_only;
else {
if (MapFlags & UR_MAP_FLAG_READ) {
AccessMode = ur_mem_handle_t_::read_only;
if (MapFlags & UR_MAP_FLAG_WRITE)
AccessMode = ur_mem_handle_t_::read_write;
} else if (MapFlags & UR_MAP_FLAG_WRITE)
AccessMode = ur_mem_handle_t_::write_only;
}
UR_ASSERT(AccessMode != ur_mem_handle_t_::unknown,
UR_RESULT_ERROR_INVALID_VALUE);
// TODO: Level Zero is missing the memory "mapping" capabilities, so we are
// left to doing new memory allocation and a copy (read) on discrete devices.
// For integrated devices, we have allocated the buffer in host memory so no
// actions are needed here except for synchronizing on incoming events.
// A host-to-host copy is done if a host pointer had been supplied during
// buffer creation on integrated devices.
//
// TODO: for discrete, check if the input buffer is already allocated
// in shared memory and thus is accessible from the host as is.
// Can we get SYCL RT to predict/allocate in shared memory
// from the beginning?
// For integrated devices the buffer has been allocated in host memory.
if (Buffer->OnHost) {
// Wait on incoming events before doing the copy
if (NumEventsInWaitList > 0)
UR_CALL(ur::level_zero::urEventWait(NumEventsInWaitList, EventWaitList));
if (Queue->isInOrderQueue())
UR_CALL(ur::level_zero::urQueueFinish(Queue));
// Lock automatically releases when this goes out of scope.
std::scoped_lock<ur_shared_mutex> Guard(Buffer->Mutex);
char *ZeHandleSrc;
UR_CALL(Buffer->getZeHandle(ZeHandleSrc, AccessMode, Queue->Device,
EventWaitList, NumEventsInWaitList));
if (Buffer->MapHostPtr) {
*RetMap = Buffer->MapHostPtr + Offset;
if (ZeHandleSrc != Buffer->MapHostPtr &&
AccessMode != ur_mem_handle_t_::write_only) {
memcpy(*RetMap, ZeHandleSrc + Offset, Size);
}
} else {
*RetMap = ZeHandleSrc + Offset;
}
auto Res = Buffer->Mappings.insert({*RetMap, {Offset, Size}});
// False as the second value in pair means that mapping was not inserted
// because mapping already exists.
if (!Res.second) {