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> 23 #include <dune/common/nullptr.hh> 80 template<
typename size_type>
138 return _m.entry(_i,j);
167 if (m.buildMode() != Matrix::implicit)
168 DUNE_THROW(
BCRSMatrixError,
"You can only create an ImplicitBuilder for a matrix in implicit build mode");
169 if (m.buildStage() != Matrix::building)
170 DUNE_THROW(
BCRSMatrixError,
"You can only create an ImplicitBuilder for a matrix with set size that has not been compressed() yet");
188 ImplicitMatrixBuilder(Matrix& m, size_type rows, size_type cols, size_type avg_cols_per_row,
double overflow_fraction)
191 if (m.buildStage() != Matrix::notAllocated)
192 DUNE_THROW(
BCRSMatrixError,
"You can only set up a matrix for this ImplicitBuilder if it has no memory allocated yet");
193 m.setBuildMode(Matrix::implicit);
194 m.setImplicitBuildModeParameters(avg_cols_per_row,overflow_fraction);
195 m.setSize(rows,cols);
411 template<
class B,
class A=std::allocator<B> >
455 blocklevel = B::blocklevel+1
498 row_type& operator[] (size_type i)
500 #ifdef DUNE_ISTL_WITH_CHECKING 501 if (build_mode == implicit && ready != built)
502 DUNE_THROW(
BCRSMatrixError,
"You cannot use operator[] in implicit build mode before calling compress()");
510 const row_type& operator[] (size_type i)
const 512 #ifdef DUNE_ISTL_WITH_CHECKING 513 if (build_mode == implicit && ready != built)
514 DUNE_THROW(
BCRSMatrixError,
"You cannot use operator[] in implicit build mode before calling compress()");
515 if (built!=ready) DUNE_THROW(
BCRSMatrixError,
"row not initialized yet");
527 :
public RandomAccessIteratorFacade<RealRowIterator<T>, T>
534 friend class RandomAccessIteratorFacade<
RealRowIterator<const ValueType>, const ValueType>;
599 void advance(std::ptrdiff_t diff)
604 T& elementAt(std::ptrdiff_t diff)
const 610 row_type& dereference ()
const 626 return Iterator(r,0);
632 return Iterator(r,n);
639 return Iterator(r,n-1);
646 return Iterator(r,-1);
663 return ConstIterator(r,0);
667 ConstIterator
end ()
const 669 return ConstIterator(r,n);
676 return ConstIterator(r,n-1);
683 return ConstIterator(r,-1);
699 : build_mode(unknown), ready(notAllocated), n(0), m(0), nnz_(0),
700 allocationSize(0), r(0), a(0),
701 avg(0), overflowsize(-1.0)
706 : build_mode(bm), ready(notAllocated), n(0), m(0), nnz_(0),
707 allocationSize(0), r(0), a(0),
708 avg(0), overflowsize(-1.0)
710 allocate(_n, _m, _nnz,
true,
false);
715 : build_mode(bm), ready(notAllocated), n(0), m(0), nnz_(0),
716 allocationSize(0), r(0), a(0),
717 avg(0), overflowsize(-1.0)
719 allocate(_n, _m,0,
true,
false);
734 : build_mode(bm), ready(notAllocated), n(0), m(0), nnz_(0),
735 allocationSize(0), r(0), a(0),
736 avg(_avg), overflowsize(_overflowsize)
739 DUNE_THROW(
BCRSMatrixError,
"Only call this constructor when using the implicit build mode");
744 if (_overflowsize < 0.0)
745 DUNE_THROW(
BCRSMatrixError,
"You cannot set a negative overflow fraction");
746 implicit_allocate(_n,_m);
755 : build_mode(Mat.build_mode), ready(notAllocated), n(0), m(0), nnz_(0),
756 allocationSize(0), r(0), a(0),
757 avg(Mat.
avg), overflowsize(Mat.overflowsize)
759 if (!(Mat.
ready == notAllocated || Mat.
ready == built))
760 DUNE_THROW(InvalidStateException,
"BCRSMatrix can only be copy-constructed when source matrix is completely empty (size not set) or fully built)");
763 size_type _nnz = Mat.
nnz_;
769 for (size_type i=0; i<Mat.
n; i++)
774 allocate(Mat.
n, Mat.
m, _nnz,
true,
true);
777 copyWindowStructure(Mat);
792 if (ready == notAllocated)
797 if (ready == building && (build_mode == unknown || build_mode == random || build_mode == row_wise) && (bm == row_wise || bm == random))
800 DUNE_THROW(InvalidStateException,
"Matrix structure cannot be changed at this stage anymore (ready == "<<ready<<
").");
818 void setSize(size_type rows, size_type columns, size_type nnz=0)
823 if (build_mode == implicit)
826 DUNE_THROW(
Dune::BCRSMatrixError,
"number of non-zeroes may not be set in implicit mode, use setImplicitBuildModeParameters() instead");
829 implicit_allocate(rows,columns);
834 allocate(rows, columns, nnz,
true,
false);
853 DUNE_THROW(
BCRSMatrixError,
"You cannot set a negative overflow fraction");
856 if (ready != notAllocated)
857 DUNE_THROW(InvalidStateException,
"You cannot modify build mode parameters at this stage anymore");
859 overflowsize = _overflow;
871 if (&Mat==
this)
return *
this;
873 if (!((ready == notAllocated || ready == built) && (Mat.
ready == notAllocated || Mat.
ready == built)))
874 DUNE_THROW(InvalidStateException,
"BCRSMatrix can only be copied when both target and source are empty or fully built)");
878 deallocate(n!=Mat.
n);
881 if (n>0 && n!=Mat.
n) {
883 for(row_type *riter=r+(n-1), *rend=r-1; riter!=rend; --riter)
884 rowAllocator_.destroy(riter);
885 rowAllocator_.deallocate(r,n);
891 for (size_type i=0; i<Mat.
n; i++)
897 allocate(Mat.
n, Mat.
m, nnz_, n!=Mat.
n,
true);
900 copyWindowStructure(Mat);
908 if (!(ready == notAllocated || ready == built))
909 DUNE_THROW(InvalidStateException,
"Scalar assignment only works on fully built BCRSMatrix)");
911 for (size_type i=0; i<n; i++) r[i] = k;
923 : Mat(_Mat), i(_i), nnz(0), current_row(nullptr, Mat.j_.
get(), 0)
925 if (Mat.build_mode == unknown && Mat.ready == building)
927 Mat.build_mode = row_wise;
929 if (i==0 && Mat.ready != building)
930 DUNE_THROW(
BCRSMatrixError,
"creation only allowed for uninitialized matrix");
931 if(Mat.build_mode!=row_wise)
932 DUNE_THROW(
BCRSMatrixError,
"creation only allowed if row wise allocation was requested in the constructor");
939 if (Mat.ready != building)
963 Mat.r[i].set(s,
nullptr,current_row.getindexptr());
964 current_row.setindexptr(current_row.getindexptr()+s);
968 B* b = Mat.allocator_.allocate(s);
972 size_type* j = Mat.sizeAllocator_.allocate(s);
981 size_type *j = Mat.r[i].getindexptr();
982 for (
typename PatternType::const_iterator it=
pattern.begin(); it!=
pattern.end(); ++it)
1000 Mat.setDataPointers();
1010 return (i!=it.i) || (&Mat!=&it.Mat);
1016 return (i==it.i) && (&Mat==&it.Mat);
1053 typedef std::set<size_type,std::less<size_type> > PatternType;
1055 row_type current_row;
1079 if (build_mode!=random)
1081 if (ready != building)
1090 #ifdef DUNE_ISTL_WITH_CHECKING 1094 return r[i].getsize();
1100 if (build_mode!=random)
1102 if (ready != building)
1105 r[i].setsize(r[i].getsize()+s);
1111 if (build_mode!=random)
1113 if (ready != building)
1118 for (size_type i=0; i<n; i++)
1120 total += r[i].getsize();
1125 allocate(n,m,total,
false,
false);
1126 else if(nnz_ < total)
1127 DUNE_THROW(
BCRSMatrixError,
"Specified number of nonzeros ("<<nnz_<<
") not " 1128 <<
"sufficient for calculated nonzeros ("<<total<<
"! ");
1131 setColumnPointers(begin());
1135 for (size_type k=0; k<nnz_; k++)
1137 ready = rowSizesBuilt;
1153 if (build_mode!=random)
1157 if (ready==building)
1159 if (ready==notAllocated)
1160 DUNE_THROW(
BCRSMatrixError,
"matrix size not set and no memory allocated yet");
1166 size_type*
const first = r[row].getindexptr();
1167 size_type*
const last = first + r[row].getsize();
1170 size_type* pos = std::lower_bound(first,last,col);
1173 if (pos!=last && *pos == col)
return;
1176 size_type* end = std::lower_bound(pos,last,m);
1181 std::copy_backward(pos,end,end+1);
1193 template<
typename It>
1196 size_type row_size = r[row].size();
1197 size_type* col_begin = r[row].getindexptr();
1200 if ((col_end = std::copy(begin,end,r[row].getindexptr())) != col_begin + row_size)
1203 <<
") does not match number of passed entries (" << (col_end - col_begin) <<
")");
1204 std::sort(col_begin,col_end);
1210 if (build_mode!=random)
1214 if (ready==building)
1216 if (ready==notAllocated)
1217 DUNE_THROW(
BCRSMatrixError,
"matrix size not set and no memory allocated yet");
1220 RowIterator endi=end();
1221 for (RowIterator i=begin(); i!=endi; ++i)
1223 ColIterator endj = (*i).end();
1224 for (ColIterator j=(*i).begin(); j!=endj; ++j) {
1225 if (j.index() >= m) {
1226 dwarn <<
"WARNING: size of row "<< i.index()<<
" is "<<j.offset()<<
". But was specified as being "<< (*i).end().offset()
1227 <<
". This means you are wasting valuable space and creating additional cache misses!"<<std::endl;
1228 r[i.index()].setsize(j.offset());
1257 #ifdef DUNE_ISTL_WITH_CHECKING 1258 if (build_mode!=implicit)
1261 DUNE_THROW(
BCRSMatrixError,
"matrix already built up, use operator[] for entry access now");
1262 if (ready==notAllocated)
1263 DUNE_THROW(
BCRSMatrixError,
"matrix size not set and no memory allocated yet");
1264 if (ready!=building)
1265 DUNE_THROW(InvalidStateException,
"You may only use entry() during the 'building' stage");
1273 size_type* begin = r[row].getindexptr();
1274 size_type* end = begin + r[row].getsize();
1276 size_type* pos = std::find(begin, end, col);
1282 std::ptrdiff_t offset = pos - r[row].getindexptr();
1283 B* aptr = r[row].getptr() + offset;
1289 if (r[row].getsize() ==
avg)
1290 return overflow[std::make_pair(row,col)];
1297 std::ptrdiff_t offset = end - r[row].getindexptr();
1298 B* apos = r[row].getptr() + offset;
1301 r[row].setsize(r[row].getsize()+1);
1321 if (build_mode!=implicit)
1324 DUNE_THROW(
BCRSMatrixError,
"matrix already built up, no more need for compression");
1325 if (ready==notAllocated)
1326 DUNE_THROW(
BCRSMatrixError,
"matrix size not set and no memory allocated yet");
1327 if (ready!=building)
1328 DUNE_THROW(InvalidStateException,
"You may only call compress() at the end of the 'building' stage");
1331 CompressionStatistics stats;
1336 size_type* jiit = j_.get();
1340 typename OverflowType::iterator oit = overflow.begin();
1343 std::vector<size_type*> perm;
1346 for (size_type i=0; i<n; i++)
1349 size_type* begin = r[i].getindexptr();
1351 size_type size = r[i].getsize();
1355 typename std::vector<size_type*>::iterator it = perm.begin();
1356 for (size_type* iit = begin; iit < begin + size; ++iit, ++it)
1363 r[i].setindexptr(jiit);
1366 for (it = perm.begin(); it != perm.end(); ++it)
1369 while ((oit!=overflow.end()) && (oit->first < std::make_pair(i,**it)))
1374 "Allocated memory for BCRSMatrix exhausted during compress()!" 1375 "Please increase either the average number of entries per row or the overflow fraction." 1378 *jiit = oit->first.second;
1380 *aiit = oit->second;
1383 r[i].setsize(r[i].getsize()+1);
1389 "Allocated memory for BCRSMatrix exhausted during compress()!" 1390 "Please increase either the average number of entries per row or the overflow fraction." 1396 B* apos = *it - j_.get() + a;
1402 while ((oit!=overflow.end()) && (oit->first.first == i))
1407 "Allocated memory for BCRSMatrix exhausted during compress()!" 1408 "Please increase either the average number of entries per row or the overflow fraction." 1412 *jiit = oit->first.second;
1414 *aiit = oit->second;
1417 r[i].setsize(r[i].getsize()+1);
1421 if (r[i].getsize()>stats.
maximum)
1422 stats.
maximum = r[i].getsize();
1429 std::ptrdiff_t diff = (r[n-1].getindexptr() + r[n-1].getsize() - j_.get());
1431 stats.
avg = (double) (nnz_) / (double) n;
1432 stats.
mem_ratio = (double) (nnz_) / (double) allocationSize;
1445 #ifdef DUNE_ISTL_WITH_CHECKING 1447 DUNE_THROW(
BCRSMatrixError,
"You can only call arithmetic operations on fully built BCRSMatrix instances");
1453 for (size_type i=0; i<nnz_; i++)
1458 RowIterator endi=end();
1459 for (RowIterator i=begin(); i!=endi; ++i)
1461 ColIterator endj = (*i).end();
1462 for (ColIterator j=(*i).begin(); j!=endj; ++j)
1473 #ifdef DUNE_ISTL_WITH_CHECKING 1475 DUNE_THROW(
BCRSMatrixError,
"You can only call arithmetic operations on fully built BCRSMatrix instances");
1481 for (size_type i=0; i<nnz_; i++)
1486 RowIterator endi=end();
1487 for (RowIterator i=begin(); i!=endi; ++i)
1489 ColIterator endj = (*i).end();
1490 for (ColIterator j=(*i).begin(); j!=endj; ++j)
1506 #ifdef DUNE_ISTL_WITH_CHECKING 1507 if (ready != built || b.
ready != built)
1508 DUNE_THROW(
BCRSMatrixError,
"You can only call arithmetic operations on fully built BCRSMatrix instances");
1509 if(N()!=b.
N() || M() != b.
M())
1510 DUNE_THROW(RangeError,
"Matrix sizes do not match!");
1512 RowIterator endi=end();
1513 ConstRowIterator j=b.
begin();
1514 for (RowIterator i=begin(); i!=endi; ++i, ++j) {
1528 #ifdef DUNE_ISTL_WITH_CHECKING 1529 if (ready != built || b.
ready != built)
1530 DUNE_THROW(
BCRSMatrixError,
"You can only call arithmetic operations on fully built BCRSMatrix instances");
1531 if(N()!=b.
N() || M() != b.
M())
1532 DUNE_THROW(RangeError,
"Matrix sizes do not match!");
1534 RowIterator endi=end();
1535 ConstRowIterator j=b.
begin();
1536 for (RowIterator i=begin(); i!=endi; ++i, ++j) {
1553 #ifdef DUNE_ISTL_WITH_CHECKING 1554 if (ready != built || b.
ready != built)
1555 DUNE_THROW(
BCRSMatrixError,
"You can only call arithmetic operations on fully built BCRSMatrix instances");
1556 if(N()!=b.
N() || M() != b.
M())
1557 DUNE_THROW(RangeError,
"Matrix sizes do not match!");
1559 RowIterator endi=end();
1560 ConstRowIterator j=b.
begin();
1561 for(RowIterator i=begin(); i!=endi; ++i, ++j)
1570 template<
class X,
class Y>
1571 void mv (
const X& x, Y& y)
const 1573 #ifdef DUNE_ISTL_WITH_CHECKING 1575 DUNE_THROW(
BCRSMatrixError,
"You can only call arithmetic operations on fully built BCRSMatrix instances");
1577 "Size mismatch: M: " << N() <<
"x" << M() <<
" x: " << x.N());
1579 "Size mismatch: M: " << N() <<
"x" << M() <<
" y: " << y.N());
1581 ConstRowIterator endi=end();
1582 for (ConstRowIterator i=begin(); i!=endi; ++i)
1585 ConstColIterator endj = (*i).end();
1586 for (ConstColIterator j=(*i).begin(); j!=endj; ++j)
1587 (*j).umv(x[j.index()],y[i.index()]);
1592 template<
class X,
class Y>
1593 void umv (
const X& x, Y& y)
const 1595 #ifdef DUNE_ISTL_WITH_CHECKING 1597 DUNE_THROW(
BCRSMatrixError,
"You can only call arithmetic operations on fully built BCRSMatrix instances");
1601 ConstRowIterator endi=end();
1602 for (ConstRowIterator i=begin(); i!=endi; ++i)
1604 ConstColIterator endj = (*i).end();
1605 for (ConstColIterator j=(*i).begin(); j!=endj; ++j)
1606 (*j).umv(x[j.index()],y[i.index()]);
1611 template<
class X,
class Y>
1612 void mmv (
const X& x, Y& y)
const 1614 #ifdef DUNE_ISTL_WITH_CHECKING 1616 DUNE_THROW(
BCRSMatrixError,
"You can only call arithmetic operations on fully built BCRSMatrix instances");
1620 ConstRowIterator endi=end();
1621 for (ConstRowIterator i=begin(); i!=endi; ++i)
1623 ConstColIterator endj = (*i).end();
1624 for (ConstColIterator j=(*i).begin(); j!=endj; ++j)
1625 (*j).mmv(x[j.index()],y[i.index()]);
1630 template<
typename F,
class X,
class Y>
1631 void usmv (F&& alpha,
const X& x, Y& y)
const 1633 #ifdef DUNE_ISTL_WITH_CHECKING 1635 DUNE_THROW(
BCRSMatrixError,
"You can only call arithmetic operations on fully built BCRSMatrix instances");
1639 ConstRowIterator endi=end();
1640 for (ConstRowIterator i=begin(); i!=endi; ++i)
1642 ConstColIterator endj = (*i).end();
1643 for (ConstColIterator j=(*i).begin(); j!=endj; ++j)
1644 (*j).usmv(alpha,x[j.index()],y[i.index()]);
1649 template<
class X,
class Y>
1650 void mtv (
const X& x, Y& y)
const 1652 #ifdef DUNE_ISTL_WITH_CHECKING 1654 DUNE_THROW(
BCRSMatrixError,
"You can only call arithmetic operations on fully built BCRSMatrix instances");
1658 for(size_type i=0; i<y.N(); ++i)
1664 template<
class X,
class Y>
1667 #ifdef DUNE_ISTL_WITH_CHECKING 1669 DUNE_THROW(
BCRSMatrixError,
"You can only call arithmetic operations on fully built BCRSMatrix instances");
1673 ConstRowIterator endi=end();
1674 for (ConstRowIterator i=begin(); i!=endi; ++i)
1676 ConstColIterator endj = (*i).end();
1677 for (ConstColIterator j=(*i).begin(); j!=endj; ++j)
1678 (*j).umtv(x[i.index()],y[j.index()]);
1683 template<
class X,
class Y>
1686 #ifdef DUNE_ISTL_WITH_CHECKING 1690 ConstRowIterator endi=end();
1691 for (ConstRowIterator i=begin(); i!=endi; ++i)
1693 ConstColIterator endj = (*i).end();
1694 for (ConstColIterator j=(*i).begin(); j!=endj; ++j)
1695 (*j).mmtv(x[i.index()],y[j.index()]);
1700 template<
class X,
class Y>
1701 void usmtv (
const field_type& alpha,
const X& x, Y& y)
const 1703 #ifdef DUNE_ISTL_WITH_CHECKING 1705 DUNE_THROW(
BCRSMatrixError,
"You can only call arithmetic operations on fully built BCRSMatrix instances");
1709 ConstRowIterator endi=end();
1710 for (ConstRowIterator i=begin(); i!=endi; ++i)
1712 ConstColIterator endj = (*i).end();
1713 for (ConstColIterator j=(*i).begin(); j!=endj; ++j)
1714 (*j).usmtv(alpha,x[i.index()],y[j.index()]);
1719 template<
class X,
class Y>
1722 #ifdef DUNE_ISTL_WITH_CHECKING 1724 DUNE_THROW(
BCRSMatrixError,
"You can only call arithmetic operations on fully built BCRSMatrix instances");
1728 ConstRowIterator endi=end();
1729 for (ConstRowIterator i=begin(); i!=endi; ++i)
1731 ConstColIterator endj = (*i).end();
1732 for (ConstColIterator j=(*i).begin(); j!=endj; ++j)
1733 (*j).umhv(x[i.index()],y[j.index()]);
1738 template<
class X,
class Y>
1741 #ifdef DUNE_ISTL_WITH_CHECKING 1743 DUNE_THROW(
BCRSMatrixError,
"You can only call arithmetic operations on fully built BCRSMatrix instances");
1747 ConstRowIterator endi=end();
1748 for (ConstRowIterator i=begin(); i!=endi; ++i)
1750 ConstColIterator endj = (*i).end();
1751 for (ConstColIterator j=(*i).begin(); j!=endj; ++j)
1752 (*j).mmhv(x[i.index()],y[j.index()]);
1757 template<
class X,
class Y>
1758 void usmhv (
const field_type& alpha,
const X& x, Y& y)
const 1760 #ifdef DUNE_ISTL_WITH_CHECKING 1762 DUNE_THROW(
BCRSMatrixError,
"You can only call arithmetic operations on fully built BCRSMatrix instances");
1766 ConstRowIterator endi=end();
1767 for (ConstRowIterator i=begin(); i!=endi; ++i)
1769 ConstColIterator endj = (*i).end();
1770 for (ConstColIterator j=(*i).begin(); j!=endj; ++j)
1771 (*j).usmhv(alpha,x[i.index()],y[j.index()]);
1781 #ifdef DUNE_ISTL_WITH_CHECKING 1783 DUNE_THROW(
BCRSMatrixError,
"You can only call arithmetic operations on fully built BCRSMatrix instances");
1788 ConstRowIterator endi=end();
1789 for (ConstRowIterator i=begin(); i!=endi; ++i)
1791 ConstColIterator endj = (*i).end();
1792 for (ConstColIterator j=(*i).begin(); j!=endj; ++j)
1793 sum += (*j).frobenius_norm2();
1802 return sqrt(frobenius_norm2());
1809 DUNE_THROW(
BCRSMatrixError,
"You can only call arithmetic operations on fully built BCRSMatrix instances");
1812 ConstRowIterator endi=end();
1813 for (ConstRowIterator i=begin(); i!=endi; ++i)
1816 ConstColIterator endj = (*i).end();
1817 for (ConstColIterator j=(*i).begin(); j!=endj; ++j)
1818 sum += (*j).infinity_norm();
1819 max = std::max(max,sum);
1827 #ifdef DUNE_ISTL_WITH_CHECKING 1829 DUNE_THROW(
BCRSMatrixError,
"You can only call arithmetic operations on fully built BCRSMatrix instances");
1833 ConstRowIterator endi=end();
1834 for (ConstRowIterator i=begin(); i!=endi; ++i)
1837 ConstColIterator endj = (*i).end();
1838 for (ConstColIterator j=(*i).begin(); j!=endj; ++j)
1839 sum += (*j).infinity_norm_real();
1840 max = std::max(max,sum);
1849 size_type
N ()
const 1855 size_type
M ()
const 1883 #ifdef DUNE_ISTL_WITH_CHECKING 1884 if (i<0 || i>=n) DUNE_THROW(
BCRSMatrixError,
"row index out of range");
1885 if (j<0 || j>=m) DUNE_THROW(
BCRSMatrixError,
"column index out of range");
1887 if (r[i].size() && r[i].find(j)!=r[i].end())
1920 std::shared_ptr<size_type>
j_;
1931 row_type current_row(a,j_.get(),0);
1932 for (size_type i=0; i<n; i++, ++row) {
1934 size_type s = row->getsize();
1938 r[i].
set(s,current_row.getptr(), current_row.getindexptr());
1940 current_row.setptr(current_row.getptr()+s);
1941 current_row.setindexptr(current_row.getindexptr()+s);
1956 size_type* jptr = j_.get();
1957 for (size_type i=0; i<n; ++i, ++row) {
1959 size_type s = row->getsize();
1983 for (size_type i=0; i<n; ++i) {
1985 if (r[i].getsize() > 0) {
2001 setWindowPointers(Mat.
begin());
2004 for (size_type i=0; i<n; i++) r[i] = Mat.
r[i];
2007 build_mode = row_wise;
2022 if (allocationSize>0)
2028 for(B *aiter=a+(allocationSize-1), *aend=a-1; aiter!=aend; --aiter)
2029 allocator_.destroy(aiter);
2030 allocator_.deallocate(a,allocationSize);
2037 for (size_type i=0; i<n; i++)
2038 if (r[i].getsize()>0)
2040 for (B *
col=r[i].getptr()+(r[i].getsize()-1),
2041 *colend = r[i].getptr()-1;
col!=colend; --
col) {
2042 allocator_.destroy(
col);
2044 sizeAllocator_.deallocate(r[i].getindexptr(),1);
2045 allocator_.deallocate(r[i].getptr(),1);
2048 r[i].
set(0,
nullptr,
nullptr);
2053 if (n>0 && deallocateRows && r) {
2054 for(row_type *riter=r+(n-1), *rend=r-1; riter!=rend; --riter)
2055 rowAllocator_.destroy(riter);
2056 rowAllocator_.deallocate(r,n);
2068 typename A::template rebind<size_type>::other& sizeAllocator_;
2071 Deallocator(
typename A::template rebind<size_type>::other& sizeAllocator)
2072 : sizeAllocator_(sizeAllocator)
2075 void operator()(size_type* p) { sizeAllocator_.deallocate(p,1); }
2096 void allocate(size_type rows, size_type columns, size_type allocationSize_,
bool allocateRows,
bool allocate_data)
2101 nnz_ = allocationSize_;
2102 allocationSize = allocationSize_;
2108 DUNE_THROW(InvalidStateException,
"Rows have already been allocated, cannot allocate a second time");
2109 r = rowAllocator_.allocate(rows);
2118 if (allocationSize>0) {
2121 j_.reset(sizeAllocator_.allocate(allocationSize),
Deallocator(sizeAllocator_));
2124 for(row_type* ri=r; ri!=r+rows; ++ri)
2125 rowAllocator_.construct(ri, row_type());
2135 DUNE_THROW(InvalidStateException,
"Cannot allocate data array (already allocated)");
2136 if (allocationSize>0) {
2137 a = allocator_.allocate(allocationSize);
2140 new (a) B[allocationSize];
2153 if (build_mode != implicit)
2154 DUNE_THROW(InvalidStateException,
"implicit_allocate() may only be called in implicit build mode");
2155 if (ready != notAllocated)
2156 DUNE_THROW(InvalidStateException,
"memory has already been allocated");
2159 if (overflowsize < 0)
2160 DUNE_THROW(InvalidStateException,
"You have to set the implicit build mode parameters before starting to build the matrix");
2162 size_type osize = (size_type) (_n*avg)*overflowsize + 4*
avg;
2163 allocationSize = _n*avg + osize;
2165 allocate(_n, _m, allocationSize,
true,
true);
2168 size_type* jptr = j_.get() + osize;
2169 B* aptr = a + osize;
2170 for (size_type i=0; i<n; i++)
2172 r[i].
set(0,aptr,jptr);
void usmv(F &&alpha, const X &x, Y &y) const
y += alpha A x
Definition: bcrsmatrix.hh:1631
void setWindowPointers(ConstRowIterator row)
Definition: bcrsmatrix.hh:1929
void umtv(const X &x, Y &y) const
y += A^T x
Definition: bcrsmatrix.hh:1665
void copyWindowStructure(const BCRSMatrix &Mat)
Copy the window structure from another matrix.
Definition: bcrsmatrix.hh:1999
void usmtv(const field_type &alpha, const X &x, Y &y) const
y += alpha A^T x
Definition: bcrsmatrix.hh:1701
size_type nnz_
Definition: bcrsmatrix.hh:1909
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:733
B * a
Definition: bcrsmatrix.hh:1917
M_ Matrix
The underlying matrix.
Definition: bcrsmatrix.hh:116
void setsize(size_type _n)
set size only
Definition: bvector.hh:1032
ConstIterator beforeBegin() const
Definition: bcrsmatrix.hh:681
void allocateData()
Definition: bcrsmatrix.hh:2132
size_type getrowsize(size_type i) const
get current number of indices in row i
Definition: bcrsmatrix.hh:1088
remove_const< T >::type ValueType
The unqualified value type.
Definition: bcrsmatrix.hh:532
void setBuildMode(BuildMode bm)
Sets the build mode of the matrix.
Definition: bcrsmatrix.hh:790
Deallocator(typename A::template rebind< size_type >::other &sizeAllocator)
Definition: bcrsmatrix.hh:2071
RealRowIterator< row_type > Iterator
Definition: bcrsmatrix.hh:621
OverflowType overflow
Definition: bcrsmatrix.hh:1927
::Dune::CompressionStatistics< size_type > CompressionStatistics
The type for the statistics object returned by compress()
Definition: bcrsmatrix.hh:450
void umv(const X &x, Y &y) const
y += A x
Definition: bcrsmatrix.hh:1593
void incrementrowsize(size_type i, size_type s=1)
increment size of row i by s (1 by default)
Definition: bcrsmatrix.hh:1098
std::ptrdiff_t distanceTo(const RealRowIterator< ValueType > &other) const
Definition: bcrsmatrix.hh:560
ConstIterator begin() const
Get const iterator to first row.
Definition: bcrsmatrix.hh:661
Definition: bvector.hh:924
Error specific to BCRSMatrix.
Definition: istlexception.hh:19
size_type index() const
dereferencing
Definition: bcrsmatrix.hh:1020
CreateIterator(BCRSMatrix &_Mat, size_type _i)
constructor
Definition: bcrsmatrix.hh:922
Build entries randomly with an educated guess on entries per row.
Definition: bcrsmatrix.hh:488
Iterator access to matrix rows
Definition: bcrsmatrix.hh:526
RealRowIterator< const row_type > const_iterator
The const iterator over the matrix rows.
Definition: bcrsmatrix.hh:656
ConstIterator ConstRowIterator
rename the const row iterator for easier access
Definition: bcrsmatrix.hh:687
void insert(size_type j)
put column index in row
Definition: bcrsmatrix.hh:1026
iterator class for sequential access
Definition: basearray.hh:580
bool equals(const RealRowIterator< ValueType > &other) const
equality
Definition: bcrsmatrix.hh:573
void umhv(const X &x, Y &y) const
y += A^H x
Definition: bcrsmatrix.hh:1720
std::shared_ptr< size_type > j_
Definition: bcrsmatrix.hh:1920
Iterator RowIterator
rename the iterators for easier access
Definition: bcrsmatrix.hh:650
size_type m
Definition: bcrsmatrix.hh:1908
void setDataPointers()
Set data pointers for all rows.
Definition: bcrsmatrix.hh:1980
Iterator beforeBegin()
Definition: bcrsmatrix.hh:644
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:188
CreateIterator createend()
get create iterator pointing to one after the last block
Definition: bcrsmatrix.hh:1068
size_type N() const
The number of rows in the matrix.
Definition: bcrsmatrix.hh:205
BCRSMatrix(const BCRSMatrix &Mat)
copy constructor
Definition: bcrsmatrix.hh:754
RealRowIterator(const RealRowIterator< ValueType > &it)
Definition: bcrsmatrix.hh:549
BuildMode
we support two modes
Definition: bcrsmatrix.hh:459
Iterator begin()
Get iterator to first row.
Definition: bcrsmatrix.hh:624
CompressedBlockVectorWindow< B, A > row_type
implement row_type with compressed vector
Definition: bcrsmatrix.hh:444
B block_type
export the type representing the components
Definition: bcrsmatrix.hh:438
void setptr(B *_p)
set pointer only
Definition: bvector.hh:1038
BuildStage ready
Definition: bcrsmatrix.hh:1897
Proxy row object for entry access.
Definition: bcrsmatrix.hh:130
size_type getsize() const
get size
Definition: bvector.hh:1073
void endrowsizes()
indicate that size of all rows is defined
Definition: bcrsmatrix.hh:1109
row_type * r
Definition: bcrsmatrix.hh:1914
void mmtv(const X &x, Y &y) const
y -= A^T x
Definition: bcrsmatrix.hh:1684
block_type & operator[](size_type j) const
Returns entry in column j.
Definition: bcrsmatrix.hh:136
double overflowsize
Definition: bcrsmatrix.hh:1924
void mmv(const X &x, Y &y) const
y -= A x
Definition: bcrsmatrix.hh:1612
T block_type
Export the type representing the components.
Definition: matrix.hh:32
void mtv(const X &x, Y &y) const
y = A^T x
Definition: bcrsmatrix.hh:1650
std::map< std::pair< size_type, size_type >, B > OverflowType
Definition: bcrsmatrix.hh:1926
A::template rebind< B >::other allocator_
Definition: bcrsmatrix.hh:1900
B & entry(size_type row, size_type col)
Returns reference to entry (row,col) of the matrix.
Definition: bcrsmatrix.hh:1255
BCRSMatrix & axpy(field_type alpha, const BCRSMatrix &b)
Add the scaled entries of another matrix to this one.
Definition: bcrsmatrix.hh:1551
size_type n
Definition: bcrsmatrix.hh:1907
row_type::Iterator ColIterator
Iterator for the entries of each row.
Definition: bcrsmatrix.hh:653
This file implements a vector space as a tensor product of a given vector space. The number of compon...
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:2151
row_object operator[](size_type i) const
Returns a proxy for entries in row i.
Definition: bcrsmatrix.hh:199
size_type avg
Definition: bcrsmatrix.hh:1923
Definition: matrixmarket.hh:258
BCRSMatrix()
an empty matrix
Definition: bcrsmatrix.hh:698
BuildStage
Definition: bcrsmatrix.hh:416
size_type M() const
number of columns (counted in blocks)
Definition: bcrsmatrix.hh:1855
A sparse block matrix with compressed row storage.
Definition: bcrsmatrix.hh:412
size_type M() const
The number of columns in the matrix.
Definition: bcrsmatrix.hh:211
BuildStage buildStage() const
The current build stage of the matrix.
Definition: bcrsmatrix.hh:1867
The overflow error used during implicit BCRSMatrix construction was exhausted.
Definition: istlexception.hh:32
BuildMode buildMode() const
The currently selected build mode of the matrix.
Definition: bcrsmatrix.hh:1873
RealRowIterator< const row_type > ConstIterator
Definition: bcrsmatrix.hh:657
Class used by shared_ptr to deallocate memory using the proper allocator.
Definition: bcrsmatrix.hh:2066
A::size_type size_type
The type for the index access and the size.
Definition: bcrsmatrix.hh:447
B::field_type field_type
export the type representing the field
Definition: bcrsmatrix.hh:435
A::template rebind< size_type >::other sizeAllocator_
Definition: bcrsmatrix.hh:1904
size_type overflow_total
total number of elements written to the overflow area during construction.
Definition: bcrsmatrix.hh:88
size_type nonzeroes() const
number of blocks that are stored (the number of blocks that possibly are nonzero) ...
Definition: bcrsmatrix.hh:1861
FieldTraits< field_type >::real_type infinity_norm_real() const
simplified infinity norm (uses Manhattan norm for complex values)
Definition: bcrsmatrix.hh:1825
void addindex(size_type row, size_type col)
add index (row,col) to the matrix
Definition: bcrsmatrix.hh:1151
void usmhv(const field_type &alpha, const X &x, Y &y) const
y += alpha A^H x
Definition: bcrsmatrix.hh:1758
CreateIterator createbegin()
get initial create iterator
Definition: bcrsmatrix.hh:1062
A::template rebind< row_type >::other rowAllocator_
Definition: bcrsmatrix.hh:1902
Definition: matrixutils.hh:513
void setImplicitBuildModeParameters(size_type _avg, double _overflow)
Set parameters needed for creation in implicit build mode.
Definition: bcrsmatrix.hh:846
Build in a row-wise manner.
Definition: bcrsmatrix.hh:470
Matrix::size_type size_type
The size_type of the underlying matrix.
Definition: bcrsmatrix.hh:122
RealRowIterator()
empty constructor, use with care!
Definition: bcrsmatrix.hh:545
A allocator_type
export the allocator type
Definition: bcrsmatrix.hh:441
RealRowIterator(row_type *_p, size_type _i)
constructor
Definition: bcrsmatrix.hh:540
BuildMode build_mode
Definition: bcrsmatrix.hh:1896
RealRowIterator< row_type > iterator
The iterator over the (mutable matrix rows.
Definition: bcrsmatrix.hh:620
void setColumnPointers(ConstRowIterator row)
Copy row sizes from iterator range starting at row and set column index pointers for all rows...
Definition: bcrsmatrix.hh:1954
double mem_ratio
fraction of wasted memory resulting from non-used overflow area.
Definition: bcrsmatrix.hh:93
Build entries randomly.
Definition: bcrsmatrix.hh:479
FieldTraits< field_type >::real_type frobenius_norm2() const
square of frobenius norm, need for block recursion
Definition: bcrsmatrix.hh:1779
A::size_type size_type
Type for indices and sizes.
Definition: matrix.hh:41
bool contains(size_type j)
return true if column index is in row
Definition: bcrsmatrix.hh:1032
BCRSMatrix(size_type _n, size_type _m, size_type _nnz, BuildMode bm)
matrix with known number of nonzeroes
Definition: bcrsmatrix.hh:705
~BCRSMatrix()
destructor
Definition: bcrsmatrix.hh:781
std::ptrdiff_t distanceTo(const RealRowIterator< const ValueType > &other) const
Definition: bcrsmatrix.hh:566
void setIndices(size_type row, It begin, It end)
Set all column indices for row from the given iterator range.
Definition: bcrsmatrix.hh:1194
Col col
Definition: matrixmatrix.hh:347
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
ConstIterator end() const
Get const iterator to one beyond last row.
Definition: bcrsmatrix.hh:667
void operator()(size_type *p)
Definition: bcrsmatrix.hh:2075
size_type maximum
maximum number of non-zeroes per row.
Definition: bcrsmatrix.hh:86
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:2096
A wrapper for uniform access to the BCRSMatrix during and after the build stage in implicit build mod...
Definition: bcrsmatrix.hh:110
void set(size_type _n, B *_p, size_type *_j)
set size and pointer
Definition: bvector.hh:1024
size_type index() const
return index
Definition: bcrsmatrix.hh:555
row_type::ConstIterator ConstColIterator
Const iterator to the entries of a row.
Definition: bcrsmatrix.hh:690
BCRSMatrix(size_type _n, size_type _m, BuildMode bm)
matrix with unknown number of nonzeroes
Definition: bcrsmatrix.hh:714
CompressionStatistics compress()
Finishes the buildstage in implicit mode.
Definition: bcrsmatrix.hh:1319
Some handy generic functions for ISTL matrices.
bool exists(size_type i, size_type j) const
return true if (i,j) is in pattern
Definition: bcrsmatrix.hh:1881
void mmhv(const X &x, Y &y) const
y -= A^H x
Definition: bcrsmatrix.hh:1739
Statistics about compression achieved in implicit mode.
Definition: bcrsmatrix.hh:81
Matrix::block_type block_type
The block_type of the underlying matrix.
Definition: bcrsmatrix.hh:119
size_type size() const
Get the current row size.
Definition: bcrsmatrix.hh:1044
void setindexptr(size_type *_j)
set pointer only
Definition: bvector.hh:1044
FieldTraits< field_type >::real_type frobenius_norm() const
frobenius norm: sqrt(sum over squared values of entries)
Definition: bcrsmatrix.hh:1800
ConstIterator beforeEnd() const
Definition: bcrsmatrix.hh:674
Definition: bcrsmatrix.hh:71
CreateIterator & operator++()
prefix increment
Definition: bcrsmatrix.hh:936
double avg
average number of non-zeroes per row.
Definition: bcrsmatrix.hh:84
void mv(const X &x, Y &y) const
y = A x
Definition: bcrsmatrix.hh:1571
Iterator class for sequential creation of blocks
Definition: bcrsmatrix.hh:918
void endindices()
indicate that all indices are defined, check consistency
Definition: bcrsmatrix.hh:1208
FieldTraits< field_type >::real_type infinity_norm() const
infinity norm (row sum norm, how to generalize for blocks?)
Definition: bcrsmatrix.hh:1806
size_type allocationSize
Definition: bcrsmatrix.hh:1910
Iterator end()
Get iterator to one beyond last row.
Definition: bcrsmatrix.hh:630
Iterator beforeEnd()
Definition: bcrsmatrix.hh:637
bool equals(const RealRowIterator< const ValueType > &other) const
equality
Definition: bcrsmatrix.hh:580
void deallocate(bool deallocateRows=true)
deallocate memory of the matrix.
Definition: bcrsmatrix.hh:2016
Definition: basearray.hh:19
void setSize(size_type rows, size_type columns, size_type nnz=0)
Set the size of the matrix.
Definition: bcrsmatrix.hh:818
void setrowsize(size_type i, size_type s)
set number of indices in row i to s
Definition: bcrsmatrix.hh:1077
ImplicitMatrixBuilder(Matrix &m)
Creates an ImplicitMatrixBuilder for matrix m.
Definition: bcrsmatrix.hh:164
size_type N() const
number of rows (counted in blocks)
Definition: bcrsmatrix.hh:1849