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];
309 template <Perspective perspective_ = Perspective::native>
312 return first().template size<perspective_>() + rest().template size<perspective_>();
323 first().format(
value);
324 rest().format(
value);
336 first().format(rng, min, max);
337 rest().format(rng, min, max);
350 return String(
"PowerVector<") + SubVectorType::name() +
"," +
stringify(count_) +
">";
359 template<
typename SubType2_>
362 first().copy(x.first(), full);
363 rest().copy(x.rest(), full);
380 first().axpy(x.first(), alpha);
381 rest().axpy(x.rest(), alpha);
392 first().component_product(x.first(), y.first());
393 rest().component_product(x.rest(), y.rest());
407 first().component_invert(x.first(), alpha);
408 rest().component_invert(x.rest(), alpha);
422 first().scale(x.first(), alpha);
423 rest().scale(x.rest(), alpha);
434 return first().dot(x.first()) + rest().dot(x.rest());
442 return first().triple_dot(x.first(), y.first())
443 + rest().triple_dot(x.rest(), y.rest());
451 return first().norm2sqr() + rest().norm2sqr();
523 return (first().
same_layout(x.first())) && (rest().same_layout(x.rest()));
537 if (index < first().
size())
539 return first()(index);
543 return rest()(index - first().size());
557 if (index < first().
size())
559 first()(index,
value);
563 rest()(index - first().size(),
value);
569 void set_vec(
DataType *
const pval_set)
const
571 this->first().set_vec(pval_set);
572 this->rest().set_vec(pval_set + this->first().
template size<Perspective::pod>());
576 void set_vec_inv(
const DataType *
const pval_set)
578 this->first().set_vec_inv(pval_set);
579 this->rest().set_vec_inv(pval_set + this->first().
template size<Perspective::pod>());
590 template<
typename SubType2_>
593 this->first().convert(other.first());
594 this->rest().convert(other.rest());
611 std::ifstream file(filename.c_str(), std::ifstream::in | std::ifstream::binary);
612 if (! file.is_open())
613 XABORTM(
"Unable to open Vector file " + filename);
631 file.read((
char *)&magic, (
long)(
sizeof(std::uint64_t)));
633 XABORTM(
"Given file or file component is no PowerVector!");
635 file.read((
char *)&count, (
long)(
sizeof(std::uint64_t)));
637 XABORTM(
"PowerVector file read in component count mismatch: class has " +
stringify(count_) +
"- " +
stringify(count) +
" read in!");
643 XABORTM(
"Filemode not supported!");
655 std::ofstream file(filename.c_str(), std::ofstream::out | std::ofstream::binary);
656 if (! file.is_open())
657 XABORTM(
"Unable to open Vector file " + filename);
674 size_t gsize(2 *
sizeof(std::uint64_t));
675 std::vector<char> result(gsize);
676 char * array(result.data());
677 std::uint64_t * uiarray(
reinterpret_cast<std::uint64_t *
>(array));
681 file.write(result.data(),
long(result.size()));
687 XABORTM(
"Filemode not supported!");
698 template<
typename SubType_>
699 class PowerVector<SubType_, 1>
703 template<
typename,
int>
704 friend class PowerVector;
723 typedef typename SubVectorType::DataType
DataType;
724 typedef typename SubVectorType::IndexType
IndexType;
725 template <
typename DT2_ = DataType,
typename IT2_ = IndexType>
726 using ContainerType = PowerVector<typename SubType_::template ContainerType<DT2_, IT2_>,
Index(1)>;
729 template <
typename DataType2_,
typename IndexType2_>
742 explicit PowerVector(
Index sub_size)
753 _first(std::move(the_first))
757 PowerVector(PowerVector&& other) :
762 PowerVector&
operator=(PowerVector&& other)
772 PowerVector(
const PowerVector&) =
delete;
774 PowerVector&
operator=(
const PowerVector&) =
delete;
789 return PowerVector(
_first.clone(mode));
810 void clone(
const PowerVector& other)
818 static_assert(i == 0,
"invalid sub-vector index");
825 static_assert(i == 0,
"invalid sub-vector index");
831 XASSERTM(i == 0,
"invalid sub-vector index");
837 XASSERTM(i == 0,
"invalid sub-vector index");
854 return _first.get_checkpoint_size(config);
860 _first.restore_from_checkpoint_data(data);
866 return _first.set_checkpoint_data(data, config);
874 template <Perspective perspective_ = Perspective::native>
877 return _first.template size<perspective_>();
887 first().format(rng, min, max);
897 return String(
"PowerVector<") + SubVectorType::name() +
",1>";
900 template<
typename SubType2_>
901 void copy(
const PowerVector<SubType2_, 1>& x,
bool full =
false)
903 first().copy(x.first(), full);
908 first().axpy(x.first(), alpha);
913 first().component_product(x.first(), y.first());
918 first().component_invert(x.first(), alpha);
923 first().scale(x.first(), alpha);
928 return first().dot(x.first());
933 return first().triple_dot(x.first(), y.first());
936 DataType triple_dot_i(
const PowerVector& x,
const PowerVector& y)
const
938 return first().triple_dot_i(x.first(), y.first());
943 return first().norm2sqr();
953 return first().max_abs_element();
958 return first().min_abs_element();
963 return first().max_element();
968 return first().min_element();
975 return first()(index);
982 first()(index,
value);
985 void set_vec(
DataType *
const pval_set)
const
987 this->first().set_vec(pval_set);
990 void set_vec_inv(
const DataType *
const pval_set)
992 this->first().set_vec_inv(pval_set);
1002 template<
typename SubType2_>
1003 void convert(
const PowerVector<SubType2_, 1>& other)
1005 this->first().convert(
other.first());
1009 std::size_t
bytes()
const
1025 read_from_binary(filename);
1028 XABORTM(
"Filemode not supported!");
1043 read_from_binary(file);
1046 XABORTM(
"Filemode not supported!");
1055 void read_from_binary(
const String& filename)
1057 std::ifstream file(filename.c_str(), std::ifstream::in | std::ifstream::binary);
1058 if (! file.is_open())
1059 XABORTM(
"Unable to open Vector file " + filename);
1060 read_from_binary(file);
1069 void read_from_binary(std::istream& file)
1071 std::uint64_t magic;
1072 file.read((
char *)&magic, (
long)(
sizeof(std::uint64_t)));
1074 XABORTM(
"Given file or file component is no PowerVector!");
1075 std::uint64_t count;
1076 file.read((
char *)&count, (
long)(
sizeof(std::uint64_t)));
1078 XABORTM(
"PowerVector file read in component count mismatch: class has 1 - " +
stringify(count) +
" read in!");
1094 write_out_binary(filename);
1097 XABORTM(
"Filemode not supported!");
1112 write_out_binary(file);
1115 XABORTM(
"Filemode not supported!");
1124 void write_out_binary(
const String& filename)
const
1126 std::ofstream file(filename.c_str(), std::ofstream::out | std::ofstream::binary);
1127 if (! file.is_open())
1128 XABORTM(
"Unable to open Vector file " + filename);
1129 write_out_binary(file);
1140 void write_out_binary(std::ostream& file)
const
1142 size_t gsize(2 *
sizeof(std::uint64_t));
1143 std::vector<char> result(gsize);
1144 char * array(result.data());
1145 std::uint64_t * uiarray(
reinterpret_cast<std::uint64_t *
>(array));
1149 file.write(result.data(),
long(result.size()));
1155 template <
typename SubType_, Index count_>
1156 inline void dump_power_vector(std::ostream & os,
const PowerVector<SubType_, count_>& x)
1158 os <<
"," << x.first();
1159 dump_power_vector(os, x.rest());
1162 template <
typename SubType_>
1163 inline void dump_power_vector(std::ostream & os,
const PowerVector<SubType_, 1>& x)
1168 template <
typename SubType_, Index count_>
1169 inline std::ostream& operator<< (std::ostream & os,
const PowerVector<SubType_, count_>& x)
1172 dump_power_vector(os, x);
#define ASSERT(expr)
Debug-Assertion macro definition.
#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
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.
void operator()(Index index, DataType value)
Set specific PowerVector element.
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.
const DataType operator()(Index index) const
Retrieve specific PowerVector element.
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.
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 .
Index size() const
Returns the total number of scalar entries of this power-vector.
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.
void format(DataType value=DataType(0))
Reset all elements of the container to a given value or zero if missing.
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.