11#include <kernel/util/math.hpp>
12#include <kernel/util/memory_arbiter.hpp>
13#include <kernel/adjacency/graph.hpp>
14#include <kernel/lafem/forward.hpp>
15#include <kernel/lafem/container.hpp>
16#include <kernel/lafem/dense_vector.hpp>
17#include <kernel/lafem/sparse_layout.hpp>
18#include <kernel/lafem/arch/axpy_dense.hpp>
19#include <kernel/lafem/arch/matvecmult_banded_dense.hpp>
20#include <kernel/lafem/arch/norm2_dense.hpp>
21#include <kernel/lafem/arch/scale_dense.hpp>
75 template <
typename DT_,
typename IT_ = Index>
92 template <
typename DT2_ = DT_,
typename IT2_ = IT_>
96 template <
typename DataType2_,
typename IndexType2_>
122 template <
typename ITX_>
141 template <
typename ITX_>
205 this->
_indices_size.assign(layout_in._indices_size.begin(), layout_in._indices_size.end());
206 this->
_scalar_index.assign(layout_in._scalar_index.begin(), layout_in._scalar_index.end());
209 this->
_indices.push_back(idx.attach());
230 if (val_in.
size() != rows_in * bands_in.
size())
232 XABORTM(
"Size of values does not match to number of offsets and row count!");
241 for (
Index i(0); i < bands_in.
size(); ++i)
245 if (toffset +
Index(2) > rows_in + columns_in)
247 XABORTM(
"Offset out of matrix!");
250 tnonzeros += columns_in +
Math::min(rows_in, columns_in + rows_in - toffset - 1) -
Math::max(columns_in + rows_in - toffset - 1, columns_in);
270 template <
typename MT_>
291 std::set<IT_> moffsets;
295 for(
Index j(dom_ptr[row]); j < dom_ptr[row+1]; ++j)
297 moffsets.insert(IT_(
num_rows - 1 + img_idx[j] - row));
305 for (
auto off : moffsets)
307 ptoffsets[idx] = off;
349 template <
typename DT2_ = DT_,
typename IT2_ = IT_>
352 this->deserialize<DT2_, IT2_>(input);
376 this->
move(std::forward<SparseMatrixBanded>(other));
391 t.
clone(*
this, clone_mode);
403 template<
typename DT2_,
typename IT2_>
424 this->
_indices_size.assign(layout_in._indices_size.begin(), layout_in._indices_size.end());
425 this->
_scalar_index.assign(layout_in._scalar_index.begin(), layout_in._scalar_index.end());
428 this->
_indices.push_back(idx.attach());
444 template <
typename DT2_,
typename IT2_>
450 template <
typename DT2_,
typename IT2_>
459 std::set<IT_> offset_set;
462 for (
Index row(0) ; row < nrows ; ++row)
464 for (
Index i(row_ptr[row]) ; i < row_ptr[row+1] ; ++i)
467 IT_ off = IT_(col_idx[i] - row + nrows - 1);
468 offset_set.insert(off);
472 DenseVector<DT_, IT_> val_new(
Index(offset_set.size()) * nrows, DT_(0));
473 Memory::TypedView<DT_ > pval = val_new.elements_view_w();
474 for (
Index row(0) ; row < nrows ; ++row)
476 for (
Index i(row_ptr[row]) ; i < row_ptr[row+1] ; ++i)
478 Index col = col_idx[i];
479 Index offset = (col - row + nrows - 1);
480 Index band =
Index(std::distance(offset_set.begin(), offset_set.find(IT_(offset))));
481 pval[band * nrows + row] = csr_val[i];
485 DenseVector<IT_, IT_> offsets_new(
Index(offset_set.size()));
486 Memory::TypedView<IT_> poffsets = offsets_new.elements_view_w();
487 auto it_offset = offset_set.begin();
488 for (
Index i(0) ; i < offset_set.size() ; ++i, ++it_offset)
490 poffsets[i] = *it_offset;
497 SparseMatrixBanded<DT_, IT_> temp(nrows, ncolumns, val_new, offsets_new);
498 this->
move(std::move(temp));
509 std::ios_base::openmode bin = std::ofstream::out | std::ofstream::binary;
511 bin = std::ofstream::out;
513 char* buff =
nullptr;
519 file.open(filename.c_str(), bin);
521 XABORTM(
"Unable to open Matrix file " + filename);
540 if (! std::is_same<DT_, double>::value)
541 std::cout<<
"Warning: You are writing out a banded matrix that is not double precision!\n";
542 this->
template _serialize<double, std::uint64_t>(
FileMode::fm_bm, file);
546 XABORTM(
"Filemode not supported!");
685 return Memory::TypedView<DT_>();
692 return Memory::TypedView<DT_>();
693 return Memory::TypedView<DT_>(this->
_elements.at(0).view(loc, acc));
699 return Memory::TypedView<IT_>();
706 return Memory::TypedView<IT_>();
713 return Memory::TypedView<IT_>();
720 return Memory::TypedView<IT_>();
721 return Memory::TypedView<IT_>(this->
_indices.at(0).view(loc, acc));
731 return "SparseMatrixBanded";
742 this->_copy_content(x, full);
759 XASSERTM(x.
num_nzes() == this->num_nzes(),
"Matrix nonzeros do not match!");
771 Statistics::add_time_axpy(ts_stop.
elapsed(ts_start));
785 XASSERTM(x.
num_nzes() == this->num_nzes(),
"Matrix nonzeros do not match!");
797 Statistics::add_time_axpy(ts_stop.
elapsed(ts_start));
816 Statistics::add_time_reduction(ts_stop.
elapsed(ts_start));
829 XASSERTM(r.
size() == this->num_rows(),
"Vector size of r does not match!");
830 XASSERTM(x.
size() == this->num_cols(),
"Vector size of x does not match!");
845 Arch::MatVecMultBandedDense::template exec<DT_, IT_>(
847 this->val_arbiter(), this->offsets_arbiter(), this->num_bands(), this->num_rows(), this->num_cols());
852 Statistics::add_time_blas2(ts_stop.
elapsed(ts_start));
863 XASSERTM(r.
size() == this->num_cols(),
"Vector size of r does not match!");
864 XASSERTM(x.
size() == this->num_rows(),
"Vector size of x does not match!");
879 Arch::MatVecMultBandedDense::template exec<DT_, IT_>(
881 this->val_arbiter(), this->offsets_arbiter(), this->num_bands(), this->num_rows(), this->num_cols(),
true);
885 Statistics::add_time_blas2(ts_stop.
elapsed(ts_start));
899 const DT_ alpha = DT_(1))
const
901 XASSERTM(r.
size() == this->num_rows(),
"Vector size of r does not match!");
902 XASSERTM(x.
size() == this->num_cols(),
"Vector size of x does not match!");
903 XASSERTM(y.
size() == this->num_rows(),
"Vector size of y does not match!");
920 Arch::MatVecMultBandedDense::template exec<DT_, IT_>(
922 this->val_arbiter(), this->offsets_arbiter(), this->num_bands(), this->num_rows(), this->num_cols());
927 Statistics::add_time_blas2(ts_stop.
elapsed(ts_start));
941 const DT_ alpha = DT_(1))
const
943 XASSERTM(r.
size() == this->num_cols(),
"Vector size of r does not match!");
944 XASSERTM(x.
size() == this->num_rows(),
"Vector size of x does not match!");
945 XASSERTM(y.
size() == this->num_cols(),
"Vector size of y does not match!");
963 Arch::MatVecMultBandedDense::template exec<DT_, IT_>(
965 this->val_arbiter(), this->offsets_arbiter(), this->num_bands(), this->num_rows(), this->num_cols(),
true);
968 Statistics::add_time_blas2(ts_stop.
elapsed(ts_start));
987 if (offs[i]+1 == nrows)
1010 template <
typename DT2_ = DT_,
typename IT2_ = IT_>
1024 XASSERTM(x.
num_nzes() == this->num_nzes(),
"Nonzero count does not match!");
1029 DT_ result = Arch::MaxRelDiffDense::template exec<DT_>(this->
val_arbiter(), x.
val_arbiter(), this->val_size());
1032 Statistics::add_time_reduction(ts_stop.
elapsed(ts_start));
1068 if (offsets_a[i] != offsets_b[i])
1086 template <
typename DT2_ = DT_,
typename IT2_ = IT_>
1089 return this->
template _serialize<DT2_, IT2_>(
FileMode::fm_bm, config);
1100 std::ios_base::openmode bin = std::ifstream::in | std::ifstream::binary;
1102 bin = std::ifstream::in;
1103 std::ifstream file(filename.c_str(), bin);
1104 if (! file.is_open())
1105 XABORTM(
"Unable to open Matrix file " + filename);
1124 this->
template _deserialize<double, std::uint64_t>(
FileMode::fm_bm, file);
1128 XABORTM(
"Filemode not supported!");
1154 void print(std::ostream& os,
bool print_dense)
const
1169 for (
Index i(0) ; i < nrows ; ++i)
1174 for(IT_ j(0); j < nbands; ++j)
1176 if((off[j] + i + 2 > nrows) && (off[j] + i + 1 < 2 * nrows))
1178 const Index col_idx =
Index(off[j] + i + 1) - nrows;
1181 for(; k < col_idx; ++k)
1182 os <<
" " << DT_(0);
1186 os <<
" " << col_idx <<
":";
1188 os <<
" " << val(j * nrows + i);
1196 for(; k < ncols; ++k)
1197 os <<
" " << DT_(0);
1227 const IT_ *
const _offsets;
1230 ImageIterator() : _k(IT_(0)), _s(IT_(0)), _offsets(
nullptr) {}
1232 ImageIterator(IT_ k, IT_ s,
const IT_ * offsets) : _k(k), _s(s), _offsets(offsets) {}
1238 _offsets = other._offsets;
1244 return this->_k != other._k;
1255 return Index(_s + _offsets[_k]);
1266 _offsets(matrix.offsets_view_r())
1290 for(IT_ k(0); k < tbands; ++k)
1292 if(_offsets[k] + domain_node + 1 >= trows)
1305 for(IT_ k(0); k < tbands; ++k)
1307 if(_offsets[k] + domain_node + 1 >= 2 * trows)
1330 static constexpr IT_ _deadcode = ~IT_(0);
1346 _offsets(matrix.offsets_view_r()),
1352 template<
typename LocalMatrix_,
typename RowMapping_,
typename ColMapping_>
1353 void operator()(
const LocalMatrix_& loc_mat,
const RowMapping_& row_map,
1354 const ColMapping_& col_map, DT_ alpha = DT_(1))
1357 for(
int i(0); i < row_map.get_num_local_dofs(); ++i)
1360 const Index ix = row_map.get_index(i);
1363 for(IT_ k(0); k < _num_bands; ++k)
1365 if(_offsets[k] + ix + 1 >= _num_rows && _offsets[k] + ix + 1 < 2 * _num_rows)
1367 _col_ptr[_offsets[k] + ix + 1 - _num_rows] = IT_(k * _num_rows + ix);
1372 for(
int j(0); j < col_map.get_num_local_dofs(); ++j)
1375 const Index jx = col_map.get_index(j);
1378 ASSERTM(_col_ptr[jx] != _deadcode,
"invalid column index");
1381 _data[_col_ptr[jx]] += alpha * loc_mat[i][j];
1388 for(IT_ k(0); k < _num_bands; ++k)
1390 if(_offsets[k] + ix + 1 >= _num_rows && _offsets[k] + ix + 1 < 2 * _num_rows)
1392 _col_ptr[_offsets[k] + ix + 1 - _num_rows] = _deadcode;
1415 static constexpr IT_ _deadcode = ~IT_(0);
1431 _offsets(matrix.offsets_view_r()),
1433 _data(matrix.val_view_r())
1437 template<
typename LocalMatrix_,
typename RowMapping_,
typename ColMapping_>
1438 void operator()(LocalMatrix_& loc_mat,
const RowMapping_& row_map,
1439 const ColMapping_& col_map, DT_ alpha = DT_(1))
1442 for(
int i(0); i < row_map.get_num_local_dofs(); ++i)
1445 const Index ix = row_map.get_index(i);
1448 for(IT_ k(0); k < _num_bands; ++k)
1450 if(_offsets[k] + ix + 1 >= _num_rows && _offsets[k] + ix + 1 < 2 * _num_rows)
1452 _col_ptr[_offsets[k] + ix + 1 - _num_rows] = IT_(k * _num_rows + ix);
1457 for(
int j(0); j < col_map.get_num_local_dofs(); ++j)
1460 const Index jx = col_map.get_index(j);
1463 ASSERTM(_col_ptr[jx] != _deadcode,
"invalid column index");
1466 loc_mat[i][j] += alpha * _data[_col_ptr[jx]];
1473 for(IT_ k(0); k < _num_bands; ++k)
1475 if(_offsets[k] + ix + 1 >= _num_rows && _offsets[k] + ix + 1 < 2 * _num_rows)
1477 _col_ptr[_offsets[k] + ix + 1 - _num_rows] = _deadcode;
#define XABORTM(msg)
Abortion macro definition with custom message.
#define ASSERTM(expr, msg)
Debug-Assertion 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.
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.
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.
ImageIterator class for Adjactor interface implementation.
Index get_num_nodes_domain() const
ImageIterator image_begin(Index domain_node) const
ImageIterator image_end(Index domain_node) const
Index get_num_nodes_image() const
Gather-Axpy operation for SparseMatrixBanded.
Scatter-Axpy operation for SparseMatrixBanded.
void extract_diag(VectorTypeL &diag) const
SparseMatrixBanded clone(CloneMode clone_mode=CloneMode::Weak) const
Clone operation.
bool empty() const
Checks whether the matrix is empty, i.e. whether it is a 0x0 matrix.
SparseMatrixBanded & operator=(SparseMatrixBanded &&other)
Move operator.
void read_from(FileMode mode, std::istream &file)
Read in matrix from stream.
void apply(DenseVector< DT_, IT_ > &r, const DenseVector< DT_, IT_ > &x) const
Calculate .
DT_ ValueType
our value type
const Memory::Arbiter & offsets_arbiter() const
Returns a reference to the band offsets array arbiter.
void convert(const SparseMatrixBanded< DT2_, IT2_ > &other)
Conversion method.
bool same_layout(const SparseMatrixBanded &x) const
Checks if the structural layout of this matrix matches that of another matrix. This excludes comparis...
Index num_nzes() const
Retrieve non zero element count.
SparseMatrixBanded(const SparseLayout< IT_, layout_id > &layout_in)
Constructor.
Memory::Arbiter & offsets_arbiter()
Returns a reference to the band offsets array arbiter.
void copy(const SparseMatrixBanded &x, bool full=false)
Performs .
DT_ DataType
Our datatype.
SparseLayout< IT_, layout_id > layout() const
Retrieve convenient sparse matrix layout object.
DenseVector< DataType, IT_ > VectorTypeR
Compatible R-vector type.
bool hollow() const
Checks whether the matrix has no non-zero entries, i.e. whether it is a null matrix.
void deserialize(std::vector< char > input)
Deserialization of complete container entity.
const Memory::Arbiter & val_arbiter() const
Returns a reference to the values array arbiter.
Memory::Arbiter & val_arbiter()
Returns a reference to the values array arbiter.
IT_ IndexType
Our indextype.
void write_out(FileMode mode, const String &filename) const
Write out matrix to file.
static constexpr SparseLayoutId layout_id
Our used layout type.
SparseMatrixBanded(FileMode mode, String filename)
Constructor.
friend std::ostream & operator<<(std::ostream &os, const SparseMatrixBanded &b)
SparseMatrixBanded streaming operator.
void apply_transposed(DenseVector< DT_, IT_ > &r, const DenseVector< DT_, IT_ > &x) const
Calculate .
Index val_size() const
Retrieve total element count.
Index _start_offset(const Index i, const ITX_ *const offsets, const Index rows_in, const Index columns_in, const Index noo) const
Returns first row-index of the diagonal matching to the offset i.
VectorTypeL extract_diag() const
extract main diagonal vector from matrix
SparseMatrixBanded(const Index rows_in, const Index columns_in, DenseVector< DT_, IT_ > &val_in, DenseVector< IT_, IT_ > &bands_in)
Constructor.
Index num_cols() const
Retrieve matrix column count.
DenseVector< DataType, IT_ > VectorTypeL
Compatible L-vector type.
SparseMatrixBanded(const Index rows_in, const Index columns_in, const Index bands_in, const Index nzes_in)
Constructor.
void read_from(FileMode mode, const String &filename)
Read in matrix from file.
DT_ max_rel_diff(const SparseMatrixBanded &x) const
Retrieve the maximum relative difference of this matrix and another one y.max_rel_diff(x) returns .
SparseMatrixBanded(FileMode mode, std::istream &file)
Constructor.
Index _end_offset(const Index i, const ITX_ *const offsets, const Index rows_in, const Index columns_in, const Index noo) const
Returns last row-index of the diagonal matching to the offset i.
void write_out(FileMode mode, std::ostream &file) const
Write out matrix to file.
SparseMatrixBanded(const Adjacency::Graph &graph)
Constructor.
DT_ norm_frobenius() const
Calculates the Frobenius norm of this matrix.
void clone(const SparseMatrixBanded< DT2_, IT2_ > &other, CloneMode clone_mode=CloneMode::Weak)
Clone operation.
std::vector< char > serialize(const LAFEM::SerialConfig &config=SerialConfig()) const
Serialization of complete container entity.
VectorTypeL create_vector_l() const
Returns a new compatible L-Vector.
SparseMatrixBanded()
Constructor.
VectorTypeR create_vector_r() const
Returns a new compatible R-Vector.
void apply(DenseVector< DT_, IT_ > &r, const DenseVector< DT_, IT_ > &x, const DenseVector< DT_, IT_ > &y, const DT_ alpha=DT_(1)) const
Calculate .
SparseMatrixBanded(std::vector< char > input)
Constructor.
Index num_rows() const
Retrieve matrix row count.
Index num_bands() const
Retrieve number of bands.
void axpy(const SparseMatrixBanded &x, const DT_ alpha=DT_(1))
Calculate .
SparseMatrixBanded(const MT_ &other)
Constructor.
Index num_cols_raw() const
Returns the scalar matrix column count.
Index num_nzes_raw() const
Returns the scalar matrix nonzero element count.
void scale(const SparseMatrixBanded &x, const DT_ alpha)
Calculate .
SparseMatrixBanded(SparseMatrixBanded &&other)
Move Constructor.
Index num_rows_raw() const
Returns the scalar matrix row count.
void apply_transposed(DenseVector< DT_, IT_ > &r, const DenseVector< DT_, IT_ > &x, const DenseVector< DT_, IT_ > &y, const DT_ alpha=DT_(1)) const
Calculate .
static String name()
Returns a descriptive string.
void print(std::ostream &os, bool print_dense) const
Prints this sparse matrix in human-readable format into an output stream.
Index num_cols() const
Retrieve matrix column count.
Index num_rows() const
Retrieve matrix row count.
Index num_nzes() const
Retrieve non zero element count.
void copy(const Arbiter &source, Location mem_loc=Location::none)
Copies of the contents of one memory block into another one.
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.
View view(Location mem_loc, Access access)
Maps a raw view to a memory block.
const T_ * get_r() const
Returns a const typed pointer to the array that grants 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.
bool operator*(TestMatrixFlags a, TestMatrixFlags b)
checks whether a & b != 0
T_ min(T_ a, T_ b)
Returns the minimum of two values.
T_ max(T_ a, T_ b)
Returns the maximum of two values.
Access
Memory access enumeration.
@ read_write
read-write access
@ overlap
explicitly allow overlapping writes (see Overlapping Memory Views)
@ write
write-only access (see attention note above)
Location
Memory location enumeration.
@ none
no or unknown or unspecified location
@ format_to_zero
allocate and format all bits to zero
@ format_to_one
allocate and format all bits to one
std::uint64_t Index
Index data type.