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");
259 template <Perspective perspective_ = Perspective::native>
262 return _first.template size<perspective_>() +
_rest.template size<perspective_>();
285 first().format(
value);
286 rest().format(
value);
298 first().format(rng, min, max);
299 rest().format(rng, min, max);
318 first().copy(x.first(), full);
319 rest().copy(x.rest(), full);
324 first().axpy(x.first(), alpha);
325 rest().axpy(x.rest(), alpha);
328 void component_product(
const TupleVector & x,
const TupleVector & y)
330 first().component_product(x.first(), y.first());
331 rest().component_product(x.rest(), y.rest());
336 first().component_invert(x.first(), alpha);
337 rest().component_invert(x.rest(), alpha);
340 void scale(
const TupleVector& x,
DataType alpha)
342 first().scale(x.first(), alpha);
343 rest().scale(x.rest(), alpha);
346 DataType dot(
const TupleVector& x)
const
348 return first().dot(x.first()) +
rest().dot(x.rest());
356 return first().triple_dot(x.first(), y.first())
357 + rest().triple_dot(x.rest(), y.rest());
362 return first().norm2sqr() + rest().norm2sqr();
416 template <
typename First2_,
typename... Rest2_>
424 void set_vec(
DataType *
const pval_set)
const
426 this->first().set_vec(pval_set);
427 this->rest().set_vec(pval_set + this->first().
template size<Perspective::pod>());
431 void set_vec_inv(
const DataType *
const pval_set)
433 this->first().set_vec_inv(pval_set);
434 this->rest().set_vec_inv(pval_set + this->first().
template size<Perspective::pod>());
445 template <
typename First2_,
typename... Rest2_>
448 this->first().convert(other.first());
449 this->rest().convert(other.rest());
466 std::ifstream file(filename.c_str(), std::ifstream::in | std::ifstream::binary);
467 if (! file.is_open())
468 XABORTM(
"Unable to open Vector file " + filename);
486 file.read((
char *)&magic, (
long)(
sizeof(std::uint64_t)));
488 XABORTM(
"Given file or file component is no TupleVector!");
490 file.read((
char *)&count, (
long)(
sizeof(std::uint64_t)));
498 XABORTM(
"Filemode not supported!");
510 std::ofstream file(filename.c_str(), std::ofstream::out | std::ofstream::binary);
511 if (! file.is_open())
512 XABORTM(
"Unable to open Vector file " + filename);
529 size_t gsize(2 *
sizeof(std::uint64_t));
530 std::vector<char> result(gsize);
531 char * array(result.data());
532 std::uint64_t * uiarray(
reinterpret_cast<std::uint64_t *
>(array));
536 file.write(result.data(),
long(result.size()));
542 XABORTM(
"Filemode not supported!");
547 void dump(std::ostream& os)
const
549 os << this->first() <<
",";
550 this->rest().dump(os);
555 template<
typename First_>
556 class TupleVector<First_>
559 template<
typename,
typename...>
560 friend class TupleVector;
579 typedef typename First_::DataType
DataType;
580 typedef typename First_::IndexType
IndexType;
582 template <
typename DT2_ = DataType,
typename IT2_ = IndexType>
583 using ContainerType = TupleVector<typename First_::template ContainerType<DT2_, IT2_> >;
586 template <
typename DataType2_,
typename IndexType2_>
594 return First_::name();
602 explicit TupleVector(First_&& the_first) :
603 _first(std::forward<First_>(the_first))
608 TupleVector(TupleVector&& other) :
614 TupleVector&
operator=(TupleVector&& other)
624 TupleVector(
const TupleVector&) =
delete;
626 TupleVector&
operator=(
const TupleVector&) =
delete;
637 return TupleVector(
_first.clone(mode));
658 void clone(
const TupleVector& other)
668 const First_& first()
const
676 return _first.get_checkpoint_size(config);
682 _first.restore_from_checkpoint_data(data);
688 return _first.set_checkpoint_data(data, config);
691 template <Perspective perspective_ = Perspective::native>
694 return _first.template size<perspective_>();
710 typename TupleElement<i_, First_>::Type&
at()
712 static_assert(i_ == 0,
"invalid sub-vector index");
717 typename TupleElement<i_, First_>::Type
const&
at()
const
719 static_assert(i_ == 0,
"invalid sub-vector index");
730 first().format(rng, min, max);
739 void copy(
const TupleVector& x,
bool full =
false)
741 first().copy(x.first(), full);
746 first().axpy(x.first(), alpha);
749 void component_product(
const TupleVector & x,
const TupleVector & y)
751 first().component_product(x.first(), y.first());
756 first().component_invert(x.first(), alpha);
759 void scale(
const TupleVector& x,
DataType alpha)
761 first().scale(x.first(), alpha);
764 DataType dot(
const TupleVector& x)
const
766 return first().dot(x.first());
771 return first().triple_dot(x.first(), y.first());
774 DataType triple_dot_i(
const TupleVector& x,
const TupleVector& y)
const
776 return first().triple_dot_i(x.first(), y.first());
781 return first().norm2sqr();
786 return first().norm2();
791 return first().max_abs_element();
796 return first().min_abs_element();
801 return first().max_element();
806 return first().min_element();
809 const typename First_::DataType operator()(
Index index)
const
812 return first()(index);
815 void operator()(
Index index,
typename First_::DataType
value)
818 first()(index,
value);
821 void set_vec(
DataType *
const pval_set)
const
823 this->first().set_vec(pval_set);
826 void set_vec_inv(
const DataType *
const pval_set)
828 this->first().set_vec_inv(pval_set);
838 template <
typename First2_>
839 void convert(
const TupleVector<First2_>& other)
841 this->first().convert(
other.first());
845 std::size_t
bytes()
const
861 read_from_binary(filename);
864 XABORTM(
"Filemode not supported!");
879 read_from_binary(file);
882 XABORTM(
"Filemode not supported!");
891 void read_from_binary(
const String& filename)
893 std::ifstream file(filename.c_str(), std::ifstream::in | std::ifstream::binary);
894 if (! file.is_open())
895 XABORTM(
"Unable to open Vector file " + filename);
896 read_from_binary(file);
905 void read_from_binary(std::istream& file)
908 file.read((
char *)&magic, (
long)(
sizeof(std::uint64_t)));
910 XABORTM(
"Given file or file component is no TupleVector!");
912 file.read((
char *)&count, (
long)(
sizeof(std::uint64_t)));
914 XABORTM(
"PowerVector file read in component count mismatch: class has 1 - " +
stringify(count) +
" read in!");
930 write_out_binary(filename);
933 XABORTM(
"Filemode not supported!");
948 write_out_binary(file);
951 XABORTM(
"Filemode not supported!");
960 void write_out_binary(
const String& filename)
const
962 std::ofstream file(filename.c_str(), std::ofstream::out | std::ofstream::binary);
963 if (! file.is_open())
964 XABORTM(
"Unable to open Vector file " + filename);
965 write_out_binary(file);
976 void write_out_binary(std::ostream& file)
const
978 size_t gsize(2 *
sizeof(std::uint64_t));
979 std::vector<char> result(gsize);
980 char * array(result.data());
981 std::uint64_t * uiarray(
reinterpret_cast<std::uint64_t *
>(array));
985 file.write(result.data(),
long(result.size()));
991 void dump(std::ostream& os)
const
997 template <
typename First_,
typename... Rest_>
998 inline std::ostream& operator<< (std::ostream & os,
const TupleVector<First_, Rest_...>& x)
#define ASSERT(expr)
Debug-Assertion macro definition.
#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.
DataType max_rel_diff(const TupleVector< First2_, Rest2_... > &x) const
Retrieve the maximum relative difference of this vector and another one y.max_rel_diff(x) returns .
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 format(DataType value=DataType(0))
Reset all elements of the container to a given value or zero if missing.
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.
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.
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.
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.
Index size() const
Returns the total size of this tuple-vector.
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.