dune-fem  2.8-git
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 
13 #include <dune/fem/io/parameter.hh>
18 
19 namespace Dune
20 {
21 
22  namespace Fem
23  {
24 
37  {
38  protected:
41 
42  public:
44  virtual ~LoadBalancerInterface () {}
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 
150  bool loadBalance ()
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  assert( Fem :: ThreadManager :: singleThreadMode() );
160 
161  // get stopwatch
162  Dune::Timer timer ;
163 
164 
165  try {
166  // call grids load balance, only implemented in ALUGrid right now
167  changed = grid_.loadBalance( dm_ );
168  }
169  catch (...)
170  {
171  std::cout << "P[" << grid_.comm().rank() << "] : Caught an exepction during load balance" << std::endl;
172  abort();
173  }
174 
175  // get time
176  balanceTime_ = timer.elapsed();
177 
178  // restore data consistency
179  communicate();
180 
181  return changed;
182  }
183 
185  virtual double loadBalanceTime() const
186  {
187  return balanceTime_;
188  }
189 
191  template <class DiscreteFunctionType>
192  void addToLoadBalancer(DiscreteFunctionType& df)
193  {
195  }
196 
198  template <class DiscreteFunctionType>
199  void addDiscreteFunction( DiscreteFunctionType& df )
200  {
201  addDiscreteFunction( df, df.defaultLoadBalanceContainsCheck() );
202  }
203 
205  template <class DiscreteFunctionType, class ContainsCheck >
206  void addDiscreteFunction(DiscreteFunctionType& df, const ContainsCheck& containsCheck )
207  {
208  static_assert( std::is_convertible< DiscreteFunctionType, IsDiscreteFunction >::value,
209  "Only valid for discrete functions" );
210 
212  //
213  // Note: DiscreteFunctionType here can also be
214  // FemPy::DiscreteFunctionList for
215  // python adaptation and load balance
216  //
218 
219  const IsDiscreteFunction * fct = &df;
220 
221  // if discrete functions is not in list already
222  if( listOfFcts_.find(fct) == listOfFcts_.end() )
223  {
224  // insert into set
225  listOfFcts_.insert( fct );
226 
228  // data inliners
230  LocalDataCollectorPairType localPair;
231  DataCollectorPairType collPair;
232  {
234  LocalInlinerType * di = new LocalInlinerType(df, containsCheck );
235  localPair.first = di ;
236 
237  typedef DataCollector<GridType, LocalInlinerType > DataCollectorImp;
238  DataCollectorImp* gdi = new DataCollectorImp( grid_, dm_ , *di, di->readWriteInfo() );
239  collPair.first = gdi ;
240 
241  dm_.addDataInliner( *gdi );
242  }
243 
245  // data xtractors
247  {
249  LocalXtractorType * dx = new LocalXtractorType(df, containsCheck );
250  localPair.second = dx ;
251 
252  typedef DataCollector<GridType,LocalXtractorType> DataCollectorImp;
253  DataCollectorImp* gdx = new DataCollectorImp( grid_, dm_ , *dx, dx->readWriteInfo() );
254  collPair.second = gdx ;
255 
256  dm_.addDataXtractor( *gdx );
257  }
258 
259  // for later removal
260  localList_.push_back( localPair );
261  collList_.push_back( collPair );
262 
263  // enable this discrete function for dof compression
264  df.enableDofCompression();
265  }
266  }
267 
268  protected:
270  GridType & grid_;
271 
274 
275  // list of created local data collectors
276  std::vector< LocalDataCollectorPairType > localList_;
277  std::vector< DataCollectorPairType > collList_;
278 
279  // list of already added discrete functions
280  std::set< const IsDiscreteFunction * > listOfFcts_;
281 
283 
284  // time for last load balance call
285  double balanceTime_;
286  };
287 
290  } // namespace Fem
291 
292 } // namespace Dune
293 #endif // #ifndef DUNE_FEM_LOADBALANCER_HH
void exchange() const
Definition: communicationmanager.hh:377
DataCollectorInterface< GridType, InlineStreamType > DataInlinerType
Definition: dofmanager.hh:781
void addDataXtractor(DataCollType &d)
add data handler for data xtracting to dof manager
Definition: dofmanager.hh:1088
DataCollectorInterface< GridType, XtractStreamType > DataXtractorType
Definition: dofmanager.hh:780
void clearDataXtractors()
clear data xtractor list
Definition: dofmanager.hh:1094
void clearDataInliners()
clear data inliner list
Definition: dofmanager.hh:1081
void addDataInliner(DataCollType &d)
add data handler for data inlining to dof manager
Definition: dofmanager.hh:1075
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:762
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:185
bool loadBalance()
do load balance
Definition: loadbalancer.hh:150
double balanceTime_
Definition: loadbalancer.hh:285
void addToLoadBalancer(DiscreteFunctionType &df)
add discrete function to data inliner/xtractor list
Definition: loadbalancer.hh:192
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:276
GridType & grid_
corresponding grid
Definition: loadbalancer.hh:270
std::vector< DataCollectorPairType > collList_
Definition: loadbalancer.hh:277
void addDiscreteFunction(DiscreteFunctionType &df, const ContainsCheck &containsCheck)
add discrete function to data inliner/xtractor list
Definition: loadbalancer.hh:206
void addDiscreteFunction(DiscreteFunctionType &df)
add discrete function to data inliner/xtractor list
Definition: loadbalancer.hh:199
void communicate() const
Definition: loadbalancer.hh:131
DofManagerType & dm_
DofManager corresponding to grid.
Definition: loadbalancer.hh:273
CommunicationManagerList commList_
Definition: loadbalancer.hh:282
std::set< const IsDiscreteFunction * > listOfFcts_
Definition: loadbalancer.hh:280