9#include <kernel/lafem/tuple_vector.hpp>
10#include <kernel/lafem/dense_vector.hpp>
11#include <kernel/lafem/container.hpp>
63 typename MatrixB_ = MatrixA_,
64 typename MatrixD_ = MatrixB_>
76 static_assert(std::is_same<typename MatrixA_::DataType, typename MatrixB_::DataType>::value,
77 "A and B have different data-types");
78 static_assert(std::is_same<typename MatrixA_::DataType, typename MatrixD_::DataType>::value,
79 "A and D have different data-types");
82 static_assert(std::is_same<typename MatrixA_::VectorTypeL, typename MatrixB_::VectorTypeL>::value,
83 "A and B have different compatible L-vectors");
84 static_assert(std::is_same<typename MatrixA_::VectorTypeR, typename MatrixD_::VectorTypeR>::value,
85 "A and D have different compatible R-vectors");
88 typedef typename MatrixTypeA::DataType
DataType;
97 template <
typename DT2_ = DataType,
typename IT2_ = IndexType>
103 template <
typename DataType2_,
typename IndexType2_>
146 void init_workspace()
const
153 void reset_workspace()
const
182 auto found = filename.rfind(
"/");
183 if (found != std::string::npos)
185 directory = filename.substr(0, found + 1);
188 std::ifstream file(filename.c_str(), std::ifstream::in);
189 if (! file.is_open())
190 XABORTM(
"Unable to open Matrix file " + filename);
193 std::getline(file, line);
194 if (line.find(
"%%MatrixMarket saddlepointmatrix coordinate real general") == String::npos)
195 XABORTM(
"Input-file is not a complatible file");
200 std::getline(file, line);
202 }
while (line.find(
"%%") == 0 || line ==
"");
204 MatrixA_ tmp_a(mode, directory + line);
207 std::getline(file, line);
209 MatrixB_ tmp_b(mode, directory + line);
212 std::getline(file, line);
214 MatrixD_ tmp_d(mode, directory + line);
225 _matrix_a = std::forward<MatrixTypeA>(other._matrix_a);
226 _matrix_b = std::forward<MatrixTypeB>(other._matrix_b);
227 _matrix_d = std::forward<MatrixTypeD>(other._matrix_d);
244 std::ofstream file(filename.c_str(), std::ofstream::out);
245 if (! file.is_open())
246 XABORTM(
"Unable to open Matrix file " + filename);
249 auto found = filename.rfind(
".");
250 if (found != std::string::npos)
252 suffix = filename.substr(found);
253 filename.erase(found);
255 found = filename.rfind(
"/");
256 if (found != std::string::npos)
258 directory = filename.substr(0, found + 1);
259 filename.erase(0, found + 1);
262 file <<
"%%MatrixMarket saddlepointmatrix coordinate real general\n";
263 file << filename <<
"_a" << suffix <<
"\n";
264 file << filename <<
"_b" << suffix <<
"\n";
265 file << filename <<
"_d" << suffix <<
"\n";
269 _matrix_a.write_out(mode, directory + filename +
"_a" + suffix);
270 _matrix_b.write_out(mode, directory + filename +
"_b" + suffix);
271 _matrix_d.write_out(mode, directory + filename +
"_d" + suffix);
336 template<
int i_,
int j_>
339 static_assert((0 <= i_) && (0 <= j_),
"invalid sub-matrix index");
340 static_assert((i_ <= 1) && (j_ <= 1),
"invalid sub-matrix index");
341 static_assert((i_ < 1) || (j_ < 1),
"sub-matrix block (1,1) does not exist in SaddlePointMatrix");
346 template<
int i_,
int j_>
349 static_assert((0 <= i_) && (0 <= j_),
"invalid sub-matrix index");
350 static_assert((i_ <= 1) && (j_ <= 1),
"invalid sub-matrix index");
351 static_assert((i_ < 1) || (j_ < 1),
"sub-matrix block (1,1) does not exist in SaddlePointMatrix");
406 return String(
"SaddePointMatrix<") + MatrixTypeA::name() +
"," + MatrixTypeB::name() +
"," + MatrixTypeD::name() +
">";
446 void lump_rows(
VectorTypeL& lump,
bool =
true)
const
449 lump.rest().format();
466 block_a().apply(r.template at<0>(), x.template at<0>());
467 block_b().apply(r.template at<0>(), x.template at<1>(), r.template at<0>(),
DataType(1));
468 block_d().apply(r.template at<1>(), x.template at<0>());
473 XASSERTM(r.
size() == this->num_rows(),
"Vector size of r does not match!");
474 XASSERTM(x.
size() == this->num_cols(),
"Vector size of x does not match!");
482 block_a().apply(r_first, x_first);
484 block_d().apply(r_rest, x_first);
501 block_a().apply_transposed(r.template at<0>(), x.template at<0>());
502 block_d().apply_transposed(r.template at<0>(), x.template at<1>(), r.template at<0>(),
DataType(1));
503 block_b().apply_transposed(r.template at<1>(), x.template at<0>());
508 XASSERTM(r.
size() == this->num_cols(),
"Vector size of r does not match!");
509 XASSERTM(x.
size() == this->num_rows(),
"Vector size of x does not match!");
517 block_a().apply_transposed(r_first, x_first);
519 block_b().applytransposed(r_rest, x_first);
541 block_a().apply(r.template at<0>(), x.template at<0>(), y.template at<0>(), alpha);
542 block_b().apply(r.template at<0>(), x.template at<1>(), r.template at<0>(), alpha);
543 block_d().apply(r.template at<1>(), x.template at<0>(), y.template at<1>(), alpha);
549 XASSERTM(r.
size() == this->num_rows(),
"Vector size of r does not match!");
550 XASSERTM(x.
size() == this->num_cols(),
"Vector size of x does not match!");
551 XASSERTM(y.
size() == this->num_rows(),
"Vector size of y does not match!");
562 block_a().apply(r_first, x_first, y_first, alpha);
563 block_b().apply(r_first, x_rest, r_first, alpha);
564 block_d().apply(r_rest, x_first, y_rest, alpha);
586 block_a().apply_transposed(r.template at<0>(), x.template at<0>(), y.template at<0>(), alpha);
587 block_d().apply_transposed(r.template at<0>(), x.template at<1>(), r.template at<0>(), alpha);
588 block_b().apply_transposed(r.template at<1>(), x.template at<0>(), y.template at<1>(), alpha);
594 XASSERTM(r.
size() == this->num_cols(),
"Vector size of r does not match!");
595 XASSERTM(x.
size() == this->num_rows(),
"Vector size of x does not match!");
596 XASSERTM(y.
size() == this->num_cols(),
"Vector size of y does not match!");
607 block_a().apply_transposed(r_first, x_first, y_first, alpha);
608 block_d().apply_transposed(r_first, x_rest, r_first, alpha);
609 block_b().apply_transposed(r_rest, x_first, y_rest, alpha);
658 return block_d().row_degree(row - rows_a);
661 template<
typename IT2_>
662 Index get_row_col_indices(
const Index row, IT2_*
const pcol_idx,
const IT2_ col_offset)
const
668 Index degree_a =
block_a().get_row_col_indices(row, pcol_idx, col_offset);
669 return degree_a +
block_b().get_row_col_indices(row, pcol_idx + degree_a, col_offset + IT2_(cols_a));
672 return block_d().get_row_col_indices(row - rows_a, pcol_idx, col_offset);
675 template<
typename DT2_>
676 Index get_row_values(
const Index row, DT2_ *
const pvals)
const
682 return degree_a +
block_b().get_row_values(row, pvals + degree_a);
685 return block_d().get_row_values(row - rows_a, pvals);
688 template<
typename DT2_>
689 Index set_row_values(
const Index row,
const DT2_ *
const pvals)
695 return degree_a +
block_b().set_row_values(row, pvals + degree_a);
698 return block_d().set_row_values(row - rows_a, pvals);
706 return (3 *
sizeof(std::uint64_t)) + this->
block_a().get_checkpoint_size(config) + this->
block_b().get_checkpoint_size(config) + this->
block_d().get_checkpoint_size(config);
712 std::uint64_t isize = *(std::uint64_t*) data.data();
713 std::vector<char>::iterator start = std::begin(data) +
sizeof(std::uint64_t);
714 std::vector<char>::iterator end = std::begin(data) +
sizeof(std::uint64_t) + (
int) isize;
715 std::vector<char> buffer_a(start, end);
716 this->
block_a().restore_from_checkpoint_data(buffer_a);
718 data.erase(std::begin(data), end);
719 isize = *(std::uint64_t*) data.data();
720 start = std::begin(data) +
sizeof(std::uint64_t);
721 end = std::begin(data) +
sizeof(std::uint64_t) + (
int) isize;
722 std::vector<char> buffer_b(start, end);
723 this->
block_b().restore_from_checkpoint_data(buffer_b);
725 data.erase(std::begin(data), end);
726 this->
block_d().restore_from_checkpoint_data(data);
732 std::size_t old_size = data.size();
733 data.insert(std::end(data),
sizeof(std::uint64_t),0);
734 std::uint64_t ireal_size = this->
block_a().set_checkpoint_data(data, config);
735 std::uint64_t ret_size = ireal_size;
736 char* csize =
reinterpret_cast<char*
>(&ireal_size);
737 for(std::size_t i(0) ; i <
sizeof(std::uint64_t) ; ++i)
739 data[old_size + i] = csize[i];
742 old_size = data.size();
743 data.insert(std::end(data),
sizeof(std::uint64_t), 0);
744 ireal_size = this->
block_b().set_checkpoint_data(data, config);
745 ret_size += ireal_size;
746 csize =
reinterpret_cast<char*
>(&ireal_size);
747 for(std::size_t i(0) ; i <
sizeof(std::uint64_t) ; ++i)
749 data[old_size + i] = csize[i];
752 return 2*
sizeof(std::uint64_t) + ret_size + this->
block_d().set_checkpoint_data(data, config);
762 template <
typename MatrixA2_,
typename MatrixB2_,
typename MatrixD2_>
770 template <
typename MatrixA2_,
typename MatrixB2_,
typename MatrixD2_>
778 template <
typename MatrixA2_,
typename MatrixB2_,
typename MatrixD2_>
779 void copy_to(SaddlePointMatrix<MatrixA2_, MatrixB2_, MatrixD2_> & other)
const
781 this->
block_a().copy_to(other.block_a());
782 this->
block_b().copy_to(other.block_b());
783 this->
block_d().copy_to(other.block_d());
788 template<
typename MatrixA_,
typename MatrixB_,
typename MatrixD_>
789 struct SaddlePointMatrixElement<MatrixA_, MatrixB_, MatrixD_, 0, 0>
791 typedef MatrixA_
Type;
792 static Type& get(SaddlePointMatrix<MatrixA_, MatrixB_, MatrixD_>& matrix)
794 return matrix.block_a();
796 static const Type& get(
const SaddlePointMatrix<MatrixA_, MatrixB_, MatrixD_>& matrix)
798 return matrix.block_a();
802 template<
typename MatrixA_,
typename MatrixB_,
typename MatrixD_>
803 struct SaddlePointMatrixElement<MatrixA_, MatrixB_, MatrixD_, 0, 1>
805 typedef MatrixB_
Type;
806 static Type& get(SaddlePointMatrix<MatrixA_, MatrixB_, MatrixD_>& matrix)
808 return matrix.block_b();
810 static const Type& get(
const SaddlePointMatrix<MatrixA_, MatrixB_, MatrixD_>& matrix)
812 return matrix.block_b();
816 template<
typename MatrixA_,
typename MatrixB_,
typename MatrixD_>
817 struct SaddlePointMatrixElement<MatrixA_, MatrixB_, MatrixD_, 1, 0>
819 typedef MatrixD_
Type;
820 static Type& get(SaddlePointMatrix<MatrixA_, MatrixB_, MatrixD_>& matrix)
822 return matrix.block_d();
824 static const Type& get(
const SaddlePointMatrix<MatrixA_, MatrixB_, MatrixD_>& matrix)
826 return matrix.block_d();
#define XABORTM(msg)
Abortion macro definition with custom message.
#define XASSERT(expr)
Assertion macro definition.
#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.
Saddle-Point matrix meta class template.
Index num_nzes_raw() const
Returns the total number of non-zeros in this matrix.
SaddlePointMatrix clone(LAFEM::CloneMode mode=LAFEM::CloneMode::Weak) const
Creates and returns a deep copy of this matrix.
void apply(VectorTypeL &r, const VectorTypeR &x, const VectorTypeL &y, DataType alpha=DataType(1)) const
Applies this matrix onto a vector.
MatrixD_ MatrixTypeD
type of sub-matrix D
const SaddlePointMatrixElement< MatrixA_, MatrixB_, MatrixD_, i_, j_ >::Type & at() const
Returns a sub-matrix block.
MatrixTypeB _matrix_b
sub-matrix B
VectorTypeL create_vector_l() const
Returns a new compatible L-Vector.
MatrixTypeA _matrix_a
sub-matrix A
MatrixB_ MatrixTypeB
type of sub-matrix B
static constexpr int num_row_blocks
number of row blocks (vertical size)
bool same_layout(const SaddlePointMatrix &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.
DataType max_rel_diff(const SaddlePointMatrix &x) const
Retrieve the maximum relative difference of this matrix and another one y.max_rel_diff(x) returns .
std::uint64_t set_checkpoint_data(std::vector< char > &data, SerialConfig &config)
SaddlePointMatrix()=default
default CTOR
void apply_transposed(VectorTypeR &r, const VectorTypeL &x) const
Applies this matrix onto a vector.
MatrixTypeA::DataType DataType
data type
void write_out(FileMode mode, String filename) const
Write out matrix to file.
MatrixTypeA & block_a()
Returns the sub-matrix block A.
SaddlePointMatrix(SaddlePointMatrix &&other)
move ctor
VectorTypeR create_vector_r() const
Returns a new compatible R-Vector.
void apply(VectorTypeL &r, const VectorTypeR &x) const
Applies this matrix onto a vector.
const MatrixTypeB & block_b() const
Returns the sub-matrix block B.
MatrixTypeA::IndexType IndexType
index type
Index num_cols_raw() const
Returns the total number of columns in this matrix.
MatrixA_ MatrixTypeA
type of sub-matrix A
static constexpr int num_col_blocks
number of column blocks (horizontal size)
std::uint64_t get_checkpoint_size(SerialConfig &config)
Calculate size.
TupleVector< typename MatrixTypeA::VectorTypeL, typename MatrixTypeD::VectorTypeL > VectorTypeL
Compatible L-vector type.
const MatrixTypeA & block_a() const
Returns the sub-matrix block A.
Index num_nzes() const
Returns the total number of non-zeros in this matrix.
void clear()
Free all allocated arrays.
SaddlePointMatrix & operator=(SaddlePointMatrix &&other)
move-assign operator
static String name()
Returns a descriptive string for this container.
void convert(const SaddlePointMatrix< MatrixA2_, MatrixB2_, MatrixD2_ > &other)
Conversion method.
MatrixTypeD _matrix_d
sub-matrix C
void extract_diag(VectorTypeL &diag) const
extract main diagonal vector from matrix
std::size_t bytes() const
Returns the total amount of bytes allocated.
void restore_from_checkpoint_data(std::vector< char > &data)
Extract object from checkpoint.
Index num_rows_raw() const
Returns the total number of rows in this matrix.
const MatrixTypeD & block_d() const
Returns the sub-matrix block D.
TupleVector< typename MatrixTypeA::VectorTypeR, typename MatrixTypeB::VectorTypeR > VectorTypeR
Compatible R-vector type.
void format()
Reset all elements of the container to a given value or zero if missing.
MatrixTypeB & block_b()
Returns the sub-matrix block B.
SaddlePointMatrix(FileMode mode, const String &filename)
Constructor.
void read_from(FileMode mode, const String &filename)
Read in matrix from file.
SaddlePointMatrixElement< MatrixA_, MatrixB_, MatrixD_, i_, j_ >::Type & at()
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.
virtual ~SaddlePointMatrix()
virtual destructor
MatrixTypeD & block_d()
Returns the sub-matrix block D.
Index num_rows() const
Returns the total number of rows in this matrix.
SaddlePointMatrix(MatrixTypeA &&matrix_a, MatrixTypeB &&matrix_b, MatrixTypeD &&matrix_d)
Sub-Matrix move constructor.
Config class for serialize parameter.
Variadic TupleVector class template.
void format()
Reset all elements of the container to a given value or zero if missing.
String class implementation.
String & trim_me(const String &charset)
Trims this string.
T_ max(T_ a, T_ b)
Returns the maximum of two values.
@ value
specifies whether the space should supply basis function values
std::uint64_t Index
Index data type.
Saddle-Point matrix element helper class template.