FEAT 3
Finite Element Analysis Toolbox
Loading...
Searching...
No Matches
tuple_matrix.hpp
1// FEAT3: Finite Element Analysis Toolbox, Version 3
2// Copyright (C) 2010 by Stefan Turek & the FEAT group
3// FEAT3 is released under the GNU General Public License version 3,
4// see the file 'copyright.txt' in the top level directory for details.
5
6#pragma once
7
8#include <kernel/lafem/tuple_vector.hpp>
9
10
11namespace FEAT
12{
13 namespace LAFEM
14 {
30 template<typename First_, typename... Rest_>
32 {
33 private:
34 template<typename,typename...>
35 friend class TupleMatrixRow;
36
37 typedef TupleMatrixRow<Rest_...> RestClass;
38
39 public:
41 static constexpr int num_blocks = TupleMatrixRow<Rest_...>::num_blocks + 1;
42
44 typedef typename First_::DataType DataType;
46 typedef typename First_::IndexType IndexType;
47
48 // ensure that all sub-matrix have the same data-type
49 static_assert(std::is_same<DataType, typename RestClass::DataType>::value,
50 "sub-matrices have different data-types");
51 static_assert(std::is_same<IndexType, typename RestClass::IndexType>::value,
52 "sub-matrices have different index-types");
53
55 template <typename DT2_ = DataType, typename IT2_ = IndexType>
57 typename First_::template ContainerType<DT2_, IT2_>,
58 typename Rest_::template ContainerType<DT2_, IT2_>...>;
59
61 template <typename DataType2_, typename IndexType2_>
63
65 typedef typename First_::VectorTypeL VectorTypeL;
67 typedef TupleVector<typename First_::VectorTypeR, typename Rest_::VectorTypeR...> VectorTypeR;
68
69 protected:
71 First_ _first;
74
77 {
78 return First_::name() + "," + RestClass::sub_name_list();
79 }
80
81 public:
84 {
85 }
86
88 explicit TupleMatrixRow(First_&& the_first, RestClass&& the_rest) :
89 _first(std::forward<First_>(the_first)),
90 _rest(std::forward<RestClass>(the_rest))
91 {
92 }
93
95 explicit TupleMatrixRow(First_&& the_first, Rest_&&... the_rest) :
96 _first(std::forward<First_>(the_first)),
97 _rest(std::forward<Rest_>(the_rest)...)
98 {
99 }
100
103 _first(std::forward<First_>(other._first)),
104 _rest(std::forward<RestClass>(other._rest))
105 {
106 }
107
110 {
111 if(this != &other)
112 {
113 _first = std::forward<First_>(other._first);
114 _rest = std::forward<RestClass>(other._rest);
115 }
116 return *this;
117 }
118
123
131 {
132 return TupleMatrixRow(_first.clone(mode), _rest.clone(mode));
133 }
134
135 void init_workspace() const
136 {
137 _first.init_workspace();
138 _rest.init_workspace();
139 }
140
141 void reset_workspace() const
142 {
143 _first.reset_workspace();
144 _rest.reset_workspace();
145 }
146
153 void clone(const TupleMatrixRow& other, CloneMode clone_mode)
154 {
155 _first.clone(other._first, clone_mode);
156 _rest.clone(other._rest, clone_mode);
157 }
158
164 void clone(const TupleMatrixRow& other)
165 {
166 _first.clone(other._first);
167 _rest.clone(other._rest);
168 }
169
171 First_& first()
172 {
173 return _first;
174 }
175
176 const First_& first() const
177 {
178 return _first;
179 }
180
181 TupleMatrixRow<Rest_...>& rest()
182 {
183 return _rest;
184 }
185
186 const TupleMatrixRow<Rest_...>& rest() const
187 {
188 return _rest;
189 }
191
201 template<int i_>
202 typename TupleElement<i_, First_, Rest_...>::Type& at()
203 {
204 static_assert((0 <= i_) && (i_ < num_blocks), "invalid sub-matrix index");
206 }
207
209 template<int i_>
210 typename TupleElement<i_, First_, Rest_...>::Type const& at() const
211 {
212 static_assert((0 <= i_) && (i_ < num_blocks), "invalid sub-matrix index");
214 }
215
220 {
221 const Index rows_f = _first.num_rows();
222 const Index rows_r = _rest.num_rows();
223 XASSERTM(rows_f == rows_r, "row count mismatch");
224 return rows_f;
225 }
226
231 {
232 return _first.num_cols() + _rest.num_cols();
233 }
234
239 {
240 return _first.num_nzes() + _rest.num_nzes();
241 }
242
247 {
248 const Index rows_f = _first.num_rows_raw();
249 const Index rows_r = _rest.num_rows_raw();
250 XASSERTM(rows_f == rows_r, "row count mismatch");
251 return rows_f;
252 }
253
258 {
259 return _first.num_cols_raw() + _rest.num_cols_raw();
260 }
261
266 {
267 return _first.num_nzes_raw() + _rest.num_nzes_raw();
268 }
269
275 std::size_t bytes() const
276 {
277 return _first.bytes() + _rest.bytes();
278 }
279
281 static String name()
282 {
283 return String("TupleMatrixRow<") + sub_name_list() + ">";
284 }
285
292 void format()
293 {
294 first().format();
295 rest().format();
296 }
297
298 void format(DataType value)
299 {
300 first().format(value);
301 rest().format(value);
302 }
303
311 void format(Random & rng, DataType min, DataType max)
312 {
313 first().format(rng, min, max);
314 rest().format(rng, min, max);
315 }
316
318 void clear()
319 {
320 first().clear();
321 rest().clear();
322 }
323
326 {
327 return first().create_vector_l();
328 }
329
332 {
333 return VectorTypeR(first().create_vector_r(), rest().create_vector_r());
334 }
335
336 void apply(VectorTypeL& r, const VectorTypeR& x) const
337 {
338 first().apply(r, x.first());
339 rest().apply(r, x.rest(), r, DataType(1));
340 }
341
342 void apply_transposed(VectorTypeR& r, const VectorTypeL& x) const
343 {
344 first().apply_transposed(r.first(), x);
345 rest().apply_transposed(r.rest(), x);
346 }
347
348 void apply(VectorTypeL& r, const VectorTypeR& x, const VectorTypeL& y, DataType alpha = DataType(1)) const
349 {
350 first().apply(r, x.first(), y, alpha);
351 rest().apply(r, x.rest(), r, alpha);
352 }
353
354 void apply_transposed(VectorTypeR& r, const VectorTypeL& x, const VectorTypeR& y, DataType alpha = DataType(1)) const
355 {
356 first().apply_transposed(r.first(), x, y.first(), alpha);
357 rest().apply_transposed(r.rest(), x, y.rest(), alpha);
358 }
359
360 Index row_degree(const Index row) const
361 {
362 return first().row_degree(row) + rest().row_degree(row);
363 }
364
365 template<typename IT2_>
366 Index get_row_col_indices(const Index row, IT2_* const pcol_idx, const IT2_ col_offset) const
367 {
368 const Index first_cols = first().num_cols_raw();
369 const Index first_degree = first().get_row_col_indices(row, pcol_idx, col_offset);
370 return first_degree + rest().get_row_col_indices(row, pcol_idx + first_degree, col_offset + IT2_(first_cols));
371 }
372
373 template<typename DT2_>
374 Index get_row_values(const Index row, DT2_ * const pvals) const
375 {
376 const Index first_degree = first().get_row_values(row, pvals);
377 return first_degree + rest().get_row_values(row, pvals + first_degree);
378 }
379
380 template<typename DT2_>
381 Index set_row_values(const Index row, const DT2_ * const pvals)
382 {
383 const Index first_degree = first().set_row_values(row, pvals);
384 return first_degree + rest().set_row_values(row, pvals + first_degree);
385 }
386
388 std::uint64_t get_checkpoint_size(SerialConfig& config)
389 {
390 return sizeof(std::uint64_t) + _first.get_checkpoint_size(config) + _rest.get_checkpoint_size(config); //sizeof(std::uint64_t) bits needed to store lenght of checkpointed _first
391 }
392
394 void restore_from_checkpoint_data(std::vector<char> & data)
395 {
396 std::uint64_t isize = *(std::uint64_t*) data.data(); //get size of checkpointed _first
397 std::vector<char>::iterator start = std::begin(data) + sizeof(std::uint64_t);
398 std::vector<char>::iterator last_of_first = std::begin(data) + sizeof(std::uint64_t) + (int) isize;
399 std::vector<char> buffer_first(start, last_of_first);
400 _first.restore_from_checkpoint_data(buffer_first);
401
402 data.erase(std::begin(data), last_of_first);
404 }
405
407 std::uint64_t set_checkpoint_data(std::vector<char>& data, SerialConfig& config)
408 {
409 std::size_t old_size = data.size();
410 data.insert(std::end(data), sizeof(std::uint64_t), 0); //add placeholder
411 std::uint64_t ireal_size = _first.set_checkpoint_data(data, config); //add data of _first to the overall checkpoint and save its size
412 char* csize = reinterpret_cast<char*>(&ireal_size);
413 for(std::size_t i(0) ; i < sizeof(std::uint64_t) ; ++i) //overwrite the guessed datalength
414 {
415 data[old_size + i] = csize[i];
416 }
417
418 return sizeof(std::uint64_t) + ireal_size + _rest.set_checkpoint_data(data, config); //generate and add checkpoint data for the _rest
419 }
420
421 template<typename OtherFirst_, typename... OtherRest_>
422 void convert(const TupleMatrixRow<OtherFirst_, OtherRest_...>& source)
423 {
424 first().convert(source.first());
425 rest().convert(source.rest());
426 }
427
428 template<typename OtherFirst_, typename... OtherRest_>
429 void copy(const TupleMatrixRow<OtherFirst_, OtherRest_...>& source)
430 {
431 first().copy(source.first());
432 rest().copy(source.rest());
433 }
434
435 template<typename OtherFirst_, typename... OtherRest_>
436 void copy_to(TupleMatrixRow<OtherFirst_, OtherRest_...>& target) const
437 {
438 first().copy_to(target.first());
439 rest().copy_to(target.rest());
440 }
441
442 bool same_layout(const TupleMatrixRow& other) const
443 {
444 return first().same_layout(other.first()) && rest().same_layout(other.rest());
445 }
446
447 DataType max_rel_diff(const TupleMatrixRow& other) const
448 {
449 return Math::max(first().max_rel_diff(other.first()), rest().max_rel_diff(other.rest()));
450 }
451 }; // template class TupleMatrixRow
452
454 // specialization for a single sub-matrix
455 template<typename First_>
456 class TupleMatrixRow<First_>
457 {
458 private:
459 template<typename,typename...>
460 friend class TupleMatrixRow;
461
462 public:
464 static constexpr int num_blocks = 1;
465
467 typedef typename First_::DataType DataType;
469 typedef typename First_::IndexType IndexType;
470
471 template <typename DT2_ = DataType, typename IT2_ = IndexType>
472 using ContainerType = TupleMatrixRow<typename First_::template ContainerType<DT2_, IT2_> >;
473
475 template <typename DataType2_, typename IndexType2_>
476 using ContainerTypeByDI = ContainerType<DataType2_, IndexType2_>;
477
479 typedef typename First_::VectorTypeL VectorTypeL;
481 typedef TupleVector<typename First_::VectorTypeR> VectorTypeR;
482
483 protected:
485 First_ _first;
486
488 static String sub_name_list()
489 {
490 return First_::name();
491 }
492
493 public:
495 TupleMatrixRow()
496 {
497 }
498
500 explicit TupleMatrixRow(First_&& the_first) :
501 _first(std::forward<First_>(the_first))
502 {
503 }
504
506 TupleMatrixRow(TupleMatrixRow&& other) :
507 _first(std::forward<First_>(other._first))
508 {
509 }
510
512 TupleMatrixRow& operator=(TupleMatrixRow&& other)
513 {
514 if(this != &other)
515 {
516 _first = std::forward<First_>(other._first);
517 }
518 return *this;
519 }
520
522 TupleMatrixRow(const TupleMatrixRow&) = delete;
524 TupleMatrixRow& operator=(const TupleMatrixRow&) = delete;
525
526 TupleMatrixRow clone(LAFEM::CloneMode mode = LAFEM::CloneMode::Weak) const
527 {
528 return TupleMatrixRow(_first.clone(mode));
529 }
530
531 void init_workspace() const
532 {
533 _first.init_workspace();
534 }
535
536 void reset_workspace() const
537 {
538 _first.reset_workspace();
539 }
540
541 void clone(const TupleMatrixRow& other, CloneMode clone_mode)
542 {
543 _first.clone(other._first, clone_mode);
544 }
545
546 void clone(const TupleMatrixRow& other)
547 {
548 _first.clone(other._first);
549 }
550
552 First_& first()
553 {
554 return _first;
555 }
556
557 const First_& first() const
558 {
559 return _first;
560 }
562
563 template<int i_>
564 First_& at()
565 {
566 static_assert(i_ == 0, "invalid sub-matrix index");
567 return first();
568 }
569
571 template<int i_>
572 const First_& at() const
573 {
574 static_assert(i_ == 0, "invalid sub-matrix index");
575 return first();
576 }
577
578 Index num_rows() const
579 {
580 return _first.num_rows();
581 }
582
583 Index num_cols() const
584 {
585 return _first.num_cols();
586 }
587
588 Index num_nzes() const
589 {
590 return _first.num_nzes();
591 }
592
593 Index num_rows_raw() const
594 {
595 return _first.num_rows_raw();
596 }
597
598 Index num_cols_raw() const
599 {
600 return _first.num_cols_raw();
601 }
602
603 Index num_nzes_raw() const
604 {
605 return _first.num_nzes_raw();
606 }
607
608 std::size_t bytes() const
609 {
610 return _first.bytes();
611 }
612
613 static String name()
614 {
615 return String("TupleMatrixRow<") + sub_name_list() + ">";
616 }
617
618 void format()
619 {
620 first().format();
621 }
622
623 void format(DataType value)
624 {
625 first().format(value);
626 }
627
628 void format(Random & rng, DataType min, DataType max)
629 {
630 first().format(rng, min, max);
631 }
632
633 void clear()
634 {
635 first().clear();
636 }
637
639 {
640 return first().create_vector_l();
641 }
642
644 {
645 return VectorTypeR(first().create_vector_r());
646 }
647
648 void apply(VectorTypeL& r, const VectorTypeR& x) const
649 {
650 first().apply(r, x.first());
651 }
652
653 void apply(VectorTypeL& r, const VectorTypeR& x, const VectorTypeL& y, DataType alpha = DataType(1)) const
654 {
655 first().apply(r, x.first(), y, alpha);
656 }
657
658 void apply_transposed(VectorTypeR& r, const VectorTypeL& x) const
659 {
660 first().apply_transposed(r.first(), x);
661 }
662
663 void apply_transposed(VectorTypeR& r, const VectorTypeL& x, const VectorTypeR& y, DataType alpha = DataType(1)) const
664 {
665 first().apply_transposed(r.first(), x, y.first(), alpha);
666 }
667
668 Index row_degree(const Index row) const
669 {
670 return first().row_degree(row);
671 }
672
673 template<typename IT2_>
674 Index get_row_col_indices(const Index row, IT2_* const pcol_idx, const IT2_ col_offset) const
675 {
676 return first().get_row_col_indices(row, pcol_idx, col_offset);
677 }
678
679 template<typename DT2_>
680 Index get_row_values(const Index row, DT2_ * const pvals) const
681 {
682 return first().get_row_values(row, pvals);
683 }
684
685 template<typename DT2_>
686 Index set_row_values(const Index row, const DT2_ * const pvals)
687 {
688 return first().set_row_values(row, pvals);
689 }
690
692 std::uint64_t get_checkpoint_size(SerialConfig& config)
693 {
694 return _first.get_checkpoint_size(config);
695 }
696
698 void restore_from_checkpoint_data(std::vector<char> & data)
699 {
700 _first.restore_from_checkpoint_data(data);
701 }
702
704 std::uint64_t set_checkpoint_data(std::vector<char>& data, SerialConfig& config)
705 {
706 return _first.set_checkpoint_data(data, config);
707 }
708
709 template<typename OtherFirst_>
710 void convert(const TupleMatrixRow<OtherFirst_>& source)
711 {
712 first().convert(source.first());
713 }
714
715 template<typename OtherFirst_>
716 void copy(const TupleMatrixRow<OtherFirst_>& source)
717 {
718 first().copy(source.first());
719 }
720
721 template<typename OtherFirst_>
722 void copy_to(TupleMatrixRow<OtherFirst_>& target) const
723 {
724 first().copy_to(target.first());
725 }
726
727 bool same_layout(const TupleMatrixRow& other) const
728 {
729 return first().same_layout(other.first());
730 }
731
732 DataType max_rel_diff(const TupleMatrixRow& other) const
733 {
734 return first().max_rel_diff(other.first());
735 }
736 }; // template class TupleMatrixRow
738
740 namespace Intern
741 {
742 // helper class: checks whether 'T_' is an instance of 'TupleMatrixRow<...>'
743 template<typename T_>
744 struct IsTMR_
745 {
746 static constexpr bool value = false;
747 };
748
749 template<typename... T_>
750 struct IsTMR_<TupleMatrixRow<T_...>>
751 {
752 static constexpr bool value = true;
753 };
754 } // namespace Intern
756
765 template<int i_, int j_, typename FirstRow_, typename... RestRows_>
767 {
768 public:
769 static_assert(i_ > 0, "invalid row index"); // i_=0 is specialized below
770 static_assert(j_ >= 0, "invalid column index");
771
772 typedef typename TupleMatrixElement<i_-1, j_, RestRows_...>::Type Type;
773
774 template<typename Meta_>
775 static Type& get(Meta_& meta)
776 {
778 }
779
780 template<typename Meta_>
781 static const Type& get(const Meta_& meta)
782 {
784 }
785 };
786
788 // specialization for i_ = 0;
789 // Note: This specialization explicitly assumes that 'FirstRow_' is an instance of
790 // 'TupleMatrixRow', which is ensured by a static_assert in the TupleMatrix class.
791 template<int j_, typename First_, typename... Rest_, typename... RestRows_>
792 class TupleMatrixElement<0, j_, TupleMatrixRow<First_, Rest_...>, RestRows_... >
793 {
794 public:
795 static_assert(j_ >= 0, "invalid column index");
796
797 typedef typename TupleElement<j_, First_, Rest_...>::Type Type;
798
799 template<typename Meta_>
800 static Type& get(Meta_& meta)
801 {
802 return TupleElement<j_, First_, Rest_...>::get(meta.first());
803 }
804
805 template<typename Meta_>
806 static const Type& get(const Meta_& meta)
807 {
808 return TupleElement<j_, First_, Rest_...>::get(meta.first());
809 }
810 };
812
825 template<typename FirstRow_, typename... RestRows_>
827 {
828 private:
829 template<typename,typename...>
830 friend class TupleMatrix;
831
832 typedef TupleMatrix<RestRows_...> RestClass;
833
834 // sanity check: ensure that 'FirstRow_' is an instance of TupleMatrixRow
835 // note: this is essential for the implementation of TupleMatrixElement!
836 static_assert(Intern::IsTMR_<FirstRow_>::value,
837 "template parameter of TupleMatrix must be TupleMatrixRow<...>");
838
839 public:
841 static constexpr int num_row_blocks = TupleMatrix<RestRows_...>::num_row_blocks + 1;
843 static constexpr int num_col_blocks = FirstRow_::num_blocks;
844
845 // sanity check: number of column blocks must match for all rows
846 static_assert(num_col_blocks == RestClass::num_col_blocks, "column blocks mismatch");
847
849 typedef typename FirstRow_::DataType DataType;
851 typedef typename FirstRow_::IndexType IndexType;
852
853 // ensure that all sub-matrix have the same data-type
854 static_assert(std::is_same<DataType, typename RestClass::DataType>::value,
855 "sub-matrices have different data-types");
856 static_assert(std::is_same<IndexType, typename RestClass::IndexType>::value,
857 "sub-matrices have different index-types");
858
860 template <typename DT2_ = DataType, typename IT2_ = IndexType>
862 typename FirstRow_::template ContainerType<DT2_, IT2_>,
863 typename RestRows_::template ContainerType<DT2_, IT2_>...>;
864
866 template <typename DataType2_, typename IndexType2_>
868
870 typedef TupleVector<typename FirstRow_::VectorTypeL, typename RestRows_::VectorTypeL...> VectorTypeL;
872 typedef typename FirstRow_::VectorTypeR VectorTypeR;
873
875 static_assert(std::is_same<VectorTypeR, typename RestClass::VectorTypeR>::value, "vector type mismatch");
876
877 protected:
879 FirstRow_ _first;
882
885 {
886 return FirstRow_::name() + "," + RestClass::sub_name_list();
887 }
888
889 public:
892 {
893 }
894
896 explicit TupleMatrix(FirstRow_&& the_first, RestClass&& the_rest) :
897 _first(std::forward<FirstRow_>(the_first)),
898 _rest(std::forward<RestClass>(the_rest))
899 {
900 }
901
903 explicit TupleMatrix(FirstRow_&& the_first, RestRows_&&... the_rest) :
904 _first(std::forward<FirstRow_>(the_first)),
905 _rest(std::forward<RestRows_>(the_rest)...)
906 {
907 }
908
911 _first(std::forward<FirstRow_>(other._first)),
912 _rest(std::forward<RestClass>(other._rest))
913 {
914 }
915
918 {
919 if(this != &other)
920 {
921 _first = std::forward<FirstRow_>(other._first);
922 _rest = std::forward<RestClass>(other._rest);
923 }
924 return *this;
925 }
926
928 TupleMatrix(const TupleMatrix&) = delete;
931
939 {
940 return TupleMatrix(_first.clone(mode), _rest.clone(mode));
941 }
942
943 void init_workspace() const
944 {
945 _first.init_workspace();
946 _rest.init_workspace();
947 }
948
949 void reset_workspace() const
950 {
951 _first.reset_workspace();
952 _rest.reset_workspace();
953 }
954
961 void clone(const TupleMatrix& other, CloneMode clone_mode)
962 {
963 _first.clone(other._first, clone_mode);
964 _rest.clone(other._rest, clone_mode);
965 }
966
972 void clone(const TupleMatrix& other)
973 {
974 _first.clone(other._first);
975 _rest.clone(other._rest);
976 }
977
979 FirstRow_& first()
980 {
981 return _first;
982 }
983
984 const FirstRow_& first() const
985 {
986 return _first;
987 }
988
989 TupleMatrix<RestRows_...>& rest()
990 {
991 return _rest;
992 }
993
994 const TupleMatrix<RestRows_...>& rest() const
995 {
996 return _rest;
997 }
999
1009 template<int i_, int j_>
1010 typename TupleMatrixElement<i_, j_, FirstRow_, RestRows_...>::Type& at()
1011 {
1012 static_assert((0 <= i_) && (i_ < num_row_blocks), "invalid sub-matrix row index");
1013 static_assert((0 <= j_) && (j_ < num_col_blocks), "invalid sub-matrix column index");
1015 }
1016
1018 template<int i_, int j_>
1019 const typename TupleMatrixElement<i_, j_, FirstRow_, RestRows_...>::Type& at() const
1020 {
1021 static_assert((0 <= i_) && (i_ < num_row_blocks), "invalid sub-matrix row index");
1022 static_assert((0 <= j_) && (j_ < num_col_blocks), "invalid sub-matrix column index");
1024 }
1025
1028 {
1029 return _first.num_rows() + _rest.num_rows();
1030 }
1031
1034 {
1035 const Index cols_f = _first.num_cols();
1036 const Index cols_r = _rest.num_cols();
1037 XASSERTM(cols_f == cols_r, "column count mismatch");
1038 return cols_f;
1039 }
1040
1043 {
1044 return _first.num_nzes() + _rest.num_nzes();
1045 }
1046
1049 {
1050 return _first.num_rows_raw() + _rest.num_rows_raw();
1051 }
1052
1055 {
1056 const Index cols_f = _first.num_cols_raw();
1057 const Index cols_r = _rest.num_cols_raw();
1058 XASSERTM(cols_f == cols_r, "column count mismatch");
1059 return cols_f;
1060 }
1061
1064 {
1065 return _first.num_nzes_raw() + _rest.num_nzes_raw();
1066 }
1067
1073 std::size_t bytes() const
1074 {
1075 return _first.bytes() + _rest.bytes();
1076 }
1077
1079 static String name()
1080 {
1081 return String("TupleMatrix<") + sub_name_list() + ">";
1082 }
1083
1090 void format()
1091 {
1092 first().format();
1093 rest().format();
1094 }
1095
1096 void format(DataType value)
1097 {
1098 first().format(value);
1099 rest().format(value);
1100 }
1101
1109 void format(Random & rng, DataType min, DataType max)
1110 {
1111 first().format(rng, min, max);
1112 rest().format(rng, min, max);
1113 }
1114
1116 void clear()
1117 {
1118 first().clear();
1119 rest().clear();
1120 }
1121
1124 {
1125 return VectorTypeL(first().create_vector_l(), rest().create_vector_l());
1126 }
1127
1130 {
1131 return first().create_vector_r();
1132 }
1133
1140 void apply(VectorTypeL& r, const VectorTypeR& x) const
1141 {
1142 first().apply(r.first(), x);
1143 rest().apply(r.rest(), x);
1144 }
1145
1153 {
1154 first().apply_transposed(r, x.first());
1155 rest().apply_transposed(r, x.rest(), r, DataType(1));
1156 }
1157
1166 void apply(VectorTypeL& r, const VectorTypeR& x, const VectorTypeL& y, DataType alpha = DataType(1)) const
1167 {
1168 first().apply(r.first(), x, y.first(), alpha);
1169 rest().apply(r.rest(), x, y.rest(), alpha);
1170 }
1171
1180 void apply_transposed(VectorTypeR& r, const VectorTypeL& x, const VectorTypeR& y, DataType alpha = DataType(1)) const
1181 {
1182 first().apply_transposed(r, x.first(), y, alpha);
1183 rest().apply_transposed(r, x.rest(), r, alpha);
1184 }
1185
1186 Index row_degree(const Index row) const
1187 {
1188 const Index first_rows = first().num_rows_raw();
1189 if(row < first_rows)
1190 return first().row_degree(row);
1191 else
1192 return rest().row_degree(row - first_rows);
1193 }
1194
1195 template<typename IT2_>
1196 Index get_row_col_indices(const Index row, IT2_* const pcol_idx, const IT2_ col_offset) const
1197 {
1198 const Index first_rows = first().num_rows_raw();
1199 if(row < first_rows)
1200 return first().get_row_col_indices(row, pcol_idx, col_offset);
1201 else
1202 return rest().get_row_col_indices(row - first_rows, pcol_idx, col_offset);
1203 }
1204
1205 template<typename DT2_>
1206 Index get_row_values(const Index row, DT2_ * const pvals) const
1207 {
1208 const Index first_rows = first().num_rows_raw();
1209 if(row < first_rows)
1210 return first().get_row_values(row, pvals);
1211 else
1212 return rest().get_row_values(row - first_rows, pvals);
1213 }
1214
1215 template<typename DT2_>
1216 Index set_row_values(const Index row, const DT2_ * const pvals)
1217 {
1218 const Index first_rows = first().num_rows_raw();
1219 if(row < first_rows)
1220 return first().set_row_values(row, pvals);
1221 else
1222 return rest().set_row_values(row - first_rows, pvals);
1223 }
1224
1226 std::uint64_t get_checkpoint_size(SerialConfig& config)
1227 {
1228 return sizeof(std::uint64_t) + _first.get_checkpoint_size(config) + _rest.get_checkpoint_size(config); //sizeof(std::uint64_t) bits needed to store lenght of checkpointed _first
1229 }
1230
1232 void restore_from_checkpoint_data(std::vector<char> & data)
1233 {
1234 std::uint64_t isize = *(std::uint64_t*) data.data(); //get size of checkpointed _first
1235 std::vector<char>::iterator start = std::begin(data) + sizeof(std::uint64_t);
1236 std::vector<char>::iterator last_of_first = std::begin(data) + sizeof(std::uint64_t) + (int) isize;
1237 std::vector<char> buffer_first(start, last_of_first);
1238 _first.restore_from_checkpoint_data(buffer_first);
1239
1240 data.erase(std::begin(data), last_of_first);
1242 }
1243
1245 std::uint64_t set_checkpoint_data(std::vector<char>& data, SerialConfig& config)
1246 {
1247 std::size_t old_size = data.size();
1248 data.insert(std::end(data), sizeof(std::uint64_t), 0); //add placeholder
1249 std::uint64_t ireal_size = _first.set_checkpoint_data(data, config); //add data of _first to the overall checkpoint and save its size
1250 char* csize = reinterpret_cast<char*>(&ireal_size);
1251 for(std::size_t i(0) ; i < sizeof(std::uint64_t) ; ++i) //overwrite the guessed datalength
1252 {
1253 data[old_size +i] = csize[i];
1254 }
1255 return sizeof(std::uint64_t) + ireal_size + _rest.set_checkpoint_data(data, config); //generate and add checkpoint data for the _rest
1256 }
1257
1258 template<typename OtherFirstRow_, typename... OtherRestRows_>
1259 void convert(const TupleMatrix<OtherFirstRow_, OtherRestRows_...>& source)
1260 {
1261 first().convert(source.first());
1262 rest().convert(source.rest());
1263 }
1264
1265 template<typename OtherFirstRow_, typename... OtherRestRows_>
1266 void copy(const TupleMatrix<OtherFirstRow_, OtherRestRows_...>& source)
1267 {
1268 first().copy(source.first());
1269 rest().copy(source.rest());
1270 }
1271
1272 template<typename OtherFirstRow_, typename... OtherRestRows_>
1273 void copy_to(TupleMatrix<OtherFirstRow_, OtherRestRows_...>& target) const
1274 {
1275 first().copy_to(target.first());
1276 rest().copy_to(target.rest());
1277 }
1278
1279 bool same_layout(const TupleMatrix& other) const
1280 {
1281 return first().same_layout(other.first()) && rest().same_layout(other.rest());
1282 }
1283
1284 DataType max_rel_diff(const TupleMatrix& other) const
1285 {
1286 return Math::max(first().max_rel_diff(other.first()), rest().max_rel_diff(other.rest()));
1287 }
1288 }; // template class TupleMatrix
1289
1291 // specialization for 1 row
1292 template<typename FirstRow_>
1293 class TupleMatrix<FirstRow_>
1294 {
1295 private:
1296 template<typename,typename...>
1297 friend class TupleMatrix;
1298
1299 // sanity check: ensure that 'FirstRow_' is an instance of TupleMatrixRow
1300 static_assert(Intern::IsTMR_<FirstRow_>::value,
1301 "template parameter of TupleMatrix must be TupleMatrixRow<...>");
1302
1303 public:
1305 static constexpr int num_blocks = 1;
1306
1308 static constexpr int num_row_blocks = num_blocks;
1310 static constexpr int num_col_blocks = FirstRow_::num_blocks;
1311
1312
1314 typedef typename FirstRow_::DataType DataType;
1316 typedef typename FirstRow_::IndexType IndexType;
1317
1318 template < typename DT2_ = DataType, typename IT2_ = IndexType>
1319 using ContainerType = TupleMatrix<typename FirstRow_::template ContainerType<DT2_, IT2_> >;
1320
1321 template <typename DataType2_, typename IndexType2_>
1322 using ContainerTypeByDI = ContainerType<DataType2_, IndexType2_>;
1323
1325 typedef TupleVector<typename FirstRow_::VectorTypeL> VectorTypeL;
1327 typedef typename FirstRow_::VectorTypeR VectorTypeR;
1328
1329 protected:
1331 FirstRow_ _first;
1332
1334 static String sub_name_list()
1335 {
1336 return FirstRow_::name();
1337 }
1338
1339 public:
1341 TupleMatrix()
1342 {
1343 }
1344
1346 explicit TupleMatrix(FirstRow_&& the_first) :
1347 _first(std::forward<FirstRow_>(the_first))
1348 {
1349 }
1350
1352 TupleMatrix(TupleMatrix&& other) :
1353 _first(std::forward<FirstRow_>(other._first))
1354 {
1355 }
1356
1358 TupleMatrix& operator=(TupleMatrix&& other)
1359 {
1360 if(this != &other)
1361 {
1362 _first = std::forward<FirstRow_>(other._first);
1363 }
1364 return *this;
1365 }
1366
1368 TupleMatrix(const TupleMatrix&) = delete;
1370 TupleMatrix& operator=(const TupleMatrix&) = delete;
1371
1372 TupleMatrix clone(LAFEM::CloneMode mode = LAFEM::CloneMode::Weak) const
1373 {
1374 return TupleMatrix(_first.clone(mode));
1375 }
1376
1377 void init_workspace() const
1378 {
1379 _first.init_workspace();
1380 }
1381
1382 void reset_workspace() const
1383 {
1384 _first.reset_workspace();
1385 }
1386
1387 void clone(const TupleMatrix& other, CloneMode clone_mode)
1388 {
1389 _first.clone(other._first, clone_mode);
1390 }
1391
1392 void clone(const TupleMatrix& other)
1393 {
1394 _first.clone(other._first);
1395 }
1396
1397 FirstRow_& first()
1398 {
1399 return _first;
1400 }
1401
1402 const FirstRow_& first() const
1403 {
1404 return _first;
1405 }
1406
1407 template<int i_, int j_>
1408 typename TupleMatrixElement<i_, j_, FirstRow_>::Type& at()
1409 {
1410 static_assert(0 == i_, "invalid sub-matrix row index");
1411 static_assert((0 <= j_) && (j_ < num_col_blocks), "invalid sub-matrix column index");
1412 return TupleMatrixElement<i_, j_, FirstRow_>::get(*this);
1413 }
1414
1415 template<int i_, int j_>
1416 const typename TupleMatrixElement<i_, j_, FirstRow_>::Type& at() const
1417 {
1418 static_assert(0 == i_, "invalid sub-matrix row index");
1419 static_assert((0 <= j_) && (j_ < num_col_blocks), "invalid sub-matrix column index");
1420 return TupleMatrixElement<i_, j_, FirstRow_>::get(*this);
1421 }
1422
1423 Index num_rows() const
1424 {
1425 return _first.num_rows();
1426 }
1427
1428 Index num_cols() const
1429 {
1430 return _first.num_cols();
1431 }
1432
1433 Index num_nzes() const
1434 {
1435 return _first.num_nzes();
1436 }
1437
1438 Index num_rows_raw() const
1439 {
1440 return _first.num_rows_raw();
1441 }
1442
1443 Index num_cols_raw() const
1444 {
1445 return _first.num_cols_raw();
1446 }
1447
1448 Index num_nzes_raw() const
1449 {
1450 return _first.num_nzes_raw();
1451 }
1452
1453 std::size_t bytes() const
1454 {
1455 return _first.bytes();
1456 }
1457
1458 static String name()
1459 {
1460 return String("TupleMatrix<") + sub_name_list() + ">";
1461 }
1462
1463 void format()
1464 {
1465 first().format();
1466 }
1467
1468 void format(DataType value)
1469 {
1470 first().format(value);
1471 }
1472
1473 void format(Random & rng, DataType min, DataType max)
1474 {
1475 first().format(rng, min, max);
1476 }
1477
1478 void clear()
1479 {
1480 first().clear();
1481 }
1482
1485 {
1486 return VectorTypeL(first().create_vector_l());
1487 }
1488
1491 {
1492 return first().create_vector_r();
1493 }
1494
1495 void apply(VectorTypeL& r, const VectorTypeR& x) const
1496 {
1497 first().apply(r.first(), x);
1498 }
1499
1500 void apply(VectorTypeL& r, const VectorTypeR& x, const VectorTypeL& y, DataType alpha = DataType(1)) const
1501 {
1502 first().apply(r.first(), x, y.first(), alpha);
1503 }
1504
1505 void apply_transposed(VectorTypeR& r, const VectorTypeL& x) const
1506 {
1507 first().apply_transposed(r, x.first());
1508 }
1509
1510 void apply_transposed(VectorTypeR& r, const VectorTypeL& x, const VectorTypeR& y, DataType alpha = DataType(1)) const
1511 {
1512 first().apply(r, x.first(), y, alpha);
1513 }
1514
1515 Index row_degree(const Index row) const
1516 {
1517 return first().row_degree(row);
1518 }
1519
1520 template<typename IT2_>
1521 Index get_row_col_indices(const Index row, IT2_* const pcol_idx, const IT2_ col_offset) const
1522 {
1523 return first().get_row_col_indices(row, pcol_idx, col_offset);
1524 }
1525
1526 template<typename DT2_>
1527 Index get_row_values(const Index row, DT2_ * const pvals) const
1528 {
1529 return first().get_row_values(row, pvals);
1530 }
1531
1532 template<typename DT2_>
1533 Index set_row_values(const Index row, const DT2_ * const pvals)
1534 {
1535 return first().set_row_values(row, pvals);
1536 }
1537
1539 std::uint64_t get_checkpoint_size(SerialConfig& config)
1540 {
1541 return _first.get_checkpoint_size(config);
1542 }
1543
1545 void restore_from_checkpoint_data(std::vector<char> & data)
1546 {
1547 _first.restore_from_checkpoint_data(data);
1548 }
1549
1551 std::uint64_t set_checkpoint_data(std::vector<char>& data, SerialConfig& config)
1552 {
1553 return _first.set_checkpoint_data(data, config);
1554 }
1555
1556 template<typename OtherFirstRow_>
1557 void convert(const TupleMatrix<OtherFirstRow_>& source)
1558 {
1559 first().convert(source.first());
1560 }
1561
1562 template<typename OtherFirstRow_>
1563 void copy(const TupleMatrix<OtherFirstRow_>& source)
1564 {
1565 first().copy(source.first());
1566 }
1567
1568 template<typename OtherFirstRow_>
1569 void copy_to(TupleMatrix<OtherFirstRow_>& target) const
1570 {
1571 first().copy_to(target.first());
1572 }
1573
1574 bool same_layout(const TupleMatrix& other) const
1575 {
1576 return first().same_layout(other.first());
1577 }
1578
1579 DataType max_rel_diff(const TupleMatrix& other) const
1580 {
1581 return first().max_rel_diff(other.first());
1582 }
1583 }; // template class TupleMatrix
1585 } // namespace LAFEM
1586} // namespace FEAT
#define XASSERTM(expr, msg)
Assertion macro definition with custom message.
Definition: assertion.hpp:263
Config class for serialize parameter.
Definition: container.hpp:50
TupleMatrix element helper class template.
Variadic TupleMatrix class template.
TupleMatrix & operator=(TupleMatrix &&other)
move-assign operator
static constexpr int num_col_blocks
number of column blocks (horizontal size)
FirstRow_::IndexType IndexType
sub-matrix index-type
Index num_nzes_raw() const
Returns the total number of non-zeros in this matrix.
TupleMatrixElement< i_, j_, FirstRow_, RestRows_... >::Type & at()
Returns a sub-matrix block.
void format(Random &rng, DataType min, DataType max)
Reset all elements of the container to random values.
Index num_cols_raw() const
Returns the total number of columns in this matrix.
VectorTypeL create_vector_l() const
Returns a new compatible L-Vector.
VectorTypeR create_vector_r() const
Returns a new compatible R-Vector.
TupleMatrix clone(LAFEM::CloneMode mode=LAFEM::CloneMode::Weak) const
Creates and returns a copy of this matrix.
Index num_rows_raw() const
Returns the total number of rows in this matrix.
void clone(const TupleMatrix &other)
Turns this matrix into a clone of other.
static constexpr int num_row_blocks
number of row blocks (vertical size)
std::size_t bytes() const
Returns the total amount of bytes allocated.
void format()
Reset all elements of the container to a given value or zero if missing.
void clear()
Free all allocated arrays.
std::uint64_t get_checkpoint_size(SerialConfig &config)
Calculate size.
FirstRow_ _first
sanity check: vector types must match for all rows
Index num_rows() const
Returns the total number of rows in this matrix.
TupleMatrix(FirstRow_ &&the_first, RestRows_ &&... the_rest)
Sub-Matrix emplacement constructor.
void apply(VectorTypeL &r, const VectorTypeR &x) const
Calculate .
ContainerType< DataType2_, IndexType2_ > ContainerTypeByDI
this typedef lets you create a vector container with new Datatype and Index types
void restore_from_checkpoint_data(std::vector< char > &data)
Extract object from checkpoint.
FirstRow_::DataType DataType
sub-matrix data-type
RestClass _rest
all remaining sub-matrix rows
Index num_nzes() const
Returns the total number of non-zeros in this matrix.
TupleMatrix & operator=(const TupleMatrix &)=delete
deleted copy-assign operator
void apply_transposed(VectorTypeR &r, const VectorTypeL &x) const
Calculate .
static String sub_name_list()
Returns a list of all sub-matrix type names.
TupleMatrix< typename FirstRow_::template ContainerType< DT2_, IT2_ >, typename RestRows_::template ContainerType< DT2_, IT2_ >... > ContainerType
Our 'base' class type.
void apply_transposed(VectorTypeR &r, const VectorTypeL &x, const VectorTypeR &y, DataType alpha=DataType(1)) const
Calculate .
TupleMatrix(FirstRow_ &&the_first, RestClass &&the_rest)
rest-class emplacement ctor; for internal use only
TupleVector< typename FirstRow_::VectorTypeL, typename RestRows_::VectorTypeL... > VectorTypeL
Compatible L-vector type.
Index num_cols() const
Returns the total number of columns in this matrix.
TupleMatrix(const TupleMatrix &)=delete
deleted copy-ctor
const TupleMatrixElement< i_, j_, FirstRow_, RestRows_... >::Type & at() const
Returns a sub-matrix block.
void clone(const TupleMatrix &other, CloneMode clone_mode)
Turns this matrix into a clone of other.
std::uint64_t set_checkpoint_data(std::vector< char > &data, SerialConfig &config)
TupleMatrix(TupleMatrix &&other)
move ctor
static String name()
Returns a descriptive string for this container.
void apply(VectorTypeL &r, const VectorTypeR &x, const VectorTypeL &y, DataType alpha=DataType(1)) const
Calculate .
FirstRow_::VectorTypeR VectorTypeR
Compatible R-vector type.
TupleMatrix row helper class template.
void restore_from_checkpoint_data(std::vector< char > &data)
Extract object from checkpoint.
static String name()
Returns a descriptive string for this container.
VectorTypeL create_vector_l() const
Returns a new compatible L-Vector.
void format()
Reset all elements of the container to a given value or zero if missing.
void clone(const TupleMatrixRow &other, CloneMode clone_mode)
Turns this matrix into a clone of other.
static constexpr int num_blocks
number of matrix columns
Index num_rows() const
Returns the total number of rows in this matrix.
static String sub_name_list()
Returns a list of all sub-matrix type names.
TupleVector< typename First_::VectorTypeR, typename Rest_::VectorTypeR... > VectorTypeR
Compatible R-vector type.
TupleElement< i_, First_, Rest_... >::Type const & at() const
Returns a sub-matrix block.
TupleMatrixRow< typename First_::template ContainerType< DT2_, IT2_ >, typename Rest_::template ContainerType< DT2_, IT2_ >... > ContainerType
Our 'base' class type.
std::uint64_t get_checkpoint_size(SerialConfig &config)
Calculate size.
VectorTypeR create_vector_r() const
Returns a new compatible R-Vector.
Index num_rows_raw() const
Returns the total number of rows in this matrix.
TupleMatrixRow & operator=(TupleMatrixRow &&other)
move-assign operator
Index num_cols() const
Returns the total number of columns in this matrix.
RestClass _rest
all remaining sub-matrices
TupleMatrixRow(TupleMatrixRow &&other)
move ctor
TupleMatrixRow(First_ &&the_first, RestClass &&the_rest)
rest-class emplacement ctor; for internal use only
Index num_nzes_raw() const
Returns the total number of non-zeros in this matrix.
ContainerType< DataType2_, IndexType2_ > ContainerTypeByDI
this typedef lets you create a vector container with new Datatype and Index types
void format(Random &rng, DataType min, DataType max)
Reset all elements of the container to random values.
TupleMatrixRow(First_ &&the_first, Rest_ &&... the_rest)
Sub-Matrix emplacement constructor.
First_::IndexType IndexType
sub-matrix index-type
std::size_t bytes() const
Returns the total amount of bytes allocated.
Index num_cols_raw() const
Returns the total number of columns in this matrix.
First_::DataType DataType
sub-matrix data-type
TupleMatrixRow clone(LAFEM::CloneMode mode=LAFEM::CloneMode::Weak) const
Creates and returns a copy of this matrix.
void clone(const TupleMatrixRow &other)
Turns this matrix into a clone of other.
TupleMatrixRow(const TupleMatrixRow &)=delete
deleted copy-ctor
Index num_nzes() const
Returns the total number of non-zeros in this matrix.
TupleElement< i_, First_, Rest_... >::Type & at()
Returns a sub-matrix block.
First_::VectorTypeL VectorTypeL
Compatible L-vector type.
First_ _first
the first sub-matrix
TupleMatrixRow & operator=(const TupleMatrixRow &)=delete
deleted copy-assign operator
void clear()
Free all allocated arrays.
std::uint64_t set_checkpoint_data(std::vector< char > &data, SerialConfig &config)
Variadic TupleVector class template.
Pseudo-Random Number Generator.
Definition: random.hpp:54
String class implementation.
Definition: string.hpp:47
@ other
generic/other permutation strategy
T_ max(T_ a, T_ b)
Returns the maximum of two values.
Definition: math.hpp:137
@ rest
restriction (multigrid)
FEAT namespace.
Definition: adjactor.hpp:12
@ value
specifies whether the space should supply basis function values
std::uint64_t Index
Index data type.
Tuple container element helper class template.