11#include <kernel/util/math.hpp>
12#include <kernel/lafem/forward.hpp>
13#include <kernel/lafem/container.hpp>
14#include <kernel/lafem/dense_vector.hpp>
15#include <kernel/lafem/arch/axpy_dense.hpp>
16#include <kernel/lafem/arch/matmatmult_dense_dense.hpp>
17#include <kernel/lafem/arch/matmatmult_sparse_dense.hpp>
18#include <kernel/lafem/arch/matvecmult_dense_dense.hpp>
19#include <kernel/lafem/arch/norm2_dense.hpp>
20#include <kernel/lafem/arch/scale_dense.hpp>
21#include <kernel/lafem/arch/transpose_dense.hpp>
44 template <
typename DT_,
typename IT_ = Index>
57 template <
typename DT2_ = DT_,
typename IT2_ = IT_>
120 template <
typename DT2_ = DT_,
typename IT2_ = IT_>
123 this->deserialize<DT2_, IT2_>(input);
170 this->
move(std::forward<DenseMatrix>(other));
186 t.
clone(*
this, clone_mode);
198 template<
typename DT2_,
typename IT2_>
211 template <
typename DT2_,
typename IT2_>
224 template <
typename DT2_ = DT_,
typename IT2_ = IT_>
242 template <
typename DT2_ = DT_,
typename IT2_ = IT_>
256 std::ios_base::openmode bin = std::ifstream::in | std::ifstream::binary;
258 bin = std::ifstream::in;
259 std::ifstream file(filename.c_str(), bin);
261 XABORTM(
"Unable to open Matrix file " + filename);
282 std::getline(file, line);
284 const bool array_format((line.find(
"%%MatrixMarket matrix array real general") != String::npos) ?
true :
false);
285 if (array_format ==
false)
287 XABORTM(
"Input-file is not a compatible array real mtx-file");
292 std::getline(file,line);
294 XABORTM(
"Input-file is empty");
296 String::size_type begin(line.find_first_not_of(
" "));
297 if (line.at(begin) !=
'%')
302 String::size_type begin(line.find_first_not_of(
" "));
303 line.erase(0, begin);
304 String::size_type end(line.find_first_of(
" "));
305 String srow(line, 0, end);
306 trows =
Index(atol(srow.c_str()));
309 begin = line.find_first_not_of(
" ");
310 line.erase(0, begin);
311 end = line.find_first_of(
" ");
312 String scol(line, 0, end);
313 tcols =
Index(atol(scol.c_str()));
324 std::getline(file, line);
328 String::size_type begin = line.find_first_not_of(
" ");
329 line.erase(0, begin);
330 String::size_type end = line.find_first_of(
" ");
331 String sval(line, 0, end);
332 DT_ tval((DT_)atof(sval.c_str()));
338 XASSERTM(i == trows * tcols,
"Dense MTX file did not contain enough entries!");
341 this->
move(std::move(tmp));
350 this->
template _deserialize<double, std::uint64_t>(
FileMode::fm_dm, file);
354 XABORTM(
"Filemode not supported!");
366 std::ios_base::openmode bin = std::ofstream::out | std::ofstream::binary;
368 bin = std::ofstream::out;
370 char* buff =
nullptr;
376 file.open(filename.c_str(), bin);
378 XABORTM(
"Unable to open Matrix file " + filename);
396 file <<
"%%MatrixMarket matrix array real general\n";
402 for (
Index i(0); i < tsize; ++i)
404 file << elem_view(i) <<
"\n";
413 this->
template _serialize<double, std::uint64_t>(
FileMode::fm_dm, file);
417 XABORTM(
"Filemode not supported!");
481 return Memory::TypedView<DT_>();
488 return Memory::TypedView<DT_>();
489 return Memory::TypedView<DT_>(this->
_elements.at(0).view(loc, acc));
499 return "DenseMatrix";
510 this->_copy_content(x, full);
548 Statistics::add_time_axpy(ts_stop.
elapsed(ts_start));
567 Statistics::add_time_reduction(ts_stop.
elapsed(ts_start));
581 XASSERTM(x.
num_rows() == this->num_rows(),
"Matrix row count does not match!");
582 XASSERTM(x.
num_cols() == this->num_cols(),
"Matrix column count does not match!");
594 Statistics::add_time_axpy(ts_stop.
elapsed(ts_start));
605 XASSERTM(r.
size() == this->num_rows(),
"Vector size of r does not match!");
606 XASSERTM(x.
size() == this->num_cols(),
"Vector size of x does not match!");
621 Statistics::add_time_blas2(ts_stop.
elapsed(ts_start));
632 XASSERTM(r.
size() == this->num_cols(),
"Vector size of r does not match!");
633 XASSERTM(x.
size() == this->num_rows(),
"Vector size of x does not match!");
648 Statistics::add_time_blas2(ts_stop.
elapsed(ts_start));
661 XASSERTM(r.
size() == this->num_rows(),
"Vector size of r does not match!");
662 XASSERTM(x.
size() == this->num_cols(),
"Vector size of x does not match!");
663 XASSERTM(y.
size() == this->num_rows(),
"Vector size of y does not match!");
673 this->elements_arbiter(), x.
elements_arbiter(), this->num_rows(), this->num_cols(),
false);
678 Statistics::add_time_blas2(ts_stop.
elapsed(ts_start));
693 const DT_ alpha = DT_(1))
const
695 XASSERTM(r.
size() == this->num_cols(),
"Vector size of r does not match!");
696 XASSERTM(x.
size() == this->num_rows(),
"Vector size of x does not match!");
697 XASSERTM(y.
size() == this->num_cols(),
"Vector size of y does not match!");
707 this->elements_arbiter(), x.
elements_arbiter(), this->num_rows(), this->num_cols(),
true);
712 Statistics::add_time_blas2(ts_stop.
elapsed(ts_start));
723 XASSERTM(x.
num_rows() == this->num_rows(),
"Matrix row count does not match!");
724 XASSERTM(x.
num_cols() == this->num_cols(),
"Matrix column count does not match!");
732 Statistics::add_time_reduction(ts_stop.
elapsed(ts_start));
774 Statistics::add_time_blas3(ts_stop.
elapsed(ts_start));
795 this->num_rows(), this->num_cols(), x.
num_cols());
799 Statistics::add_time_blas3(ts_stop.
elapsed(ts_start));
812 const DT_ alpha = DT_(1))
829 Statistics::add_time_blas3(ts_stop.
elapsed(ts_start));
843 const DT_ alpha = DT_(1))
856 this->num_rows(), this->num_cols(), x.
num_cols());
861 Statistics::add_time_blas3(ts_stop.
elapsed(ts_start));
871 std::vector<IT_> temp(this->
num_rows());
881 Statistics::add_time_blas3(ts_stop.
elapsed(ts_start));
901 Arch::TransposeDense::template exec<DT_>(r.
elements_arbiter(), this->elements_arbiter(), this->num_rows(), this->num_cols());
920 for (
Index i(0) ; i < m ; ++i)
923 for (
Index j(0) ; j < n ; ++j)
925 lhs <<
" " << val(i*n + j);
#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.
void format()
Reset all elements of the container to a given value or zero if missing.
void move(Container &&source)
Assignment move operation.
void clone(const Container &source, CloneMode clone_mode=CloneMode::Weak)
Clone operation.
std::vector< Index > _elements_size
List of corresponding datatype array sizes.
void assign(const Container< DT2_, IT2_ > &other)
Assignment operation.
virtual void clear()
Free all allocated arrays.
std::vector< Memory::Arbiter > _elements
List of pointers to all datatype dependent arrays.
std::vector< Index > _scalar_index
List of scalars with datatype index.
Dense data matrix class template.
DenseMatrix(DenseMatrix &&other)
Move Constructor.
void read_from(FileMode mode, const String &filename)
Read in matrix from file.
void set_product(DenseMatrix &x, DenseMatrix &y)
Calculate .
DenseMatrix(Index rows_in, Index columns_in, DT_ value)
Constructor.
DenseMatrix(std::vector< char > input)
Constructor.
Index num_cols() const
Retrieve matrix column count.
DenseMatrix()=default
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...
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.
Index size() const
Retrieve matrix element count.
void add_product(const DenseMatrix &x, const DenseMatrix &y, const DT_ alpha=DT_(1))
Calculate .
Memory::Arbiter & elements_arbiter()
Returns a reference to the element array arbiter.
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.
Index num_rows() const
Retrieve matrix row count.
void axpy(const DenseMatrix &x, const DT_ alpha=DT_(1))
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 num_nzes() const
Retrieve matrix non-zero element count.
void write_out(FileMode mode, const String &filename) const
Write out matrix to file.
bool empty() const
Checks whether the vector is empty, i.e. if it has size 0.
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 add_product(const SparseMatrixCSR< DT_, IT_ > &x, const DenseMatrix &y, const DT_ alpha=DT_(1))
Calculate .
void apply_transposed(DenseVector< DT_, IT_ > &r, const DenseVector< DT_, IT_ > &x, const DenseVector< DT_, IT_ > &y, const DT_ alpha=DT_(1)) const
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 .
const Memory::Arbiter & elements_arbiter() const
Returns a reference to the element array arbiter.
void set_product(SparseMatrixCSR< DT_, IT_ > &x, DenseMatrix &y)
Calculate .
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.
Memory::Arbiter & elements_arbiter()
Returns a reference to the element array arbiter.
Index size() const
Returns the size of this vector, i.e. its number of entries.
Config class for serialize parameter.
Memory::Arbiter & val_arbiter()
Returns a reference to the values array arbiter.
Index num_cols() const
Retrieve matrix column count.
Index num_rows() const
Retrieve matrix row count.
Memory::Arbiter & col_idx_arbiter()
Returns a reference to the column-index array arbiter.
Index num_nzes() const
Retrieve non zero element count.
Memory::Arbiter & row_ptr_arbiter()
Returns a reference to the row-pointer array arbiter.
T_ * get_w()
Returns a typed pointer to the array that grants write (and read) access.
void release() const
Releases this view.
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 file_out_stream_buffer_size
OutStreamBufferSize.
DT_ invert_matrix(const IT_ n, const IT_ stride, DT_ a[], IT_ p[])
Inverts a matrix and returns its determinant.
Access
Memory access enumeration.
@ read_write
read-write access
@ write
write-only access (see attention note above)
Location
Memory location enumeration.
@ value
specifies whether the space should supply basis function values
std::uint64_t Index
Index data type.