10#include <kernel/lafem/forward.hpp>
12#include <kernel/util/math.hpp>
13#include <kernel/lafem/container.hpp>
14#include <kernel/lafem/arch/scale.hpp>
15#include <kernel/lafem/arch/norm.hpp>
16#include <kernel/lafem/arch/axpy.hpp>
17#include <kernel/lafem/arch/apply.hpp>
18#include <kernel/lafem/arch/product_matmat.hpp>
19#include <kernel/lafem/arch/transpose.hpp>
20#include <kernel/lafem/dense_vector.hpp>
45 template <
typename DT_,
typename IT_ = Index>
69 template <
typename DT2_ = DT_,
typename IT2_ = IT_>
114 Container<DT_, IT_>(rows_in * columns_in)
132 template <
typename DT2_ = DT_,
typename IT2_ = IT_>
136 deserialize<DT2_, IT2_>(input);
185 this->
move(std::forward<DenseMatrix>(other));
201 t.
clone(*
this, clone_mode);
213 template<
typename DT2_,
typename IT2_>
226 template <
typename DT2_,
typename IT2_>
259 MemoryPool::synchronize();
275 MemoryPool::synchronize();
285 template <
typename DT2_ = DT_,
typename IT2_ = IT_>
304 template <
typename DT2_ = DT_,
typename IT2_ = IT_>
319 std::ios_base::openmode bin = std::ifstream::in | std::ifstream::binary;
321 bin = std::ifstream::in;
322 std::ifstream file(filename.c_str(), bin);
324 XABORTM(
"Unable to open Matrix file " + filename);
346 std::getline(file, line);
348 const bool array_format((line.find(
"%%MatrixMarket matrix array real general") != String::npos) ?
true :
false);
349 if (array_format ==
false)
351 XABORTM(
"Input-file is not a compatible array real mtx-file");
356 std::getline(file,line);
358 XABORTM(
"Input-file is empty");
360 String::size_type begin(line.find_first_not_of(
" "));
361 if (line.at(begin) !=
'%')
366 String::size_type begin(line.find_first_not_of(
" "));
367 line.erase(0, begin);
368 String::size_type end(line.find_first_of(
" "));
369 String srow(line, 0, end);
370 trows =
Index(atol(srow.c_str()));
373 begin = line.find_first_not_of(
" ");
374 line.erase(0, begin);
375 end = line.find_first_of(
" ");
376 String scol(line, 0, end);
377 tcols =
Index(atol(scol.c_str()));
387 std::getline(file, line);
391 String::size_type begin = line.find_first_not_of(
" ");
392 line.erase(0, begin);
393 String::size_type end = line.find_first_of(
" ");
394 String sval(line, 0, end);
395 DT_ tval((DT_)atof(sval.c_str()));
397 Index row(i / tcols);
398 Index col(i % tcols);
399 result(row, col, tval);
403 XASSERTM(i == trows * tcols,
"Dense MTX file did not contain enough entries!");
405 this->
move(std::move(result));
411 this->
template _deserialize<double, std::uint64_t>(
FileMode::fm_dm, file);
414 XABORTM(
"Filemode not supported!");
426 std::ios_base::openmode bin = std::ofstream::out | std::ofstream::binary;
428 bin = std::ofstream::out;
430 char* buff =
nullptr;
436 file.open(filename.c_str(), bin);
438 XABORTM(
"Unable to open Matrix file " + filename);
456 file <<
"%%MatrixMarket matrix array real general\n";
459 for(IT_ row(0) ; row <
rows(); ++row)
461 for(IT_ col(0) ; col <
columns() ; ++col)
470 this->
template _serialize<double, std::uint64_t>(
FileMode::fm_dm, file);
473 XABORTM(
"Filemode not supported!");
505 return "DenseMatrix";
516 this->_copy_content(x, full);
547 XASSERTM(x.
rows() == this->rows(),
"Row count does not match!");
548 XASSERTM(x.
columns() == this->columns(),
"Column count does not match!");
554 Arch::Scale::value(this->
elements(), x.
elements(), alpha, this->used_elements());
557 Statistics::add_time_axpy(ts_stop.
elapsed(ts_start));
573 Statistics::add_time_reduction(ts_stop.
elapsed(ts_start));
587 const DT_ alpha = DT_(1))
589 XASSERTM(x.
size() == this->size(),
"Vector size does not match!");
597 Statistics::add_time_axpy(ts_stop.
elapsed(ts_start));
608 XASSERTM(r.
size() == this->rows(),
"Vector size of r does not match!");
609 XASSERTM(x.
size() == this->columns(),
"Vector size of x does not match!");
611 XASSERTM(r.template elements<Perspective::pod>() != x.template elements<Perspective::pod>(),
"Vector x and r must not share the same memory!");
616 Arch::Apply::dense(r.
elements(), DT_(1), DT_(0), r.
elements(), this->elements(),
617 x.
elements(), this->rows(), this->columns());
620 Statistics::add_time_blas2(ts_stop.
elapsed(ts_start));
631 XASSERTM(r.
size() == this->columns(),
"Vector size of r does not match!");
632 XASSERTM(x.
size() == this->rows(),
"Vector size of x does not match!");
634 XASSERTM(r.template elements<Perspective::pod>() != x.template elements<Perspective::pod>(),
"Vector x and r must not share the same memory!");
639 Arch::Apply::dense_transposed(r.
elements(), DT_(1), DT_(0), r.
elements(), this->elements(),
640 x.
elements(), this->rows(), this->columns());
643 Statistics::add_time_blas2(ts_stop.
elapsed(ts_start));
658 const DT_ alpha = DT_(1))
const
660 XASSERTM(r.
size() == this->rows(),
"Vector size of r does not match!");
661 XASSERTM(x.
size() == this->columns(),
"Vector size of x does not match!");
662 XASSERTM(y.
size() == this->rows(),
"Vector size of y does not match!");
664 XASSERTM(r.template elements<Perspective::pod>() != x.template elements<Perspective::pod>(),
"Vector x and r must not share the same memory!");
675 Arch::Apply::dense(r.
elements(), alpha, DT_(1), y.
elements(), this->elements(),
676 x.
elements(), this->rows(), this->columns());
679 Statistics::add_time_blas2(ts_stop.
elapsed(ts_start));
694 const DT_ alpha = DT_(1))
const
696 XASSERTM(r.
size() == this->columns(),
"Vector size of r does not match!");
697 XASSERTM(x.
size() == this->rows(),
"Vector size of x does not match!");
698 XASSERTM(y.
size() == this->columns(),
"Vector size of y does not match!");
700 XASSERTM(r.template elements<Perspective::pod>() != x.template elements<Perspective::pod>(),
"Vector x and r must not share the same memory!");
711 Arch::Apply::dense_transposed(r.
elements(), alpha, DT_(1), y.
elements(), this->elements(),
712 x.
elements(), this->rows(), this->columns());
715 Statistics::add_time_blas2(ts_stop.
elapsed(ts_start));
732 Statistics::add_time_reduction(ts_stop.
elapsed(ts_start));
747 if(this->
size() == 0 && x.
size() == 0 && this->get_elements().size() == 0 && x.
get_elements().size() == 0)
778 Arch::ProductMatMat::dense(this->
elements(), DT_(1.0), DT_(0.0), x.
elements(),
779 y.
elements(), this->elements(), this->rows(), this->columns(), x.
columns());
782 Statistics::add_time_blas3(ts_stop.
elapsed(ts_start));
801 Statistics::add_time_blas3(ts_stop.
elapsed(ts_start));
815 const DT_ alpha = DT_(1),
816 const DT_ beta = DT_(1))
826 y.
elements(), this->elements(), this->rows(), this->columns(), x.
columns());
829 Statistics::add_time_axpy(ts_stop.
elapsed(ts_start));
843 const DT_ alpha = DT_(1),
844 const DT_ beta = DT_(1))
857 Statistics::add_time_axpy(ts_stop.
elapsed(ts_start));
868 IT_ * temp =
new IT_[this->
rows()];
873 Statistics::add_time_blas3(ts_stop.
elapsed(ts_start));
914 this->
move(std::move(r));
928 this->_rows() = this->
columns();
929 this->_columns() = t;
937 void set_line(
const Index row, DT_ *
const pval_set, IT_ *
const pcol_set,
938 const Index col_start,
const Index stride = 1)
const
944 pval_set[i * stride] = pval[
columns() * row + i];
945 pcol_set[i * stride] = IT_(i) + IT_(col_start);
949 void set_line_reverse(
const Index row, DT_ *
const pval_set,
const Index stride = 1)
955 pval_set[i * stride] = pval[
columns() * row + i];
#define ASSERT(expr)
Debug-Assertion macro definition.
#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.
const std::vector< IT_ * > & get_indices() const
Returns a list of all Index arrays.
std::vector< DT_ * > _elements
List of pointers to all datatype dependent arrays.
Index used_elements() const
Returns the number of effective stored elements.
const std::vector< DT_ * > & get_elements() const
Returns a list of all data arrays.
std::vector< Index > _elements_size
List of corresponding datatype array sizes.
Index size() const
Returns the containers size.
void assign(const Container< DT2_, IT2_ > &other)
Assignment operation.
void clone(const Container &other, CloneMode clone_mode=CloneMode::Weak)
Clone operation.
void move(Container &&other)
Assignment move operation.
virtual void clear()
Free all allocated arrays.
std::vector< Index > _scalar_index
List of scalars with datatype index.
Dense data matrix class template.
void operator()(Index row, Index col, DT_ value)
Set specific matrix element.
void multiply(const DenseMatrix &x, const DenseMatrix &y, const DT_ alpha=DT_(1), const DT_ beta=DT_(1))
Calculate .
DenseMatrix(DenseMatrix &&other)
Move Constructor.
void read_from(FileMode mode, const String &filename)
Read in matrix from file.
DenseMatrix()
Constructor.
DenseMatrix(Index rows_in, Index columns_in, DT_ value)
Constructor.
DT_ * elements()
Get a pointer to the data array.
DenseMatrix(std::vector< char > input)
Constructor.
void apply(DenseVector< DT_, IT_ > &r, const DenseVector< DT_, IT_ > &x) const
Calculate .
void apply_transposed(DenseVector< DT_, IT_ > &r, const DenseVector< DT_, IT_ > &x) const
Calculate .
void invert()
Invert the matrix insitu.
void copy(const DenseMatrix &x, bool full=false)
Performs .
static String name()
Returns a descriptive string.
bool same_layout(const DenseMatrix &x) const
Checks if the structural layout of this matrix matches that of another matrix. This excludes comparis...
const DT_ operator()(Index row, Index col) const
Retrieve specific matrix element.
void multiply(DenseMatrix &x, DenseMatrix &y)
Calculate .
DenseMatrix & operator=(DenseMatrix &&other)
Move operator.
void read_from(FileMode mode, std::istream &file)
Read in matrix from file.
DenseMatrix(FileMode mode, String filename)
Constructor.
DenseVector< DT_, IT_ > VectorTypeR
Compatible R-vector type.
VectorTypeR create_vector_r() const
Returns a new compatible R-Vector.
DenseMatrix(FileMode mode, std::istream &file)
Constructor.
DenseMatrix clone(CloneMode clone_mode=CloneMode::Deep) const
Clone operation.
void axpy(const DenseMatrix &x, const DT_ alpha=DT_(1))
Calculate .
void transpose(const DenseMatrix &x)
Calculate .
void clone(const DenseMatrix< DT2_, IT2_ > &other, CloneMode clone_mode=CloneMode::Deep)
Clone operation.
DenseMatrix transpose() const
Calculate .
void write_out(FileMode mode, std::ostream &file) const
Write outmatrix to file.
DenseMatrix inverse() const
Create an inverse of the current matrix.
Index rows() const
Retrieve matrix row count.
void multiply(const SparseMatrixCSR< DT_, IT_ > &x, const DenseMatrix &y, const DT_ alpha=DT_(1), const DT_ beta=DT_(1))
Calculate .
void write_out(FileMode mode, const String &filename) const
Write out matrix to file.
DT_ DataType
Our datatype.
friend std::ostream & operator<<(std::ostream &lhs, const DenseMatrix &b)
DenseMatrix streaming operator.
IT_ IndexType
Our indextype.
VectorTypeL create_vector_l() const
Returns a new compatible L-Vector.
DT_ norm_frobenius() const
Calculates the Frobenius norm of this matrix.
DenseVector< DT_, IT_ > VectorTypeL
Compatible L-vector type.
DenseMatrix(Index rows_in, Index columns_in)
Constructor.
std::vector< char > serialize(const LAFEM::SerialConfig &config=LAFEM::SerialConfig()) const
Serialization of complete container entity.
void apply_transposed(DenseVector< DT_, IT_ > &r, const DenseVector< DT_, IT_ > &x, const DenseVector< DT_, IT_ > &y, const DT_ alpha=DT_(1)) const
Calculate .
void multiply(SparseMatrixCSR< DT_, IT_ > &x, DenseMatrix &y)
Calculate .
DT_ max_rel_diff(const DenseMatrix &x) const
Retrieve the maximum relative difference of this matrix and another one y.max_rel_diff(x) returns .
void convert(const DenseMatrix< DT2_, IT2_ > &other)
Conversion method.
void scale(const DenseMatrix &x, const DT_ alpha)
Calculate .
Index columns() const
Retrieve matrix column count.
void transpose_inplace()
Calculate inplace.
Index get_length_of_line(const Index) const
Returns the number of NNZ-elements of the selected row.
void apply(DenseVector< DT_, IT_ > &r, const DenseVector< DT_, IT_ > &x, const DenseVector< DT_, IT_ > &y, const DT_ alpha=DT_(1)) const
Calculate .
void deserialize(std::vector< char > input)
Deserialization of complete container entity.
Dense data vector class template.
DT_ * elements()
Get a pointer to the data array.
void copy(const VT_ &a)
Performs .
Config class for serialize parameter.
IT_ * col_ind()
Retrieve column indices array.
DT_ * val()
Retrieve non zero element array.
Index rows() const
Retrieve matrix row count.
Index columns() const
Retrieve matrix column count.
Index used_elements() const
Retrieve non zero element count.
IT_ * row_ptr()
Retrieve row start index array.
static void set_memory(DT_ *address, const DT_ val, const Index count=1)
set memory to specific value
static void add_flops(Index flops)
Add an amount of flops to the global flop counter.
String class implementation.
double elapsed(const TimeStamp &before) const
Calculates the time elapsed between two time stamps.
constexpr std::size_t FileOutStreamBufferSize
OutStreamBufferSize.
T_ abs(T_ x)
Returns the absolute value.
DT_ invert_matrix(const IT_ n, const IT_ stride, DT_ a[], IT_ p[])
Inverts a matrix and returns its determinant.
String stringify(const T_ &item)
Converts an item into a String.
String stringify_fp_sci(DataType_ value, int precision=0, int width=0, bool sign=false)
Prints a floating point value to a string in scientific notation.
@ value
specifies whether the space should supply basis function values
std::uint64_t Index
Index data type.