dune-fem 2.12-git
Loading...
Searching...
No Matches
loadbalancer.hh
Go to the documentation of this file.
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
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
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
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>
201 void addToLoadBalancer(DiscreteFunctionType& df)
202 {
204 }
205
207 template <class DiscreteFunctionType>
208 void addDiscreteFunction( DiscreteFunctionType& df )
209 {
210 addDiscreteFunction( df, df.defaultLoadBalanceContainsCheck() );
211 }
212
214 template <class DiscreteFunctionType, class ContainsCheck >
215 void addDiscreteFunction(DiscreteFunctionType& df, const ContainsCheck& containsCheck )
216 {
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
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
273 df.enableDofCompression();
274 }
275 }
276
277 protected:
279 GridType & grid_;
280
283
284 // list of created local data collectors
287
288 // list of already added discrete functions
290
292
293 // time for last load balance call
295 };
296
299 } // namespace Fem
300
301} // namespace Dune
302#endif // #ifndef DUNE_FEM_LOADBALANCER_HH
#define DUNE_THROW(E,...)
const char * what() const noexcept override
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
double elapsed() const noexcept
base class for determing whether a class is a discrete function or not
Definition common/discretefunction.hh:53
Proxy class to DependencyCache which is singleton per space.
Definition communicationmanager.hh:304
Definition dofmanager.hh:786
Definition datacollector.hh:83
The DataCollector is an example for a grid walk done while load balancing moves entities from one pro...
Definition datacollector.hh:435
Inline DiscreteFunction data during load balancing.
Definition datacollector.hh:683
Inline DiscreteFunction data during load balancing.
Definition datacollector.hh:775
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
LoadBalancer(GridType &grid)
Definition loadbalancer.hh:99
virtual double loadBalanceTime() const
time that last load balance cycle took
Definition loadbalancer.hh:194
bool loadBalance()
do load balance
Definition loadbalancer.hh:150
double balanceTime_
Definition loadbalancer.hh:294
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
std::vector< LocalDataCollectorPairType > localList_
Definition loadbalancer.hh:285
GridType & grid_
corresponding grid
Definition loadbalancer.hh:279
std::vector< DataCollectorPairType > collList_
Definition loadbalancer.hh:286
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
void communicate() const
Definition loadbalancer.hh:131
DofManagerType & dm_
DofManager corresponding to grid.
Definition loadbalancer.hh:282
CommunicationManagerList commList_
Definition loadbalancer.hh:291
std::set< const IsDiscreteFunction * > listOfFcts_
Definition loadbalancer.hh:289
T abort(T... args)
T endl(T... args)