dune-fem  2.4.1-rc
communicationmanager.hh
Go to the documentation of this file.
1 #ifndef DUNE_FEM_COMMUNICATION_MANAGER_HH
2 #define DUNE_FEM_COMMUNICATION_MANAGER_HH
3 
4 //- system includes
5 #include <iostream>
6 #include <map>
7 #include <vector>
8 
9 //- Dune includes
10 #include <dune/common/timer.hh>
11 #include <dune/grid/common/datahandleif.hh>
12 #include <dune/grid/common/grid.hh>
13 
16 
17 // include ALUGrid to check whether the
18 // parallel version is avaiable
19 #if defined ENABLE_ALUGRID
20 // we only need the 3d version here.
21 #include <dune/grid/alugrid/3d/alugrid.hh>
22 #elif HAVE_DUNE_ALUGRID
23 #include <dune/alugrid/3d/alugrid.hh>
24 #endif
25 
26 // default is: enabled
27 #ifndef WANT_CACHED_COMM_MANAGER
28 #define WANT_CACHED_COMM_MANAGER 1
29 #endif
30 
31 #if ALU3DGRID_PARALLEL && WANT_CACHED_COMM_MANAGER
32 #define USE_CACHED_COMM_MANAGER
33 #else
34 #ifndef NDEBUG
35 #if HAVE_MPI == 0
36  #ifdef DUNE_DEVEL_MODE
37  #warning "HAVE_MPI == 0, therefore default CommunicationManager is used!"
38  #endif
39 #elif !ALU3DGRID_PARALLEL
40  #warning "No Parallel ALUGrid found, using default CommunicationManager!"
41 #elif ! WANT_CACHED_COMM_MANAGER
42  #warning "CachedCommunication Manager disabled by WANT_CACHED_COMM_MANAGER=0!"
43 #endif
44 #endif
45 #endif
46 
47 #undef WANT_CACHED_COMM_MANAGER
48 
49 #ifdef USE_CACHED_COMM_MANAGER
50 #include "cachedcommmanager.hh"
51 #endif
52 
53 //- Dune-fem includes
57 
58 namespace Dune
59 {
60 
61  namespace Fem
62  {
63 
64  // External Forward Declarations
65  // -----------------------------
66 
67  template< class DiscreteFunctionSpace >
68  class PetscDiscreteFunction;
69 
70  class IsDiscreteFunction;
71 
72 
73 
83  template< class Space >
85  {
86  public:
87  typedef Space SpaceType;
88 
89  protected:
91 
93  // begin NonBlockingCommunication
96  {
97  const SpaceType& space_;
98  const InterfaceType interface_;
99  const CommunicationDirection dir_;
100 
101  public:
102  NonBlockingCommunication( const SpaceType& space,
103  InterfaceType interface,
104  CommunicationDirection dir )
105  : space_( space ),
106  interface_( interface ),
107  dir_ ( dir )
108  {}
109 
111  template < class DiscreteFunction >
112  void send( const DiscreteFunction& discreteFunction )
113  {
114  // nothing to do here, since DUNE does not support
115  // non-blocking communcation yet
116  }
117 
119  template < class DiscreteFunctionSpace, class Operation >
121  const Operation* operation )
122  {
123  // get stopwatch
124  Dune::Timer exchangeT;
125 
126  // PetscDiscreteFunction has it's own communication
127  discreteFunction.communicate();
128 
129  return exchangeT.elapsed();
130  }
131 
133  template < class DiscreteFunction, class Operation >
134  double receive( DiscreteFunction& discreteFunction, const Operation* operation )
135  {
136  // get type of data handle from the discrete function space
137  typedef typename DiscreteFunction
138  :: template CommDataHandle< Operation > :: Type
139  DataHandleType;
140 
141  // on serial runs: do nothing
142  if( space_.gridPart().comm().size() <= 1 )
143  return 0.0;
144 
145  // get stopwatch
146  Dune::Timer exchangeT;
147 
148  // communicate data
149  DataHandleType dataHandle = discreteFunction.dataHandle( operation );
150  space_.gridPart().communicate( dataHandle, interface_ , dir_ );
151 
152  // store time
153  return exchangeT.elapsed();
154  }
155 
157  template < class DiscreteFunction >
158  double receive( DiscreteFunction& discreteFunction )
159  {
160  // get type of default operation
161  typedef typename DiscreteFunction :: DiscreteFunctionSpaceType
162  :: template CommDataHandle< DiscreteFunction > :: OperationType DefaultOperationType;
163  return receive( discreteFunction, (DefaultOperationType *) 0 );
164  }
165 
166  };
167 
168  const SpaceType& space_;
169 
170  const InterfaceType interface_;
171  const CommunicationDirection dir_;
172 
173  mutable double exchangeTime_;
174 
175  public:
177 
180  ( const SpaceType &space,
181  const InterfaceType interface,
182  const CommunicationDirection dir)
183  : space_( space ),
184  interface_( interface ),
185  dir_ ( dir ),
186  exchangeTime_(0.0)
187  {}
188 
189  private:
190  // prohibit copying
192 
193  public:
195  InterfaceType communicationInterface() const {
196  return interface_;
197  }
198 
200  CommunicationDirection communicationDirection() const
201  {
202  return dir_;
203  }
204 
209  double buildTime() const { return 0.0; }
210 
215  double exchangeTime() const { return exchangeTime_; }
216 
221  NonBlockingCommunicationType nonBlockingCommunication() const
222  {
223  return NonBlockingCommunicationType( space_, interface_, dir_ );
224  }
225 
230  template< class DiscreteFunction >
231  inline void exchange ( DiscreteFunction &discreteFunction ) const
232  {
233  // get type of default operation
234  typedef typename DiscreteFunction :: DiscreteFunctionSpaceType ::
235  template CommDataHandle< DiscreteFunction > :: OperationType DefaultOperationType;
236 
237  exchange( discreteFunction, (DefaultOperationType *) 0 );
238  }
239 
248  template< class DiscreteFunction, class Operation >
249  inline void exchange ( DiscreteFunction &discreteFunction,
250  const Operation *operation ) const
251  {
252  // on serial runs: do nothing
253  if( space_.gridPart().comm().size() <= 1 )
254  return;
255 
256  NonBlockingCommunicationType nbc( space_, interface_, dir_ );
257 
258  // send data (probably done in receive)
259  nbc.send( discreteFunction );
260 
261  exchangeTime_ = nbc.receive( discreteFunction, operation );
262  }
263  };
264 
265 
266 
267 #ifndef USE_CACHED_COMM_MANAGER
268  // if no ALUGrid found, supply default implementation
270  template <class SpaceImp>
272  : public DefaultCommunicationManager<SpaceImp>
273  {
276  public:
278  CommunicationManager(const SpaceImp & space,
279  const InterfaceType interface,
280  const CommunicationDirection dir)
281  : BaseType(space,interface,dir)
282  {}
284  CommunicationManager(const SpaceImp & space)
285  : BaseType(space,
286  space.communicationInterface(),
287  space.communicationDirection() )
288  {}
289  };
290 
291 
292 
295  {
297  class DiscreteFunctionCommunicatorInterface
298  {
299  protected:
300  DiscreteFunctionCommunicatorInterface () {}
301  public:
302  virtual ~DiscreteFunctionCommunicatorInterface () {}
303  virtual void exchange () const = 0;
304  virtual bool handles ( IsDiscreteFunction &df ) const = 0;
305  };
306 
308  template <class DiscreteFunctionImp>
309  class DiscreteFunctionCommunicator
310  : public DiscreteFunctionCommunicatorInterface
311  {
312  typedef DiscreteFunctionImp DiscreteFunctionType;
313  typedef typename DiscreteFunctionType :: DiscreteFunctionSpaceType DiscreteFunctionSpaceType;
314 
315  typedef CommunicationManager<DiscreteFunctionSpaceType> CommunicationManagerType;
316 
317  DiscreteFunctionType& df_;
318  CommunicationManagerType comm_;
319  public:
321  DiscreteFunctionCommunicator(DiscreteFunctionType& df)
322  : df_(df), comm_(df_.space())
323  {
324  }
325 
326  // exchange discrete function
327  void exchange () const
328  {
329  comm_.exchange(df_);
330  }
331 
332  bool handles ( IsDiscreteFunction &df ) const { return (&df_ == &df); }
333  };
334 
335  typedef DiscreteFunctionCommunicatorInterface CommObjIFType;
336  typedef std::list < DiscreteFunctionCommunicatorInterface * > CommObjListType;
337  CommObjListType objList_;
338 
340  public:
342  template <class CombinedObjectType>
343  CommunicationManagerList(CombinedObjectType& cObj)
344  {
345  cObj.addToList(*this);
346  }
347 
350  {
351  // delete all entries
352  while( objList_.size() > 0 )
353  {
354  CommObjIFType * obj = objList_.back();
355  objList_.pop_back();
356  delete obj;
357  }
358  }
359 
361  template <class DiscreteFunctionImp>
362  void addToList(DiscreteFunctionImp &df)
363  {
364  typedef DiscreteFunctionCommunicator<DiscreteFunctionImp> CommObjType;
365  CommObjType* obj = new CommObjType(df);
366  objList_.push_back(obj);
367  }
368 
369  template< class DiscreteFunction >
370  void removeFromList ( DiscreteFunction &df )
371  {
372  const auto handles = [ &df ] ( const CommObjIFType *commObj ) { return commObj->handles( df ); };
373  CommObjListType::reverse_iterator pos = std::find_if( objList_.rbegin(), objList_.rend(), handles );
374  if( pos != objList_.rend() )
375  objList_.erase( pos.base() );
376  else
377  DUNE_THROW( RangeError, "Trying to remove discrete function that was never added" );
378  }
379 
382  void exchange() const
383  {
384  typedef CommObjListType :: const_iterator iterator;
385  {
386  iterator end = objList_.end();
387  for(iterator it = objList_.begin(); it != end; ++it)
388  {
389  (*it)->exchange();
390  }
391  }
392  }
393  };
394 
395  // end toggle AULGrid yes/no
396 #endif
397 
398 
399  } // namespace Fem
400 
401 } // namespace Dune
402 
403 #endif // #ifndef DUNE_FEM_COMMUNICATION_MANAGER_HH
void exchange() const
Definition: communicationmanager.hh:382
double receive(DiscreteFunction &discreteFunction)
receive method with default operation
Definition: communicationmanager.hh:158
DefaultCommunicationManager(const SpaceType &space, const InterfaceType interface, const CommunicationDirection dir)
constructor taking space and communication interface/direction
Definition: communicationmanager.hh:180
Proxy class to DependencyCache which is singleton per space.
Definition: communicationmanager.hh:294
double buildTime() const
return time needed for last build
Definition: communicationmanager.hh:209
const InterfaceType interface_
Definition: communicationmanager.hh:170
Definition: discretefunction.hh:50
CommunicationManagerList(CombinedObjectType &cObj)
constructor
Definition: communicationmanager.hh:343
CommunicationManager(const SpaceImp &space)
constructor taking space
Definition: communicationmanager.hh:284
void removeFromList(DiscreteFunction &df)
Definition: communicationmanager.hh:370
void exchange(DiscreteFunction &discreteFunction) const
exchange data for a discrete function using the copy operation
Definition: communicationmanager.hh:231
double exchangeTime() const
return time needed for last exchange of data
Definition: communicationmanager.hh:215
double receive(PetscDiscreteFunction< DiscreteFunctionSpace > &discreteFunction, const Operation *operation)
receive data for discrete function and given operation
Definition: communicationmanager.hh:120
Space SpaceType
Definition: communicationmanager.hh:87
use Default CommunicationManager as Communication Manager
Definition: communicationmanager.hh:271
CommunicationManager(const SpaceImp &space, const InterfaceType interface, const CommunicationDirection dir)
constructor taking space and communication interface/direction
Definition: communicationmanager.hh:278
Definition: coordinate.hh:4
~CommunicationManagerList()
remove object comm
Definition: communicationmanager.hh:349
const SpaceType & space_
Definition: communicationmanager.hh:168
double receive(DiscreteFunction &discreteFunction, const Operation *operation)
receive data for discrete function and given operation
Definition: communicationmanager.hh:134
const CommunicationDirection dir_
Definition: communicationmanager.hh:171
void addToList(DiscreteFunctionImp &df)
add discrete function to communication list
Definition: communicationmanager.hh:362
InterfaceType communicationInterface() const
return communication interface
Definition: communicationmanager.hh:195
NonBlockingCommunication(const SpaceType &space, InterfaceType interface, CommunicationDirection dir)
Definition: communicationmanager.hh:102
default communication manager using just the grids communicate method
Definition: communicationmanager.hh:84
CommunicationDirection communicationDirection() const
return communication direction
Definition: communicationmanager.hh:200
double exchangeTime_
Definition: communicationmanager.hh:173
NonBlockingCommunication NonBlockingCommunicationType
Definition: communicationmanager.hh:176
NonBlockingCommunicationType nonBlockingCommunication() const
return object for non-blocking communication
Definition: communicationmanager.hh:221
void send(const DiscreteFunction &discreteFunction)
send data for given discrete function
Definition: communicationmanager.hh:112
Definition: cachedcommmanager.hh:44
void exchange(DiscreteFunction &discreteFunction, const Operation *operation) const
exchange data for a discrete function using the given operation
Definition: communicationmanager.hh:249
DefaultCommunicationManager< Space > ThisType
Definition: communicationmanager.hh:90