1 #ifndef DUNE_FEM_THREADITERATOR_HH 2 #define DUNE_FEM_THREADITERATOR_HH 6 #include <dune/common/exceptions.hh> 21 template <
class Gr
idPart, PartitionIteratorType ptype = InteriorBorder_Partition >
28 static const PartitionIteratorType
pitype = ptype ;
31 typedef typename GridPartType :: GridType
GridType;
45 #ifdef USE_SMP_PARALLEL 47 std::vector< IteratorType > iterators_;
49 std::vector< std::vector< int > > threadId_;
50 std::vector< FilterType* > filters_;
60 : gridPart_( gridPart )
61 , dofManager_( DofManagerType :: instance( gridPart_.grid() ) )
62 , indexSet_( gridPart_.indexSet() )
63 #ifdef USE_SMP_PARALLEL
65 , iterators_(
ThreadManager::maxThreads() + 1 , gridPart_.template
end< 0, pitype >() )
68 , communicationThread_( parameter.getValue<bool>(
"fem.threads.communicationthread", false)
71 parameter.getValue<bool>(
"fem.threads.verbose", false ) )
73 #ifdef USE_SMP_PARALLEL 84 #ifdef USE_SMP_PARALLEL 87 for(
size_t i = 0; i<filters_.size(); ++i )
94 const FilterType& filter(
const unsigned int thread )
const 96 assert( thread < filters_.size() );
97 return *(filters_[
thread ]);
104 #ifdef USE_SMP_PARALLEL 105 const int sequence = gridPart_.sequence();
107 if( sequence_ != sequence )
111 std::cerr <<
"Don't call ThreadIterator::update in a parallel environment!" << std::endl;
119 const IteratorType endit = gridPart_.template end< 0, pitype >();
120 IteratorType it = gridPart_.template begin< 0, pitype >();
124 for(
size_t thread = 0; thread <= maxThreads; ++
thread )
125 iterators_[ thread ] = endit ;
131 sequence_ = sequence;
136 iterators_[ 0 ] = it ;
141 const size_t size = indexSet_.size( 0 );
144 threadNum_.
resize( size );
146 for(
size_t i = 0; i<size; ++i) threadNum_[ i ] = -1;
149 size_t checkSize = 0;
150 const size_t roundOff = (iterSize % maxThreads);
151 const size_t counterBase = ((size_t) iterSize / maxThreads );
154 std::vector< int > nElems( maxThreads, 0 );
156 for(
size_t thread = 1; thread <= maxThreads; ++
thread )
159 const size_t counter = counterBase + (( (thread-1) < roundOff ) ? 1 : 0);
160 nElems[ thread-1 ] = counter ;
161 checkSize += counter ;
163 while( (i < counter) && (it != endit) )
165 const EntityType &entity = *it;
166 assert( std::size_t( indexSet_.index( entity ) ) < std::size_t( threadNum_.
size() ) );
167 threadNum_[ indexSet_.index( entity ) ] = thread - 1;
171 iterators_[
thread ] = it ;
173 iterators_[ maxThreads ] = endit ;
175 if( checkSize != iterSize )
177 assert( checkSize == iterSize );
178 DUNE_THROW(InvalidStateException,
"Partitioning inconsistent!");
182 sequence_ = sequence;
186 std::cout <<
"ThreadIterator: sequence = " << sequence_ <<
" size = " << checkSize << std::endl;
187 const size_t counterSize = nElems.size();
188 for(
size_t i = 0; i<counterSize; ++i )
189 std::cout <<
"ThreadIterator: T[" << i <<
"] = " << nElems[ i ] << std::endl;
192 checkConsistency( iterSize );
203 #ifdef USE_SMP_PARALLEL 206 return gridPart_.template begin< 0, pitype >();
213 #ifdef USE_SMP_PARALLEL 216 return gridPart_.template end< 0, pitype >();
221 int index(
const EntityType& entity )
const 223 return indexSet_.index( entity );
227 int thread(
const EntityType& entity )
const 229 #ifdef USE_SMP_PARALLEL 230 assert( std::size_t( threadNum_.
size() ) > std::size_t( indexSet_.index( entity ) ) );
233 return threadNum_[ indexSet_.index( entity ) ];
245 template <
class Iterator >
249 for( Iterator it = begin; it !=
end; ++ it )
254 #ifdef USE_SMP_PARALLEL 256 void checkConsistency(
const size_t totalElements )
260 std::set< int > indices ;
261 for(
int thread = 0; thread < maxThreads; ++
thread )
263 const IteratorType
end = iterators_[ thread+1 ];
264 for( IteratorType it = iterators_[ thread ]; it !=
end; ++it )
266 const int idx = gridPart_.indexSet().index( *it );
267 assert( indices.find( idx ) == indices.end() ) ;
268 indices.insert( idx );
271 assert( indices.size() == totalElements );
278 template <
class Gr
idPart, PartitionIteratorType pitype = InteriorBorder_Partition >
285 : BaseType( gridPart )
293 #endif // #ifndef DUNE_FEM_THREADITERATOR_HH int thread(const EntityType &entity) const
return thread number this entity belongs to
Definition: threaditerator.hh:227
size_t size() const
return number of enties of array
Definition: arrays.hh:213
Thread iterators.
Definition: threaditerator.hh:22
Container for User Specified Parameters.
Definition: io/parameter.hh:187
int index(const EntityType &entity) const
return thread number this entity belongs to
Definition: threaditerator.hh:221
static const PartitionIteratorType pitype
Definition: threaditerator.hh:28
ThreadFilter< GridPartType > FilterType
Definition: threaditerator.hh:37
ThreadIteratorStorage(const GridPart &gridPart)
Definition: threaditerator.hh:284
ThreadIterator(const GridPartType &gridPart, const ParameterReader ¶meter=Parameter::container())
contructor creating thread iterators
Definition: threaditerator.hh:59
void resize(size_t nsize)
Definition: arrays.hh:491
The ThreadManager wrapps basic shared memory functionality provided by OpenMP or pthreads such as thr...
Definition: threadmanager.hh:199
Definition: datacollector.hh:45
const bool verbose_
Definition: threaditerator.hh:55
IteratorType end() const
return end iterator for current thread
Definition: threaditerator.hh:211
GridPartType::template Codim< 0 >::EntityType EntityType
Definition: threaditerator.hh:33
static int thread()
return thread number
Definition: threadmanager.hh:208
size_t countElements(const Iterator &begin, const Iterator &end) const
Definition: threaditerator.hh:246
const IndexSetType & indexSet_
Definition: threaditerator.hh:43
Definition: coordinate.hh:4
IteratorType begin() const
return begin iterator for current thread
Definition: threaditerator.hh:201
Storage of thread iterators using domain decomposition.
Definition: threaditeratorstorage.hh:23
void update()
update internal list of iterators
Definition: threaditerator.hh:102
const bool communicationThread_
Definition: threaditerator.hh:54
GridPartType::template Codim< 0 >::template Partition< pitype >::IteratorType IteratorType
Definition: threaditerator.hh:32
static ParameterContainer & container()
Definition: io/parameter.hh:190
DofManager< GridType > DofManagerType
Definition: threaditerator.hh:35
void setMemoryFactor(const double memFactor)
set memory factor
Definition: arrays.hh:465
GridPartType::GridType GridType
Definition: threaditerator.hh:31
Definition: threadfilter.hh:20
static int maxThreads()
return maximal number of threads possbile in the current run
Definition: threadmanager.hh:202
void setMasterRatio(const double ratio)
set ratio between master thread and other threads in comp time
Definition: threaditerator.hh:240
Storage of thread iterators.
Definition: threaditerator.hh:279
GridPartType::IndexSetType IndexSetType
Definition: threaditerator.hh:34
const DofManagerType & dofManager_
Definition: threaditerator.hh:42
static bool singleThreadMode()
returns true if program is operating on one thread currently
Definition: threadmanager.hh:217
const GridPartType & gridPart_
Definition: threaditerator.hh:41
GridPart GridPartType
Definition: threaditerator.hh:30