-
Notifications
You must be signed in to change notification settings - Fork 154
/
buffer.hpp
1234 lines (1019 loc) · 38.5 KB
/
buffer.hpp
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
/*
* This file is part of hipSYCL, a SYCL implementation based on CUDA/HIP
*
* Copyright (c) 2018-2020 Aksel Alpay and contributors
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef HIPSYCL_BUFFER_HPP
#define HIPSYCL_BUFFER_HPP
#include <cstddef>
#include <cstring>
#include <iterator>
#include <limits>
#include <memory>
#include <mutex>
#include <type_traits>
#include <algorithm>
#include <utility>
#include "hipSYCL/common/debug.hpp"
#include "hipSYCL/runtime/allocator.hpp"
#include "hipSYCL/runtime/application.hpp"
#include "hipSYCL/runtime/data.hpp"
#include "hipSYCL/runtime/device_id.hpp"
#include "hipSYCL/runtime/hints.hpp"
#include "hipSYCL/runtime/operations.hpp"
#include "hipSYCL/runtime/util.hpp"
#include "hipSYCL/sycl/access.hpp"
#include "hipSYCL/sycl/device.hpp"
#include "hipSYCL/sycl/exception.hpp"
#include "hipSYCL/sycl/extensions.hpp"
#include "property.hpp"
#include "types.hpp"
#include "context.hpp"
#include "buffer_allocator.hpp"
#include "hipSYCL/sycl/libkernel/id.hpp"
#include "hipSYCL/sycl/libkernel/range.hpp"
#include "../common/reinterpret_pointer_cast.hpp"
#include "libkernel/accessor.hpp"
namespace hipsycl {
namespace sycl {
namespace detail::buffer_policy {
class destructor_waits : public buffer_property
{
public:
destructor_waits(bool v): _v{v}{}
bool value() const {return _v;}
private:
bool _v;
};
class writes_back : public buffer_property
{
public:
writes_back(bool v): _v{v}{}
bool value() const {return _v;}
private:
bool _v;
};
class use_external_storage : public buffer_property
{
public:
use_external_storage(bool v): _v{v}{}
bool value() const {return _v;}
private:
bool _v;
};
}
namespace property::buffer {
class use_host_ptr : public detail::buffer_property
{
public:
use_host_ptr() = default;
};
class use_mutex : public detail::buffer_property
{
public:
use_mutex(mutex_class& ref);
mutex_class* get_mutex_ptr() const;
};
class context_bound : public detail::buffer_property
{
public:
context_bound(context bound_context)
: _ctx{bound_context}
{}
context get_context() const
{
return _ctx;
}
private:
context _ctx;
};
class use_optimized_host_memory : public detail::buffer_property
{};
template<int Dim>
class hipSYCL_page_size : public detail::buffer_property
{
public:
hipSYCL_page_size(const sycl::range<Dim>& page_size)
: _page_size{page_size} {}
sycl::range<Dim> get_page_size() const
{
return _page_size;
}
private:
sycl::range<Dim> _page_size;
};
class hipSYCL_write_back_node_group : public detail::buffer_property
{
public:
hipSYCL_write_back_node_group(std::size_t group)
: _node_group{group} {}
std::size_t get_node_group() const {
return _node_group;
}
private:
std::size_t _node_group;
};
using hipSYCL_buffer_uses_external_storage =
detail::buffer_policy::use_external_storage;
using hipSYCL_buffer_writes_back =
detail::buffer_policy::writes_back;
using hipSYCL_buffer_destructor_blocks =
detail::buffer_policy::destructor_waits;
} // property::buffer
namespace detail {
template <class BufferT>
std::shared_ptr<rt::buffer_data_region>
extract_buffer_data_region(const BufferT &buff);
struct buffer_impl
{
rt::runtime_keep_alive_token requires_runtime;
std::mutex lock;
// Only used if a shared_ptr is passed to set_final_data()
std::shared_ptr<void> writeback_buffer;
// Only used if writeback is enabled
void* writeback_ptr;
// Only used if a shared_ptr is passed to the buffer constructor
std::shared_ptr<void> shared_host_data;
std::size_t write_back_node_group;
std::shared_ptr<rt::buffer_data_region> data;
bool writes_back;
bool destructor_waits;
bool use_external_storage;
~buffer_impl() {
if (writes_back) {
if (!writeback_ptr) {
HIPSYCL_DEBUG_WARNING
<< "buffer_impl::~buffer_impl: Writeback was requested but "
"writeback pointer is null. Skipping write-back."
<< std::endl;
} else {
HIPSYCL_DEBUG_INFO
<< "buffer_impl::~buffer_impl: Preparing submission of writeback..."
<< std::endl;
if (data->has_allocation(get_host_device()) &&
(data->get_memory(get_host_device()) != this->writeback_ptr)) {
// We are writing back to an external location, i.e. a location
// set with set_final_data()
// TODO Currently, we are requesting an host update and then
// submit an explicit copy to writeback_ptr.
// We could directly copy from device if
// there is a device that has up-to-date data.
submit_copy(detail::get_host_device(), writeback_ptr);
} else {
rt::dag_build_guard build{requires_runtime.get()->dag()};
auto explicit_requirement =
rt::make_operation<rt::buffer_memory_requirement>(
data, rt::id<3>{}, data->get_num_elements(),
sycl::access::mode::read, sycl::access::target::host_buffer);
rt::execution_hints hints;
add_writeback_hints(detail::get_host_device(), hints);
build.builder()->add_explicit_mem_requirement(
std::move(explicit_requirement),
rt::requirements_list{requires_runtime.get()}, hints);
}
}
}
if(destructor_waits) {
HIPSYCL_DEBUG_INFO
<< "buffer_impl::~buffer_impl: Waiting for operations to complete..."
<< std::endl;
auto buffer_users = data->get_users().get_users();
for (auto &user : buffer_users) {
auto user_ptr = user.user.lock();
if(user_ptr) {
if(!user_ptr->is_submitted()) {
HIPSYCL_DEBUG_INFO
<< "buffer_impl::~buffer_impl: dag node is registered as user "
"but not marked as submitted, performing emergency DAG flush."
<< std::endl;
requires_runtime.get()->dag().flush_sync();
}
assert(user_ptr->is_submitted());
user_ptr->wait();
}
}
}
}
private:
bool has_writeback_node_group() const {
return write_back_node_group != std::numeric_limits<std::size_t>::max();
}
void add_writeback_hints(rt::device_id dev, rt::execution_hints& hints) {
hints.add_hint(
rt::make_execution_hint<rt::hints::bind_to_device>(dev));
if (has_writeback_node_group()) {
hints.add_hint(rt::make_execution_hint<rt::hints::node_group>(
write_back_node_group));
}
}
rt::dag_node_ptr submit_copy(rt::device_id source_dev, void* dest) {
std::shared_ptr<rt::buffer_data_region> data_src = this->data;
rt::dag_build_guard build{requires_runtime.get()->dag()};
rt::execution_hints hints;
add_writeback_hints(source_dev, hints);
rt::requirements_list reqs{requires_runtime.get()};
auto req = std::make_unique<rt::buffer_memory_requirement>(
data_src, rt::id<3>{}, data_src->get_num_elements(), access_mode::read,
target::device);
reqs.add_requirement(std::move(req));
rt::memory_location source_location{source_dev, rt::id<3>{},
data_src};
rt::memory_location dest_location{detail::get_host_device(), dest,
rt::id<3>{}, data_src->get_num_elements(),
data_src->get_element_size()};
auto explicit_copy = rt::make_operation<rt::memcpy_operation>(
source_location, dest_location, data_src->get_num_elements());
rt::dag_node_ptr node = build.builder()->add_memcpy(
std::move(explicit_copy), reqs, hints);
return node;
}
};
}
namespace buffer_allocation {
// This class is part of the USM-buffer interop API
//
// TODO Currently cannot represent allocations from
// malloc_host()
template <class T> struct descriptor {
// the USM allocation used for this device
T *ptr;
// the device for which this allocation is used.
// Note that the runtime may only maintain
// a single allocation for all host devices.
device dev;
// Whether the runtime will delete this allocation
// when the buffer is released.
bool is_owned;
};
template<class T>
struct tracked_descriptor {
descriptor<T> desc;
bool is_recent;
};
enum class management_mode {
owning,
non_owning
};
inline constexpr management_mode take_ownership = management_mode::owning;
inline constexpr management_mode no_ownership = management_mode::non_owning;
/// Construct an allocation descriptor for outdated data
/// that needs to be updated by the runtime when accessed
template <class T>
tracked_descriptor<T> empty_view(T *ptr, device dev,
management_mode m = no_ownership) {
tracked_descriptor<T> d;
bool is_owned = (m == take_ownership) ? true : false;
d.desc = descriptor<T>{ptr, dev, is_owned};
d.is_recent = false;
return d;
}
/// Construct an allocation descriptor for an allocation
/// already holding live data that is up-to-date and does
/// not need updating before use.
template <class T>
tracked_descriptor<T> view(T *ptr, device dev,
management_mode m = no_ownership) {
tracked_descriptor<T> d;
bool is_owned = (m == take_ownership) ? true : false;
d.desc = descriptor<T>{ptr, dev, is_owned};
d.is_recent = true;
return d;
}
}
template <typename T, int dimensions = 1,
typename AllocatorT = buffer_allocator<std::remove_const_t<T>>>
class buffer : public detail::property_carrying_object
{
public:
template <class OtherT, int OtherDim, typename OtherAllocator>
friend class buffer;
template <class BufferT>
friend std::shared_ptr<rt::buffer_data_region>
detail::extract_buffer_data_region(const BufferT &buff);
using value_type = T;
using reference = value_type &;
using const_reference = const value_type &;
using allocator_type = AllocatorT;
static constexpr int buffer_dim = dimensions;
/// buffer USM interop constructor
buffer(const std::vector<buffer_allocation::tracked_descriptor<T>>
&input_allocations,
const range<dimensions> &r,
const property_list &propList = {})
: detail::property_carrying_object{propList}
{
_impl = std::make_shared<detail::buffer_impl>();
default_policies dpol;
dpol.destructor_waits = true;
dpol.use_external_storage = true;
dpol.writes_back = false;
init_policies_from_properties_or_default(dpol);
if(_impl->writes_back) {
HIPSYCL_DEBUG_WARNING
<< "buffer: Explicit writeback policy was requested, but buffers "
"using USM interoperability cannot enable writeback at "
"construction. Disabling writeback."
<< std::endl;
_impl->writes_back = false;
}
if(!_impl->use_external_storage) {
HIPSYCL_DEBUG_WARNING
<< "buffer: No external storage policy was explicitly requested, but "
"this does not make sense for USM interoperability buffers. "
"Enabling using external storage."
<< std::endl;
_impl->writes_back = false;
}
this->init(r, input_allocations);
}
buffer(const std::vector<buffer_allocation::tracked_descriptor<T>>
&input_allocations,
const range<dimensions> &r,
AllocatorT allocator, const property_list &propList = {})
: buffer(r, input_allocations, propList)
{
_alloc = allocator;
}
buffer(const range<dimensions> &bufferRange,
const property_list &propList = {})
: detail::property_carrying_object{propList}
{
_impl = std::make_shared<detail::buffer_impl>();
default_policies dpol;
dpol.destructor_waits = true;
dpol.use_external_storage = false;
dpol.writes_back = false;
init_policies_from_properties_or_default(dpol);
this->init(bufferRange);
if(_impl->use_external_storage) {
HIPSYCL_DEBUG_WARNING
<< "buffer: was constructed with use_external_storage but no host "
"pointer was supplied. Cannot initialize this buffer with "
"external storage."
<< std::endl;
}
}
buffer(const range<dimensions> &bufferRange, AllocatorT allocator,
const property_list &propList = {})
: buffer(bufferRange, propList)
{ _alloc = allocator; }
buffer(std::remove_const_t<T> *hostData, const range<dimensions> &bufferRange,
const property_list &propList = {})
: detail::property_carrying_object{propList}
{
_impl = std::make_shared<detail::buffer_impl>();
default_policies dpol;
dpol.destructor_waits = true;
dpol.use_external_storage = true;
dpol.writes_back = true;
init_policies_from_properties_or_default(dpol);
if(_impl->use_external_storage)
this->init(bufferRange, hostData);
else {
this->init(bufferRange);
copy_host_content(hostData);
}
}
buffer(std::remove_const_t<T> *hostData, const range<dimensions> &bufferRange,
AllocatorT allocator, const property_list &propList = {})
: buffer{hostData, bufferRange, propList} {
_alloc = allocator;
}
template <class t = T, std::enable_if_t<!std::is_const_v<t>, bool> = true>
buffer(const T *hostData, const range<dimensions> &bufferRange,
const property_list &propList = {})
: detail::property_carrying_object{propList} {
_impl = std::make_shared<detail::buffer_impl>();
default_policies dpol;
dpol.destructor_waits = true;
dpol.use_external_storage = false;
dpol.writes_back = false;
init_policies_from_properties_or_default(dpol);
if(!_impl->use_external_storage) {
// Construct buffer
this->init(bufferRange);
// Only use hostData for initialization
copy_host_content(hostData);
} else {
HIPSYCL_DEBUG_WARNING
<< "buffer: constructed with property use_external_storage, but user "
"passed a const pointer to buffer constructor. Removing const to enforce "
"requested view semantics."
<< std::endl;
this->init(bufferRange, const_cast<T*>(hostData));
}
}
buffer(const T *hostData, const range<dimensions> &bufferRange,
AllocatorT allocator, const property_list &propList = {})
: buffer{hostData, bufferRange, propList}
{ _alloc = allocator; }
buffer(const std::shared_ptr<T> &hostData,
const range<dimensions> &bufferRange, AllocatorT allocator,
const property_list &propList = {})
: detail::property_carrying_object{propList}
{
_impl = std::make_shared<detail::buffer_impl>();
_alloc = allocator;
default_policies dpol;
dpol.destructor_waits = true;
dpol.use_external_storage = true;
dpol.writes_back = true;
init_policies_from_properties_or_default(dpol);
if(_impl->use_external_storage) {
_impl->shared_host_data = hostData;
this->init(bufferRange, hostData.get());
} else {
this->init(bufferRange);
this->copy_host_content(hostData.get());
}
}
buffer(const shared_ptr_class<T> &hostData,
const range<dimensions> &bufferRange,
const property_list &propList = {})
: buffer(hostData, bufferRange, AllocatorT(), propList)
{}
template <class InputIterator,
int D = dimensions,
typename = std::enable_if_t<D==1>>
buffer(InputIterator first, InputIterator last,
AllocatorT allocator,
const property_list &propList = {})
: detail::property_carrying_object{propList}
{
_impl = std::make_shared<detail::buffer_impl>();
default_policies dpol;
dpol.destructor_waits = true;
dpol.use_external_storage = false;
dpol.writes_back = false;
init_policies_from_properties_or_default(dpol);
if(_impl->use_external_storage)
// TODO This could be allowed for special cases, e.g. if iterators are pointers
throw invalid_parameter_error{
"buffer: Cannot comply: User requested to using external storage, "
"but this is not yet possible with iterators."};
_alloc = allocator;
std::size_t num_elements = std::distance(first, last);
// Construct buffer
this->init(range<1>{num_elements});
// Work around vector<bool> specialization..
if constexpr(std::is_same_v<bool, std::remove_const_t<T>>){
std::vector<char> contiguous_buffer(num_elements);
std::copy(first, last, reinterpret_cast<T*>(&(contiguous_buffer[0])));
copy_host_content(reinterpret_cast<T*>(contiguous_buffer.data()));
} else {
std::vector<T> contiguous_buffer(num_elements);
std::copy(first, last, contiguous_buffer.begin());
copy_host_content(contiguous_buffer.data());
}
}
template <class InputIterator, int D = dimensions,
typename = std::enable_if_t<D == 1>>
buffer(InputIterator first, InputIterator last,
const property_list &propList = {})
: buffer(first, last, AllocatorT(), propList)
{}
buffer(buffer<T, dimensions, AllocatorT> b,
const id<dimensions> &baseIndex,
const range<dimensions> &subRange)
{
assert(false && "subbuffer is unimplemented");
}
// Allow conversion to buffer<const T> from buffer<T>
template <class t = T, std::enable_if_t<std::is_const_v<t>, bool> = true>
buffer(const buffer<std::remove_const_t<T>, dimensions, AllocatorT> &other)
: _alloc{other._alloc}, _range{other._range}, _impl{other._impl},
detail::property_carrying_object{other} {}
range<dimensions> get_range() const
{
return _range;
}
std::size_t size() const noexcept
{
return _range.size();
}
std::size_t byte_size() const noexcept
{
return size() * sizeof(T);
}
std::size_t get_size() const
{
return byte_size();
}
std::size_t get_count() const
{
return size();
}
AllocatorT get_allocator() const
{
return _alloc;
}
template <access_mode mode = access_mode::read_write,
access::target target = access::target::device>
auto get_access(handler &commandGroupHandler) {
#ifdef HIPSYCL_EXT_ACCESSOR_VARIANT_DEDUCTION
constexpr accessor_variant variant = accessor_variant::unranged;
#else
constexpr accessor_variant variant = accessor_variant::false_t;
#endif
return accessor<T, dimensions, mode, target, variant>{
*this, commandGroupHandler};
}
// Deprecated
template <access::mode mode>
auto get_access()
{
return accessor<T, dimensions, mode, access::target::host_buffer,
accessor_variant::false_t>{*this};
}
template <access_mode mode = access_mode::read_write,
access::target target = access::target::device>
auto get_access(handler &commandGroupHandler, range<dimensions> accessRange,
id<dimensions> accessOffset = {}) {
#ifdef HIPSYCL_EXT_ACCESSOR_VARIANT_DEDUCTION
constexpr accessor_variant variant = accessor_variant::ranged;
#else
constexpr accessor_variant variant = accessor_variant::false_t;
#endif
return accessor<T, dimensions, mode, target, variant>{
*this, commandGroupHandler, accessRange, accessOffset
};
}
// Deprecated
template <access::mode mode>
auto get_access(
range<dimensions> accessRange, id<dimensions> accessOffset = {})
{
return accessor<T, dimensions, mode, access::target::host_buffer,
accessor_variant::false_t>{*this, accessRange,
accessOffset};
}
template<typename... Args>
auto get_access(Args&&... args) {
return accessor{*this, std::forward<Args>(args)...};
}
template<typename... Args>
auto get_host_access(Args&&... args) {
return host_accessor{*this, std::forward<Args>(args)...};
}
void set_final_data(std::shared_ptr<T> finalData)
{
std::lock_guard<std::mutex> lock {_impl->lock};
set_write_back_target(finalData.get());
_impl->writeback_buffer = finalData;
}
// TODO Add special handling of iterators for set_final_data()
template <typename Destination = std::nullptr_t>
void set_final_data(Destination finalData = nullptr)
{
std::lock_guard<std::mutex> lock {_impl->lock};
set_write_back_target(finalData);
}
void set_write_back(bool flag = true)
{
std::lock_guard<std::mutex> lock {_impl->lock};
this->enable_write_back(flag);
}
// ToDo Subbuffers are unsupported
bool is_sub_buffer() const
{ return false; }
template <typename ReinterpretT, int ReinterpretDim>
buffer<ReinterpretT, ReinterpretDim,
typename std::allocator_traits<AllocatorT>
::template rebind_alloc<ReinterpretT>>
reinterpret(range<ReinterpretDim> reinterpretRange) const {
if(_range.size() * sizeof(T) != reinterpretRange.size() * sizeof(ReinterpretT))
throw invalid_parameter_error{"reinterpret must preserve the byte count of the buffer"};
buffer<ReinterpretT, ReinterpretDim,
typename std::allocator_traits<AllocatorT>::template rebind_alloc<
ReinterpretT>> new_buffer;
static_cast<detail::property_carrying_object&>(new_buffer) = *this;
new_buffer._alloc = _alloc;
new_buffer._impl = _impl;
new_buffer._range = reinterpretRange;
return new_buffer;
}
template <typename ReinterpretT, int ReinterpretDim = dimensions,
std::enable_if_t<ReinterpretDim == 1 ||
(ReinterpretDim == dimensions && sizeof(ReinterpretT) == sizeof(T)), int> = 0>
buffer<ReinterpretT, ReinterpretDim,
typename std::allocator_traits<AllocatorT>
::template rebind_alloc<ReinterpretT>>
reinterpret() const {
if constexpr (ReinterpretDim == 1) {
return reinterpret<ReinterpretT, 1>(range<1>{
(_range.size() * sizeof(T)) / sizeof(ReinterpretT)});
} else {
return reinterpret<ReinterpretT, ReinterpretDim>(_range);
}
}
friend bool operator==(const buffer& lhs, const buffer& rhs)
{
return lhs._impl == rhs._impl;
}
friend bool operator!=(const buffer& lhs, const buffer& rhs)
{
return !(lhs == rhs);
}
std::size_t hipSYCL_hash_code() const {
return std::hash<void*>{}(_impl.get());
}
rt::runtime* hipSYCL_runtime() const {
return _impl->requires_runtime.get();
}
// --- The following methods are part the hipSYCL buffer introspection API
// which is part of the hipSYCL buffer-USM interoperability framework.
/// Iterate over each allocation.
/// \param h Handler that will be invoked for each allocation.
/// Signature: void(const buffer_allocation::descriptor<T>&)
template <class Handler>
void for_each_allocation(Handler &&h) const{
_impl->data->for_each_allocation_while(
[&h](const rt::data_allocation<void *> &alloc) {
buffer_allocation::descriptor<T> a =
rt_data_allocation_to_buffer_alloc(alloc);
h(a);
return true;
});
}
/// Instruct buffer to free the allocation on the specified device at buffer
/// destruction.
/// Throws \c invalid_parameter_error if no allocation for specified device
/// exists.
void own_allocation(const device &dev) {
rt::device_id rt_dev = detail::extract_rt_device(dev);
bool found = _impl->data->find_and_handle_allocation(
rt_dev,
[](rt::data_allocation<void *> &alloc) { alloc.is_owned = true; });
if (!found)
throw invalid_parameter_error{
"Buffer does not contain allocation for specified device"};
}
/// Instruct buffer to free the allocation at buffer destruction.
/// \c ptr must be an existing allocation managed by the buffer.
/// If \c ptr cannot be found among the managed memory allocations,
/// \c invalid_parameter_error is thrown.
void own_allocation(const T *ptr) {
bool found = _impl->data->find_and_handle_allocation(
static_cast<void *>(const_cast<T*>(ptr)),
[&](rt::data_allocation<void *> &rt_allocation) {
rt_allocation.is_owned = true;
});
if (!found) {
throw invalid_parameter_error{"Provided pointer was not found among the "
"managed buffer allocations."};
}
}
/// Instruct buffer to not free the allocation on the specified device at buffer
/// destruction.
/// Throws \c invalid_parameter_error if no allocation for specified device
/// exists.
void disown_allocation(const device &dev) {
rt::device_id rt_dev = detail::extract_rt_device(dev);
bool found = _impl->data->find_and_handle_allocation(
rt_dev,
[](rt::data_allocation<void *> &alloc) { alloc.is_owned = false; });
if (!found)
throw invalid_parameter_error{
"Buffer does not contain allocation for specified device"};
}
/// Instruct buffer to not free the allocation associated with the provided
/// pointer.
/// Throws \c invalid_parameter_error if no allocation managed by the buffer
/// is described by \c ptr.
void disown_allocation(const T *ptr) {
bool found = _impl->data->find_and_handle_allocation(
static_cast<void *>(const_cast<T*>(ptr)),
[&](rt::data_allocation<void *> &rt_allocation) {
rt_allocation.is_owned = false;
});
if (!found) {
throw invalid_parameter_error{"Provided pointer was not found among the "
"managed buffer allocations."};
}
}
/// Get USM pointer for the buffer allocation of the specified device.
/// \return The USM pointer associated with the device, or nullptr if
/// the buffer does not contain an allocation for the device.
T *get_pointer(const device &dev) const {
rt::device_id rt_dev = detail::extract_rt_device(dev);
if (!_impl->data->has_allocation(rt_dev))
return nullptr;
// Because the hipSYCL buffer-accessor model spec guarantees that
// allocations are never freed before buffer destruction,
// it is not a race condition to assume that the allocation still
// exists after the check above.
return static_cast<T*>(_impl->data->get_memory(rt_dev));
}
/// \return Whether the buffer contains an allocation for the given device.
bool has_allocation(const device &dev) const {
rt::device_id rt_dev = detail::extract_rt_device(dev);
return _impl->data->has_allocation(rt_dev);
}
/// \return the buffer allocation object associated with the provided
/// device. If the buffer does not contain an allocation for the specified
/// device, throws \c invalid_parameter_error.
buffer_allocation::descriptor<T> get_allocation(const device &dev) const {
rt::device_id rt_dev = detail::extract_rt_device(dev);
if (!_impl->data->has_allocation(rt_dev))
throw invalid_parameter_error{
"No allocation for the given device was found"};
auto rt_allocation = _impl->data->get_allocation(rt_dev);
return rt_data_allocation_to_buffer_alloc(rt_allocation);
}
/// \return the buffer allocation object associated with the provided pointer.
/// If the buffer does not contain an allocation described by ptr,
/// throws \c invalid_parameter_error.
buffer_allocation::descriptor<T> get_allocation(const T *ptr) const {
buffer_allocation::descriptor<T> result = null_allocation();
bool found = _impl->data->find_and_handle_allocation(
static_cast<void *>(const_cast<T*>(ptr)),
[&](const auto &rt_allocation) {
result = rt_data_allocation_to_buffer_alloc(rt_allocation);
});
if (!found) {
throw invalid_parameter_error{"Provided pointer was not found among the "
"managed buffer allocations."};
}
return result;
}
// -- End of hipSYCL buffer-USM introspection API
private:
struct default_policies
{
bool destructor_waits;
bool writes_back;
bool use_external_storage;
};
static buffer_allocation::descriptor<T>
rt_data_allocation_to_buffer_alloc(const rt::data_allocation<void *> &alloc) {
buffer_allocation::descriptor<T> buffer_alloc;
buffer_alloc.dev = sycl::device{alloc.dev};
buffer_alloc.is_owned = alloc.is_owned;
buffer_alloc.ptr = static_cast<T *>(alloc.memory);
return buffer_alloc;
}
static buffer_allocation::descriptor<T> null_allocation() {
buffer_allocation::descriptor<T> result;
result.ptr = nullptr;
result.is_owned = false;
result.dev = detail::get_host_device();
return result;
}
template <typename Destination = std::nullptr_t>
void set_write_back_target(Destination finalData = nullptr)
{
if constexpr(std::is_pointer_v<Destination> || std::is_null_pointer_v<Destination>){
if (finalData) {
enable_write_back(true);
}
else {
enable_write_back(false);
}
_impl->writeback_ptr = finalData;
} else {
// Assume it is an iterator to contiguous memory
enable_write_back(true);
_impl->writeback_ptr = &(*finalData);
}
}
void enable_write_back(bool flag) {
if(this->has_property<detail::buffer_policy::writes_back>()){
if (_impl->writes_back != flag){
// Deny changing policy if it has previously been explicitly requested
// by the user
throw invalid_parameter_error{
"buffer::set_write_back(): buffer was constructed explicitly with "
"writeback policy, denying changing the policy as this likely "
"indicates a bug in user code"};
}
}
if(_impl->writes_back != flag) {
HIPSYCL_DEBUG_INFO << "buffer: Changing write back policy to: " << flag
<< std::endl;
_impl->writes_back = flag;
}
}
void copy_host_content(const T* data)
{
assert(_impl);
auto host_device = detail::get_host_device();
preallocate_host_buffer();
std::memcpy(_impl->data->get_memory(host_device), data,
sizeof(T) * _range.size());
// Mark the modified range current so that the runtime
// knows that it needs to transfer this data if it is
// accessed on device
_impl->data->mark_range_current(host_device,