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_;
46 const typename MPIManager::ThreadPoolType& threadPool_;
51 std::vector< IteratorType > iterators_;
53 std::vector< std::vector< int > > threadId_;
54 std::vector< std::unique_ptr< FilterType > > filters_;
57 const bool communicationThread_;
62 explicit ThreadIterator(
const GridPartType& gridPart,
const ParameterReader ¶meter = Parameter::container() )
63 : gridPart_( gridPart )
65 , indexSet_( gridPart_.indexSet() )
66 , threadPool_( MPIManager::threadPool() )
68 , numThreads_( threadPool_.numThreads() )
69 , iterators_( threadPool_.maxThreads() + 1 , gridPart_.template
end< 0, pitype >() )
70 , threadId_( threadPool_.maxThreads() )
71 , filters_( threadPool_.maxThreads() )
72 , communicationThread_( parameter.getValue<bool>(
"fem.threads.communicationthread", false)
73 && threadPool_.maxThreads() > 1 )
75 parameter.getValue<bool>(
"fem.threads.verbose", false ) )
80 filters_[
thread ].reset(
new FilterType( gridPart_, threadNum_,
thread ) );
88 assert(
thread < filters_.size() );
89 return *(filters_[
thread ]);
95 const int sequence = dofManager_.
sequence();
97 if( sequence_ != sequence || numThreads_ != threadPool_.numThreads() )
99 if( ! threadPool_.singleThreadMode() )
101 std::cerr <<
"Don't call ThreadIterator::update in a parallel environment!" << std::endl;
107 numThreads_ = threadPool_.numThreads() ;
114 const size_t numThreads = numThreads_;
117 const IteratorType endit = gridPart_.template end< 0, pitype >();
120 iterators_.resize( numThreads+1, endit );
122 IteratorType it = gridPart_.template begin< 0, pitype >();
127 iterators_[
thread ] = endit ;
133 sequence_ = sequence;
138 iterators_[ 0 ] = it ;
142 const size_t iterSize = countElements( it, endit );
143 const size_t size = indexSet_.size( 0 );
148 for(
size_t i = 0; i<
size; ++i) threadNum_[ i ] = -1;
151 size_t checkSize = 0;
152 const size_t roundOff = (iterSize % numThreads);
153 const size_t counterBase = ((size_t) iterSize / numThreads );
156 std::vector< int > nElems( numThreads, 0 );
161 const size_t counter = counterBase + (( (
thread-1) < roundOff ) ? 1 : 0);
162 nElems[
thread-1 ] = counter ;
163 checkSize += counter ;
165 while( (i < counter) && (it != endit) )
167 const EntityType &entity = *it;
168 assert( std::size_t( indexSet_.index( entity ) ) < std::size_t( threadNum_.
size() ) );
169 threadNum_[ indexSet_.index( entity ) ] =
thread - 1;
173 iterators_[
thread ] = it ;
175 iterators_[ numThreads ] = endit ;
177 if( checkSize != iterSize )
179 assert( checkSize == iterSize );
184 sequence_ = sequence;
188 std::cout <<
"ThreadIterator: sequence = " << sequence_ <<
" size = " << checkSize << std::endl;
189 const size_t counterSize = nElems.size();
190 for(
size_t i = 0; i<counterSize; ++i )
191 std::cout <<
"ThreadIterator: T[" << i <<
"] = " << nElems[ i ] << std::endl;
194 checkConsistency( iterSize );
204 if( threadPool_.singleThreadMode() )
206 return gridPart_.template begin< 0, pitype >();
211 assert( threadPool_.thread() < numThreads_ );
212 return iterators_[ threadPool_.thread() ];
217 return iterators_[
thread ];
223 if( threadPool_.singleThreadMode() )
225 return gridPart_.template end< 0, pitype >();
230 assert( threadPool_.thread() < numThreads_ );
231 return iterators_[ threadPool_.thread() + 1 ];
236 return iterators_[
thread + 1 ];
240 int index(
const EntityType& entity )
const
242 return indexSet_.index( entity );
245 int threadParallel(
const EntityType& entity )
const
247 assert( std::size_t( threadNum_.
size() ) > std::size_t( indexSet_.index( entity ) ) );
250 return threadNum_[ indexSet_.index( entity ) ];
253 int thread(
const EntityType& entity )
const
255 if( threadPool_.singleThreadMode() )
258 return threadParallel(entity);
264 return threadPool_.thread();
273 template <
class Iterator >
274 size_t countElements(
const Iterator&
begin,
const Iterator&
end )
const
277 for( Iterator it =
begin; it !=
end; ++ it )
283 void checkConsistency(
const size_t totalElements )
286 const int numThreads = threadPool_.numThreads() ;
287 std::set< int > indices ;
290 const IteratorType
end = iterators_[
thread+1 ];
291 for( IteratorType it = iterators_[
thread ]; it !=
end; ++it )
293 const int idx = gridPart_.indexSet().index( *it );
294 assert( indices.find( idx ) == indices.end() ) ;
295 indices.insert( idx );
298 assert( indices.size() == totalElements );
304 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:307
Thread iterators.
Definition: threaditerator.hh:24
int thread() const
return thread number of running thread
Definition: threaditerator.hh:262
int thread(const EntityType &entity) const
return thread number this entity belongs to
Definition: threaditerator.hh:253
IteratorType end() const
return end iterator for current thread
Definition: threaditerator.hh:221
void update()
update internal list of iterators
Definition: threaditerator.hh:93
void setMasterRatio(const double ratio)
set ratio between master thread and other threads in comp time
Definition: threaditerator.hh:268
IteratorType begin() const
return begin iterator for current thread
Definition: threaditerator.hh:202
const FilterType & filter(const unsigned int thread) const
return filter for given thread
Definition: threaditerator.hh:86
int index(const EntityType &entity) const
return thread number this entity belongs to
Definition: threaditerator.hh:240
ThreadIterator(const GridPartType &gridPart, const ParameterReader ¶meter=Parameter::container())
contructor creating thread iterators
Definition: threaditerator.hh:62
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