dune-fem 2.8.0
Loading...
Searching...
No Matches
domainthreaditerator.hh
Go to the documentation of this file.
1#ifndef DUNE_FEM_DOMAINTHREADITERATOR_HH
2#define DUNE_FEM_DOMAINTHREADITERATOR_HH
3
4#include <vector>
5
6#include <dune/common/exceptions.hh>
7
12
13#ifdef USE_SMP_PARALLEL
14#if HAVE_DUNE_ALUGRID
15#define USE_THREADPARTITIONER
17#endif
18#endif
19
20namespace Dune {
21
22 namespace Fem {
23
25 template <class GridPart>
27 {
28 public:
29 typedef GridPart GridPartType;
30 typedef typename GridPartType :: GridType GridType;
31 typedef typename GridPartType :: IndexSetType IndexSetType;
32
34#ifdef USE_THREADPARTITIONER
35 typedef FilteredGridPart< GridPartType, FilterType, false > FilteredGridPartType ;
36
37 typedef typename FilteredGridPartType :: template Codim< 0 > :: IteratorType
39#else
40 typedef typename GridPartType :: template Codim< 0 > :: IteratorType IteratorType ;
41#endif
42
43 typedef typename IteratorType :: Entity EntityType ;
44
46
47 static const PartitionIteratorType pitype = GridPartType :: indexSetPartitionType ;
48
49#ifdef USE_THREADPARTITIONER
50 typedef ThreadPartitioner< GridPartType > ThreadPartitionerType;
51 typedef typename ThreadPartitionerType :: Method PartitioningMethodType ;
52#endif // #ifdef USE_SMP_PARALLEL
53
54 protected:
58
59#ifdef USE_THREADPARTITIONER
60 int sequence_;
61 int numThreads_;
62 std::vector< std::unique_ptr< FilteredGridPartType > > filteredGridParts_;
63
64 typedef typename FilterType :: DomainArrayType ThreadArrayType;
65 ThreadArrayType threadNum_;
66#endif
67 // ratio of computing time needed by the master thread compared to the other threads
68 double masterRatio_ ;
69
70#ifdef USE_THREADPARTITIONER
71 const PartitioningMethodType method_;
72#endif // #ifdef USE_SMP_PARALLEL
73
74 // if true, thread 0 does only communication and no computation
76 const bool verbose_ ;
77
78 protected:
79#ifdef USE_THREADPARTITIONER
80 static PartitioningMethodType getMethod ( const ParameterReader &parameter )
81 {
82 // default is recursive
83 const std::string methodNames[] = { "recursive", "kway", "sfc" };
84 return (PartitioningMethodType ) parameter.getEnum("fem.threads.partitioningmethod", methodNames, 0 );
85 }
86#endif // #ifdef USE_SMP_PARALLEL
87
88 public:
90 explicit DomainDecomposedIterator( const GridPartType& gridPart, const ParameterReader &parameter = Parameter::container() )
91 : gridPart_( gridPart ),
92 dofManager_( DofManagerType :: instance( gridPart_.grid() ) ),
93 indexSet_( gridPart_.indexSet() )
94#ifdef USE_THREADPARTITIONER
95 , sequence_( -1 )
96 , numThreads_( Fem :: MPIManager :: numThreads() )
97 , filteredGridParts_( Fem :: MPIManager :: maxThreads() )
98#endif
99 , masterRatio_( 1.0 )
100#ifdef USE_THREADPARTITIONER
101 , method_( getMethod( parameter ) )
102#endif // #ifdef USE_SMP_PARALLEL
103 , communicationThread_( parameter.getValue<bool>("fem.threads.communicationthread", false)
104 && Fem :: MPIManager :: maxThreads() > 1 ) // only possible if maxThreads > 1
105 , verbose_( Parameter::verbose() &&
106 parameter.getValue<bool>("fem.threads.verbose", false ) )
107 {
108#ifdef USE_THREADPARTITIONER
109 for(int thread=0; thread < Fem :: MPIManager :: maxThreads(); ++thread )
110 {
111 // thread is the thread number of this filter
112 filteredGridParts_[ thread ].reset(
113 new FilteredGridPartType( const_cast<GridPartType &> (gridPart_),
114 FilterType( gridPart_, threadNum_, thread ) ) );
115 }
116
117 threadNum_.setMemoryFactor( 1.1 );
118#endif
119 update();
120 }
121
122#ifdef USE_THREADPARTITIONER
124 const FilterType& filter( const int thread ) const
125 {
126 return filteredGridParts_[ thread ]->filter();
127 }
128#endif // USE_SMP_PARALLEL
129
131 void update()
132 {
133#ifdef USE_THREADPARTITIONER
134 const int sequence = gridPart_.sequence() ;
135 // if grid got updated also update iterators
136 if( sequence_ != sequence || numThreads_ != MPIManager :: numThreads() )
137 {
138 if( ! MPIManager :: singleThreadMode() )
139 {
140 std::cerr << "Don't call ThreadIterator::update in a parallel environment!" << std::endl;
141 assert( false );
142 abort();
143 }
144
145 const int commThread = communicationThread_ ? 1 : 0;
146 // get number of partitions possible
147 const size_t partitions = MPIManager :: numThreads() - commThread ;
148
149 // create partitioner
150 ThreadPartitionerType db( gridPart_, partitions, masterRatio_ );
151 // do partitioning
152 db.serialPartition( method_ );
153
154 // get end iterator
155 typedef typename GridPartType :: template Codim< 0 > :: IteratorType GPIteratorType;
156 const GPIteratorType endit = gridPart_.template end< 0 >();
157
158 // get size for index set
159 const size_t size = indexSet_.size( 0 );
160
161 // resize threads storage
162 threadNum_.resize( size );
163 // set all values to default value
164 for(size_t i = 0; i<size; ++i) threadNum_[ i ] = -1;
165
166 {
167 // just for diagnostics
168 std::vector< int > counter( partitions+commThread , 0 );
169
170 int numInteriorElems = 0;
171 for(GPIteratorType it = gridPart_.template begin< 0 >();
172 it != endit; ++it, ++numInteriorElems )
173 {
174 const EntityType& entity = * it;
175 const int rank = db.getRank( entity ) + commThread ;
176 assert( rank >= 0 );
177 //std::cout << "Got rank = " << rank << "\n";
178 threadNum_[ indexSet_.index( entity ) ] = rank ;
179 ++counter[ rank ];
180 }
181
182 // update sequence number
183 sequence_ = sequence;
184
185 // update numThreads_
186 numThreads_ = MPIManager :: numThreads();
187
188 if( verbose_ )
189 {
190 std::cout << "DomainDecomposedIterator: sequence = " << sequence_ << " size = " << numInteriorElems << std::endl;
191 const size_t counterSize = counter.size();
192 for(size_t i = 0; i<counterSize; ++i )
193 std::cout << "DomainDecomposedIterator: T[" << i << "] = " << counter[ i ] << std::endl;
194 }
195
196#ifndef NDEBUG
197 // check that all interior elements have got a valid thread number
198 for(GPIteratorType it = gridPart_.template begin< 0 >(); it != endit; ++it )
199 {
200 assert( threadNum_[ indexSet_.index( *it ) ] >= 0 );
201 }
202#endif
203 //for(size_t i = 0; i<size; ++i )
204 //{
205 // //std::cout << threadNum_[ i ] << std::endl;
206 //}
207 }
208 }
209#endif
210 }
211
214 {
215#ifdef USE_THREADPARTITIONER
216 if( ! MPIManager :: singleThreadMode () )
217 {
218 const int thread = MPIManager :: thread() ;
219 if( communicationThread_ && thread == 0 )
220 return filteredGridParts_[ thread ]->template end< 0 > ();
221 else
222 return filteredGridParts_[ thread ]->template begin< 0 > ();
223 }
224 else
225#endif
226 {
227 return gridPart_.template begin< 0 > ();
228 }
229 }
230
233 {
234#ifdef USE_THREADPARTITIONER
235 if( ! MPIManager :: singleThreadMode () )
236 {
237 return filteredGridParts_[ MPIManager :: thread() ]->template end< 0 > ();
238 }
239 else
240#endif
241 {
242 return gridPart_.template end< 0 > ();
243 }
244 }
245
247 int index(const EntityType& entity ) const
248 {
249 return indexSet_.index( entity );
250 }
251
253 int thread(const EntityType& entity ) const
254 {
255#ifdef USE_THREADPARTITIONER
256 assert( (int)threadNum_.size() > (int) indexSet_.index( entity ) );
257 // NOTE: this number can also be negative for ghost elements or elements
258 // that do not belong to the set covered by the space iterators
259 return threadNum_[ indexSet_.index( entity ) ];
260#else
261 return 0;
262#endif
263 }
264
265 void setMasterRatio( const double ratio )
266 {
267 masterRatio_ = 0.5 * (ratio + masterRatio_);
268 }
269 };
270
271
273 template <class GridPart>
275 : public ThreadIteratorStorageBase< DomainDecomposedIterator< GridPart > >
276 {
278 public:
279 DomainDecomposedIteratorStorage( const GridPart& gridPart )
280 : BaseType( gridPart )
281 {}
282 };
283
284 } // end namespace Fem
285
286} // end namespace Dune
287
288#endif // #ifndef DUNE_FEM_DG_DOMAINTHREADITERATOR_HH
Definition: bindguard.hh:11
Definition: domainfilter.hh:55
A FilteredGridPart allows to extract a set of entities from a grid satisfying a given constrainted de...
Definition: filteredgridpart.hh:228
Container for User Specified Parameters.
Definition: io/parameter.hh:191
static ParameterContainer & container()
Definition: io/parameter.hh:193
int getEnum(const std::string &key, const std::string(&values)[n]) const
Definition: reader.hh:225
Definition: mpimanager.hh:337
Thread iterators using domain decomposition.
Definition: domainthreaditerator.hh:27
void setMasterRatio(const double ratio)
Definition: domainthreaditerator.hh:265
const bool communicationThread_
Definition: domainthreaditerator.hh:75
static const PartitionIteratorType pitype
Definition: domainthreaditerator.hh:47
GridPart GridPartType
Definition: domainthreaditerator.hh:29
int index(const EntityType &entity) const
return thread number this entity belongs to
Definition: domainthreaditerator.hh:247
const IndexSetType & indexSet_
Definition: domainthreaditerator.hh:57
GridPartType::template Codim< 0 >::IteratorType IteratorType
Definition: domainthreaditerator.hh:40
const bool verbose_
Definition: domainthreaditerator.hh:76
IteratorType begin() const
return begin iterator for current thread
Definition: domainthreaditerator.hh:213
IteratorType end() const
return end iterator for current thread
Definition: domainthreaditerator.hh:232
DomainFilter< GridPartType > FilterType
Definition: domainthreaditerator.hh:33
GridPartType::IndexSetType IndexSetType
Definition: domainthreaditerator.hh:31
const DofManagerType & dofManager_
Definition: domainthreaditerator.hh:56
DofManager< GridType > DofManagerType
Definition: domainthreaditerator.hh:45
GridPartType::GridType GridType
Definition: domainthreaditerator.hh:30
int thread(const EntityType &entity) const
return thread number this entity belongs to
Definition: domainthreaditerator.hh:253
const GridPartType & gridPart_
Definition: domainthreaditerator.hh:55
IteratorType::Entity EntityType
Definition: domainthreaditerator.hh:43
DomainDecomposedIterator(const GridPartType &gridPart, const ParameterReader &parameter=Parameter::container())
contructor creating thread iterators
Definition: domainthreaditerator.hh:90
void update()
update internal list of iterators
Definition: domainthreaditerator.hh:131
double masterRatio_
Definition: domainthreaditerator.hh:68
Storage of thread iterators using domain decomposition.
Definition: domainthreaditerator.hh:276
DomainDecomposedIteratorStorage(const GridPart &gridPart)
Definition: domainthreaditerator.hh:279
Storage of thread iterators using domain decomposition.
Definition: threaditeratorstorage.hh:22
Definition: dofmanager.hh:761