13#ifndef THREADED_MATRIX
14#define THREADED_MATRIX
18#include <boost/timer/timer.hpp>
20#include "dune/common/config.h"
21#include <dune/istl/bcrsmatrix.hh>
22#include <dune/istl/operators.hh>
47 template <
class Entry,
class Index>
51 using ColIterator =
typename std::vector<Index,NumaAllocator<Index>>::const_iterator;
52 typedef typename std::vector<Entry,NumaAllocator<Entry>>::const_iterator
ValueIterator;
118 template <
class Entry,
class Index>
122 typedef typename std::vector<Index,NumaAllocator<Index>>::const_iterator
ColIterator;
123 typedef typename std::vector<Entry,NumaAllocator<Entry>>::iterator
ValueIterator;
144 template <
class Arguments,
class Operation>
156 template <
class Entry,
class Index>
199 if (ci==
cEnd || *ci!=c)
212 if (ci==
cEnd || *ci!=c)
229 static Entry
const zero(0);
232 if (ci==
cEnd || *ci!=c)
248 if (ci==
cEnd || *ci!=c)
249 throw LookupException(
"write access to nonexistent matrix entry",__FILE__,__LINE__);
256 typedef typename std::vector<Index,NumaAllocator<Index>>::const_iterator
ColIterator;
257 typedef typename std::vector<Entry,NumaAllocator<Entry>>::iterator
ValueIterator;
265 if (0<=row && row<matrix.
N())
267 auto& c = matrix.
chunk(chunk);
269 assert(p.first()<=row && row<p.last());
270 Index first = p.first();
271 cBegin = p.colStartIterator(row-first);
272 cEnd = p.colStartIterator(row-first+1);
273 vBegin = c.valStartIterator(row-first);
274 vEnd = c.valStartIterator(row-first+1);
281 template <
class Entry,
class Index>
286 using value_type = Row;
290 : matrix(const_cast<
NumaBCRSMatrix<Entry,Index>*>(&matrix_)), row(row_), chunk(getChunk(row_))
292 assert(row>=0 && row<=matrix->N());
293 this->
update(*matrix,row,chunk);
300 if (row==matrix->N())
302 while (matrix->getPattern()->rowStart()[chunk+1]==row)
304 this->
update(*matrix,row,chunk);
310 while (matrix->getPattern()->rowStart()[chunk] > row)
312 this->
update(*matrix,row,chunk);
318 if (row > matrix->N())
323 chunk = getChunk(row);
324 this->
update(*matrix,row,chunk);
327 bool operator==(Self
const& it)
const {
return matrix==it.matrix && row==it.row; }
328 bool operator!=(Self
const& it)
const {
return !(*
this==it); }
350 int getChunk(Index r)
const {
return matrix->
getPattern()->chunk(r); }
355 template <
class Entry,
class Index>
391 template <
class Index=
size_t>
429 : rows(rows_), columns(
cols)
430 , sym(symmetric && rows==columns)
433 creators.reserve(
nodes);
435 for (
int i=0; i<
nodes; ++i)
437 creators.push_back(ChunkCreator(i*rows/
nodes,(i+1)*rows/
nodes,
cols,sym,i));
439 creators.push_back(ChunkCreator(i*rows/
nodes,(i+1)*rows/
nodes,
cols,sym,i));
444 this->creators[i].reserve(nnzPerRow);
455 this->creators[i].clear();
479 assert(fromRow>=0 && toRow<=rows && fromRow<=toRow);
480 assert(fromCol>=0 && toCol<=columns && fromCol<=toCol);
482 for (
int r=fromRow; r<toRow; ++r)
483 for (
int c=fromCol; c<toCol; ++c)
501 template <
class IterRow,
class IterCol>
502 void addElements(IterRow
const fromRow, IterRow
const toRow, IterCol
const fromCol, IterCol
const toCol,
bool colIsSorted=
false)
504 assert(fromCol==toCol || *std::max_element(fromCol,toCol) < columns);
505 assert(fromRow==toRow || *std::max_element(fromRow,toRow) < rows);
506 for (
auto& c: creators)
507 c.addElements(fromRow,toRow,fromCol,toCol,colIsSorted);
524 template <
class RowRangeSequence,
class ColRangeSequence>
525 void addElements(RowRangeSequence
const& rrs, ColRangeSequence
const& crs,
bool colsAreSorted=
false)
527 assert(rrs.size() == crs.size());
529 for (
auto c=std::begin(crs); c!=std::end(crs); ++c)
530 assert((*c).empty() || (*std::max_element(std::begin(*c),std::end(*c)) < columns));
533 auto c = std::begin(crs);
534 for (
auto r=std::begin(rrs); r!=std::end(rrs); ++r, ++c)
535 this->creators[i].
addElements(std::begin(*r),std::end(*r),std::begin(*c),std::end(*c),colsAreSorted);
549 this->creators[i].addAllElements(this->columns);
558 for (Index i=0; i<std::min(rows,columns); ++i)
570 for (
auto const& c: creators)
571 nnz += c.nonzeroes();
578 Index
cols()
const {
return columns; }
594 int nodes()
const {
return creators.size(); }
600 ChunkCreator
const&
creator(
int node)
const {
return creators[node]; }
611 std::vector<ChunkCreator> creators;
622 template <
class Index=
size_t>
644 template <
class IndexA>
646 : patterns(p.
nodes())
651 for (
int i=0; i<p.
nodes(); ++i)
652 patterns[i] = std::make_shared<ChunkPattern>(*p.
pattern(i));
659 : patterns(creator.
nodes()), sym(creator.
isSymmetric()), rowSt(creator.
nodes()+1), cols(creator.cols())
663 this->patterns[i] = std::make_shared<ChunkPattern>(creator.
creator(i));
664 this->rowSt[i] = this->patterns[i]->first();
666 rowSt[patterns.size()] = patterns.back()->last();
682 template <
class Expanded,
class Condensed,
class Matrix>
685 , rowSt(eIndices.size(),0), cols(eIndices.size())
687 assert(mat.M()==mat.N());
688 assert(eIndices.size() <= cIndices.size());
690 for (Index r=0; r<eIndices.size(); ++r)
692 auto row = mat[eIndices[r]];
693 for (
auto ci=row.begin(); ci!=row.end(); ++ci)
694 if (cIndices[ci.index()] < eIndices.size())
700 for (
int i=0; i<patterns.size(); ++i)
701 patterns[i] = std::make_shared<ChunkPattern>(rowSt[i],rowSt[i+1],eIndices,cIndices,mat,i);
718 template <
class Matrix>
720 : sym(symmetric), cols(isTransposed? matrix.
N(): matrix.
M())
725 Index rows = isTransposed? matrix.M(): matrix.N();
726 std::vector<size_t> rowCount(rows,0);
731 patterns.reserve(rowCount.size()-1);
732 for (
int i=0; i<rowCount.size()-1; ++i)
733 patterns.push_back(std::make_shared<ChunkPattern>(matrix,
isSymmetric,isTransposed,
734 rowCount[i],rowCount[i+1],symmetric,i));
736 rowSt.resize(patterns.size()+1);
737 for (
int i=0; i<patterns.size(); ++i)
738 rowSt[i] = patterns[i]->first();
739 rowSt[patterns.size()] = patterns.back()->last();
749 Index
N()
const {
return rowSt.back(); }
754 Index
M()
const {
return cols; }
764 for (
auto const& p: patterns)
775 for (
auto const& p: patterns)
776 nnz += p->nonzeroes();
783 int nodes()
const {
return patterns.size(); }
788 std::shared_ptr<ChunkPattern>
pattern(
int i)
const
808 assert(0<=row && row<=
N());
809 auto it = std::upper_bound(rowSt.begin(),rowSt.end(),row);
810 int c = it - rowSt.begin() - 1;
811 assert(0<=c && c<=patterns.size());
812 assert(c==patterns.size() || (patterns[c]->first() <= row && row < patterns[c]->last()));
822 std::vector<Index>
const&
rowStart()
const {
return rowSt; }
829 return patterns[
chunk(r)]->exists(r,c);
833 std::vector<std::shared_ptr<ChunkPattern>> patterns;
835 std::vector<Index> rowSt;
841 template <
class Index,
class Index2>
844 assert(pa.
N()==pb.
N() && pa.
M()==pb.
M());
848 for (
int i=0; i<pa.
nodes(); ++i)
851 auto const& chunkPattern = *pa.
pattern(i);
852 Index first = chunkPattern.first();
853 for (Index row=first; row<chunkPattern.last(); ++row)
854 for (
auto it=chunkPattern.colStartIterator(row-first); it!=chunkPattern.colStartIterator(row+1-first); ++it)
862 for (
int i=0; i<pb.
nodes(); ++i)
865 auto const& chunkPattern = *pb.
pattern(i);
866 Index2 first = chunkPattern.first();
867 for (Index2 row=first; row<chunkPattern.last(); ++row)
868 for (
auto it=chunkPattern.colStartIterator(row-first); it!=chunkPattern.colStartIterator(row+1-first); ++it)
870 creator.
addElement(
static_cast<Index
>(row),*it);
872 creator.
addElement(*it,
static_cast<Index
>(row));
919 template <
class Entry,
class Index=
size_t>
992 for (
int i=0; i<pattern->nodes(); ++i)
993 chunks.push_back(Chunk(i));
997 this->chunks[i] = std::move(Chunk(this->pattern->pattern(i),init));
1000 dps.resize(chunks.size());
1008 template <
class Expanded,
class Condensed,
class Matrix>
1010 : pattern(std::make_shared<
NumaCRSPattern<Index>>(eIndices,cIndices,mat))
1012 chunks.reserve(pattern->nodes());
1013 for (
int i=0; i<pattern->nodes(); ++i)
1014 chunks.push_back(Chunk(pattern->pattern(i),eIndices,cIndices,mat));
1015 dps.resize(chunks.size());
1024 template <
class Expanded>
1025 static std::vector<Index> createCondensed(Expanded
const& eIndices, Index n)
1027 std::vector<Index> cIndices(n,eIndices.size());
1028 for (Index i=0; i<eIndices.size(); ++i)
1029 cIndices[eIndices[i]] = i;
1047 template <
class Expanded,
class Matrix>
1064 template <
class Matrix>
1066 bool isSymmetric,
bool isTransposed)
1069 chunks.reserve(pattern->nodes());
1070 for (
int i=0; i<pattern->nodes(); ++i)
1071 chunks.push_back(Chunk(pattern->pattern(i),matrix,isSymmetric,isTransposed));
1072 dps.resize(chunks.size());
1087 template <
class OtherEntry>
1109 template <
class Matrix>
1111 bool isSymmetric,
bool isTransposed=
false,
bool symmetric=
false)
1113 matrix,isSymmetric,isTransposed)
1115 assert(matrix.N()==matrix.M() || !(symmetric||isSymmetric));
1116 assert(Entry::rows==Entry::cols || !(symmetric||isSymmetric));
1119 symmetric &= Entry::rows==Entry::cols && matrix.N()==matrix.M();
1120 isSymmetric &= Entry::rows==Entry::cols && matrix.N()==matrix.M();
1142 bool isSymmetric,
bool isTransposed=
false,
bool symmetric=
false)
1143 :
NumaBCRSMatrix(isTransposed==false && isSymmetric==symmetric? matrix.pattern
1144 : std::make_shared<
NumaCRSPattern<Index>>(matrix,isSymmetric,isTransposed,symmetric),
1145 matrix,isSymmetric,isTransposed)
1147 assert(matrix.
N()==matrix.
M() || !(symmetric||isSymmetric));
1148 assert(Entry::rows==Entry::cols || !(symmetric||isSymmetric));
1149 assert(isTransposed? (matrix.
N()==
M() && matrix.
M()==
N()): (matrix.
N()==
N() && matrix.
M()==
M()));
1150 assert(!isTransposed || Entry::rows==Entry::cols);
1166 template <
class TripletIndex,
class TripletScalar>
1167 NumaBCRSMatrix(std::vector<TripletIndex>
const& ridx, std::vector<TripletIndex>
const& cidx,
1168 TripletIndex nRows, TripletIndex nCols,
1169 std::vector<TripletScalar>
const& entries)
1175 assert(ridx.size()==cidx.size() && cidx.size()==entries.size());
1179 for (
size_t i=0; i<entries.size(); ++i)
1184 for (
size_t i=0; i<entries.size(); ++i)
1185 if constexpr (nr==1 && nc==1)
1186 A[ridx[i]][cidx[i]] += entries[i];
1188 A[ridx[i]/nr][cidx[i]/nc][ridx[i]%nr][cidx[i]%nc] += entries[i];
1190 *
this = std::move(A);
1222 template <
class EntryA,
class IndexA,
1223 typename = std::enable_if_t<std::is_convertible_v<EntryA,Entry>>>
1228 if constexpr (std::is_same_v<Index,IndexA>)
1234 chunks.reserve(pattern->nodes());
1235 for (
int i=0; i<pattern->nodes(); ++i)
1236 chunks.push_back(Chunk(pattern->pattern(i),A.
chunk(i)));
1237 dps.resize(chunks.size());
1247 if (chunks.size()>0)
1250 this->chunks[i] = a;
1261 return *
this = Entry(a);
1267 template <
class Arguments,
class Operation>
1270 if (chunks.size()>0)
1273 this->chunks[i] = e[i];
1284 template <
class EntryB,
class IndexB>
1287 entrywiseOp([](Entry
const& a, Entry
const& b) {
return a+b; }, B);
1296 template <
class EntryB,
class IndexB>
1299 entrywiseOp([](Entry
const& a, Entry
const& b) {
return a-b; }, B);
1308 template <
class Factor>
1311 entrywiseOp([=](Entry
const& e, Entry
const&) {
return a*e; }, *
this);
1365 template <
class RowIndices,
class ColIndices>
1366 Self
operator()(RowIndices
const& ri, ColIndices
const& ci)
const
1383 Index
N()
const {
return pattern->N(); }
1388 Index
M()
const {
return pattern->M(); }
1398 return pattern->nonzeroes();
1427 return pattern->exists(r,c);
1446 for (
auto ri=
begin(); ri!=
end(); ++ri)
1447 for (
auto ci=ri->begin(); ci!=ri->end(); ++ci)
1448 sum += ci->frobenius_norm2();
1464 template <
class X,
class Y>
1475 template <
class X,
class Y>
1476 void mtv(X
const& x, Y& y)
const { doMtv(1.0,x,y,
true); }
1481 template <
class X,
class Y>
1482 void mmv(X
const& x, Y& y)
const { doMv(-1.0,x,y,
true); }
1487 template <
class X,
class Y>
1496 template <
class X,
class Y>
1502 template <
class X,
class Y>
1511 template <
class X,
class Y>
1512 void umtv(X
const& x, Y& y)
const { doMtv(1.0,x,y,
false); }
1517 template <
class X,
class Y>
1526 template <
class X,
class Y>
1560 template <
class LMIterator>
1563 for (
auto& c: chunks)
1564 c.scatter(first,last);
1591 template <
class RowIndices,
class ColIndices,
class BinaryOp=std::plus<Entry>>
1593 BinaryOp
const& op = BinaryOp())
1596 for (
int r=0; r<rows.size(); ++r)
1597 for (
int c=0; c<cols.size(); ++c)
1599 auto ci = (*this)[rows[r]].find(cols[c]);
1600 assert(ci!=(*
this)[rows[r]].
end());
1601 *ci = op(*ci,B[r][c]);
1609 std::shared_ptr<NumaCRSPattern<Index>> pattern;
1610 std::vector<Chunk> chunks;
1611 mutable std::vector<field_type> dps;
1615 template <
class X,
class Y>
1616 Scalar doMv(
Scalar const& a, X
const& x, Y& y,
bool initToZero)
const
1618 assert(y.size()==
N());
1619 assert(x.size()==
M());
1621 if (chunks.size()>1)
1625 if (pattern->isSymmetric())
1626 for (
int k=i; k<n; ++k)
1627 this->chunks[i].gatherMirrored(a,x,y,this->chunks[k],
true,initToZero&&k==i);
1628 this->dps[i] = this->chunks[i].apply(a,x,y,initToZero&&!pattern->isSymmetric());
1631 return std::accumulate(dps.begin(),dps.end(),0.0);
1635 if (pattern->isSymmetric())
1636 chunks[0].gatherMirrored(a,x,y,chunks[0],
true,initToZero);
1637 return chunks[0].apply(a,x,y,initToZero&&!pattern->isSymmetric());
1642 template <
class X,
class Y>
1643 void doMtv(
Scalar const& a, X
const& x, Y& y,
bool initToZero)
const
1645 assert(y.size()==
M());
1646 assert(x.size()==
N());
1648 if (pattern->isSymmetric())
1649 doMv(a,x,y,initToZero);
1654 for (
auto const&
chunk: chunks)
1655 chunk.applyTransposed(a,x,y);
1662 template <
class F,
class EntryB,
class IndexB>
1665 assert(
N()==B.N() &&
M()==B.M());
1668 this->chunks[i].entrywiseOp(f,B);
1682 template <
class Entry,
class Index>
1688 for (
auto ri=A.
begin(); ri!=A.
end(); ++ri)
1689 for (
auto ci=ri->begin(); ci!=ri->end(); ++ci)
1690 if constexpr (nr>1 || nc>1)
1692 for (
int k=0; k<nr; ++k)
1693 for (
int l=0; l<nc; ++l)
1694 out << nr*ri.index()+k <<
' ' << nc*ci.index()+l <<
' ' << (*ci)[k][l] <<
'\n';
1697 out << ri.index() <<
' ' << ci.index() <<
' ' << *ci <<
'\n';
1712 template <
class SparseMatrix>
1716 for (
auto ri=A.begin(); ri!=A.end(); ++ri)
1717 for (
auto ci=ri->begin(); ci!=ri->end(); ++ci)
1718 B[ri.index()][ci.index()] = *ci;
1739 template <
class SparseMatrix,
class RowRange,
class ColRange>
1744 using Index =
typename ColRange::value_type;
1748 std::vector<std::pair<Index,Index>> cidx;
1749 cidx.reserve(cols.size());
1754 cidx.push_back(std::make_pair(c,i));
1762 for (
size_t r=0; r<rows.size(); ++r)
1763 for (
auto ci=A[rows[r]].
begin(); ci!=A[rows[r]].end(); ++ci)
1765 auto it = std::lower_bound(
begin(cidx),
end(cidx),
1766 std::make_pair(ci.index(),ci.index()),
FirstLess());
1767 if (it!=
end(cidx) && it->first==ci.index())
1768 B[r][it->second] = *ci;
1783 template <
class Entry,
class Index,
class Index2>
1791 for (Index row=0; row<AB.
N(); ++row)
1793 auto ABrow = AB[row];
1794 for (
auto ci=A[row].
begin(); ci!=A[row].
end(); ++ci)
1795 ABrow[ci.index()] += *ci;
1796 for (
auto ci=B[row].
begin(); ci!=B[row].
end(); ++ci)
1797 ABrow[
static_cast<Index
>(ci.index())] += *ci;
1816 template <
class Scalar,
int n,
class Index=
size_t>
1820 for (Index i=0; i<N; ++i)
1840 template <
class Scalar,
int n,
int m,
class Index=
size_t>
1854 template <
class Entry,
class Index>
1877 template <
class Target,
class Source,
class RowIndices,
class ColIndices>
1881 std::vector<size_t> rows(A.N()), cols(A.M());
1882 std::iota(rows.begin(),rows.end(),0);
1883 std::iota(cols.begin(),cols.end(),0);
1886 for(
int i=0; i<ri.size();++i)
1887 rows.erase(rows.begin() + ri[i]);
1890 for(
int i=0; i<ci.size();++i)
1891 cols.erase(cols.begin() + ci[i]);
1908 template <
class Target,
class Source,
class RowIndices>
1912 std::vector<size_t> cols;
1928 template <
class Target,
class Source,
class ColIndices>
1932 std::vector<size_t> rows;
1947 template <
class Source>
1953 std::vector<size_t> nzFlags(A.M(),0);
1954 for(
auto r=A.begin(); r!=A.end(); ++r)
1955 for(
auto c=r->begin(); c!= r->end(); ++c)
1956 nzFlags.at(c.index()) = 1;
1959 std::vector<size_t> nzCols;
1960 for(
int i=0; i<nzFlags.size(); ++i)
1961 if(nzFlags.at(i) == 1) nzCols.push_back(i);
1984 template<
int row2,
int col2,
int row1,
int col1,
class Scalar,
class Index>
1990 if constexpr (row2==row1 && col2==col1)
1993 static_assert(row1 % row2 == 0);
1994 static_assert(col1 % col2 == 0);
1995 int const qN = row1/row2;
1996 int const qM = col1/col2;
2002 for(
auto row=A.begin(); row!=A.end(); ++row)
2003 for(
auto col=row->begin(); col!=row->end(); ++col)
2004 for(
int i=0; i<qN; ++i)
2005 for(
int j=0; j<qM; ++j)
2006 creator.
addElement(row.index()*qN + i, col.index()*qM + j);
2011 for(
auto row=A.begin(); row!=A.end(); ++row)
2012 for(
auto col=row->begin(); col!=row->end(); ++col)
2015 for(
int k=0; k<row1; ++k)
2016 for(
int l=0; l<col1; ++l)
2017 B[row.index()*qN + k/row2][col.index()*qM + l/col2][k%row2][l%col2] = mat[k][l];
2037 template<
int blockrows,
int blockcols,
class Scalar,
class Index>
2042 assert(A.N() == B.N());
2043 Index cols = (A.M() + B.M());
2049 for(
auto row=A.begin(); row!=A.end(); ++row)
2050 for(
auto col=row->begin(); col!=row->end(); ++col)
2051 creator.
addElement(row.index(), col.index());
2053 for(
auto row=B.begin(); row!=B.end(); ++row)
2054 for(
auto col=row->begin(); col!=row->end(); ++col)
2055 creator.
addElement(row.index(), col.index()+A.M());
2060 for(
auto row=A.begin(); row!=A.end(); ++row)
2061 for(
auto col=row->begin(); col!=row->end(); ++col)
2062 result[row.index()][col.index()] = *col;
2064 for(
auto row=B.begin(); row!=B.end(); ++row)
2065 for(
auto col=row->begin(); col!=row->end(); ++col)
2066 result[row.index()][col.index()+A.M()] = *col;
2084 template<
int blockrows,
int blockcols,
class Scalar,
class Index>
2089 assert(A.M() == B.M());
2091 Index rows = (A.N()+B.N());
2095 for(
auto row=A.begin(); row!=A.end(); ++row)
2096 for(
auto col=row->begin(); col!=row->end(); ++col)
2097 creator.
addElement(row.index(), col.index());
2099 for(
auto row=B.begin(); row!=B.end(); ++row)
2100 for(
auto col=row->begin(); col!=row->end(); ++col)
2101 creator.
addElement(row.index()+A.N(), col.index());
2106 for(
auto row=A.begin(); row!=A.end(); ++row)
2107 for(
auto col=row->begin(); col!=row->end(); ++col)
2108 result[row.index()][col.index()] = *col;
2110 for(
auto row=B.begin(); row!=B.end(); ++row)
2111 for(
auto col=row->begin(); col!=row->end(); ++col)
2112 result[row.index()+A.N()][col.index()] = *col;
2131 template<
int blockrows,
int blockcols,
class Index=
size_t>
2135 Index cols = (A.M()+B.M());
2136 Index rows = (A.N()+B.N());
2140 for(
auto row=A.begin(); row!=A.end(); ++row)
2141 for(
auto col=row->begin(); col!=row->end(); ++col)
2142 creator.
addElement(row.index(), col.index());
2144 for(
auto row=B.begin(); row!=B.end(); ++row)
2145 for(
auto col=row->begin(); col!=row->end(); ++col)
2146 creator.
addElement(row.index()+A.N(), col.index()+A.M());
2151 for(
auto row=A.begin(); row!=A.end(); ++row)
2152 for(
auto col=row->begin(); col!=row->end(); ++col)
2155 for(
int k=0; k < blockrows; ++k)
2156 for(
int l= 0; l < blockcols; ++l)
2157 result[row.index()][col.index()][k][l] =mat[k][l];
2160 for(
auto row=B.begin(); row!=B.end(); ++row)
2161 for(
auto col=row->begin(); col!=row->end(); ++col)
2164 for(
int k=0; k < blockrows; ++k)
2165 for(
int l= 0; l < blockcols; ++l)
2166 result[row.index()+A.N()][col.index()+A.M()][k][l] = mat[k][l];
A LAPACK-compatible dense matrix class with shape specified at runtime.
An exception that can be thrown whenever a key lookup fails.
A NUMA-aware compressed row storage matrix adhering mostly to the Dune ISTL interface (to complete....
iterator ConstRowIterator
iterator end()
returns an iterator to the first row
Self & operator-=(NumaBCRSMatrix< EntryB, IndexB > const &B)
Subtracts a sparse matrix from this one.
NumaBCRSMatrix(Expanded const &eIndices, Condensed const &cIndices, Matrix const &mat)
Deprecated. Use simpler version without cIndices instead.
const_iterator const_row_type
void mmv(X const &x, Y &y) const
Matrix-vector multiplication .
NumaBCRSMatrix(NumaBCRSMatrix< OtherEntry, Index > const &matrix)
Constructor copying a given matrix.
NumaBCRSMatrix(Matrix const &matrix, bool isSymmetric, bool isTransposed=false, bool symmetric=false)
Constructor.
Self & operator=(Self &&mat)=default
Move assignment.
Chunk & chunk(int i)
Obtains a reference to the given chunk.
Self & operator=(ThreadedMatrixDetail::NumaBCRSMatrixExpression< Arguments, Operation > const &e)
NOT YET IMPLEMENTED Assigns the given Numa matrix expression.
Self & operator+=(NumaBCRSMatrix< EntryB, IndexB > const &B)
Adds a sparse matrix to this one.
size_t nonzeroes() const
Returns the number of structurally nonzero elements.
Chunk const & chunk(int i) const
NumaBCRSMatrix(Self &&A)=default
Move constructor.
Self & operator*=(Factor a)
Multiplication with a "scalar".
NumaBCRSMatrix(std::shared_ptr< NumaCRSPattern< Index > > const &pattern_, Entry const &init=Entry(0))
Constructor creating a matrix from a given sparsity pattern.
std::shared_ptr< NumaCRSPattern< Index > > getPattern() const
Returns a pointer to the sparsity pattern.
Dune::FieldMatrix< Scalar, 1, 1 > value_type
Self & operator=(typename Entry::field_type const &a)
Assigns the given scalar value to each entry.
NumaBCRSMatrix(NumaCRSPatternCreator< Index > const &creator, Entry const &init=Entry(0))
Constructor creating a matrix from a given sparsity pattern creator.
bool exists(Index r, Index c) const
returns true if (r,c) is structurally nonzero
void scatter(DynamicMatrix< Entry > const &B, RowIndices const &rows, ColIndices const &cols, BinaryOp const &op=BinaryOp())
Scatters given submarix into the matrix.
NumaBCRSMatrix(std::vector< TripletIndex > const &ridx, std::vector< TripletIndex > const &cidx, TripletIndex nRows, TripletIndex nCols, std::vector< TripletScalar > const &entries)
Constructor.
field_type smv(field_type const &a, X const &x, Y &y) const
Matrix-vector multiplication .
void scatter(LMIterator first, LMIterator last)
Scatters given sub-matrices into the matrix by adding up their entries.
const_iterator end() const
NumaBCRSMatrix()
Constructs an empty 0x0 matrix.
NumaBCRSMatrix(Self const &A)=default
Copy constructor.
Self & operator=(NumaBCRSMatrix< EntryA, IndexA > const &A)
Copy assignment from another NumaBCRSMatrix, with compatible entry and index types.
NumaBCRSMatrix(std::shared_ptr< NumaCRSPattern< Index > > const &pattern_, Matrix const &matrix, bool isSymmetric, bool isTransposed)
Constructor copying a given matrix.
ThreadedMatrixDetail::NumaBCRSMatrixIterator< Dune::FieldMatrix< Scalar, 1, 1 >, Index > iterator
Self & operator=(Entry const &a)
Assigns the given value to each entry.
NumaBCRSMatrix(Expanded const &eIndices, Matrix const &mat)
Indexed submatrix constructor.
iterator begin()
returns an iterator to the first row
field_type usmv(field_type const &a, X const &x, Y &y) const
Matrix-vector multiplication and subsequent computation of if A is square.
row_type operator[](Index r)
Subscript operator allowing random access to rows.
const_row_type operator[](Index r) const
Dune::FieldMatrix< Scalar, 1, 1 > block_type
void umtv(X const &x, Y &y) const
Matrix-vector multiplication .
const_iterator begin() const
typename iterator::Iterator ColIterator
field_type frobenius_norm() const
Computes the Frobenius norm .
ThreadedMatrixDetail::NumaBCRSMatrixConstIterator< Dune::FieldMatrix< Scalar, 1, 1 >, Index > const_iterator
NumaBCRSMatrix(NumaBCRSMatrix< Entry, Index > const &matrix, bool isSymmetric, bool isTransposed=false, bool symmetric=false)
Constructor.
Self & operator=(Self const &mat)=default
Copy assignment.
void smtv(field_type const &a, X const &x, Y &y) const
Matrix-vector multiplication .
void usmtv(field_type const &a, X const &x, Y &y) const
Matrix-vector multiplication .
field_type umv(X const &x, Y &y) const
Matrix-vector multiplication and subsequent computation of if A is square.
Self operator()(RowIndices const &ri, ColIndices const &ci) const
Index M() const
The number of columns.
void mtv(X const &x, Y &y) const
Matrix-vector multiplication .
typename iterator::ConstIterator ConstColIterator
field_type frobenius_norm2() const
Computes the square of the Frobenius norm .
Index N() const
The number of rows.
ScalarType< Dune::FieldMatrix< Scalar, 1, 1 > > Scalar
field_type mv(X const &x, Y &y) const
Matrix-vector multiplication with computation of if A is square.
A NUMA-aware creator for matrix sparsity patterns.
Index cols() const
The number of columns.
void addElements(IterRow const fromRow, IterRow const toRow, IterCol const fromCol, IterCol const toCol, bool colIsSorted=false)
Enters entries into the sparsity pattern.
ChunkCreator const & creator(int node) const
Returns the chunk creator.
void balance()
Redistributes the rows to the NUMA chunks in order to have the same number of entries in each chunk.
void addElement(Index row, Index col)
Enters a single entry into the sparsity pattern.
int nodes() const
Returns the number of NUMA nodes/chunks used.
void addElements(RowRangeSequence const &rrs, ColRangeSequence const &crs, bool colsAreSorted=false)
Enters elements into the sparsity pattern.
size_t nonzeroes() const
The number of structurally nonzero elements.
void addAllElements()
Enters all possible elements (defining a dense matrix).
bool isSymmetric() const
Returns the symmetry status of the pattern.
NumaCRSPatternCreator(Index rows_, Index cols, bool symmetric=false, int nnzPerRow=0)
Constructs a rows times cols matrix sparsity structure creator.
void addDiagonal()
Enters the diagonal elements.
void addDenseBlock(Index fromRow, Index toRow, Index fromCol, Index toCol)
Enters a contiguous dense block into the sparsity pattern.
A NUMA-aware compressed row storage sparsity pattern.
std::shared_ptr< ChunkPattern > pattern(int i) const
Returns the individual pattern of chunk i.
int nodes() const
Returns the number of NUMA nodes/chunks used.
int chunk(Index row) const
Returns the number of the chunk containing the given row.
Index N() const
The number of rows.
NumaCRSPattern()
Constructs an empty 0x0 pattern.
NumaCRSPattern(NumaCRSPatternCreator< Index > const &creator)
Constructor creating a sparsity pattern from the given creator.
NumaCRSPattern(Expanded const &eIndices, Condensed const &cIndices, Matrix const &mat)
Constructor.
NumaCRSPattern(NumaCRSPattern< IndexA > const &p)
Copy constructor.
bool isSymmetric() const
Returns the symmetry status of the pattern.
Index M() const
The number of columns.
std::vector< Index > const & rowStart() const
Returns the limiting row indices between the chunks.
size_t nonzeroes() const
Returns the number of structurally nonzero entries.
size_t storage() const
Returns the number of stored entries.
bool exists(Index r, Index c) const
queries whether an entry is present in the sparsity pattern
NumaCRSPattern(Matrix const &matrix, bool isSymmetric, bool isTransposed, bool symmetric)
Constructor extracting the sparsity pattern of a given matrix (usually a Dune::BCRSMatrix).
Implementation of thread pools suitable for parallelization of (more or less) memory-bound algorithms...
static NumaThreadPool & instance(int maxThreads=std::numeric_limits< int >::max())
Returns a globally unique thread pool instance.
int nodes() const
Reports the number of NUMA nodes (i.e., memory interfaces/CPU sockets)
This class stores a couple of compressed row storage rows in memory allocated locally on a NUMA node.
CRSChunkPattern< Index > const & pattern() const
Returns the sparsity pattern of this chunk.
A class supporting two-stage construction of sparsity patterns of NUMA matrix chunks.
This class maintains the sparsity structure of a couple of matrix rows (a NUMA matrix chunk).
bool operator!=(Self const &it) const
NumaBCRSMatrixConstIterator(NumaBCRSMatrix< Entry, Index > const &matrix_, Index row_)
Row const * operator->() const
Row const & operator*() const
Dereferentiation yields the row.
bool operator==(Self const &it) const
void operator+=(Index inc)
An iterator stepping through all entries in a row.
std::random_access_iterator_tag iterator_category
bool operator!=(Self const &it) const
NumaBCRSMatrixConstRowIterator(ColIterator col_, ValueIterator val_)
NumaBCRSMatrixConstRowIterator< Entry, Index > Self
std::vector< Entry, NumaAllocator< Entry > >::const_iterator ValueIterator
Entry const * operator->() const
NumaBCRSMatrixConstRowIterator()=default
Default constructor.
typename std::vector< Index, NumaAllocator< Index > >::const_iterator ColIterator
Entry const & operator*() const
bool operator==(Self const &it) const
difference_type operator-(Self const &it) const
std::ptrdiff_t difference_type
ThreadedMatrixDetail::NumaBCRSMatrixExpressionChunk< Arguments, Operation > const & operator[](int i) const
NumaBCRSMatrixIterator(NumaBCRSMatrix< Entry, Index > &matrix_, Index row_)
Entry & operator*() const
Entry * operator->() const
std::vector< Entry, NumaAllocator< Entry > >::iterator ValueIterator
NumaBCRSMatrixRowIterator(ColIterator col_, ValueIterator val_)
NumaBCRSMatrixRowIterator()=default
std::vector< Index, NumaAllocator< Index > >::const_iterator ColIterator
Entry const & operator[](Index c) const
Random read access to row entries by global column index.
NumaBCRSMatrixRowIterator< Entry, Index > Iterator
void update(NumaBCRSMatrix< Entry, Index > &matrix, Index row, int chunk)
Entry & operator[](Index c)
Random write access to row entries by global column index.
ConstIterator end() const
Iterator find(Index c)
Looks up a particular entry specified by column index.
ConstIterator begin() const
Start of row entries.
size_type size() const
Returns the number of entries in the current row.
Index const * getindexptr() const
Low-level access to array of (sorted) column indices in this row.
block_type const * getptr() const
Low-level access to array of values in this row.
std::vector< Index, NumaAllocator< Index > >::const_iterator ColIterator
NumaBCRSMatrixConstRowIterator< Entry, Index > ConstIterator
std::vector< Entry, NumaAllocator< Entry > >::iterator ValueIterator
ConstIterator find(Index c) const
Looks up a particular entry specified by column index.
This file contains various utility functions that augment the basic functionality of Dune.
NumaBCRSMatrix< Dune::FieldMatrix< Scalar, row2, col2 >, Index > reshapeBlocks(NumaBCRSMatrix< Dune::FieldMatrix< Scalar, row1, col1 >, Index > const &A)
reshapes NumaBRCSMAtrix entry block structure
NumaBCRSMatrix< Dune::FieldMatrix< Scalar, n, n >, Index > sparseUnitMatrix(Index N)
creates a unit matrix in NUMA block compressed row storage format
NumaBCRSMatrix< Dune::FieldMatrix< Scalar, blockrows, blockcols >, Index > vertcat(NumaBCRSMatrix< Dune::FieldMatrix< Scalar, blockrows, blockcols >, Index > const &A, NumaBCRSMatrix< Dune::FieldMatrix< Scalar, blockrows, blockcols >, Index > const &B)
concatenate two matrices vertically
auto transpose(NumaBCRSMatrix< Entry, Index > const &A)
Creates the transposed sparse matrix .
DynamicMatrix< typename SparseMatrix::block_type > full(SparseMatrix const &A)
Converts a sparse NumaBCRSMatrix to a dense matrix.
Target submatrix(Source const &A, RowIndices const &ri, ColIndices const &ci)
extracts a sparse or dense submatrix of
Dune::FieldMatrix< T, n, n > unitMatrix()
Returns the identity matrix of size n .
Target eraseCols(Source const &A, ColIndices const &ci)
"deletes" columns by extracting a copy of the matrix keeping only the non-deleted columns.
NumaBCRSMatrix< Dune::FieldMatrix< Scalar, n, m >, Index > sparseZeroMatrix(Index N, Index M)
creates a zero matrix in NUMA block compressed row storage format
DynamicMatrix< typename SparseMatrix::block_type > full(SparseMatrix const &A, RowRange const &rows, ColRange const &cols)
Converts a subrange of a sparse matrix to a dense matrix.
Target eraseRows(Source const &A, RowIndices const &ri)
"deletes" rows by extracting a copy of the matrix keeping only the non-deleted rows.
DynamicMatrix< MEntry > submatrix(DynamicMatrix< MEntry > const &A, int rowstart, int rowend, int colstart, int colend)
extracts contiguous submatrices, copying the data
NumaBCRSMatrix< Dune::FieldMatrix< Scalar, blockrows, blockcols >, Index > horzcat(NumaBCRSMatrix< Dune::FieldMatrix< Scalar, blockrows, blockcols >, Index > const &A, NumaBCRSMatrix< Dune::FieldMatrix< Scalar, blockrows, blockcols >, Index > const &B)
concatenate two matrices horizontally
Target eraseRowsNCols(Source const &A, RowIndices const &ri, ColIndices const &ci)
"deletes" rows/columns by extracting a copy of the matrix keeping the non-deleted rows/columns.
std::vector< size_t > nonZeroColumns(Source const &A)
returns all indices of nonzero columns.
NumaBCRSMatrix< Dune::FieldMatrix< double, blockrows, blockcols >, Index > diagcat(NumaBCRSMatrix< Dune::FieldMatrix< double, blockrows, blockcols >, Index > const &A, NumaBCRSMatrix< Dune::FieldMatrix< double, blockrows, blockcols >, Index > const &B)
concatenate two matrices diagonally, resulting matrix is zero on the off block-diagonal
NumaBCRSMatrix< Entry, Index > operator+(NumaBCRSMatrix< Entry, Index > const &A, NumaBCRSMatrix< Entry, Index2 > const &B)
Matrix addition . The sparsity patterns of both matrices can be different. The size of the matrices h...
void parallelForNodes(Func const &f, int maxTasks=std::numeric_limits< int >::max())
A parallel for loop that executes the given functor in parallel on different NUMA nodes.
void equalWeightRanges(std::vector< Index > &x, int n)
Computes partitioning points such that the sum of weights in each partition is roughly the same.
void getRowCount(Dune::BCRSMatrix< Entry, Allocator > const &matrix, bool isTransposed, std::vector< Index > &rowCount)
typename GetScalar< Type >::type ScalarType
Extracts the scalar field type from linear algebra data types.
NumaCRSPattern< Index > operator+(NumaCRSPattern< Index > const &pa, NumaCRSPattern< Index2 > const &pb)
std::ostream & operator<<(std::ostream &stream, VTKReader::FunctionInfo const &info)
A comparator functor that supports sorting std::pair by their first component.