1#ifndef DUNE_FEM_THREADITERATOR_HH
2#define DUNE_FEM_THREADITERATOR_HH
8#include <dune/fem/gridpart/filter/domainfilter.hh>
9#include <dune/fem/misc/threads/threaditeratorstorage.hh>
10#include <dune/fem/space/common/dofmanager.hh>
11#include <dune/fem/storage/dynamicarray.hh>
22 template <
class Gr
idPart, PartitionIteratorType ptype = InteriorBorder_Partition >
31 typedef GridPart GridPartType;
32 typedef typename GridPartType :: GridType GridType;
33 typedef typename GridPartType :: template
Codim< 0 > :: template Partition< pitype > :: IteratorType IteratorType ;
34 typedef typename GridPartType :: template
Codim< 0 > :: EntityType EntityType ;
35 typedef typename GridPartType :: IndexSetType IndexSetType ;
38 typedef DomainFilter<GridPartType> FilterType;
42 const GridPartType& gridPart_;
44 const IndexSetType& indexSet_;
49 std::vector< IteratorType > iterators_;
51 std::vector< std::vector< int > > threadId_;
52 std::vector< std::unique_ptr< FilterType > > filters_;
55 const bool communicationThread_;
60 explicit ThreadIterator(
const GridPartType& gridPart,
const ParameterReader ¶meter = Parameter::container() )
61 : gridPart_( gridPart )
63 , indexSet_( gridPart_.indexSet() )
65 , numThreads_( MPIManager::numThreads() )
66 , iterators_( MPIManager::maxThreads() + 1 , gridPart_.template
end< 0, pitype >() )
67 , threadId_( MPIManager::maxThreads() )
68 , filters_( MPIManager::maxThreads() )
69 , communicationThread_( parameter.getValue<bool>(
"fem.threads.communicationthread", false)
70 && Fem :: MPIManager :: maxThreads() > 1 )
72 parameter.getValue<bool>(
"fem.threads.verbose", false ) )
77 filters_[
thread ].reset(
new FilterType( gridPart_, threadNum_,
thread ) );
85 assert(
thread < filters_.size() );
86 return *(filters_[
thread ]);
92 const int sequence = dofManager_.
sequence();
94 if( sequence_ != sequence || numThreads_ != MPIManager :: numThreads() )
96 if( ! MPIManager :: singleThreadMode() )
98 std::cerr <<
"Don't call ThreadIterator::update in a parallel environment!" << std::endl;
104 numThreads_ = MPIManager :: numThreads() ;
111 const size_t numThreads = numThreads_;
114 const IteratorType endit = gridPart_.template end< 0, pitype >();
117 iterators_.resize( numThreads+1, endit );
119 IteratorType it = gridPart_.template begin< 0, pitype >();
124 iterators_[
thread ] = endit ;
130 sequence_ = sequence;
135 iterators_[ 0 ] = it ;
139 const size_t iterSize = countElements( it, endit );
140 const size_t size = indexSet_.size( 0 );
145 for(
size_t i = 0; i<
size; ++i) threadNum_[ i ] = -1;
148 size_t checkSize = 0;
149 const size_t roundOff = (iterSize % numThreads);
150 const size_t counterBase = ((size_t) iterSize / numThreads );
153 std::vector< int > nElems( numThreads, 0 );
158 const size_t counter = counterBase + (( (
thread-1) < roundOff ) ? 1 : 0);
159 nElems[
thread-1 ] = counter ;
160 checkSize += counter ;
162 while( (i < counter) && (it != endit) )
164 const EntityType &entity = *it;
165 assert( std::size_t( indexSet_.index( entity ) ) < std::size_t( threadNum_.
size() ) );
166 threadNum_[ indexSet_.index( entity ) ] =
thread - 1;
170 iterators_[
thread ] = it ;
172 iterators_[ numThreads ] = endit ;
174 if( checkSize != iterSize )
176 assert( checkSize == iterSize );
181 sequence_ = sequence;
185 std::cout <<
"ThreadIterator: sequence = " << sequence_ <<
" size = " << checkSize << std::endl;
186 const size_t counterSize = nElems.size();
187 for(
size_t i = 0; i<counterSize; ++i )
188 std::cout <<
"ThreadIterator: T[" << i <<
"] = " << nElems[ i ] << std::endl;
191 checkConsistency( iterSize );
201 if( MPIManager :: singleThreadMode() )
203 return gridPart_.template begin< 0, pitype >();
208 assert( MPIManager :: thread() < numThreads_ );
209 return iterators_[ MPIManager :: thread() ];
214 return iterators_[
thread ];
220 if( MPIManager :: singleThreadMode() )
222 return gridPart_.template end< 0, pitype >();
227 assert( MPIManager :: thread() < numThreads_ );
228 return iterators_[ MPIManager :: thread() + 1 ];
233 return iterators_[
thread + 1 ];
237 int index(
const EntityType& entity )
const
239 return indexSet_.index( entity );
242 int threadParallel(
const EntityType& entity )
const
244 assert( std::size_t( threadNum_.
size() ) > std::size_t( indexSet_.index( entity ) ) );
247 return threadNum_[ indexSet_.index( entity ) ];
250 int thread(
const EntityType& entity )
const
252 if( MPIManager::singleThreadMode() )
255 return threadParallel(entity);
264 template <
class Iterator >
265 size_t countElements(
const Iterator&
begin,
const Iterator&
end )
const
268 for( Iterator it =
begin; it !=
end; ++ it )
274 void checkConsistency(
const size_t totalElements )
277 const int numThreads = MPIManager :: numThreads() ;
278 std::set< int > indices ;
281 const IteratorType
end = iterators_[
thread+1 ];
282 for( IteratorType it = iterators_[
thread ]; it !=
end; ++it )
284 const int idx = gridPart_.indexSet().index( *it );
285 assert( indices.find( idx ) == indices.end() ) ;
286 indices.insert( idx );
289 assert( indices.size() == totalElements );
295 template <
class Gr
idPart, PartitionIteratorType pitype = InteriorBorder_Partition >
Definition: dofmanager.hh:786
void setMemoryFactor(double memFactor)
set memory factor
Definition: dynamicarray.hh:296
void resize(size_type nsize)
Definition: dynamicarray.hh:334
Container for User Specified Parameters.
Definition: parameter.hh:191
size_type size() const
return size of array
Definition: dynamicarray.hh:170
Storage of thread iterators using domain decomposition.
Definition: threaditeratorstorage.hh:22
Storage of thread iterators.
Definition: threaditerator.hh:298
Thread iterators.
Definition: threaditerator.hh:24
int thread(const EntityType &entity) const
return thread number this entity belongs to
Definition: threaditerator.hh:250
IteratorType end() const
return end iterator for current thread
Definition: threaditerator.hh:218
void update()
update internal list of iterators
Definition: threaditerator.hh:90
void setMasterRatio(const double ratio)
set ratio between master thread and other threads in comp time
Definition: threaditerator.hh:259
IteratorType begin() const
return begin iterator for current thread
Definition: threaditerator.hh:199
const FilterType & filter(const unsigned int thread) const
return filter for given thread
Definition: threaditerator.hh:83
int index(const EntityType &entity) const
return thread number this entity belongs to
Definition: threaditerator.hh:237
ThreadIterator(const GridPartType &gridPart, const ParameterReader ¶meter=Parameter::container())
contructor creating thread iterators
Definition: threaditerator.hh:60
Default exception if a function was called while the object is not in a valid state for that function...
Definition: exceptions.hh:281
A few common exception classes.
A set of traits classes to store static information about grid implementation.
int sequence() const
return number of sequence, if dofmanagers memory was changed by calling some method like resize,...
Definition: dofmanager.hh:1007
#define DUNE_THROW(E, m)
Definition: exceptions.hh:218
Dune namespace.
Definition: alignedallocator.hh:13
constexpr std::integral_constant< std::size_t, sizeof...(II)> size(std::integer_sequence< T, II... >)
Return the size of the sequence.
Definition: integersequence.hh:75
Specialize with 'true' if the grid implementation is thread safe, while it is not modified....
Definition: capabilities.hh:169