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]
#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>
namespace PDELab {
namespace Simple {
class SparseMatrixPattern
: public std::vector< std::unordered_set<std::size_t> >
{
public:
typedef std::unordered_set<std::size_t>
col_type;
template<typename RI, typename CI>
void add_link(
const RI& ri,
const CI& ci)
{
(*this)[ri.back()].insert(ci.back());
}
: std::vector< std::unordered_set<std::size_t> >(rows)
{}
};
template<template<typename> class C, typename ET, typename I>
struct SparseMatrixData
{
};
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:
typedef typename GFSV::Ordering::Traits::ContainerIndex
RowIndex;
typedef typename GFSU::Ordering::Traits::ContainerIndex
ColIndex;
template<typename RowCache, typename ColCache>
using LocalView = UncachedMatrixView<SparseMatrixContainer,RowCache,ColCache>;
template<typename RowCache, typename ColCache>
using ConstLocalView = ConstUncachedMatrixView<const SparseMatrixContainer,RowCache,ColCache>;
typedef SparseMatrixPattern
Pattern;
template<typename GO>
{
}
template<typename GO>
{
}
{}
{}
:
_container(std::make_shared<Container>(*(rhs._container)))
{}
{
if (this == &rhs)
return *this;
{
(*_container) = (*(rhs._container));
}
else
{
_container = std::make_shared<Container>(*(rhs._container));
}
return *this;
}
{
}
void attach(std::shared_ptr<Container> container)
{
}
{
}
const std::shared_ptr<Container>&
storage()
const {
}
{
}
{
}
{
return *this;
}
{
using namespace std::placeholders;
return *this;
}
template<typename V>
void mv(
const V& x, V& y)
const {
for (std::size_t r = 0; r <
N(); ++r)
{
}
}
template<typename V>
{
for (std::size_t r = 0; r <
N(); ++r)
{
}
}
{
auto it = std::lower_bound(begin,end,ci[0]);
assert (it != end);
}
{
auto it = std::lower_bound(begin,end,ci[0]);
assert(it != end);
}
{
}
{
}
private:
{
}
{
}
public:
{}
{}
{
std::fill(
(*this)(ri,ri) = diagonal_entry;
}
{
std::fill(
(*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();
auto calc_offset = [=](
const col_type & entry)
mutable ->
size_t {
offset += entry.size();
return offset; };
std::transform(pattern.begin(), pattern.end(),
calc_offset);
for (auto & row : pattern)
{
auto last = std::copy(row.begin(),row.end(),colit);
std::sort(colit, last);
colit = last;
}
}
template<typename V>
return std::inner_product(
accu, mult
);
}
};
}
}
}
#endif // DUNE_PDELAB_BACKEND_SIMPLE_SPARSE_HH