2 #ifndef DUNE_PDELAB_BACKEND_SIMPLE_SPARSE_HH 3 #define DUNE_PDELAB_BACKEND_SIMPLE_SPARSE_HH 10 #include <unordered_set> 12 #include <dune/common/typetraits.hh> 23 :
public std::vector< std::unordered_set<std::size_t> >
28 typedef std::unordered_set<std::size_t>
col_type;
30 template<
typename RI,
typename CI>
33 (*this)[ri.back()].insert(ci.back());
37 :
std::vector<
std::unordered_set<
std::size_t> >(rows)
42 template<
template<
typename>
class C,
typename ET,
typename I>
76 template<
typename GFSV,
typename GFSU,
template<
typename>
class C,
typename ET,
typename I>
78 :
public Backend::impl::Wrapper<SparseMatrixData<C,ET,I> >
87 friend Backend::impl::Wrapper<Container>;
100 typedef typename GFSV::Ordering::Traits::ContainerIndex
RowIndex;
101 typedef typename GFSU::Ordering::Traits::ContainerIndex
ColIndex;
103 template<
typename RowCache,
typename ColCache>
106 template<
typename RowCache,
typename ColCache>
111 template<
typename GO>
113 : _container(
std::make_shared<Container>())
115 allocate_matrix(_container, go, ElementType(0));
118 template<
typename GO>
120 : _container(
std::make_shared<Container>())
122 allocate_matrix(_container, go, e);
131 : _container(
std::make_shared<Container>())
135 : _container(
std::make_shared<Container>(*(rhs._container)))
148 _container = std::make_shared<Container>(*(rhs.
_container));
158 void attach(std::shared_ptr<Container> container)
160 _container = container;
165 return bool(_container);
168 const std::shared_ptr<Container>&
storage()
const 175 return _container->_rows;
180 return _container->_cols;
185 std::fill(_container->_data.begin(),_container->_data.end(),
e);
191 using namespace std::placeholders;
192 std::transform(_container->_data.begin(),_container->_data.end(),_container->_data.begin(),std::bind(std::multiplies<ET>(),e,_1));
197 void mv(
const V& x, V& y)
const 199 assert(y.N() == N());
200 assert(x.N() == M());
201 for (std::size_t r = 0; r < N(); ++r)
203 y.base()[r] = sparse_inner_product(r,x);
208 void usmv(
const ElementType alpha,
const V& x, V& y)
const 210 assert(y.N() == N());
211 assert(x.N() == M());
212 for (std::size_t r = 0; r < N(); ++r)
214 y.base()[r] += alpha * sparse_inner_product(r,x);
218 ElementType&
operator()(
const RowIndex& ri,
const ColIndex& ci)
221 auto begin = _container->_colindex.begin() + _container->_rowoffset[ri[0]];
222 auto end = _container->_colindex.begin() + _container->_rowoffset[ri[0]+1];
223 auto it = std::lower_bound(begin,end,ci[0]);
225 return _container->_data[it - _container->_colindex.begin()];
228 const ElementType&
operator()(
const RowIndex& ri,
const ColIndex& ci)
const 231 auto begin = _container->_colindex.begin() + _container->_rowoffset[ri[0]];
232 auto end = _container->_colindex.begin() + _container->_rowoffset[ri[0]+1];
233 auto it = std::lower_bound(begin,end,ci[0]);
235 return _container->_data[it - _container->_colindex.begin()];
250 const Container&
native()
const 268 void clear_row(
const RowIndex& ri,
const ElementType& diagonal_entry)
271 _container->_data.begin() + _container->_rowoffset[ri[0]],
272 _container->_data.begin() + _container->_rowoffset[ri[0]+1], ElementType(0));
273 (*this)(ri,ri) = diagonal_entry;
279 _container->_data.begin() + _container->_rowoffset[ri[0]],
280 _container->_data.begin() + _container->_rowoffset[ri[0]+1], ElementType(0));
281 (*this)(ri,ri) = diagonal_entry;
285 template<
typename GO>
286 static void allocate_matrix(std::shared_ptr<Container> & c,
const GO & go,
const ElementType&
e)
289 Pattern pattern(go.testGridFunctionSpace().ordering().blockCount());
290 go.fill_pattern(pattern);
292 c->_rows = go.testGridFunctionSpace().size();
293 c->_cols = go.trialGridFunctionSpace().size();
295 c->_rowoffset.resize(c->_rows+1);
297 auto calc_offset = [=](
const col_type & entry)
mutable ->
size_t { offset += entry.size();
return offset; };
298 std::transform(pattern.begin(), pattern.end(),
299 c->_rowoffset.begin()+1,
302 c->_non_zeros = c->_rowoffset.back();
304 c->_data.resize(c->_non_zeros, e);
305 c->_colindex.resize(c->_non_zeros);
307 auto colit = c->_colindex.begin();
308 c->_rowoffset[0] = 0;
309 for (
auto & row : pattern)
311 auto last = std::copy(row.begin(),row.end(),colit);
312 std::sort(colit, last);
319 std::size_t begin = _container->_rowoffset[row];
320 std::size_t end = _container->_rowoffset[row+1];
321 auto accu = [](
const ElementType & a,
const ElementType & b) -> ElementType {
return a+b; };
322 auto mult = [=](
const ElementType & a,
const index_type & i) -> ElementType {
return a * x.base()[i]; };
323 return std::inner_product(
324 &_container->_data[begin],
325 &_container->_data[end],
326 &_container->_colindex[begin],
339 #endif // DUNE_PDELAB_BACKEND_SIMPLE_SPARSE_HH std::shared_ptr< Container > _container
Definition: sparse.hh:332
Definition: uncachedmatrixview.hh:12
const Entity & e
Definition: localfunctionspace.hh:120
SparseMatrixContainer(const GO &go)
Definition: sparse.hh:112
SparseMatrixData< C, ET, I > Container
Definition: sparse.hh:83
ElementType field_type
Definition: sparse.hh:93
void detach()
Definition: sparse.hh:153
const ElementType & operator()(const RowIndex &ri, const ColIndex &ci) const
Definition: sparse.hh:228
void clear_row(const RowIndex &ri, const ElementType &diagonal_entry)
Definition: sparse.hh:268
SparseMatrixContainer & operator=(const SparseMatrixContainer &rhs)
Definition: sparse.hh:138
bool attached() const
Definition: sparse.hh:163
C< ElementType > _data
Definition: sparse.hh:51
std::size_t _rows
Definition: sparse.hh:48
void clear_row_block(const RowIndex &ri, const ElementType &diagonal_entry)
Definition: sparse.hh:276
SparseMatrixContainer(Backend::attached_container)
Creates an SparseMatrixContainer with an empty underlying ISTL matrix.
Definition: sparse.hh:130
Tag for requesting a vector or matrix container with a pre-attached underlying object.
Definition: backend/common/tags.hh:27
const std::shared_ptr< Container > & storage() const
Definition: sparse.hh:168
void mv(const V &x, V &y) const
Definition: sparse.hh:197
void add_link(const RI &ri, const CI &ci)
Definition: sparse.hh:31
std::size_t _non_zeros
Definition: sparse.hh:50
SparseMatrixContainer(const SparseMatrixContainer &rhs)
Definition: sparse.hh:134
ET ElementType
Definition: sparse.hh:91
Container & base()
Definition: sparse.hh:243
const Container & base() const
Definition: sparse.hh:238
Container::size_type size_type
Definition: sparse.hh:94
C< index_type > _colindex
Definition: sparse.hh:52
SparseMatrixPattern(std::size_t rows)
Definition: sparse.hh:36
void attach(std::shared_ptr< Container > container)
Definition: sparse.hh:158
SparseMatrixContainer(const GO &go, const ElementType &e)
Definition: sparse.hh:119
const std::size_t offset
Definition: localfunctionspace.hh:74
Definition: uncachedmatrixview.hh:165
GFSU::Ordering::Traits::ContainerIndex ColIndex
Definition: sparse.hh:101
C< index_type > _rowoffset
Definition: sparse.hh:53
std::unordered_set< std::size_t > col_type
Definition: sparse.hh:28
static void allocate_matrix(std::shared_ptr< Container > &c, const GO &go, const ElementType &e)
Definition: sparse.hh:286
void finalize()
Definition: sparse.hh:265
For backward compatibility – Do not use this!
Definition: adaptivity.hh:28
void usmv(const ElementType alpha, const V &x, V &y) const
Definition: sparse.hh:208
size_type M() const
Definition: sparse.hh:178
GFSV::Ordering::Traits::ContainerIndex RowIndex
Definition: sparse.hh:100
ElementType & operator()(const RowIndex &ri, const ColIndex &ci)
Definition: sparse.hh:218
std::size_t _cols
Definition: sparse.hh:49
I index_type
Definition: sparse.hh:46
SparseMatrixPattern Pattern
Definition: sparse.hh:109
ET ElementType
Definition: sparse.hh:45
GFSV TestGridFunctionSpace
Definition: sparse.hh:98
I index_type
Definition: sparse.hh:95
SparseMatrixContainer(Backend::unattached_container=Backend::unattached_container())
Creates an SparseMatrixContainer without allocating an underlying ISTL matrix.
Definition: sparse.hh:126
size_type N() const
Definition: sparse.hh:173
SparseMatrixContainer & operator*=(const ElementType &e)
Definition: sparse.hh:189
SparseMatrixContainer & operator=(const ElementType &e)
Definition: sparse.hh:183
std::size_t size_type
Definition: sparse.hh:47
GFSU TrialGridFunctionSpace
Definition: sparse.hh:97
Tag for requesting a vector or matrix container without a pre-attached underlying object...
Definition: backend/common/tags.hh:23
std::enable_if< std::is_base_of< impl::WrapperBase, T >::value, Native< T > &>::type native(T &t)
Definition: backend/interface.hh:192
Various tags for influencing backend behavior.
ElementType sparse_inner_product(std::size_t row, const V &x) const
Definition: sparse.hh:318
void flush()
Definition: sparse.hh:262