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> 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");
187 ImplicitMatrixBuilder(Matrix& m, size_type rows, size_type cols, size_type avg_cols_per_row,
double overflow_fraction)
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> >
454 blocklevel = B::blocklevel+1
497 row_type& operator[] (size_type i)
499 #ifdef DUNE_ISTL_WITH_CHECKING 500 if (build_mode == implicit && ready != built)
501 DUNE_THROW(
BCRSMatrixError,
"You cannot use operator[] in implicit build mode before calling compress()");
509 const row_type& operator[] (size_type i)
const 511 #ifdef DUNE_ISTL_WITH_CHECKING 512 if (build_mode == implicit && ready != built)
513 DUNE_THROW(
BCRSMatrixError,
"You cannot use operator[] in implicit build mode before calling compress()");
514 if (built!=ready) DUNE_THROW(
BCRSMatrixError,
"row not initialized yet");
526 :
public RandomAccessIteratorFacade<RealRowIterator<T>, T>
533 friend class RandomAccessIteratorFacade<
RealRowIterator<const ValueType>, const ValueType>;
598 void advance(std::ptrdiff_t diff)
603 T& elementAt(std::ptrdiff_t diff)
const 609 row_type& dereference ()
const 625 return Iterator(r,0);
631 return Iterator(r,n);
638 return Iterator(r,n-1);
645 return Iterator(r,-1);
662 return ConstIterator(r,0);
666 ConstIterator
end ()
const 668 return ConstIterator(r,n);
675 return ConstIterator(r,n-1);
682 return ConstIterator(r,-1);
698 : build_mode(unknown), ready(notAllocated), n(0), m(0), nnz_(0),
699 allocationSize_(0), r(0), a(0),
700 avg(0), overflowsize(-1.0)
705 : build_mode(bm), ready(notAllocated), n(0), m(0), nnz_(0),
706 allocationSize_(0), r(0), a(0),
707 avg(0), overflowsize(-1.0)
709 allocate(_n, _m, _nnz,
true,
false);
714 : build_mode(bm), ready(notAllocated), n(0), m(0), nnz_(0),
715 allocationSize_(0), r(0), a(0),
716 avg(0), overflowsize(-1.0)
718 allocate(_n, _m,0,
true,
false);
733 : build_mode(bm), ready(notAllocated), n(0), m(0), nnz_(0),
734 allocationSize_(0), r(0), a(0),
735 avg(_avg), overflowsize(_overflowsize)
738 DUNE_THROW(
BCRSMatrixError,
"Only call this constructor when using the implicit build mode");
743 if (_overflowsize < 0.0)
744 DUNE_THROW(
BCRSMatrixError,
"You cannot set a negative overflow fraction");
745 implicit_allocate(_n,_m);
754 : build_mode(Mat.build_mode), ready(notAllocated), n(0), m(0), nnz_(0),
755 allocationSize_(0), r(0), a(0),
756 avg(Mat.
avg), overflowsize(Mat.overflowsize)
758 if (!(Mat.
ready == notAllocated || Mat.
ready == built))
759 DUNE_THROW(InvalidStateException,
"BCRSMatrix can only be copy-constructed when source matrix is completely empty (size not set) or fully built)");
762 size_type _nnz = Mat.
nnz_;
768 for (size_type i=0; i<Mat.
n; i++)
773 allocate(Mat.
n, Mat.
m, _nnz,
true,
true);
776 copyWindowStructure(Mat);
791 if (ready == notAllocated)
796 if (ready == building && (build_mode == unknown || build_mode == random || build_mode == row_wise) && (bm == row_wise || bm == random))
799 DUNE_THROW(InvalidStateException,
"Matrix structure cannot be changed at this stage anymore (ready == "<<ready<<
").");
817 void setSize(size_type rows, size_type columns, size_type nnz=0)
822 if (build_mode == implicit)
825 DUNE_THROW(
Dune::BCRSMatrixError,
"number of non-zeroes may not be set in implicit mode, use setImplicitBuildModeParameters() instead");
828 implicit_allocate(rows,columns);
833 allocate(rows, columns, nnz,
true,
false);
852 DUNE_THROW(
BCRSMatrixError,
"You cannot set a negative overflow fraction");
855 if (ready != notAllocated)
856 DUNE_THROW(InvalidStateException,
"You cannot modify build mode parameters at this stage anymore");
858 overflowsize = _overflow;
870 if (&Mat==
this)
return *
this;
872 if (!((ready == notAllocated || ready == built) && (Mat.
ready == notAllocated || Mat.
ready == built)))
873 DUNE_THROW(InvalidStateException,
"BCRSMatrix can only be copied when both target and source are empty or fully built)");
877 deallocate(n!=Mat.
n);
880 if (n>0 && n!=Mat.
n) {
882 for(row_type *riter=r+(n-1), *rend=r-1; riter!=rend; --riter)
883 rowAllocator_.destroy(riter);
884 rowAllocator_.deallocate(r,n);
890 for (size_type i=0; i<Mat.
n; i++)
896 allocate(Mat.
n, Mat.
m, nnz_, n!=Mat.
n,
true);
899 copyWindowStructure(Mat);
907 if (!(ready == notAllocated || ready == built))
908 DUNE_THROW(InvalidStateException,
"Scalar assignment only works on fully built BCRSMatrix)");
910 for (size_type i=0; i<n; i++) r[i] = k;
922 : Mat(_Mat), i(_i), nnz(0), current_row(nullptr, Mat.j_.
get(), 0)
924 if (Mat.build_mode == unknown && Mat.ready == building)
926 Mat.build_mode = row_wise;
928 if (i==0 && Mat.ready != building)
929 DUNE_THROW(
BCRSMatrixError,
"creation only allowed for uninitialized matrix");
930 if(Mat.build_mode!=row_wise)
931 DUNE_THROW(
BCRSMatrixError,
"creation only allowed if row wise allocation was requested in the constructor");
938 if (Mat.ready != building)
962 Mat.r[i].set(s,
nullptr,current_row.getindexptr());
963 current_row.setindexptr(current_row.getindexptr()+s);
967 B* b = Mat.allocator_.allocate(s);
971 size_type* j = Mat.sizeAllocator_.allocate(s);
976 Mat.r[i].set(0,
nullptr,
nullptr);
979 std::copy(
pattern.cbegin(),
pattern.cend(), Mat.r[i].getindexptr());
996 Mat.setDataPointers();
1006 return (i!=it.i) || (&Mat!=&it.Mat);
1012 return (i==it.i) && (&Mat==&it.Mat);
1046 typedef std::set<size_type,std::less<size_type> > PatternType;
1048 row_type current_row;
1077 if (build_mode!=random)
1079 if (ready != building)
1088 #ifdef DUNE_ISTL_WITH_CHECKING 1092 return r[i].getsize();
1098 if (build_mode!=random)
1100 if (ready != building)
1103 r[i].setsize(r[i].getsize()+s);
1109 if (build_mode!=random)
1111 if (ready != building)
1116 for (size_type i=0; i<n; i++)
1118 total += r[i].getsize();
1123 allocate(n,m,total,
false,
false);
1124 else if(nnz_ < total)
1125 DUNE_THROW(
BCRSMatrixError,
"Specified number of nonzeros ("<<nnz_<<
") not " 1126 <<
"sufficient for calculated nonzeros ("<<total<<
"! ");
1129 setColumnPointers(begin());
1133 for (size_type k=0; k<nnz_; k++)
1135 ready = rowSizesBuilt;
1151 if (build_mode!=random)
1155 if (ready==building)
1157 if (ready==notAllocated)
1158 DUNE_THROW(
BCRSMatrixError,
"matrix size not set and no memory allocated yet");
1164 size_type*
const first = r[row].getindexptr();
1165 size_type*
const last = first + r[row].getsize();
1168 size_type* pos = std::lower_bound(first,last,col);
1171 if (pos!=last && *pos == col)
return;
1174 size_type* end = std::lower_bound(pos,last,m);
1179 std::copy_backward(pos,end,end+1);
1191 template<
typename It>
1194 size_type row_size = r[row].size();
1195 size_type* col_begin = r[row].getindexptr();
1198 if ((col_end = std::copy(begin,end,r[row].getindexptr())) != col_begin + row_size)
1201 <<
") does not match number of passed entries (" << (col_end - col_begin) <<
")");
1202 std::sort(col_begin,col_end);
1208 if (build_mode!=random)
1212 if (ready==building)
1214 if (ready==notAllocated)
1215 DUNE_THROW(
BCRSMatrixError,
"matrix size not set and no memory allocated yet");
1218 RowIterator endi=end();
1219 for (RowIterator i=begin(); i!=endi; ++i)
1221 ColIterator endj = (*i).end();
1222 for (ColIterator j=(*i).begin(); j!=endj; ++j) {
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;
1226 r[i.index()].setsize(j.offset());
1255 #ifdef DUNE_ISTL_WITH_CHECKING 1256 if (build_mode!=implicit)
1259 DUNE_THROW(
BCRSMatrixError,
"matrix already built up, use operator[] for entry access now");
1260 if (ready==notAllocated)
1261 DUNE_THROW(
BCRSMatrixError,
"matrix size not set and no memory allocated yet");
1262 if (ready!=building)
1263 DUNE_THROW(InvalidStateException,
"You may only use entry() during the 'building' stage");
1271 size_type* begin = r[row].getindexptr();
1272 size_type* end = begin + r[row].getsize();
1274 size_type* pos = std::find(begin, end, col);
1280 std::ptrdiff_t offset = pos - r[row].getindexptr();
1281 B* aptr = r[row].getptr() + offset;
1287 if (r[row].getsize() ==
avg)
1288 return overflow[std::make_pair(row,col)];
1295 std::ptrdiff_t offset = end - r[row].getindexptr();
1296 B* apos = r[row].getptr() + offset;
1299 r[row].setsize(r[row].getsize()+1);
1319 if (build_mode!=implicit)
1322 DUNE_THROW(
BCRSMatrixError,
"matrix already built up, no more need for compression");
1323 if (ready==notAllocated)
1324 DUNE_THROW(
BCRSMatrixError,
"matrix size not set and no memory allocated yet");
1325 if (ready!=building)
1326 DUNE_THROW(InvalidStateException,
"You may only call compress() at the end of the 'building' stage");
1329 CompressionStatistics stats;
1334 size_type* jiit = j_.get();
1338 typename OverflowType::iterator oit = overflow.begin();
1341 std::vector<size_type*> perm;
1344 for (size_type i=0; i<n; i++)
1347 size_type* begin = r[i].getindexptr();
1349 size_type size = r[i].getsize();
1353 typename std::vector<size_type*>::iterator it = perm.begin();
1354 for (size_type* iit = begin; iit < begin + size; ++iit, ++it)
1361 r[i].setindexptr(jiit);
1364 for (it = perm.begin(); it != perm.end(); ++it)
1367 while ((oit!=overflow.end()) && (oit->first < std::make_pair(i,**it)))
1372 "Allocated memory for BCRSMatrix exhausted during compress()!" 1373 "Please increase either the average number of entries per row or the overflow fraction." 1376 *jiit = oit->first.second;
1378 *aiit = oit->second;
1381 r[i].setsize(r[i].getsize()+1);
1387 "Allocated memory for BCRSMatrix exhausted during compress()!" 1388 "Please increase either the average number of entries per row or the overflow fraction." 1394 B* apos = *it - j_.get() + a;
1400 while ((oit!=overflow.end()) && (oit->first.first == i))
1405 "Allocated memory for BCRSMatrix exhausted during compress()!" 1406 "Please increase either the average number of entries per row or the overflow fraction." 1410 *jiit = oit->first.second;
1412 *aiit = oit->second;
1415 r[i].setsize(r[i].getsize()+1);
1419 if (r[i].getsize()>stats.
maximum)
1420 stats.
maximum = r[i].getsize();
1427 std::ptrdiff_t diff = (r[n-1].getindexptr() + r[n-1].getsize() - j_.get());
1429 stats.
avg = (double) (nnz_) / (double) n;
1430 stats.
mem_ratio = (double) (nnz_) / (double) allocationSize_;
1443 #ifdef DUNE_ISTL_WITH_CHECKING 1445 DUNE_THROW(
BCRSMatrixError,
"You can only call arithmetic operations on fully built BCRSMatrix instances");
1451 for (size_type i=0; i<nnz_; i++)
1456 RowIterator endi=end();
1457 for (RowIterator i=begin(); i!=endi; ++i)
1459 ColIterator endj = (*i).end();
1460 for (ColIterator j=(*i).begin(); j!=endj; ++j)
1471 #ifdef DUNE_ISTL_WITH_CHECKING 1473 DUNE_THROW(
BCRSMatrixError,
"You can only call arithmetic operations on fully built BCRSMatrix instances");
1479 for (size_type i=0; i<nnz_; i++)
1484 RowIterator endi=end();
1485 for (RowIterator i=begin(); i!=endi; ++i)
1487 ColIterator endj = (*i).end();
1488 for (ColIterator j=(*i).begin(); j!=endj; ++j)
1504 #ifdef DUNE_ISTL_WITH_CHECKING 1505 if (ready != built || b.
ready != built)
1506 DUNE_THROW(
BCRSMatrixError,
"You can only call arithmetic operations on fully built BCRSMatrix instances");
1507 if(N()!=b.
N() || M() != b.
M())
1508 DUNE_THROW(RangeError,
"Matrix sizes do not match!");
1510 RowIterator endi=end();
1511 ConstRowIterator j=b.
begin();
1512 for (RowIterator i=begin(); i!=endi; ++i, ++j) {
1526 #ifdef DUNE_ISTL_WITH_CHECKING 1527 if (ready != built || b.
ready != built)
1528 DUNE_THROW(
BCRSMatrixError,
"You can only call arithmetic operations on fully built BCRSMatrix instances");
1529 if(N()!=b.
N() || M() != b.
M())
1530 DUNE_THROW(RangeError,
"Matrix sizes do not match!");
1532 RowIterator endi=end();
1533 ConstRowIterator j=b.
begin();
1534 for (RowIterator i=begin(); i!=endi; ++i, ++j) {
1551 #ifdef DUNE_ISTL_WITH_CHECKING 1552 if (ready != built || b.
ready != built)
1553 DUNE_THROW(
BCRSMatrixError,
"You can only call arithmetic operations on fully built BCRSMatrix instances");
1554 if(N()!=b.
N() || M() != b.
M())
1555 DUNE_THROW(RangeError,
"Matrix sizes do not match!");
1557 RowIterator endi=end();
1558 ConstRowIterator j=b.
begin();
1559 for(RowIterator i=begin(); i!=endi; ++i, ++j)
1568 template<
class X,
class Y>
1569 void mv (
const X& x, Y& y)
const 1571 #ifdef DUNE_ISTL_WITH_CHECKING 1573 DUNE_THROW(
BCRSMatrixError,
"You can only call arithmetic operations on fully built BCRSMatrix instances");
1575 "Size mismatch: M: " << N() <<
"x" << M() <<
" x: " << x.N());
1577 "Size mismatch: M: " << N() <<
"x" << M() <<
" y: " << y.N());
1579 ConstRowIterator endi=end();
1580 for (ConstRowIterator i=begin(); i!=endi; ++i)
1583 ConstColIterator endj = (*i).end();
1584 for (ConstColIterator j=(*i).begin(); j!=endj; ++j)
1585 (*j).umv(x[j.index()],y[i.index()]);
1590 template<
class X,
class Y>
1591 void umv (
const X& x, Y& y)
const 1593 #ifdef DUNE_ISTL_WITH_CHECKING 1595 DUNE_THROW(
BCRSMatrixError,
"You can only call arithmetic operations on fully built BCRSMatrix instances");
1599 ConstRowIterator endi=end();
1600 for (ConstRowIterator i=begin(); i!=endi; ++i)
1602 ConstColIterator endj = (*i).end();
1603 for (ConstColIterator j=(*i).begin(); j!=endj; ++j)
1604 (*j).umv(x[j.index()],y[i.index()]);
1609 template<
class X,
class Y>
1610 void mmv (
const X& x, Y& y)
const 1612 #ifdef DUNE_ISTL_WITH_CHECKING 1614 DUNE_THROW(
BCRSMatrixError,
"You can only call arithmetic operations on fully built BCRSMatrix instances");
1618 ConstRowIterator endi=end();
1619 for (ConstRowIterator i=begin(); i!=endi; ++i)
1621 ConstColIterator endj = (*i).end();
1622 for (ConstColIterator j=(*i).begin(); j!=endj; ++j)
1623 (*j).mmv(x[j.index()],y[i.index()]);
1628 template<
class X,
class Y,
class F>
1629 void usmv (F&& alpha,
const X& x, Y& y)
const 1631 #ifdef DUNE_ISTL_WITH_CHECKING 1633 DUNE_THROW(
BCRSMatrixError,
"You can only call arithmetic operations on fully built BCRSMatrix instances");
1637 ConstRowIterator endi=end();
1638 for (ConstRowIterator i=begin(); i!=endi; ++i)
1640 ConstColIterator endj = (*i).end();
1641 for (ConstColIterator j=(*i).begin(); j!=endj; ++j)
1642 (*j).usmv(alpha,x[j.index()],y[i.index()]);
1647 template<
class X,
class Y>
1648 void mtv (
const X& x, Y& y)
const 1650 #ifdef DUNE_ISTL_WITH_CHECKING 1652 DUNE_THROW(
BCRSMatrixError,
"You can only call arithmetic operations on fully built BCRSMatrix instances");
1656 for(size_type i=0; i<y.N(); ++i)
1662 template<
class X,
class Y>
1665 #ifdef DUNE_ISTL_WITH_CHECKING 1667 DUNE_THROW(
BCRSMatrixError,
"You can only call arithmetic operations on fully built BCRSMatrix instances");
1671 ConstRowIterator endi=end();
1672 for (ConstRowIterator i=begin(); i!=endi; ++i)
1674 ConstColIterator endj = (*i).end();
1675 for (ConstColIterator j=(*i).begin(); j!=endj; ++j)
1676 (*j).umtv(x[i.index()],y[j.index()]);
1681 template<
class X,
class Y>
1684 #ifdef DUNE_ISTL_WITH_CHECKING 1688 ConstRowIterator endi=end();
1689 for (ConstRowIterator i=begin(); i!=endi; ++i)
1691 ConstColIterator endj = (*i).end();
1692 for (ConstColIterator j=(*i).begin(); j!=endj; ++j)
1693 (*j).mmtv(x[i.index()],y[j.index()]);
1698 template<
class X,
class Y>
1699 void usmtv (
const field_type& alpha,
const X& x, Y& y)
const 1701 #ifdef DUNE_ISTL_WITH_CHECKING 1703 DUNE_THROW(
BCRSMatrixError,
"You can only call arithmetic operations on fully built BCRSMatrix instances");
1707 ConstRowIterator endi=end();
1708 for (ConstRowIterator i=begin(); i!=endi; ++i)
1710 ConstColIterator endj = (*i).end();
1711 for (ConstColIterator j=(*i).begin(); j!=endj; ++j)
1712 (*j).usmtv(alpha,x[i.index()],y[j.index()]);
1717 template<
class X,
class Y>
1720 #ifdef DUNE_ISTL_WITH_CHECKING 1722 DUNE_THROW(
BCRSMatrixError,
"You can only call arithmetic operations on fully built BCRSMatrix instances");
1726 ConstRowIterator endi=end();
1727 for (ConstRowIterator i=begin(); i!=endi; ++i)
1729 ConstColIterator endj = (*i).end();
1730 for (ConstColIterator j=(*i).begin(); j!=endj; ++j)
1731 (*j).umhv(x[i.index()],y[j.index()]);
1736 template<
class X,
class Y>
1739 #ifdef DUNE_ISTL_WITH_CHECKING 1741 DUNE_THROW(
BCRSMatrixError,
"You can only call arithmetic operations on fully built BCRSMatrix instances");
1745 ConstRowIterator endi=end();
1746 for (ConstRowIterator i=begin(); i!=endi; ++i)
1748 ConstColIterator endj = (*i).end();
1749 for (ConstColIterator j=(*i).begin(); j!=endj; ++j)
1750 (*j).mmhv(x[i.index()],y[j.index()]);
1755 template<
class X,
class Y>
1756 void usmhv (
const field_type& alpha,
const X& x, Y& y)
const 1758 #ifdef DUNE_ISTL_WITH_CHECKING 1760 DUNE_THROW(
BCRSMatrixError,
"You can only call arithmetic operations on fully built BCRSMatrix instances");
1764 ConstRowIterator endi=end();
1765 for (ConstRowIterator i=begin(); i!=endi; ++i)
1767 ConstColIterator endj = (*i).end();
1768 for (ConstColIterator j=(*i).begin(); j!=endj; ++j)
1769 (*j).usmhv(alpha,x[i.index()],y[j.index()]);
1779 #ifdef DUNE_ISTL_WITH_CHECKING 1781 DUNE_THROW(
BCRSMatrixError,
"You can only call arithmetic operations on fully built BCRSMatrix instances");
1786 ConstRowIterator endi=end();
1787 for (ConstRowIterator i=begin(); i!=endi; ++i)
1789 ConstColIterator endj = (*i).end();
1790 for (ConstColIterator j=(*i).begin(); j!=endj; ++j)
1791 sum += (*j).frobenius_norm2();
1800 return sqrt(frobenius_norm2());
1804 template <
typename ft = field_type,
1805 typename std::enable_if<!has_nan<ft>::value,
int>::type = 0>
1808 DUNE_THROW(
BCRSMatrixError,
"You can only call arithmetic operations on fully built BCRSMatrix instances");
1810 using real_type =
typename FieldTraits<ft>::real_type;
1814 for (
auto const &x : *
this) {
1816 for (
auto const &y : x)
1817 sum += y.infinity_norm();
1818 norm = max(sum, norm);
1824 template <
typename ft = field_type,
1825 typename std::enable_if<!has_nan<ft>::value,
int>::type = 0>
1828 DUNE_THROW(
BCRSMatrixError,
"You can only call arithmetic operations on fully built BCRSMatrix instances");
1830 using real_type =
typename FieldTraits<ft>::real_type;
1834 for (
auto const &x : *
this) {
1836 for (
auto const &y : x)
1837 sum += y.infinity_norm_real();
1838 norm = max(sum, norm);
1844 template <
typename ft = field_type,
1845 typename std::enable_if<has_nan<ft>::value,
int>::type = 0>
1848 DUNE_THROW(
BCRSMatrixError,
"You can only call arithmetic operations on fully built BCRSMatrix instances");
1850 using real_type =
typename FieldTraits<ft>::real_type;
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;
1867 template <
typename ft = field_type,
1868 typename std::enable_if<has_nan<ft>::value,
int>::type = 0>
1871 DUNE_THROW(
BCRSMatrixError,
"You can only call arithmetic operations on fully built BCRSMatrix instances");
1873 using real_type =
typename FieldTraits<ft>::real_type;
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;
1892 size_type
N ()
const 1898 size_type
M ()
const 1926 #ifdef DUNE_ISTL_WITH_CHECKING 1927 if (i<0 || i>=n) DUNE_THROW(
BCRSMatrixError,
"row index out of range");
1928 if (j<0 || j>=m) DUNE_THROW(
BCRSMatrixError,
"column index out of range");
1930 return (r[i].size() && r[i].find(j) != r[i].end());
1960 std::shared_ptr<size_type>
j_;
1971 row_type current_row(a,j_.get(),0);
1972 for (size_type i=0; i<n; i++, ++row) {
1974 size_type s = row->getsize();
1978 r[i].
set(s,current_row.getptr(), current_row.getindexptr());
1980 current_row.setptr(current_row.getptr()+s);
1981 current_row.setindexptr(current_row.getindexptr()+s);
1984 r[i].
set(0,
nullptr,
nullptr);
1996 size_type* jptr = j_.get();
1997 for (size_type i=0; i<n; ++i, ++row) {
1999 size_type s = row->getsize();
2007 r[i].
set(0,
nullptr,
nullptr);
2023 for (size_type i=0; i<n; ++i) {
2025 if (r[i].getsize() > 0) {
2030 r[i].
set(0,
nullptr,
nullptr);
2041 setWindowPointers(Mat.
begin());
2044 for (size_type i=0; i<n; i++) r[i] = Mat.
r[i];
2047 build_mode = row_wise;
2062 if (allocationSize_>0)
2068 for(B *aiter=a+(allocationSize_-1), *aend=a-1; aiter!=aend; --aiter)
2069 allocator_.destroy(aiter);
2070 allocator_.deallocate(a,allocationSize_);
2077 for (size_type i=0; i<n; i++)
2078 if (r[i].getsize()>0)
2080 for (B *
col=r[i].getptr()+(r[i].getsize()-1),
2081 *colend = r[i].getptr()-1;
col!=colend; --
col) {
2082 allocator_.destroy(
col);
2084 sizeAllocator_.deallocate(r[i].getindexptr(),1);
2085 allocator_.deallocate(r[i].getptr(),1);
2088 r[i].
set(0,
nullptr,
nullptr);
2093 if (n>0 && deallocateRows && r) {
2094 for(row_type *riter=r+(n-1), *rend=r-1; riter!=rend; --riter)
2095 rowAllocator_.destroy(riter);
2096 rowAllocator_.deallocate(r,n);
2108 typename A::template rebind<size_type>::other& sizeAllocator_;
2111 Deallocator(
typename A::template rebind<size_type>::other& sizeAllocator)
2112 : sizeAllocator_(sizeAllocator)
2115 void operator()(size_type* p) { sizeAllocator_.deallocate(p,1); }
2136 void allocate(size_type rows, size_type columns, size_type allocationSize,
bool allocateRows,
bool allocate_data)
2141 nnz_ = allocationSize;
2142 allocationSize_ = allocationSize;
2148 DUNE_THROW(InvalidStateException,
"Rows have already been allocated, cannot allocate a second time");
2149 r = rowAllocator_.allocate(rows);
2158 if (allocationSize_>0) {
2161 j_.reset(sizeAllocator_.allocate(allocationSize_),
Deallocator(sizeAllocator_));
2164 for(row_type* ri=r; ri!=r+rows; ++ri)
2165 rowAllocator_.construct(ri, row_type());
2175 DUNE_THROW(InvalidStateException,
"Cannot allocate data array (already allocated)");
2176 if (allocationSize_>0) {
2177 a = allocator_.allocate(allocationSize_);
2180 new (a) B[allocationSize_];
2193 if (build_mode != implicit)
2194 DUNE_THROW(InvalidStateException,
"implicit_allocate() may only be called in implicit build mode");
2195 if (ready != notAllocated)
2196 DUNE_THROW(InvalidStateException,
"memory has already been allocated");
2199 if (overflowsize < 0)
2200 DUNE_THROW(InvalidStateException,
"You have to set the implicit build mode parameters before starting to build the matrix");
2202 size_type osize = (size_type) (_n*avg)*overflowsize + 4*
avg;
2203 allocationSize_ = _n*avg + osize;
2205 allocate(_n, _m, allocationSize_,
true,
true);
2208 size_type* jptr = j_.get() + osize;
2209 B* aptr = a + osize;
2210 for (size_type i=0; i<n; i++)
2212 r[i].
set(0,aptr,jptr);
void insert(size_type j)
put column index in row
Definition: bcrsmatrix.hh:1022
Class used by shared_ptr to deallocate memory using the proper allocator.
Definition: bcrsmatrix.hh:2106
BuildMode
we support two modes
Definition: bcrsmatrix.hh:458
Col col
Definition: matrixmatrix.hh:347
ConstIterator begin() const
Get const iterator to first row.
Definition: bcrsmatrix.hh:660
OverflowType overflow
Definition: bcrsmatrix.hh:1967
BCRSMatrix(size_type _n, size_type _m, BuildMode bm)
matrix with unknown number of nonzeroes
Definition: bcrsmatrix.hh:713
::Dune::CompressionStatistics< size_type > CompressionStatistics
The type for the statistics object returned by compress()
Definition: bcrsmatrix.hh:449
BCRSMatrix(size_type _n, size_type _m, size_type _nnz, BuildMode bm)
matrix with known number of nonzeroes
Definition: bcrsmatrix.hh:704
void mtv(const X &x, Y &y) const
y = A^T x
Definition: bcrsmatrix.hh:1648
void setImplicitBuildModeParameters(size_type _avg, double _overflow)
Set parameters needed for creation in implicit build mode.
Definition: bcrsmatrix.hh:845
A::size_type size_type
Type for indices and sizes.
Definition: matrix.hh:571
Definition: bvector.hh:1049
void umhv(const X &x, Y &y) const
y += A^H x
Definition: bcrsmatrix.hh:1718
void setsize(size_type _n)
set size only
Definition: bvector.hh:1157
FieldTraits< field_type >::real_type frobenius_norm() const
frobenius norm: sqrt(sum over squared values of entries)
Definition: bcrsmatrix.hh:1798
void mv(const X &x, Y &y) const
y = A x
Definition: bcrsmatrix.hh:1569
std::ptrdiff_t distanceTo(const RealRowIterator< ValueType > &other) const
Definition: bcrsmatrix.hh:559
A::size_type size_type
The type for the index access and the size.
Definition: bcrsmatrix.hh:446
size_type allocationSize_
Definition: bcrsmatrix.hh:1950
size_type M() const
The number of columns in the matrix.
Definition: bcrsmatrix.hh:210
BuildStage
Definition: bcrsmatrix.hh:415
ConstIterator beforeBegin() const
Definition: bcrsmatrix.hh:680
void setrowsize(size_type i, size_type s)
Set number of indices in row i to s.
Definition: bcrsmatrix.hh:1075
~BCRSMatrix()
destructor
Definition: bcrsmatrix.hh:780
size_type m
Definition: bcrsmatrix.hh:1948
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 nonzeroes() const
number of blocks that are stored (the number of blocks that possibly are nonzero) ...
Definition: bcrsmatrix.hh:1904
void endindices()
indicate that all indices are defined, check consistency
Definition: bcrsmatrix.hh:1206
Iterator end()
Get iterator to one beyond last row.
Definition: bcrsmatrix.hh:629
void set(size_type _n, B *_p, size_type *_j)
set size and pointer
Definition: bvector.hh:1149
Definition: matrixutils.hh:511
Iterator begin()
Get iterator to first row.
Definition: bcrsmatrix.hh:623
BCRSMatrix()
an empty matrix
Definition: bcrsmatrix.hh:697
B::field_type field_type
export the type representing the field
Definition: bcrsmatrix.hh:434
size_type n
Definition: bcrsmatrix.hh:1947
std::remove_const< T >::type ValueType
The unqualified value type.
Definition: bcrsmatrix.hh:531
void setBuildMode(BuildMode bm)
Sets the build mode of the matrix.
Definition: bcrsmatrix.hh:789
double avg
average number of non-zeroes per row.
Definition: bcrsmatrix.hh:83
void setWindowPointers(ConstRowIterator row)
Definition: bcrsmatrix.hh:1969
ConstIterator end() const
Get const iterator to one beyond last row.
Definition: bcrsmatrix.hh:666
bool equals(const RealRowIterator< ValueType > &other) const
equality
Definition: bcrsmatrix.hh:572
Definition: bcrsmatrix.hh:70
Error specific to BCRSMatrix.
Definition: istlexception.hh:19
FieldTraits< ft >::real_type infinity_norm_real() const
simplified infinity norm (uses Manhattan norm for complex values)
Definition: bcrsmatrix.hh:1826
void operator()(size_type *p)
Definition: bcrsmatrix.hh:2115
RealRowIterator< row_type > iterator
The iterator over the (mutable matrix rows.
Definition: bcrsmatrix.hh:619
double mem_ratio
fraction of wasted memory resulting from non-used overflow area.
Definition: bcrsmatrix.hh:92
T block_type
Export the type representing the components.
Definition: matrix.hh:562
B & entry(size_type row, size_type col)
Returns reference to entry (row,col) of the matrix.
Definition: bcrsmatrix.hh:1253
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
Definition: matrixmarket.hh:258
Iterator beforeBegin()
Definition: bcrsmatrix.hh:643
block_type & operator[](size_type j) const
Returns entry in column j.
Definition: bcrsmatrix.hh:135
void copyWindowStructure(const BCRSMatrix &Mat)
Copy the window structure from another matrix.
Definition: bcrsmatrix.hh:2039
size_type avg
Definition: bcrsmatrix.hh:1963
bool contains(size_type j)
return true if column index is in row
Definition: bcrsmatrix.hh:1028
CompressedBlockVectorWindow< B, A > row_type
implement row_type with compressed vector
Definition: bcrsmatrix.hh:443
CreateIterator(BCRSMatrix &_Mat, size_type _i)
constructor
Definition: bcrsmatrix.hh:921
BuildStage ready
Definition: bcrsmatrix.hh:1937
void setindexptr(size_type *_j)
set pointer only
Definition: bvector.hh:1169
Build entries randomly with an educated guess for the number of entries per row.
Definition: bcrsmatrix.hh:487
B * a
Definition: bcrsmatrix.hh:1957
BuildStage buildStage() const
The current build stage of the matrix.
Definition: bcrsmatrix.hh:1910
Iterator RowIterator
rename the iterators for easier access
Definition: bcrsmatrix.hh:649
Matrix::block_type block_type
The block_type of the underlying matrix.
Definition: bcrsmatrix.hh:118
Matrix::size_type size_type
The size_type of the underlying matrix.
Definition: bcrsmatrix.hh:121
void usmtv(const field_type &alpha, const X &x, Y &y) const
y += alpha A^T x
Definition: bcrsmatrix.hh:1699
CreateIterator createend()
get create iterator pointing to one after the last block
Definition: bcrsmatrix.hh:1061
FieldTraits< ft >::real_type infinity_norm() const
infinity norm (row sum norm, how to generalize for blocks?)
Definition: bcrsmatrix.hh:1806
BuildMode build_mode
Definition: bcrsmatrix.hh:1936
size_type getsize() const
get size
Definition: bvector.hh:1198
A sparse block matrix with compressed row storage.
Definition: bcrsmatrix.hh:411
void usmv(F &&alpha, const X &x, Y &y) const
y += alpha A x
Definition: bcrsmatrix.hh:1629
std::map< std::pair< size_type, size_type >, B > OverflowType
Definition: bcrsmatrix.hh:1966
void mmv(const X &x, Y &y) const
y -= A x
Definition: bcrsmatrix.hh:1610
BCRSMatrix & axpy(field_type alpha, const BCRSMatrix &b)
Add the scaled entries of another matrix to this one.
Definition: bcrsmatrix.hh:1549
void setptr(B *_p)
set pointer only
Definition: bvector.hh:1163
Statistics about compression achieved in implicit mode.
Definition: bcrsmatrix.hh:80
B block_type
export the type representing the components
Definition: bcrsmatrix.hh:437
void setSize(size_type rows, size_type columns, size_type nnz=0)
Set the size of the matrix.
Definition: bcrsmatrix.hh:817
void usmhv(const field_type &alpha, const X &x, Y &y) const
y += alpha A^H x
Definition: bcrsmatrix.hh:1756
void setIndices(size_type row, It begin, It end)
Set all column indices for row from the given iterator range.
Definition: bcrsmatrix.hh:1192
Definition: basearray.hh:19
void endrowsizes()
indicate that size of all rows is defined
Definition: bcrsmatrix.hh:1107
RealRowIterator()
empty constructor, use with care!
Definition: bcrsmatrix.hh:544
BCRSMatrix(const BCRSMatrix &Mat)
copy constructor
Definition: bcrsmatrix.hh:753
A allocator_type
export the allocator type
Definition: bcrsmatrix.hh:440
This file implements a vector space as a tensor product of a given vector space. The number of compon...
Iterator class for sequential creation of blocks
Definition: bcrsmatrix.hh:917
size_type overflow_total
total number of elements written to the overflow area during construction.
Definition: bcrsmatrix.hh:87
The overflow error used during implicit BCRSMatrix construction was exhausted.
Definition: istlexception.hh:32
void setDataPointers()
Set data pointers for all rows.
Definition: bcrsmatrix.hh:2020
Build in a row-wise manner.
Definition: bcrsmatrix.hh:469
M_ Matrix
The underlying matrix.
Definition: bcrsmatrix.hh:115
ImplicitMatrixBuilder(Matrix &m)
Creates an ImplicitMatrixBuilder for matrix m.
Definition: bcrsmatrix.hh:163
RealRowIterator(const RealRowIterator< ValueType > &it)
Definition: bcrsmatrix.hh:548
size_type N() const
The number of rows in the matrix.
Definition: bcrsmatrix.hh:204
bool exists(size_type i, size_type j) const
return true if (i,j) is in pattern
Definition: bcrsmatrix.hh:1924
A::template rebind< size_type >::other sizeAllocator_
Definition: bcrsmatrix.hh:1944
void mmtv(const X &x, Y &y) const
y -= A^T x
Definition: bcrsmatrix.hh:1682
Iterator beforeEnd()
Definition: bcrsmatrix.hh:636
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
void umv(const X &x, Y &y) const
y += A x
Definition: bcrsmatrix.hh:1591
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
size_type index() const
return index
Definition: bcrsmatrix.hh:554
RealRowIterator(row_type *_p, size_type _i)
constructor
Definition: bcrsmatrix.hh:539
Build entries randomly.
Definition: bcrsmatrix.hh:478
std::shared_ptr< size_type > j_
Definition: bcrsmatrix.hh:1960
size_type N() const
number of rows (counted in blocks)
Definition: bcrsmatrix.hh:1892
row_type * r
Definition: bcrsmatrix.hh:1954
double overflowsize
Definition: bcrsmatrix.hh:1964
size_type index() const
The number of the row that the iterator currently points to.
Definition: bcrsmatrix.hh:1016
size_type maximum
maximum number of non-zeroes per row.
Definition: bcrsmatrix.hh:85
RealRowIterator< const row_type > const_iterator
The const iterator over the matrix rows.
Definition: bcrsmatrix.hh:655
ConstIterator ConstRowIterator
rename the const row iterator for easier access
Definition: bcrsmatrix.hh:686
ConstIterator beforeEnd() const
Definition: bcrsmatrix.hh:673
row_object operator[](size_type i) const
Returns a proxy for entries in row i.
Definition: bcrsmatrix.hh:198
size_type size() const
Get the current row size.
Definition: bcrsmatrix.hh:1037
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
void deallocate(bool deallocateRows=true)
deallocate memory of the matrix.
Definition: bcrsmatrix.hh:2056
row_type::Iterator ColIterator
Iterator for the entries of each row.
Definition: bcrsmatrix.hh:652
RealRowIterator< row_type > Iterator
Definition: bcrsmatrix.hh:620
Proxy row object for entry access.
Definition: bcrsmatrix.hh:129
CreateIterator createbegin()
get initial create iterator
Definition: bcrsmatrix.hh:1055
A::template rebind< row_type >::other rowAllocator_
Definition: bcrsmatrix.hh:1942
Deallocator(typename A::template rebind< size_type >::other &sizeAllocator)
Definition: bcrsmatrix.hh:2111
RealRowIterator< const row_type > ConstIterator
Definition: bcrsmatrix.hh:656
std::ptrdiff_t distanceTo(const RealRowIterator< const ValueType > &other) const
Definition: bcrsmatrix.hh:565
size_type M() const
number of columns (counted in blocks)
Definition: bcrsmatrix.hh:1898
Some handy generic functions for ISTL matrices.
void umtv(const X &x, Y &y) const
y += A^T x
Definition: bcrsmatrix.hh:1663
A::template rebind< B >::other allocator_
Definition: bcrsmatrix.hh:1940
iterator class for sequential access
Definition: basearray.hh:584
size_type getrowsize(size_type i) const
get current number of indices in row i
Definition: bcrsmatrix.hh:1086
FieldTraits< field_type >::real_type frobenius_norm2() const
square of frobenius norm, need for block recursion
Definition: bcrsmatrix.hh:1777
void incrementrowsize(size_type i, size_type s=1)
increment size of row i by s (1 by default)
Definition: bcrsmatrix.hh:1096
Iterator access to matrix rows
Definition: bcrsmatrix.hh:525
A wrapper for uniform access to the BCRSMatrix during and after the build stage in implicit build mod...
Definition: bcrsmatrix.hh:109
void allocateData()
Definition: bcrsmatrix.hh:2172
CreateIterator & operator++()
prefix increment
Definition: bcrsmatrix.hh:935
bool equals(const RealRowIterator< const ValueType > &other) const
equality
Definition: bcrsmatrix.hh:579
void mmhv(const X &x, Y &y) const
y -= A^H x
Definition: bcrsmatrix.hh:1737
size_type nnz_
Definition: bcrsmatrix.hh:1949
BuildMode buildMode() const
The currently selected build mode of the matrix.
Definition: bcrsmatrix.hh:1916
row_type::ConstIterator ConstColIterator
Const iterator to the entries of a row.
Definition: bcrsmatrix.hh:689
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
void addindex(size_type row, size_type col)
add index (row,col) to the matrix
Definition: bcrsmatrix.hh:1149
CompressionStatistics compress()
Finishes the buildstage in implicit mode.
Definition: bcrsmatrix.hh:1317