4#ifndef DUNE_ISTL_BCRSMATRIX_HH
5#define DUNE_ISTL_BCRSMATRIX_HH
19#include <dune/common/stdstreams.hh>
20#include <dune/common/iteratorfacades.hh>
21#include <dune/common/typetraits.hh>
22#include <dune/common/ftraits.hh>
70 struct MatrixDimension;
79 template<
typename size_type>
137 return _m.entry(_i,j);
166 if (m.buildMode() != Matrix::implicit)
167 DUNE_THROW(
BCRSMatrixError,
"You can only create an ImplicitBuilder for a matrix in implicit build mode");
168 if (m.buildStage() != Matrix::building)
169 DUNE_THROW(
BCRSMatrixError,
"You can only create an ImplicitBuilder for a matrix with set size that has not been compressed() yet");
190 if (m.buildStage() != Matrix::notAllocated)
191 DUNE_THROW(
BCRSMatrixError,
"You can only set up a matrix for this ImplicitBuilder if it has no memory allocated yet");
192 m.setBuildMode(Matrix::implicit);
193 m.setImplicitBuildModeParameters(avg_cols_per_row,overflow_fraction);
194 m.setSize(rows,cols);
410 template<
class B,
class A=std::allocator<B> >
499#ifdef DUNE_ISTL_WITH_CHECKING
511#ifdef DUNE_ISTL_WITH_CHECKING
526 :
public RandomAccessIteratorFacade<RealRowIterator<T>, T>
598 void advance(std::ptrdiff_t
diff)
603 T& elementAt(std::ptrdiff_t
diff)
const
870 if (&Mat==
this)
return *
this;
880 if (
n>0 &&
n!=Mat.
n) {
976 Mat.
r[i].
set(0,
nullptr,
nullptr);
979 std::copy(pattern.cbegin(), pattern.cend(), Mat.
r[i].
getindexptr());
1006 return (i!=
it.i) || (&Mat!=&
it.Mat);
1012 return (i==
it.i) && (&Mat==&
it.Mat);
1030 return pattern.find(j) != pattern.end();
1039 return pattern.size();
1046 typedef std::set<size_type,std::less<size_type> > PatternType;
1047 PatternType pattern;
1088#ifdef DUNE_ISTL_WITH_CHECKING
1126 <<
"sufficient for calculated nonzeros ("<<
total<<
"! ");
1191 template<
typename It>
1201 <<
") does not match number of passed entries (" << (
col_end -
col_begin) <<
")");
1223 if (j.index() >=
m) {
1224 dwarn <<
"WARNING: size of row "<< i.index()<<
" is "<<j.offset()<<
". But was specified as being "<< (*i).end().offset()
1225 <<
". This means you are wasting valuable space and creating additional cache misses!"<<std::endl;
1255#ifdef DUNE_ISTL_WITH_CHECKING
1287 if (
r[row].getsize() ==
avg)
1338 typename OverflowType::iterator
oit =
overflow.begin();
1341 std::vector<size_type*>
perm;
1353 typename std::vector<size_type*>::iterator
it =
perm.begin();
1372 "Allocated memory for BCRSMatrix exhausted during compress()!"
1373 "Please increase either the average number of entries per row or the overflow fraction."
1387 "Allocated memory for BCRSMatrix exhausted during compress()!"
1388 "Please increase either the average number of entries per row or the overflow fraction."
1405 "Allocated memory for BCRSMatrix exhausted during compress()!"
1406 "Please increase either the average number of entries per row or the overflow fraction."
1443#ifdef DUNE_ISTL_WITH_CHECKING
1471#ifdef DUNE_ISTL_WITH_CHECKING
1504#ifdef DUNE_ISTL_WITH_CHECKING
1507 if(
N()!=b.
N() ||
M() != b.
M())
1526#ifdef DUNE_ISTL_WITH_CHECKING
1529 if(
N()!=b.
N() ||
M() != b.
M())
1551#ifdef DUNE_ISTL_WITH_CHECKING
1554 if(
N()!=b.
N() ||
M() != b.
M())
1568 template<
class X,
class Y>
1571#ifdef DUNE_ISTL_WITH_CHECKING
1575 "Size mismatch: M: " <<
N() <<
"x" <<
M() <<
" x: " << x.N());
1577 "Size mismatch: M: " <<
N() <<
"x" <<
M() <<
" y: " <<
y.N());
1585 (*j).umv(x[j.index()],
y[i.index()]);
1590 template<
class X,
class Y>
1593#ifdef DUNE_ISTL_WITH_CHECKING
1604 (*j).umv(x[j.index()],
y[i.index()]);
1609 template<
class X,
class Y>
1612#ifdef DUNE_ISTL_WITH_CHECKING
1623 (*j).mmv(x[j.index()],
y[i.index()]);
1628 template<
class X,
class Y,
class F>
1631#ifdef DUNE_ISTL_WITH_CHECKING
1642 (*j).usmv(alpha,x[j.index()],
y[i.index()]);
1647 template<
class X,
class Y>
1650#ifdef DUNE_ISTL_WITH_CHECKING
1662 template<
class X,
class Y>
1665#ifdef DUNE_ISTL_WITH_CHECKING
1676 (*j).umtv(x[i.index()],
y[j.index()]);
1681 template<
class X,
class Y>
1684#ifdef DUNE_ISTL_WITH_CHECKING
1693 (*j).mmtv(x[i.index()],
y[j.index()]);
1698 template<
class X,
class Y>
1701#ifdef DUNE_ISTL_WITH_CHECKING
1712 (*j).usmtv(alpha,x[i.index()],
y[j.index()]);
1717 template<
class X,
class Y>
1720#ifdef DUNE_ISTL_WITH_CHECKING
1731 (*j).umhv(x[i.index()],
y[j.index()]);
1736 template<
class X,
class Y>
1739#ifdef DUNE_ISTL_WITH_CHECKING
1750 (*j).mmhv(x[i.index()],
y[j.index()]);
1755 template<
class X,
class Y>
1758#ifdef DUNE_ISTL_WITH_CHECKING
1769 (*j).usmhv(alpha,x[i.index()],
y[j.index()]);
1779#ifdef DUNE_ISTL_WITH_CHECKING
1791 sum += (*j).frobenius_norm2();
1805 typename std::enable_if<!has_nan<ft>::value,
int>::type = 0>
1814 for (
auto const &x : *
this) {
1816 for (
auto const &
y : x)
1817 sum +=
y.infinity_norm();
1818 norm = max(sum, norm);
1825 typename std::enable_if<!has_nan<ft>::value,
int>::type = 0>
1834 for (
auto const &x : *
this) {
1836 for (
auto const &
y : x)
1837 sum +=
y.infinity_norm_real();
1838 norm = max(sum, norm);
1845 typename std::enable_if<has_nan<ft>::value,
int>::type = 0>
1854 real_type
isNaN = 1;
1855 for (
auto const &x : *
this) {
1857 for (
auto const &
y : x)
1858 sum +=
y.infinity_norm();
1859 norm = max(sum, norm);
1863 return norm *
isNaN;
1868 typename std::enable_if<has_nan<ft>::value,
int>::type = 0>
1877 real_type
isNaN = 1;
1878 for (
auto const &x : *
this) {
1880 for (
auto const &
y : x)
1881 sum +=
y.infinity_norm_real();
1882 norm = max(sum, norm);
1886 return norm *
isNaN;
1926#ifdef DUNE_ISTL_WITH_CHECKING
1930 return (
r[i].size() &&
r[i].find(j) !=
r[i].
end());
1960 std::shared_ptr<size_type>
j_;
1984 r[i].
set(0,
nullptr,
nullptr);
2007 r[i].
set(0,
nullptr,
nullptr);
2025 if (
r[i].getsize() > 0) {
2030 r[i].
set(0,
nullptr,
nullptr);
2078 if (
r[i].getsize()>0)
2080 for (B *
col=
r[i].getptr()+(
r[i].getsize()-1),
2088 r[i].
set(0,
nullptr,
nullptr);
Some handy generic functions for ISTL matrices.
This file implements a vector space as a tensor product of a given vector space. The number of compon...
Col col
Definition matrixmatrix.hh:347
Definition basearray.hh:19
PropertyMapTypeSelector< Amg::VertexVisitedTag, Amg::PropertiesGraph< G, Amg::VertexProperties, EP, VM, EM > >::Type get(const Amg::VertexVisitedTag &tag, Amg::PropertiesGraph< G, Amg::VertexProperties, EP, VM, EM > &graph)
Definition dependency.hh:292
size_type size() const
number of blocks in the array (are of size 1 here)
Definition basearray.hh:756
Definition matrixutils.hh:231
Statistics about compression achieved in implicit mode.
Definition bcrsmatrix.hh:81
size_type overflow_total
total number of elements written to the overflow area during construction.
Definition bcrsmatrix.hh:87
size_type maximum
maximum number of non-zeroes per row.
Definition bcrsmatrix.hh:85
double avg
average number of non-zeroes per row.
Definition bcrsmatrix.hh:83
double mem_ratio
fraction of wasted memory resulting from non-used overflow area.
Definition bcrsmatrix.hh:92
A wrapper for uniform access to the BCRSMatrix during and after the build stage in implicit build mod...
Definition bcrsmatrix.hh:110
Matrix::block_type block_type
The block_type of the underlying matrix.
Definition bcrsmatrix.hh:118
ImplicitMatrixBuilder(Matrix &m)
Creates an ImplicitMatrixBuilder for matrix m.
Definition bcrsmatrix.hh:163
M_ Matrix
The underlying matrix.
Definition bcrsmatrix.hh:115
ImplicitMatrixBuilder(Matrix &m, size_type rows, size_type cols, size_type avg_cols_per_row, double overflow_fraction)
Sets up matrix m for implicit construction using the given parameters and creates an ImplicitBmatrixu...
Definition bcrsmatrix.hh:187
size_type M() const
The number of columns in the matrix.
Definition bcrsmatrix.hh:210
Matrix::size_type size_type
The size_type of the underlying matrix.
Definition bcrsmatrix.hh:121
row_object operator[](size_type i) const
Returns a proxy for entries in row i.
Definition bcrsmatrix.hh:198
size_type N() const
The number of rows in the matrix.
Definition bcrsmatrix.hh:204
Proxy row object for entry access.
Definition bcrsmatrix.hh:130
block_type & operator[](size_type j) const
Returns entry in column j.
Definition bcrsmatrix.hh:135
A sparse block matrix with compressed row storage.
Definition bcrsmatrix.hh:412
bool exists(size_type i, size_type j) const
return true if (i,j) is in pattern
Definition bcrsmatrix.hh:1924
BuildStage buildStage() const
The current build stage of the matrix.
Definition bcrsmatrix.hh:1910
Iterator begin()
Get iterator to first row.
Definition bcrsmatrix.hh:623
void copyWindowStructure(const BCRSMatrix &Mat)
Copy the window structure from another matrix.
Definition bcrsmatrix.hh:2039
B & entry(size_type row, size_type col)
Returns reference to entry (row,col) of the matrix.
Definition bcrsmatrix.hh:1253
void mmhv(const X &x, Y &y) const
y -= A^H x
Definition bcrsmatrix.hh:1737
void usmhv(const field_type &alpha, const X &x, Y &y) const
y += alpha A^H x
Definition bcrsmatrix.hh:1756
void umtv(const X &x, Y &y) const
y += A^T x
Definition bcrsmatrix.hh:1663
size_type m
Definition bcrsmatrix.hh:1948
RealRowIterator< const row_type > const_iterator
The const iterator over the matrix rows.
Definition bcrsmatrix.hh:655
double overflowsize
Definition bcrsmatrix.hh:1964
BCRSMatrix(size_type _n, size_type _m, size_type _avg, double _overflowsize, BuildMode bm)
construct matrix with a known average number of entries per row
Definition bcrsmatrix.hh:732
void allocate(size_type rows, size_type columns, size_type allocationSize, bool allocateRows, bool allocate_data)
Allocate memory for the matrix structure.
Definition bcrsmatrix.hh:2136
B::field_type field_type
export the type representing the field
Definition bcrsmatrix.hh:434
BCRSMatrix & axpy(field_type alpha, const BCRSMatrix &b)
Add the scaled entries of another matrix to this one.
Definition bcrsmatrix.hh:1549
FieldTraits< ft >::real_type infinity_norm_real() const
simplified infinity norm (uses Manhattan norm for complex values)
Definition bcrsmatrix.hh:1826
~BCRSMatrix()
destructor
Definition bcrsmatrix.hh:780
void allocateData()
Definition bcrsmatrix.hh:2172
void deallocate(bool deallocateRows=true)
deallocate memory of the matrix.
Definition bcrsmatrix.hh:2056
Iterator RowIterator
rename the iterators for easier access
Definition bcrsmatrix.hh:649
row_type & operator[](size_type i)
random access to the rows
Definition bcrsmatrix.hh:497
BCRSMatrix()
an empty matrix
Definition bcrsmatrix.hh:697
void endrowsizes()
indicate that size of all rows is defined
Definition bcrsmatrix.hh:1107
void incrementrowsize(size_type i, size_type s=1)
increment size of row i by s (1 by default)
Definition bcrsmatrix.hh:1096
void mtv(const X &x, Y &y) const
y = A^T x
Definition bcrsmatrix.hh:1648
void umhv(const X &x, Y &y) const
y += A^H x
Definition bcrsmatrix.hh:1718
size_type nonzeroes() const
number of blocks that are stored (the number of blocks that possibly are nonzero)
Definition bcrsmatrix.hh:1904
size_type allocationSize_
Definition bcrsmatrix.hh:1950
ConstIterator ConstRowIterator
rename the const row iterator for easier access
Definition bcrsmatrix.hh:686
BuildStage ready
Definition bcrsmatrix.hh:1937
BuildMode build_mode
Definition bcrsmatrix.hh:1936
void setrowsize(size_type i, size_type s)
Set number of indices in row i to s.
Definition bcrsmatrix.hh:1075
RealRowIterator< row_type > Iterator
Definition bcrsmatrix.hh:620
size_type nnz_
Definition bcrsmatrix.hh:1949
BCRSMatrix & operator*=(const field_type &k)
vector space multiplication with scalar
Definition bcrsmatrix.hh:1441
RealRowIterator< row_type > iterator
The iterator over the (mutable matrix rows.
Definition bcrsmatrix.hh:619
void usmtv(const field_type &alpha, const X &x, Y &y) const
y += alpha A^T x
Definition bcrsmatrix.hh:1699
ConstIterator beforeBegin() const
Definition bcrsmatrix.hh:680
RealRowIterator< const row_type > ConstIterator
Definition bcrsmatrix.hh:656
Iterator beforeBegin()
Definition bcrsmatrix.hh:643
B * a
Definition bcrsmatrix.hh:1957
BuildMode
we support two modes
Definition bcrsmatrix.hh:458
@ implicit
Build entries randomly with an educated guess for the number of entries per row.
Definition bcrsmatrix.hh:487
@ unknown
Build mode not set!
Definition bcrsmatrix.hh:491
@ random
Build entries randomly.
Definition bcrsmatrix.hh:478
@ row_wise
Build in a row-wise manner.
Definition bcrsmatrix.hh:469
BCRSMatrix(size_type _n, size_type _m, size_type _nnz, BuildMode bm)
matrix with known number of nonzeroes
Definition bcrsmatrix.hh:704
::Dune::CompressionStatistics< size_type > CompressionStatistics
The type for the statistics object returned by compress()
Definition bcrsmatrix.hh:449
BCRSMatrix & operator-=(const BCRSMatrix &b)
Subtract the entries of another matrix from this one.
Definition bcrsmatrix.hh:1524
BCRSMatrix(const BCRSMatrix &Mat)
copy constructor
Definition bcrsmatrix.hh:753
Iterator end()
Get iterator to one beyond last row.
Definition bcrsmatrix.hh:629
row_type * r
Definition bcrsmatrix.hh:1954
void setIndices(size_type row, It begin, It end)
Set all column indices for row from the given iterator range.
Definition bcrsmatrix.hh:1192
void addindex(size_type row, size_type col)
add index (row,col) to the matrix
Definition bcrsmatrix.hh:1149
std::map< std::pair< size_type, size_type >, B > OverflowType
Definition bcrsmatrix.hh:1966
row_type::Iterator ColIterator
Iterator for the entries of each row.
Definition bcrsmatrix.hh:652
FieldTraits< field_type >::real_type frobenius_norm() const
frobenius norm: sqrt(sum over squared values of entries)
Definition bcrsmatrix.hh:1798
A::size_type size_type
The type for the index access and the size.
Definition bcrsmatrix.hh:446
BCRSMatrix & operator/=(const field_type &k)
vector space division by scalar
Definition bcrsmatrix.hh:1469
OverflowType overflow
Definition bcrsmatrix.hh:1967
BCRSMatrix & operator+=(const BCRSMatrix &b)
Add the entries of another matrix to this one.
Definition bcrsmatrix.hh:1502
CreateIterator createend()
get create iterator pointing to one after the last block
Definition bcrsmatrix.hh:1061
FieldTraits< field_type >::real_type frobenius_norm2() const
square of frobenius norm, need for block recursion
Definition bcrsmatrix.hh:1777
Iterator beforeEnd()
Definition bcrsmatrix.hh:636
row_type::ConstIterator ConstColIterator
Const iterator to the entries of a row.
Definition bcrsmatrix.hh:689
A::template rebind< B >::other allocator_
Definition bcrsmatrix.hh:1940
void usmv(F &&alpha, const X &x, Y &y) const
y += alpha A x
Definition bcrsmatrix.hh:1629
CompressedBlockVectorWindow< B, A > row_type
implement row_type with compressed vector
Definition bcrsmatrix.hh:443
size_type getrowsize(size_type i) const
get current number of indices in row i
Definition bcrsmatrix.hh:1086
size_type M() const
number of columns (counted in blocks)
Definition bcrsmatrix.hh:1898
size_type n
Definition bcrsmatrix.hh:1947
CreateIterator createbegin()
get initial create iterator
Definition bcrsmatrix.hh:1055
BuildStage
Definition bcrsmatrix.hh:415
@ rowSizesBuilt
The row sizes of the matrix are known.
Definition bcrsmatrix.hh:426
@ built
The matrix structure is fully built.
Definition bcrsmatrix.hh:428
@ notbuilt
Matrix is not built at all, no memory has been allocated, build mode and size can still be set.
Definition bcrsmatrix.hh:417
@ notAllocated
Matrix is not built at all, no memory has been allocated, build mode and size can still be set.
Definition bcrsmatrix.hh:419
@ building
Matrix is currently being built, some memory has been allocated, build mode and size are fixed.
Definition bcrsmatrix.hh:421
BuildMode buildMode() const
The currently selected build mode of the matrix.
Definition bcrsmatrix.hh:1916
A::template rebind< row_type >::other rowAllocator_
Definition bcrsmatrix.hh:1942
void mmv(const X &x, Y &y) const
y -= A x
Definition bcrsmatrix.hh:1610
FieldTraits< ft >::real_type infinity_norm() const
infinity norm (row sum norm, how to generalize for blocks?)
Definition bcrsmatrix.hh:1806
void mv(const X &x, Y &y) const
y = A x
Definition bcrsmatrix.hh:1569
B block_type
export the type representing the components
Definition bcrsmatrix.hh:437
void mmtv(const X &x, Y &y) const
y -= A^T x
Definition bcrsmatrix.hh:1682
size_type avg
Definition bcrsmatrix.hh:1963
A::template rebind< size_type >::other sizeAllocator_
Definition bcrsmatrix.hh:1944
void umv(const X &x, Y &y) const
y += A x
Definition bcrsmatrix.hh:1591
void implicit_allocate(size_type _n, size_type _m)
organizes allocation implicit mode calculates correct array size to be allocated and sets the the win...
Definition bcrsmatrix.hh:2191
BCRSMatrix(size_type _n, size_type _m, BuildMode bm)
matrix with unknown number of nonzeroes
Definition bcrsmatrix.hh:713
void endindices()
indicate that all indices are defined, check consistency
Definition bcrsmatrix.hh:1206
CompressionStatistics compress()
Finishes the buildstage in implicit mode.
Definition bcrsmatrix.hh:1317
@ blocklevel
The number of blocklevels the matrix contains.
Definition bcrsmatrix.hh:454
void setDataPointers()
Set data pointers for all rows.
Definition bcrsmatrix.hh:2020
size_type N() const
number of rows (counted in blocks)
Definition bcrsmatrix.hh:1892
void setBuildMode(BuildMode bm)
Sets the build mode of the matrix.
Definition bcrsmatrix.hh:789
void setSize(size_type rows, size_type columns, size_type nnz=0)
Set the size of the matrix.
Definition bcrsmatrix.hh:817
std::shared_ptr< size_type > j_
Definition bcrsmatrix.hh:1960
void setWindowPointers(ConstRowIterator row)
Definition bcrsmatrix.hh:1969
BCRSMatrix & operator=(const BCRSMatrix &Mat)
assignment
Definition bcrsmatrix.hh:867
void setColumnPointers(ConstRowIterator row)
Copy row sizes from iterator range starting at row and set column index pointers for all rows.
Definition bcrsmatrix.hh:1994
ConstIterator end() const
Get const iterator to one beyond last row.
Definition bcrsmatrix.hh:666
ConstIterator begin() const
Get const iterator to first row.
Definition bcrsmatrix.hh:660
void setImplicitBuildModeParameters(size_type _avg, double _overflow)
Set parameters needed for creation in implicit build mode.
Definition bcrsmatrix.hh:845
A allocator_type
export the allocator type
Definition bcrsmatrix.hh:440
ConstIterator beforeEnd() const
Definition bcrsmatrix.hh:673
Iterator access to matrix rows
Definition bcrsmatrix.hh:527
RealRowIterator()
empty constructor, use with care!
Definition bcrsmatrix.hh:544
bool equals(const RealRowIterator< ValueType > &other) const
equality
Definition bcrsmatrix.hh:572
std::remove_const< T >::type ValueType
The unqualified value type.
Definition bcrsmatrix.hh:531
RealRowIterator(const RealRowIterator< ValueType > &it)
Definition bcrsmatrix.hh:548
bool equals(const RealRowIterator< const ValueType > &other) const
equality
Definition bcrsmatrix.hh:579
RealRowIterator(row_type *_p, size_type _i)
constructor
Definition bcrsmatrix.hh:539
std::ptrdiff_t distanceTo(const RealRowIterator< const ValueType > &other) const
Definition bcrsmatrix.hh:565
size_type index() const
return index
Definition bcrsmatrix.hh:554
std::ptrdiff_t distanceTo(const RealRowIterator< ValueType > &other) const
Definition bcrsmatrix.hh:559
Iterator class for sequential creation of blocks
Definition bcrsmatrix.hh:918
bool operator==(const CreateIterator &it) const
equality
Definition bcrsmatrix.hh:1010
CreateIterator & operator++()
prefix increment
Definition bcrsmatrix.hh:935
size_type index() const
The number of the row that the iterator currently points to.
Definition bcrsmatrix.hh:1016
bool operator!=(const CreateIterator &it) const
inequality
Definition bcrsmatrix.hh:1004
CreateIterator(BCRSMatrix &_Mat, size_type _i)
constructor
Definition bcrsmatrix.hh:921
void insert(size_type j)
put column index in row
Definition bcrsmatrix.hh:1022
bool contains(size_type j)
return true if column index is in row
Definition bcrsmatrix.hh:1028
size_type size() const
Get the current row size.
Definition bcrsmatrix.hh:1037
Class used by shared_ptr to deallocate memory using the proper allocator.
Definition bcrsmatrix.hh:2107
Deallocator(typename A::template rebind< size_type >::other &sizeAllocator)
Definition bcrsmatrix.hh:2111
void operator()(size_type *p)
Definition bcrsmatrix.hh:2115
Definition bvector.hh:1050
void set(size_type _n, B *_p, size_type *_j)
set size and pointer
Definition bvector.hh:1149
size_type * getindexptr()
get pointer
Definition bvector.hh:1181
void setsize(size_type _n)
set size only
Definition bvector.hh:1157
void setptr(B *_p)
set pointer only
Definition bvector.hh:1163
B * getptr()
get pointer
Definition bvector.hh:1175
size_type getsize() const
get size
Definition bvector.hh:1198
void setindexptr(size_type *_j)
set pointer only
Definition bvector.hh:1169
Error specific to BCRSMatrix.
Definition istlexception.hh:21
The overflow error used during implicit BCRSMatrix construction was exhausted.
Definition istlexception.hh:34
A generic dynamic dense matrix.
Definition matrix.hh:555
A::size_type size_type
Type for indices and sizes.
Definition matrix.hh:571
T block_type
Export the type representing the components.
Definition matrix.hh:562