|
dune-pdelab 2.10-git
|
Loading...
Searching...
No Matches
/builds/infrastructure/dune-website/build-doxygen/dune-pdelab/dune/pdelab/backend/simple/sparse.hh
Simple backend for CSR matrices.
Simple backend for CSR matricesMatrix stored as a: nnz Number of nonzero elements data CSR format data array of the matrix indices CSR format index array of the matrix rowptr CSR format index pointer array of the matrix
Consider the following 3x3 matrix [1, 0, 2] [0, 0, 3] [4, 5, 6] the data would be stored as nnz=6 data=[1 2 3 4 5 6] indices=[0 2 2 0 1 2] rowptr=[0 2 3 6]
// -*- tab-width: 4; indent-tabs-mode: nil -*-
#ifndef DUNE_PDELAB_BACKEND_SIMPLE_SPARSE_HH
#define DUNE_PDELAB_BACKEND_SIMPLE_SPARSE_HH
#include <vector>
#include <algorithm>
#include <functional>
#include <numeric>
#include <memory>
#include <unordered_set>
#include <dune/common/typetraits.hh>
#include <dune/pdelab/backend/common/tags.hh>
#include <dune/pdelab/backend/interface.hh>
#include <dune/pdelab/backend/simple/descriptors.hh>
namespace Dune {
namespace PDELab {
namespace Simple {
class SparseMatrixPattern
{
public:
template<typename RI, typename CI>
{
(*this)[ri.back()].insert(ci.back());
}
SparseMatrixPattern(std::size_t rows)
{}
};
template<template<typename> class C, typename ET, typename I>
struct SparseMatrixData
{
C<ElementType> _data;
C<index_type> _colindex;
C<index_type> _rowoffset;
};
template<typename GFSV, typename GFSU, template<typename> class C, typename ET, typename I>
class SparseMatrixContainer
: public Backend::impl::Wrapper<SparseMatrixData<C,ET,I> >
{
public:
private:
friend Backend::impl::Wrapper<Container>;
public:
template<typename RowCache, typename ColCache>
template<typename RowCache, typename ColCache>
template<typename GO>
SparseMatrixContainer(const GO& go)
: _container(std::make_shared<Container>())
{
}
template<typename GO>
: _container(std::make_shared<Container>())
{
allocate_matrix(_container, go, e);
}
explicit SparseMatrixContainer(Backend::unattached_container = Backend::unattached_container())
{}
explicit SparseMatrixContainer(Backend::attached_container)
: _container(std::make_shared<Container>())
{}
: _container(std::make_shared<Container>(*(rhs._container)))
{}
{
return *this;
{
(*_container) = (*(rhs._container));
}
else
{
_container = std::make_shared<Container>(*(rhs._container));
}
return *this;
}
{
}
{
_container = container;
}
{
return bool(_container);
}
{
return _container;
}
{
}
{
}
{
return *this;
}
{
std::transform(_container->_data.begin(),_container->_data.end(),_container->_data.begin(),std::bind(std::multiplies<ET>(),e,_1));
return *this;
}
template<typename V>
{
assert(y.N() == N());
assert(x.N() == M());
{
y.base()[r] = sparse_inner_product(r,x);
}
}
template<typename V>
{
assert(y.N() == N());
assert(x.N() == M());
{
y.base()[r] += alpha * sparse_inner_product(r,x);
}
}
{
// entries are in ascending order
assert (it != end);
}
{
// entries are in ascending order
assert(it != end);
}
{
return *_container;
}
{
return *_container;
}
private:
{
return *_container;
}
Container& native()
{
return *_container;
}
public:
{}
{}
{
(*this)(ri,ri) = diagonal_entry;
}
{
(*this)(ri,ri) = diagonal_entry;
}
protected:
template<typename GO>
{
Pattern pattern(go.testGridFunctionSpace().ordering().blockCount());
go.fill_pattern(pattern);
c->_rows = go.testGridFunctionSpace().size();
c->_cols = go.trialGridFunctionSpace().size();
// compute row offsets
c->_rowoffset.resize(c->_rows+1);
auto calc_offset = [=](const col_type & entry) mutable -> size_t { offset += entry.size(); return offset; };
std::transform(pattern.begin(), pattern.end(),
c->_rowoffset.begin()+1,
calc_offset);
// compute non-zeros
c->_non_zeros = c->_rowoffset.back();
// allocate col/data vectors
c->_data.resize(c->_non_zeros, e);
c->_colindex.resize(c->_non_zeros);
// copy pattern
auto colit = c->_colindex.begin();
c->_rowoffset[0] = 0;
for (auto & row : pattern)
{
std::sort(colit, last);
colit = last;
}
}
template<typename V>
auto mult = [=](const ElementType & a, const index_type & i) -> ElementType { return a * x.base()[i]; };
ElementType(0),
accu, mult
);
}
};
} // namespace Simple
} // namespace PDELab
} // namespace Dune
#endif // DUNE_PDELAB_BACKEND_SIMPLE_SPARSE_HH
double alpha() const
Y & rhs()
iterator end()
iterator begin()
virtual void operator()()=0
For backward compatibility – Do not use this!
std::unordered_set< std::size_t > col_type
Definition sparse.hh:28
void add_link(const RI &ri, const CI &ci)
Definition sparse.hh:31
Container::size_type size_type
Definition sparse.hh:94
void clear_row_block(const RowIndex &ri, const ElementType &diagonal_entry)
Definition sparse.hh:276
void mv(const V &x, V &y) const
Definition sparse.hh:197
ElementType field_type
Definition sparse.hh:93
UncachedMatrixView< SparseMatrixContainer, RowCache, ColCache > LocalView
Definition sparse.hh:104
SparseMatrixContainer & operator=(const SparseMatrixContainer &rhs)
Definition sparse.hh:138
void clear_row(const RowIndex &ri, const ElementType &diagonal_entry)
Definition sparse.hh:268
void attach(std::shared_ptr< Container > container)
Definition sparse.hh:158
SparseMatrixPattern Pattern
Definition sparse.hh:109
std::shared_ptr< Container > _container
Definition sparse.hh:332
const Container & base() const
Definition sparse.hh:238
const std::shared_ptr< Container > & storage() const
Definition sparse.hh:168
GFSV::Ordering::Traits::ContainerIndex RowIndex
Definition sparse.hh:100
ElementType sparse_inner_product(std::size_t row, const V &x) const
Definition sparse.hh:318
GFSU::Ordering::Traits::ContainerIndex ColIndex
Definition sparse.hh:101
GFSU TrialGridFunctionSpace
Definition sparse.hh:97
SparseMatrixData< C, ET, I > Container
Definition sparse.hh:83
static void allocate_matrix(std::shared_ptr< Container > &c, const GO &go, const ElementType &e)
Definition sparse.hh:286
void usmv(const ElementType alpha, const V &x, V &y) const
Definition sparse.hh:208
ConstUncachedMatrixView< const SparseMatrixContainer, RowCache, ColCache > ConstLocalView
Definition sparse.hh:107
GFSV TestGridFunctionSpace
Definition sparse.hh:98
SparseMatrixContainer & operator*=(const ElementType &e)
Definition sparse.hh:189
Various tags for influencing backend behavior.
T bind(T... args)
T copy(T... args)
T fill(T... args)
T inner_product(T... args)
T insert(T... args)
T lower_bound(T... args)
T reset(T... args)
T sort(T... args)
T transform(T... args)
Legal Statements / Impressum | Hosted by TU Dresden & Uni Heidelberg | Generated by
1.9.8