dune-fem 2.8.0
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
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
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::adapt: 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 (...)
174 {
175 std::cout << "P[" << grid_.comm().rank() << "] : Caught an exepction during load balance" << std::endl;
176 abort();
177 }
178
179 // get time
180 balanceTime_ = timer.elapsed();
181
182 // restore data consistency
183 communicate();
184
185 return changed;
186 }
187
189 virtual double loadBalanceTime() const
190 {
191 return balanceTime_;
192 }
193
195 template <class DiscreteFunctionType>
196 void addToLoadBalancer(DiscreteFunctionType& df)
197 {
199 }
200
202 template <class DiscreteFunctionType>
203 void addDiscreteFunction( DiscreteFunctionType& df )
204 {
205 addDiscreteFunction( df, df.defaultLoadBalanceContainsCheck() );
206 }
207
209 template <class DiscreteFunctionType, class ContainsCheck >
210 void addDiscreteFunction(DiscreteFunctionType& df, const ContainsCheck& containsCheck )
211 {
212 static_assert( std::is_convertible< DiscreteFunctionType, IsDiscreteFunction >::value,
213 "Only valid for discrete functions" );
214
216 //
217 // Note: DiscreteFunctionType here can also be
218 // FemPy::DiscreteFunctionList for
219 // python adaptation and load balance
220 //
222
223 const IsDiscreteFunction * fct = &df;
224
225 // if discrete functions is not in list already
226 if( listOfFcts_.find(fct) == listOfFcts_.end() )
227 {
228 // insert into set
229 listOfFcts_.insert( fct );
230
232 // data inliners
234 LocalDataCollectorPairType localPair;
235 DataCollectorPairType collPair;
236 {
238 LocalInlinerType * di = new LocalInlinerType(df, containsCheck );
239 localPair.first = di ;
240
241 typedef DataCollector<GridType, LocalInlinerType > DataCollectorImp;
242 DataCollectorImp* gdi = new DataCollectorImp( grid_, dm_ , *di, di->readWriteInfo() );
243 collPair.first = gdi ;
244
245 dm_.addDataInliner( *gdi );
246 }
247
249 // data xtractors
251 {
253 LocalXtractorType * dx = new LocalXtractorType(df, containsCheck );
254 localPair.second = dx ;
255
256 typedef DataCollector<GridType,LocalXtractorType> DataCollectorImp;
257 DataCollectorImp* gdx = new DataCollectorImp( grid_, dm_ , *dx, dx->readWriteInfo() );
258 collPair.second = gdx ;
259
260 dm_.addDataXtractor( *gdx );
261 }
262
263 // for later removal
264 localList_.push_back( localPair );
265 collList_.push_back( collPair );
266
267 // enable this discrete function for dof compression
268 df.enableDofCompression();
269 }
270 }
271
272 protected:
274 GridType & grid_;
275
278
279 // list of created local data collectors
280 std::vector< LocalDataCollectorPairType > localList_;
281 std::vector< DataCollectorPairType > collList_;
282
283 // list of already added discrete functions
284 std::set< const IsDiscreteFunction * > listOfFcts_;
285
287
288 // time for last load balance call
290 };
291
294 } // namespace Fem
295
296} // namespace Dune
297#endif // #ifndef DUNE_FEM_LOADBALANCER_HH
void exchange() const
Definition: communicationmanager.hh:377
void addDataXtractor(DataCollType &d)
add data handler for data xtracting to dof manager
Definition: dofmanager.hh:1085
void clearDataXtractors()
clear data xtractor list
Definition: dofmanager.hh:1091
void clearDataInliners()
clear data inliner list
Definition: dofmanager.hh:1078
void addDataInliner(DataCollType &d)
add data handler for data inlining to dof manager
Definition: dofmanager.hh:1072
Definition: bindguard.hh:11
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:290
Definition: dofmanager.hh:761
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:436
Inline DiscreteFunction data during load balancing.
Definition: datacollector.hh:680
Inline DiscreteFunction data during load balancing.
Definition: datacollector.hh:772
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:189
bool loadBalance()
do load balance
Definition: loadbalancer.hh:150
double balanceTime_
Definition: loadbalancer.hh:289
void addToLoadBalancer(DiscreteFunctionType &df)
add discrete function to data inliner/xtractor list
Definition: loadbalancer.hh:196
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:280
GridType & grid_
corresponding grid
Definition: loadbalancer.hh:274
std::vector< DataCollectorPairType > collList_
Definition: loadbalancer.hh:281
void addDiscreteFunction(DiscreteFunctionType &df, const ContainsCheck &containsCheck)
add discrete function to data inliner/xtractor list
Definition: loadbalancer.hh:210
void addDiscreteFunction(DiscreteFunctionType &df)
add discrete function to data inliner/xtractor list
Definition: loadbalancer.hh:203
void communicate() const
Definition: loadbalancer.hh:131
DofManagerType & dm_
DofManager corresponding to grid.
Definition: loadbalancer.hh:277
CommunicationManagerList commList_
Definition: loadbalancer.hh:286
std::set< const IsDiscreteFunction * > listOfFcts_
Definition: loadbalancer.hh:284