9#include <kernel/lafem/power_vector.hpp>
10#include <kernel/lafem/sparse_layout.hpp>
11#include <kernel/lafem/meta_element.hpp>
12#include <kernel/lafem/container.hpp>
41 static_assert(blocks_ > 1,
"invalid block size");
44 template<
typename,
int>
54 typedef typename SubMatrixType::DataType
DataType;
56 typedef typename SubMatrixType::IndexType
IndexType;
64 template <
typename DT2_ = DataType,
typename IT2_ = IndexType>
80 _first(std::move(the_first)),
81 _rest(std::move(the_rest))
134 std::getline(file, line);
136 }
while (line.find(
"%%") == 0 || line ==
"");
139 _first = std::move(tmp_first);
141 RestClass tmp_rest(mode, file, directory);
142 _rest = std::move(tmp_rest);
154 auto found = filename.rfind(
"/");
155 if (found != std::string::npos)
157 directory = filename.substr(0, found + 1);
160 std::ifstream file(filename.c_str(), std::ifstream::in);
161 if (! file.is_open())
162 XABORTM(
"Unable to open Matrix file " + filename);
165 std::getline(file, line);
166 if (line.find(
"%%MatrixMarket powerrowmatrix coordinate real general") == String::npos)
167 XABORTM(
"Input-file is not a complatible file");
171 _first = std::move(other._first);
172 _rest = std::move(other._rest);
182 _first = std::move(other._first);
183 _rest = std::move(other._rest);
206 std::ofstream file(filename.c_str(), std::ofstream::out);
207 if (! file.is_open())
208 XABORTM(
"Unable to open Matrix file " + filename);
211 auto found = filename.rfind(
".");
212 if (found != std::string::npos)
214 suffix = filename.substr(found);
215 filename.erase(found);
217 found = filename.rfind(
"/");
218 if (found != std::string::npos)
220 directory = filename.substr(0, found + 1);
221 filename.erase(0, found + 1);
224 file <<
"%%MatrixMarket powerrowmatrix coordinate real general\n";
225 for (
Index i(1); i <= blocks_; ++i)
227 file << filename <<
"_pr" << i << suffix <<
"\n";
245 _first.write_out(mode, directory + prefix +
"_pr" +
stringify(length + 1 - blocks_) + suffix);
275 template<
int i_,
int j_>
278 static_assert(i_ == 0,
"invalid sub-matrix index");
279 static_assert((0 <= j_) && (j_ < blocks_),
"invalid sub-matrix index");
284 template<
int i_,
int j_>
287 static_assert(i_ == 0,
"invalid sub-matrix index");
288 static_assert((0 <= j_) && (j_ < blocks_),
"invalid sub-matrix index");
303 XASSERTM((i == 0) && (0 <= j) && (j < blocks_),
"invalid sub-matrix index");
310 XASSERTM((i == 0) && (0 <= j) && (j < blocks_),
"invalid sub-matrix index");
335 int row_blocks()
const
340 int col_blocks()
const
354 return first().num_rows();
357 Index num_rows_raw()
const
359 return first().num_rows_raw();
370 return first().num_cols() + rest().num_cols();
373 Index num_cols_raw()
const
375 return first().num_cols_raw() + rest().num_cols_raw();
386 return first().num_nzes() + rest().num_nzes();
389 Index num_nzes_raw()
const
391 return first().num_nzes_raw() + rest().num_nzes_raw();
397 return String(
"PowerRowMatrix<") + SubMatrixType::name() +
"," +
stringify(blocks_) +
">";
414 first().format(
value);
415 rest().format(
value);
441 first().apply(r, x.first());
442 rest().apply(r, x.rest(), r,
DataType(1));
447 XASSERTM(r.
size() == this->num_rows(),
"Vector size of r does not match!");
448 XASSERTM(x.
size() == this->num_cols(),
"Vector size of x does not match!");
453 first().apply(r, x_first);
454 rest().apply(r, x_rest, r,
DataType(1));
471 first().apply_transposed(r.first(), x);
472 rest().apply_transposed(r.rest(), x);
477 XASSERTM(r.
size() == this->num_cols(),
"Vector size of r does not match!");
478 XASSERTM(x.
size() == this->num_rows(),
"Vector size of x does not match!");
483 first().apply_transposed(r_first, x);
484 rest().apply_transposed(r_rest, x);
505 first().apply(r, x.first(), y, alpha);
506 rest().apply(r, x.rest(), r, alpha);
512 XASSERTM(r.
size() == this->num_rows(),
"Vector size of r does not match!");
513 XASSERTM(x.
size() == this->num_cols(),
"Vector size of x does not match!");
514 XASSERTM(y.
size() == this->num_rows(),
"Vector size of y does not match!");
519 first().apply(r, x_first, y, alpha);
520 rest().apply(r, x_rest, r, alpha);
541 first().apply_transposed(r.first(), x, y.first(), alpha);
542 rest().apply_transposed(r.rest(), x, y.rest(), alpha);
548 XASSERTM(r.
size() == this->num_cols(),
"Vector size of r does not match!");
549 XASSERTM(x.
size() == this->num_rows(),
"Vector size of x does not match!");
550 XASSERTM(y.
size() == this->num_cols(),
"Vector size of y does not match!");
558 first().apply(r_first, x, y_first, alpha);
559 rest().apply(r_rest, x, y_rest, alpha);
583 return this->first().same_layout(x.first()) && (this->rest().same_layout(x.rest()));
589 return first().create_vector_l();
601 return first().row_degree(row) + rest().row_degree(row);
604 template<
typename IT2_>
605 Index get_row_col_indices(
const Index row, IT2_*
const pcol_idx,
const IT2_ col_offset)
const
607 const Index first_cols = first().num_cols_raw();
608 const Index first_degree = first().get_row_col_indices(row, pcol_idx, col_offset);
609 return first_degree + rest().get_row_col_indices(row, pcol_idx + first_degree, col_offset + IT2_(first_cols));
612 template<
typename DT2_>
613 Index get_row_values(
const Index row, DT2_ *
const pvals)
const
615 const Index first_degree = first().get_row_values(row, pvals);
616 return first_degree + rest().get_row_values(row, pvals + first_degree);
619 template<
typename DT2_>
620 Index set_row_values(
const Index row,
const DT2_ *
const pvals)
622 const Index first_degree = first().set_row_values(row, pvals);
623 return first_degree +
rest().set_row_values(row, pvals + first_degree);
637 std::uint64_t isize = *(std::uint64_t*) data.data();
638 std::vector<char>::iterator start = std::begin(data) +
sizeof(std::uint64_t);
639 std::vector<char>::iterator last_of_first = std::begin(data) +
sizeof(std::uint64_t) + (
int) isize;
640 std::vector<char> buffer_first(start, last_of_first);
641 _first.restore_from_checkpoint_data(buffer_first);
643 data.erase(std::begin(data), last_of_first);
650 std::size_t old_size = data.size();
651 data.insert(std::end(data),
sizeof(std::uint64_t), 0);
652 std::uint64_t ireal_size =
_first.set_checkpoint_data(data, config);
653 char* csize =
reinterpret_cast<char*
>(&ireal_size);
654 for(std::size_t i(0) ; i <
sizeof(std::uint64_t) ; ++i)
656 data[old_size +i] = csize[i];
669 template <
typename SubType2_>
672 this->first().convert(source.first());
673 this->rest().convert(source.rest());
676 template <
typename SubType2_>
679 this->first().copy(source.first());
680 this->rest().copy(source.rest());
683 template <
typename SubType2_>
684 void copy_to(PowerRowMatrix<SubType2_, blocks_> & target)
const
686 this->first().copy_to(target.first());
687 this->rest().copy_to(target.rest());
692 template<
typename SubType_>
693 class PowerRowMatrix<SubType_, 1>
695 template<
typename,
int>
696 friend class PowerRowMatrix;
700 typedef typename SubMatrixType::DataType
DataType;
701 typedef typename SubMatrixType::IndexType
IndexType;
705 typedef typename SubMatrixType::VectorTypeL
VectorTypeL;
707 typedef PowerVector<typename SubMatrixType::VectorTypeR, 1>
VectorTypeR;
709 template <
typename DT2_ = DataType,
typename IT2_ = IndexType>
710 using ContainerType = PowerRowMatrix<typename SubType_::template ContainerType<DT2_, IT2_>, 1>;
720 _first(std::move(the_first))
731 explicit PowerRowMatrix(
const SparseLayout<IndexType, layout_id>& layout) :
737 PowerRowMatrix(PowerRowMatrix&& other) :
743 explicit PowerRowMatrix(
FileMode mode,
const String& filename)
749 explicit PowerRowMatrix(
FileMode mode, std::istream& file,
const String& directory =
"")
755 std::getline(file, line);
757 }
while (line.find(
"%%") == 0 || line ==
"");
760 _first = std::move(tmp_first);
766 auto found = filename.rfind(
"/");
767 if (found != std::string::npos)
769 directory = filename.substr(0, found + 1);
772 std::ifstream file(filename.c_str(), std::ifstream::in);
773 if (! file.is_open())
774 XABORTM(
"Unable to open Matrix file " + filename);
776 PowerRowMatrix
other(mode, file, directory);
784 PowerRowMatrix&
operator=(PowerRowMatrix&& other)
794 PowerRowMatrix(
const PowerRowMatrix&) =
delete;
796 PowerRowMatrix&
operator=(
const PowerRowMatrix&) =
delete;
805 std::ofstream file(filename.c_str(), std::ofstream::out);
806 if (! file.is_open())
807 XABORTM(
"Unable to open Matrix file " + filename);
809 String suffix, directory;
810 auto found = filename.rfind(
".");
811 if (found != std::string::npos)
813 suffix = filename.substr(found);
814 filename.erase(found);
816 found = filename.rfind(
"/");
817 if (found != std::string::npos)
819 directory = filename.substr(0, found + 1);
820 filename.erase(0, found + 1);
823 file <<
"%%MatrixMarket powerrowmatrix coordinate real general\n";
824 file << filename <<
"_pr" << 1 << suffix <<
"\n";
833 _first.write_out(mode, directory + prefix +
"_pr" +
stringify(length) + suffix);
838 return PowerRowMatrix(
_first.clone(mode));
842 std::size_t
bytes()
const
848 template<
int i,
int j>
851 static_assert(i == 0,
"invalid sub-matrix index");
852 static_assert(j == 0,
"invalid sub-matrix index");
856 template<
int i,
int j>
859 static_assert(i == 0,
"invalid sub-matrix index");
860 static_assert(j == 0,
"invalid sub-matrix index");
866 XASSERTM((i == 0) && (j == 0),
"invalid sub-matrix index");
872 XASSERTM((i == 0) && (j == 0),
"invalid sub-matrix index");
886 int row_blocks()
const
891 int col_blocks()
const
898 return first().num_rows();
903 return first().num_cols();
908 return first().num_nzes();
911 Index num_rows_raw()
const
913 return first().num_rows_raw();
916 Index num_cols_raw()
const
918 return first().num_cols_raw();
921 Index num_nzes_raw()
const
923 return first().num_nzes_raw();
928 return String(
"PowerRowMatrix<") + SubMatrixType::name() +
"," +
stringify(1) +
">";
938 first().format(
value);
948 first().apply(r, x.first());
951 void apply(DenseVector<DataType, IndexType>& r,
const DenseVector<DataType, IndexType>& x)
const
958 first().apply(r, x.first(), y, alpha);
961 void apply(DenseVector<DataType, IndexType>& r,
const DenseVector<DataType, IndexType>& x,
962 const DenseVector<DataType, IndexType>& y,
DataType alpha =
DataType(1))
const
964 first().apply(r, x, y, alpha);
969 first().apply_transposed(r.first(), x);
972 void apply_transposed(DenseVector<DataType, IndexType>& r,
const DenseVector<DataType, IndexType>& x)
const
974 first().apply_transposed(r, x);
979 first().apply_transposed(r.first(), x, y.first(), alpha);
982 void apply_transposed(DenseVector<DataType, IndexType>& r,
const DenseVector<DataType, IndexType>& x,
983 const DenseVector<DataType, IndexType>& y,
DataType alpha =
DataType(1))
const
985 first().apply_transposed(r, x, y, alpha);
996 return this->first().max_rel_diff(x.first());
1009 return this->first().same_layout(x.first());
1026 return first().row_degree(row);
1029 template<
typename IT2_>
1030 Index get_row_col_indices(
const Index row, IT2_*
const pcol_idx,
const IT2_ col_offset)
const
1032 return first().get_row_col_indices(row, pcol_idx, col_offset);
1035 template<
typename DT2_>
1036 Index get_row_values(
const Index row, DT2_ *
const pvals)
const
1038 return first().get_row_values(row, pvals);
1041 template<
typename DT2_>
1042 Index set_row_values(
const Index row,
const DT2_ *
const pvals)
1044 return first().set_row_values(row, pvals);
1050 return _first.get_checkpoint_size(config);
1056 _first.restore_from_checkpoint_data(data);
1062 return _first.set_checkpoint_data(data, config);
1072 template <
typename SubType2_>
1073 void convert(
const PowerRowMatrix<SubType2_, 1> & source)
1075 this->first().convert(source.first());
1078 template <
typename SubType2_>
1079 void copy(
const PowerRowMatrix<SubType2_, 1> & source)
1081 this->first().copy(source.first());
1084 template <
typename SubType2_>
1085 void copy_to(PowerRowMatrix<SubType2_, 1> & target)
const
1087 this->first().copy_to(target.first());
#define XABORTM(msg)
Abortion macro definition with custom message.
#define XASSERTM(expr, msg)
Assertion macro definition with custom message.
Dense data vector class template.
Index size() const
Returns the size of this vector, i.e. its number of entries.
Power-Row-Matrix meta class template.
Index num_rows() const
Returns the total number of rows in this matrix.
const SubMatrixType & get(int i, int j) const
Returns a sub-matrix block.
SubMatrixType & get(int i, int j)
Returns a sub-matrix block.
PowerRowMatrix< SubType_, blocks_-1 > RestClass
rest-class typedef
PowerRowMatrix(const PowerRowMatrix &)=delete
deleted copy-ctor
void convert(const PowerRowMatrix< SubType2_, blocks_ > &source)
Conversion method.
void write_out_submatrices(FileMode mode, String directory, String prefix, String suffix, Index length=blocks_) const
Write out submatrices to file.
void apply(VectorTypeL &r, const VectorTypeR &x) const
Applies this matrix onto a vector.
SubMatrixType & at()
Returns a sub-matrix block.
bool same_layout(const PowerRowMatrix &x) const
Checks if the structural layout of this matrix matches that of another matrix. This excludes comparis...
Index num_cols() const
Returns the total number of columns in this matrix.
PowerRowMatrix(FileMode mode, String filename)
Constructor.
void read_from(FileMode mode, String filename)
Read in matrix from file.
void write_out(FileMode mode, String filename) const
Write out matrix to file.
PowerRowMatrix & operator=(const PowerRowMatrix &)=delete
deleted copy-assign operator
VectorTypeR create_vector_r() const
Returns a new compatible R-Vector.
PowerRowMatrix()
default ctor
SubMatrixType::IndexType IndexType
sub-matrix index type
PowerVector< typename SubMatrixType::VectorTypeR, blocks_ > VectorTypeR
Compatible R-vector type.
void format()
Reset all elements of the container to a given value or zero if missing.
void apply(VectorTypeL &r, const VectorTypeR &x, const VectorTypeL &y, DataType alpha=DataType(1)) const
Applies this matrix onto a vector.
VectorTypeL create_vector_l() const
Returns a new compatible L-Vector.
std::size_t bytes() const
Returns the total amount of bytes allocated.
PowerRowMatrix(PowerRowMatrix &&other)
move ctor
void restore_from_checkpoint_data(std::vector< char > &data)
Extract object from checkpoint.
Index num_nzes() const
Returns the total number of non-zeros in this matrix.
PowerRowMatrix(SubMatrixType &&the_first, RestClass &&the_rest)
base-class constructor; this one is protected for a reason
SubMatrixType::VectorTypeL VectorTypeL
Compatible L-vector type.
static constexpr int num_col_blocks
number of column blocks (horizontal size)
static constexpr int num_row_blocks
number of row blocks (vertical size)
void apply_transposed(VectorTypeR &r, const VectorTypeL &x, const VectorTypeR &y, DataType alpha=DataType(1)) const
Applies this matrix onto a vector.
std::uint64_t get_checkpoint_size(SerialConfig &config)
Calculate size.
SubType_ SubMatrixType
sub-matrix type
std::uint64_t set_checkpoint_data(std::vector< char > &data, SerialConfig &config)
DataType max_rel_diff(const PowerRowMatrix &x) const
Retrieve the maximum relative difference of this matrix and another one y.max_rel_diff(x) returns .
PowerRowMatrix(const SparseLayout< IndexType, layout_id > &layout)
sub-matrix layout ctor
static String name()
Returns a descriptive string for this container.
SubMatrixType::DataType DataType
sub-matrix data type
static constexpr SparseLayoutId layout_id
sub-matrix layout type
void clear()
Free all allocated arrays.
SubMatrixType _first
the first sub-matrix
virtual ~PowerRowMatrix()
virtual destructor
PowerRowMatrix< typename SubType_::template ContainerType< DT2_, IT2_ >, blocks_ > ContainerType
Our 'base' class type.
PowerRowMatrix & operator=(PowerRowMatrix &&other)
move-assign operator
RestClass _rest
the remaining part
PowerRowMatrix(FileMode mode, std::istream &file, String directory="")
Constructor.
PowerRowMatrix clone(LAFEM::CloneMode mode=LAFEM::CloneMode::Weak) const
Creates and returns a deep copy of this matrix.
const SubMatrixType & at() const
Returns a sub-matrix block.
void apply_transposed(VectorTypeR &r, const VectorTypeL &x) const
Applies this matrix onto a vector.
Power-Vector meta class template.
Config class for serialize parameter.
Layout scheme for sparse matrix containers.
String class implementation.
String & trim_me(const String &charset)
Trims this string.
@ other
generic/other permutation strategy
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.