DUNE-FEM (unstable)

loadbalancer.hh
1#ifndef DUNE_FEM_LOADBALANCER_HH
2#define DUNE_FEM_LOADBALANCER_HH
3
4#include <cassert>
5#include <iostream>
6#include <set>
7#include <type_traits>
8#include <vector>
9
10#include <dune/common/timer.hh>
11
12#include <dune/fem/function/common/discretefunction.hh>
13#include <dune/fem/io/parameter.hh>
14#include <dune/fem/misc/mpimanager.hh>
15#include <dune/fem/space/common/datacollector.hh>
16#include <dune/fem/space/common/dofmanager.hh>
17#include <dune/fem/space/common/commoperations.hh>
18
19namespace Dune
20{
21
22 namespace Fem
23 {
24
37 {
38 protected:
41
42 public:
45
49 virtual bool loadBalance () = 0;
50
52 virtual double loadBalanceTime () const
53 {
54 return 0.0;
55 }
56 };
57
63 template <class GridType>
65 : virtual public LoadBalancerInterface
66 {
67 // type of this
69 // dof manager
71
72 // type of data inlining during load balance
73 typedef typename DofManagerType :: DataInlinerType DataInlinerType;
74
75 // type of data extraction during load balance
76 typedef typename DofManagerType :: DataXtractorType DataXtractorType;
77
78 // type of local data collector interface
79 typedef typename DataInlinerType :: LocalInterfaceType LocalDataInlinerInterfaceType;
80 // type of local data collector interface
81 typedef typename DataXtractorType :: LocalInterfaceType LocalDataXtractorInterfaceType;
82
83 typedef std::pair< LocalDataInlinerInterfaceType*, LocalDataXtractorInterfaceType* > LocalDataCollectorPairType;
84 typedef std::pair< DataInlinerType* , DataXtractorType* > DataCollectorPairType;
85 protected:
87 template< class RestrictProlongOperator >
88 LoadBalancer ( GridType &grid, RestrictProlongOperator &rpOp )
89 : grid_( grid ),
90 dm_ ( DofManagerType::instance( grid_ ) ),
91 localList_(),
92 collList_(),
93 commList_(rpOp),
94 balanceTime_( 0.0 )
95 {
96 rpOp.addToLoadBalancer( *this );
97 }
98
99 explicit LoadBalancer ( GridType &grid )
100 : grid_( grid ),
101 dm_ ( DofManagerType::instance( grid_ ) ),
102 localList_(),
103 collList_(),
104 commList_(),
105 balanceTime_( 0.0 )
106 {}
107
108 public:
110 virtual ~LoadBalancer ()
111 {
112 // clear objects from dof managers list
115
116 // remove data collectors
117 for(size_t i=0; i<collList_.size(); ++i)
118 {
119 delete collList_[ i ].first ;
120 delete collList_[ i ].second ;
121 }
122
123 // remove local data handler
124 for(size_t i=0; i<localList_.size(); ++i)
125 {
126 delete localList_[ i ].first ;
127 delete localList_[ i ].second ;
128 }
129 }
130
131 void communicate () const
132 {
133 // if overlap or ghost elements are available
134 // these need to be synchronized here
135 const auto gv = grid_.leafGridView();
136 if( (gv.overlapSize( 0 ) > 0) || (gv.ghostSize( 0 ) > 0) )
137 {
138 // exchange all modified data
139 // this also rebuilds the dependecy cache of the
140 // cached communication manager if used
141 commList_.exchange();
142 }
143#ifndef NDEBUG
144 // make sure every process is on the same page
145 gv.comm().barrier();
146#endif // #ifndef NDEBUG
147 }
148
151 {
152 bool changed = false;
153
154 // if only one core don't do anything
155 if( grid_.comm().size() <= 1 )
156 return changed;
157
158 // make sure this is only called in single thread mode
159 if( ! Fem :: MPIManager :: singleThreadMode() )
160 {
161 assert( Fem :: MPIManager :: singleThreadMode() );
162 DUNE_THROW(InvalidStateException,"LoadBalancer::loadBalance: only call in single thread mode!");
163 }
164
165 // get stopwatch
166 Dune::Timer timer ;
167
168
169 try {
170 // call grids load balance, only implemented in ALUGrid right now
171 changed = grid_.loadBalance( dm_ );
172 }
173 catch (const Exception& e)
174 {
175 std::cout << "P[" << grid_.comm().rank() << "] : Caught " << e.what() << " during LoadBalancer::loadBalance()." << std::endl;
176 std::abort();
177 }
178 catch (...)
179 {
180 std::cout << "P[" << grid_.comm().rank() << "] : Caught a generic exception during LoadBalancer::loadBalance()." << std::endl;
181 std::abort();
182 }
183
184 // get time
185 balanceTime_ = timer.elapsed();
186
187 // restore data consistency
188 communicate();
189
190 return changed;
191 }
192
194 virtual double loadBalanceTime() const
195 {
196 return balanceTime_;
197 }
198
200 template <class DiscreteFunctionType>
202 {
204 }
205
207 template <class DiscreteFunctionType>
209 {
210 addDiscreteFunction( df, df.defaultLoadBalanceContainsCheck() );
211 }
212
214 template <class DiscreteFunctionType, class ContainsCheck >
215 void addDiscreteFunction(DiscreteFunctionType& df, const ContainsCheck& containsCheck )
216 {
217 static_assert( std::is_convertible< DiscreteFunctionType, IsDiscreteFunction >::value,
218 "Only valid for discrete functions" );
219
221 //
222 // Note: DiscreteFunctionType here can also be
223 // FemPy::DiscreteFunctionList for
224 // python adaptation and load balance
225 //
227
228 const IsDiscreteFunction * fct = &df;
229
230 // if discrete functions is not in list already
231 if( listOfFcts_.find(fct) == listOfFcts_.end() )
232 {
233 // insert into set
234 listOfFcts_.insert( fct );
235
237 // data inliners
239 LocalDataCollectorPairType localPair;
240 DataCollectorPairType collPair;
241 {
243 LocalInlinerType * di = new LocalInlinerType(df, containsCheck );
244 localPair.first = di ;
245
246 typedef DataCollector<GridType, LocalInlinerType > DataCollectorImp;
247 DataCollectorImp* gdi = new DataCollectorImp( grid_, dm_ , *di, di->readWriteInfo() );
248 collPair.first = gdi ;
249
250 dm_.addDataInliner( *gdi );
251 }
252
254 // data xtractors
256 {
258 LocalXtractorType * dx = new LocalXtractorType(df, containsCheck );
259 localPair.second = dx ;
260
261 typedef DataCollector<GridType,LocalXtractorType> DataCollectorImp;
262 DataCollectorImp* gdx = new DataCollectorImp( grid_, dm_ , *dx, dx->readWriteInfo() );
263 collPair.second = gdx ;
264
265 dm_.addDataXtractor( *gdx );
266 }
267
268 // for later removal
269 localList_.push_back( localPair );
270 collList_.push_back( collPair );
271
272 // enable this discrete function for dof compression
274 }
275 }
276
277 protected:
279 GridType & grid_;
280
283
284 // list of created local data collectors
285 std::vector< LocalDataCollectorPairType > localList_;
286 std::vector< DataCollectorPairType > collList_;
287
288 // list of already added discrete functions
289 std::set< const IsDiscreteFunction * > listOfFcts_;
290
291 mutable CommunicationManagerList commList_;
292
293 // time for last load balance call
294 double balanceTime_;
295 };
296
299 } // namespace Fem
300
301} // namespace Dune
302#endif // #ifndef DUNE_FEM_LOADBALANCER_HH
Base class for Dune-Exceptions.
Definition: exceptions.hh:96
Proxy class to DependencyCache which is singleton per space.
Definition: communicationmanager.hh:304
The DataCollector is an example for a grid walk done while load balancing moves entities from one pro...
Definition: datacollector.hh:435
Definition: dofmanager.hh:786
base class for determing whether a class is a discrete function or not
Definition: discretefunction.hh:53
Interface class for load balancing.
Definition: loadbalancer.hh:37
virtual double loadBalanceTime() const
time that last load balance cycle took
Definition: loadbalancer.hh:52
LoadBalancerInterface()
default constructor
Definition: loadbalancer.hh:40
virtual bool loadBalance()=0
call load balance, returns true if grid was changed
virtual ~LoadBalancerInterface()
destructor
Definition: loadbalancer.hh:44
This class manages the adaptation process. If the method adapt is called, then the grid is adapted an...
Definition: loadbalancer.hh:66
virtual double loadBalanceTime() const
time that last load balance cycle took
Definition: loadbalancer.hh:194
bool loadBalance()
do load balance
Definition: loadbalancer.hh:150
void addToLoadBalancer(DiscreteFunctionType &df)
add discrete function to data inliner/xtractor list
Definition: loadbalancer.hh:201
LoadBalancer(GridType &grid, RestrictProlongOperator &rpOp)
constructor of LoadBalancer
Definition: loadbalancer.hh:88
virtual ~LoadBalancer()
destructor
Definition: loadbalancer.hh:110
GridType & grid_
corresponding grid
Definition: loadbalancer.hh:279
void addDiscreteFunction(DiscreteFunctionType &df, const ContainsCheck &containsCheck)
add discrete function to data inliner/xtractor list
Definition: loadbalancer.hh:215
void addDiscreteFunction(DiscreteFunctionType &df)
add discrete function to data inliner/xtractor list
Definition: loadbalancer.hh:208
DofManagerType & dm_
DofManager corresponding to grid.
Definition: loadbalancer.hh:282
Inline DiscreteFunction data during load balancing.
Definition: datacollector.hh:683
Inline DiscreteFunction data during load balancing.
Definition: datacollector.hh:775
forward declaration
Definition: discretefunction.hh:51
void enableDofCompression()
Enable this discrete function for dof compression, i.e. during grid changes a dof compression is done...
Definition: discretefunction.hh:131
Default exception if a function was called while the object is not in a valid state for that function...
Definition: exceptions.hh:281
A simple stop watch.
Definition: timer.hh:43
double elapsed() const noexcept
Get elapsed user-time from last reset until now/last stop in seconds.
Definition: timer.hh:77
void exchange() const
Definition: communicationmanager.hh:391
void addDataXtractor(DataCollType &d)
add data handler for data xtracting to dof manager
Definition: dofmanager.hh:1115
void clearDataXtractors()
clear data xtractor list
Definition: dofmanager.hh:1121
void clearDataInliners()
clear data inliner list
Definition: dofmanager.hh:1108
void addDataInliner(DataCollType &d)
add data handler for data inlining to dof manager
Definition: dofmanager.hh:1102
const char * what() const noexcept override
output internal message buffer
Definition: exceptions.cc:37
#define DUNE_THROW(E, m)
Definition: exceptions.hh:218
Dune namespace.
Definition: alignedallocator.hh:13
A simple timing class.
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden  |  generated with Hugo v0.111.3 (Nov 21, 23:30, 2024)