dune-spgrid  2.7
decomposition.hh
Go to the documentation of this file.
1 #ifndef DUNE_SPGRID_DECOMPOSITION_HH
2 #define DUNE_SPGRID_DECOMPOSITION_HH
3 
4 #include <algorithm>
5 
8 
9 namespace Dune
10 {
11 
12  // SPDecomposition
13  // ---------------
14 
15  template< int dim >
17  {
19 
20  public:
21  static const int dimension = dim;
22 
25 
26  private:
27  struct Node
28  {
29  Node ( const Mesh &mesh, const unsigned int size );
30  ~Node ();
31 
32  const Mesh &mesh () const;
33  const Mesh &subMesh ( const unsigned int rank ) const;
34  void subMeshes ( std::vector< Mesh > &meshes ) const;
35 
36  unsigned int size () const;
37 
38  private:
39  Mesh mesh_;
40  unsigned int size_;
41  Node *left_, *right_;
42  };
43 
44  public:
45  SPDecomposition ( const Mesh &mesh, const unsigned int size );
46  SPDecomposition ( const MultiIndex &width, const unsigned int size );
47 
48  const Mesh &mesh () const;
49  const Mesh &subMesh ( const unsigned int rank ) const;
50  std::vector< Mesh > subMeshes () const;
51 
52  unsigned int size () const;
53 
54  private:
55  Node root_;
56  };
57 
58 
59 
60  // Implementation of SPDecomposition::Node
61  // ---------------------------------------
62 
63  template< int dim >
64  inline SPDecomposition< dim >::Node::Node ( const Mesh &mesh, const unsigned int size )
65  : mesh_( mesh ),
66  size_( size ),
67  left_( 0 ),
68  right_( 0 )
69  {
70  if( size_ > 1 )
71  {
72  const int leftWeight = size_/2;
73  const int rightWeight = size_ - leftWeight;
74 
75  const MultiIndex &width = mesh.width();
76  const std::pair< Mesh, Mesh > split
77  = mesh_.split( std::max_element( width.begin(), width.end() ) - width.begin(), leftWeight, rightWeight );
78  left_ = new Node( split.first, leftWeight );
79  right_ = new Node( split.second, rightWeight );
80  }
81  }
82 
83 
84  template< int dim >
85  inline SPDecomposition< dim >::Node::~Node ()
86  {
87  delete left_;
88  delete right_;
89  }
90 
91 
92  template< int dim >
93  inline const typename SPDecomposition< dim >::Mesh &
94  SPDecomposition< dim >::Node::mesh () const
95  {
96  return mesh_;
97  }
98 
99 
100  template< int dim >
101  inline const typename SPDecomposition< dim >::Mesh &
102  SPDecomposition< dim >::Node::subMesh ( const unsigned int rank ) const
103  {
104  assert( rank < size_ );
105  if( size_ > 1 )
106  {
107  assert( (left_ != 0) && (right_ != 0) );
108  if( rank < size_/2 )
109  return left_->subMesh( rank );
110  else
111  return right_->subMesh( rank - size_/2 );
112  }
113  else
114  return mesh();
115  }
116 
117 
118  template< int dim >
119  inline void
120  SPDecomposition< dim >::Node::subMeshes ( std::vector< Mesh > &meshes ) const
121  {
122  if( size_ > 1 )
123  {
124  assert( (left_ != 0) && (right_ != 0) );
125  left_->subMeshes( meshes );
126  right_->subMeshes( meshes );
127  }
128  else
129  meshes.push_back( mesh() );
130  }
131 
132 
133  template< int dim >
134  inline unsigned int SPDecomposition< dim >::Node::size () const
135  {
136  return size_;
137  }
138 
139 
140 
141  // Implementation of SPDecomposition
142  // ---------------------------------
143 
144  template< int dim >
146  ::SPDecomposition ( const Mesh &mesh, const unsigned int size )
147  : root_( mesh, size )
148  {}
149 
150 
151  template< int dim >
153  ::SPDecomposition ( const MultiIndex &width, const unsigned int size )
154  : root_( Mesh( width ), size )
155  {}
156 
157 
158  template< int dim >
159  inline const typename SPDecomposition< dim >::Mesh &
161  {
162  return root_.mesh();
163  }
164 
165 
166  template< int dim >
167  inline const typename SPDecomposition< dim >::Mesh &
168  SPDecomposition< dim >::subMesh ( const unsigned int rank ) const
169  {
170  return root_.subMesh( rank );
171  }
172 
173 
174  template< int dim >
175  inline std::vector< typename SPDecomposition< dim >::Mesh >
177  {
178  std::vector< Mesh > meshes;
179  meshes.reserve( root_.size() );
180  root_.subMeshes( meshes );
181  return meshes;
182  }
183 
184 
185  template< int dim >
186  inline unsigned int SPDecomposition< dim >::size () const
187  {
188  return root_.size();
189  }
190 
191 } // namespace Dune
192 
193 #endif // #ifndef DUNE_SPGRID_DECOMPOSITION_HH
Definition: iostream.hh:7
Definition: decomposition.hh:17
const Mesh & subMesh(const unsigned int rank) const
Definition: decomposition.hh:168
static const int dimension
Definition: decomposition.hh:21
SPMultiIndex< dimension > MultiIndex
Definition: decomposition.hh:23
std::vector< Mesh > subMeshes() const
Definition: decomposition.hh:176
SPMesh< dimension > Mesh
Definition: decomposition.hh:24
SPDecomposition(const Mesh &mesh, const unsigned int size)
Definition: decomposition.hh:146
const Mesh & mesh() const
Definition: decomposition.hh:160
unsigned int size() const
Definition: decomposition.hh:186
MultiIndex width() const
Definition: mesh.hh:194