DUNE PDELab (git)

bcrsmatrixbackend.hh
1 // -*- tab-width: 4; indent-tabs-mode: nil -*-
2 #ifndef DUNE_PDELAB_BACKEND_ISTL_BCRSMATRIXBACKEND_HH
3 #define DUNE_PDELAB_BACKEND_ISTL_BCRSMATRIXBACKEND_HH
4 
5 #include <dune/pdelab/backend/istl/bcrsmatrix.hh>
6 #include <dune/pdelab/backend/istl/bcrspattern.hh>
7 #include <dune/pdelab/backend/istl/patternstatistics.hh>
8 
9 namespace Dune {
10  namespace PDELab {
11  namespace ISTL {
12 
13  // ********************************************************************************
14  // infrastructure for deducing the pattern type from row and column orderings
15  // ********************************************************************************
16 
17  namespace {
18 
19  template<typename M, typename RowOrdering, typename ColOrdering, bool pattern>
20  struct _build_bcrs_pattern_type
21  {
22  // we use void as a marker for a nonexisting subpattern
23  typedef void type;
24  };
25 
26  // central TMP that is invoked recursively while traversing the ordering hierarchy
27  // and builds up the possibly nested pattern.
28  template<typename M, typename RowOrdering, typename ColOrdering>
29  struct _build_bcrs_pattern_type<M,RowOrdering,ColOrdering,true>
30  {
31 
32  // descend into blocks
33  typedef typename _build_bcrs_pattern_type<
34  typename M::block_type,
35  RowOrdering,
36  ColOrdering,
37  requires_pattern<
38  typename M::block_type
39  >::value
40  >::type BlockOrdering;
41 
42  // Leafs -> BCRSPattern, interior nodes -> NestedPattern
43  typedef typename std::conditional<
44  std::is_same<BlockOrdering,void>::value,
45  BCRSPattern<
46  RowOrdering,
47  ColOrdering
48  >,
49  NestedPattern<
50  RowOrdering,
51  ColOrdering,
52  BlockOrdering
53  >
54  >::type type;
55 
56  };
57 
58  // Wrapper TMP for constructing OrderingBase types from function spaces and for
59  // shielding user from recursive implementation
60  template<typename M, typename GFSV, typename GFSU, typename Tag>
61  struct build_bcrs_pattern_type
62  {
63 
64  typedef OrderingBase<
65  typename GFSV::Ordering::Traits::DOFIndex,
66  typename GFSV::Ordering::Traits::ContainerIndex
67  > RowOrdering;
68 
69  typedef OrderingBase<
70  typename GFSU::Ordering::Traits::DOFIndex,
71  typename GFSU::Ordering::Traits::ContainerIndex
72  > ColOrdering;
73 
74  typedef typename _build_bcrs_pattern_type<M,RowOrdering,ColOrdering,requires_pattern<M>::value>::type type;
75  };
76 
77  // Specialization for forcibly flat backends
78  template<typename M, typename GFSV, typename GFSU>
79  struct build_bcrs_pattern_type<M,GFSV,GFSU,FlatContainerAllocationTag>
80  {
81  typedef BCRSPattern<typename GFSV::Ordering, typename GFSU::Ordering> type;
82  };
83 
84 
85  // leaf BCRSMatrix
86  template<typename OrderingV, typename OrderingU, typename Pattern, typename Container, typename StatsVector>
87  typename std::enable_if<
88  std::is_same<typename Pattern::SubPattern,void>::value
89  >::type
90  allocate_bcrs_matrix(const OrderingV& ordering_v,
91  const OrderingU& ordering_u,
92  Pattern& p,
93  Container& c,
94  StatsVector& stats)
95  {
96  c.setSize(ordering_v.blockCount(),ordering_u.blockCount(),0);
97  c.setBuildMode(Container::random);
98 
99  std::vector<typename Pattern::size_type> row_sizes(p.sizes());
100 
101  typename Pattern::size_type nnz = 0;
102  typename Pattern::size_type longest_row = 0;
103 
104  for (typename Pattern::size_type i = 0; i < c.N(); ++i)
105  {
106  nnz += row_sizes[i];
107  longest_row = std::max(longest_row,row_sizes[i]);
108  c.setrowsize(i,row_sizes[i]);
109  }
110  c.endrowsizes();
111 
112  stats.push_back(typename StatsVector::value_type(nnz,longest_row,p.overflowCount(),p.entriesPerRow(),ordering_v.blockCount()));
113 
114  for (typename Pattern::size_type i = 0; i < c.N(); ++i)
115  c.setIndices(i,p.begin(i),p.end(i));
116 
117  // free temporary index storage in pattern before allocating data array in matrix
118  p.clear();
119  // allocate data array
120  c.endindices();
121  }
122 
123 
124  // ********************************************************************************
125  // nested matrix allocation
126  // In contrast to the older implementation, this code still uses BCRSMatrix for nested matrices,
127  // but we do not attempt to keep the pattern of those interior matrices sparse and always allocate all
128  // blocks. That greatly simplifies the code, and as those interior matrices really shouldn't be very
129  // large, any performance impact is minimal.
130  // The code also collects statistics about the pattern of the leaf BCRSMatrices and collates those stats
131  // in a row-major ordering.
132  // ********************************************************************************
133 
134  // interior BCRSMatrix
135  template<typename OrderingV, typename OrderingU, typename Pattern, typename Container, typename StatsVector>
136  typename std::enable_if<
137  !std::is_same<typename Pattern::SubPattern,void>::value &&
138  requires_pattern<Container>::value
139  >::type
140  allocate_bcrs_matrix(const OrderingV& ordering_v,
141  const OrderingU& ordering_u,
142  Pattern& p,
143  Container& c,
144  StatsVector& stats)
145  {
146  c.setSize(ordering_v.blockCount(),ordering_u.blockCount(),ordering_v.blockCount()*ordering_u.blockCount());
147  c.setBuildMode(Container::random);
148 
149  for (std::size_t i = 0; i < c.N(); ++i)
150  c.setrowsize(i,ordering_u.blockCount());
151  c.endrowsizes();
152 
153  for (std::size_t i = 0; i < c.N(); ++i)
154  for (std::size_t j = 0; j < c.M(); ++j)
155  c.addindex(i,j);
156  c.endindices();
157 
158  for (std::size_t i = 0; i < c.N(); ++i)
159  for (std::size_t j = 0; j < c.M(); ++j)
160  {
161  allocate_bcrs_matrix(ordering_v.childOrdering(i),
162  ordering_u.childOrdering(j),
163  p.subPattern(i,j),
164  c[i][j],
165  stats);
166  }
167  }
168 
169  } // anonymous namespace
170 
171 
172 
174 
186  template<typename EntriesPerRow = std::size_t>
188  {
189 
191  typedef std::size_t size_type;
192 
195 
197  template<typename Matrix, typename GFSV, typename GFSU>
198  using Pattern = typename build_bcrs_pattern_type<
199  typename Matrix::Container,
200  GFSV,
201  GFSU,
202  typename GFSV::Ordering::ContainerAllocationTag
203  >::type;
204 
205  template<typename VV, typename VU, typename E>
206  struct MatrixHelper
207  {
208  typedef BCRSMatrix<
209  typename VV::GridFunctionSpace,
210  typename VU::GridFunctionSpace,
211  typename build_matrix_type<
212  E,
213  typename VV::Container,
214  typename VU::Container
215  >::type,
216  Statistics
217  > type;
218  };
219 
221 
224  template<typename GridOperator, typename Matrix>
225  std::vector<Statistics> buildPattern(const GridOperator& grid_operator, Matrix& matrix) const
226  {
227  Pattern<
228  Matrix,
231  > pattern(grid_operator.testGridFunctionSpace().ordering(),grid_operator.trialGridFunctionSpace().ordering(),_entries_per_row);
232  grid_operator.fill_pattern(pattern);
233  std::vector<Statistics> stats;
234  allocate_bcrs_matrix(grid_operator.testGridFunctionSpace().ordering(),
235  grid_operator.trialGridFunctionSpace().ordering(),
236  pattern,
237  Backend::native(matrix),
238  stats
239  );
240  return stats;
241  }
242 
244 
249  BCRSMatrixBackend(const EntriesPerRow& entries_per_row)
250  : _entries_per_row(entries_per_row)
251  {}
252 
253  private:
254 
255  EntriesPerRow _entries_per_row;
256 
257  };
258 
259  } // namespace ISTL
260  } // namespace PDELab
261 } // namespace Dune
262 
263 #endif // DUNE_PDELAB_BACKEND_ISTL_BCRSMATRIXBACKEND_HH
A generic dynamic dense matrix.
Definition: matrix.hh:561
Standard grid operator implementation.
Definition: gridoperator.hh:36
const GFSV & testGridFunctionSpace() const
Get the test grid function space.
Definition: gridoperator.hh:98
const GFSU & trialGridFunctionSpace() const
Get the trial grid function space.
Definition: gridoperator.hh:92
void fill_pattern(Pattern &p) const
Fill pattern of jacobian matrix.
Definition: gridoperator.hh:168
Statistics about the pattern of a BCRSMatrix.
Definition: patternstatistics.hh:14
constexpr auto max
Function object that returns the greater of the given values.
Definition: hybridutilities.hh:484
Dune namespace.
Definition: alignedallocator.hh:13
GFSU TrialGridFunctionSpace
The trial grid function space.
Definition: gridoperatorutilities.hh:37
GFSV TestGridFunctionSpace
The test grid function space.
Definition: gridoperatorutilities.hh:40
Backend using (possibly nested) ISTL BCRSMatrices.
Definition: bcrsmatrixbackend.hh:188
PatternStatistics< size_type > Statistics
The type of the object holding the statistics generated during pattern construction.
Definition: bcrsmatrixbackend.hh:194
BCRSMatrixBackend(const EntriesPerRow &entries_per_row)
Constructs a BCRSMatrixBackend.
Definition: bcrsmatrixbackend.hh:249
typename build_bcrs_pattern_type< typename Matrix::Container, GFSV, GFSU, typename GFSV::Ordering::ContainerAllocationTag >::type Pattern
The type of the pattern object passed to the GridOperator for pattern construction.
Definition: bcrsmatrixbackend.hh:203
std::size_t size_type
The size type of the BCRSMatrix.
Definition: bcrsmatrixbackend.hh:191
std::vector< Statistics > buildPattern(const GridOperator &grid_operator, Matrix &matrix) const
Builds the matrix pattern associated with grid_operator and initializes matrix with it.
Definition: bcrsmatrixbackend.hh:225
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden  |  generated with Hugo v0.80.0 (May 16, 22:29, 2024)