-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Copy pathvulkan.cpp
1486 lines (1262 loc) · 64.2 KB
/
vulkan.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
#include "HalideRuntimeVulkan.h"
#include "device_buffer_utils.h"
#include "device_interface.h"
#include "runtime_internal.h"
#include "vulkan_context.h"
#include "vulkan_extensions.h"
#include "vulkan_internal.h"
#include "vulkan_memory.h"
#include "vulkan_resources.h"
using namespace Halide::Runtime::Internal::Vulkan;
// --------------------------------------------------------------------------
extern "C" {
// --------------------------------------------------------------------------
// The default implementation of halide_acquire_vulkan_context uses
// the global pointers above, and serializes access with a spin lock.
// Overriding implementations of acquire/release must implement the
// following behavior:
// - halide_acquire_vulkan_context should always store a valid
// instance/device/queue in the corresponding out parameters,
// or return an error code.
// - A call to halide_acquire_vulkan_context is followed by a matching
// call to halide_release_vulkan_context. halide_acquire_vulkan_context
// should block while a previous call (if any) has not yet been
// released via halide_release_vulkan_context.
WEAK int halide_vulkan_acquire_context(void *user_context,
halide_vulkan_memory_allocator **allocator,
VkInstance *instance,
VkDevice *device,
VkPhysicalDevice *physical_device,
VkQueue *queue,
uint32_t *queue_family_index,
VkDebugUtilsMessengerEXT *messenger,
bool create) {
#ifdef DEBUG_RUNTIME
halide_start_clock(user_context);
#endif
halide_debug_assert(user_context, instance != nullptr);
halide_debug_assert(user_context, device != nullptr);
halide_debug_assert(user_context, queue != nullptr);
halide_debug_assert(user_context, &thread_lock != nullptr);
halide_mutex_lock(&thread_lock);
// If the context has not been initialized, initialize it now.
if ((cached_instance == nullptr) && create) {
int error_code = vk_create_context(user_context,
reinterpret_cast<VulkanMemoryAllocator **>(&cached_allocator),
&cached_instance,
&cached_device,
&cached_physical_device,
&cached_queue,
&cached_queue_family_index,
&cached_messenger);
if (error_code != halide_error_code_success) {
debug(user_context) << "halide_vulkan_acquire_context: FAILED to create context!\n";
halide_mutex_unlock(&thread_lock);
return error_code;
}
}
*allocator = cached_allocator;
*instance = cached_instance;
*device = cached_device;
*physical_device = cached_physical_device;
*queue = cached_queue;
*queue_family_index = cached_queue_family_index;
*messenger = cached_messenger;
return halide_error_code_success;
}
WEAK int halide_vulkan_release_context(void *user_context, VkInstance instance, VkDevice device, VkQueue queue, VkDebugUtilsMessengerEXT messenger) {
halide_mutex_unlock(&thread_lock);
return halide_error_code_success;
}
WEAK bool halide_vulkan_is_initialized() {
halide_mutex_lock(&thread_lock);
bool is_initialized = (cached_instance != nullptr) && (cached_device != nullptr);
halide_mutex_unlock(&thread_lock);
return is_initialized;
}
WEAK int halide_vulkan_device_free(void *user_context, halide_buffer_t *halide_buffer) {
debug(user_context)
<< "halide_vulkan_device_free (user_context: " << user_context
<< ", halide_buffer: " << halide_buffer << ")\n";
// halide_vulkan_device_free, at present, can be exposed to clients and they
// should be allowed to call halide_vulkan_device_free on any halide_buffer_t
// including ones that have never been used with a GPU.
if (halide_buffer->device == 0) {
return halide_error_code_success;
}
VulkanContext ctx(user_context);
if (ctx.error != halide_error_code_success) {
error(user_context) << "Vulkan: Failed to acquire context!\n";
return ctx.error;
}
#ifdef DEBUG_RUNTIME
uint64_t t_before = halide_current_time_ns(user_context);
#endif
// get the allocated region for the device
MemoryRegion *device_region = reinterpret_cast<MemoryRegion *>(halide_buffer->device);
MemoryRegion *memory_region = ctx.allocator->owner_of(user_context, device_region);
if (ctx.allocator && memory_region && memory_region->handle) {
if (halide_can_reuse_device_allocations(user_context)) {
ctx.allocator->release(user_context, memory_region);
} else {
ctx.allocator->reclaim(user_context, memory_region);
}
}
halide_buffer->device = 0;
halide_buffer->device_interface->impl->release_module();
halide_buffer->device_interface = nullptr;
#ifdef DEBUG_RUNTIME
debug(user_context) << "Vulkan: Released memory for device region ("
<< "user_context: " << user_context << ", "
<< "buffer: " << halide_buffer << ", "
<< "size_in_bytes: " << (uint64_t)device_region->size << ")\n";
uint64_t t_after = halide_current_time_ns(user_context);
debug(user_context) << " Time: " << (t_after - t_before) / 1.0e6 << " ms\n";
#endif
return halide_error_code_success;
}
WEAK int halide_vulkan_compute_capability(void *user_context, int *major, int *minor) {
debug(user_context) << " halide_vulkan_compute_capability (user_context: " << user_context << ")\n";
return vk_find_compute_capability(user_context, major, minor);
}
WEAK int halide_vulkan_initialize_kernels(void *user_context, void **state_ptr, const char *src, int size) {
debug(user_context)
<< "halide_vulkan_init_kernels (user_context: " << user_context
<< ", state_ptr: " << state_ptr
<< ", program: " << (void *)src
<< ", size: " << size << "\n";
VulkanContext ctx(user_context);
if (ctx.error != halide_error_code_success) {
error(user_context) << "Vulkan: Failed to acquire context!\n";
return ctx.error;
}
#ifdef DEBUG_RUNTIME
uint64_t t_before = halide_current_time_ns(user_context);
#endif
debug(user_context) << "halide_vulkan_initialize_kernels got compilation_cache mutex.\n";
VulkanCompilationCacheEntry *cache_entry = nullptr;
if (!compilation_cache.kernel_state_setup(user_context, state_ptr, ctx.device, cache_entry,
Halide::Runtime::Internal::Vulkan::vk_compile_kernel_module,
user_context, ctx.allocator, src, size)) {
error(user_context) << "Vulkan: Failed to setup compilation cache!\n";
return halide_error_code_generic_error;
}
#ifdef DEBUG_RUNTIME
uint64_t t_after = halide_current_time_ns(user_context);
debug(user_context) << " Time: " << (t_after - t_before) / 1.0e6 << " ms\n";
#endif
return halide_error_code_success;
}
WEAK void halide_vulkan_finalize_kernels(void *user_context, void *state_ptr) {
debug(user_context)
<< "halide_vulkan_finalize_kernels (user_context: " << user_context
<< ", state_ptr: " << state_ptr << "\n";
#ifdef DEBUG_RUNTIME
uint64_t t_before = halide_current_time_ns(user_context);
#endif
VulkanContext ctx(user_context);
if (ctx.error == halide_error_code_success) {
compilation_cache.release_hold(user_context, ctx.device, state_ptr);
}
#ifdef DEBUG_RUNTIME
uint64_t t_after = halide_current_time_ns(user_context);
debug(user_context) << " Time: " << (t_after - t_before) / 1.0e6 << " ms\n";
#endif
}
// Used to generate correct timings when tracing
WEAK int halide_vulkan_device_sync(void *user_context, halide_buffer_t *) {
debug(user_context) << "halide_vulkan_device_sync (user_context: " << user_context << ")\n";
VulkanContext ctx(user_context);
if (ctx.error != halide_error_code_success) {
error(user_context) << "Vulkan: Failed to acquire context!\n";
return ctx.error;
}
#ifdef DEBUG_RUNTIME
uint64_t t_before = halide_current_time_ns(user_context);
#endif
VkResult result = vkQueueWaitIdle(ctx.queue);
if (result != VK_SUCCESS) {
error(user_context) << "Vulkan: vkQueueWaitIdle returned " << vk_get_error_name(result) << "\n";
return halide_error_code_generic_error;
}
#ifdef DEBUG_RUNTIME
uint64_t t_after = halide_current_time_ns(user_context);
debug(user_context) << " Time: " << (t_after - t_before) / 1.0e6 << " ms\n";
#endif
return halide_error_code_success;
}
WEAK int halide_vulkan_device_release(void *user_context) {
debug(user_context)
<< "halide_vulkan_device_release (user_context: " << user_context << ")\n";
VulkanMemoryAllocator *allocator = nullptr;
VkInstance instance = VK_NULL_HANDLE;
VkDevice device = VK_NULL_HANDLE;
VkPhysicalDevice physical_device = VK_NULL_HANDLE;
VkQueue queue = VK_NULL_HANDLE;
uint32_t queue_family_index = 0;
VkDebugUtilsMessengerEXT messenger = VK_NULL_HANDLE;
int destroy_status = halide_error_code_success;
int acquire_status = halide_vulkan_acquire_context(user_context,
reinterpret_cast<halide_vulkan_memory_allocator **>(&allocator),
&instance, &device, &physical_device, &queue, &queue_family_index, &messenger, false);
if (acquire_status == halide_error_code_success) {
// Destroy the context if we created it
if ((instance == cached_instance) && (device == cached_device)) {
destroy_status = vk_destroy_context(user_context, allocator, instance, device, physical_device, queue, messenger);
cached_allocator = nullptr;
cached_device = VK_NULL_HANDLE;
cached_physical_device = VK_NULL_HANDLE;
cached_queue = VK_NULL_HANDLE;
cached_queue_family_index = 0;
cached_instance = VK_NULL_HANDLE;
cached_messenger = VK_NULL_HANDLE;
}
halide_vulkan_release_context(user_context, instance, device, queue, messenger);
}
return destroy_status;
}
WEAK int halide_vulkan_device_malloc(void *user_context, halide_buffer_t *buf) {
debug(user_context)
<< "halide_vulkan_device_malloc (user_context: " << user_context
<< ", buf: " << buf << ")\n";
VulkanContext ctx(user_context);
if (ctx.error != halide_error_code_success) {
error(user_context) << "Vulkan: Failed to acquire context!\n";
return ctx.error;
}
size_t size = buf->size_in_bytes();
if (buf->device) {
MemoryRegion *device_region = (MemoryRegion *)(buf->device);
if (device_region->size >= size) {
debug(user_context) << "Vulkan: Requested allocation for existing device memory ... using existing buffer!\n";
return halide_error_code_success;
} else {
debug(user_context) << "Vulkan: Requested allocation of different size ... reallocating buffer!\n";
if (halide_can_reuse_device_allocations(user_context)) {
ctx.allocator->release(user_context, device_region);
} else {
ctx.allocator->reclaim(user_context, device_region);
}
buf->device = 0;
}
}
for (int i = 0; i < buf->dimensions; i++) {
halide_debug_assert(user_context, buf->dim[i].stride >= 0);
}
#ifdef DEBUG_RUNTIME
debug(user_context) << " allocating buffer: ";
if (buf && buf->dim) {
debug(user_context) << "extents: ";
for (int i = 0; i < buf->dimensions; i++) {
debug(user_context) << buf->dim[i].extent << " ";
}
debug(user_context) << "strides: ";
for (int i = 0; i < buf->dimensions; i++) {
debug(user_context) << buf->dim[i].stride << " ";
}
}
debug(user_context) << "type: " << buf->type << " "
<< "size_in_bytes: " << (uint64_t)size << " "
<< "(or " << (size * 1e-6f) << "MB)\n";
uint64_t t_before = halide_current_time_ns(user_context);
#endif
// request uncached device only memory
MemoryRequest request = {0};
request.size = size;
request.properties.usage = MemoryUsage::TransferSrcDst;
request.properties.caching = MemoryCaching::Uncached;
request.properties.visibility = MemoryVisibility::DeviceOnly;
// allocate a new region
MemoryRegion *device_region = ctx.allocator->reserve(user_context, request);
if ((device_region == nullptr) || (device_region->handle == nullptr)) {
error(user_context) << "Vulkan: Failed to allocate device memory!\n";
return halide_error_code_device_malloc_failed;
}
buf->device = (uint64_t)device_region;
buf->device_interface = &vulkan_device_interface;
buf->device_interface->impl->use_module();
#ifdef DEBUG_RUNTIME
debug(user_context)
<< " allocated device region=" << (void *)device_region << "\n"
<< " containing device buffer=" << (void *)device_region->handle << "\n"
<< " for halide buffer " << buf << "\n";
#endif
// retrieve the buffer from the region
VkBuffer *device_buffer = reinterpret_cast<VkBuffer *>(device_region->handle);
if (device_buffer == nullptr) {
error(user_context) << "Vulkan: Failed to retrieve device buffer for device memory!\n";
return halide_error_code_internal_error;
}
ScopedVulkanCommandBufferAndPool cmds(user_context, ctx.allocator, ctx.queue_family_index);
if (cmds.error_code != halide_error_code_success) {
error(user_context) << "Vulkan: Failed to create command buffer and pool for context!\n";
return cmds.error_code;
}
int error_code = vk_clear_device_buffer(user_context, ctx.allocator, cmds.command_buffer, ctx.queue, *device_buffer);
if (error_code != halide_error_code_success) {
error(user_context) << "Vulkan: Failed to clear device buffer!\n";
}
#ifdef DEBUG_RUNTIME
debug(user_context) << "Vulkan: Reserved memory for device region ("
<< "user_context: " << user_context << ", "
<< "buffer: " << buf << ", "
<< "size_in_bytes: " << (uint64_t)size << ")\n";
uint64_t t_after = halide_current_time_ns(user_context);
debug(user_context) << " Time: " << (t_after - t_before) / 1.0e6 << " ms\n";
#endif
return error_code;
}
WEAK int halide_vulkan_copy_to_device(void *user_context, halide_buffer_t *halide_buffer) {
int error_code = halide_vulkan_device_malloc(user_context, halide_buffer);
if (error_code != halide_error_code_success) {
error(user_context) << "Vulkan: Failed to allocate device memory!\n";
return error_code;
}
debug(user_context)
<< "halide_vulkan_copy_to_device (user_context: " << user_context
<< ", halide_buffer: " << halide_buffer << ")\n";
// Acquire the context so we can use the command queue.
VulkanContext ctx(user_context);
if (ctx.error != halide_error_code_success) {
error(user_context) << "Vulkan: Failed to acquire context!\n";
return ctx.error;
}
#ifdef DEBUG_RUNTIME
uint64_t t_before = halide_current_time_ns(user_context);
#endif
if ((halide_buffer->host == nullptr) || (halide_buffer->device == 0)) {
error(user_context) << "Vulkan: Missing host/device pointers for halide buffer!\n";
return halide_error_code_internal_error;
}
device_copy copy_helper = make_host_to_device_copy(halide_buffer);
// We construct a staging buffer to copy into from host memory. Then,
// we use vkCmdCopyBuffer() to copy from the staging buffer into the
// the actual device memory.
MemoryRequest request = {0};
request.size = halide_buffer->size_in_bytes();
request.properties.usage = MemoryUsage::TransferSrc;
request.properties.caching = MemoryCaching::UncachedCoherent;
request.properties.visibility = MemoryVisibility::HostToDevice;
// allocate a new region
MemoryRegion *staging_region = ctx.allocator->reserve(user_context, request);
if ((staging_region == nullptr) || (staging_region->handle == nullptr)) {
error(user_context) << "Vulkan: Failed to allocate device memory!\n";
return halide_error_code_device_malloc_failed;
}
// map the region to a host ptr
uint8_t *stage_host_ptr = (uint8_t *)ctx.allocator->map(user_context, staging_region);
if (stage_host_ptr == nullptr) {
error(user_context) << "Vulkan: Failed to map host pointer to device memory!\n";
return halide_error_code_internal_error;
}
// copy to the (host-visible/coherent) staging buffer
copy_helper.dst = (uint64_t)(stage_host_ptr);
copy_memory(copy_helper, user_context);
// retrieve the buffer from the region
VkBuffer *staging_buffer = reinterpret_cast<VkBuffer *>(staging_region->handle);
if (staging_buffer == nullptr) {
error(user_context) << "Vulkan: Failed to retrieve staging buffer for device memory!\n";
return halide_error_code_internal_error;
}
// unmap the pointer
error_code = ctx.allocator->unmap(user_context, staging_region);
if (error_code != halide_error_code_success) {
error(user_context) << "Vulkan: Failed to unmap host pointer to device memory!\n";
return error_code;
}
// get the allocated region for the device
MemoryRegion *device_region = reinterpret_cast<MemoryRegion *>(halide_buffer->device);
if (device_region == nullptr) {
error(user_context) << "Vulkan: Failed to retrieve device region for buffer!\n";
return halide_error_code_internal_error;
}
MemoryRegion *memory_region = ctx.allocator->owner_of(user_context, device_region);
if (memory_region == nullptr) {
error(user_context) << "Vulkan: Failed to retrieve memory region for device!\n";
return halide_error_code_internal_error;
}
// retrieve the buffer from the region
VkBuffer *device_buffer = reinterpret_cast<VkBuffer *>(memory_region->handle);
if (device_buffer == nullptr) {
error(user_context) << "Vulkan: Failed to retrieve buffer for device memory!\n";
return halide_error_code_internal_error;
}
#ifdef DEBUG_RUNTIME
debug(user_context)
<< " copying into device region=" << (void *)device_region << "\n"
<< " containing device buffer=" << (void *)device_buffer << "\n"
<< " from halide buffer=" << halide_buffer << "\n";
#endif
ScopedVulkanCommandBufferAndPool cmds(user_context, ctx.allocator, ctx.queue_family_index);
if (cmds.error_code != halide_error_code_success) {
error(user_context) << "Vulkan: Failed to create command buffer and pool!\n";
return cmds.error_code;
}
// begin the command buffer
VkCommandBufferBeginInfo command_buffer_begin_info =
{
VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO, // struct type
nullptr, // pointer to struct extending this
VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT, // flags
nullptr // pointer to parent command buffer
};
VkResult result = vkBeginCommandBuffer(cmds.command_buffer, &command_buffer_begin_info);
if (result != VK_SUCCESS) {
error(user_context) << "Vulkan: vkBeginCommandBuffer returned " << vk_get_error_name(result) << "\n";
return halide_error_code_device_buffer_copy_failed;
}
// define the src and dst config
bool from_host = true;
bool to_host = false;
copy_helper.src = (uint64_t)(staging_buffer);
copy_helper.dst = (uint64_t)(device_buffer);
uint64_t src_offset = copy_helper.src_begin;
uint64_t dst_offset = copy_helper.dst_begin + device_region->range.head_offset;
// enqueue the copy operation, using the allocated buffers
error_code = vk_do_multidimensional_copy(user_context, cmds.command_buffer, copy_helper,
src_offset, dst_offset,
halide_buffer->dimensions,
from_host, to_host);
if (error_code != halide_error_code_success) {
error(user_context) << "Vulkan: vk_do_multidimensional_copy failed!\n";
return error_code;
}
// end the command buffer
result = vkEndCommandBuffer(cmds.command_buffer);
if (result != VK_SUCCESS) {
error(user_context) << "Vulkan: vkEndCommandBuffer returned " << vk_get_error_name(result) << "\n";
return halide_error_code_device_buffer_copy_failed;
}
//// 13. Submit the command buffer to our command queue
VkSubmitInfo submit_info =
{
VK_STRUCTURE_TYPE_SUBMIT_INFO, // struct type
nullptr, // pointer to struct extending this
0, // wait semaphore count
nullptr, // semaphores
nullptr, // pipeline stages where semaphore waits occur
1, // how many command buffers to execute
&(cmds.command_buffer), // the command buffers
0, // number of semaphores to signal
nullptr // the semaphores to signal
};
result = vkQueueSubmit(ctx.queue, 1, &submit_info, VK_NULL_HANDLE);
if (result != VK_SUCCESS) {
error(user_context) << "Vulkan: vkQueueSubmit returned " << vk_get_error_name(result) << "\n";
return halide_error_code_device_buffer_copy_failed;
}
//// 14. Wait until the queue is done with the command buffer
result = vkQueueWaitIdle(ctx.queue);
if (result != VK_SUCCESS) {
error(user_context) << "Vulkan: vkQueueWaitIdle returned " << vk_get_error_name(result) << "\n";
return halide_error_code_device_buffer_copy_failed;
}
//// 15. Reclaim the staging buffer
if (halide_can_reuse_device_allocations(user_context)) {
ctx.allocator->release(user_context, staging_region);
} else {
ctx.allocator->reclaim(user_context, staging_region);
}
#ifdef DEBUG_RUNTIME
uint64_t t_after = halide_current_time_ns(user_context);
debug(user_context) << " Time: " << (t_after - t_before) / 1.0e6 << " ms\n";
#endif
return halide_error_code_success;
}
WEAK int halide_vulkan_copy_to_host(void *user_context, halide_buffer_t *halide_buffer) {
#ifdef DEBUG_RUNTIME
debug(user_context)
<< "halide_copy_to_host (user_context: " << user_context
<< ", halide_buffer: " << halide_buffer << ")\n";
#endif
if (halide_buffer == nullptr) {
error(user_context) << "Vulkan: Failed to copy buffer to host ... invalid halide buffer!\n";
return halide_error_code_copy_to_host_failed;
}
// Acquire the context so we can use the command queue. This also avoids multiple
// redundant calls to enqueue a download when multiple threads are trying to copy
// the same buffer.
VulkanContext ctx(user_context);
if (ctx.error != halide_error_code_success) {
error(user_context) << "Vulkan: Failed to acquire context!\n";
return ctx.error;
}
#ifdef DEBUG_RUNTIME
uint64_t t_before = halide_current_time_ns(user_context);
#endif
if ((halide_buffer->host == nullptr) || (halide_buffer->device == 0)) {
error(user_context) << "Vulkan: Missing host/device pointers for halide buffer!\n";
return halide_error_code_internal_error;
}
device_copy copy_helper = make_device_to_host_copy(halide_buffer);
// This is the inverse of copy_to_device: we create a staging buffer, copy into
// it, map it so the host can see it, then copy into the host buffer
MemoryRequest request = {0};
request.size = halide_buffer->size_in_bytes();
request.properties.usage = MemoryUsage::TransferDst;
request.properties.caching = MemoryCaching::UncachedCoherent;
request.properties.visibility = MemoryVisibility::DeviceToHost;
// allocate a new region for staging the transfer
MemoryRegion *staging_region = ctx.allocator->reserve(user_context, request);
if ((staging_region == nullptr) || (staging_region->handle == nullptr)) {
error(user_context) << "Vulkan: Failed to allocate device memory!\n";
return halide_error_code_device_malloc_failed;
}
// retrieve the buffer from the region
VkBuffer *staging_buffer = reinterpret_cast<VkBuffer *>(staging_region->handle);
if (staging_buffer == nullptr) {
error(user_context) << "Vulkan: Failed to retrieve staging buffer for device memory!\n";
return halide_error_code_internal_error;
}
// get the allocated region for the device
MemoryRegion *device_region = reinterpret_cast<MemoryRegion *>(halide_buffer->device);
if (device_region == nullptr) {
error(user_context) << "Vulkan: Failed to retrieve device region for buffer!\n";
return halide_error_code_internal_error;
}
MemoryRegion *memory_region = ctx.allocator->owner_of(user_context, device_region);
if (memory_region == nullptr) {
error(user_context) << "Vulkan: Failed to retrieve memory region for buffer!\n";
return halide_error_code_internal_error;
}
// retrieve the buffer from the region
VkBuffer *device_buffer = reinterpret_cast<VkBuffer *>(memory_region->handle);
if (device_buffer == nullptr) {
error(user_context) << "Vulkan: Failed to retrieve buffer for device memory!\n";
return halide_error_code_internal_error;
}
#ifdef DEBUG_RUNTIME
debug(user_context)
<< " copying from device region=" << (void *)device_region << "\n"
<< " containing device buffer=" << (void *)device_buffer << "\n"
<< " into halide buffer=" << halide_buffer << "\n";
#endif
ScopedVulkanCommandBufferAndPool cmds(user_context, ctx.allocator, ctx.queue_family_index);
if (cmds.error_code != halide_error_code_success) {
error(user_context) << "Vulkan: Failed to create command buffer and pool!\n";
return cmds.error_code;
}
// begin the command buffer
VkCommandBufferBeginInfo command_buffer_begin_info =
{
VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO, // struct type
nullptr, // pointer to struct extending this
VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT, // flags
nullptr // pointer to parent command buffer
};
VkResult result = vkBeginCommandBuffer(cmds.command_buffer, &command_buffer_begin_info);
if (result != VK_SUCCESS) {
error(user_context) << "Vulkan: vkBeginCommandBuffer returned " << vk_get_error_name(result) << "\n";
return halide_error_code_device_buffer_copy_failed;
}
// define the src and dst config
bool from_host = false;
bool to_host = true;
uint64_t copy_dst = copy_helper.dst;
copy_helper.src = (uint64_t)(device_buffer);
copy_helper.dst = (uint64_t)(staging_buffer);
uint64_t src_offset = copy_helper.src_begin + device_region->range.head_offset;
uint64_t dst_offset = copy_helper.dst_begin;
// enqueue the copy operation, using the allocated buffers
int error_code = vk_do_multidimensional_copy(user_context, cmds.command_buffer, copy_helper,
src_offset, dst_offset,
halide_buffer->dimensions,
from_host, to_host);
if (error_code != halide_error_code_success) {
error(user_context) << "Vulkan: vk_do_multidimensional_copy failed!\n";
return error_code;
}
// end the command buffer
result = vkEndCommandBuffer(cmds.command_buffer);
if (result != VK_SUCCESS) {
error(user_context) << "vkEndCommandBuffer returned " << vk_get_error_name(result) << "\n";
return result;
}
//// 13. Submit the command buffer to our command queue
VkSubmitInfo submit_info =
{
VK_STRUCTURE_TYPE_SUBMIT_INFO, // struct type
nullptr, // pointer to struct extending this
0, // wait semaphore count
nullptr, // semaphores
nullptr, // pipeline stages where semaphore waits occur
1, // how many command buffers to execute
&(cmds.command_buffer), // the command buffers
0, // number of semaphores to signal
nullptr // the semaphores to signal
};
result = vkQueueSubmit(ctx.queue, 1, &submit_info, VK_NULL_HANDLE);
if (result != VK_SUCCESS) {
error(user_context) << "Vulkan: vkQueueSubmit returned " << vk_get_error_name(result) << "\n";
return halide_error_code_copy_to_device_failed;
}
//// 14. Wait until the queue is done with the command buffer
result = vkQueueWaitIdle(ctx.queue);
if (result != VK_SUCCESS) {
error(user_context) << "Vulkan: vkQueueWaitIdle returned " << vk_get_error_name(result) << "\n";
return halide_error_code_copy_to_device_failed;
}
// map the staging region to a host ptr
uint8_t *stage_host_ptr = (uint8_t *)ctx.allocator->map(user_context, staging_region);
if (stage_host_ptr == nullptr) {
error(user_context) << "Vulkan: Failed to map host pointer to device memory!\n";
return halide_error_code_copy_to_device_failed;
}
// copy to the (host-visible/coherent) staging buffer
copy_helper.dst = copy_dst;
copy_helper.src = (uint64_t)(stage_host_ptr);
copy_memory(copy_helper, user_context);
// unmap the pointer and reclaim the staging region
error_code = ctx.allocator->unmap(user_context, staging_region);
if (error_code != halide_error_code_success) {
error(user_context) << "Vulkan: Failed to umap staging region!\n";
return error_code;
}
if (halide_can_reuse_device_allocations(user_context)) {
ctx.allocator->release(user_context, staging_region);
} else {
ctx.allocator->reclaim(user_context, staging_region);
}
#ifdef DEBUG_RUNTIME
uint64_t t_after = halide_current_time_ns(user_context);
debug(user_context) << " Time: " << (t_after - t_before) / 1.0e6 << " ms\n";
#endif
return halide_error_code_success;
}
WEAK int halide_vulkan_buffer_copy(void *user_context, struct halide_buffer_t *src,
const struct halide_device_interface_t *dst_device_interface,
struct halide_buffer_t *dst) {
if (dst->dimensions > MAX_COPY_DIMS) {
error(user_context) << "Vulkan: Buffer has too many dimensions to copy to/from GPU\n";
return halide_error_code_buffer_extents_too_large;
}
// We only handle copies to Vulkan buffers or to host
if ((dst_device_interface != nullptr) && (dst_device_interface != &vulkan_device_interface)) {
error(user_context) << "Vulkan: Unable to copy buffer ... only Vulkan allocated device buffers copying to/from host are supported!\n";
return halide_error_code_device_buffer_copy_failed;
}
if ((src->device_dirty() || src->host == nullptr) && (src->device_interface != &vulkan_device_interface)) {
// This is handled at the higher level.
return halide_error_code_incompatible_device_interface;
}
bool from_host = (src->device_interface != &vulkan_device_interface) ||
(src->device == 0) ||
(src->host_dirty() && src->host != nullptr);
bool to_host = !dst_device_interface;
if (!(from_host || src->device)) {
error(user_context) << "Vulkan: halide_vulkan_buffer_copy: invalid copy source\n";
return halide_error_code_device_buffer_copy_failed;
}
if (!(to_host || dst->device)) {
error(user_context) << "Vulkan: halide_vulkan_buffer_copy: invalid copy destination\n";
return halide_error_code_device_buffer_copy_failed;
}
device_copy copy_helper = make_buffer_copy(src, from_host, dst, to_host);
int error_code = halide_error_code_success;
{
VulkanContext ctx(user_context);
if (ctx.error != halide_error_code_success) {
error(user_context) << "Vulkan: Failed to acquire context!\n";
return ctx.error;
}
debug(user_context)
<< "halide_vulkan_buffer_copy (user_context: " << user_context
<< ", src: " << src << ", dst: " << dst << ")\n";
#ifdef DEBUG_RUNTIME
uint64_t t_before = halide_current_time_ns(user_context);
#endif
MemoryRegion *staging_region = nullptr;
MemoryRegion *src_buffer_region = nullptr;
MemoryRegion *dst_buffer_region = nullptr;
//// wait until the queue is done with the command buffer
VkResult wait_result = vkQueueWaitIdle(ctx.queue);
if (wait_result != VK_SUCCESS) {
error(user_context) << "Vulkan: vkQueueWaitIdle returned " << vk_get_error_name(wait_result) << "\n";
if (to_host) {
return halide_error_code_copy_to_host_failed;
} else {
return halide_error_code_copy_to_device_failed;
}
}
int error_code = halide_error_code_success;
if (!from_host && !to_host) {
// Device only case
debug(user_context) << " buffer copy from: device to: device\n";
// get the buffer regions for the device
src_buffer_region = reinterpret_cast<MemoryRegion *>(src->device);
dst_buffer_region = reinterpret_cast<MemoryRegion *>(dst->device);
} else if (!from_host && to_host) {
// Device to Host
debug(user_context) << " buffer copy from: device to: host\n";
// Need to make sure all reads and writes to/from source are complete.
MemoryRequest request = {0};
request.size = src->size_in_bytes();
// NOTE: We may re-use this buffer so enable both src and dst
request.properties.usage = MemoryUsage::TransferSrcDst;
request.properties.caching = MemoryCaching::UncachedCoherent;
request.properties.visibility = MemoryVisibility::DeviceToHost;
// allocate a new region
staging_region = ctx.allocator->reserve(user_context, request);
if ((staging_region == nullptr) || (staging_region->handle == nullptr)) {
error(user_context) << "Vulkan: Failed to allocate device memory!\n";
return halide_error_code_device_malloc_failed;
}
// use the staging region and buffer from the copy destination
src_buffer_region = reinterpret_cast<MemoryRegion *>(src->device);
dst_buffer_region = staging_region;
} else if (from_host && !to_host) {
// Host to Device
debug(user_context) << " buffer copy from: host to: device\n";
// Need to make sure all reads and writes to/from destination are complete.
MemoryRequest request = {0};
request.size = src->size_in_bytes();
// NOTE: We may re-use this buffer so enable both src and dst
request.properties.usage = MemoryUsage::TransferSrcDst;
request.properties.caching = MemoryCaching::UncachedCoherent;
request.properties.visibility = MemoryVisibility::HostToDevice;
// allocate a new region
staging_region = ctx.allocator->reserve(user_context, request);
if ((staging_region == nullptr) || (staging_region->handle == nullptr)) {
error(user_context) << "Vulkan: Failed to allocate device memory!\n";
return halide_error_code_device_malloc_failed;
}
// map the region to a host ptr
uint8_t *stage_host_ptr = (uint8_t *)ctx.allocator->map(user_context, staging_region);
if (stage_host_ptr == nullptr) {
error(user_context) << "Vulkan: Failed to map host pointer to device memory!\n";
return halide_error_code_copy_to_device_failed;
}
// copy to the (host-visible/coherent) staging buffer, then restore the dst pointer
uint64_t copy_dst_ptr = copy_helper.dst;
copy_helper.dst = (uint64_t)(stage_host_ptr);
copy_memory(copy_helper, user_context);
copy_helper.dst = copy_dst_ptr;
// unmap the pointer
error_code = ctx.allocator->unmap(user_context, staging_region);
if (error_code != halide_error_code_success) {
error(user_context) << "Vulkan: Failed to unmap staging region!\n";
return halide_error_code_copy_to_device_failed;
}
// use the staging region and buffer from the copy source
src_buffer_region = staging_region;
dst_buffer_region = reinterpret_cast<MemoryRegion *>(dst->device);
} else if (from_host && to_host) {
debug(user_context) << " buffer copy from: host to: host\n";
copy_memory(copy_helper, user_context);
return halide_error_code_success;
}
if (src_buffer_region == nullptr) {
error(user_context) << "Vulkan: Failed to retrieve source buffer for device memory!\n";
return halide_error_code_internal_error;
}
if (dst_buffer_region == nullptr) {
error(user_context) << "Vulkan: Failed to retrieve destination buffer for device memory!\n";
return halide_error_code_internal_error;
}
// get the owning memory region (that holds the allocation)
MemoryRegion *src_memory_region = ctx.allocator->owner_of(user_context, src_buffer_region);
MemoryRegion *dst_memory_region = ctx.allocator->owner_of(user_context, dst_buffer_region);
// retrieve the buffers from the owning allocation region
VkBuffer *src_device_buffer = reinterpret_cast<VkBuffer *>(src_memory_region->handle);
VkBuffer *dst_device_buffer = reinterpret_cast<VkBuffer *>(dst_memory_region->handle);
ScopedVulkanCommandBufferAndPool cmds(user_context, ctx.allocator, ctx.queue_family_index);
if (cmds.error_code != halide_error_code_success) {
error(user_context) << "Vulkan: Failed to create command buffer and pool!\n";
if (to_host) {
return halide_error_code_copy_to_host_failed;
} else {
return halide_error_code_copy_to_device_failed;
}
}
// begin the command buffer
VkCommandBufferBeginInfo command_buffer_begin_info =
{
VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO, // struct type
nullptr, // pointer to struct extending this
VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT, // flags
nullptr // pointer to parent command buffer
};
VkResult result = vkBeginCommandBuffer(cmds.command_buffer, &command_buffer_begin_info);
if (result != VK_SUCCESS) {
error(user_context) << "Vulkan: vkBeginCommandBuffer returned " << vk_get_error_name(result) << "\n";
if (to_host) {
return halide_error_code_copy_to_host_failed;
} else {
return halide_error_code_copy_to_device_failed;
}
}
// define the src and dst config
uint64_t copy_dst = copy_helper.dst;
copy_helper.src = (uint64_t)(src_device_buffer);
copy_helper.dst = (uint64_t)(dst_device_buffer);
uint64_t src_offset = copy_helper.src_begin + src_buffer_region->range.head_offset;
uint64_t dst_offset = copy_helper.dst_begin + dst_buffer_region->range.head_offset;
debug(user_context) << " src region=" << (void *)src_memory_region << " buffer=" << (void *)src_device_buffer << " crop_offset=" << (uint64_t)src_buffer_region->range.head_offset << " copy_offset=" << src_offset << "\n";
debug(user_context) << " dst region=" << (void *)dst_memory_region << " buffer=" << (void *)dst_device_buffer << " crop_offset=" << (uint64_t)dst_buffer_region->range.head_offset << " copy_offset=" << dst_offset << "\n";
// enqueue the copy operation, using the allocated buffers
error_code = vk_do_multidimensional_copy(user_context, cmds.command_buffer, copy_helper,
src_offset, dst_offset,
src->dimensions,
from_host, to_host);
if (error_code != halide_error_code_success) {
error(user_context) << "Vulkan: vk_do_multidimensional_copy failed!\n";
return error_code;
}
// end the command buffer
result = vkEndCommandBuffer(cmds.command_buffer);
if (result != VK_SUCCESS) {
error(user_context) << "vkEndCommandBuffer returned " << vk_get_error_name(result) << "\n";
if (to_host) {
return halide_error_code_copy_to_host_failed;
} else {
return halide_error_code_copy_to_device_failed;
}
}
//// submit the command buffer to our command queue
VkSubmitInfo submit_info =
{
VK_STRUCTURE_TYPE_SUBMIT_INFO, // struct type
nullptr, // pointer to struct extending this
0, // wait semaphore count
nullptr, // semaphores
nullptr, // pipeline stages where semaphore waits occur
1, // how many command buffers to execute
&(cmds.command_buffer), // the command buffers
0, // number of semaphores to signal
nullptr // the semaphores to signal
};
result = vkQueueSubmit(ctx.queue, 1, &submit_info, VK_NULL_HANDLE);
if (result != VK_SUCCESS) {
error(user_context) << "vkQueueSubmit returned " << vk_get_error_name(result) << "\n";
return result;
}
//// wait until the queue is done with the command buffer
result = vkQueueWaitIdle(ctx.queue);
if (result != VK_SUCCESS) {
error(user_context) << "vkQueueWaitIdle returned " << vk_get_error_name(result) << "\n";
return result;
}
if (!from_host && to_host) {
// map the staging region to a host ptr
uint8_t *stage_host_ptr = (uint8_t *)ctx.allocator->map(user_context, staging_region);
if (stage_host_ptr == nullptr) {
error(user_context) << "Vulkan: Failed to map host pointer to device memory!\n";
return halide_error_code_internal_error;
}