dune-fem 2.12-git
Loading...
Searching...
No Matches
parallel.hh
Go to the documentation of this file.
1#ifndef DUNE_FEM_SPACE_MAPPER_PARALLEL_HH
2#define DUNE_FEM_SPACE_MAPPER_PARALLEL_HH
3
4#include <cstddef>
5
6#include <algorithm>
7#include <tuple>
8#include <type_traits>
9#include <utility>
10#include <vector>
11
13
17
18namespace Dune
19{
20
21 namespace Fem
22 {
23
24 namespace __ParallelDofMapper
25 {
26
27 // BuildDataHandle
28 // ---------------
29
30 template< class GridPart, class BaseMapper, class GlobalKey >
32 : public CommDataHandleIF< BuildDataHandle< GridPart, BaseMapper, GlobalKey >, GlobalKey >
33 {
34 explicit BuildDataHandle ( const BaseMapper &baseMapper, const AuxiliaryDofs< GridPart, BaseMapper > &auxiliaryDofs, std::vector< GlobalKey > &mapping )
35 : baseMapper_( baseMapper ), auxiliaryDofs_( auxiliaryDofs ), mapping_( mapping )
36 {}
37
38 bool contains ( int dim, int codim ) const { return baseMapper_.contains( codim ); }
39 bool fixedSize ( int dim, int codim ) const { return false; }
40
41 template< class Buffer, class Entity >
42 void gather ( Buffer &buffer, const Entity &entity ) const
43 {
44 baseMapper_.mapEachEntityDof( entity, [ this, &buffer ] ( int, auto index ) {
46 buffer.write( mapping_[ index ] );
47 } );
48 }
49
50 template< class Buffer, class Entity >
51 void scatter ( Buffer &buffer, const Entity &entity, std::size_t n )
52 {
53 if( n == 0 )
54 return;
55
56 assert( n == static_cast< std::size_t >( baseMapper_.numEntityDofs( entity ) ) );
57 baseMapper_.mapEachEntityDof( entity, [ this, &buffer ] ( int, auto index ) {
58 assert( auxiliaryDofs_.contains( index ) );
59 buffer.read( mapping_[ index ] );
60 } );
61 }
62
63 template< class Entity >
64 std::size_t size ( const Entity &entity ) const
65 {
66 std::size_t size = 0;
67 baseMapper_.mapEachEntityDof( entity, [ this, &size ] ( int, auto index )
68 { size += static_cast< std::size_t >( !auxiliaryDofs_.contains( index ) ); } );
69 return size;
70 }
71
72 protected:
73 const BaseMapper &baseMapper_;
76 };
77
78 } // namespace __ParallelDofMapper
79
80
81
82 // ParallelDofMapper
83 // -----------------
84
85 template< class GridPart, class BaseMapper, class GlobalKey = std::size_t >
87 {
89
90 public:
91 typedef GridPart GridPartType;
92 typedef BaseMapper BaseMapperType;
93
95 typedef GlobalKey GlobalKeyType;
96
97 typedef typename BaseMapperType::ElementType ElementType;
98
100 : gridPart_( gridPart ), baseMapper_( baseMapper ), commInterface_( commInterface )
101 {
102 update();
103 }
104
105 ParallelDofMapper ( const ThisType & ) = delete;
107
108 ThisType &operator= ( const ThisType & ) = delete;
109 ThisType &operator= ( ThisType && ) = delete;
110
111 template< class Functor >
112 void mapEach ( const ElementType &element, Functor f ) const
113 {
114 baseMapper().mapEach( element, [ this, f ] ( auto local, auto i ) { f( local, mapping_[ i ] ); } );
115 }
116
117 void map ( const ElementType &element, std::vector< GlobalKeyType > &indices ) const
118 {
119 indices.resize( numDofs( element ) );
120 mapEach( element, [ &indices ] ( int local, GlobalKeyType global ) { indices[ local ] = global; } );
121 }
122
123 [[deprecated("Use onSubEntity method with char vector instead")]]
124 void onSubEntity ( const ElementType &element, int i, int c, std::vector< bool > &indices ) const
125 {
127 onSubEntity(element, i, c, _idx);
128 indices.resize( _idx.size() );
129 for (std::size_t i=0; i<_idx.size();++i)
130 _idx[i] = indices[i] > 0;
131 }
132 // this method returns which local dofs are attached to the given subentity.
133 // indices[locDofNr] =
134 // 0 : not attached, not equal to 0 : attached
135 // (so this method can still be used in the way the deprecated method was).
136 // New: In case the dof can be associated to a component of the
137 // space, the value returned is that component+1. In other
138 // cases (normal velocity for RT for example) the value is -1).
139 // So indices[i] is in [-1,dimRange+1]
140 void onSubEntity ( const ElementType &element, int i, int c, std::vector< char > &indices ) const
141 {
142 baseMapper().onSubEntity( element, i, c, indices );
143 }
144
145 unsigned int maxNumDofs () const { return baseMapper().maxNumDofs(); }
146 unsigned int numDofs ( const ElementType &element ) const { return baseMapper().numDofs( element ); }
147
148 // assignment of DoFs to entities
149
150 template< class Entity, class Functor >
151 void mapEachEntityDof ( const Entity &entity, Functor f ) const
152 {
153 baseMapper().mapEachEntityDof( entity, [ this, f ] ( auto local, auto i ) { f( local, mapping_[ i ] ); } );
154 }
155
156 template< class Entity >
157 void mapEntityDofs ( const Entity &entity, std::vector< GlobalKeyType > &indices ) const
158 {
159 indices.resize( numEntityDofs( entity ) );
160 mapEachEntityDof( entity, [ &indices ] ( int local, GlobalKeyType global ) { indices[ local ] = global; } );
161 }
162
163 template< class Entity >
164 unsigned int numEntityDofs ( const Entity &entity ) const
165 {
166 return baseMapper().numEntityDofs( entity );
167 }
168
169 // global information
170
171 bool contains ( int codim ) const { return baseMapper().contains( codim ); }
172
173 bool fixedDataSize ( int codim ) const { return baseMapper().fixedDataSize( codim ); }
174
175 SizeType size () const { return size_; }
176
177 InterfaceType communicationInterface() const { return commInterface_; }
178
179 // adaptation interface
180
181 bool consecutive () const { return false; }
182
183 int numBlocks () const { DUNE_THROW( NotImplemented, "Adaptive dof mapper interface not implemented." ); }
184 SizeType offSet ( int blk ) const { DUNE_THROW( NotImplemented, "Adaptive dof mapper interface not implemented." ); }
185 SizeType oldOffSet ( int blk ) const { DUNE_THROW( NotImplemented, "Adaptive dof mapper interface not implemented." ); }
186 SizeType numberOfHoles ( int blk ) const { DUNE_THROW( NotImplemented, "Adaptive dof mapper interface not implemented." ); }
187 SizeType oldIndex ( SizeType hole, int blk ) const { DUNE_THROW( NotImplemented, "Adaptive dof mapper interface not implemented." ); }
188 SizeType newIndex ( SizeType hole, int blk ) const { DUNE_THROW( NotImplemented, "Adaptive dof mapper interface not implemented." ); }
189
190 // update
191
192 void update ( )
193 {
195 auxiliaryDofs.rebuild();
196 //auto primaryDofs = Dune::Fem::primaryDofs( auxiliaryDofs );
197
198 const auto primarySize = auxiliaryDofs.primarySize(); // primaryDofs.size();
199 size_ = primarySize; // primaryDofs.size();
200 offset_ = exScan( gridPart().comm(), size_ );
201 size_ = gridPart().comm().sum( size_ );
202
203 std::size_t baseSize = baseMapper().size();
204 mapping_.resize( baseSize );
205 GlobalKeyType next = static_cast< GlobalKeyType >( offset_ );
207 auto mapNext = [&mapping, &next] (const auto i) { mapping[ i ] = next++; };
208 // for all primary dofs build mapping
209 forEachPrimaryDof( auxiliaryDofs, mapNext );
210 assert( next == static_cast< GlobalKeyType >( offset_ + primarySize ) );
211
213 gridPart().communicate( dataHandle, communicationInterface(), ForwardCommunication );
214 }
215
216 const GridPartType &gridPart () const { return gridPart_; }
217 const BaseMapperType &baseMapper () const { return baseMapper_; }
218
219 const std::vector< GlobalKeyType > &mapping () const { return mapping_; }
220
221 private:
222 template< class Comm, class T >
223 static T exScan ( const Communication< Comm > &comm, T in )
224 {
225 return T( 0 );
226 }
227
228#if HAVE_MPI
229 template< class T >
230 static T exScan ( const Communication< MPI_Comm > &comm, T in )
231 {
232 T out( 0 );
233 MPI_Exscan( &in, &out, 1, MPITraits< T >::getType(), MPI_SUM, static_cast< MPI_Comm >( comm ) );
234 return out;
235 }
236#endif // #if HAVE_MPI
237
238 const GridPartType &gridPart_;
239 const BaseMapperType &baseMapper_;
240 const InterfaceType commInterface_;
242 SizeType offset_, size_;
243 };
244
245
246
247 // Capabilities for IndexSetDofMapper
248 // ----------------------------------
249
250 namespace Capabilities
251 {
252
253 template< class GridPart, class BaseMapper, class GlobalKey >
254 struct isAdaptiveDofMapper< ParallelDofMapper< GridPart, BaseMapper, GlobalKey > >
255 {
256 static const bool v = false;
257 };
258
259 template< class GridPart, class BaseMapper, class GlobalKey >
260 struct isConsecutiveIndexSet< ParallelDofMapper< GridPart, BaseMapper, GlobalKey > >
261 {
262 static const bool v = true;
263 };
264
265 } // namespace Capabilities
266
267 } // namespace Fem
268
269} // namespace Dune
270
271#endif // #ifndef DUNE_FEM_SPACE_MAPPER_PARALLEL_HH
int size() const
size_type dim() const
std::ptrdiff_t index() const
#define DUNE_THROW(E,...)
const GlobalIndex & global() const
LocalIndex & local()
InterfaceType
ForwardCommunication
void rebuild()
Definition auxiliarydofs.hh:151
bool contains(IndexType index) const
return true if index is contained, meaning it is a auxiliary dof
Definition auxiliarydofs.hh:146
IndexType primarySize() const
return number of primaryDofs
Definition auxiliarydofs.hh:134
static void forEachPrimaryDof(const AuxiliaryDofs &auxiliaryDofs, F &&f)
Apply action encoded in Functor f to all primary dofs.
Definition auxiliarydofs.hh:303
static const bool v
Definition common/indexset.hh:68
static const bool v
Definition space/mapper/capabilities.hh:23
bool fixedSize(int dim, int codim) const
Definition parallel.hh:39
BuildDataHandle(const BaseMapper &baseMapper, const AuxiliaryDofs< GridPart, BaseMapper > &auxiliaryDofs, std::vector< GlobalKey > &mapping)
Definition parallel.hh:34
std::vector< GlobalKey > & mapping_
Definition parallel.hh:75
bool contains(int dim, int codim) const
Definition parallel.hh:38
void scatter(Buffer &buffer, const Entity &entity, std::size_t n)
Definition parallel.hh:51
std::size_t size(const Entity &entity) const
Definition parallel.hh:64
const BaseMapper & baseMapper_
Definition parallel.hh:73
void gather(Buffer &buffer, const Entity &entity) const
Definition parallel.hh:42
const AuxiliaryDofs< GridPart, BaseMapper > & auxiliaryDofs_
Definition parallel.hh:74
Definition parallel.hh:87
ThisType & operator=(const ThisType &)=delete
SizeType offSet(int blk) const
Definition parallel.hh:184
ParallelDofMapper(ThisType &&)=delete
void mapEach(const ElementType &element, Functor f) const
Definition parallel.hh:112
void map(const ElementType &element, std::vector< GlobalKeyType > &indices) const
Definition parallel.hh:117
void onSubEntity(const ElementType &element, int i, int c, std::vector< bool > &indices) const
Definition parallel.hh:124
GridPart GridPartType
Definition parallel.hh:91
SizeType size() const
Definition parallel.hh:175
BaseMapperType::ElementType ElementType
Definition parallel.hh:97
bool consecutive() const
Definition parallel.hh:181
int numBlocks() const
Definition parallel.hh:183
ParallelDofMapper(const GridPartType &gridPart, const BaseMapperType &baseMapper, const InterfaceType commInterface)
Definition parallel.hh:99
bool fixedDataSize(int codim) const
Definition parallel.hh:173
GlobalKey GlobalKeyType
Definition parallel.hh:95
ParallelDofMapper(const ThisType &)=delete
void mapEachEntityDof(const Entity &entity, Functor f) const
Definition parallel.hh:151
SizeType newIndex(SizeType hole, int blk) const
Definition parallel.hh:188
void onSubEntity(const ElementType &element, int i, int c, std::vector< char > &indices) const
Definition parallel.hh:140
unsigned int maxNumDofs() const
Definition parallel.hh:145
SizeType numberOfHoles(int blk) const
Definition parallel.hh:186
bool contains(int codim) const
Definition parallel.hh:171
SizeType oldIndex(SizeType hole, int blk) const
Definition parallel.hh:187
SizeType oldOffSet(int blk) const
Definition parallel.hh:185
unsigned int numDofs(const ElementType &element) const
Definition parallel.hh:146
BaseMapper BaseMapperType
Definition parallel.hh:92
InterfaceType communicationInterface() const
Definition parallel.hh:177
const std::vector< GlobalKeyType > & mapping() const
Definition parallel.hh:219
void mapEntityDofs(const Entity &entity, std::vector< GlobalKeyType > &indices) const
Definition parallel.hh:157
unsigned int numEntityDofs(const Entity &entity) const
Definition parallel.hh:164
const GridPartType & gridPart() const
Definition parallel.hh:216
std::size_t SizeType
Definition parallel.hh:94
void update()
Definition parallel.hh:192
const BaseMapperType & baseMapper() const
Definition parallel.hh:217
T resize(T... args)
T size(T... args)