-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmdspan_omp.h
More file actions
969 lines (713 loc) · 26.3 KB
/
mdspan_omp.h
File metadata and controls
969 lines (713 loc) · 26.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
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
#ifndef MDSPANH
#define MDSPANH
#include <iostream>
#include <array>
#include <vector>
#include <unordered_map>
#include <numeric>
#include <cmath>
#include <numbers>
#include <memory>
#include <cassert>
#include "datablock.h"
#include <array>
#include <vector>
#include <cstddef>
#include <unordered_map>
#include <set>
#include "datablock.h"
#include "datablock_gpu_memory_functions.h"
using namespace std;
// Concept definitions
template <typename Container>
concept StaticContainer =
requires(Container c, size_t i)
{
{
c.size()
}
-> std::convertible_to<size_t>;
{
c[i]
}
-> std::convertible_to<typename Container::value_type>;
(!requires(Container c, size_t i)
{
c.reserve(i);
});
};
template <typename Container>
concept DynamicContainer =
requires(Container c, size_t i)
{
{
c.size()
}
-> std::convertible_to<size_t>;
{
c[i]
}
-> std::convertible_to<typename Container::value_type>;
c.reserve(i); // Require reserve() for dynamic containers
};
// Concept to check if two containers are of the same type and have matching size
template <typename ExtentsContainer>
concept Container =
(StaticContainer<ExtentsContainer> || // Same size for static containers
(DynamicContainer<ExtentsContainer>)); // Same size for dynamic containers
// Class template for mdspan
template <typename T, typename Container>
class mdspan:public DataBlock<T>
{
protected:
class DevicemappingManager
{
protected:
struct Interval
{
intptr_t start;
intptr_t end;
bool operator<(const Interval& other) const
{
return start < other.start;
}
};
std::unordered_map<int, std::set<Interval>> device_intervals;
bool overlaps(const Interval& a, const Interval& b) const
{
return a.start < b.end && b.start < a.end;
}
public:
bool insert(int device, intptr_t start, intptr_t end)
{
Interval new_iv{start, end};
auto& s = device_intervals[device];
auto it = s.lower_bound(new_iv);
if (it != s.end() && overlaps(new_iv, *it)) return false;
if (it != s.begin() && overlaps(new_iv, *std::prev(it))) return false;
s.insert(it, new_iv);
return true;
}
// Remove interval
bool remove(int device, intptr_t start, intptr_t end)
{
auto it = device_intervals.find(device);
if (it != device_intervals.end())
{
Interval iv{start, end};
size_t erased = it->second.erase(iv);
if (erased == 0) return false;
if (it->second.empty()) device_intervals.erase(it);
return true;
}
else
return false;
}
void showmapped() const
{
for (const auto& [device, intervals] : device_intervals)
{
std::cout << "Device " << device << ": ";
for (const auto& iv : intervals)
std::cout << "[" << iv.start << "," << iv.end << ") ";
std::cout << "\n";
}
}
};
void initialize_extents_and_strides(const Container&extents,const Container & strides);
void initialize_extents(const Container&extents);
void allocate_extents_and_strides(size_t r);
void adopt_subDataBlock_helper(const DataBlock<T>& sub);
Container pextents;
Container pstrides;
shared_ptr<DevicemappingManager> mapping_manager=make_shared<DevicemappingManager>();
bool p_has_offloaded_host_data=false;
public:
mdspan() {};
mdspan(const DataBlock<T>& ds,const shared_ptr<mdspan<T,Container>::DevicemappingManager> &dev);
mdspan(const mdspan<T, Container>& other);
mdspan(mdspan<T, Container>&& other)noexcept;
mdspan<T, Container> &operator=(const mdspan<T,Container> & other);
mdspan<T, Container> &operator=(const DataBlock<T> & other);
mdspan<T, Container> &operator=(mdspan<T, Container>&& other)noexcept;
mdspan(T* data, const size_t datalength, const Container& extents, const Container& strides,const bool rowm=true,bool dpdata_is_devptr=false,int devnum=0);
mdspan(T* data, const Container& extents, const Container& strides,const bool rowm=true, bool dpdata_is_devptr=false,int devnum=0);
mdspan(T* data, const Container& extents, const bool rowm=true,bool dpdata_is_devptr=false,int devnum=0);
mdspan(T* data, const size_t rows,const size_t cols,const bool rowm=true,bool dpdata_is_devptr=false,int devnum=0);
mdspan(T* data,const size_t rows, const bool rowm=true,bool dpdata_is_devptr=false,int devnum=0);
virtual ~mdspan();
using DataBlock<T>::operator();
inline T& operator()(const Container& extents);
inline T operator()(const Container& extents)const;
using DataBlock<T>::operator=;
// Subspan methods
using DataBlock<T>::tensor_subspan;
mdspan<T, Container>tensor_subspan(const Container& offsets, Container& sub_extents) const;
using DataBlock<T>::matrix_subspan;
mdspan<T, Container>matrix_subspan(const size_t row, const size_t col,const size_t tile_rows,const size_t tile_cols)const;
using DataBlock<T>::matrix_column;
mdspan<T, Container>matrix_column(const size_t col_index);
using DataBlock<T>::matrix_row;
mdspan<T, Container>matrix_row(const size_t row_index);
using DataBlock<T>::matrix_transpose;
mdspan<T, Container>matrix_transpose();
using DataBlock<T>::collapsed_view;
mdspan<T, std::vector<size_t>> collapsed_view();
bool device_data_upload(bool default_device,int devicenum=0);
bool device_data_alloc(bool default_device,int devicenum=0);
bool device_data_download_release();
bool device_data_release();
bool host_data_update();
bool device_data_update();
size_t extent(const size_t dim) const
{
return this->dpextents[dim];
};
size_t rank() const
{
return this->dprank;
};
size_t stride(const size_t dim) const
{
return pstrides[dim];
};
// Member function declarations
const Container& extents()const
{
return pextents;
};
const Container& strides()const
{
return pstrides;
};
size_t datalength() const
{
return this->dpdatalength;
};
};
struct dynamic_tag {};
template<size_t Rank>
struct static_tag {};
// Primary template (undefined on purpose)
template<typename Tag>
struct container_for_tag;
// Specialization for dynamic
template<>
struct container_for_tag<dynamic_tag>
{
using type = std::vector<size_t>;
};
// Specialization for static
template<size_t Rank>
struct container_for_tag<static_tag<Rank>>
{
using type = std::array<size_t, Rank>;
};
// Alias template
template<typename T, typename Tag>
using mdspan_t = mdspan<T, typename container_for_tag<Tag>::type>;
template <typename T, typename Container>
mdspan<T,Container>& mdspan<T, Container>:: operator=(const mdspan<T,Container> & other)
{
if(this->dpdata!=other.dpdata)
{
if(p_has_offloaded_host_data)
this->device_data_release();
//does not get copied. Only set to true for upload.
p_has_offloaded_host_data = false;
}
mapping_manager=other.mapping_manager;
pextents = other.pextents;
pstrides = other.pstrides;
this->dpextents = pextents.data();
this->dpstrides = pstrides.data();
this->dpdata = other.dpdata;
this->dpdatalength = other.dpdatalength;
this->dprowmajor = other.dprowmajor;
this->dprank = other.dprank;
this->dpdata_is_devptr = other.dpdata_is_devptr;
this->devptr_devicenum=other.devptr_devicenum;
this->devptr_former_hostptr=other.devptr_former_hostptr;
return *this;
}
template <typename T, typename Container>
mdspan<T, Container>&mdspan<T, Container>::operator=(const DataBlock<T> & other)
{
if(this->dpdata!=other.dpdata)
{
if(p_has_offloaded_host_data)
this->device_data_release();
p_has_offloaded_host_data = false;
this->devptr_devicenum=-1;
this->devptr_former_hostptr=nullptr;
}
this->dpdata = other.dpdata;
this->dpdatalength =other.dpdatalength;
this->dprowmajor = other.dprowmajor;
this->dpdata_is_devptr = other.dpdata_is_devptr;
this->dprank=other.dprank;
if(pextents.size()!=other.dprank)
if constexpr (DynamicContainer<Container>)
pextents.resize(other.dprank);
if(pextents.data()!=other.dpextents)
copy(other.dpextents,other.dpextents+other.dprank,begin(pextents));
if(pstrides.size()!=other.dprank)
if constexpr (DynamicContainer<Container>)
pstrides.resize(other.dprank);
if(pextents.data()!=other.dpstrides)
copy(other.dpstrides,other.dpstrides+other.dprank,begin(pstrides));
this->dpextents = pextents.data();
this->dpstrides = pstrides.data();
return *this;
}
template<typename T, typename Container>
mdspan<T, Container>& mdspan<T, Container>::operator=( mdspan<T, Container>&& other)noexcept
{
if(this->dpdata!=other.dpdata)
{
if(p_has_offloaded_host_data)
this->device_data_release();
}
this->dpdata = other.dpdata;
this->dpdatalength =other.dpdatalength;
this->dprowmajor = other.dprowmajor;
this->dpdata_is_devptr = other.dpdata_is_devptr;
this->dprank=other.dprank;
if constexpr (DynamicContainer<Container>)
{
pextents = std::move(other.pextents);
pstrides = std::move(other.pstrides);
}
if constexpr (StaticContainer<Container>)
{
if(pextents.data()!=other.dpextents)
copy(other.dpextents,other.dpextents+other.dprank,begin(pextents));
if(pextents.data()!=other.dpstrides)
copy(other.dpstrides,other.dpstrides+other.dprank,begin(pstrides));
}
this->dpextents = pextents.data();
this->dpstrides = pstrides.data();
mapping_manager=std::move(other.mapping_manager);
// Move other raw pointers and flags
this->dpdata = other.dpdata;
this->dprowmajor = other.dprowmajor;
this->dprank = other.dprank;
this->dpdata_is_devptr = other.dpdata_is_devptr;
this->dpdatalength = other.dpdatalength;
p_has_offloaded_host_data = other.p_has_offloaded_host_data;
this->devptr_devicenum = other.devptr_devicenum;
this->devptr_former_hostptr = other.devptr_former_hostptr;
other.p_has_offloaded_host_data = false;
other.dpdata = nullptr;
other.dpstrides = nullptr;
other.dpextents = nullptr;
other.devptr_former_hostptr=nullptr;
return *this;
}
template<typename T, typename Container>
mdspan<T, Container>::mdspan(const mdspan<T, Container>& other)
{
// don't take ownership of device memory on copy
p_has_offloaded_host_data = false;
// shared mapping manager (shared_ptr copy)
mapping_manager = other.mapping_manager;
// copy extents/strides container contents
if constexpr (DynamicContainer<Container>)
{
pextents = other.pextents;
pstrides = other.pstrides;
}
if constexpr(StaticContainer<Container>)
{
// only copy actual rank elements
if (pextents.data() != other.dpextents)
std::copy(other.dpextents, other.dpextents + other.dprank, std::begin(pextents));
if (pstrides.data() != other.dpstrides)
std::copy(other.dpstrides, other.dpstrides + other.dprank, std::begin(pstrides));
}
// update raw pointers used by base DataBlock
this->dpextents = pextents.data();
this->dpstrides = pstrides.data();
// copy the underlying pointer/metadata (shallow copy of host/device pointer)
this->dpdata = other.dpdata;
this->dpdatalength = other.dpdatalength;
this->dprowmajor = other.dprowmajor;
this->dprank = other.dprank;
this->dpdata_is_devptr = other.dpdata_is_devptr;
this->devptr_devicenum=other.devptr_devicenum;
this->devptr_former_hostptr=other.devptr_former_hostptr;
}
template <typename T, typename Container>
mdspan<T, Container>::mdspan(mdspan<T, Container>&& other)noexcept
{
this->dpdata = other.dpdata;
this->dpdatalength =other.dpdatalength;
this->dprowmajor = other.dprowmajor;
this->dpdata_is_devptr = other.dpdata_is_devptr;
this->dprank=other.dprank;
if constexpr (DynamicContainer<Container>)
{
pextents = std::move(other.pextents);
pstrides = std::move(other.pstrides);
}
if constexpr (StaticContainer<Container>)
{
if(pextents.data()!=other.dpextents)
copy(other.dpextents,other.dpextents+other.dprank,begin(pextents));
if(pextents.data()!=other.dpstrides)
copy(other.dpstrides,other.dpstrides+other.dprank,begin(pstrides));
}
mapping_manager=std::move(other.mapping_manager);
this->dpextents = pextents.data();
this->dpstrides = pstrides.data();
// Move other raw pointers and flags
this->dpdata = other.dpdata;
this->dprowmajor = other.dprowmajor;
this->dprank = other.dprank;
this->dpdata_is_devptr = other.dpdata_is_devptr;
this->dpdatalength = other.dpdatalength;
p_has_offloaded_host_data = other.p_has_offloaded_host_data;
this->devptr_devicenum = other.devptr_devicenum;
this->devptr_former_hostptr = other.devptr_former_hostptr;
other.p_has_offloaded_host_data = false;
other.dpdata = nullptr;
other.dpstrides = nullptr;
other.dpextents = nullptr;
other.devptr_former_hostptr=nullptr;
}
template <typename T, typename Container>
mdspan<T, Container>::mdspan(const DataBlock<T>&other,const shared_ptr<mdspan<T,Container>::DevicemappingManager>&m )
{
p_has_offloaded_host_data = false;
this->dpdata = other.dpdata;
this->dpdatalength =other.dpdatalength;
this->dprowmajor = other.dprowmajor;
this->dpdata_is_devptr = other.dpdata_is_devptr;
this->dprank=other.dprank;
if(pextents.size()!=other.dprank)
if constexpr (DynamicContainer<Container>)
pextents.resize(other.dprank);
if(pextents.data()!=other.dpextents)
copy(other.dpextents,other.dpextents+other.dprank,begin(pextents));
if(pstrides.size()!=other.dprank)
if constexpr (DynamicContainer<Container>)
pstrides.resize(other.dprank);
if(pextents.data()!=other.dpstrides)
copy(other.dpstrides,other.dpstrides+other.dprank,begin(pstrides));
this->dpextents = pextents.data();
this->dpstrides = pstrides.data();
mapping_manager=m;
this->dpextents = pextents.data();
this->dpstrides = pstrides.data();
// Move other raw pointers and flags
this->dpdata = other.dpdata;
this->dprowmajor = other.dprowmajor;
this->dprank = other.dprank;
this->dpdata_is_devptr = other.dpdata_is_devptr;
this->dpdatalength = other.dpdatalength;
p_has_offloaded_host_data = false;
this->devptr_devicenum = other.devptr_devicenum;
this->devptr_former_hostptr = other.devptr_former_hostptr;
}
template <typename T, typename Container>
mdspan<T, Container>::~mdspan()
{
if(p_has_offloaded_host_data)
this->device_data_release();
}
// Access operator for multidimensional indices
template <typename T, typename Container>
inline T& mdspan<T, Container>::operator()(const Container& indices)
{
size_t offset = 0;
#pragma omp simd reduction( + : offset)
for (size_t i = 0; i < indices.size(); ++i)
{
offset += indices[i] * this->dpstrides[i];
}
return this->dpdata[offset];
}
// Access operator for multidimensional indices
template <typename T, typename Container>
T mdspan<T, Container>::operator()(const Container& indices)const
{
size_t offset = 0;
#pragma omp simd reduction( + : offset)
for (size_t i = 0; i < indices.size(); ++i)
{
offset += indices[i] * this->dpstrides[i];
}
return this->dpdata[offset];
}
template <typename Container>
void compute_strides(const Container& extents, Container& strides,const bool rowmajor)
{
const size_t n = extents.size();
if (n == 0) return;
if constexpr (StaticContainer<Container>)
{
strides = {}; // Default-initialize static container
}
if constexpr (DynamicContainer<Container>)
{
strides.resize(n); // Resize dynamic container
}
if(n==1)
{
strides[0]=1;
return;
}
if (rowmajor)
{
// Row-major layout: last dimension has stride 1
strides[n - 1] = 1;
#pragma omp unroll partial
for (int i =(int) n - 2; i > 0; --i)
{
strides[i] = strides[i + 1] * extents[i + 1];
}
strides[0] = strides[1] * extents[1];
}
else
{
// Column-major layout: first dimension has stride 1
strides[0] = 1;
#pragma omp unroll partial
for (size_t i = 1; i < n; ++i)
{
strides[i] = strides[i - 1] * extents[i - 1];
}
}
}
template <typename T, typename Container>
void mdspan<T, Container>::initialize_extents_and_strides(const Container& extents, const Container& strides)
{
const size_t r = extents.size();
allocate_extents_and_strides(r);
#pragma omp simd
for (size_t i = 0; i < r; ++i)
{
pextents[i] = extents[i];
pstrides[i] = strides[i];
}
// Assign to DataBlock
this->dpextents = pextents.data();
this->dpstrides = pstrides.data();
}
template <typename T, typename Container>
void mdspan<T, Container>::allocate_extents_and_strides(size_t r)
{
if constexpr (StaticContainer<Container>)
{
pextents = {};
pstrides = {};
}
if constexpr (DynamicContainer<Container>)
{
pextents.resize(r);
pstrides.resize(r);
}
this->dpstrides=pstrides.data();
this->dpextents=pextents.data();
}
template <typename T, typename Container>
void mdspan<T, Container>::initialize_extents(const Container& extents)
{
const size_t r = extents.size();
if constexpr (StaticContainer<Container>)
{
pextents = {};
}
if constexpr (DynamicContainer<Container>)
{
pextents.resize(r);
}
#pragma omp simd
for (size_t i = 0; i < r; ++i)
{
pextents[i] = extents[i];
}
// Assign to DataBlock
this->dpextents = pextents.data();
}
template <typename T, typename Container>
mdspan<T, Container>::mdspan(T* data, const size_t datalength, const Container& extents, const Container& strides,const bool rowm,bool dpdata_is_devptr,int devnum)
:DataBlock<T>(data,datalength,rowm,extents.size(),nullptr,nullptr,false,false,dpdata_is_devptr,devnum)
{
initialize_extents_and_strides(extents,strides);
}
template <typename T, typename Container>
mdspan<T, Container>::mdspan(T* data, const Container& extents, const Container& strides,const bool rowm,bool dpdata_is_devptr,int devnum)
: DataBlock<T>(data, 0,rowm,extents.size(),nullptr,nullptr,false,false,dpdata_is_devptr,devnum)
{
initialize_extents_and_strides(extents,strides);
this->dpdatalength=compute_data_length_w(this->dpextents,this->dpstrides,this->dprank);
}
template <typename T, typename Container>
mdspan<T, Container>::mdspan(T* data, const Container& extents,const bool rowm,bool dpdata_is_devptr,int devnum)
: DataBlock<T>(data,0,rowm,extents.size(),nullptr,nullptr,false,false,dpdata_is_devptr,devnum)
{
initialize_extents(extents);
compute_strides(pextents,pstrides,rowm);
this->dpstrides = pstrides.data();
this->dpdatalength=compute_data_length_w(this->dpextents,this->dpstrides,this->dprank);
}
template <typename T, typename Container>
mdspan<T, Container>::mdspan(T* data, const size_t rows, const size_t cols,const bool rowm,bool dpdata_is_devptr,int devnum)
: DataBlock<T>(data,0,rowm,2,nullptr,nullptr,false,false,dpdata_is_devptr,devnum)
{
const size_t r=2;
if constexpr (StaticContainer<Container>)
{
pextents = {}; // Default-initialize static container
}
if constexpr (DynamicContainer<Container>)
{
pextents.resize(r); // Resize dynamic container
}
// Resize and copy extents from container
pextents[0]=rows;
pextents[1]=cols;
compute_strides(pextents,pstrides,rowm);
this->dpextents = pextents.data();
this->dpstrides = pstrides.data();
this->dpdatalength=compute_data_length_w(this->dpextents,this->dpstrides,this->dprank);
}
template <typename T, typename Container>
mdspan<T, Container>::mdspan(T* data, const size_t rows,const bool rowm,bool dpdata_is_devptr,int devnum)
: DataBlock<T>(data,0,rowm,1,nullptr,nullptr,false,false,dpdata_is_devptr,devnum)
{
const size_t r=1;
if constexpr (StaticContainer<Container>)
{
pextents = {}; // Default-initialize static container
}
if constexpr (DynamicContainer<Container>)
{
pextents.resize(r); // Resize dynamic container
}
// Resize and copy extents from container
pextents[0]=rows;
pstrides[0]=1;
this->dpextents = pextents.data();
this->dpstrides = pstrides.data();
this->dpdatalength=rows;
}
template <typename T, typename Container>inline
bool mdspan<T, Container>:: device_data_upload(bool default_device,int devicenum)
{
if (default_device)
devicenum=omp_get_default_device();
if(devicenum>=omp_get_num_devices()) return false;
if(this->dpdata_is_devptr && devicenum==this->devptr_devicenum )return false;
if(mapping_manager==nullptr)
{
mapping_manager = std::make_shared<DevicemappingManager>();
}
if(!mapping_manager->insert(devicenum, (intptr_t)this->dpdata, (intptr_t)(this->dpdata+this->dpdatalength)))return false;
DataBlock_GPU_Memory_Functions<T>::copy_data_to_device_set_devptr(*this,devicenum);
p_has_offloaded_host_data=true;
return true;
}
template <typename T, typename Container>inline
bool mdspan<T, Container>:: device_data_alloc(bool default_device,int devicenum)
{
if (default_device)
devicenum=omp_get_default_device();
if(devicenum>=omp_get_num_devices()) return false;
if(this->dpdata_is_devptr && devicenum==this->devptr_devicenum )return false;
if(mapping_manager==nullptr)
mapping_manager = std::make_shared<DevicemappingManager>();
if(!mapping_manager->insert(devicenum, (intptr_t)this->dpdata, (intptr_t)(this->dpdata+this->dpdatalength)))return false;
DataBlock_GPU_Memory_Functions<T>::alloc_data_to_device_set_devptr(*this,devicenum);
p_has_offloaded_host_data=true;
return true;
}
template <typename T, typename Container>inline
bool mdspan<T, Container>:: device_data_download_release()
{
if(!p_has_offloaded_host_data)return false;
if(mapping_manager==nullptr) return false;
if(!mapping_manager->remove(this->devptr_devicenum, (intptr_t)this->devptr_former_hostptr, (intptr_t)(this->devptr_former_hostptr+this->dpdatalength)))
return false;
DataBlock_GPU_Memory_Functions<T>::copy_data_to_host_set_host_ptr(*this);
p_has_offloaded_host_data=false;
return true;
}
template <typename T, typename Container>inline
bool mdspan<T, Container>:: device_data_release()
{
if(!p_has_offloaded_host_data)return false;
if(mapping_manager==nullptr) return false;
if(!mapping_manager->remove(this->devptr_devicenum, (intptr_t)this->devptr_former_hostptr, (intptr_t)(this->devptr_former_hostptr+this->dpdatalength)))
return false;
DataBlock_GPU_Memory_Functions<T>::free_device_data_set_host_ptr(*this);
p_has_offloaded_host_data=false;
return true;
}
template <typename T, typename Container>inline
bool mdspan<T, Container>:: host_data_update()
{
if(!this->dpdata_is_devptr)return false;
if(this->devptr_former_hostptr==nullptr)return false;
DataBlock_GPU_Memory_Functions<T>::copy_data_to_host_ptr(*this);
return true;
}
template <typename T, typename Container>inline
bool mdspan<T, Container>:: device_data_update()
{
if(!this->dpdata_is_devptr)return false;
if(this->devptr_former_hostptr==nullptr)return false;
DataBlock_GPU_Memory_Functions<T>::copy_data_to_device_ptr(*this);
return true;
}
template <typename T, typename Container>
mdspan<T,std::vector<size_t>> mdspan<T, Container>::collapsed_view()
{
size_t num_dims = this->count_noncollapsed_dims();
size_t *tempext=new size_t[num_dims],
*tempstr=new size_t[num_dims];
mdspan<T, std::vector<size_t>> result(this->collapsed_view(num_dims,tempext, tempstr),mapping_manager);
delete []tempext;
delete []tempstr;
return result;
}
template <typename T, typename Container>
mdspan<T, Container> mdspan<T, Container>::tensor_subspan(const Container&offsets, Container &sub_extents)const
{
size_t *tempstr=new size_t[offsets.size()];
size_t *tempext=new size_t[offsets.size()];
mdspan<T,Container> result( this->tensor_subspan(offsets.data(),sub_extents.data(),tempext, tempstr),mapping_manager);
delete [] tempstr;
delete [] tempext;
return result;
}
template <typename T, typename Container>inline
mdspan<T, Container> mdspan<T, Container>::matrix_subspan(const size_t row, const size_t col,const size_t tile_rows,const size_t tile_cols )const
{
size_t tempext[2], tempstr[2];
mdspan<T,Container> result(this->matrix_subspan(row,col,tile_rows,tile_cols, tempext, tempstr),mapping_manager);
return result;
}
template <typename T, typename Container>
mdspan<T,Container> mdspan<T, Container>:: matrix_row(const size_t row_index)
{
size_t tempext[1], tempstr[1];
mdspan<T,Container> result(this->matrix_row(row_index,tempext, tempstr),mapping_manager);
return result;
}
template <typename T, typename Container>
mdspan<T,Container> mdspan<T, Container>::matrix_column(const size_t column_index)
{
size_t tempext[1], tempstr[1];
mdspan<T,Container> result(this->matrix_column(column_index,tempext, tempstr),mapping_manager);
return result;
}
template <typename T, typename Container>
mdspan<T, Container>mdspan<T, Container>::matrix_transpose()
{
size_t tempext[2], tempstr[2];
mdspan<T,Container> result(matrix_transpose(tempext,tempstr),mapping_manager);
return result;
}
#endif