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().num_rows() + rest().num_rows();
341 return first().num_cols() + rest().num_cols();
347 return first().num_nzes() + rest().num_nzes();
353 return first().num_rows_raw() + rest().num_rows_raw();
359 return first().num_cols_raw() + rest().num_cols_raw();
365 return first().num_nzes_raw() + rest().num_nzes_raw();
388 first().format(
value);
389 rest().format(
value);
404 first().extract_diag(diag.first());
405 rest().extract_diag(diag.rest());
422 first().apply(r.first(), x.first());
423 rest().apply(r.rest(), x.rest());
428 XASSERTM(r.
size() == this->num_rows(),
"Vector size of r does not match!");
429 XASSERTM(x.
size() == this->num_cols(),
"Vector size of x does not match!");
437 first().apply(r_first, x_first);
438 rest().apply(r_rest, x_rest);
455 first().apply_transposed(r.first(), x.first());
456 rest().apply_transposed(r.rest(), x.rest());
461 XASSERTM(r.
size() == this->num_cols(),
"Vector size of r does not match!");
462 XASSERTM(x.
size() == this->num_rows(),
"Vector size of x does not match!");
470 first().apply_transposed(r_first, x_first);
471 rest().apply_transposed(r_rest, x_rest);
492 first().apply(r.first(), x.first(), y.first(), alpha);
493 rest().apply(r.rest(), x.rest(), y.rest(), alpha);
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!");
501 XASSERTM(y.
size() == this->num_rows(),
"Vector size of y does not match!");
512 first().apply(r_first, x_first, y_first, alpha);
513 rest().apply(r_rest, x_rest, y_rest, alpha);
534 first().apply_transposed(r.first(), x.first(), y.first(), alpha);
535 rest().apply_transposed(r.rest(), x.rest(), y.rest(), alpha);
541 XASSERTM(r.
size() == this->num_cols(),
"Vector size of r does not match!");
542 XASSERTM(x.
size() == this->num_rows(),
"Vector size of x does not match!");
543 XASSERTM(y.
size() == this->num_cols(),
"Vector size of y does not match!");
554 first().apply_transposed(r_first, x_first, y_first, alpha);
555 rest().apply_transposed(r_rest, x_rest, y_rest, alpha);
572 first().scale_rows(a.first(), w.first());
573 rest().scale_rows(a.rest(), w.rest());
576 void scale_cols(
const TupleDiagMatrix& a,
const VectorTypeR& w)
578 first().scale_cols(a.first(), w.first());
579 rest().scale_cols(a.rest(), w.rest());
584 const Index first_rows = first().num_rows_raw();
586 return first().row_degree(row);
588 return rest().row_degree(row - first_rows);
591 template<
typename IT2_>
592 Index get_row_col_indices(
const Index row, IT2_*
const pcol_idx,
const IT2_ col_offset)
const
594 const Index first_rows = first().num_rows_raw();
595 const Index first_cols = first().num_cols_raw();
597 return first().get_row_col_indices(row, pcol_idx, col_offset);
599 return rest().get_row_col_indices(row - first_rows, pcol_idx, col_offset + IT2_(first_cols));
602 template<
typename DT2_>
603 Index get_row_values(
const Index row, DT2_ *
const pvals)
const
605 const Index first_rows = first().num_rows_raw();
607 return first().get_row_values(row, pvals);
609 return rest().get_row_values(row - first_rows, pvals);
612 template<
typename DT2_>
613 Index set_row_values(
const Index row,
const DT2_ *
const pvals)
615 const Index first_rows = first().num_rows_raw();
617 return first().set_row_values(row, pvals);
619 return rest().set_row_values(row - first_rows, pvals);
633 std::uint64_t isize = *(std::uint64_t*) data.data();
634 std::vector<char>::iterator start = std::begin(data) +
sizeof(std::uint64_t);
635 std::vector<char>::iterator last_of_first = std::begin(data) +
sizeof(std::uint64_t) + (
int) isize;
636 std::vector<char> buffer_first(start, last_of_first);
637 _first.restore_from_checkpoint_data(buffer_first);
639 data.erase(std::begin(data), last_of_first);
646 std::size_t old_size = data.size();
647 data.insert(std::end(data),
sizeof(std::uint64_t), 0);
648 std::uint64_t ireal_size =
_first.set_checkpoint_data(data, config);
649 char* csize =
reinterpret_cast<char*
>(&ireal_size);
650 for(std::size_t i(0) ; i <
sizeof(std::uint64_t) ; ++i)
652 data[old_size +i] = csize[i];
665 template <
typename First2_,
typename... Rest2_>
668 this->first().convert(source.first());
669 this->rest().convert(source.rest());
672 template <
typename First2_,
typename... Rest2_>
675 this->first().copy(source.first());
676 this->rest().copy(source.rest());
679 template <
typename First2_,
typename... Rest2_>
680 void copy_to(TupleDiagMatrix<First2_, Rest2_...>& target)
const
682 this->first().copy_to(target.first());
683 this->rest().copy_to(target.rest());
696 return (this->
name() == x.
name()) && (this->first().same_layout(x.first())) && (this->rest().same_layout(x.rest()));
701 template<
typename First_>
702 class TupleDiagMatrix<First_>
704 template<
typename,
typename...>
705 friend class TupleDiagMatrix;
708 typedef typename First_::DataType
DataType;
709 typedef typename First_::IndexType
IndexType;
711 typedef TupleVector<typename First_::VectorTypeL>
VectorTypeL;
713 typedef TupleVector<typename First_::VectorTypeR>
VectorTypeR;
715 template <
typename DT2_ = DataType,
typename IT2_ = IndexType>
716 using ContainerType = TupleDiagMatrix<typename First_::template ContainerType<DT2_, IT2_> >;
726 return First_::name();
735 explicit TupleDiagMatrix(First_&& the_first) :
736 _first(std::forward<First_>(the_first))
741 TupleDiagMatrix(TupleDiagMatrix&& other) :
747 explicit TupleDiagMatrix(
FileMode mode,
const String& filename)
753 explicit TupleDiagMatrix(
FileMode mode, std::istream& file,
const String& directory =
"")
759 std::getline(file, line);
761 }
while (line.find(
"%%") == 0 || line ==
"");
763 _first = First_(mode, directory + line);
769 auto found = filename.rfind(
"/");
770 if (found != std::string::npos)
772 directory = filename.substr(0, found + 1);
775 std::ifstream file(filename.c_str(), std::ifstream::in);
776 if (! file.is_open())
777 XABORTM(
"Unable to open Matrix file " + filename);
779 TupleDiagMatrix
other(mode, file, directory);
787 TupleDiagMatrix&
operator=(TupleDiagMatrix&& other)
797 TupleDiagMatrix(
const TupleDiagMatrix&) =
delete;
799 TupleDiagMatrix&
operator=(
const TupleDiagMatrix&) =
delete;
808 std::ofstream file(filename.c_str(), std::ofstream::out);
809 if (! file.is_open())
810 XABORTM(
"Unable to open Matrix file " + filename);
812 String suffix, directory;
813 auto found = filename.rfind(
".");
814 if (found != std::string::npos)
816 suffix = filename.substr(found);
817 filename.erase(found);
819 found = filename.rfind(
"/");
820 if (found != std::string::npos)
822 directory = filename.substr(0, found + 1);
823 filename.erase(0, found + 1);
826 file <<
"%%MatrixMarket tuplediagmatrix coordinate real general\n";
827 file << filename <<
"_td" << 1 << suffix <<
"\n";
836 _first.write_out(mode, directory + prefix +
"_td" +
stringify(length) + suffix);
841 return TupleDiagMatrix(
_first.clone(mode));
845 std::size_t
bytes()
const
852 return TupleDiagMatrix(
_first.transpose());
855 template<
int i,
int j>
856 typename TupleElement<i, First_>::Type&
at()
858 static_assert(i == 0,
"invalid sub-matrix index");
859 static_assert(j == 0,
"invalid sub-matrix index");
863 template<
int i,
int j>
864 const typename TupleElement<i, First_>::Type&
at()
const
866 static_assert(i == 0,
"invalid sub-matrix index");
867 static_assert(j == 0,
"invalid sub-matrix index");
876 const First_& first()
const
881 int row_blocks()
const
886 int col_blocks()
const
893 return first().num_rows();
898 return first().num_cols();
903 return first().num_nzes();
908 return first().num_rows_raw();
913 return first().num_cols_raw();
918 return first().num_nzes_raw();
928 first().format(
value);
944 first().extract_diag(diag.first());
949 first().apply(r.first(), x.first());
954 first().apply(r.first(), x.first(), y.first(), alpha);
959 first().apply_transposed(r.first(), x.first());
964 first().apply_transposed(r.first(), x.first(), y.first(), alpha);
979 void scale_rows(
const TupleDiagMatrix& a,
const VectorTypeL& w)
981 first().scale_rows(a.first(), w.first());
984 void scale_cols(
const TupleDiagMatrix& a,
const VectorTypeR& w)
986 first().scale_cols(a.first(), w.first());
991 return first().row_degree(row);
994 template<
typename IT2_>
995 Index get_row_col_indices(
const Index row, IT2_*
const pcol_idx,
const IT2_ col_offset)
const
997 return first().get_row_col_indices(row, pcol_idx, col_offset);
1000 template<
typename DT2_>
1001 Index get_row_values(
const Index row, DT2_ *
const pvals)
const
1003 return first().get_row_values(row, pvals);
1006 template<
typename DT2_>
1007 Index set_row_values(
const Index row,
const DT2_ *
const pvals)
1009 return first().set_row_values(row, pvals);
1015 return _first.get_checkpoint_size(config);
1021 _first.restore_from_checkpoint_data(data);
1027 return _first.set_checkpoint_data(data, config);
1037 template <
typename First2_>
1038 void convert(
const TupleDiagMatrix<First2_>& source)
1040 this->first().convert(source.first());
1043 template <
typename First2_>
1044 void copy(
const TupleDiagMatrix<First2_>& source)
1046 this->first().copy(source.first());
1049 template <
typename First2_>
1050 void copy_to(TupleDiagMatrix<First2_>& target)
const
1052 this->first().copy_to(target.first());
1065 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.
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
TupleDiagMatrix(const TupleDiagMatrix &)=delete
deleted copy-ctor
Index num_nzes_raw() const
Returns the total number of non-zeros in this matrix.
TupleDiagMatrix clone(LAFEM::CloneMode mode=LAFEM::CloneMode::Weak) const
Creates and returns a deep copy of this matrix.
void format()
Reset all elements of the container to a given value or zero if missing.
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 num_nzes() const
Returns the total number of non-zeros in this matrix.
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 num_cols_raw() const
Returns the total number of columns in this matrix.
TupleElement< i_, First_, Rest_... >::Type & at()
Returns a sub-matrix block.
void clear()
Free all allocated arrays.
Index num_rows() const
Returns the total number of rows in this matrix.
void convert(const TupleDiagMatrix< First2_, Rest2_... > &source)
Conversion method.
Index num_cols() const
Returns the total number of columns in this matrix.
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 num_rows_raw() const
Returns the total number of rows 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.