FEAT 3
Finite Element Analysis Toolbox
Loading...
Searching...
No Matches
tuple_vector.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// includes, FEAT
9#include <kernel/lafem/meta_element.hpp>
10
11
12// includes, system
13#include <iostream>
14#include <type_traits>
15
16namespace FEAT
17{
18 namespace LAFEM
19 {
30 template<
31 typename First_,
32 typename... Rest_>
34 {
35 private:
36 template<typename,typename...>
37 friend class TupleVector;
38
39 typedef TupleVector<Rest_...> RestClass;
40
42 void _read_from_binary(std::istream& file)
43 {
44 //process first
45 _first.read_from(FileMode::fm_binary, file);
46
47 // append rest
49 }
50
52 void _write_out_binary(std::ostream& file) const
53 {
54 //process first
55 _first.write_out(FileMode::fm_binary, file);
56
57 // append rest
59 }
60
61 public:
63 static constexpr int num_blocks = TupleVector<Rest_...>::num_blocks + 1;
64
66 typedef typename First_::DataType DataType;
68 typedef typename First_::IndexType IndexType;
69
71 template <typename DT2_ = DataType, typename IT2_ = IndexType>
73 typename First_::template ContainerType<DT2_, IT2_>,
74 typename Rest_::template ContainerType<DT2_, IT2_>...>;
75
77 template <typename DataType2_, typename IndexType2_>
79
80 // ensure that all sub-vector have the same data-type
81 static_assert(std::is_same<DataType, typename RestClass::DataType>::value,
82 "sub-vectors have different data-types");
83 static_assert(std::is_same<IndexType, typename RestClass::IndexType>::value,
84 "sub-vectors have different index-types");
85
86 protected:
88 First_ _first;
91
94 {
95 return First_::name() + "," + RestClass::sub_name_list();
96 }
97
98 public:
101 {
102 }
103
105 explicit TupleVector(First_&& the_first, RestClass&& the_rest) :
106 _first(std::forward<First_>(the_first)),
107 _rest(std::forward<RestClass>(the_rest))
108 {
109 }
110
112 explicit TupleVector(First_&& the_first, Rest_&&... the_rest) :
113 _first(std::forward<First_>(the_first)),
114 _rest(std::forward<Rest_>(the_rest)...)
115 {
116 }
117
120 _first(std::forward<First_>(other._first)),
121 _rest(std::forward<RestClass>(other._rest))
122 {
123 }
124
127 {
128 if(this != &other)
129 {
130 _first = std::forward<First_>(other._first);
131 _rest = std::forward<RestClass>(other._rest);
132 }
133 return *this;
134 }
135
137 TupleVector(const TupleVector&) = delete;
140
149 {
150 return TupleVector(_first.clone(mode), _rest.clone(mode));
151 }
152
160 void clone(const TupleVector& other, CloneMode clone_mode)
161 {
162 _first.clone(other._first, clone_mode);
163 _rest.clone(other._rest, clone_mode);
164 }
165
172 void clone(const TupleVector& other)
173 {
174 _first.clone(other._first);
175 _rest.clone(other._rest);
176 }
177
179 std::uint64_t get_checkpoint_size(SerialConfig& config)
180 {
181 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
182 }
183
185 void restore_from_checkpoint_data(std::vector<char> & data)
186 {
187 std::uint64_t isize = *(std::uint64_t*) data.data(); //get size of checkpointed _first
188 std::vector<char>::iterator start = std::begin(data) + sizeof(std::uint64_t);
189 std::vector<char>::iterator last_of_first = std::begin(data) + sizeof(std::uint64_t) + (int) isize;
190 std::vector<char> buffer_first(start, last_of_first);
191 _first.restore_from_checkpoint_data(buffer_first);
192
193 data.erase(std::begin(data), last_of_first);
195 }
196
198 std::uint64_t set_checkpoint_data(std::vector<char>& data, SerialConfig& config)
199 {
200 std::size_t old_size = data.size();
201 data.insert(std::end(data), sizeof(std::uint64_t), 0); //add placeholder
202 std::uint64_t ireal_size = _first.set_checkpoint_data(data, config); //add data of _first to the overall checkpoint and save its size
203 char* csize = reinterpret_cast<char*>(&ireal_size);
204 for(std::size_t i(0) ; i < sizeof(std::uint64_t) ; ++i) //overwrite the guessed datalength
205 {
206 data[old_size +i] = csize[i];
207 }
208
209 return sizeof(std::uint64_t) + ireal_size + _rest.set_checkpoint_data(data, config); //generate and add checkpoint data for the _rest
210 }
211
213 First_& first()
214 {
215 return _first;
216 }
217
218 const First_& first() const
219 {
220 return _first;
221 }
222
223 TupleVector<Rest_...>& rest()
224 {
225 return _rest;
226 }
227
228 const TupleVector<Rest_...>& rest() const
229 {
230 return _rest;
231 }
233
243 template<int i_>
244 typename TupleElement<i_, First_, Rest_...>::Type& at()
245 {
246 static_assert((0 <= i_) && (i_ < num_blocks), "invalid sub-vector index");
248 }
249
251 template<int i_>
252 typename TupleElement<i_, First_, Rest_...>::Type const& at() const
253 {
254 static_assert((0 <= i_) && (i_ < num_blocks), "invalid sub-vector index");
256 }
257
259 Index size() const
260 {
261 return _first.size() + _rest.size();
262 }
263
266 {
267 return _first.size_raw() + _rest.size_raw();
268 }
269
271 int blocks() const
272 {
273 return num_blocks;
274 }
275
277 static String name()
278 {
279 return String("TupleVector<") + sub_name_list() + ">";
280 }
281
288 void format()
289 {
290 first().format();
291 rest().format();
292 }
293
294 void format(DataType value)
295 {
296 first().format(value);
297 rest().format(value);
298 }
299
307 void format(Random & rng, DataType min, DataType max)
308 {
309 first().format(rng, min, max);
310 rest().format(rng, min, max);
311 }
312
314 void clear()
315 {
316 first().clear();
317 rest().clear();
318 }
319
326 //template<typename First2_, typename... Rest2_>
327 void copy(const TupleVector/*<First2_, Rest2_...>*/& x, bool full = false)
328 {
329 first().copy(x.first(), full);
330 rest().copy(x.rest(), full);
331 }
332
333 void axpy(const TupleVector& x, DataType alpha = DataType(1))
334 {
335 first().axpy(x.first(), alpha);
336 rest().axpy(x.rest(), alpha);
337 }
338
339 void component_product(const TupleVector & x, const TupleVector & y)
340 {
341 first().component_product(x.first(), y.first());
342 rest().component_product(x.rest(), y.rest());
343 }
344
345 void component_invert(const TupleVector& x, DataType alpha = DataType(1))
346 {
347 first().component_invert(x.first(), alpha);
348 rest().component_invert(x.rest(), alpha);
349 }
350
351 void scale(const TupleVector& x, DataType alpha)
352 {
353 first().scale(x.first(), alpha);
354 rest().scale(x.rest(), alpha);
355 }
356
357 DataType dot(const TupleVector& x) const
358 {
359 return first().dot(x.first()) + rest().dot(x.rest());
360 }
361
365 DataType triple_dot(const TupleVector& x, const TupleVector& y) const
366 {
367 return first().triple_dot(x.first(), y.first())
368 + rest().triple_dot(x.rest(), y.rest());
369 }
370
371 DataType norm2sqr() const
372 {
373 return first().norm2sqr() + rest().norm2sqr();
374 }
375
376 DataType norm2() const
377 {
378 return Math::sqrt(norm2sqr());
379 }
380
387 {
388 return Math::max(first().max_abs_element(), rest().max_abs_element());
389 }
390
397 {
398 return Math::min(first().min_abs_element(), rest().min_abs_element());
399 }
400
407 {
408 return Math::max(first().max_element(), rest().max_element());
409 }
410
417 {
418 return Math::min(first().min_element(), rest().min_element());
419 }
420
428 {
429 return Math::max(first().max_rel_diff(x.first()), rest().max_rel_diff(x.rest()));
430 }
431
434 Index get_values(DataType * const pval_set) const
435 {
436 Index n = this->first().get_values(pval_set);
437 return this->rest().get_values(pval_set + n) + n;
438 }
439
441 Index set_values(const DataType * const pval_set)
442 {
443 Index n = this->first().set_values(pval_set);
444 return this->rest().set_values(pval_set + n) + n;
445 }
447
455 template <typename First2_, typename... Rest2_>
457 {
458 this->first().convert(other.first());
459 this->rest().convert(other.rest());
460 }
461
463 std::size_t bytes() const
464 {
465 return _first.bytes() + _rest.bytes();
466 }
467
476 bool same_layout(const TupleVector& x) const
477 {
478 return _first.same_layout(x._first) && _rest.same_layout(x._rest);
479 }
480
487 void read_from(FileMode mode, const String& filename)
488 {
489 std::ifstream file(filename.c_str(), std::ifstream::in | std::ifstream::binary);
490 if (! file.is_open())
491 XABORTM("Unable to open Vector file " + filename);
492 read_from(mode, file);
493 file.close();
494 }
495
502 void read_from(FileMode mode, std::istream& file)
503 {
504 switch(mode)
505 {
507 {
508 std::uint64_t magic; // magic_number
509 file.read((char *)&magic, (long)(sizeof(std::uint64_t)));
510 if (magic != 101)
511 XABORTM("Given file or file component is no TupleVector!");
512 std::uint64_t count; // subvector count
513 file.read((char *)&count, (long)(sizeof(std::uint64_t)));
514 //if (count != num_blocks)
515 // XABORTM("TupleVector file read in component count mismatch: class has " + stringify(num_blocks) + "- " + stringify(count) + " read in!");
516
517 _read_from_binary(file);
518 break;
519 }
520 default:
521 XABORTM("Filemode not supported!");
522 }
523 }
524
531 void write_out(FileMode mode, const String& filename) const
532 {
533 std::ofstream file(filename.c_str(), std::ofstream::out | std::ofstream::binary);
534 if (! file.is_open())
535 XABORTM("Unable to open Vector file " + filename);
536 write_out(mode, file);
537 file.close();
538 }
539
546 void write_out(FileMode mode, std::ostream& file) const
547 {
548 switch(mode)
549 {
551 {
552 size_t gsize(2 * sizeof(std::uint64_t)); // magic_number and subvector count
553 std::vector<char> result(gsize);
554 char * array(result.data());
555 std::uint64_t * uiarray(reinterpret_cast<std::uint64_t *>(array));
556 uiarray[0] = 101;
557 uiarray[1] = num_blocks;
558
559 file.write(result.data(), long(result.size()));
560
561 _write_out_binary(file);
562 break;
563 }
564 default:
565 XABORTM("Filemode not supported!");
566 }
567 }
568
570 void dump(std::ostream& os) const
571 {
572 os << this->first() << ",";
573 this->rest().dump(os);
574 }
575 }; // class TupleVector<...>
576
578 template<typename First_>
579 class TupleVector<First_>
580 {
581 private:
582 template<typename,typename...>
583 friend class TupleVector;
584
586 void _read_from_binary(std::istream& file)
587 {
588 //process first
589 _first.read_from(FileMode::fm_binary, file);
590 }
591
593 void _write_out_binary(std::ostream& file) const
594 {
595 //process first
596 _first.write_out(FileMode::fm_binary, file);
597 }
598
599 public:
600 static constexpr int num_blocks = 1;
601
602 typedef typename First_::DataType DataType;
603 typedef typename First_::IndexType IndexType;
604
605 template <typename DT2_ = DataType, typename IT2_ = IndexType>
606 using ContainerType = TupleVector<typename First_::template ContainerType<DT2_, IT2_> >;
607
609 template <typename DataType2_, typename IndexType2_>
610 using ContainerTypeByDI = ContainerType<DataType2_, IndexType2_>;
611
612 protected:
613 First_ _first;
614
615 static String sub_name_list()
616 {
617 return First_::name();
618 }
619
620 public:
621 TupleVector()
622 {
623 }
624
625 explicit TupleVector(First_&& the_first) :
626 _first(std::forward<First_>(the_first))
627 {
628 }
629
631 TupleVector(TupleVector&& other) :
632 _first(std::forward<First_>(other._first))
633 {
634 }
635
637 TupleVector& operator=(TupleVector&& other)
638 {
639 if(this != &other)
640 {
641 _first = std::forward<First_>(other._first);
642 }
643 return *this;
644 }
645
647 TupleVector(const TupleVector&) = delete;
649 TupleVector& operator=(const TupleVector&) = delete;
650
658 TupleVector clone(LAFEM::CloneMode mode = LAFEM::CloneMode::Weak) const
659 {
660 return TupleVector(_first.clone(mode));
661 }
662
670 void clone(const TupleVector& other, CloneMode clone_mode)
671 {
672 _first.clone(other._first, clone_mode);
673 }
674
681 void clone(const TupleVector& other)
682 {
683 _first.clone(other._first);
684 }
685
686 First_& first()
687 {
688 return _first;
689 }
690
691 const First_& first() const
692 {
693 return _first;
694 }
695
697 std::uint64_t get_checkpoint_size(SerialConfig& config)
698 {
699 return _first.get_checkpoint_size(config);
700 }
701
703 void restore_from_checkpoint_data(std::vector<char> & data)
704 {
705 _first.restore_from_checkpoint_data(data);
706 }
707
709 std::uint64_t set_checkpoint_data(std::vector<char>& data, SerialConfig& config)
710 {
711 return _first.set_checkpoint_data(data, config);
712 }
713
715 Index size() const
716 {
717 return _first.size();
718 }
719
721 Index size_raw() const
722 {
723 return _first.size_raw();
724 }
725
727 int blocks() const
728 {
729 return num_blocks;
730 }
731
733 static String name()
734 {
735 return String("TupleVector<") + sub_name_list() + ">";
736 }
737
738 template<int i_>
739 typename TupleElement<i_, First_>::Type& at()
740 {
741 static_assert(i_ == 0, "invalid sub-vector index");
742 return first();
743 }
744
745 template<int i_>
746 typename TupleElement<i_, First_>::Type const& at() const
747 {
748 static_assert(i_ == 0, "invalid sub-vector index");
749 return first();
750 }
751
752 void format()
753 {
754 _first.format();
755 }
756
757 void format(DataType value)
758 {
759 _first.format(value);
760 }
761
762 void format(Random & rng, DataType min, DataType max)
763 {
764 first().format(rng, min, max);
765 }
766
767 void clear()
768 {
769 _first.clear();
770 }
771
772 //template<typename First2_>
773 void copy(const TupleVector/*<First2_>*/& x, bool full = false)
774 {
775 first().copy(x.first(), full);
776 }
777
778 void axpy(const TupleVector& x, DataType alpha = DataType(1))
779 {
780 first().axpy(x.first(), alpha);
781 }
782
783 void component_product(const TupleVector & x, const TupleVector & y)
784 {
785 first().component_product(x.first(), y.first());
786 }
787
788 void component_invert(const TupleVector& x, DataType alpha = DataType(1))
789 {
790 first().component_invert(x.first(), alpha);
791 }
792
793 void scale(const TupleVector& x, DataType alpha)
794 {
795 first().scale(x.first(), alpha);
796 }
797
798 DataType dot(const TupleVector& x) const
799 {
800 return first().dot(x.first());
801 }
802
803 DataType triple_dot(const TupleVector& x, const TupleVector& y) const
804 {
805 return first().triple_dot(x.first(), y.first());
806 }
807
808 DataType triple_dot_i(const TupleVector& x, const TupleVector& y) const
809 {
810 return first().triple_dot_i(x.first(), y.first());
811 }
812
813 DataType norm2sqr() const
814 {
815 return first().norm2sqr();
816 }
817
818 DataType norm2() const
819 {
820 return first().norm2();
821 }
822
824 {
825 return first().max_abs_element();
826 }
827
829 {
830 return first().min_abs_element();
831 }
832
833 DataType max_element() const
834 {
835 return first().max_element();
836 }
837
838 DataType min_element() const
839 {
840 return first().min_element();
841 }
842
843 DataType max_rel_diff(const TupleVector& x) const
844 {
845 return first().max_rel_diff(x.first());
846 }
847
848 Index get_values(DataType * const pval_set) const
849 {
850 return this->first().get_values(pval_set);
851 }
852
853 Index set_values(const DataType * const pval_set)
854 {
855 return this->first().set_values(pval_set);
856 }
857
865 template <typename First2_>
866 void convert(const TupleVector<First2_>& other)
867 {
868 this->first().convert(other.first());
869 }
870
872 std::size_t bytes() const
873 {
874 return _first.bytes();
875 }
876
877 bool same_layout(const TupleVector& x) const
878 {
879 return _first.same_layout(x._first);
880 }
881
888 void read_from(FileMode mode, const String& filename)
889 {
890 switch(mode)
891 {
893 read_from_binary(filename);
894 break;
895 default:
896 XABORTM("Filemode not supported!");
897 }
898 }
899
906 void read_from(FileMode mode, std::istream& file)
907 {
908 switch(mode)
909 {
911 read_from_binary(file);
912 break;
913 default:
914 XABORTM("Filemode not supported!");
915 }
916 }
917
923 void read_from_binary(const String& filename)
924 {
925 std::ifstream file(filename.c_str(), std::ifstream::in | std::ifstream::binary);
926 if (! file.is_open())
927 XABORTM("Unable to open Vector file " + filename);
928 read_from_binary(file);
929 file.close();
930 }
931
937 void read_from_binary(std::istream& file)
938 {
939 std::uint64_t magic; // magic_number
940 file.read((char *)&magic, (long)(sizeof(std::uint64_t)));
941 if (magic != 101)
942 XABORTM("Given file or file component is no TupleVector!");
943 std::uint64_t count; // subvector count
944 file.read((char *)&count, (long)(sizeof(std::uint64_t)));
945 if (count != 1)
946 XABORTM("PowerVector file read in component count mismatch: class has 1 - " + stringify(count) + " read in!");
947
948 _read_from_binary(file);
949 }
950
957 void write_out(FileMode mode, const String& filename) const
958 {
959 switch(mode)
960 {
962 write_out_binary(filename);
963 break;
964 default:
965 XABORTM("Filemode not supported!");
966 }
967 }
968
975 void write_out(FileMode mode, std::ostream& file) const
976 {
977 switch(mode)
978 {
980 write_out_binary(file);
981 break;
982 default:
983 XABORTM("Filemode not supported!");
984 }
985 }
986
992 void write_out_binary(const String& filename) const
993 {
994 std::ofstream file(filename.c_str(), std::ofstream::out | std::ofstream::binary);
995 if (! file.is_open())
996 XABORTM("Unable to open Vector file " + filename);
997 write_out_binary(file);
998 file.close();
999 }
1000
1008 void write_out_binary(std::ostream& file) const
1009 {
1010 size_t gsize(2 * sizeof(std::uint64_t)); // magic_number and subvector count
1011 std::vector<char> result(gsize);
1012 char * array(result.data());
1013 std::uint64_t * uiarray(reinterpret_cast<std::uint64_t *>(array));
1014 uiarray[0] = 101;
1015 uiarray[1] = 1; //fixed num_blocks for tuple vector specialization
1016
1017 file.write(result.data(), long(result.size()));
1018
1019 _write_out_binary(file);
1020 }
1021
1023 void dump(std::ostream& os) const
1024 {
1025 os << this->first();
1026 }
1027 };
1028
1029 template <typename First_, typename... Rest_>
1030 inline std::ostream& operator<< (std::ostream & os, const TupleVector<First_, Rest_...>& x)
1031 {
1032 os << "[";
1033 x.dump(os);
1034 os << "]";
1035 return os;
1036 }
1038 } // namespace LAFEM
1039} // namespace FEAT
#define XABORTM(msg)
Abortion macro definition with custom message.
Definition: assertion.hpp:192
Config class for serialize parameter.
Definition: container.hpp:50
Variadic TupleVector class template.
void clone(const TupleVector &other)
Turns this vector into a clone of other.
TupleElement< i_, First_, Rest_... >::Type & at()
Returns a sub-vector block.
std::size_t bytes() const
Returns the total amount of bytes allocated.
DataType min_abs_element() const
Retrieve the absolute minimum value of this vector.
TupleElement< i_, First_, Rest_... >::Type const & at() const
Returns a sub-vector block.
int blocks() const
Returns the number of blocks in this tuple-vector.
void read_from(FileMode mode, const String &filename)
Read in vector from file.
void clone(const TupleVector &other, CloneMode clone_mode)
Turns this vector into a clone of other.
void write_out(FileMode mode, const String &filename) const
Write out vector to file.
std::uint64_t set_checkpoint_data(std::vector< char > &data, SerialConfig &config)
First_ _first
the first sub-vector
TupleVector(TupleVector &&other)
move ctor
void restore_from_checkpoint_data(std::vector< char > &data)
Extract object from checkpoint.
void copy(const TupleVector &x, bool full=false)
Performs .
void write_out(FileMode mode, std::ostream &file) const
Write out vector to file.
First_::DataType DataType
sub-vector data-type
RestClass _rest
all remaining sub-vectors
void convert(const TupleVector< First2_, Rest2_... > &other)
Conversion method.
TupleVector(const TupleVector &)=delete
deleted copy-ctor
TupleVector< typename First_::template ContainerType< DT2_, IT2_ >, typename Rest_::template ContainerType< DT2_, IT2_ >... > ContainerType
Our 'base' class type.
DataType max_element() const
Retrieve the maximum value of this vector.
TupleVector(First_ &&the_first, Rest_ &&... the_rest)
Sub-Vector emplacement constructor.
void _write_out_binary(std::ostream &file) const
write binary data of _first and _rest to file.
Index size() const
Returns the total size of this tuple-vector.
void _read_from_binary(std::istream &file)
read binary data of _first and _rest to file.
DataType min_element() const
Retrieve the minimum value of this vector.
DataType triple_dot(const TupleVector &x, const TupleVector &y) const
Calculate .
TupleVector(First_ &&the_first, RestClass &&the_rest)
rest-class emplacement ctor; for internal use only
void clear()
Free all allocated arrays.
TupleVector clone(LAFEM::CloneMode mode=LAFEM::CloneMode::Weak) const
Creates and returns a copy of this vector.
Index size_raw() const
Returns the total size of this tuple-vector.
void format()
Reset all elements of the container to a given value or zero if missing.
std::uint64_t get_checkpoint_size(SerialConfig &config)
Calculate size.
void format(Random &rng, DataType min, DataType max)
Reset all elements of the container to random values.
bool same_layout(const TupleVector &x) const
Checks if the structural layout of this vector matches that of another vector. This excludes comparis...
First_::IndexType IndexType
sub-vector index-type
TupleVector & operator=(const TupleVector &)=delete
deleted copy-assign operator
static String sub_name_list()
Returns a list of all sub-vector type names.
DataType max_abs_element() const
Retrieve the absolute maximum value of this vector.
void read_from(FileMode mode, std::istream &file)
Read in vector from stream.
static String name()
Returns a descriptive string for this container.
DataType max_rel_diff(const TupleVector &x) const
Retrieve the maximum relative difference of this vector and another one y.max_rel_diff(x) returns .
TupleVector & operator=(TupleVector &&other)
move-assign operator
void dump(std::ostream &os) const
auxiliary function for operator<< implementation
ContainerType< DataType2_, IndexType2_ > ContainerTypeByDI
this typedef lets you create a vector container with different Data and Index types
static constexpr int num_blocks
number of vector blocks
Pseudo-Random Number Generator.
Definition: random.hpp:54
String class implementation.
Definition: string.hpp:47
@ other
generic/other permutation strategy
T_ sqrt(T_ x)
Returns the square-root of a value.
Definition: math.hpp:300
T_ min(T_ a, T_ b)
Returns the minimum of two values.
Definition: math.hpp:123
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
String stringify(const T_ &item)
Converts an item into a String.
Definition: string.hpp:993
@ value
specifies whether the space should supply basis function values
std::uint64_t Index
Index data type.
Tuple container element helper class template.