dune-fem 2.8.0
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
12#include <dune/grid/common/datahandleif.hh>
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 ) {
45 if( !auxiliaryDofs_.contains( 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_;
75 std::vector< GlobalKey > &mapping_;
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
94 typedef std::size_t SizeType;
95 typedef GlobalKey GlobalKeyType;
96
97 typedef typename BaseMapperType::ElementType ElementType;
98
100 : gridPart_( gridPart ), baseMapper_( baseMapper )
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 void onSubEntity ( const ElementType &element, int i, int c, std::vector< bool > &indices ) const
124 {
125 baseMapper().onSubEntity( element, i, c, indices );
126 }
127
128 unsigned int maxNumDofs () const { return baseMapper().maxNumDofs(); }
129 unsigned int numDofs ( const ElementType &element ) const { return baseMapper().numDofs( element ); }
130
131 // assignment of DoFs to entities
132
133 template< class Entity, class Functor >
134 void mapEachEntityDof ( const Entity &entity, Functor f ) const
135 {
136 baseMapper().mapEachEntityDof( entity, [ this, f ] ( auto local, auto i ) { f( local, mapping_[ i ] ); } );
137 }
138
139 template< class Entity >
140 void mapEntityDofs ( const Entity &entity, std::vector< GlobalKeyType > &indices ) const
141 {
142 indices.resize( numEntityDofs( entity ) );
143 mapEachEntityDof( entity, [ &indices ] ( int local, GlobalKeyType global ) { indices[ local ] = global; } );
144 }
145
146 template< class Entity >
147 unsigned int numEntityDofs ( const Entity &entity ) const
148 {
149 return baseMapper().numEntityDofs( entity );
150 }
151
152 // global information
153
154 bool contains ( int codim ) const { return baseMapper().contains( codim ); }
155
156 bool fixedDataSize ( int codim ) const { return baseMapper().fixedDataSize( codim ); }
157
158 SizeType size () const { return size_; }
159
160 // adaptation interface
161
162 bool consecutive () const { return false; }
163
164 int numBlocks () const { DUNE_THROW( NotImplemented, "Adaptive dof mapper interface not implemented." ); }
165 SizeType offSet ( int blk ) const { DUNE_THROW( NotImplemented, "Adaptive dof mapper interface not implemented." ); }
166 SizeType oldOffSet ( int blk ) const { DUNE_THROW( NotImplemented, "Adaptive dof mapper interface not implemented." ); }
167 SizeType numberOfHoles ( int blk ) const { DUNE_THROW( NotImplemented, "Adaptive dof mapper interface not implemented." ); }
168 SizeType oldIndex ( SizeType hole, int blk ) const { DUNE_THROW( NotImplemented, "Adaptive dof mapper interface not implemented." ); }
169 SizeType newIndex ( SizeType hole, int blk ) const { DUNE_THROW( NotImplemented, "Adaptive dof mapper interface not implemented." ); }
170
171 // update
172
173 void update ()
174 {
176 auxiliaryDofs.rebuild();
177 auto primaryDofs = Dune::Fem::primaryDofs( auxiliaryDofs );
178
179 size_ = primaryDofs.size();
180 offset_ = exScan( gridPart().comm(), size_ );
181 size_ = gridPart().comm().sum( size_ );
182
183 std::size_t baseSize = baseMapper().size();
184 mapping_.resize( baseSize );
185 GlobalKeyType next = static_cast< GlobalKeyType >( offset_ );
186 for( const auto i : primaryDofs )
187 mapping_[ i ] = next++;
188 assert( next == static_cast< GlobalKeyType >( offset_ + primaryDofs.size() ) );
189
191 gridPart().communicate( dataHandle, InteriorBorder_All_Interface, ForwardCommunication );
192 }
193
194 const GridPartType &gridPart () const { return gridPart_; }
195 const BaseMapperType &baseMapper () const { return baseMapper_; }
196
197 const std::vector< GlobalKeyType > &mapping () const { return mapping_; }
198
199 private:
200 template< class Comm, class T >
201 static T exScan ( const CollectiveCommunication< Comm > &comm, T in )
202 {
203 return T( 0 );
204 }
205
206#if HAVE_MPI
207 template< class T >
208 static T exScan ( const CollectiveCommunication< MPI_Comm > &comm, T in )
209 {
210 T out( 0 );
211 MPI_Exscan( &in, &out, 1, MPITraits< T >::getType(), MPI_SUM, static_cast< MPI_Comm >( comm ) );
212 return out;
213 }
214#endif // #if HAVE_MPI
215
216 const GridPartType &gridPart_;
217 const BaseMapperType &baseMapper_;
218 std::vector< GlobalKeyType > mapping_;
219 SizeType offset_, size_;
220 };
221
222
223
224 // Capabilities for IndexSetDofMapper
225 // ----------------------------------
226
227 namespace Capabilities
228 {
229
230 template< class GridPart, class BaseMapper, class GlobalKey >
231 struct isAdaptiveDofMapper< ParallelDofMapper< GridPart, BaseMapper, GlobalKey > >
232 {
233 static const bool v = false;
234 };
235
236 template< class GridPart, class BaseMapper, class GlobalKey >
237 struct isConsecutiveIndexSet< ParallelDofMapper< GridPart, BaseMapper, GlobalKey > >
238 {
239 static const bool v = true;
240 };
241
242 } // namespace Capabilities
243
244 } // namespace Fem
245
246} // namespace Dune
247
248#endif // #ifndef DUNE_FEM_SPACE_MAPPER_PARALLEL_HH
static PrimaryDofs< AuxiliaryDofs > primaryDofs(const AuxiliaryDofs &auxiliaryDofs)
Definition: auxiliarydofs.hh:341
void rebuild()
Definition: auxiliarydofs.hh:136
bool contains(int index) const
return true if index is contained, meaning it is a auxiliary dof
Definition: auxiliarydofs.hh:131
Definition: bindguard.hh:11
specialize with true if index set implements the interface for consecutive index sets
Definition: common/indexset.hh:42
static const bool v
Definition: common/indexset.hh:49
Definition: space/mapper/capabilities.hh:22
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:165
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:123
GridPart GridPartType
Definition: parallel.hh:91
SizeType size() const
Definition: parallel.hh:158
BaseMapperType::ElementType ElementType
Definition: parallel.hh:97
bool consecutive() const
Definition: parallel.hh:162
int numBlocks() const
Definition: parallel.hh:164
bool fixedDataSize(int codim) const
Definition: parallel.hh:156
GlobalKey GlobalKeyType
Definition: parallel.hh:95
ParallelDofMapper(const ThisType &)=delete
void mapEachEntityDof(const Entity &entity, Functor f) const
Definition: parallel.hh:134
SizeType newIndex(SizeType hole, int blk) const
Definition: parallel.hh:169
ParallelDofMapper(const GridPartType &gridPart, const BaseMapperType &baseMapper)
Definition: parallel.hh:99
unsigned int maxNumDofs() const
Definition: parallel.hh:128
SizeType numberOfHoles(int blk) const
Definition: parallel.hh:167
bool contains(int codim) const
Definition: parallel.hh:154
SizeType oldIndex(SizeType hole, int blk) const
Definition: parallel.hh:168
SizeType oldOffSet(int blk) const
Definition: parallel.hh:166
unsigned int numDofs(const ElementType &element) const
Definition: parallel.hh:129
BaseMapper BaseMapperType
Definition: parallel.hh:92
const std::vector< GlobalKeyType > & mapping() const
Definition: parallel.hh:197
void mapEntityDofs(const Entity &entity, std::vector< GlobalKeyType > &indices) const
Definition: parallel.hh:140
unsigned int numEntityDofs(const Entity &entity) const
Definition: parallel.hh:147
const GridPartType & gridPart() const
Definition: parallel.hh:194
std::size_t SizeType
Definition: parallel.hh:94
void update()
Definition: parallel.hh:173
const BaseMapperType & baseMapper() const
Definition: parallel.hh:195