9#include <kernel/lafem/tuple_vector.hpp>
10#include <kernel/lafem/sparse_layout.hpp>
11#include <kernel/lafem/meta_element.hpp>
37 template<
typename,
typename...>
55 template <
typename DT2_ = DataType,
typename IT2_ = IndexType>
61 template <
typename DT2_,
typename IT2_>
79 _first(std::forward<First_>(the_first)),
98 _first(std::forward<First_>(the_first)),
99 _rest(std::forward<Rest_>(the_rest)...)
139 std::getline(file, line);
141 }
while (line.find(
"%%") == 0 || line ==
"");
143 _first = First_(mode, directory + line);
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 tuplediagmatrix 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::forward<First_>(other._first);
185 _rest = std::forward<RestClass>(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 tuplediagmatrix coordinate real general\n";
229 file << filename <<
"_td" << i << suffix <<
"\n";
285 template<
int i_,
int j_>
288 static_assert(i_ == j_,
"invalid sub-matrix index");
293 template<
int i_,
int j_>
296 static_assert(i_ == j_,
"invalid sub-matrix index");
306 const First_& first()
const
321 int row_blocks()
const
326 int col_blocks()
const
335 return first().rows() + rest().rows();
341 return first().columns() + rest().columns();
347 return first().used_elements() + rest().used_elements();
369 first().format(
value);
370 rest().format(
value);
385 first().extract_diag(diag.first());
386 rest().extract_diag(diag.rest());
403 first().apply(r.first(), x.first());
404 rest().apply(r.rest(), x.rest());
409 XASSERTM(r.
size() == this->rows(),
"Vector size of r does not match!");
410 XASSERTM(x.
size() == this->columns(),
"Vector size of x does not match!");
418 first().apply(r_first, x_first);
419 rest().apply(r_rest, x_rest);
436 first().apply_transposed(r.first(), x.first());
437 rest().apply_transposed(r.rest(), x.rest());
442 XASSERTM(r.
size() == this->columns(),
"Vector size of r does not match!");
443 XASSERTM(x.
size() == this->rows(),
"Vector size of x does not match!");
451 first().apply_transposed(r_first, x_first);
452 rest().apply_transposed(r_rest, x_rest);
473 first().apply(r.first(), x.first(), y.first(), alpha);
474 rest().apply(r.rest(), x.rest(), y.rest(), alpha);
480 XASSERTM(r.
size() == this->rows(),
"Vector size of r does not match!");
481 XASSERTM(x.
size() == this->columns(),
"Vector size of x does not match!");
482 XASSERTM(y.
size() == this->rows(),
"Vector size of y does not match!");
493 first().apply(r_first, x_first, y_first, alpha);
494 rest().apply(r_rest, x_rest, y_rest, alpha);
515 first().apply_transposed(r.first(), x.first(), y.first(), alpha);
516 rest().apply_transposed(r.rest(), x.rest(), y.rest(), alpha);
522 XASSERTM(r.
size() == this->columns(),
"Vector size of r does not match!");
523 XASSERTM(x.
size() == this->rows(),
"Vector size of x does not match!");
524 XASSERTM(y.
size() == this->columns(),
"Vector size of y does not match!");
535 first().apply_transposed(r_first, x_first, y_first, alpha);
536 rest().apply_transposed(r_rest, x_rest, y_rest, alpha);
553 first().scale_rows(a.first(), w.first());
554 rest().scale_rows(a.rest(), w.rest());
557 void scale_cols(
const TupleDiagMatrix& a,
const VectorTypeR& w)
559 first().scale_cols(a.first(), w.first());
560 rest().scale_cols(a.rest(), w.rest());
570 return this->first().get_length_of_line(row);
574 return this->rest().get_length_of_line(row - brows);
581 const Index col_start,
const Index stride = 1)
const
588 this->first().set_line(row, pval_set, pcol_set, col_start, stride);
592 this->rest().set_line(row - brows, pval_set, pcol_set, col_start + bcolumns, stride);
596 void set_line_reverse(
const Index row,
DataType *
const pval_set,
const Index stride = 1)
602 this->first().set_line_reverse(row, pval_set, stride);
606 this->
rest().set_line_reverse(row - brows, pval_set, stride);
620 std::uint64_t isize = *(std::uint64_t*) data.data();
621 std::vector<char>::iterator start = std::begin(data) +
sizeof(std::uint64_t);
622 std::vector<char>::iterator last_of_first = std::begin(data) +
sizeof(std::uint64_t) + (
int) isize;
623 std::vector<char> buffer_first(start, last_of_first);
624 _first.restore_from_checkpoint_data(buffer_first);
626 data.erase(std::begin(data), last_of_first);
633 std::size_t old_size = data.size();
634 data.insert(std::end(data),
sizeof(std::uint64_t), 0);
635 std::uint64_t ireal_size =
_first.set_checkpoint_data(data, config);
636 char* csize =
reinterpret_cast<char*
>(&ireal_size);
637 for(std::size_t i(0) ; i <
sizeof(std::uint64_t) ; ++i)
639 data[old_size +i] = csize[i];
652 template <
typename First2_,
typename... Rest2_>
655 this->first().convert(other.first());
656 this->rest().convert(other.rest());
659 template <
typename First2_,
typename... Rest2_>
662 this->first().convert_reverse(other.first());
663 this->rest().convert_reverse(other.rest());
676 return (this->
name() == x.
name()) && (this->first().same_layout(x.first())) && (this->rest().same_layout(x.rest()));
681 template<
typename First_>
682 class TupleDiagMatrix<First_>
684 template<
typename,
typename...>
685 friend class TupleDiagMatrix;
688 typedef typename First_::DataType
DataType;
689 typedef typename First_::IndexType
IndexType;
691 typedef TupleVector<typename First_::VectorTypeL>
VectorTypeL;
693 typedef TupleVector<typename First_::VectorTypeR>
VectorTypeR;
695 template <
typename DT2_ = DataType,
typename IT2_ = IndexType>
696 using ContainerType = TupleDiagMatrix<typename First_::template ContainerType<DT2_, IT2_> >;
706 return First_::name();
715 explicit TupleDiagMatrix(First_&& the_first) :
716 _first(std::forward<First_>(the_first))
721 TupleDiagMatrix(TupleDiagMatrix&& other) :
727 explicit TupleDiagMatrix(
FileMode mode,
const String& filename)
733 explicit TupleDiagMatrix(
FileMode mode, std::istream& file,
const String& directory =
"")
739 std::getline(file, line);
741 }
while (line.find(
"%%") == 0 || line ==
"");
743 _first = First_(mode, directory + line);
749 auto found = filename.rfind(
"/");
750 if (found != std::string::npos)
752 directory = filename.substr(0, found + 1);
755 std::ifstream file(filename.c_str(), std::ifstream::in);
756 if (! file.is_open())
757 XABORTM(
"Unable to open Matrix file " + filename);
759 TupleDiagMatrix
other(mode, file, directory);
767 TupleDiagMatrix&
operator=(TupleDiagMatrix&& other)
777 TupleDiagMatrix(
const TupleDiagMatrix&) =
delete;
779 TupleDiagMatrix&
operator=(
const TupleDiagMatrix&) =
delete;
788 std::ofstream file(filename.c_str(), std::ofstream::out);
789 if (! file.is_open())
790 XABORTM(
"Unable to open Matrix file " + filename);
792 String suffix, directory;
793 auto found = filename.rfind(
".");
794 if (found != std::string::npos)
796 suffix = filename.substr(found);
797 filename.erase(found);
799 found = filename.rfind(
"/");
800 if (found != std::string::npos)
802 directory = filename.substr(0, found + 1);
803 filename.erase(0, found + 1);
806 file <<
"%%MatrixMarket tuplediagmatrix coordinate real general\n";
807 file << filename <<
"_td" << 1 << suffix <<
"\n";
816 _first.write_out(mode, directory + prefix +
"_td" +
stringify(length) + suffix);
821 return TupleDiagMatrix(
_first.clone(mode));
825 std::size_t
bytes()
const
832 return TupleDiagMatrix(
_first.transpose());
835 template<
int i,
int j>
836 typename TupleElement<i, First_>::Type&
at()
838 static_assert(i == 0,
"invalid sub-matrix index");
839 static_assert(j == 0,
"invalid sub-matrix index");
843 template<
int i,
int j>
844 const typename TupleElement<i, First_>::Type&
at()
const
846 static_assert(i == 0,
"invalid sub-matrix index");
847 static_assert(j == 0,
"invalid sub-matrix index");
856 const First_& first()
const
861 int row_blocks()
const
866 int col_blocks()
const
873 return first().rows();
878 return first().columns();
883 return first().used_elements();
888 first().format(
value);
909 first().extract_diag(diag.first());
914 first().apply(r.first(), x.first());
919 first().apply(r.first(), x.first(), y.first(), alpha);
924 first().apply_transposed(r.first(), x.first());
929 first().apply_transposed(r.first(), x.first(), y.first(), alpha);
944 void scale_rows(
const TupleDiagMatrix& a,
const VectorTypeL& w)
946 first().scale_rows(a.first(), w.first());
949 void scale_cols(
const TupleDiagMatrix& a,
const VectorTypeR& w)
951 first().scale_cols(a.first(), w.first());
957 return this->first().get_length_of_line(row);
962 const Index col_start,
const Index stride = 1)
const
964 this->first().set_line(row, pval_set, pcol_set, col_start, stride);
967 void set_line_reverse(
const Index row,
DataType *
const pval_set,
const Index stride = 1)
const
969 this->first().set_line_reverse(row, pval_set, stride);
975 return _first.get_checkpoint_size(config);
981 _first.restore_from_checkpoint_data(data);
987 return _first.set_checkpoint_data(data, config);
997 template <
typename First2_>
998 void convert(
const TupleDiagMatrix<First2_>& other)
1000 this->first().convert(
other.first());
1003 template <
typename First2_>
1004 void convert_reverse(TupleDiagMatrix<First2_>& other)
const
1006 this->first().convert_reverse(
other.first());
1019 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.
Index size() const
Returns the containers size.
Dense data vector class template.
Config class for serialize parameter.
Tuple-Diag-Matrix meta class template.
void write_out(FileMode mode, String filename) const
Write out matrix to file.
TupleDiagMatrix< Rest_... > RestClass
rest-class typedef
TupleDiagMatrix(FileMode mode, std::istream &file, const String &directory="")
Constructor.
TupleDiagMatrix & operator=(TupleDiagMatrix &&other)
move-assign operator
TupleDiagMatrix()
default ctor
void convert(const TupleDiagMatrix< First2_, Rest2_... > &other)
Conversion method.
TupleDiagMatrix(const TupleDiagMatrix &)=delete
deleted copy-ctor
TupleDiagMatrix clone(LAFEM::CloneMode mode=LAFEM::CloneMode::Weak) const
Creates and returns a deep copy of this matrix.
TupleDiagMatrix(First_ &&the_first, RestClass &&the_rest)
base-class constructor; this one is protected for a reason
void restore_from_checkpoint_data(std::vector< char > &data)
Extract object from checkpoint.
Index columns() const
Returns the total number of columns in this matrix.
void format(DataType value=DataType(0))
Reset all elements of the container to a given value or zero if missing.
Index get_length_of_line(const Index row) const
Returns the number of NNZ-elements of the selected row.
TupleDiagMatrix< typename First_::template ContainerType< DT2_, IT2_ >, typename Rest_::template ContainerType< DT2_, IT2_ >... > ContainerType
Our 'base' class type.
TupleDiagMatrix & operator=(const TupleDiagMatrix &)=delete
deleted copy-assign operator
First_ _first
the first sub-matrix
std::uint64_t get_checkpoint_size(SerialConfig &config)
Calculate size.
VectorTypeR create_vector_r() const
Returns a new compatible R-Vector.
const TupleElement< i_, First_, Rest_... >::Type & at() const
Returns a sub-matrix block.
void apply(VectorTypeL &r, const VectorTypeR &x, const VectorTypeL &y, DataType alpha=DataType(1)) const
Applies this matrix onto a vector.
void apply(VectorTypeL &r, const VectorTypeR &x) const
Applies this matrix onto a vector.
void read_from(FileMode mode, const String &filename)
Read in matrix from file.
std::uint64_t set_checkpoint_data(std::vector< char > &data, SerialConfig &config)
TupleDiagMatrix transpose() const
Creates and returns the transpose of this matrix.
First_::DataType DataType
sub-matrix data type
bool same_layout(const TupleDiagMatrix &x) const
Checks if the structural layout of this matrix matches that of another matrix. This excludes comparis...
void apply_transposed(VectorTypeR &r, const VectorTypeL &x, const VectorTypeR &y, DataType alpha=DataType(1)) const
Applies this matrix onto a vector.
RestClass _rest
the remaining part
Index rows() const
Returns the total number of rows in this matrix.
TupleElement< i_, First_, Rest_... >::Type & at()
Returns a sub-matrix block.
void clear()
Free all allocated arrays.
TupleDiagMatrix(FileMode mode, const String &filename)
Constructor.
static String name()
Returns a descriptive string for this container.
void extract_diag(VectorTypeL &diag) const
extract main diagonal vector from matrix
virtual ~TupleDiagMatrix()
virtual destructor
std::size_t bytes() const
Returns the total amount of bytes allocated.
TupleVector< typename First_::VectorTypeL, typename Rest_::VectorTypeL... > VectorTypeL
Compatible L-vector type.
TupleVector< typename First_::VectorTypeR, typename Rest_::VectorTypeR... > VectorTypeR
Compatible R-vector type.
void write_out_submatrices(FileMode mode, const String &directory, const String &prefix, const String &suffix, Index length=num_row_blocks) const
Write out submatrices to file.
static String sub_name_list()
Returns a list of all sub-matrix type names.
void apply_transposed(VectorTypeR &r, const VectorTypeL &x) const
Applies this matrix onto a vector.
static constexpr int num_row_blocks
number of row blocks (vertical size)
TupleDiagMatrix(TupleDiagMatrix &&other)
move ctor
First_::IndexType IndexType
sub-matrix index type
static constexpr int num_col_blocks
number of column blocks (horizontal size)
Index used_elements() const
Returns the total number of non-zeros in this matrix.
TupleDiagMatrix(First_ &&the_first, Rest_ &&... the_rest)
Sub-Matrix emplacement constructor.
VectorTypeL create_vector_l() const
Returns a new compatible L-Vector.
Variadic TupleVector class template.
String class implementation.
String & trim_me(const String &charset)
Trims this string.
@ other
generic/other permutation strategy
@ 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.
Tuple container element helper class template.