11#include <kernel/util/memory_arbiter.hpp>
12#include <kernel/util/statistics.hpp>
13#include <kernel/util/time_stamp.hpp>
14#include <kernel/adjacency/graph.hpp>
15#include <kernel/adjacency/permutation.hpp>
16#include <kernel/lafem/forward.hpp>
17#include <kernel/lafem/container.hpp>
18#include <kernel/lafem/dense_vector.hpp>
19#include <kernel/lafem/sparse_layout.hpp>
20#include <kernel/lafem/vector_mirror.hpp>
21#include <kernel/lafem/arch/axpy_dense.hpp>
22#include <kernel/lafem/arch/max_rel_diff_dense.hpp>
23#include <kernel/lafem/arch/norm2_dense.hpp>
24#include <kernel/lafem/arch/matvecmult_cscr_dense.hpp>
25#include <kernel/lafem/arch/scale_dense.hpp>
57 template <
typename DT_,
typename IT_ = Index>
74 template <
typename DT2_ = DT_,
typename IT2_ = IT_>
78 template <
typename DataType2_,
typename IndexType2_>
81 static constexpr bool is_global =
false;
82 static constexpr bool is_local =
true;
95 Index & _nonzero_rows()
119 this->
_indices.push_back(Memory::Arbiter(
sizeof(IT_) * (nz_rows + 1)));
122 this->
_indices.push_back(Memory::Arbiter(
sizeof(IT_) * nz_rows));
125 this->
_indices.push_back(Memory::Arbiter(
sizeof(IT_) * nzes));
128 this->
_elements.push_back(Memory::Arbiter(
sizeof(DT_) * nzes));
180 this->_alloc(rows, cols, nz_rows, nzes);
192 this->
_indices_size.assign(layout_in._indices_size.begin(), layout_in._indices_size.end());
193 this->
_scalar_index.assign(layout_in._scalar_index.begin(), layout_in._scalar_index.end());
196 this->
_indices.push_back(idx.attach());
209 template <
typename MT_>
300 for (
Index i(0) ; i < nz_rows ; ++i)
301 nnzes += row_ptr_in[mir_idx[i]+1] - row_ptr_in[mir_idx[i]];
311 for(
Index i(0); i < nz_rows; ++i)
313 IT_ row = mir_idx[i];
316 for(IT_ k(row_ptr_in[row]); k < row_ptr_in[row+1]; ++k, ++j)
319 col_idx[j] = col_idx_in[k];
324 XASSERT(row_ptr[nz_rows] == nnzes);
355 num_nzrs +=
Index(dom_ptr[i] < dom_ptr[i+1] ? 1 : 0);
370 if(dom_ptr[i] < dom_ptr[i + 1])
389 template <
typename DT2_ = DT_,
typename IT2_ = IT_>
392 this->deserialize<DT2_, IT2_>(input);
416 this->
move(std::forward<SparseMatrixCSCR>(other));
432 t.
clone(*
this, clone_mode);
444 template<
typename DT2_,
typename IT2_>
457 template <
typename DT2_,
typename IT2_>
470 template <
typename MT_>
473 XASSERT((
void*)
this != (
void*)&a);
477 const Index num_rows_a = a.num_rows_raw();
478 const Index num_cols_a = a.num_cols_raw();
479 const Index num_nzes_a = a.num_nzes_raw();
480 if(num_nzes_a <=
Index(0))
482 this->_alloc(num_rows_a, num_cols_a,
Index(0),
Index(0));
486 Index num_nz_rows_a(0);
487 for (
Index i(0); i < num_rows_a; ++i)
489 num_nz_rows_a +=
Index(a.row_degree(i) >
Index(0) ? 1 : 0);
492 this->_alloc(num_rows_a, num_cols_a, num_nz_rows_a, num_nzes_a);
499 row_ptr[0] =
Index(0);
500 for(
Index row(0), k(0); row < num_rows_a; ++row)
502 Index nz = a.row_degree(row);
505 a.get_row_col_indices(row, &col_idx[row_ptr[k]], IT_(0));
506 a.get_row_values(row, &val[row_ptr[k]]);
507 row_idx[k] = IT_(row);
508 row_ptr[k+1] = row_ptr[k] + IT_(nz);
525 this->
_indices_size.assign(layout_in._indices_size.begin(), layout_in._indices_size.end());
526 this->
_scalar_index.assign(layout_in._scalar_index.begin(), layout_in._scalar_index.end());
529 this->
_indices.push_back(idx.attach());
544 template <
typename DT2_ = DT_,
typename IT2_ = IT_>
560 template <
typename DT2_ = DT_,
typename IT2_ = IT_>
574 std::ios_base::openmode bin = std::ifstream::in | std::ifstream::binary;
576 bin = std::ifstream::in;
577 std::ifstream file(filename.c_str(), bin);
578 if (! file.is_open())
579 XABORTM(
"Unable to open Matrix file " + filename);
602 XABORTM(
"Filemode not supported!");
614 std::ios_base::openmode bin = std::ofstream::out | std::ofstream::binary;
616 bin = std::ofstream::out;
618 char* buff =
nullptr;
624 file.open(filename.c_str(), bin);
626 XABORTM(
"Unable to open Matrix file " + filename);
650 XABORTM(
"Filemode not supported!");
799 return Memory::TypedView<DT_>();
806 return Memory::TypedView<DT_>();
807 return Memory::TypedView<DT_>(this->
_elements.at(0).view(loc, acc));
813 return Memory::TypedView<IT_>();
820 return Memory::TypedView<IT_>();
827 return Memory::TypedView<IT_>();
834 return Memory::TypedView<IT_>();
835 return Memory::TypedView<IT_>(this->
_indices.at(0).view(loc, acc));
841 return Memory::TypedView<IT_>();
848 return Memory::TypedView<IT_>();
855 return Memory::TypedView<IT_>();
862 return Memory::TypedView<IT_>();
863 return Memory::TypedView<IT_>(this->
_indices.at(1).view(loc, acc));
869 return Memory::TypedView<IT_>();
876 return Memory::TypedView<IT_>();
883 return Memory::TypedView<IT_>();
890 return Memory::TypedView<IT_>();
891 return Memory::TypedView<IT_>(this->
_indices.at(2).view(loc, acc));
901 return "SparseMatrixCSCR";
912 this->_copy_content(x, full);
930 XASSERTM(x.
num_nzes() == this->num_nzes(),
"Matrix nonzeros do not match!");
942 Statistics::add_time_axpy(ts_stop.
elapsed(ts_start));
955 XASSERTM(x.
num_nzes() == this->num_nzes(),
"Nonzero count does not match!");
967 Statistics::add_time_axpy(ts_stop.
elapsed(ts_start));
986 Statistics::add_time_reduction(ts_stop.
elapsed(ts_start));
999 XASSERTM(x.
num_nzes() == this->num_nzes(),
"Nonzero count does not match!");
1004 DT_ result = Arch::MaxRelDiffDense::template exec<DT_>(this->
val_arbiter(), x.
val_arbiter(), this->num_nzes());
1007 Statistics::add_time_reduction(ts_stop.
elapsed(ts_start));
1048 for (
Index i(0) ; i < nzr; ++i)
1050 if(row_ptr_a[i] != row_ptr_b[i])
1052 if(row_idx_a[i] != row_idx_b[i])
1055 if(row_ptr_a[nzr] != row_ptr_b[nzr])
1059 for (
Index i(0) ; i < nze ; ++i)
1061 if (col_idx_a[i] != col_idx_b[i])
1076 XASSERTM(r.
size() == this->num_rows(),
"Vector size of r does not match!");
1077 XASSERTM(x.
size() == this->num_cols(),
"Vector size of x does not match!");
1092 this->val_arbiter(), this->col_idx_arbiter(), this->row_ptr_arbiter(), this->row_idx_arbiter(),
1093 this->num_rows(), this->num_cols(), this->num_nonzero_rows(), this->num_nzes(),
false);
1098 Statistics::add_time_blas2(ts_stop.
elapsed(ts_start));
1109 XASSERTM(r.
size() == this->num_cols(),
"Vector size of r does not match!");
1110 XASSERTM(x.
size() == this->num_rows(),
"Vector size of x does not match!");
1125 this->val_arbiter(), this->col_idx_arbiter(), this->row_ptr_arbiter(), this->row_idx_arbiter(),
1126 this->num_rows(), this->num_cols(), this->num_nonzero_rows(), this->num_nzes(),
true);
1131 Statistics::add_time_blas2(ts_stop.
elapsed(ts_start));
1146 const DT_ alpha = DT_(1))
const
1148 XASSERTM(r.
size() == this->num_rows(),
"Vector size of r does not match!");
1149 XASSERTM(x.
size() == this->num_cols(),
"Vector size of x does not match!");
1150 XASSERTM(y.
size() == this->num_rows(),
"Vector size of y does not match!");
1156 else if(this->
hollow() || (alpha == DT_(0)))
1167 this->val_arbiter(), this->col_idx_arbiter(), this->row_ptr_arbiter(), this->row_idx_arbiter(),
1168 this->num_rows(), this->num_cols(), this->num_nonzero_rows(), this->num_nzes(),
false);
1173 Statistics::add_time_blas2(ts_stop.
elapsed(ts_start));
1188 const DT_ alpha = DT_(1))
const
1190 XASSERTM(r.
size() == this->num_cols(),
"Vector size of r does not match!");
1191 XASSERTM(x.
size() == this->num_rows(),
"Vector size of x does not match!");
1192 XASSERTM(y.
size() == this->num_cols(),
"Vector size of y does not match!");
1198 else if(this->
hollow() || (alpha == DT_(0)))
1209 this->val_arbiter(), this->col_idx_arbiter(), this->row_ptr_arbiter(), this->row_idx_arbiter(),
1210 this->num_rows(), this->num_cols(), this->num_nonzero_rows(), this->num_nzes(),
true);
1215 Statistics::add_time_blas2(ts_stop.
elapsed(ts_start));
1241 void print(std::ostream & os,
bool print_dense)
const
1259 for (
Index i(0) ; i < nzrows ; ++i)
1264 for(; l < row_idx[i]; ++l)
1267 for(
Index j = 0; j < ncols; ++j)
1268 os <<
" " << DT_(0);
1275 os << row_idx[i] <<
':';
1280 for(IT_ j = row_ptr[i]; j < row_ptr[i+1]; ++j)
1285 for(; k < col_idx[j]; ++k)
1286 os <<
" " << DT_(0);
1290 os <<
" " << col_idx[j] <<
':';
1294 os <<
" " << val[j];
1301 for(; k < ncols; ++k)
1302 os <<
" " << DT_(0);
1311 for(; l < nrows; ++l)
1314 for(
Index j = 0; j < ncols; ++j)
1315 os <<
" " << DT_(0);
1337 extern template class SparseMatrixCSCR<float, std::uint32_t>;
1338 extern template class SparseMatrixCSCR<double, std::uint32_t>;
1339 extern template class SparseMatrixCSCR<float, std::uint64_t>;
1340 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.
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.
std::vector< Memory::Arbiter > _indices
List of pointers to all IT_ dependent arrays.
virtual void clear()
Free all allocated arrays.
std::vector< Index > _indices_size
List of corresponding IT_ array sizes.
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 vector class template.
Memory::Arbiter & elements_arbiter()
Returns a reference to the element array arbiter.
void copy(const VT_ &a)
Performs .
Index size() const
Returns the size of this vector, i.e. its number of entries.
Config class for serialize parameter.
Layout scheme for sparse matrix containers.
const std::vector< Memory::Arbiter > & get_indices() const
Returns a list of all Index arrays.
CSCR based sparse matrix.
bool same_layout(const SparseMatrixCSCR &x) const
Checks if the structural layout of this matrix matches that of another matrix. This excludes comparis...
SparseMatrixCSCR(FileMode mode, const String &filename)
Constructor.
const Memory::Arbiter & row_ptr_arbiter() const
Returns a reference to the row-pointer array arbiter.
SparseMatrixCSCR(Index rows, Index cols, Index nz_rows, Index nzes)
Constructor.
void convert(const MT_ &a)
Conversion method.
bool hollow() const
Checks whether the matrix has no non-zero entries, i.e. whether it is a null matrix.
SparseMatrixCSCR(const SparseLayout< IT_, layout_id > &layout_in)
Constructor.
Memory::Arbiter & row_idx_arbiter()
Returns a reference to the row-index array arbiter.
void clone(const SparseMatrixCSCR< DT2_, IT2_ > &other, CloneMode clone_mode=CloneMode::Weak)
Clone operation.
Index num_rows_raw() const
Returns the scalar matrix row count.
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 num_nzes() const
Retrieve non zero element count.
void print(std::ostream &os, bool print_dense) const
Prints this sparse matrix in human-readable format into an output stream.
void write_out(FileMode mode, const String &filename) const
Write out matrix to file.
Index num_nzes_raw() const
Returns the scalar matrix nonzero element count.
friend std::ostream & operator<<(std::ostream &os, const SparseMatrixCSCR &b)
SparseMatrixCSR streaming operator.
DenseVector< DT_, IT_ > VectorTypeL
Compatible L-vector type.
void copy(const SparseMatrixCSCR &x, bool full=false)
Performs .
const Memory::Arbiter & col_idx_arbiter() const
Returns a reference to the column-index array arbiter.
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.
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.
Memory::Arbiter & col_idx_arbiter()
Returns a reference to the column-index array arbiter.
void read_from(FileMode mode, const String &filename)
Read in matrix from file.
SparseMatrixCSCR(SparseMatrixCSCR &&other)
Move Constructor.
SparseMatrixCSCR(const Adjacency::Graph &graph)
Constructor.
bool empty() const
Checks whether the matrix is empty, i.e. whether it is a 0x0 matrix.
DenseVector< DT_, IT_ > VectorTypeR
Compatible R-vector type.
void convert(const SparseMatrixCSCR< DT2_, IT2_ > &other)
Conversion method.
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 Memory::Arbiter & row_idx_arbiter() const
Returns a reference to the row-index array arbiter.
SparseMatrixCSCR(std::vector< char > input)
Constructor.
SparseMatrixCSCR(const Index rows_in, const Index columns_in, DenseVector< IT_, IT_ > &row_ptr_in, DenseVector< IT_, IT_ > &row_idx_in, DenseVector< IT_, IT_ > &col_idx_in, DenseVector< DT_, IT_ > &val_in)
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.
Index num_cols() const
Retrieve matrix column count.
Memory::Arbiter & row_ptr_arbiter()
Returns a reference to the row-pointer array arbiter.
Index num_rows() const
Retrieve matrix row count.
Index num_nonzero_rows() const
Retrieve used matrix non zero row count.
SparseMatrixCSCR clone(CloneMode clone_mode=CloneMode::Weak) const
Clone operation.
Index num_cols_raw() const
Returns the scalar matrix column count.
Memory::Arbiter & val_arbiter()
Returns a reference to the values array arbiter.
SparseMatrixCSCR(const SparseMatrixCSR< DT_, IT_ > &csr, const VectorMirror< DT_, IT_ > &non_zero_rows)
Constructor.
DT_ ValueType
Value type, meaning the type of each 'block'.
const Memory::Arbiter & val_arbiter() const
Returns a reference to the values array arbiter.
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.
Index num_cols() const
Retrieve matrix column count.
Index num_rows() const
Retrieve matrix row count.
Handles vector prolongation, restriction and serialization.
Index num_indices() const
Returns the number of indices in the mirror.
Arbiter attach(std::size_t offset=0u, std::size_t size=full_size) const
Creates a new arbiter that is attached to the same memory block as this arbiter.
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.
Access
Memory access enumeration.
@ read_write
read-write access
@ write
write-only access (see attention note above)
Location
Memory location enumeration.
std::uint64_t Index
Index data type.