-
Notifications
You must be signed in to change notification settings - Fork 1
/
lov-e.hpp
1458 lines (1210 loc) · 38.5 KB
/
lov-e.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
// Copyright 2022 Vincent Jacques
// Copyright 2022 Laurent Cabaret
// Documentation and license information for this file are available at https://github.com/jacquev6/lov-e-cuda
#ifndef LOV_E_HPP_
#define LOV_E_HPP_
#include <cuda_runtime.h>
#include <cassert>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <exception>
#include <type_traits>
#ifdef __NVCC__
#define HOST_DEVICE_DECORATORS __host__ __device__
#else
#define HOST_DEVICE_DECORATORS
#endif
/* *
* Error checking utilities *
* */
struct CudaError : public std::exception {
CudaError(const char* const file_, const unsigned line_, const cudaError_t error_) :
file(file_), line(line_), error(error_) {
std::snprintf(
_what, sizeof(_what),
"CUDA ERROR, detected at %s:%i: code %i=%s: %s",
file, line, static_cast<unsigned>(error), cudaGetErrorName(error), cudaGetErrorString(error));
}
const char* what() const noexcept override {
return _what;
}
const char* const file;
const unsigned line;
const cudaError_t error;
private:
char _what[256];
};
// @todo Test the __device__ versions of the error checking functions
HOST_DEVICE_DECORATORS
inline void check_cuda_error_(const cudaError_t error, const char* file, const unsigned line) {
if (error != cudaSuccess) {
#ifdef __CUDA_ARCH__
printf(
"CUDA ERROR, detected at %s:%i: code %i=%s: %s\n",
file, line, static_cast<unsigned int>(error), cudaGetErrorName(error), cudaGetErrorString(error));
#else
throw CudaError(file, line, error);
#endif
}
}
HOST_DEVICE_DECORATORS
inline void check_last_cuda_error_no_sync_(const char* file, const unsigned line) {
check_cuda_error_(cudaGetLastError(), file, line);
}
HOST_DEVICE_DECORATORS
inline void check_last_cuda_error_sync_device_(const char* const file, const unsigned line) {
cudaDeviceSynchronize();
check_last_cuda_error_no_sync_(file, line);
}
HOST_DEVICE_DECORATORS
inline void check_last_cuda_error_sync_stream_(cudaStream_t stream, const char* const file, const unsigned line) {
cudaStreamSynchronize(stream);
check_last_cuda_error_no_sync_(file, line);
}
#define check_cuda_error(e) check_cuda_error_(e, __FILE__, __LINE__)
#define check_last_cuda_error_no_sync() check_last_cuda_error_no_sync_(__FILE__, __LINE__)
#define check_last_cuda_error_sync_stream(stream) check_last_cuda_error_sync_stream_(stream, __FILE__, __LINE__)
#define check_last_cuda_error_sync_device() check_last_cuda_error_sync_device_(__FILE__, __LINE__)
/* *
* Memory management *
* */
class Anywhere {};
class Host {
public:
static const bool can_be_allocated_on_device = false;
private:
template<typename T>
static void force_memset(const std::size_t n, const char v, T* const p) {
std::memset(reinterpret_cast<void*>(p), v, n * sizeof(T));
}
template<typename T>
static T* force_alloc(const std::size_t n) {
if (n == 0) {
return nullptr;
} else {
T* const p = reinterpret_cast<T*>(std::malloc(n * sizeof(T)));
#ifndef NDEBUG
// Attempt to make use of uninitialized memory more noticeable by actually
// initializing it to a large weird-looking value made of a repeated byte.
// The repetition of the byte 0x66 yields the following values:
// - 8-bits integer: 102
// - 16-bits integer: 26214
// - 32-bits integer: 1717986918
// - 64-bits integer: 7378697629483820646
// - IEEE 754 float: 2.72008e+23
// - IEEE 754 double: 1.9035985662552932e+185
force_memset(n, 0x66, p);
#endif
return p;
}
}
public:
template<typename T>
static void memset(const std::size_t n, const char v, T* const p) {
static_assert(std::is_trivial<T>::value, "T must be trivial. Workaround: search for 'trivial' in the doc.");
force_memset<T>(n, v, p);
}
template<typename T>
static void memreset(const std::size_t n, T* const p) {
memset(n, 0, p);
}
template<typename T>
static T* alloc(const std::size_t n) {
static_assert(std::is_trivial<T>::value, "T must be trivial. Workaround: search for 'trivial' in the doc.");
return force_alloc<T>(n);
}
template<typename T>
static T* alloc_zeroed(const std::size_t n) {
T* const p = alloc<T>(n);
memreset(n, p);
return p;
}
template<typename T>
HOST_DEVICE_DECORATORS // Required in ~Array?D
static void free(T* p) {
#ifndef __CUDA_ARCH__
if (p == nullptr) {
return;
} else {
std::free(p);
}
#endif
}
};
class Device {
public:
static const bool can_be_allocated_on_device = true;
private:
template<typename T>
static void force_memset(const std::size_t n, const char v, T* const p) {
check_cuda_error(cudaMemset(p, v, n * sizeof(T)));
}
template<typename T>
HOST_DEVICE_DECORATORS
static T* force_alloc(const std::size_t n) {
if (n == 0) {
return nullptr;
} else {
T* p;
check_cuda_error(cudaMalloc(&p, n * sizeof(T)));
#if !defined(NDEBUG) && !defined(__CUDA_ARCH__)
// See comment in Host::force_alloc
force_memset(n, 0x66, p);
#endif
return p;
}
}
public:
template<typename T>
static void memset(const std::size_t n, const char v, T* const p) {
static_assert(std::is_trivial<T>::value, "T must be trivial. Workaround: search for 'trivial' in the doc.");
force_memset<T>(n, v, p);
}
template<typename T>
static void memreset(const std::size_t n, T* const p) {
memset(n, 0, p);
}
template<typename T>
HOST_DEVICE_DECORATORS
static T* alloc(const std::size_t n) {
static_assert(std::is_trivial<T>::value, "T must be trivial. Workaround: search for 'trivial' in the doc.");
return force_alloc<T>(n);
}
template<typename T>
static T* alloc_zeroed(const std::size_t n) {
T* const p = alloc<T>(n);
memreset(n, p);
return p;
}
template<typename T>
HOST_DEVICE_DECORATORS
static void free(T* p) {
if (p == nullptr) {
return;
} else {
check_cuda_error(cudaFree(p));
}
}
};
template<typename WhereFrom>
struct From {
template<typename WhereTo>
struct To {
template<typename T>
static void copy(const std::size_t n, const T* const src, typename std::remove_const<T>::type* const dst);
template<typename T>
static T* clone(const std::size_t n, const T* const src) {
T* dst = WhereTo::template alloc<T>(n);
copy(n, src, dst);
return dst;
}
};
};
template<> template<> template<typename T>
void From<Host>::To<Host>::copy(
const std::size_t n,
const T* const src,
typename std::remove_const<T>::type* const dst) {
if (n == 0) {
return;
} else {
std::memcpy(dst, src, n * sizeof(T));
}
}
template<> template<> template<typename T>
void From<Host>::To<Device>::copy(
const std::size_t n,
const T* const src,
typename std::remove_const<T>::type* const dst) {
if (n == 0) {
return;
} else {
check_cuda_error(cudaMemcpy(dst, src, n * sizeof(T), cudaMemcpyHostToDevice));
}
}
template<> template<> template<typename T>
void From<Device>::To<Device>::copy(
const std::size_t n,
const T* const src,
typename std::remove_const<T>::type* const dst) {
if (n == 0) {
return;
} else {
check_cuda_error(cudaMemcpy(dst, src, n * sizeof(T), cudaMemcpyDeviceToDevice));
}
}
template<> template<> template<typename T>
void From<Device>::To<Host>::copy(
const std::size_t n,
const T* const src,
typename std::remove_const<T>::type* const dst) {
if (n == 0) {
return;
} else {
check_cuda_error(cudaMemcpy(dst, src, n * sizeof(T), cudaMemcpyDeviceToHost));
}
}
/* *
* Arrays and ArrayViews *
* */
// The 'ArrayView?D' classes have "non-owning pointer" semantics, so they follow the
// [Rule of Zero](http://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#Rc-zero).
// The 'Array?D' classes have "owning pointer" semantics, so they follow the
// [Rule of Five](http://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#Rc-five).
// The one-dimensional case is special because its operator[] actually accesses the underlying memory.
// Trying to access device memory from the host (and reciprocally) fails at run time. We provide
// specializations for each possible 'Where', to decorate (with '__host__' and/or '__device__') the
// 'T& operator[](unsigned i0)' to forbid that *during compilation*. This implies some code duplication,
// but it's worth it.
// Constructor parameters for 'Array?D'
enum Zeroed {zeroed};
enum Uninitialized {uninitialized};
// BEGIN GENERATED SECTION: arrays-and-array-views
template<typename Where, typename T> class Array1D;
template<typename Where, typename T> class ArrayView1D;
template<typename T>
class ArrayView1D<Anywhere, T> {
public:
// Constructor
HOST_DEVICE_DECORATORS
ArrayView1D(std::size_t s0, T* data) :
_s0(s0), _data(data) {}
// No need for custom copy and move constructors and operators (cf. "Rule Of Zero" above)
// Generalized copy constructor and operator
template<typename U>
HOST_DEVICE_DECORATORS
ArrayView1D(const ArrayView1D<Anywhere, U>& o) :
_s0(o.s0()), _data(o.data()) {}
template<typename U>
HOST_DEVICE_DECORATORS
ArrayView1D& operator=(const ArrayView1D<Anywhere, U>& o) {
_s0 = o.s0();
_data = o.data();
return *this;
}
// Generalized conversion operator
template<typename U>
HOST_DEVICE_DECORATORS
operator ArrayView1D<Anywhere, U>() {
return ArrayView1D<Anywhere, U>(_s0, _data);
}
// Accessors
HOST_DEVICE_DECORATORS
std::size_t s0() const { return _s0; }
HOST_DEVICE_DECORATORS
std::size_t total_size() const { return _s0; }
HOST_DEVICE_DECORATORS
T& operator[](unsigned i0) const {
assert(i0 < _s0);
return *(_data + i0);
}
HOST_DEVICE_DECORATORS
T* data() const { return _data; }
private:
std::size_t _s0;
T* _data;
};
template<typename T>
class ArrayView1D<Host, T> {
public:
// Constructor
HOST_DEVICE_DECORATORS
ArrayView1D(std::size_t s0, T* data) :
_s0(s0), _data(data) {}
// No need for custom copy and move constructors and operators (cf. "Rule Of Zero" above)
// Generalized copy constructor and operator
template<typename U>
HOST_DEVICE_DECORATORS
ArrayView1D(const ArrayView1D<Host, U>& o) :
_s0(o.s0()), _data(o.data()) {}
template<typename U>
HOST_DEVICE_DECORATORS
ArrayView1D& operator=(const ArrayView1D<Host, U>& o) {
_s0 = o.s0();
_data = o.data();
return *this;
}
// Generalized conversion operator
template<typename U>
HOST_DEVICE_DECORATORS
operator ArrayView1D<Anywhere, U>() {
return ArrayView1D<Anywhere, U>(_s0, _data);
}
// Accessors
HOST_DEVICE_DECORATORS
std::size_t s0() const { return _s0; }
HOST_DEVICE_DECORATORS
std::size_t total_size() const { return _s0; }
T& operator[](unsigned i0) const {
assert(i0 < _s0);
return *(_data + i0);
}
HOST_DEVICE_DECORATORS
T* data() const { return _data; }
void fill_with_zeros() const {
Host::template memreset<T>(total_size(), data());
}
// Clonable
template<typename WhereTo>
Array1D<WhereTo, typename std::remove_const<T>::type> clone_to() const;
Array1D<Host, typename std::remove_const<T>::type> clone() const { return clone_to<Host>(); }
private:
std::size_t _s0;
T* _data;
friend class Array1D<Host, typename std::remove_const<T>::type>;
};
template<typename T>
class ArrayView1D<Device, T> {
public:
// Constructor
HOST_DEVICE_DECORATORS
ArrayView1D(std::size_t s0, T* data) :
_s0(s0), _data(data) {}
// No need for custom copy and move constructors and operators (cf. "Rule Of Zero" above)
// Generalized copy constructor and operator
template<typename U>
HOST_DEVICE_DECORATORS
ArrayView1D(const ArrayView1D<Device, U>& o) :
_s0(o.s0()), _data(o.data()) {}
template<typename U>
HOST_DEVICE_DECORATORS
ArrayView1D& operator=(const ArrayView1D<Device, U>& o) {
_s0 = o.s0();
_data = o.data();
return *this;
}
// Generalized conversion operator
template<typename U>
HOST_DEVICE_DECORATORS
operator ArrayView1D<Anywhere, U>() {
return ArrayView1D<Anywhere, U>(_s0, _data);
}
// Accessors
HOST_DEVICE_DECORATORS
std::size_t s0() const { return _s0; }
HOST_DEVICE_DECORATORS
std::size_t total_size() const { return _s0; }
#ifdef __NVCC__
__device__
T& operator[](unsigned i0) const {
assert(i0 < _s0);
return *(_data + i0);
}
#endif
HOST_DEVICE_DECORATORS
T* data() const { return _data; }
void fill_with_zeros() const {
Device::template memreset<T>(total_size(), data());
}
// Clonable
template<typename WhereTo>
Array1D<WhereTo, typename std::remove_const<T>::type> clone_to() const;
Array1D<Device, typename std::remove_const<T>::type> clone() const { return clone_to<Device>(); }
private:
std::size_t _s0;
T* _data;
friend class Array1D<Device, typename std::remove_const<T>::type>;
};
template<typename WhereFrom, typename WhereTo, typename T>
void copy(ArrayView1D<WhereFrom, T> src, ArrayView1D<WhereTo, typename std::remove_const<T>::type> dst) {
assert(dst.s0() == src.s0());
From<WhereFrom>::template To<WhereTo>::template copy(
src.s0(), src.data(), dst.data());
}
template<typename Where, typename T>
class Array1D : public ArrayView1D<Where, const T> {
public:
// RAII
template<typename W = Where, typename = typename std::enable_if<!W::can_be_allocated_on_device>::type>
Array1D(std::size_t s0, Uninitialized) :
ArrayView1D<Where, const T>(s0, Where::template alloc<T>(s0))
{}
template<
typename = void,
typename W = Where,
typename = typename std::enable_if<W::can_be_allocated_on_device>::type
>
HOST_DEVICE_DECORATORS
Array1D(std::size_t s0, Uninitialized) :
ArrayView1D<Where, const T>(s0, Where::template alloc<T>(s0))
{}
Array1D(std::size_t s0, Zeroed) :
ArrayView1D<Where, const T>(s0, Where::template alloc_zeroed<T>(s0))
{}
HOST_DEVICE_DECORATORS
~Array1D() {
free();
}
// Accessors
HOST_DEVICE_DECORATORS
T* data() const { return const_cast<T*>(this->_data); }
// @todo Fix decorators: forbid dereferencing host memory on host and vice versa
HOST_DEVICE_DECORATORS
T& operator[](unsigned i0) const {
assert(i0 < this->_s0);
return *(data() + i0);
}
void fill_with_zeros() const {
Where::template memreset<T>(this->total_size(), data());
}
// Not copyable
Array1D(const Array1D&) = delete;
Array1D& operator=(const Array1D&) = delete;
// But movable
HOST_DEVICE_DECORATORS
Array1D(Array1D&& o) : ArrayView1D<Where, const T>(o) {
o._s0 = 0;
o._data = nullptr;
}
HOST_DEVICE_DECORATORS
Array1D& operator=(Array1D&& o) {
free();
static_cast<ArrayView1D<Where, const T>&>(*this) = o;
o._s0 = 0;
o._data = nullptr;
return *this;
}
private:
HOST_DEVICE_DECORATORS
void free() {
Where::free(data());
}
};
template<typename Where, typename T>
HOST_DEVICE_DECORATORS
ArrayView1D<Where, T> ref(const Array1D<Where, T>& a) {
return ArrayView1D<Where, T>(a.s0(), a.data());
}
template<typename Where, typename T>
HOST_DEVICE_DECORATORS
ArrayView1D<Where, T> ref(const ArrayView1D<Where, T>& a) {
return a;
}
template<typename T>
template<typename WhereTo>
Array1D<WhereTo, typename std::remove_const<T>::type> ArrayView1D<Host, T>::clone_to() const {
Array1D<WhereTo, typename std::remove_const<T>::type> dst(this->s0(), uninitialized);
copy(*this, ref(dst)); // NOLINT(build/include_what_you_use)
return dst;
}
template<typename T>
template<typename WhereTo>
Array1D<WhereTo, typename std::remove_const<T>::type> ArrayView1D<Device, T>::clone_to() const {
Array1D<WhereTo, typename std::remove_const<T>::type> dst(this->s0(), uninitialized);
copy(*this, ref(dst)); // NOLINT(build/include_what_you_use)
return dst;
}
template<typename Where, typename T> class Array2D;
template<typename Where, typename T>
class ArrayView2D {
public:
// Constructor
HOST_DEVICE_DECORATORS
ArrayView2D(std::size_t s1, std::size_t s0, T* data) :
_s1(s1), _s0(s0), _data(data) {}
// No need for custom copy and move constructors and operators (cf. "Rule Of Zero" above)
// Generalized copy constructor and operator
template<typename U>
HOST_DEVICE_DECORATORS
ArrayView2D(const ArrayView2D<Where, U>& o) :
_s1(o.s1()), _s0(o.s0()), _data(o.data()) {}
template<typename U>
HOST_DEVICE_DECORATORS
ArrayView2D& operator=(const ArrayView2D<Where, U>& o) {
_s1 = o.s1();
_s0 = o.s0();
_data = o.data();
return *this;
}
// Generalized conversion operator
template<typename U>
HOST_DEVICE_DECORATORS
operator ArrayView2D<Anywhere, U>() {
return ArrayView2D<Anywhere, U>(_s1, _s0, _data);
}
// Accessors
HOST_DEVICE_DECORATORS
std::size_t s1() const { return _s1; }
HOST_DEVICE_DECORATORS
std::size_t s0() const { return _s0; }
HOST_DEVICE_DECORATORS
std::size_t total_size() const { return _s1 * _s0; }
HOST_DEVICE_DECORATORS
ArrayView1D<Where, T> operator[](unsigned i1) const {
assert(i1 < _s1);
return ArrayView1D<Where, T>(_s0, _data + i1 * _s0);
}
HOST_DEVICE_DECORATORS
T* data() const { return _data; }
void fill_with_zeros() const {
Where::template memreset<T>(total_size(), data());
}
// Clonable
template<typename WhereTo>
Array2D<WhereTo, typename std::remove_const<T>::type> clone_to() const;
Array2D<Where, typename std::remove_const<T>::type> clone() const { return clone_to<Where>(); }
private:
std::size_t _s1;
std::size_t _s0;
T* _data;
friend class Array2D<Where, typename std::remove_const<T>::type>;
};
template<typename WhereFrom, typename WhereTo, typename T>
void copy(ArrayView2D<WhereFrom, T> src, ArrayView2D<WhereTo, typename std::remove_const<T>::type> dst) {
assert(dst.s1() == src.s1());
assert(dst.s0() == src.s0());
From<WhereFrom>::template To<WhereTo>::template copy(
src.s1() * src.s0(), src.data(), dst.data());
}
template<typename Where, typename T>
class Array2D : public ArrayView2D<Where, const T> {
public:
// RAII
template<typename W = Where, typename = typename std::enable_if<!W::can_be_allocated_on_device>::type>
Array2D(std::size_t s1, std::size_t s0, Uninitialized) :
ArrayView2D<Where, const T>(s1, s0, Where::template alloc<T>(s1 * s0))
{}
template<
typename = void,
typename W = Where,
typename = typename std::enable_if<W::can_be_allocated_on_device>::type
>
HOST_DEVICE_DECORATORS
Array2D(std::size_t s1, std::size_t s0, Uninitialized) :
ArrayView2D<Where, const T>(s1, s0, Where::template alloc<T>(s1 * s0))
{}
Array2D(std::size_t s1, std::size_t s0, Zeroed) :
ArrayView2D<Where, const T>(s1, s0, Where::template alloc_zeroed<T>(s1 * s0))
{}
HOST_DEVICE_DECORATORS
~Array2D() {
free();
}
// Accessors
HOST_DEVICE_DECORATORS
T* data() const { return const_cast<T*>(this->_data); }
HOST_DEVICE_DECORATORS
ArrayView1D<Where, T> operator[](unsigned i1) const {
assert(i1 < this->_s1);
return ArrayView1D<Where, T>(
this->_s0, data() + i1 * this->_s0);
}
void fill_with_zeros() const {
Where::template memreset<T>(this->total_size(), data());
}
// Not copyable
Array2D(const Array2D&) = delete;
Array2D& operator=(const Array2D&) = delete;
// But movable
HOST_DEVICE_DECORATORS
Array2D(Array2D&& o) : ArrayView2D<Where, const T>(o) {
o._s1 = 0;
o._s0 = 0;
o._data = nullptr;
}
HOST_DEVICE_DECORATORS
Array2D& operator=(Array2D&& o) {
free();
static_cast<ArrayView2D<Where, const T>&>(*this) = o;
o._s1 = 0;
o._s0 = 0;
o._data = nullptr;
return *this;
}
private:
HOST_DEVICE_DECORATORS
void free() {
Where::free(data());
}
};
template<typename Where, typename T>
HOST_DEVICE_DECORATORS
ArrayView2D<Where, T> ref(const Array2D<Where, T>& a) {
return ArrayView2D<Where, T>(a.s1(), a.s0(), a.data());
}
template<typename Where, typename T>
HOST_DEVICE_DECORATORS
ArrayView2D<Where, T> ref(const ArrayView2D<Where, T>& a) {
return a;
}
template<typename WhereFrom, typename T>
template<typename WhereTo>
Array2D<WhereTo, typename std::remove_const<T>::type> ArrayView2D<WhereFrom, T>::clone_to() const {
Array2D<WhereTo, typename std::remove_const<T>::type> dst(
this->s1(), this->s0(), uninitialized);
copy(*this, ref(dst)); // NOLINT(build/include_what_you_use)
return dst;
}
template<typename Where, typename T> class Array3D;
template<typename Where, typename T>
class ArrayView3D {
public:
// Constructor
HOST_DEVICE_DECORATORS
ArrayView3D(std::size_t s2, std::size_t s1, std::size_t s0, T* data) :
_s2(s2), _s1(s1), _s0(s0), _data(data) {}
// No need for custom copy and move constructors and operators (cf. "Rule Of Zero" above)
// Generalized copy constructor and operator
template<typename U>
HOST_DEVICE_DECORATORS
ArrayView3D(const ArrayView3D<Where, U>& o) :
_s2(o.s2()), _s1(o.s1()), _s0(o.s0()), _data(o.data()) {}
template<typename U>
HOST_DEVICE_DECORATORS
ArrayView3D& operator=(const ArrayView3D<Where, U>& o) {
_s2 = o.s2();
_s1 = o.s1();
_s0 = o.s0();
_data = o.data();
return *this;
}
// Generalized conversion operator
template<typename U>
HOST_DEVICE_DECORATORS
operator ArrayView3D<Anywhere, U>() {
return ArrayView3D<Anywhere, U>(_s2, _s1, _s0, _data);
}
// Accessors
HOST_DEVICE_DECORATORS
std::size_t s2() const { return _s2; }
HOST_DEVICE_DECORATORS
std::size_t s1() const { return _s1; }
HOST_DEVICE_DECORATORS
std::size_t s0() const { return _s0; }
HOST_DEVICE_DECORATORS
std::size_t total_size() const { return _s2 * _s1 * _s0; }
HOST_DEVICE_DECORATORS
ArrayView2D<Where, T> operator[](unsigned i2) const {
assert(i2 < _s2);
return ArrayView2D<Where, T>(_s1, _s0, _data + i2 * _s1 * _s0);
}
HOST_DEVICE_DECORATORS
T* data() const { return _data; }
void fill_with_zeros() const {
Where::template memreset<T>(total_size(), data());
}
// Clonable
template<typename WhereTo>
Array3D<WhereTo, typename std::remove_const<T>::type> clone_to() const;
Array3D<Where, typename std::remove_const<T>::type> clone() const { return clone_to<Where>(); }
private:
std::size_t _s2;
std::size_t _s1;
std::size_t _s0;
T* _data;
friend class Array3D<Where, typename std::remove_const<T>::type>;
};
template<typename WhereFrom, typename WhereTo, typename T>
void copy(ArrayView3D<WhereFrom, T> src, ArrayView3D<WhereTo, typename std::remove_const<T>::type> dst) {
assert(dst.s2() == src.s2());
assert(dst.s1() == src.s1());
assert(dst.s0() == src.s0());
From<WhereFrom>::template To<WhereTo>::template copy(
src.s2() * src.s1() * src.s0(), src.data(), dst.data());
}
template<typename Where, typename T>
class Array3D : public ArrayView3D<Where, const T> {
public:
// RAII
template<typename W = Where, typename = typename std::enable_if<!W::can_be_allocated_on_device>::type>
Array3D(std::size_t s2, std::size_t s1, std::size_t s0, Uninitialized) :
ArrayView3D<Where, const T>(s2, s1, s0, Where::template alloc<T>(s2 * s1 * s0))
{}
template<
typename = void,
typename W = Where,
typename = typename std::enable_if<W::can_be_allocated_on_device>::type
>
HOST_DEVICE_DECORATORS
Array3D(std::size_t s2, std::size_t s1, std::size_t s0, Uninitialized) :
ArrayView3D<Where, const T>(s2, s1, s0, Where::template alloc<T>(s2 * s1 * s0))
{}
Array3D(std::size_t s2, std::size_t s1, std::size_t s0, Zeroed) :
ArrayView3D<Where, const T>(s2, s1, s0, Where::template alloc_zeroed<T>(s2 * s1 * s0))
{}
HOST_DEVICE_DECORATORS
~Array3D() {
free();
}
// Accessors
HOST_DEVICE_DECORATORS
T* data() const { return const_cast<T*>(this->_data); }
HOST_DEVICE_DECORATORS
ArrayView2D<Where, T> operator[](unsigned i2) const {
assert(i2 < this->_s2);
return ArrayView2D<Where, T>(
this->_s1, this->_s0, data() + i2 * this->_s1 * this->_s0);
}
void fill_with_zeros() const {
Where::template memreset<T>(this->total_size(), data());
}
// Not copyable
Array3D(const Array3D&) = delete;
Array3D& operator=(const Array3D&) = delete;
// But movable
HOST_DEVICE_DECORATORS
Array3D(Array3D&& o) : ArrayView3D<Where, const T>(o) {
o._s2 = 0;
o._s1 = 0;
o._s0 = 0;
o._data = nullptr;
}
HOST_DEVICE_DECORATORS
Array3D& operator=(Array3D&& o) {
free();
static_cast<ArrayView3D<Where, const T>&>(*this) = o;
o._s2 = 0;
o._s1 = 0;
o._s0 = 0;
o._data = nullptr;
return *this;
}
private:
HOST_DEVICE_DECORATORS
void free() {
Where::free(data());
}
};
template<typename Where, typename T>
HOST_DEVICE_DECORATORS
ArrayView3D<Where, T> ref(const Array3D<Where, T>& a) {
return ArrayView3D<Where, T>(a.s2(), a.s1(), a.s0(), a.data());
}
template<typename Where, typename T>
HOST_DEVICE_DECORATORS
ArrayView3D<Where, T> ref(const ArrayView3D<Where, T>& a) {
return a;
}
template<typename WhereFrom, typename T>
template<typename WhereTo>
Array3D<WhereTo, typename std::remove_const<T>::type> ArrayView3D<WhereFrom, T>::clone_to() const {
Array3D<WhereTo, typename std::remove_const<T>::type> dst(
this->s2(), this->s1(), this->s0(), uninitialized);
copy(*this, ref(dst)); // NOLINT(build/include_what_you_use)
return dst;
}
template<typename Where, typename T> class Array4D;
template<typename Where, typename T>
class ArrayView4D {
public:
// Constructor
HOST_DEVICE_DECORATORS
ArrayView4D(std::size_t s3, std::size_t s2, std::size_t s1, std::size_t s0, T* data) :
_s3(s3), _s2(s2), _s1(s1), _s0(s0), _data(data) {}
// No need for custom copy and move constructors and operators (cf. "Rule Of Zero" above)
// Generalized copy constructor and operator
template<typename U>
HOST_DEVICE_DECORATORS
ArrayView4D(const ArrayView4D<Where, U>& o) :
_s3(o.s3()), _s2(o.s2()), _s1(o.s1()), _s0(o.s0()), _data(o.data()) {}
template<typename U>
HOST_DEVICE_DECORATORS
ArrayView4D& operator=(const ArrayView4D<Where, U>& o) {
_s3 = o.s3();
_s2 = o.s2();
_s1 = o.s1();
_s0 = o.s0();
_data = o.data();
return *this;
}
// Generalized conversion operator
template<typename U>
HOST_DEVICE_DECORATORS
operator ArrayView4D<Anywhere, U>() {
return ArrayView4D<Anywhere, U>(_s3, _s2, _s1, _s0, _data);
}
// Accessors
HOST_DEVICE_DECORATORS
std::size_t s3() const { return _s3; }
HOST_DEVICE_DECORATORS
std::size_t s2() const { return _s2; }
HOST_DEVICE_DECORATORS
std::size_t s1() const { return _s1; }
HOST_DEVICE_DECORATORS
std::size_t s0() const { return _s0; }
HOST_DEVICE_DECORATORS