9#include <kernel/lafem/meta_element.hpp>
10#include <kernel/lafem/container.hpp>
42 static_assert(count_ > 1,
"invalid block size");
45 template<
typename,
int>
75 typedef typename SubVectorType::DataType
DataType;
77 typedef typename SubVectorType::IndexType
IndexType;
79 template <
typename DT2_ = DataType,
typename IT2_ = IndexType>
83 template <
typename DataType2_,
typename IndexType2_>
130 _first(std::move(the_first)),
131 _rest(std::move(the_rest))
147 _first = std::move(other._first);
148 _rest = std::move(other._rest);
184 _first.clone(other._first, clone_mode);
196 _first.clone(other._first);
212 static_assert((0 <= i_) && (i_ < count_),
"invalid sub-vector index");
220 static_assert((0 <= i_) && (i_ < count_),
"invalid sub-vector index");
235 XASSERTM((0 <= i) && (i < count_),
"invalid sub-vector index");
242 XASSERTM((0 <= i) && (i < count_),
"invalid sub-vector index");
255 std::uint64_t isize = *(std::uint64_t*) data.data();
256 std::vector<char>::iterator start = std::begin(data) +
sizeof(std::uint64_t);
257 std::vector<char>::iterator last_of_first = std::begin(data) +
sizeof(std::uint64_t) + (
int) isize;
258 std::vector<char> buffer_first(start, last_of_first);
259 _first.restore_from_checkpoint_data(buffer_first);
261 data.erase(std::begin(data), last_of_first);
268 std::size_t old_size = data.size();
269 data.insert(std::end(data),
sizeof(std::uint64_t), 0);
270 std::uint64_t ireal_size =
_first.set_checkpoint_data(data, config);
271 char* csize =
reinterpret_cast<char*
>(&ireal_size);
272 for(std::size_t i(0) ; i <
sizeof(std::uint64_t) ; ++i)
274 data[old_size +i] = csize[i];
334 first().format(
value);
335 rest().format(
value);
347 first().format(rng, min, max);
348 rest().format(rng, min, max);
361 return String(
"PowerVector<") + SubVectorType::name() +
"," +
stringify(count_) +
">";
370 template<
typename SubType2_>
373 first().copy(x.first(), full);
374 rest().copy(x.rest(), full);
391 first().axpy(x.first(), alpha);
392 rest().axpy(x.rest(), alpha);
403 first().component_product(x.first(), y.first());
404 rest().component_product(x.rest(), y.rest());
418 first().component_invert(x.first(), alpha);
419 rest().component_invert(x.rest(), alpha);
433 first().scale(x.first(), alpha);
434 rest().scale(x.rest(), alpha);
445 return first().dot(x.first()) + rest().dot(x.rest());
453 return first().triple_dot(x.first(), y.first())
454 + rest().triple_dot(x.rest(), y.rest());
462 return first().norm2sqr() + rest().norm2sqr();
534 return (first().
same_layout(x.first())) && (rest().same_layout(x.rest()));
541 Index n = this->first().get_values(pval_set);
542 return this->rest().get_values(pval_set + n) + n;
548 Index n = this->first().set_values(pval_set);
549 return this->rest().set_values(pval_set + n) + n;
560 template<
typename SubType2_>
563 this->first().convert(other.first());
564 this->rest().convert(other.rest());
581 std::ifstream file(filename.c_str(), std::ifstream::in | std::ifstream::binary);
582 if (! file.is_open())
583 XABORTM(
"Unable to open Vector file " + filename);
601 file.read((
char *)&magic, (
long)(
sizeof(std::uint64_t)));
603 XABORTM(
"Given file or file component is no PowerVector!");
605 file.read((
char *)&count, (
long)(
sizeof(std::uint64_t)));
607 XABORTM(
"PowerVector file read in component count mismatch: class has " +
stringify(count_) +
"- " +
stringify(count) +
" read in!");
613 XABORTM(
"Filemode not supported!");
625 std::ofstream file(filename.c_str(), std::ofstream::out | std::ofstream::binary);
626 if (! file.is_open())
627 XABORTM(
"Unable to open Vector file " + filename);
644 size_t gsize(2 *
sizeof(std::uint64_t));
645 std::vector<char> result(gsize);
646 char * array(result.data());
647 std::uint64_t * uiarray(
reinterpret_cast<std::uint64_t *
>(array));
651 file.write(result.data(),
long(result.size()));
657 XABORTM(
"Filemode not supported!");
668 template<
typename SubType_>
669 class PowerVector<SubType_, 1>
673 template<
typename,
int>
674 friend class PowerVector;
693 typedef typename SubVectorType::DataType
DataType;
694 typedef typename SubVectorType::IndexType
IndexType;
695 template <
typename DT2_ = DataType,
typename IT2_ = IndexType>
696 using ContainerType = PowerVector<typename SubType_::template ContainerType<DT2_, IT2_>,
Index(1)>;
699 template <
typename DataType2_,
typename IndexType2_>
712 explicit PowerVector(
Index sub_size)
723 _first(std::move(the_first))
727 PowerVector(PowerVector&& other) :
732 PowerVector&
operator=(PowerVector&& other)
742 PowerVector(
const PowerVector&) =
delete;
744 PowerVector&
operator=(
const PowerVector&) =
delete;
759 return PowerVector(
_first.clone(mode));
780 void clone(
const PowerVector& other)
788 static_assert(i == 0,
"invalid sub-vector index");
795 static_assert(i == 0,
"invalid sub-vector index");
801 XASSERTM(i == 0,
"invalid sub-vector index");
807 XASSERTM(i == 0,
"invalid sub-vector index");
824 return _first.get_checkpoint_size(config);
830 _first.restore_from_checkpoint_data(data);
836 return _first.set_checkpoint_data(data, config);
868 first().format(rng, min, max);
878 return String(
"PowerVector<") + SubVectorType::name() +
",1>";
881 template<
typename SubType2_>
882 void copy(
const PowerVector<SubType2_, 1>& x,
bool full =
false)
884 first().copy(x.first(), full);
889 first().axpy(x.first(), alpha);
894 first().component_product(x.first(), y.first());
899 first().component_invert(x.first(), alpha);
904 first().scale(x.first(), alpha);
909 return first().dot(x.first());
914 return first().triple_dot(x.first(), y.first());
917 DataType triple_dot_i(
const PowerVector& x,
const PowerVector& y)
const
919 return first().triple_dot_i(x.first(), y.first());
924 return first().norm2sqr();
934 return first().max_abs_element();
939 return first().min_abs_element();
944 return first().max_element();
949 return first().min_element();
954 return first().max_rel_diff(x.first());
959 return this->first().get_values(pval_set);
964 return this->first().set_values(pval_set);
974 template<
typename SubType2_>
975 void convert(
const PowerVector<SubType2_, 1>& other)
977 this->first().convert(
other.first());
981 std::size_t
bytes()
const
997 read_from_binary(filename);
1000 XABORTM(
"Filemode not supported!");
1015 read_from_binary(file);
1018 XABORTM(
"Filemode not supported!");
1027 void read_from_binary(
const String& filename)
1029 std::ifstream file(filename.c_str(), std::ifstream::in | std::ifstream::binary);
1030 if (! file.is_open())
1031 XABORTM(
"Unable to open Vector file " + filename);
1032 read_from_binary(file);
1041 void read_from_binary(std::istream& file)
1043 std::uint64_t magic;
1044 file.read((
char *)&magic, (
long)(
sizeof(std::uint64_t)));
1046 XABORTM(
"Given file or file component is no PowerVector!");
1047 std::uint64_t count;
1048 file.read((
char *)&count, (
long)(
sizeof(std::uint64_t)));
1050 XABORTM(
"PowerVector file read in component count mismatch: class has 1 - " +
stringify(count) +
" read in!");
1066 write_out_binary(filename);
1069 XABORTM(
"Filemode not supported!");
1084 write_out_binary(file);
1087 XABORTM(
"Filemode not supported!");
1096 void write_out_binary(
const String& filename)
const
1098 std::ofstream file(filename.c_str(), std::ofstream::out | std::ofstream::binary);
1099 if (! file.is_open())
1100 XABORTM(
"Unable to open Vector file " + filename);
1101 write_out_binary(file);
1112 void write_out_binary(std::ostream& file)
const
1114 size_t gsize(2 *
sizeof(std::uint64_t));
1115 std::vector<char> result(gsize);
1116 char * array(result.data());
1117 std::uint64_t * uiarray(
reinterpret_cast<std::uint64_t *
>(array));
1121 file.write(result.data(),
long(result.size()));
1127 template <
typename SubType_, Index count_>
1128 inline void dump_power_vector(std::ostream & os,
const PowerVector<SubType_, count_>& x)
1130 os <<
"," << x.first();
1131 dump_power_vector(os, x.rest());
1134 template <
typename SubType_>
1135 inline void dump_power_vector(std::ostream & os,
const PowerVector<SubType_, 1>& x)
1140 template <
typename SubType_, Index count_>
1141 inline std::ostream& operator<< (std::ostream & os,
const PowerVector<SubType_, count_>& x)
1144 dump_power_vector(os, x);
#define XABORTM(msg)
Abortion macro definition with custom message.
#define XASSERTM(expr, msg)
Assertion macro definition with custom message.
Power-Vector meta class template.
PowerVector & operator=(const PowerVector &)=delete
deleted copy-assign operator
Index size() const
Returns the total size of this tuple-vector.
void _write_out_binary(std::ostream &file) const
write binary data of _first and _rest to file.
void convert(const PowerVector< SubType2_, count_ > &other)
Conversion method.
void write_out(FileMode mode, const String &filename) const
Write out vector to file.
DataType norm2sqr() const
Returns the squared euclid norm of this vector.
bool same_layout(const PowerVector &x) const
Checks if the structural layout of this vector matches that of another vector. This excludes comparis...
SubType_ SubVectorType
sub-vector type
const SubVectorType & get(int i) const
Returns a sub-vector block.
PowerVector(SubVectorType &&the_first, RestClass &&the_rest)
base-class constructor; for internal use only
static String name()
Returns a descriptive string for this container.
PowerVector(PowerVector &&other)
move CTOR
PowerVector(const PowerVector &)=delete
deleted copy-ctor
void scale(const PowerVector &x, DataType alpha)
Performs .
SubVectorType::IndexType IndexType
sub-vector index type
DataType min_abs_element() const
Retrieve the absolute minimum value of this vector.
PowerVector(Index sub_size, DataType value)
Constructor.
static constexpr int num_blocks
number of vector blocks
ContainerType< DataType2_, IndexType2_ > ContainerTypeByDI
this typedef lets you create a vector container with different Data and Index types
RestClass _rest
the remaining part
PowerVector< typename SubType_::template ContainerType< DT2_, IT2_ >, count_ > ContainerType
Our 'base' class type.
void clone(const PowerVector &other)
Turns this vector into a clone of other.
void copy(const PowerVector< SubType2_, count_ > &x, bool full=false)
Performs .
void restore_from_checkpoint_data(std::vector< char > &data)
Extract object from checkpoint.
void axpy(const PowerVector &x, DataType alpha=DataType(1))
Performs .
PowerVector()
default CTOR
void component_invert(const PowerVector &x, DataType alpha=DataType(1))
Performs .
PowerVector clone(LAFEM::CloneMode mode=LAFEM::CloneMode::Weak) const
Creates and returns a copy of this vector.
const SubVectorType & at() const
Returns a sub-vector block.
void write_out(FileMode mode, std::ostream &file) const
Write out vector to file.
DataType min_element() const
Retrieve the minimum value of this vector.
void clear()
Free all allocated arrays.
int blocks() const
Returns the number of blocks in this power-vector.
PowerVector< SubType_, count_ - 1 > RestClass
base-class typedef
std::uint64_t set_checkpoint_data(std::vector< char > &data, SerialConfig &config)
SubVectorType::DataType DataType
sub-vector data type
std::uint64_t get_checkpoint_size(SerialConfig &config)
Calculate size.
void format()
Reset all elements of the container to a given value or zero if missing.
SubVectorType _first
the first sub-vector
DataType max_abs_element() const
Retrieve the absolute maximum value of this vector.
DataType max_element() const
Retrieve the maximum value of this vector.
void _read_from_binary(std::istream &file)
read binary data of _first and _rest to file.
DataType triple_dot(const PowerVector &x, const PowerVector &y) const
Calculate .
DataType max_rel_diff(const PowerVector &x) const
Retrieve the maximum relative difference of this vector and another one y.max_rel_diff(x) returns .
PowerVector & operator=(PowerVector &&other)
move-assign operator
std::size_t bytes() const
Returns the total amount of bytes allocated.
void format(Random &rng, DataType min, DataType max)
Reset all elements of the container to random values.
void read_from(FileMode mode, const String &filename)
Read in vector from file.
SubVectorType & at()
Returns a sub-vector block.
Index size_raw() const
Returns the total size of this tuple-vector.
SubVectorType & get(int i)
Returns a sub-vector block.
PowerVector(Index sub_size)
Constructor.
void read_from(FileMode mode, std::istream &file)
Read in vector from stream.
DataType norm2() const
Returns the euclid norm of this vector.
void component_product(const PowerVector &x, const PowerVector &y)
Performs .
DataType dot(const PowerVector &x) const
Returns the dot-product of this vector and another vector.
void clone(const PowerVector &other, CloneMode clone_mode)
Turns this vector into a clone of other.
virtual ~PowerVector()
virtual destructor
Config class for serialize parameter.
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.
Power container element helper class template.