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> 22 template<
typename _RowOrdering,
typename _ColOrdering>
24 :
public std::vector< std::unordered_set<std::size_t> >
32 typedef std::unordered_set<std::size_t>
col_type;
34 template<
typename RI,
typename CI>
37 this->resize(_row_ordering.blockCount());
38 (*this)[ri.back()].insert(ci.back());
42 : _row_ordering(row_ordering)
43 , _col_ordering(col_ordering)
48 const RowOrdering& _row_ordering;
49 const ColOrdering& _col_ordering;
53 template<
template<
typename>
class C,
typename ET,
typename I>
87 template<
typename GFSV,
typename GFSU,
template<
typename>
class C,
typename ET,
typename I>
89 :
public Backend::impl::Wrapper<SparseMatrixData<C,ET,I> >
98 friend Backend::impl::Wrapper<Container>;
111 typedef typename GFSV::Ordering::Traits::ContainerIndex
RowIndex;
112 typedef typename GFSU::Ordering::Traits::ContainerIndex
ColIndex;
114 template<
typename RowCache,
typename ColCache>
117 template<
typename RowCache,
typename ColCache>
121 typename GFSV::Ordering::Traits::DOFIndex,
122 typename GFSV::Ordering::Traits::ContainerIndex
126 typename GFSU::Ordering::Traits::DOFIndex,
127 typename GFSU::Ordering::Traits::ContainerIndex
132 template<
typename GO>
134 : _container(
std::make_shared<Container>())
136 allocate_matrix(_container, go, ElementType(0));
139 template<
typename GO>
141 : _container(
std::make_shared<Container>())
143 allocate_matrix(_container, go, e);
152 : _container(
std::make_shared<Container>())
156 : _container(
std::make_shared<Container>(*(rhs._container)))
169 _container = std::make_shared<Container>(*(rhs.
_container));
179 void attach(std::shared_ptr<Container> container)
181 _container = container;
186 return bool(_container);
189 const std::shared_ptr<Container>&
storage()
const 196 return _container->_rows;
201 return _container->_cols;
206 std::fill(_container->_data.begin(),_container->_data.end(),
e);
212 using namespace std::placeholders;
213 std::transform(_container->_data.begin(),_container->_data.end(),_container->_data.begin(),std::bind(std::multiplies<ET>(),e,_1));
218 void mv(
const V& x, V& y)
const 220 assert(y.N() == N());
221 assert(x.N() == M());
222 for (std::size_t r = 0; r < N(); ++r)
224 y.base()[r] = sparse_inner_product(r,x);
229 void usmv(
const ElementType alpha,
const V& x, V& y)
const 231 assert(y.N() == N());
232 assert(x.N() == M());
233 for (std::size_t r = 0; r < N(); ++r)
235 y.base()[r] += alpha * sparse_inner_product(r,x);
239 ElementType&
operator()(
const RowIndex& ri,
const ColIndex& ci)
242 auto begin = _container->_colindex.begin() + _container->_rowoffset[ri[0]];
243 auto end = _container->_colindex.begin() + _container->_rowoffset[ri[0]+1];
244 auto it = std::lower_bound(begin,end,ci[0]);
246 return _container->_data[it - _container->_colindex.begin()];
249 const ElementType&
operator()(
const RowIndex& ri,
const ColIndex& ci)
const 252 auto begin = _container->_colindex.begin() + _container->_rowoffset[ri[0]];
253 auto end = _container->_colindex.begin() + _container->_rowoffset[ri[0]+1];
254 auto it = std::lower_bound(begin,end,ci[0]);
256 return _container->_data[it - _container->_colindex.begin()];
271 const Container&
native()
const 289 void clear_row(
const RowIndex& ri,
const ElementType& diagonal_entry)
292 _container->_data.begin() + _container->_rowoffset[ri[0]],
293 _container->_data.begin() + _container->_rowoffset[ri[0]+1], ElementType(0));
294 (*this)(ri,ri) = diagonal_entry;
298 template<
typename GO>
299 static void allocate_matrix(std::shared_ptr<Container> & c,
const GO & go,
const ElementType&
e)
302 Pattern pattern(go.testGridFunctionSpace().ordering(),go.trialGridFunctionSpace().ordering());
303 go.fill_pattern(pattern);
305 c->_rows = go.testGridFunctionSpace().size();
306 c->_cols = go.trialGridFunctionSpace().size();
308 c->_rowoffset.resize(c->_rows+1);
310 auto calc_offset = [=](
const col_type & entry)
mutable ->
size_t { offset += entry.size();
return offset; };
311 std::transform(pattern.begin(), pattern.end(),
312 c->_rowoffset.begin()+1,
315 c->_non_zeros = c->_rowoffset.back();
317 c->_data.resize(c->_non_zeros, e);
318 c->_colindex.resize(c->_non_zeros);
320 auto colit = c->_colindex.begin();
321 c->_rowoffset[0] = 0;
322 for (
auto & row : pattern)
324 auto last = std::copy(row.begin(),row.end(),colit);
325 std::sort(colit, last);
332 std::size_t begin = _container->_rowoffset[row];
333 std::size_t end = _container->_rowoffset[row+1];
334 auto accu = [](
const ElementType & a,
const ElementType & b) -> ElementType {
return a+b; };
335 auto mult = [=](
const ElementType & a,
const index_type & i) -> ElementType {
return a * x.base()[i]; };
336 return std::inner_product(
337 &_container->_data[begin],
338 &_container->_data[end],
339 &_container->_colindex[begin],
352 #endif // DUNE_PDELAB_BACKEND_SIMPLE_SPARSE_HH std::size_t size_type
Definition: sparse.hh:58
SparseMatrixContainer(Backend::unattached_container=Backend::unattached_container())
Creates an SparseMatrixContainer without allocating an underlying ISTL matrix.
Definition: sparse.hh:147
C< index_type > _rowoffset
Definition: sparse.hh:64
std::size_t _non_zeros
Definition: sparse.hh:61
const std::shared_ptr< Container > & storage() const
Definition: sparse.hh:189
ElementType field_type
Definition: sparse.hh:104
Definition: uncachedmatrixview.hh:165
Various tags for influencing backend behavior.
GFSU::Ordering::Traits::ContainerIndex ColIndex
Definition: sparse.hh:112
ElementType sparse_inner_product(std::size_t row, const V &x) const
Definition: sparse.hh:331
Tag for requesting a vector or matrix container without a pre-attached underlying object...
Definition: backend/common/tags.hh:25
SparseMatrixContainer(Backend::attached_container)
Creates an SparseMatrixContainer with an empty underlying ISTL matrix.
Definition: sparse.hh:151
void attach(std::shared_ptr< Container > container)
Definition: sparse.hh:179
I index_type
Definition: sparse.hh:106
_RowOrdering RowOrdering
Definition: sparse.hh:29
std::shared_ptr< Container > _container
Definition: sparse.hh:345
size_type N() const
Definition: sparse.hh:194
void mv(const V &x, V &y) const
Definition: sparse.hh:218
I index_type
Definition: sparse.hh:57
void add_link(const RI &ri, const CI &ci)
Definition: sparse.hh:35
Definition: uncachedmatrixview.hh:12
SparseMatrixContainer(const GO &go)
Definition: sparse.hh:133
SparseMatrixPattern(const RowOrdering &row_ordering, const ColOrdering &col_ordering)
Definition: sparse.hh:41
ET ElementType
Definition: sparse.hh:56
void finalize()
Definition: sparse.hh:286
Definition: orderingbase.hh:20
SparseMatrixContainer & operator*=(const ElementType &e)
Definition: sparse.hh:210
void detach()
Definition: sparse.hh:174
std::size_t _rows
Definition: sparse.hh:59
const ElementType & operator()(const RowIndex &ri, const ColIndex &ci) const
Definition: sparse.hh:249
GFSU TrialGridFunctionSpace
Definition: sparse.hh:108
const std::size_t offset
Definition: localfunctionspace.hh:74
void usmv(const ElementType alpha, const V &x, V &y) const
Definition: sparse.hh:229
std::size_t _cols
Definition: sparse.hh:60
GFSV TestGridFunctionSpace
Definition: sparse.hh:109
OrderingBase< typename GFSU::Ordering::Traits::DOFIndex, typename GFSU::Ordering::Traits::ContainerIndex > ColOrdering
Definition: sparse.hh:128
std::unordered_set< std::size_t > col_type
Definition: sparse.hh:32
SparseMatrixPattern< RowOrdering, ColOrdering > Pattern
Definition: sparse.hh:130
_ColOrdering ColOrdering
Definition: sparse.hh:30
void clear_row(const RowIndex &ri, const ElementType &diagonal_entry)
Definition: sparse.hh:289
SparseMatrixContainer & operator=(const SparseMatrixContainer &rhs)
Definition: sparse.hh:159
static void allocate_matrix(std::shared_ptr< Container > &c, const GO &go, const ElementType &e)
Definition: sparse.hh:299
ElementType & operator()(const RowIndex &ri, const ColIndex &ci)
Definition: sparse.hh:239
SparseMatrixContainer(const GO &go, const ElementType &e)
Definition: sparse.hh:140
size_type M() const
Definition: sparse.hh:199
OrderingBase< typename GFSV::Ordering::Traits::DOFIndex, typename GFSV::Ordering::Traits::ContainerIndex > RowOrdering
Definition: sparse.hh:123
C< ElementType > _data
Definition: sparse.hh:62
C< index_type > _colindex
Definition: sparse.hh:63
For backward compatibility – Do not use this!
Definition: adaptivity.hh:27
SparseMatrixContainer & operator=(const ElementType &e)
Definition: sparse.hh:204
const Entity & e
Definition: localfunctionspace.hh:111
SparseMatrixData< C, ET, I > Container
Definition: sparse.hh:94
Container & base()
Definition: sparse.hh:264
SparseMatrixContainer(const SparseMatrixContainer &rhs)
Definition: sparse.hh:155
const Container & base() const
Definition: sparse.hh:259
Container::size_type size_type
Definition: sparse.hh:105
std::enable_if< std::is_base_of< impl::WrapperBase, T >::value, Native< T > &>::type native(T &t)
Definition: backend/interface.hh:192
GFSV::Ordering::Traits::ContainerIndex RowIndex
Definition: sparse.hh:111
Tag for requesting a vector or matrix container with a pre-attached underlying object.
Definition: backend/common/tags.hh:29
void flush()
Definition: sparse.hh:283
bool attached() const
Definition: sparse.hh:184
ET ElementType
Definition: sparse.hh:102