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>
39 static_assert(blocks_ > 1,
"invalid block size");
42 template<
typename,
int>
52 typedef typename SubMatrixType::DataType
DataType;
54 typedef typename SubMatrixType::IndexType
IndexType;
62 template <
typename DT2_ = DataType,
typename IT2_ = IndexType>
66 template <
typename DataType2_,
typename IndexType2_>
82 _first(std::move(the_first)),
83 _rest(std::move(the_rest))
136 std::getline(file, line);
138 }
while (line.find(
"%%") == 0 || line ==
"");
141 _first = std::move(tmp_first);
143 RestClass tmp_rest(mode, file, directory);
144 _rest = std::move(tmp_rest);
156 auto found = filename.rfind(
"/");
157 if (found != std::string::npos)
159 directory = filename.substr(0, found + 1);
162 std::ifstream file(filename.c_str(), std::ifstream::in);
163 if (! file.is_open())
164 XABORTM(
"Unable to open Matrix file " + filename);
167 std::getline(file, line);
168 if (line.find(
"%%MatrixMarket powerdiagmatrix coordinate real general") == String::npos)
169 XABORTM(
"Input-file is not a complatible file");
173 _first = std::move(other._first);
174 _rest = std::move(other._rest);
184 _first = std::move(other._first);
185 _rest = std::move(other._rest);
208 std::ofstream file(filename.c_str(), std::ofstream::out);
209 if (! file.is_open())
210 XABORTM(
"Unable to open Matrix file " + filename);
213 auto found = filename.rfind(
".");
214 if (found != std::string::npos)
216 suffix = filename.substr(found);
217 filename.erase(found);
219 found = filename.rfind(
"/");
220 if (found != std::string::npos)
222 directory = filename.substr(0, found + 1);
223 filename.erase(0, found + 1);
226 file <<
"%%MatrixMarket powerdiagmatrix coordinate real general\n";
227 for (
Index i(1); i <= blocks_; ++i)
229 file << filename <<
"_pd" << i << suffix <<
"\n";
247 _first.write_out(mode, directory + prefix +
"_pd" +
stringify(length + 1 - blocks_) + suffix);
269 (*this)=other.clone(clone_mode);
298 template<
int i_,
int j_>
301 static_assert(i_ == j_,
"invalid sub-matrix index");
302 static_assert((0 <= i_) && (i_ < blocks_),
"invalid sub-matrix index");
307 template<
int i_,
int j_>
310 static_assert(i_ == j_,
"invalid sub-matrix index");
311 static_assert((0 <= i_) && (i_ < blocks_),
"invalid sub-matrix index");
326 XASSERTM((i == j) && (0 <= i) && (i < blocks_),
"invalid sub-matrix index");
333 XASSERTM((i == j) && (0 <= i) && (i < blocks_),
"invalid sub-matrix index");
346 std::uint64_t isize = *(std::uint64_t*) data.data();
347 std::vector<char>::iterator start = std::begin(data) +
sizeof(std::uint64_t);
348 std::vector<char>::iterator last_of_first = std::begin(data) +
sizeof(std::uint64_t) + (
int) isize;
349 std::vector<char> buffer_first(start, last_of_first);
350 _first.restore_from_checkpoint_data(buffer_first);
352 data.erase(std::begin(data), last_of_first);
359 std::size_t old_size = data.size();
360 data.insert(std::end(data),
sizeof(std::uint64_t), 0);
361 std::uint64_t ireal_size =
_first.set_checkpoint_data(data, config);
362 char* csize =
reinterpret_cast<char*
>(&ireal_size);
363 for(std::size_t i(0) ; i <
sizeof(std::uint64_t) ; ++i)
365 data[old_size + i] = csize[i];
392 int row_blocks()
const
397 int col_blocks()
const
406 return first().num_rows() + rest().num_rows();
412 return first().num_cols() + rest().num_cols();
418 return first().num_nzes() + rest().num_nzes();
424 return first().num_rows_raw() + rest().num_rows_raw();
430 return first().num_cols_raw() + rest().num_cols_raw();
436 return first().num_nzes_raw() + rest().num_nzes_raw();
442 return String(
"PowerDiagMatrix<") + SubMatrixType::name() +
"," +
stringify(blocks_) +
">";
459 first().format(
value);
460 rest().format(
value);
475 first().extract_diag(diag.first());
476 rest().extract_diag(diag.rest());
493 first().apply(r.first(), x.first());
494 rest().apply(r.rest(), x.rest());
499 XASSERTM(r.
size() == this->num_rows(),
"Vector size of r does not match!");
500 XASSERTM(x.
size() == this->num_cols(),
"Vector size of x does not match!");
508 first().apply(r_first, x_first);
509 rest().apply(r_rest, x_rest);
526 first().apply_transposed(r.first(), x.first());
527 rest().apply_transposed(r.rest(), x.rest());
532 XASSERTM(r.
size() == this->num_cols(),
"Vector size of r does not match!");
533 XASSERTM(x.
size() == this->num_rows(),
"Vector size of x does not match!");
541 first().apply_transposed(r_first, x_first);
542 rest().apply_transposed(r_rest, x_rest);
563 first().apply(r.first(), x.first(), y.first(), alpha);
564 rest().apply(r.rest(), x.rest(), y.rest(), alpha);
570 XASSERTM(r.
size() == this->num_rows(),
"Vector size of r does not match!");
571 XASSERTM(x.
size() == this->num_cols(),
"Vector size of x does not match!");
572 XASSERTM(y.
size() == this->num_rows(),
"Vector size of y does not match!");
583 first().apply(r_first, x_first, y_first, alpha);
584 rest().apply(r_rest, x_rest, y_rest, alpha);
605 first().apply_transposed(r.first(), x.first(), y.first(), alpha);
606 rest().apply_transposed(r.rest(), x.rest(), y.rest(), alpha);
612 XASSERTM(r.
size() == this->num_cols(),
"Vector size of r does not match!");
613 XASSERTM(x.
size() == this->num_rows(),
"Vector size of x does not match!");
614 XASSERTM(y.
size() == this->num_cols(),
"Vector size of y does not match!");
625 first().apply_transposed(r_first, x_first, y_first, alpha);
626 rest().apply_transposed(r_rest, x_rest, y_rest, alpha);
651 return (this->
name() == x.
name()) && (this->first().same_layout(x.first())) && (this->rest().same_layout(x.rest()));
668 first().scale_rows(a.first(), w.first());
669 rest().scale_rows(a.rest(), w.rest());
672 void scale_cols(
const PowerDiagMatrix& a,
const VectorTypeR& w)
674 first().scale_cols(a.first(), w.first());
675 rest().scale_cols(a.rest(), w.rest());
681 const Index first_rows = first().num_rows_raw();
683 return first().row_degree(row);
685 return rest().row_degree(row - first_rows);
688 template<
typename IT2_>
689 Index get_row_col_indices(
const Index row, IT2_*
const pcol_idx,
const IT2_ col_offset)
const
691 const Index first_rows = first().num_rows_raw();
692 const Index first_cols = first().num_cols_raw();
694 return first().get_row_col_indices(row, pcol_idx, col_offset);
696 return rest().get_row_col_indices(row - first_rows, pcol_idx, col_offset + IT2_(first_cols));
699 template<
typename DT2_>
700 Index get_row_values(
const Index row, DT2_ *
const pvals)
const
702 const Index first_rows = first().num_rows_raw();
704 return first().get_row_values(row, pvals);
706 return rest().get_row_values(row - first_rows, pvals);
709 template<
typename DT2_>
710 Index set_row_values(
const Index row,
const DT2_ *
const pvals)
712 const Index first_rows = first().num_rows_raw();
714 return first().set_row_values(row, pvals);
716 return rest().set_row_values(row - first_rows, pvals);
728 template <
typename SubType2_>
731 this->first().convert(source.first());
732 this->rest().convert(source.rest());
735 template <
typename SubType2_>
738 this->first().copy(source.first());
739 this->rest().copy(source.rest());
742 template <
typename SubType2_>
743 void copy_to(PowerDiagMatrix<SubType2_, blocks_> & target)
const
745 this->first().copy_to(target.first());
746 this->rest().copy_to(target.rest());
751 template<
typename SubType_>
752 class PowerDiagMatrix<SubType_, 1>
754 template<
typename,
int>
755 friend class PowerDiagMatrix;
759 typedef typename SubMatrixType::DataType
DataType;
760 typedef typename SubMatrixType::IndexType
IndexType;
764 typedef PowerVector<typename SubMatrixType::VectorTypeL, 1>
VectorTypeL;
766 typedef PowerVector<typename SubMatrixType::VectorTypeR, 1>
VectorTypeR;
768 template <
typename DT2_ = DataType,
typename IT2_ = IndexType>
769 using ContainerType = PowerDiagMatrix<typename SubType_::template ContainerType<DT2_, IT2_>, 1>;
772 template <
typename DataType2_,
typename IndexType2_>
783 _first(std::move(the_first))
794 explicit PowerDiagMatrix(
const SparseLayout<IndexType, layout_id>& layout) :
800 PowerDiagMatrix(PowerDiagMatrix&& other) :
806 PowerDiagMatrix&
operator=(PowerDiagMatrix&& other)
816 explicit PowerDiagMatrix(
FileMode mode,
const String& filename)
822 explicit PowerDiagMatrix(
FileMode mode, std::istream& file,
const String& directory =
"")
828 std::getline(file, line);
830 }
while (line.find(
"%%") == 0 || line ==
"");
833 _first = std::move(tmp_first);
839 auto found = filename.rfind(
"/");
840 if (found != std::string::npos)
842 directory = filename.substr(0, found + 1);
845 std::ifstream file(filename.c_str(), std::ifstream::in);
846 if (! file.is_open())
847 XABORTM(
"Unable to open Matrix file " + filename);
849 PowerDiagMatrix
other(mode, file, directory);
857 PowerDiagMatrix(
const PowerDiagMatrix&) =
delete;
859 PowerDiagMatrix&
operator=(
const PowerDiagMatrix&) =
delete;
868 std::ofstream file(filename.c_str(), std::ofstream::out);
869 if (! file.is_open())
870 XABORTM(
"Unable to open Matrix file " + filename);
872 String suffix, directory;
873 auto found = filename.rfind(
".");
874 if (found != std::string::npos)
876 suffix = filename.substr(found);
877 filename.erase(found);
879 found = filename.rfind(
"/");
880 if (found != std::string::npos)
882 directory = filename.substr(0, found + 1);
883 filename.erase(0, found + 1);
886 file <<
"%%MatrixMarket powerdiagmatrix coordinate real general\n";
887 file << filename <<
"_pd" << 1 << suffix <<
"\n";
896 _first.write_out(mode, directory + prefix +
"_pd" +
stringify(length) + suffix);
901 return PowerDiagMatrix(
_first.clone(mode));
906 (*this)=
other.clone(clone_mode);
910 std::size_t
bytes()
const
917 return PowerDiagMatrix(
_first.transpose());
920 template<
int i,
int j>
923 static_assert(i == 0,
"invalid sub-matrix index");
924 static_assert(j == 0,
"invalid sub-matrix index");
928 template<
int i,
int j>
931 static_assert(i == 0,
"invalid sub-matrix index");
932 static_assert(j == 0,
"invalid sub-matrix index");
938 XASSERTM((i == 0) && (j == 0),
"invalid sub-matrix index");
944 XASSERTM((i == 0) && (j == 0),
"invalid sub-matrix index");
951 return _first.get_checkpoint_size(config);
957 _first.restore_from_checkpoint_data(data);
963 return _first.set_checkpoint_data(data, config);
976 int row_blocks()
const
981 int col_blocks()
const
988 return first().num_rows();
993 return first().num_cols();
998 return first().num_nzes();
1003 return first().num_rows_raw();
1008 return first().num_cols_raw();
1013 return first().num_nzes_raw();
1016 static String
name()
1018 return String(
"PowerDiagMatrix<") + SubMatrixType::name() +
"," +
stringify(1) +
">";
1028 first().format(
value);
1038 first().extract_diag(diag.first());
1043 first().apply(r.first(), x.first());
1046 void apply(DenseVector<DataType, IndexType>& r,
const DenseVector<DataType, IndexType>& x)
const
1048 first().apply(r, x);
1053 first().apply(r.first(), x.first(), y.first(), alpha);
1056 void apply(DenseVector<DataType, IndexType>& r,
const DenseVector<DataType, IndexType>& x,
1057 const DenseVector<DataType, IndexType>& y,
DataType alpha =
DataType(1))
const
1059 first().apply(r, x, y, alpha);
1064 first().apply_transposed(r.first(), x.first());
1067 void apply_transposed(DenseVector<DataType, IndexType>& r,
const DenseVector<DataType, IndexType>& x)
const
1069 first().apply_transposed(r, x);
1074 first().apply_transposed(r.first(), x.first(), y.first(), alpha);
1077 void apply_transposed(DenseVector<DataType, IndexType>& r,
const DenseVector<DataType, IndexType>& x,
1078 const DenseVector<DataType, IndexType>& y,
DataType alpha =
DataType(1))
const
1080 first().apply_transposed(r, x, y, alpha);
1091 return first().max_rel_diff(x.first());
1106 void scale_rows(
const PowerDiagMatrix& a,
const VectorTypeL& w)
1108 first().scale_rows(a.first(), w.first());
1111 void scale_cols(
const PowerDiagMatrix& a,
const VectorTypeR& w)
1113 first().scale_cols(a.first(), w.first());
1118 return first().row_degree(row);
1121 template<
typename IT2_>
1122 Index get_row_col_indices(
const Index row, IT2_*
const pcol_idx,
const IT2_ col_offset)
const
1124 return first().get_row_col_indices(row, pcol_idx, col_offset);
1127 template<
typename DT2_>
1128 Index get_row_values(
const Index row, DT2_ *
const pvals)
const
1130 return first().get_row_values(row, pvals);
1133 template<
typename DT2_>
1134 Index set_row_values(
const Index row,
const DT2_ *
const pvals)
1136 return first().set_row_values(row, pvals);
1147 template <
typename SubType2_>
1148 void convert(
const PowerDiagMatrix<SubType2_, 1> & source)
1150 this->first().convert(source.first());
1153 template <
typename SubType2_>
1154 void copy(
const PowerDiagMatrix<SubType2_, 1> & source)
1156 this->first().copy(source.first());
1159 template <
typename SubType2_>
1160 void copy_to(PowerDiagMatrix<SubType2_, 1> & target)
const
1162 this->first().copy_to(target.first());
1175 return (this->
name() == x.name()) && (this->first().same_layout(x.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-Diag-Matrix meta class template.
PowerDiagMatrix(SubMatrixType &&the_first, RestClass &&the_rest)
base-class constructor; this one is protected for a reason
PowerDiagMatrix(FileMode mode, std::istream &file, String directory="")
Constructor.
SubMatrixType & at()
Returns a sub-matrix block.
SubMatrixType & get(int i, int j)
Returns a sub-matrix block.
PowerDiagMatrix(PowerDiagMatrix &&other)
move ctor
ContainerType< DataType2_, IndexType2_ > ContainerTypeByDI
this typedef lets you create a matrix container with different Data and Index types
PowerVector< typename SubMatrixType::VectorTypeL, blocks_ > VectorTypeL
Compatible L-vector type.
PowerVector< typename SubMatrixType::VectorTypeR, blocks_ > VectorTypeR
Compatible R-vector type.
PowerDiagMatrix clone(LAFEM::CloneMode mode=LAFEM::CloneMode::Weak) const
Creates and returns a deep copy of this matrix.
static constexpr SparseLayoutId layout_id
sub-matrix layout type
std::uint64_t set_checkpoint_data(std::vector< char > &data, SerialConfig &config)
Index num_nzes() const
Returns the total number of non-zeros in this matrix.
PowerDiagMatrix & operator=(PowerDiagMatrix &&other)
move-assign operator
const SubMatrixType & at() const
Returns a sub-matrix block.
PowerDiagMatrix()
default ctor
void restore_from_checkpoint_data(std::vector< char > &data)
Extract object from checkpoint.
SubMatrixType::DataType DataType
sub-matrix data type
PowerDiagMatrix(const PowerDiagMatrix &)=delete
deleted copy-ctor
PowerDiagMatrix(FileMode mode, String filename)
Constructor.
bool same_layout(const PowerDiagMatrix &x) const
Checks if the structural layout of this matrix matches that of another matrix. This excludes comparis...
void convert(const PowerDiagMatrix< SubType2_, blocks_ > &source)
Conversion method.
Index num_cols_raw() const
Returns the total number of columns in this matrix.
PowerDiagMatrix transpose() const
Creates and returns the transpose of this matrix.
void write_out(FileMode mode, String filename) const
Write out matrix to file.
void clone(const PowerDiagMatrix &other, CloneMode clone_mode=CloneMode::Weak)
Clone operation.
Index num_rows_raw() const
Returns the total number of rows in this matrix.
static String name()
Returns a descriptive string for this container.
void read_from(FileMode mode, String filename)
Read in matrix from file.
void apply(VectorTypeL &r, const VectorTypeR &x) const
Applies this matrix onto a vector.
void format()
Reset all elements of the container to a given value or zero if missing.
SubMatrixType::IndexType IndexType
sub-matrix index type
SubMatrixType _first
the first sub-matrix
std::size_t bytes() const
Returns the total amount of bytes allocated.
std::uint64_t get_checkpoint_size(SerialConfig &config)
Calculate size.
PowerDiagMatrix< SubType_, blocks_-1 > RestClass
rest-class typedef
VectorTypeL create_vector_l() const
Returns a new compatible L-Vector.
PowerDiagMatrix< typename SubType_::template ContainerType< DT2_, IT2_ >, blocks_ > ContainerType
Our 'base' class type.
void write_out_submatrices(FileMode mode, String directory, String prefix, String suffix, Index length=blocks_) const
Write out submatrices to file.
PowerDiagMatrix(const SparseLayout< IndexType, layout_id > &layout)
sub-matrix layout ctor
static constexpr int num_col_blocks
number of column blocks (horizontal size)
void apply(VectorTypeL &r, const VectorTypeR &x, const VectorTypeL &y, DataType alpha=DataType(1)) const
Applies this matrix onto a vector.
SubType_ SubMatrixType
sub-matrix type
const SubMatrixType & get(int i, int j) const
Returns a sub-matrix block.
void apply_transposed(VectorTypeR &r, const VectorTypeL &x, const VectorTypeR &y, DataType alpha=DataType(1)) const
Applies this matrix onto a vector.
DataType max_rel_diff(const PowerDiagMatrix &x) const
Retrieve the maximum relative difference of this matrix and another one y.max_rel_diff(x) returns .
static constexpr int num_row_blocks
number of row blocks (vertical size)
Index num_nzes_raw() const
Returns the total number of non-zeros in this matrix.
void clear()
Free all allocated arrays.
VectorTypeR create_vector_r() const
Returns a new compatible R-Vector.
virtual ~PowerDiagMatrix()
virtual destructor
Index num_cols() const
Returns the total number of columns in this matrix.
RestClass _rest
the remaining part
void apply_transposed(VectorTypeR &r, const VectorTypeL &x) const
Applies this matrix onto a vector.
void extract_diag(VectorTypeL &diag) const
extract main diagonal vector from matrix
PowerDiagMatrix & operator=(const PowerDiagMatrix &)=delete
deleted copy-assign operator
Index num_rows() const
Returns the total number of rows in this matrix.
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.