11#include <kernel/lafem/forward.hpp>
12#include <kernel/lafem/container.hpp>
13#include <kernel/lafem/dense_vector.hpp>
14#include <kernel/lafem/sparse_layout.hpp>
15#include <kernel/lafem/arch/scale_row_col.hpp>
16#include <kernel/lafem/arch/scale.hpp>
17#include <kernel/lafem/arch/axpy.hpp>
18#include <kernel/lafem/arch/apply.hpp>
19#include <kernel/lafem/arch/norm.hpp>
20#include <kernel/lafem/arch/diagonal.hpp>
21#include <kernel/lafem/arch/lumping.hpp>
22#include <kernel/lafem/arch/row_norm.hpp>
23#include <kernel/adjacency/graph.hpp>
24#include <kernel/adjacency/permutation.hpp>
25#include <kernel/util/statistics.hpp>
26#include <kernel/util/time_stamp.hpp>
27#include <kernel/lafem/vector_mirror.hpp>
60 template <
typename DT_,
typename IT_ = Index>
79 Index & _used_elements()
105 template <
typename DT2_ = DT_,
typename IT2_ = IT_>
109 template <
typename DataType2_,
typename IndexType2_>
112 static constexpr bool is_global =
false;
113 static constexpr bool is_local =
true;
141 Container<DT_, IT_> (rows_in * columns_in)
162 Container<DT_, IT_> (rows_in * columns_in)
171 this->
_indices.push_back(MemoryPool::template allocate_memory<IT_>(_used_elements()));
174 this->
_indices.push_back(MemoryPool::template allocate_memory<IT_>(_used_rows() + 1));
177 this->
_indices.push_back(MemoryPool::template allocate_memory<IT_>(_used_rows()));
180 this->
_elements.push_back(MemoryPool::template allocate_memory<DT_>(_used_elements()));
194 this->
_indices.assign(layout_in._indices.begin(), layout_in._indices.end());
195 this->
_indices_size.assign(layout_in._indices_size.begin(), layout_in._indices_size.end());
196 this->
_scalar_index.assign(layout_in._scalar_index.begin(), layout_in._scalar_index.end());
201 this->
_elements.push_back(MemoryPool::template allocate_memory<DT_>(_used_elements()));
212 template <
typename MT_>
263 Container<DT_, IT_>(rows_in * columns_in)
305 const IT_ * row_main(
nullptr);
306 const IT_ * indices(
nullptr);
307 IT_ * trow_main(
nullptr);
308 IT_ * tindices(
nullptr);
310 indices = non_zero_rows.
indices();
312 Index tused_elements(0);
315 const Index row(indices[i]);
316 tused_elements += row_main[row+1] - row_main[row];
324 this->
_indices.push_back(MemoryPool::template allocate_memory<IT_>(_used_elements()));
327 this->
_indices.push_back(MemoryPool::template allocate_memory<IT_>(_used_rows() + 1));
330 this->
_indices.push_back(MemoryPool::template allocate_memory<IT_>(_used_rows()));
333 this->
_elements.push_back(MemoryPool::template allocate_memory<DT_>(_used_elements()));
341 const IT_ row(indices[i]);
342 const IT_ start(row_main[row]);
343 const IT_ end(row_main[row+1]);
344 const IT_ row_size(end - start);
368 Index num_rows = graph.get_num_nodes_domain();
369 Index num_cols = graph.get_num_nodes_image();
384 for(
Index i(0); i < num_rows; ++i)
386 num_nzrs +=
Index(dom_ptr[i] < dom_ptr[i+1] ? 1 : 0);
399 for(
Index i(0), j(0); i < num_rows; ++i)
401 if(dom_ptr[i] < dom_ptr[i + 1])
409 for(
Index k(0); k < num_nzes; ++k)
422 template <
typename DT2_ = DT_,
typename IT2_ = IT_>
426 deserialize<DT2_, IT2_>(input);
450 this->
move(std::forward<SparseMatrixCSCR>(other));
466 t.
clone(*
this, clone_mode);
478 template<
typename DT2_,
typename IT2_>
491 template <
typename DT2_,
typename IT2_>
504 template <
typename MT_>
510 const Index arows(ta.template rows<Perspective::pod>());
511 const Index acolumns(ta.template columns<Perspective::pod>());
512 const Index aused_elements(ta.template used_elements<Perspective::pod>());
515 for (
Index i(0); i < arows; ++i)
517 aused_rows += ta.get_length_of_line(i) > 0;
526 IT_ * pcol_ind(tcol_ind.
elements());
527 IT_ * prow_ptr(trow_ptr.
elements());
528 IT_ * prow_numbers(trow_numbers.
elements());
532 prow_ptr[0] = IT_(0);
533 for (
Index i(0); i < arows; ++i)
535 Index length = ta.get_length_of_line(i);
538 prow_ptr[row_index + 1] = IT_(offset + length);
539 prow_numbers[row_index] = IT_(i);
546 for (
Index i(0); i < arows; ++i)
548 ta.set_line(i, pval + prow_ptr[i], pcol_ind + prow_ptr[i], 0);
574 this->
_indices.assign(layout_in._indices.begin(), layout_in._indices.end());
575 this->
_indices_size.assign(layout_in._indices_size.begin(), layout_in._indices_size.end());
576 this->
_scalar_index.assign(layout_in._scalar_index.begin(), layout_in._scalar_index.end());
581 this->
_elements.push_back(MemoryPool::template allocate_memory<DT_>(_used_elements()));
594 template <
typename DT2_ = DT_,
typename IT2_ = IT_>
610 template <
typename DT2_ = DT_,
typename IT2_ = IT_>
624 std::ios_base::openmode bin = std::ifstream::in | std::ifstream::binary;
626 bin = std::ifstream::in;
627 std::ifstream file(filename.c_str(), bin);
628 if (! file.is_open())
629 XABORTM(
"Unable to open Matrix file " + filename);
649 XABORTM(
"Filemode not supported!");
661 std::ios_base::openmode bin = std::ofstream::out | std::ofstream::binary;
663 bin = std::ofstream::out;
665 char* buff =
nullptr;
671 file.open(filename.c_str(), bin);
673 XABORTM(
"Unable to open Matrix file " + filename);
694 XABORTM(
"Filemode not supported!");
746 template <Perspective = Perspective::native>
757 template <Perspective = Perspective::native>
768 template <Perspective = Perspective::native>
779 template <Perspective = Perspective::native>
819 DT_
const *
val()
const
876 return "SparseMatrixCSCR";
887 this->_copy_content(x, full);
903 const DT_ alpha = DT_(1))
905 XASSERTM(x.
rows() == this->rows(),
"Matrix rows do not match!");
906 XASSERTM(x.
columns() == this->columns(),
"Matrix columns do not match!");
912 Arch::Axpy::value(this->
val(), alpha, x.
val(), this->used_elements());
915 Statistics::add_time_axpy(ts_stop.
elapsed(ts_start));
926 XASSERTM(x.
rows() == this->rows(),
"Row count does not match!");
927 XASSERTM(x.
columns() == this->columns(),
"Column count does not match!");
933 Arch::Scale::value(this->
val(), x.
val(), alpha, this->used_elements());
936 Statistics::add_time_axpy(ts_stop.
elapsed(ts_start));
952 Statistics::add_time_reduction(ts_stop.
elapsed(ts_start));
971 Statistics::add_time_reduction(ts_stop.
elapsed(ts_start));
986 if(this->
size() == 0 && x.
size() == 0 && this->get_elements().size() == 0 && this->get_indices().size() == 0 && x.
get_elements().size() == 0 && x.
get_indices().size() == 0)
1004 col_ind_a =
const_cast<IT_*
>(this->
col_ind());
1005 row_ptr_a =
const_cast<IT_*
>(this->
row_ptr());
1006 col_ind_b =
const_cast<IT_*
>(x.
col_ind());
1007 row_ptr_b =
const_cast<IT_*
>(x.
row_ptr());
1010 if (col_ind_a[i] != col_ind_b[i])
1020 if (row_ptr_a[i] != row_ptr_b[i])
1039 XASSERTM(r.
size() == this->rows(),
"Vector size of r does not match!");
1040 XASSERTM(x.
size() == this->columns(),
"Vector size of x does not match!");
1050 XASSERTM(r.template elements<Perspective::pod>() != x.template elements<Perspective::pod>(),
"Vector x and r must not share the same memory!");
1054 this->val(), this->col_ind(), this->row_ptr(), this->row_numbers(), this->used_rows(), this->rows(), this->columns(), this->used_elements(),
false);
1057 Statistics::add_time_blas2(ts_stop.
elapsed(ts_start));
1068 XASSERTM(r.
size() == this->columns(),
"Vector size of r does not match!");
1069 XASSERTM(x.
size() == this->rows(),
"Vector size of x does not match!");
1079 XASSERTM(r.template elements<Perspective::pod>() != x.template elements<Perspective::pod>(),
"Vector x and r must not share the same memory!");
1083 this->val(), this->col_ind(), this->row_ptr(), this->row_numbers(), this->used_rows(), this->rows(), this->columns(), this->used_elements(),
true);
1086 Statistics::add_time_blas2(ts_stop.
elapsed(ts_start));
1101 const DT_ alpha = DT_(1))
const
1103 XASSERTM(r.
size() == this->rows(),
"Vector size of r does not match!");
1104 XASSERTM(x.
size() == this->columns(),
"Vector size of x does not match!");
1105 XASSERTM(y.
size() == this->rows(),
"Vector size of y does not match!");
1116 XASSERTM(r.template elements<Perspective::pod>() != x.template elements<Perspective::pod>(),
"Vector x and r must not share the same memory!");
1120 this->val(), this->col_ind(), this->row_ptr(), this->row_numbers(), this->used_rows(), this->rows(), this->columns(), this->used_elements(),
false);
1123 Statistics::add_time_blas2(ts_stop.
elapsed(ts_start));
1138 const DT_ alpha = DT_(1))
const
1140 XASSERTM(r.
size() == this->columns(),
"Vector size of r does not match!");
1141 XASSERTM(x.
size() == this->rows(),
"Vector size of x does not match!");
1142 XASSERTM(y.
size() == this->columns(),
"Vector size of y does not match!");
1153 XASSERTM(r.template elements<Perspective::pod>() != x.template elements<Perspective::pod>(),
"Vector x and r must not share the same memory!");
1157 this->val(), this->col_ind(), this->row_ptr(), this->row_numbers(), this->used_rows(), this->rows(), this->columns(), this->used_elements(),
true);
1160 Statistics::add_time_blas2(ts_stop.
elapsed(ts_start));
1179 const IT_ * prow_ptr(this->
row_ptr());
1180 return Index(prow_ptr[row + 1] - prow_ptr[row]);
1186 void set_line(
const Index row, DT_ *
const pval_set, IT_ *
const pcol_set,
1187 const Index col_start,
const Index stride = 1)
const
1189 const IT_ * prow_ptr(this->
row_ptr());
1190 const IT_ * pcol_ind(this->
col_ind());
1191 const DT_ * pval(this->
val());
1194 while (row_idx < this->
used_rows() && row_idx < row)
1200 const Index start((
Index(prow_ptr[row_idx])));
1201 const Index end((
Index(prow_ptr[row_idx + 1] - prow_ptr[row_idx])));
1202 for (
Index i(0); i < end; ++i)
1204 pval_set[i * stride] = pval[start + i];
1205 pcol_set[i * stride] = pcol_ind[start + i] + IT_(col_start);
1237 extern template class SparseMatrixCSCR<float, std::uint32_t>;
1238 extern template class SparseMatrixCSCR<double, std::uint32_t>;
1239 extern template class SparseMatrixCSCR<float, std::uint64_t>;
1240 extern template class SparseMatrixCSCR<double, std::uint64_t>;
#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.
Adjacency Graph implementation.
Index * get_domain_ptr()
Returns the domain pointer array.
Index * get_image_idx()
Returns the image node index array.
Index get_num_indices() const
Returns the total number indices.
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.
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.
std::vector< IT_ * > _indices
List of pointers to all IT_ dependent arrays.
void clone(const Container &other, CloneMode clone_mode=CloneMode::Weak)
Clone operation.
void move(Container &&other)
Assignment move operation.
std::vector< Index > _indices_size
List of corresponding IT_ array sizes.
void format(DT_ value=DT_(0))
Reset all elements of the container to a given value or zero if missing.
std::vector< Index > _scalar_index
List of scalars with datatype index.
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.
Layout scheme for sparse matrix containers.
CSCR based sparse matrix.
IT_ * col_ind()
Retrieve column indices array.
bool same_layout(const SparseMatrixCSCR &x) const
Checks if the structural layout of this matrix matches that of another matrix. This excludes comparis...
IT_ * row_numbers()
Retrieve row numbers of non zero rows.
SparseMatrixCSCR(FileMode mode, const String &filename)
Constructor.
void convert(const MT_ &a)
Conversion method.
Index columns() const
Retrieve matrix column count.
SparseMatrixCSCR(const SparseLayout< IT_, layout_id > &layout_in)
Constructor.
Index rows() const
Retrieve matrix row count.
Index get_length_of_line(const Index row) const
Returns the number of NNZ-elements of the selected row.
void clone(const SparseMatrixCSCR< DT2_, IT2_ > &other, CloneMode clone_mode=CloneMode::Weak)
Clone operation.
DT_ operator()(Index row, Index col) const
Retrieve specific matrix element.
SparseMatrixCSCR(Index rows_in, Index columns_in, Index used_elements_in, Index used_rows_in)
Constructor.
SparseMatrixCSCR(const Index rows_in, const Index columns_in, DenseVector< IT_, IT_ > &col_ind_in, DenseVector< DT_, IT_ > &val_in, DenseVector< IT_, IT_ > &row_ptr_in, DenseVector< IT_, IT_ > &row_numbers_in)
Constructor.
IT_ IndexType
Our indextype.
VectorTypeR create_vector_r() const
Returns a new compatible R-Vector.
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 apply(DenseVector< DT_, IT_ > &r, const DenseVector< DT_, IT_ > &x, const DenseVector< DT_, IT_ > &y, const DT_ alpha=DT_(1)) const
Calculate .
SparseLayout< IT_, layout_id > layout() const
Retrieve convenient sparse matrix layout object.
DT_ DataType
Our datatype.
SparseMatrixCSCR(Index rows_in, Index columns_in)
Constructor.
void axpy(const SparseMatrixCSCR &x, const DT_ alpha=DT_(1))
Calculate .
Index used_elements() const
Retrieve non zero element count.
void write_out(FileMode mode, const String &filename) const
Write out matrix to file.
DT_ * val()
Retrieve non zero element array.
DenseVector< DT_, IT_ > VectorTypeL
Compatible L-vector type.
void copy(const SparseMatrixCSCR &x, bool full=false)
Performs .
SparseMatrixCSCR()
Constructor.
SparseMatrixCSCR & operator=(SparseMatrixCSCR &&other)
Move operator.
void apply_transposed(DenseVector< DT_, IT_ > &r, const DenseVector< DT_, IT_ > &x) const
Calculate .
static String name()
Returns a descriptive string.
friend std::ostream & operator<<(std::ostream &lhs, const SparseMatrixCSCR &b)
SparseMatrixCSCR streaming operator.
void write_out(FileMode mode, std::ostream &file) const
Write out matrix to file.
VectorTypeL create_vector_l() const
Returns a new compatible L-Vector.
Index used_rows() const
Retrieve used matrix non zero row count.
void read_from(FileMode mode, const String &filename)
Read in matrix from file.
SparseMatrixCSCR(SparseMatrixCSCR &&other)
Move Constructor.
SparseMatrixCSCR(const Adjacency::Graph &graph)
Constructor.
DenseVector< DT_, IT_ > VectorTypeR
Compatible R-vector type.
void convert(const SparseMatrixCSCR< DT2_, IT2_ > &other)
Conversion method.
IT_ * row_ptr()
Retrieve row start index array.
DT_ norm_frobenius() const
Calculates the Frobenius norm of this matrix.
SparseMatrixCSCR(FileMode mode, std::istream &file)
Constructor.
void scale(const SparseMatrixCSCR &x, const DT_ alpha)
Calculate .
void apply(DenseVector< DT_, IT_ > &r, const DenseVector< DT_, IT_ > &x) const
Calculate .
const IT_ * ImageIterator
ImageIterator typedef for Adjactor interface implementation.
SparseMatrixCSCR(std::vector< char > input)
Constructor.
std::vector< char > serialize(const LAFEM::SerialConfig &config=SerialConfig()) const
Serialization of complete container entity.
void read_from(FileMode mode, std::istream &file)
Read in matrix from stream.
static constexpr SparseLayoutId layout_id
Our used layout type.
SparseMatrixCSCR clone(CloneMode clone_mode=CloneMode::Weak) const
Clone operation.
SparseMatrixCSCR(const SparseMatrixCSR< DT_, IT_ > &csr, const VectorMirror< DT_, IT_ > &non_zero_rows)
Constructor.
DT_ ValueType
Value type, meaning the type of each 'block'.
void deserialize(std::vector< char > input)
Deserialization of complete container entity.
DT_ max_rel_diff(const SparseMatrixCSCR &x) const
Retrieve the maximum relative difference of this matrix and another one y.max_rel_diff(x) returns .
SparseMatrixCSCR(const MT_ &other)
Constructor.
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.
IT_ * row_ptr()
Retrieve row start index array.
Handles vector prolongation, restriction and serialization.
IT_ * indices()
Get a pointer to the non zero indices array.
Index num_indices() const
Returns the number of indices in the mirror.
static void copy(DT_ *dest, const DT_ *src, const Index count)
Copy memory area from src to dest.
static void set_memory(DT_ *address, const DT_ val, const Index count=1)
set memory to specific value
static void release_memory(void *address)
release memory or decrease reference counter
static void increase_memory(void *address)
increase memory counter
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.
String stringify(const T_ &item)
Converts an item into a String.
std::uint64_t Index
Index data type.