-
Notifications
You must be signed in to change notification settings - Fork 1
/
core.hpp
9840 lines (9397 loc) · 461 KB
/
core.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
/**
* @file core.hpp
* @author Wuqiong Zhao ([email protected]), et al.
* @brief Core Utilities for FLAMES
* @version 0.1.0
* @date 2024-11-04
*
* @copyright Copyright (c) 2022-2024 Wuqiong Zhao
*
*/
#ifndef _FLAMES_CORE_HPP_
#define _FLAMES_CORE_HPP_
#include <ap_fixed.h>
#include <ap_int.h>
#include <cassert>
#include <complex>
#include <cstddef>
#include <fstream>
#include <hls_vector.h>
#include <initializer_list>
#include <iostream>
#include <type_traits>
#include <vector>
#ifndef __VITIS_HLS__
# error "FLAMES library can only be used for Vitis HLS."
#endif
/*
* There are some unwanted warning messages, including:
* WARNING: [HLS 207-1462] template template parameter using 'typename' is a C++17 extension
* WARNING: [HLS 207-5292] unused parameter '...'
* They are safe to ignore, so all warnings of the FLAMES library are suppressed.
* You can restore them by defining `FLAMES_PRESERVE_WARNING`
*/
#ifndef FLAMES_KEEP_WARNING
# ifdef __SYNTHESIS__
# pragma GCC diagnostic push
# pragma GCC diagnostic ignored "-Weverything"
# endif
#endif
#ifndef PRAGMA_SUB
# define PRAGMA_SUB(x) _Pragma(#x)
#endif
#ifndef FLAMES_PRAGMA
// Alias for #pragma HLS with support for macro expansion.
# define FLAMES_PRAGMA(x) PRAGMA_SUB(HLS x)
#endif
#ifndef FLAMES_MAT_PLUS_UNROLL_FACTOR
# ifdef FLAMES_UNROLL_FACTOR
# define FLAMES_MAT_PLUS_UNROLL_FACTOR FLAMES_UNROLL_FACTOR
# else
# define FLAMES_MAT_PLUS_UNROLL_FACTOR 32
# endif
#endif
#ifndef FLAMES_MAT_MINUS_UNROLL_FACTOR
# ifdef FLAMES_UNROLL_FACTOR
# define FLAMES_MAT_MINUS_UNROLL_FACTOR FLAMES_UNROLL_FACTOR
# else
# define FLAMES_MAT_MINUS_UNROLL_FACTOR 32
# endif
#endif
#ifndef FLAMES_MAT_SET_VALUE_UNROLL_FACTOR
# ifdef FLAMES_UNROLL_FACTOR
# define FLAMES_MAT_SET_VALUE_UNROLL_FACTOR FLAMES_UNROLL_FACTOR
# else
# define FLAMES_MAT_SET_VALUE_UNROLL_FACTOR 32
# endif
#endif
#ifndef FLAMES_MAT_POWER_UNROLL_FACTOR
# ifdef FLAMES_UNROLL_FACTOR
# define FLAMES_MAT_POWER_UNROLL_FACTOR FLAMES_UNROLL_FACTOR
# else
# define FLAMES_MAT_POWER_UNROLL_FACTOR 32
# endif
#endif
#ifndef FLAMES_MAT_COPY_UNROLL_FACTOR
# ifdef FLAMES_UNROLL_FACTOR
# define FLAMES_MAT_COPY_UNROLL_FACTOR FLAMES_UNROLL_FACTOR
# else
# define FLAMES_MAT_COPY_UNROLL_FACTOR 32
# endif
#endif
#ifndef FLAMES_MAT_SCALAR_TIMES_UNROLL_FACTOR
# ifdef FLAMES_MAT_TIMES_UNROLL_FACTOR
# define FLAMES_MAT_SCALAR_TIMES_UNROLL_FACTOR FLAMES_MAT_TIMES_UNROLL_FACTOR
# else
# ifdef FLAMES_UNROLL_FACTOR
# define FLAMES_MAT_SCALAR_TIMES_UNROLL_FACTOR FLAMES_UNROLL_FACTOR
# else
# define FLAMES_MAT_SCALAR_TIMES_UNROLL_FACTOR 32
# endif
# endif
#endif
#ifndef FLAMES_MAT_TIMES_UNROLL_FACTOR
# ifdef FLAMES_UNROLL_FACTOR
# define FLAMES_MAT_TIMES_UNROLL_FACTOR FLAMES_UNROLL_FACTOR
# else
# define FLAMES_MAT_TIMES_UNROLL_FACTOR 32
# endif
#endif
#ifndef FLAMES_MAT_EMUL_UNROLL_FACTOR
# ifdef FLAMES_UNROLL_FACTOR
# define FLAMES_MAT_EMUL_UNROLL_FACTOR FLAMES_UNROLL_FACTOR
# else
# define FLAMES_MAT_EMUL_UNROLL_FACTOR 32
# endif
#endif
#ifndef FLAMES_MAT_BOOL_OPER_UNROLL_FACTOR
# ifdef FLAMES_UNROLL_FACTOR
# define FLAMES_MAT_BOOL_OPER_UNROLL_FACTOR FLAMES_UNROLL_FACTOR
# else
# define FLAMES_MAT_BOOL_OPER_UNROLL_FACTOR 32
# endif
#endif
#ifndef FLAMES_MAT_ABS_UNROLL_FACTOR
# ifdef FLAMES_UNROLL_FACTOR
# define FLAMES_MAT_ABS_UNROLL_FACTOR FLAMES_UNROLL_FACTOR
# else
# define FLAMES_MAT_ABS_UNROLL_FACTOR 32
# endif
#endif
#ifndef FLAMES_MAT_TRANSPOSE_UNROLL_FACTOR
# ifdef FLAMES_UNROLL_FACTOR
# define FLAMES_MAT_TRANSPOSE_UNROLL_FACTOR FLAMES_UNROLL_FACTOR
# else
# define FLAMES_MAT_TRANSPOSE_UNROLL_FACTOR 32
# endif
#endif
#ifndef FLAMES_MAT_INV_UNROLL_FACTOR
# ifdef FLAMES_UNROLL_FACTOR
# define FLAMES_MAT_INV_UNROLL_FACTOR FLAMES_UNROLL_FACTOR
# else
# define FLAMES_MAT_INV_UNROLL_FACTOR 32
# endif
#endif
#ifndef FLAMES_MAT_PARTITION_COMPLETE
# ifndef FLAMES_MAT_PARTITION_FACTOR
# define FLAMES_MAT_PARTITION_FACTOR 8
# endif
#endif
#ifndef FLAMES_SORT_PARTITION_COMPLETE
# ifndef FLAMES_SORT_PARTITION_FACTOR
# define FLAMES_SORT_PARTITION_FACTOR 32
# endif
#endif
#if defined __SYNTHESIS__ && defined FLAMES_PRINT_PER_MAT_COPY
# undef FLAMES_PRINT_PER_MAT_COPY
#endif
#ifdef INLINE
# define DEFINED_INLINE
# undef INLINE
#endif
/**
* @brief Namespace for the FLAMES library.
*
* @details When you include 'flames.hpp', this namespace is automatically used.
*/
namespace flames {
/**
* @brief Matrix type for storage.
*
*/
enum MatType {
NORMAL, /**< Normal matrix */
DIAGONAL, /**< Diagonal matrix */
SCALAR, /**< Scalar matrix */
UPPER, /**< Upper triangular matrix */
LOWER, /**< Lower triangular matrix */
SUPPER, /**< Strict Upper triangular matrix */
SLOWER, /**< Strict Lower triangular matrix */
SYM, /**< Symmetrical matrix */
ASYM /**< Antisymmetrical matrix */
};
/// Normal matrix as a class type.
using MATTYPE_NORMAL = std::integral_constant<int, MatType::NORMAL>;
/// Diagonal matrix as a class type.
using MATTYPE_DIAGONAL = std::integral_constant<int, MatType::DIAGONAL>;
/// Scalar matrix matrix as a class type.
using MATTYPE_SCALAR = std::integral_constant<int, MatType::SCALAR>;
/// Upper triangular matrix as a class type.
using MATTYPE_UPPER = std::integral_constant<int, MatType::UPPER>;
/// Lower triangular matrix as a class type.
using MATTYPE_LOWER = std::integral_constant<int, MatType::LOWER>;
/// Strict upper triangular matrix as a class type.
using MATTYPE_SUPPER = std::integral_constant<int, MatType::SUPPER>;
/// Strict lower triangular matrix as a class type.
using MATTYPE_SLOWER = std::integral_constant<int, MatType::SLOWER>;
/// Symmetrical matrix as a class type.
using MATTYPE_SYM = std::integral_constant<int, MatType::SYM>;
/// Antisymmetrical matrix as a class type.
using MATTYPE_ASYM = std::integral_constant<int, MatType::ASYM>;
/**
* @brief Get the MatType value from the class form.
*
* @tparam T The MatType class form.
* @return (constexpr MatType) The MatType value (as an enum item).
*/
template <typename T>
inline constexpr MatType matType() {
return MatType(T::value);
}
/**
* @brief Get the class form of MatType.
*
* @tparam type The MatType enum item value.
*/
template <int type>
using MType = std::integral_constant<int, type>;
/**
* @brief Summation type of two matrices.
*
* @param type1 The MatType of the left matrix.
* @param type2 The MatType of the right matrix.
* @return (constexpr MatType) The summation matrix type.
*/
inline constexpr MatType sumType(MatType type1, MatType type2) noexcept {
if (type1 == type2) return type1;
else if ((type1 == MatType::DIAGONAL && type2 == MatType::SCALAR) ||
(type1 == MatType::SCALAR && type2 == MatType::DIAGONAL))
return MatType::DIAGONAL;
else if ((type1 == MatType::DIAGONAL && type2 == MatType::UPPER) ||
(type1 == MatType::UPPER && type2 == MatType::DIAGONAL))
return MatType::UPPER;
else if ((type1 == MatType::DIAGONAL && type2 == MatType::LOWER) ||
(type1 == MatType::LOWER && type2 == MatType::DIAGONAL))
return MatType::LOWER;
else if ((type1 == MatType::DIAGONAL && type2 == MatType::SUPPER) ||
(type1 == MatType::SUPPER && type2 == MatType::DIAGONAL))
return MatType::UPPER;
else if ((type1 == MatType::DIAGONAL && type2 == MatType::SLOWER) ||
(type1 == MatType::SLOWER && type2 == MatType::DIAGONAL))
return MatType::LOWER;
else if ((type1 == MatType::SCALAR && type2 == MatType::SUPPER) ||
(type1 == MatType::SUPPER && type2 == MatType::SCALAR))
return MatType::UPPER;
else if ((type1 == MatType::SCALAR && type2 == MatType::SLOWER) ||
(type1 == MatType::SLOWER && type2 == MatType::SCALAR))
return MatType::LOWER;
else if ((type1 == MatType::SUPPER && type2 == MatType::UPPER) ||
(type1 == MatType::UPPER && type2 == MatType::SUPPER))
return MatType::UPPER;
else if ((type1 == MatType::SLOWER && type2 == MatType::LOWER) ||
(type1 == MatType::LOWER && type2 == MatType::SLOWER))
return MatType::LOWER;
else if ((type1 == MatType::DIAGONAL && type2 == MatType::SYM) ||
(type1 == MatType::SYM && type2 == MatType::DIAGONAL))
return MatType::SYM;
else if ((type1 == MatType::SCALAR && type2 == MatType::SYM) || (type1 == MatType::SYM && type2 == MatType::SCALAR))
return MatType::SYM;
else return MatType::NORMAL;
}
/**
* @brief Multiplication type of two matrices.
*
* @param type1 The MatType of the left matrix.
* @param type2 The MatType of the right matrix.
* @param n_rows The number of rows of the left matrix.
* @param comm The number of columns of the left matrix and the number of rows of the right matrix.
* @param n_cols The number of columns of the right matrix.
* @return (constexpr MatType) The multiplication matrix type.
*/
inline constexpr MatType mulType(MatType type1, MatType type2, size_t n_rows, size_t comm, size_t n_cols) noexcept {
if (n_rows == comm && comm == n_cols) {
if (type1 == type2) {
if (type1 != SYM && type1 != ASYM) return type1;
}
if (type1 == MatType::SCALAR) return type2;
else if (type2 == MatType::SCALAR) return type1;
else if (type1 == MatType::DIAGONAL && (type2 == MatType::SUPPER || type2 == MatType::UPPER ||
type2 == MatType::SLOWER || type2 == MatType::LOWER))
return type2;
else if (type2 == MatType::DIAGONAL && (type1 == MatType::SUPPER || type1 == MatType::UPPER ||
type1 == MatType::SLOWER || type1 == MatType::LOWER))
return type1;
else if (type1 == MatType::SUPPER && type2 == MatType::UPPER) return type1;
else if (type1 == MatType::SLOWER && type2 == MatType::LOWER) return type1;
else if (type1 == MatType::UPPER && type2 == MatType::SUPPER) return type2;
else if (type1 == MatType::LOWER && type2 == MatType::SLOWER) return type2;
}
return MatType::NORMAL;
}
/**
* @brief Transpose type of a matrix.
*
* @param type The MatType of the matrix.
* @return (constexpr MatType) The transpose matrix type.
*/
inline constexpr MatType tType(MatType type) noexcept {
if (type == MatType::SUPPER) return MatType::SLOWER;
else if (type == MatType::SLOWER) return MatType::SUPPER;
else if (type == MatType::UPPER) return MatType::LOWER;
else if (type == MatType::LOWER) return MatType::UPPER;
else return type;
}
/**
* @brief Calculate the row index of a upper triangular matrix.
*
* @param index The data index.
* @param N The matrix dimension.
* @return (constexpr size_t) The row index.
*/
inline constexpr size_t upperRow(size_t index, size_t N) {
size_t r = 0;
while (index >= N - r) {
index -= N - r;
++r;
}
return r;
}
/**
* @brief Calculate the row index of a lower triangular matrix.
*
* @param index The data index.
* @param N The matrix dimension.
* @return (constexpr size_t) The row index.
*/
inline constexpr size_t lowerRow(size_t index, size_t N) {
size_t r = 0;
while (index >= r + 1) {
index -= r + 1;
++r;
}
return r;
}
/**
* @brief Calculate the row index of a strict upper triangular matrix.
*
* @param index The data index.
* @param N The matrix dimension.
* @return (constexpr size_t) The row index.
*/
inline constexpr size_t supperRow(size_t index, size_t N) {
size_t r = 0;
while (index >= N - 1 - r) {
index -= N - 1 - r;
++r;
}
return r;
}
/**
* @brief Calculate the row index of a strict lower triangular matrix.
*
* @param index The data index.
* @param N The matrix dimension.
* @return (constexpr size_t) The row index.
*/
inline constexpr size_t slowerRow(size_t index, size_t N) {
size_t r = 0;
while (index >= r) {
index -= r;
++r;
}
return r;
}
/**
* @brief Matrix.
*
* @details The matrix design is hardware optimized,
* with easy pragma configuration to be specified by the user.
* The use of this class follows the routine of C++,
* with some exceptions for the sake of better hardware performance.
* Please check out descriptions of class member functions for details.
* @tparam T Element type.
* @tparam n_rows Number of rows.
* @tparam n_cols Number of columns.
* @tparam type matrix type.
*/
template <typename T, size_t n_rows, size_t n_cols, MatType type = MatType::NORMAL>
class Mat;
/**
* @brief Tensor.
*
* @details 3-D data structure.
* @tparam T Element type.
* @tparam n_rows Number of rows.
* @tparam n_cols Number of columns.
* @tparam n_slices Number of slices.
* @tparam type matrix type.
*/
template <typename T, size_t n_rows, size_t n_cols, size_t n_slices, MatType type = MatType::NORMAL>
class Tensor;
/**
* @brief Column vector.
*
* @details This is the type alias of Mat and the number of columns is set to 1.
* @tparam T Element type.
* @tparam N The vector dimension.
*/
template <typename T, size_t N>
using Vec = Mat<T, N, 1>;
/**
* @brief Row vector.
*
* @details This is the type alias of Mat and the number of row is set to 1.
* @tparam T Element type.
* @tparam N The vector dimension.
*/
template <typename T, size_t N>
using RowVec = Mat<T, 1, N>;
/**
* @brief Read only view version of a matrix.
*
* @tparam T Element type.
* @tparam n_rows Number of rows.
* @tparam n_cols Number of columns.
* @tparam type Matrix type.
*/
template <typename T, size_t n_rows, size_t n_cols, MatType type = MatType::NORMAL>
class MatView;
template <typename T, size_t n_rows, size_t n_cols, MatType type = MatType::NORMAL>
class MatRef;
/**
* @brief Read only view version of the opposite of a matrix.
*
* @tparam T Element type.
* @tparam n_rows Number of rows.
* @tparam n_cols Number of columns.
* @tparam type Matrix type.
*/
template <typename T, size_t n_rows, size_t n_cols, MatType type>
class MatViewOpp;
/**
* @brief Read only view version of a transposed matrix.
*
* @tparam T Element type.
* @tparam n_rows Number of rows.
* @tparam n_cols Number of columns.
* @tparam type Matrix type.
*/
template <typename T, size_t n_rows, size_t n_cols, MatType type>
class MatViewT;
/**
* @brief Read only view version of a diagonal matrix.
*
* @tparam T Element type.
* @tparam N Matrix dimension.
* @tparam N_ Matrix dimension (unused, only to ensure it is a square matrix).
* @tparam type Matrix type (surely DIAGONAL here).
* @tparam type_parent Parent matrix (where it takes the diagonal) type.
*/
template <typename T, size_t N, size_t N_, MatType type, typename type_parent = MATTYPE_NORMAL>
class MatViewDiagMat;
/**
* @brief Read only view version of a diagonal matrix as vector.
*
* @tparam T Element type.
* @tparam N Matrix dimension.
* @tparam N_ Matrix dimension (unused, only to ensure it is a square matrix).
* @tparam type Matrix type (surely NORMAL here).
* @tparam type_parent Parent matrix (where it takes the diagonal) type.
*/
template <typename T, size_t N, size_t N_, MatType type, typename type_parent = MATTYPE_NORMAL>
class MatViewDiagVec;
/**
* @brief Read only view version of a diagonal matrix as row vector.
*
* @tparam T Element type.
* @tparam N Matrix dimension.
* @tparam N_ Matrix dimension (unused, only to ensure it is a square matrix).
* @tparam type Matrix type (surely NORMAL here).
* @tparam type_parent Parent matrix (where it takes the diagonal) type.
*/
template <typename T, size_t N, size_t N_, MatType type, typename type_parent = MATTYPE_NORMAL>
class MatViewDiagRowVec;
/**
* @brief Read only view version of a off diagonal.
*
* @tparam T Element type.
* @tparam N Matrix dimension.
* @tparam N_ Matrix dimension (unused, only to ensure it is a square matrix).
* @tparam type Matrix type (surely NORMAL here).
* @tparam type_parent Parent matrix (where it takes the off diagonal) type.
*/
template <typename T, size_t N, size_t N_, MatType type, typename type_parent = MATTYPE_NORMAL>
class MatViewOffDiag;
/**
* @brief Read only view version of a certain column as colunm vector.
*
* @tparam T Element type.
* @tparam n_rows Number of rows.
* @tparam n_cols Number of columns.
* @tparam type Matrix type (surely NORMAL here).
* @tparam type_parent Parent matrix (where it takes the colunm vector) type.
*/
template <typename T, size_t n_rows, size_t n_cols, MatType type, typename type_parent = MATTYPE_NORMAL>
class MatViewCol;
template <typename T, size_t n_rows, size_t n_cols, MatType type, typename type_parent = MATTYPE_NORMAL>
class MatRefCol;
/**
* @brief Read only view version of a certain row as row vector.
*
* @tparam T Element type.
* @tparam n_rows Number of rows.
* @tparam n_cols Number of columns.
* @tparam type Matrix type (surely NORMAL here).
* @tparam type_parent Parent matrix (where it takes the row vector) type.
*/
template <typename T, size_t n_rows, size_t n_cols, MatType type, typename type_parent = MATTYPE_NORMAL>
class MatViewRow;
/**
* @brief Read only view version of successive columns.
*
* @tparam T Element type.
* @tparam n_rows Number of rows.
* @tparam n_cols Number of columns.
* @tparam type Matrix type (surely NORMAL here).
* @tparam type_parent Parent matrix (where it takes the colunm vectors) type.
*/
template <size_t first_col, size_t last_col, typename T, size_t n_rows, size_t n_cols, MatType type,
typename type_parent = MATTYPE_NORMAL>
class MatViewCols;
/**
* @brief Read only view version of successive rows.
*
* @tparam T Element type.
* @tparam n_rows Number of rows.
* @tparam n_cols Number of columns.
* @tparam type Matrix type (surely NORMAL here).
* @tparam type_parent Parent matrix (where it takes the row vectors) type.
*/
template <size_t first_row, size_t last_row, typename T, size_t n_rows, size_t n_cols, MatType type,
typename type_parent = MATTYPE_NORMAL>
class MatViewRows;
/**
* @brief Afterwards action with initialization.
*
* @note This should only be used for view classes.
*/
enum class InitAfterwards {
NONE, /**< none */
OPP, /**< opposite */
TR /**< transpose */
};
enum class Init { NONE, ZEROS, ONES };
template <typename T, size_t n_rows, size_t n_cols, MatType type>
class Mat {
friend class MatView<T, n_rows, n_cols, type>;
friend class MatViewOpp<T, n_rows, n_cols, type>;
friend class MatViewT<T, n_cols, n_rows, type>;
friend class MatViewT<T, n_rows, n_cols, type>;
template <typename View_T, size_t View_N, size_t View_N_, MatType View_type, typename type_parent>
friend class MatViewDiagMat;
template <typename View_T, size_t View_N, size_t View_N_, MatType View_type, typename type_parent>
friend class MatViewDiagVec;
template <typename View_T, size_t View_N, size_t View_N_, MatType View_type, typename type_parent>
friend class MatViewDiagRowVec;
template <typename View_T, size_t View_N, size_t View_N_, MatType View_type, typename type_parent>
friend class MatViewOffDiag;
template <typename View_T, size_t View_n_rows, size_t View_n_cols, MatType View_type, typename type_parent>
friend class MatViewCol;
template <typename View_T, size_t View_n_rows, size_t View_n_cols, MatType View_type, typename type_parent>
friend class MatViewRow;
template <size_t first_col, size_t last_col, typename View_T, size_t View_n_rows, size_t View_n_cols,
MatType View_type, typename type_parent>
friend class MatViewCols;
template <size_t first_row, size_t last_row, typename View_T, size_t View_n_rows, size_t View_n_cols,
MatType View_type, typename type_parent>
friend class MatViewRows;
template <typename View_T_T, size_t T_n_rows, size_t T_n_cols, size_t T_n_slices, MatType T_type>
friend class Tensor;
public:
using element_type = T;
using value_type = T;
/**
* @brief Construct a new Mat object.
*
* @details This is the default constructor.
* You can set the array partition using macro
* `FLAMES_MAT_PARTITION_COMPLETE` to set a complete array partition
* and `FLAMES_MAT_PARTITION_FACTOR` to set a block partition with the specific factor.
* @note Data is stored as a row major sequence.
*/
Mat() {
static_assert(n_rows != 0, "'rows' should be no smaller than 1.");
static_assert(n_cols != 0, "'n_cols' should be no smaller than 1.");
static_assert(type == MatType::NORMAL || n_rows == n_cols, "Square matrix 'rows' should be equal to 'n_cols'.");
#ifdef FLAMES_MAT_PARTITION_COMPLETE
FLAMES_PRAGMA(ARRAY_PARTITION variable = _data type = complete)
#else
FLAMES_PRAGMA(ARRAY_PARTITION variable = _data type = block factor = FLAMES_MAT_PARTITION_FACTOR)
#endif
}
/**
* @brief Construct a new Mat object with initial value.
*
* @details All values are set to the initial value you specify.
* You can configure macro `FLAMES_MAT_SET_VALUE_UNROLL_FACTOR`
* to set the initialization unroll factor.
* @param val The initial value.
*/
Mat(T val) {
static_assert(n_rows != 0, "'n_rows' should be no smaller than 1.");
static_assert(n_cols != 0, "'n_cols' should be no smaller than 1.");
static_assert(type == MatType::NORMAL || n_rows == n_cols, "Square matrix 'rows' should be equal to 'n_cols'.");
#ifdef FLAMES_MAT_PARTITION_COMPLETE
FLAMES_PRAGMA(ARRAY_PARTITION variable = _data type = complete)
#else
FLAMES_PRAGMA(ARRAY_PARTITION variable = _data type = block factor = FLAMES_MAT_PARTITION_FACTOR)
#endif
setValue(val);
}
/**
* @brief Copy constructor from a Mat object.
*
* @details Macro `FLAMES_MAT_COPY_UNROLL_FACTOR` to configure the unrolling factor.
* You can set the array partition using macro
* `FLAMES_MAT_PARTITION_COMPLETE` to set a complete array partition
* and `FLAMES_MAT_PARTITION_FACTOR` to set a block partition with the specific factor.
* @param mat The matrix to be copied.
*/
Mat(const Mat& mat) {
MAT_COPY:
for (size_t i = 0; i != size(); ++i) {
FLAMES_PRAGMA(UNROLL factor = FLAMES_MAT_COPY_UNROLL_FACTOR)
_data[i] = mat[i];
}
#ifdef FLAMES_MAT_PARTITION_COMPLETE
FLAMES_PRAGMA(ARRAY_PARTITION variable = _data type = complete)
#else
FLAMES_PRAGMA(ARRAY_PARTITION variable = _data type = block factor = FLAMES_MAT_PARTITION_FACTOR)
#endif
#ifdef FLAMES_PRINT_PER_MAT_COPY
std::cout << "Mat copy!" << std::endl;
#endif
}
template <typename T2, size_t _rows, size_t _cols, MatType _type,
std::enable_if_t<!std::is_same<T, T2>::value && type == _type && n_rows == _rows && n_cols == _cols,
bool> = true>
Mat(const Mat<T2, _rows, _cols, _type>& mat) {
MAT_COPY:
for (size_t i = 0; i != size(); ++i) {
FLAMES_PRAGMA(UNROLL factor = FLAMES_MAT_COPY_UNROLL_FACTOR)
_data[i] = mat[i];
}
#ifdef FLAMES_MAT_PARTITION_COMPLETE
FLAMES_PRAGMA(ARRAY_PARTITION variable = _data type = complete)
#else
FLAMES_PRAGMA(ARRAY_PARTITION variable = _data type = block factor = FLAMES_MAT_PARTITION_FACTOR)
#endif
#ifdef FLAMES_PRINT_PER_MAT_COPY
std::cout << "Mat copy!" << std::endl;
#endif
}
template <typename T2, size_t _rows, size_t _cols, MatType _type,
std::enable_if_t<type != _type && n_rows == _rows && n_cols == _cols, bool> = true>
Mat(const Mat<T2, _rows, _cols, _type>& mat) {
MAT_COPY:
for (size_t r = 0; r != n_rows; ++r) {
FLAMES_PRAGMA(UNROLL factor = FLAMES_MAT_COPY_UNROLL_FACTOR)
for (size_t c = 0; c != n_cols; ++c) {
FLAMES_PRAGMA(LOOP_FLATTEN)
_tryAssign(r, c, mat(r, c));
}
}
#ifdef FLAMES_MAT_PARTITION_COMPLETE
FLAMES_PRAGMA(ARRAY_PARTITION variable = _data type = complete)
#else
FLAMES_PRAGMA(ARRAY_PARTITION variable = _data type = block factor = FLAMES_MAT_PARTITION_FACTOR)
#endif
#ifdef FLAMES_PRINT_PER_MAT_COPY
std::cout << "Mat copy!" << std::endl;
#endif
}
/**
* @brief Construct a new Mat object from std::vector.
*
* @param vec The std::vector storing data in row major.
* @details You can set the array partition using macro
* `FLAMES_MAT_PARTITION_COMPLETE` to set a complete array partition
* and `FLAMES_MAT_PARTITION_FACTOR` to set a block partition with the specific factor.
*/
Mat(const std::vector<T>& vec) {
assert(vec.size() == size() && "Initialization vector size disagrees.");
MAT_COPY_FROM_STD_VEC:
for (size_t i = 0; i != size(); ++i) {
FLAMES_PRAGMA(UNROLL factor = FLAMES_MAT_COPY_UNROLL_FACTOR)
_data[i] = vec[i];
}
#ifdef FLAMES_MAT_PARTITION_COMPLETE
FLAMES_PRAGMA(ARRAY_PARTITION variable = _data type = complete)
#else
FLAMES_PRAGMA(ARRAY_PARTITION variable = _data type = block factor = FLAMES_MAT_PARTITION_FACTOR)
#endif
#ifdef FLAMES_PRINT_PER_MAT_COPY
std::cout << "Mat copy!" << std::endl;
#endif
}
template <typename T2>
Mat(std::initializer_list<T2> list) {
auto list_size = list.size();
assert(list_size <= size() && "Initializer list size should not exceed size().");
MAT_COPY_FROM_INIT_LIST:
for (size_t i = 0; i != list_size; ++i) {
FLAMES_PRAGMA(UNROLL factor = FLAMES_MAT_COPY_UNROLL_FACTOR)
_data[i] = *(list.begin() + i);
}
#ifdef FLAMES_MAT_PARTITION_COMPLETE
FLAMES_PRAGMA(ARRAY_PARTITION variable = _data type = complete)
#else
FLAMES_PRAGMA(ARRAY_PARTITION variable = _data type = block factor = FLAMES_MAT_PARTITION_FACTOR)
#endif
#ifdef FLAMES_PRINT_PER_MAT_COPY
std::cout << "Mat copy!" << std::endl;
#endif
}
Mat(Init init) {}
// public: // original private
public:
/**
* @brief Construct a new Mat object from raw data pointer.
*
* @param ptr The raw data pointer.
* @param opt Option if initialization.
* @note This function should only be used by view classes and is therefore marked as private.
*/
explicit Mat(const T* ptr, InitAfterwards opt = InitAfterwards::NONE) {
if (opt == InitAfterwards::NONE) {
MAT_COPY:
for (size_t i = 0; i != size(); ++i) {
FLAMES_PRAGMA(UNROLL factor = FLAMES_MAT_COPY_UNROLL_FACTOR)
_data[i] = ptr[i];
}
} else if (opt == InitAfterwards::OPP) {
MAT_COPY_OPP:
for (size_t i = 0; i != size(); ++i) {
FLAMES_PRAGMA(UNROLL factor = FLAMES_MAT_COPY_UNROLL_FACTOR)
_data[i] = -ptr[i];
}
} else if (opt == InitAfterwards::TR) {
Mat<T, n_cols, n_rows, type> tmp(ptr, InitAfterwards::NONE);
this->t(tmp);
}
#ifdef FLAMES_MAT_PARTITION_COMPLETE
FLAMES_PRAGMA(ARRAY_PARTITION variable = _data type = complete)
#else
FLAMES_PRAGMA(ARRAY_PARTITION variable = _data type = block factor = FLAMES_MAT_PARTITION_FACTOR)
#endif
#ifdef FLAMES_PRINT_PER_MAT_COPY
std::cout << "Mat copy!" << std::endl;
#endif
}
explicit Mat(T* const ptr, InitAfterwards opt = InitAfterwards::NONE) {
if (opt == InitAfterwards::NONE) {
MAT_COPY:
for (size_t i = 0; i != size(); ++i) {
FLAMES_PRAGMA(UNROLL factor = FLAMES_MAT_COPY_UNROLL_FACTOR)
_data[i] = ptr[i];
}
} else if (opt == InitAfterwards::OPP) {
MAT_COPY_OPP:
for (size_t i = 0; i != size(); ++i) {
FLAMES_PRAGMA(UNROLL factor = FLAMES_MAT_COPY_UNROLL_FACTOR)
_data[i] = -ptr[i];
}
} else if (opt == InitAfterwards::TR) {
Mat<T, n_cols, n_rows, type> tmp(ptr, InitAfterwards::NONE);
this->t(tmp);
}
#ifdef FLAMES_MAT_PARTITION_COMPLETE
FLAMES_PRAGMA(ARRAY_PARTITION variable = _data type = complete)
#else
FLAMES_PRAGMA(ARRAY_PARTITION variable = _data type = block factor = FLAMES_MAT_PARTITION_FACTOR)
#endif
#ifdef FLAMES_PRINT_PER_MAT_COPY
std::cout << "Mat copy!" << std::endl;
#endif
}
public:
/**
* @brief Destroy the Mat object.
*
* @details So far there is nothing to do.
*/
~Mat() {
// so far nothing to do
}
// template <typename... _unused, MatType _type = type,
// typename std::enable_if_t<_type == MatType::NORMAL, bool> = true>
// inline constexpr size_t size_const() noexcept {
// static_assert(sizeof...(_unused) == 0, "Do not specify template arguments for Mat::size()!");
// return n_rows * n_cols;
// }
/**
* @brief Get the matrix size of storage.
*
* @details This is useful in that matrices of different MatType has the data array of different sizes.
* @return (constexpr size_t) The data array size.
*/
inline static constexpr size_t size() noexcept {
return type == MatType::NORMAL ? n_rows * n_cols
: type == MatType::DIAGONAL ? n_rows
: type == MatType::SCALAR ? 1
: type == MatType::SUPPER ? (n_rows - 1) * n_rows / 2
: type == MatType::SLOWER ? (n_rows - 1) * n_rows / 2
: type == MatType::ASYM ? (n_rows - 1) * n_rows / 2
: (1 + n_rows) * n_rows / 2;
}
/**
* @brief Get read only element by row major index from the data array.
*
* @details If you are not sure which index it is
* operator() which takes tow index and column index is a better resort.
* @param index The data array index in row major.
* @return (T) The read only data element.
*/
T operator[](size_t index) const {
FLAMES_PRAGMA(INLINE)
assert(index < size() && "Matrix index should be within range");
return _data[index];
}
/**
* @brief Get writeable element by row major index from the data array.
*
* @details If you are not sure which index it is
* operator() which takes tow index and column index is a better resort.
* @param index The data array index in row major.
* @return (T) The writeable data element.
*/
T& operator[](size_t index) {
FLAMES_PRAGMA(INLINE)
assert(index < size() && "Matrix index should be within range");
return _data[index];
}
/**
* @brief Get read only data element by row index and column index.
*
* @param r The row index (starting from 0).
* @param c The column index (staring from 0).
* @return (T) The read only data element.
*/
T operator()(size_t r, size_t c) const {
FLAMES_PRAGMA(INLINE)
assert(r < n_rows && "Matrix row index should be within range");
assert(c < n_cols && "Matrix col index should be within range");
if (type == MatType::NORMAL) {
return _data[r * n_cols + c];
} else if (type == MatType::DIAGONAL) {
if (r == c) return _data[r];
else return T(0);
} else if (type == MatType::SCALAR) {
if (r == c) return _data[0];
else return T(0);
} else if (type == MatType::UPPER) {
if (r <= c) return _data[(2 * n_cols + 1 - r) * r / 2 + c - r];
else return T(0);
} else if (type == MatType::LOWER) {
if (r >= c) return _data[(1 + r) * r / 2 + c];
else return T(0);
} else if (type == MatType::SUPPER) {
if (r < c) return _data[(2 * n_cols + 1 - r) * r / 2 + c - 1 - 2 * r];
else return T(0);
} else if (type == MatType::SLOWER) {
if (r > c) return _data[(1 + r) * r / 2 + c - r];
else return T(0);
} else if (type == MatType::SYM) {
if (r <= c) return _data[(2 * n_cols + 1 - r) * r / 2 + c - r];
else return _data[(2 * n_cols + 1 - c) * c / 2 + r - c];
} else if (type == MatType::ASYM) {
if (r < c) return _data[(2 * n_cols + 1 - r) * r / 2 + c - 1 - 2 * r];
else if (r > c) return -_data[(2 * n_cols + 1 - c) * c / 2 + r - 1 - 2 * c];
else return T(0);
} else {
// Normally it is impossible to reach here.
assert(!"Impossible! Unknown MatType!");
}
}
/**
* @brief Get writeable data element by row index and column index.
*
* @param r The row index (starting from 0).
* @param c The column index (staring from 0).
* @return (T) The writeable data element.
*/
T& operator()(size_t r, size_t c) {
FLAMES_PRAGMA(INLINE)
assert(r < n_rows && "Matrix row index should be within range");
assert(c < n_cols && "Matrix col index should be within range");
if (type == MatType::NORMAL) {
return _data[r * n_cols + c];
} else if (type == MatType::DIAGONAL) {
if (r == c) return _data[r];
else assert(!"This element cannot be modified (DIAGONAL).");
} else if (type == MatType::SCALAR) {
assert(!"This element cannot be modified (SCALAR).");
} else if (type == MatType::UPPER) {
if (r <= c) return _data[(2 * n_cols + 1 - r) * r / 2 + c - r];
else assert(!"This element cannot be modified (UPPER).");
} else if (type == MatType::LOWER) {
if (r >= c) return _data[(1 + r) * r / 2 + c];
else assert(!"This element cannot be modified (LOWER).");
} else if (type == MatType::SUPPER) {
if (r < c) return _data[(2 * n_cols + 1 - r) * r / 2 + c - 1 - 2 * r];
else assert(!"This element cannot be modified (SUPPER).");
} else if (type == MatType::SLOWER) {
if (r > c) return _data[(1 + r) * r / 2 + c - r];
else assert(!"This element cannot be modified (SLOWER).");
} else if (type == MatType::SYM) {
if (r <= c) return _data[(2 * n_cols + 1 - r) * r / 2 + c - r];
else return _data[(2 * n_cols + 1 - c) * c / 2 + r - c];
} else if (type == MatType::ASYM) {
if (r < c) return _data[(2 * n_cols + 1 - r) * r / 2 + c - 1 - 2 * r];
else if (r > c) return _data[(2 * n_cols + 1 - c) * c / 2 + r - 1 - 2 * c];
// ATTENTION This part needs to be perfected , missing a minus sign.
// Because a minus sign will result in a error about reference.
else assert(!"This element cannot be modified (ASYM).");
} else {
// Normally it is impossible to reach here.
assert(!"Impossible! Unknown MatType!");
}
// Just to make the compiler happy.
return _data[0];
}
/**
* @brief Set all elements of the matrix to a value.
*
* @param val The value.
*/
void setValue(T val) {
for (size_t i = 0; i != size(); ++i) {
FLAMES_PRAGMA(UNROLL factor = FLAMES_MAT_SET_VALUE_UNROLL_FACTOR)
_data[i] = val;
}
}
/**
* @brief Set all elements of the matrix to zero.
*
*/
void setZero() { setValue(static_cast<T>(0)); }
bool read(const std::string& file_name) {
#ifndef __SYNTHESIS__
std::ifstream f(file_name);
if (f.is_open()) {
size_t in_rows, in_cols;
f >> in_rows >> in_cols;
if (n_rows != in_rows || n_cols != in_cols) return false; // dimension does not match
std::string complex_real, mat_type;
f >> complex_real >> mat_type;
if (type == MatType::NORMAL && mat_type != "normal") return false;