9#include <kernel/lafem/meta_element.hpp>
36 template<
typename,
typename...>
71 template <
typename DT2_ = DataType,
typename IT2_ = IndexType>
77 template <
typename DataType2_,
typename IndexType2_>
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");
106 _first(std::forward<First_>(the_first)),
113 _first(std::forward<First_>(the_first)),
114 _rest(std::forward<Rest_>(the_rest)...)
130 _first = std::forward<First_>(other._first);
131 _rest = std::forward<RestClass>(other._rest);
162 _first.clone(other._first, clone_mode);
174 _first.clone(other._first);
187 std::uint64_t isize = *(std::uint64_t*) data.data();
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);
193 data.erase(std::begin(data), last_of_first);
200 std::size_t old_size = data.size();
201 data.insert(std::end(data),
sizeof(std::uint64_t), 0);
202 std::uint64_t ireal_size =
_first.set_checkpoint_data(data, config);
203 char* csize =
reinterpret_cast<char*
>(&ireal_size);
204 for(std::size_t i(0) ; i <
sizeof(std::uint64_t) ; ++i)
206 data[old_size +i] = csize[i];
218 const First_& first()
const
223 TupleVector<Rest_...>& rest()
228 const TupleVector<Rest_...>&
rest()
const
246 static_assert((0 <= i_) && (i_ <
num_blocks),
"invalid sub-vector index");
254 static_assert((0 <= i_) && (i_ <
num_blocks),
"invalid sub-vector index");
296 first().format(
value);
297 rest().format(
value);
309 first().format(rng, min, max);
310 rest().format(rng, min, max);
329 first().copy(x.first(), full);
330 rest().copy(x.rest(), full);
335 first().axpy(x.first(), alpha);
336 rest().axpy(x.rest(), alpha);
339 void component_product(
const TupleVector & x,
const TupleVector & y)
341 first().component_product(x.first(), y.first());
342 rest().component_product(x.rest(), y.rest());
347 first().component_invert(x.first(), alpha);
348 rest().component_invert(x.rest(), alpha);
351 void scale(
const TupleVector& x,
DataType alpha)
353 first().scale(x.first(), alpha);
354 rest().scale(x.rest(), alpha);
357 DataType dot(
const TupleVector& x)
const
359 return first().dot(x.first()) +
rest().dot(x.rest());
367 return first().triple_dot(x.first(), y.first())
368 + rest().triple_dot(x.rest(), y.rest());
373 return first().norm2sqr() + rest().norm2sqr();
436 Index n = this->first().get_values(pval_set);
437 return this->rest().get_values(pval_set + n) + n;
443 Index n = this->first().set_values(pval_set);
444 return this->rest().set_values(pval_set + n) + n;
455 template <
typename First2_,
typename... Rest2_>
458 this->first().convert(other.first());
459 this->rest().convert(other.rest());
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);
509 file.read((
char *)&magic, (
long)(
sizeof(std::uint64_t)));
511 XABORTM(
"Given file or file component is no TupleVector!");
513 file.read((
char *)&count, (
long)(
sizeof(std::uint64_t)));
521 XABORTM(
"Filemode not supported!");
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);
552 size_t gsize(2 *
sizeof(std::uint64_t));
553 std::vector<char> result(gsize);
554 char * array(result.data());
555 std::uint64_t * uiarray(
reinterpret_cast<std::uint64_t *
>(array));
559 file.write(result.data(),
long(result.size()));
565 XABORTM(
"Filemode not supported!");
570 void dump(std::ostream& os)
const
572 os << this->first() <<
",";
573 this->rest().dump(os);
578 template<
typename First_>
579 class TupleVector<First_>
582 template<
typename,
typename...>
583 friend class TupleVector;
602 typedef typename First_::DataType
DataType;
603 typedef typename First_::IndexType
IndexType;
605 template <
typename DT2_ = DataType,
typename IT2_ = IndexType>
606 using ContainerType = TupleVector<typename First_::template ContainerType<DT2_, IT2_> >;
609 template <
typename DataType2_,
typename IndexType2_>
617 return First_::name();
625 explicit TupleVector(First_&& the_first) :
626 _first(std::forward<First_>(the_first))
631 TupleVector(TupleVector&& other) :
637 TupleVector&
operator=(TupleVector&& other)
647 TupleVector(
const TupleVector&) =
delete;
649 TupleVector&
operator=(
const TupleVector&) =
delete;
660 return TupleVector(
_first.clone(mode));
681 void clone(
const TupleVector& other)
691 const First_& first()
const
699 return _first.get_checkpoint_size(config);
705 _first.restore_from_checkpoint_data(data);
711 return _first.set_checkpoint_data(data, config);
739 typename TupleElement<i_, First_>::Type&
at()
741 static_assert(i_ == 0,
"invalid sub-vector index");
746 typename TupleElement<i_, First_>::Type
const&
at()
const
748 static_assert(i_ == 0,
"invalid sub-vector index");
764 first().format(rng, min, max);
773 void copy(
const TupleVector& x,
bool full =
false)
775 first().copy(x.first(), full);
780 first().axpy(x.first(), alpha);
783 void component_product(
const TupleVector & x,
const TupleVector & y)
785 first().component_product(x.first(), y.first());
790 first().component_invert(x.first(), alpha);
793 void scale(
const TupleVector& x,
DataType alpha)
795 first().scale(x.first(), alpha);
798 DataType dot(
const TupleVector& x)
const
800 return first().dot(x.first());
805 return first().triple_dot(x.first(), y.first());
808 DataType triple_dot_i(
const TupleVector& x,
const TupleVector& y)
const
810 return first().triple_dot_i(x.first(), y.first());
815 return first().norm2sqr();
820 return first().norm2();
825 return first().max_abs_element();
830 return first().min_abs_element();
835 return first().max_element();
840 return first().min_element();
845 return first().max_rel_diff(x.first());
850 return this->first().get_values(pval_set);
855 return this->first().set_values(pval_set);
865 template <
typename First2_>
866 void convert(
const TupleVector<First2_>& other)
868 this->first().convert(
other.first());
872 std::size_t
bytes()
const
879 return _first.same_layout(x._first);
893 read_from_binary(filename);
896 XABORTM(
"Filemode not supported!");
911 read_from_binary(file);
914 XABORTM(
"Filemode not supported!");
923 void read_from_binary(
const String& filename)
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);
937 void read_from_binary(std::istream& file)
940 file.read((
char *)&magic, (
long)(
sizeof(std::uint64_t)));
942 XABORTM(
"Given file or file component is no TupleVector!");
944 file.read((
char *)&count, (
long)(
sizeof(std::uint64_t)));
946 XABORTM(
"PowerVector file read in component count mismatch: class has 1 - " +
stringify(count) +
" read in!");
962 write_out_binary(filename);
965 XABORTM(
"Filemode not supported!");
980 write_out_binary(file);
983 XABORTM(
"Filemode not supported!");
992 void write_out_binary(
const String& filename)
const
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);
1008 void write_out_binary(std::ostream& file)
const
1010 size_t gsize(2 *
sizeof(std::uint64_t));
1011 std::vector<char> result(gsize);
1012 char * array(result.data());
1013 std::uint64_t * uiarray(
reinterpret_cast<std::uint64_t *
>(array));
1017 file.write(result.data(),
long(result.size()));
1023 void dump(std::ostream& os)
const
1025 os << this->first();
1029 template <
typename First_,
typename... Rest_>
1030 inline std::ostream& operator<< (std::ostream & os,
const TupleVector<First_, Rest_...>& x)
#define XABORTM(msg)
Abortion macro definition with custom message.
Config class for serialize parameter.
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.
TupleVector()
default CTOR
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.
String class implementation.
@ other
generic/other permutation strategy
T_ sqrt(T_ x)
Returns the square-root of a value.
T_ min(T_ a, T_ b)
Returns the minimum of two values.
T_ max(T_ a, T_ b)
Returns the maximum of two values.
@ rest
restriction (multigrid)
String stringify(const T_ &item)
Converts an item into a String.
@ value
specifies whether the space should supply basis function values
std::uint64_t Index
Index data type.
Tuple container element helper class template.