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 powercolmatrix 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 powercolmatrix coordinate real general\n";
225 for (
Index i(1); i <= blocks_; ++i)
227 file << filename <<
"_pc" << i << suffix <<
"\n";
245 _first.write_out(mode, directory + prefix +
"_pc" +
stringify(length + 1 - blocks_) + suffix);
275 template<
int i_,
int j_>
278 static_assert(j_ == 0,
"invalid sub-matrix index");
279 static_assert((0 <= i_) && (i_ < blocks_),
"invalid sub-matrix index");
284 template<
int i_,
int j_>
287 static_assert(j_ == 0,
"invalid sub-matrix index");
288 static_assert((0 <= i_) && (i_ < blocks_),
"invalid sub-matrix index");
303 XASSERTM((j == 0) && (0 <= i) && (i < blocks_),
"invalid sub-matrix index");
310 XASSERTM((j == 0) && (0 <= i) && (i < blocks_),
"invalid sub-matrix index");
335 int row_blocks()
const
340 int col_blocks()
const
352 template <Perspective perspective_ = Perspective::native>
355 return first().template rows<perspective_>() + rest().template rows<perspective_>();
364 template <Perspective perspective_ = Perspective::native>
367 return rest().template columns<perspective_>();
376 template <Perspective perspective_ = Perspective::native>
379 return first().template used_elements<perspective_>() + rest().template used_elements<perspective_>();
385 return String(
"PowerColMatrix<") + SubMatrixType::name() +
"," +
stringify(blocks_) +
">";
388 template <Perspective perspective_ = Perspective::native>
391 return rows<perspective_>() * columns<perspective_>();
402 first().format(
value);
403 rest().format(
value);
429 first().apply(r.first(), x);
430 rest().apply(r.rest(), x);
435 XASSERTM(r.
size() == this->rows(),
"Vector size of r does not match!");
436 XASSERTM(x.
size() == this->columns(),
"Vector size of x does not match!");
441 first().apply(r_first, x);
442 rest().apply(r_rest, x);
459 first().apply_transposed(r, x.first());
460 rest().apply_transposed(r, x.rest(), r,
DataType(1));
465 XASSERTM(r.
size() == this->columns(),
"Vector size of r does not match!");
466 XASSERTM(x.
size() == this->rows(),
"Vector size of x does not match!");
471 first().apply_transposed(r, x_first);
472 rest().apply_transposed(r, x_rest, r,
DataType(1));
493 first().apply(r.first(), x, y.first(), alpha);
494 rest().apply(r.rest(), x, y.rest(), alpha);
500 XASSERTM(r.
size() == this->rows(),
"Vector size of r does not match!");
501 XASSERTM(x.
size() == this->columns(),
"Vector size of x does not match!");
502 XASSERTM(y.
size() == this->rows(),
"Vector size of y does not match!");
510 first().apply(r_first, x, y_first, alpha);
511 rest().apply(r_rest, x, y_rest, alpha);
532 first().apply_transposed(r, x.first(), y, alpha);
533 rest().apply_transposed(r, x.rest(), r, alpha);
539 XASSERTM(r.
size() == this->columns(),
"Vector size of r does not match!");
540 XASSERTM(x.
size() == this->rows(),
"Vector size of x does not match!");
541 XASSERTM(y.
size() == this->columns(),
"Vector size of y does not match!");
546 first().apply_transposed(r, x_first, y, alpha);
547 rest().apply_transposed(r, x_rest, r, alpha);
572 return (this->
name() == x.
name()) && (this->first().same_layout(x.first())) && (this->rest().same_layout(x.rest()));
584 return first().create_vector_r();
590 const Index brows(this->first().
template rows<Perspective::pod>());
594 return this->first().get_length_of_line(row);
598 return this->rest().get_length_of_line(row - brows);
605 const Index col_start,
const Index stride = 1)
const
607 const Index brows(this->first().
template rows<Perspective::pod>());
611 this->first().set_line(row, pval_set, pcol_set, col_start, stride);
615 this->rest().set_line(row - brows, pval_set, pcol_set, col_start, stride);
619 void set_line_reverse(
const Index row,
const DataType *
const pval_set,
const Index stride = 1)
621 const Index brows(this->first().
template rows<Perspective::pod>());
625 this->first().set_line_reverse(row, pval_set, stride);
629 this->
rest().set_line_reverse(row - brows, pval_set, stride);
643 std::uint64_t isize = *(std::uint64_t*) data.data();
644 std::vector<char>::iterator start = std::begin(data) +
sizeof(std::uint64_t);
645 std::vector<char>::iterator last_of_first = std::begin(data) +
sizeof(std::uint64_t) + (
int) isize;
646 std::vector<char> buffer_first(start, last_of_first);
647 _first.restore_from_checkpoint_data(buffer_first);
649 data.erase(std::begin(data), last_of_first);
656 std::size_t old_size = data.size();
657 data.insert(std::end(data),
sizeof(std::uint64_t), 0);
658 std::uint64_t ireal_size =
_first.set_checkpoint_data(data, config);
659 char* csize =
reinterpret_cast<char*
>(&ireal_size);
660 for(std::size_t i(0) ; i <
sizeof(std::uint64_t) ; ++i)
662 data[old_size + i] = csize[i];
675 template <
typename SubType2_>
678 this->first().convert(other.first());
679 this->rest().convert(other.rest());
682 template <
typename SubType2_>
685 this->first().convert_reverse(other.first());
686 this->rest().convert_reverse(other.rest());
691 template<
typename SubType_>
692 class PowerColMatrix<SubType_, 1>
694 template<
typename,
int>
695 friend class PowerColMatrix;
699 typedef typename SubMatrixType::DataType
DataType;
700 typedef typename SubMatrixType::IndexType
IndexType;
704 typedef PowerVector<typename SubMatrixType::VectorTypeL, 1>
VectorTypeL;
706 typedef typename SubMatrixType::VectorTypeR
VectorTypeR;
708 template <
typename DT2_ = DataType,
typename IT2_ = IndexType>
709 using ContainerType = PowerColMatrix<typename SubType_::template ContainerType<DT2_, IT2_>, 1>;
719 _first(std::move(the_first))
730 explicit PowerColMatrix(
const SparseLayout<IndexType, layout_id>& layout) :
736 PowerColMatrix(PowerColMatrix&& other) :
742 explicit PowerColMatrix(
FileMode mode, String filename)
748 explicit PowerColMatrix(
FileMode mode, std::istream& file, String directory =
"")
754 std::getline(file, line);
756 }
while (line.find(
"%%") == 0 || line ==
"");
759 _first = std::move(tmp_first);
765 auto found = filename.rfind(
"/");
766 if (found != std::string::npos)
768 directory = filename.substr(0, found + 1);
771 std::ifstream file(filename.c_str(), std::ifstream::in);
772 if (! file.is_open())
773 XABORTM(
"Unable to open Matrix file " + filename);
775 PowerColMatrix
other(mode, file, directory);
783 PowerColMatrix&
operator=(PowerColMatrix&& other)
793 PowerColMatrix(
const PowerColMatrix&) =
delete;
795 PowerColMatrix&
operator=(
const PowerColMatrix&) =
delete;
804 std::ofstream file(filename.c_str(), std::ofstream::out);
805 if (! file.is_open())
806 XABORTM(
"Unable to open Matrix file " + filename);
808 String suffix, directory;
809 auto found = filename.rfind(
".");
810 if (found != std::string::npos)
812 suffix = filename.substr(found);
813 filename.erase(found);
815 found = filename.rfind(
"/");
816 if (found != std::string::npos)
818 directory = filename.substr(0, found + 1);
819 filename.erase(0, found + 1);
822 file <<
"%%MatrixMarket powercolmatrix coordinate real general\n";
823 file << filename <<
"_pc" << 1 << suffix <<
"\n";
832 _first.write_out(mode, directory + prefix +
"_pc" +
stringify(length) + suffix);
837 return PowerColMatrix(
_first.clone(mode));
841 std::size_t
bytes()
const
846 template<
int i,
int j>
849 static_assert(i == 0,
"invalid sub-matrix index");
850 static_assert(j == 0,
"invalid sub-matrix index");
854 template<
int i,
int j>
857 static_assert(i == 0,
"invalid sub-matrix index");
858 static_assert(j == 0,
"invalid sub-matrix index");
864 XASSERTM((i == 0) && (j == 0),
"invalid sub-matrix index");
870 XASSERTM((i == 0) && (j == 0),
"invalid sub-matrix index");
884 int row_blocks()
const
889 int col_blocks()
const
894 template <Perspective perspective_ = Perspective::native>
897 return first().template rows<perspective_>();
900 template <Perspective perspective_ = Perspective::native>
903 return first().template columns<perspective_>();
906 template <Perspective perspective_ = Perspective::native>
909 return first().template used_elements<perspective_>();
914 return String(
"PowerColMatrix<") + SubMatrixType::name() +
"," +
stringify(1) +
">";
917 template <Perspective perspective_ = Perspective::native>
920 return rows<perspective_>() * columns<perspective_>();
925 first().format(
value);
935 first().apply(r.first(), x);
938 void apply(DenseVector<DataType, IndexType>& r,
const DenseVector<DataType, IndexType>& x)
const
945 first().apply(r.first(), x, y.first(), alpha);
948 void apply(DenseVector<DataType, IndexType>& r,
const DenseVector<DataType, IndexType>& x,
949 const DenseVector<DataType, IndexType>& y,
DataType alpha =
DataType(1))
const
951 first().apply(r, x, y, alpha);
956 first().apply_transposed(r, x.first());
959 void apply_transposed(DenseVector<DataType, IndexType>& r,
const DenseVector<DataType, IndexType>& x)
const
961 first().apply_transposed(r, x);
966 first().apply_transposed(r, x.first(), y, alpha);
969 void apply_transposed(DenseVector<DataType, IndexType>& r,
const DenseVector<DataType, IndexType>& x,
970 const DenseVector<DataType, IndexType>& y,
DataType alpha =
DataType(1))
const
972 first().apply_transposed(r, x, y, alpha);
983 return this->first().max_rel_diff(x.first());
1001 return this->first().get_length_of_line(row);
1006 const Index col_start,
const Index stride = 1)
const
1008 this->first().set_line(row, pval_set, pcol_set, col_start, stride);
1011 void set_line_reverse(
const Index row,
const DataType *
const pval_set,
const Index stride = 1)
1013 this->first().set_line_reverse(row, pval_set, stride);
1019 return _first.get_checkpoint_size(config);
1025 _first.restore_from_checkpoint_data(data);
1031 return _first.set_checkpoint_data(data, config);
1041 template <
typename SubType2_>
1042 void convert(
const PowerColMatrix<SubType2_, 1> & other)
1044 this->first().convert(
other.first());
1047 template <
typename SubType2_>
1048 void convert_reverse(PowerColMatrix<SubType2_, 1> & other)
const
1050 this->first().convert_reverse(
other.first());
1063 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.
Power-Col-Matrix meta class template.
PowerColMatrix(const PowerColMatrix &)=delete
deleted copy-ctor
PowerColMatrix< typename SubType_::template ContainerType< DT2_, IT2_ >, blocks_ > ContainerType
Our 'base' class type.
PowerColMatrix clone(LAFEM::CloneMode mode=LAFEM::CloneMode::Weak) const
Creates and returns a deep copy of this matrix.
VectorTypeL create_vector_l() const
Returns a new compatible L-Vector.
void restore_from_checkpoint_data(std::vector< char > &data)
Extract object from checkpoint.
SubMatrixType _first
the last sub-matrix
PowerVector< typename SubMatrixType::VectorTypeL, blocks_ > VectorTypeL
Compatible L-vector type.
std::uint64_t get_checkpoint_size(SerialConfig &config)
Calculate size.
const SubMatrixType & get(int i, int j) 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.
PowerColMatrix & operator=(const PowerColMatrix &)=delete
deleted copy-assign operator
void write_out(FileMode mode, String filename) const
Write out matrix to file.
VectorTypeR create_vector_r() const
Returns a new compatible R-Vector.
SubMatrixType::DataType DataType
sub-matrix data type
Index rows() const
Returns the total number of rows in this matrix.
PowerColMatrix(PowerColMatrix &&other)
move ctor
PowerColMatrix(FileMode mode, const String &filename)
Constructor.
SubType_ SubMatrixType
sub-matrix type
virtual ~PowerColMatrix()
virtual destructor
void convert(const PowerColMatrix< SubType2_, blocks_ > &other)
Conversion method.
static constexpr int num_row_blocks
number of row blocks (vertical size)
SubMatrixType & get(int i, int j)
Returns a sub-matrix block.
std::uint64_t set_checkpoint_data(std::vector< char > &data, SerialConfig &config)
PowerColMatrix(const SparseLayout< IndexType, layout_id > &layout)
sub-matrix layout ctor
void apply_transposed(VectorTypeR &r, const VectorTypeL &x) const
Applies this matrix onto a vector.
static String name()
Returns a descriptive string for this container.
PowerColMatrix(SubMatrixType &&the_first, RestClass &&the_rest)
base-class constructor; this one is protected for a reason
RestClass _rest
the remaining part
SubMatrixType::IndexType IndexType
sub-matrix index type
PowerColMatrix(FileMode mode, std::istream &file, String directory="")
Constructor.
void format(DataType value=DataType(0))
Reset all elements of the container to a given value or zero if missing.
PowerColMatrix & operator=(PowerColMatrix &&other)
move-assign operator
bool same_layout(const PowerColMatrix &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.
Index used_elements() const
Returns the total number of non-zeros in this matrix.
Index columns() const
Returns the total number of columns in this matrix.
void write_out_submatrices(FileMode mode, String directory, String prefix, String suffix, Index length=blocks_) const
Write out submatrices to file.
void read_from(FileMode mode, const String &filename)
Read in matrix from file.
void apply(VectorTypeL &r, const VectorTypeR &x) const
Applies this matrix onto a vector.
Index get_length_of_line(const Index row) const
Returns the number of NNZ-elements of the selected row.
static constexpr int num_col_blocks
number of column blocks (horizontal size)
PowerColMatrix()
default ctor
std::size_t bytes() const
Returns the total amount of bytes allocated.
void clear()
Free all allocated arrays.
DataType max_rel_diff(const PowerColMatrix &x) const
Retrieve the maximum relative difference of this matrix and another one y.max_rel_diff(x) returns .
SubMatrixType::VectorTypeR VectorTypeR
Compatible R-vector type.
const SubMatrixType & at() const
Returns a sub-matrix block.
SubMatrixType & at()
Returns a sub-matrix block.
PowerColMatrix< SubType_, blocks_-1 > RestClass
rest-class typedef
static constexpr SparseLayoutId layout_id
sub-matrix layout type
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.