4 #ifndef DUNE_BCRSMATRIX_HH
5 #define DUNE_BCRSMATRIX_HH
19 #include <dune/common/shared_ptr.hh>
20 #include <dune/common/stdstreams.hh>
21 #include <dune/common/iteratorfacades.hh>
22 #include <dune/common/typetraits.hh>
23 #include <dune/common/ftraits.hh>
24 #include <dune/common/static_assert.hh>
72 struct MatrixDimension;
81 template<
typename size_type>
139 return _m.entry(_i,j);
168 if (m.buildMode() != Matrix::implicit)
169 DUNE_THROW(
BCRSMatrixError,
"You can only create an ImplicitBuilder for a matrix in implicit build mode");
170 if (m.buildStage() != Matrix::building)
171 DUNE_THROW(
BCRSMatrixError,
"You can only create an ImplicitBuilder for a matrix with set size that has not been compressed() yet");
192 if (m.buildStage() != Matrix::notAllocated)
193 DUNE_THROW(
BCRSMatrixError,
"You can only set up a matrix for this ImplicitBuilder if it has no memory allocated yet");
194 m.setBuildMode(Matrix::implicit);
195 m.setImplicitBuildModeParameters(avg_cols_per_row,overflow_fraction);
196 m.setSize(rows,cols);
412 template<
class B,
class A=std::allocator<B> >
501 #ifdef DUNE_ISTL_WITH_CHECKING
503 DUNE_THROW(
BCRSMatrixError,
"You cannot use operator[] in implicit build mode before calling compress()");
506 if (r[i].getptr()==0) DUNE_THROW(
BCRSMatrixError,
"row not initialized yet");
514 #ifdef DUNE_ISTL_WITH_CHECKING
516 DUNE_THROW(
BCRSMatrixError,
"You cannot use operator[] in implicit build mode before calling compress()");
529 :
public RandomAccessIteratorFacade<RealRowIterator<T>, T>
601 void advance(std::ptrdiff_t diff)
606 T& elementAt(std::ptrdiff_t diff)
const
702 allocationSize(0), r(0), a(0),
703 avg(0), overflowsize(-1.0)
708 : build_mode(bm), ready(
notAllocated), n(0), m(0), nnz(0),
709 allocationSize(0), r(0), a(0),
710 avg(0), overflowsize(-1.0)
712 allocate(_n, _m, _nnz,
true,
false);
717 : build_mode(bm), ready(
notAllocated), n(0), m(0), nnz(0),
718 allocationSize(0), r(0), a(0),
719 avg(0), overflowsize(-1.0)
721 allocate(_n, _m,0,
true,
false);
736 : build_mode(bm), ready(
notAllocated), n(0), m(0), nnz(0),
737 allocationSize(0), r(0), a(0),
738 avg(_avg), overflowsize(_overflowsize)
741 DUNE_THROW(
BCRSMatrixError,
"Only call this constructor when using the implicit build mode");
746 if (_overflowsize < 0.0)
747 DUNE_THROW(
BCRSMatrixError,
"You cannot set a negative overflow fraction");
748 implicit_allocate(_n,_m);
757 : build_mode(Mat.build_mode), ready(
notAllocated), n(0), m(0), nnz(0),
758 allocationSize(0), r(0), a(0),
759 avg(Mat.avg), overflowsize(Mat.overflowsize)
762 DUNE_THROW(InvalidStateException,
"BCRSMatrix can only be copy-constructed when source matrix is completely empty (size not set) or fully built)");
776 allocate(Mat.n, Mat.m, _nnz,
true,
true);
779 copyWindowStructure(Mat);
802 DUNE_THROW(InvalidStateException,
"Matrix structure cannot be changed at this stage anymore (ready == "<<ready<<
").");
828 DUNE_THROW(
Dune::BCRSMatrixError,
"number of non-zeroes may not be set in implicit mode, use setImplicitBuildModeParameters() instead");
831 implicit_allocate(rows,columns);
836 allocate(rows, columns, nnz,
true,
false);
855 DUNE_THROW(
BCRSMatrixError,
"You cannot set a negative overflow fraction");
859 DUNE_THROW(InvalidStateException,
"You cannot modify build mode parameters at this stage anymore");
861 overflowsize = _overflow;
873 if (&Mat==
this)
return *
this;
876 DUNE_THROW(InvalidStateException,
"BCRSMatrix can only be copied when both target and source are empty or fully built)");
882 if (n>0 && n!=Mat.n) {
884 for(
row_type *riter=r+(n-1), *rend=r-1; riter!=rend; --riter)
885 rowAllocator_.destroy(riter);
886 rowAllocator_.deallocate(r,n);
898 allocate(Mat.n, Mat.m, nnz, n!=Mat.n,
true);
901 copyWindowStructure(Mat);
910 DUNE_THROW(InvalidStateException,
"Scalar assignment only works on fully built BCRSMatrix)");
924 : Mat(_Mat), i(_i), nnz(0), current_row(nullptr, Mat.j.
get(), 0)
931 DUNE_THROW(
BCRSMatrixError,
"creation only allowed for uninitialized matrix");
933 DUNE_THROW(
BCRSMatrixError,
"creation only allowed if row wise allocation was requested in the constructor");
969 B* a = Mat.allocator_.allocate(s);
973 size_type* j = Mat.sizeAllocator_.allocate(s);
983 for (
typename PatternType::const_iterator it=pattern.begin(); it!=pattern.end(); ++it)
1001 Mat.setDataPointers();
1011 return (i!=it.i) || (&Mat!=&it.Mat);
1017 return (i==it.i) && (&Mat==&it.Mat);
1035 if (pattern.find(j)!=pattern.end())
1047 return pattern.size();
1054 typedef std::set<size_type,std::less<size_type> > PatternType;
1055 PatternType pattern;
1091 #ifdef DUNE_ISTL_WITH_CHECKING
1106 r[i].
setsize(r[i].getsize()+s);
1126 allocate(n,m,total,
false,
false);
1128 DUNE_THROW(
BCRSMatrixError,
"Specified number of nonzeros ("<<nnz<<
") not "
1129 <<
"sufficient for calculated nonzeros ("<<total<<
"! ");
1132 setColumnPointers(
begin());
1161 DUNE_THROW(
BCRSMatrixError,
"matrix size not set and no memory allocated yet");
1171 size_type* pos = std::lower_bound(first,last,col);
1174 if (pos!=last && *pos == col)
return;
1182 std::copy_backward(pos,end,end+1);
1194 template<
typename It>
1201 if ((col_end = std::copy(begin,end,r[row].getindexptr())) != col_begin + row_size)
1204 <<
") does not match number of passed entries (" << (col_end - col_begin) <<
")");
1205 std::sort(col_begin,col_end);
1218 DUNE_THROW(
BCRSMatrixError,
"matrix size not set and no memory allocated yet");
1227 dwarn <<
"WARNING: size of row "<< i.
index()<<
" is "<<j.offset()<<
". But was specified as being "<< (*i).end().offset()
1228 <<
". This means you are wasting valuable space and creating additional cache misses!"<<std::endl;
1229 r[i.index()].
setsize(j.offset());
1258 #ifdef DUNE_ISTL_WITH_CHECKING
1262 DUNE_THROW(
BCRSMatrixError,
"matrix already built up, use operator[] for entry access now");
1264 DUNE_THROW(
BCRSMatrixError,
"matrix size not set and no memory allocated yet");
1266 DUNE_THROW(InvalidStateException,
"You may only use entry() during the 'building' stage");
1277 size_type* pos = std::find(begin, end, col);
1290 if (r[row].getsize() == avg)
1291 return overflow[std::make_pair(row,col)];
1325 DUNE_THROW(
BCRSMatrixError,
"matrix already built up, no more need for compression");
1327 DUNE_THROW(
BCRSMatrixError,
"matrix size not set and no memory allocated yet");
1329 DUNE_THROW(InvalidStateException,
"You may only call compress() at the end of the 'building' stage");
1341 typename OverflowType::iterator oit = overflow.begin();
1344 std::vector<size_type*> perm;
1356 typename std::vector<size_type*>::iterator it = perm.begin();
1357 for (
size_type* iit = begin; iit < begin + size; ++iit, ++it)
1367 for (it = perm.begin(); it != perm.end(); ++it)
1370 while ((oit!=overflow.end()) && (oit->first < std::make_pair(i,**it)))
1375 "Allocated memory for BCRSMatrix exhausted during compress()!"
1376 "Please increase either the average number of entries per row or the overflow fraction."
1379 *jiit = oit->first.second;
1381 *aiit = oit->second;
1384 r[i].
setsize(r[i].getsize()+1);
1390 "Allocated memory for BCRSMatrix exhausted during compress()!"
1391 "Please increase either the average number of entries per row or the overflow fraction."
1397 B* apos = *it-j.get()+a;
1403 while ((oit!=overflow.end()) && (oit->first.first == i))
1408 "Allocated memory for BCRSMatrix exhausted during compress()!"
1409 "Please increase either the average number of entries per row or the overflow fraction."
1413 *jiit = oit->first.second;
1415 *aiit = oit->second;
1418 r[i].
setsize(r[i].getsize()+1);
1422 if (r[i].getsize()>stats.
maximum)
1432 stats.
avg = (double) (nnz) / (double) n;
1433 stats.
mem_ratio = (double) (nnz)/(double) allocationSize;
1446 #ifdef DUNE_ISTL_WITH_CHECKING
1448 DUNE_THROW(
BCRSMatrixError,
"You can only call arithmetic operations on fully built BCRSMatrix instances");
1474 #ifdef DUNE_ISTL_WITH_CHECKING
1476 DUNE_THROW(
BCRSMatrixError,
"You can only call arithmetic operations on fully built BCRSMatrix instances");
1507 #ifdef DUNE_ISTL_WITH_CHECKING
1509 DUNE_THROW(
BCRSMatrixError,
"You can only call arithmetic operations on fully built BCRSMatrix instances");
1510 if(
N()!=b.
N() ||
M() != b.
M())
1511 DUNE_THROW(RangeError,
"Matrix sizes do not match!");
1529 #ifdef DUNE_ISTL_WITH_CHECKING
1531 DUNE_THROW(
BCRSMatrixError,
"You can only call arithmetic operations on fully built BCRSMatrix instances");
1532 if(
N()!=b.
N() ||
M() != b.
M())
1533 DUNE_THROW(RangeError,
"Matrix sizes do not match!");
1554 #ifdef DUNE_ISTL_WITH_CHECKING
1556 DUNE_THROW(
BCRSMatrixError,
"You can only call arithmetic operations on fully built BCRSMatrix instances");
1557 if(
N()!=b.
N() ||
M() != b.
M())
1558 DUNE_THROW(RangeError,
"Matrix sizes do not match!");
1571 template<
class X,
class Y>
1572 void mv (
const X& x, Y& y)
const
1574 #ifdef DUNE_ISTL_WITH_CHECKING
1576 DUNE_THROW(
BCRSMatrixError,
"You can only call arithmetic operations on fully built BCRSMatrix instances");
1578 "Size mismatch: M: " <<
N() <<
"x" <<
M() <<
" x: " << x.N());
1580 "Size mismatch: M: " <<
N() <<
"x" <<
M() <<
" y: " << y.N());
1588 (*j).umv(x[j.index()],y[i.index()]);
1593 template<
class X,
class Y>
1594 void umv (
const X& x, Y& y)
const
1596 #ifdef DUNE_ISTL_WITH_CHECKING
1598 DUNE_THROW(
BCRSMatrixError,
"You can only call arithmetic operations on fully built BCRSMatrix instances");
1607 (*j).umv(x[j.index()],y[i.index()]);
1612 template<
class X,
class Y>
1613 void mmv (
const X& x, Y& y)
const
1615 #ifdef DUNE_ISTL_WITH_CHECKING
1617 DUNE_THROW(
BCRSMatrixError,
"You can only call arithmetic operations on fully built BCRSMatrix instances");
1626 (*j).mmv(x[j.index()],y[i.index()]);
1631 template<
class X,
class Y>
1634 #ifdef DUNE_ISTL_WITH_CHECKING
1636 DUNE_THROW(
BCRSMatrixError,
"You can only call arithmetic operations on fully built BCRSMatrix instances");
1645 (*j).usmv(alpha,x[j.index()],y[i.index()]);
1650 template<
class X,
class Y>
1651 void mtv (
const X& x, Y& y)
const
1653 #ifdef DUNE_ISTL_WITH_CHECKING
1655 DUNE_THROW(
BCRSMatrixError,
"You can only call arithmetic operations on fully built BCRSMatrix instances");
1665 template<
class X,
class Y>
1668 #ifdef DUNE_ISTL_WITH_CHECKING
1670 DUNE_THROW(
BCRSMatrixError,
"You can only call arithmetic operations on fully built BCRSMatrix instances");
1679 (*j).umtv(x[i.index()],y[j.index()]);
1684 template<
class X,
class Y>
1687 #ifdef DUNE_ISTL_WITH_CHECKING
1696 (*j).mmtv(x[i.index()],y[j.index()]);
1701 template<
class X,
class Y>
1704 #ifdef DUNE_ISTL_WITH_CHECKING
1706 DUNE_THROW(
BCRSMatrixError,
"You can only call arithmetic operations on fully built BCRSMatrix instances");
1715 (*j).usmtv(alpha,x[i.index()],y[j.index()]);
1720 template<
class X,
class Y>
1723 #ifdef DUNE_ISTL_WITH_CHECKING
1725 DUNE_THROW(
BCRSMatrixError,
"You can only call arithmetic operations on fully built BCRSMatrix instances");
1734 (*j).umhv(x[i.index()],y[j.index()]);
1739 template<
class X,
class Y>
1742 #ifdef DUNE_ISTL_WITH_CHECKING
1744 DUNE_THROW(
BCRSMatrixError,
"You can only call arithmetic operations on fully built BCRSMatrix instances");
1753 (*j).mmhv(x[i.index()],y[j.index()]);
1758 template<
class X,
class Y>
1761 #ifdef DUNE_ISTL_WITH_CHECKING
1763 DUNE_THROW(
BCRSMatrixError,
"You can only call arithmetic operations on fully built BCRSMatrix instances");
1772 (*j).usmhv(alpha,x[i.index()],y[j.index()]);
1782 #ifdef DUNE_ISTL_WITH_CHECKING
1784 DUNE_THROW(
BCRSMatrixError,
"You can only call arithmetic operations on fully built BCRSMatrix instances");
1794 sum += (*j).frobenius_norm2();
1810 DUNE_THROW(
BCRSMatrixError,
"You can only call arithmetic operations on fully built BCRSMatrix instances");
1819 sum += (*j).infinity_norm();
1820 max = std::max(max,sum);
1828 #ifdef DUNE_ISTL_WITH_CHECKING
1830 DUNE_THROW(
BCRSMatrixError,
"You can only call arithmetic operations on fully built BCRSMatrix instances");
1840 sum += (*j).infinity_norm_real();
1841 max = std::max(max,sum);
1884 #ifdef DUNE_ISTL_WITH_CHECKING
1885 if (i<0 || i>=n) DUNE_THROW(
BCRSMatrixError,
"row index out of range");
1886 if (j<0 || j>=m) DUNE_THROW(
BCRSMatrixError,
"column index out of range");
1888 if (r[i].size() && r[i].find(j)!=r[i].
end())
1901 typename A::template rebind<B>::other allocator_;
1903 typename A::template rebind<row_type>::other rowAllocator_;
1905 typename A::template rebind<size_type>::other sizeAllocator_;
1921 Dune::shared_ptr<size_type> j;
1925 double overflowsize;
1927 typedef std::map<std::pair<size_type,size_type>, B> OverflowType;
1928 OverflowType overflow;
1939 r[i].
set(s,current_row.getptr(), current_row.getindexptr());
1941 current_row.setptr(current_row.getptr()+s);
1942 current_row.setindexptr(current_row.getindexptr()+s);
1981 void setDataPointers()
1986 if (r[i].getsize() > 0) {
2000 void copyWindowStructure(
const BCRSMatrix& Mat)
2002 setWindowPointers(Mat.begin());
2005 for (
size_type i=0; i<n; i++) r[i] = Mat.r[i];
2017 void deallocate(
bool deallocateRows=
true)
2023 if (allocationSize>0)
2029 for(B *aiter=a+(allocationSize-1), *aend=a-1; aiter!=aend; --aiter)
2030 allocator_.destroy(aiter);
2031 allocator_.deallocate(a,allocationSize);
2039 if (r[i].getsize()>0)
2041 for (B *
col=r[i].getptr()+(r[i].getsize()-1),
2042 *colend = r[i].getptr()-1;
col!=colend; --
col) {
2043 allocator_.destroy(
col);
2045 sizeAllocator_.deallocate(r[i].getindexptr(),1);
2046 allocator_.deallocate(r[i].getptr(),1);
2049 r[i].
set(0,
nullptr,
nullptr);
2054 if (n>0 && deallocateRows && r) {
2055 for(
row_type *riter=r+(n-1), *rend=r-1; riter!=rend; --riter)
2056 rowAllocator_.destroy(riter);
2057 rowAllocator_.deallocate(r,n);
2069 typename A::template rebind<size_type>::other& sizeAllocator_;
2072 Deallocator(
typename A::template rebind<size_type>::other& sizeAllocator)
2073 : sizeAllocator_(sizeAllocator)
2076 void operator()(
size_type* p) { sizeAllocator_.deallocate(p,1); }
2102 nnz = allocationSize_;
2103 allocationSize = allocationSize_;
2109 DUNE_THROW(InvalidStateException,
"Rows have already been allocated, cannot allocate a second time");
2110 r = rowAllocator_.allocate(rows);
2119 if (allocationSize>0) {
2122 j.reset(sizeAllocator_.allocate(allocationSize),Deallocator(sizeAllocator_));
2125 for(
row_type* ri=r; ri!=r+rows; ++ri)
2126 rowAllocator_.construct(ri,
row_type());
2136 DUNE_THROW(InvalidStateException,
"Cannot allocate data array (already allocated)");
2137 if (allocationSize>0) {
2138 a = allocator_.allocate(allocationSize);
2141 new (a) B[allocationSize];
2155 DUNE_THROW(InvalidStateException,
"implicit_allocate() may only be called in implicit build mode");
2157 DUNE_THROW(InvalidStateException,
"memory has already been allocated");
2160 if (overflowsize < 0)
2161 DUNE_THROW(InvalidStateException,
"You have to set the implicit build mode parameters before starting to build the matrix");
2164 allocationSize = _n*avg + osize;
2166 allocate(_n, _m, allocationSize,
true,
true);
2170 B* aptr = a + osize;
2173 r[i].
set(0,aptr,jptr);