DUNE-FEM (unstable)

communicationmanager.hh
1 #ifndef DUNE_FEM_COMMUNICATION_MANAGER_HH
2 #define DUNE_FEM_COMMUNICATION_MANAGER_HH
3 
4 #include <iostream>
5 #include <map>
6 #include <memory>
7 #include <vector>
8 
9 #include <dune/common/timer.hh>
11 #include <dune/grid/common/grid.hh>
12 
13 #include <dune/fem/misc/mpimanager.hh>
14 #include <dune/fem/space/common/commoperations.hh>
15 #include <dune/fem/storage/singletonlist.hh>
16 
17 // include ALUGrid to check whether the
18 // parallel version is avaiable
19 #if HAVE_DUNE_ALUGRID
20 #include <dune/alugrid/3d/alugrid.hh>
21 #endif
22 
23 // default is: enabled
24 #ifndef WANT_CACHED_COMM_MANAGER
25 #define WANT_CACHED_COMM_MANAGER 1
26 #endif
27 
28 #if ALU3DGRID_PARALLEL && WANT_CACHED_COMM_MANAGER
29 #define USE_CACHED_COMM_MANAGER
30 #else
31 #ifndef NDEBUG
32 #if HAVE_MPI == 0
33  #ifdef DUNE_DEVEL_MODE
34  #warning "HAVE_MPI == 0, therefore default CommunicationManager is used!"
35  #endif
36 #elif !ALU3DGRID_PARALLEL
37  #warning "No Parallel ALUGrid found, using default CommunicationManager!"
38 #elif ! WANT_CACHED_COMM_MANAGER
39  #warning "CachedCommunication Manager disabled by WANT_CACHED_COMM_MANAGER=0!"
40 #endif
41 #endif
42 #endif
43 
44 #undef WANT_CACHED_COMM_MANAGER
45 
46 #ifdef USE_CACHED_COMM_MANAGER
47 #include "cachedcommmanager.hh"
48 #endif
49 
50 namespace Dune
51 {
52 
53  namespace Fem
54  {
55 
56  // External Forward Declarations
57  // -----------------------------
58 
59  template< class DiscreteFunctionSpace >
60  class PetscDiscreteFunction;
61 
62  class IsDiscreteFunction;
63 
64  class IsBlockVector;
65 
66 
76  template< class Space >
78  {
79  public:
80  typedef Space SpaceType;
81 
82  protected:
84 
86  // begin NonBlockingCommunication
88  class NonBlockingCommunication
89  {
90  const SpaceType& space_;
91  const InterfaceType interface_;
92  const CommunicationDirection dir_;
93 
94  public:
95  NonBlockingCommunication( const SpaceType& space,
96  InterfaceType interface,
98  : space_( space ),
99  interface_( interface ),
100  dir_ ( dir )
101  {}
102 
104  template < class DiscreteFunction >
105  void send( const DiscreteFunction& discreteFunction )
106  {
107  // nothing to do here, since DUNE does not support
108  // non-blocking communication yet
109  }
110 
112  template < class DiscreteFunctionSpace, class Operation >
113  double receive( PetscDiscreteFunction< DiscreteFunctionSpace > & discreteFunction,
114  const Operation& operation )
115  {
116  // on serial runs: do nothing
117  if( space_.gridPart().comm().size() <= 1 )
118  return 0.0;
119 
120  // get stopwatch
121  Dune::Timer exchangeT;
122 
123  // PetscDiscreteFunction has it's own communication
124  discreteFunction.dofVector().communicateNow( operation );
125 
126  return exchangeT.elapsed();
127  }
128 
130  template < class DiscreteFunction, class Operation >
131  double receive( DiscreteFunction& discreteFunction, const Operation& operation )
132  {
133  // on serial runs: do nothing
134  if( space_.gridPart().comm().size() <= 1 )
135  return 0.0;
136 
137  // get type of data handle from the discrete function space
138  typedef typename DiscreteFunction
139  :: template CommDataHandle< Operation > :: Type
140  DataHandleType;
141 
142  // get stopwatch
143  Dune::Timer exchangeT;
144 
145  // communicate data
146  DataHandleType dataHandle = discreteFunction.dataHandle( operation );
147  space_.gridPart().communicate( dataHandle, interface_ , dir_ );
148 
149  // store time
150  return exchangeT.elapsed();
151  }
152 
154  template < class DiscreteFunction >
155  double receive( DiscreteFunction& discreteFunction )
156  {
157  // get type of default operation
158  typedef typename DiscreteFunction :: DiscreteFunctionSpaceType
159  :: template CommDataHandle< DiscreteFunction > :: OperationType DefaultOperationType;
160  DefaultOperationType operation;
161  return receive( discreteFunction, operation );
162  }
163 
164  };
165 
166  const SpaceType& space_;
167 
168  const InterfaceType interface_;
169  const CommunicationDirection dir_;
170 
171  mutable double exchangeTime_;
172 
173  public:
174  typedef NonBlockingCommunication NonBlockingCommunicationType;
175 
178  ( const SpaceType &space,
179  const InterfaceType interface,
180  const CommunicationDirection dir)
181  : space_( space ),
182  interface_( interface ),
183  dir_ ( dir ),
184  exchangeTime_(0.0)
185  {}
186 
188 
191  return interface_;
192  }
193 
196  {
197  return dir_;
198  }
199 
204  double buildTime() const { return 0.0; }
205 
210  double exchangeTime() const { return exchangeTime_; }
211 
216  NonBlockingCommunicationType nonBlockingCommunication() const
217  {
218  return NonBlockingCommunicationType( space_, interface_, dir_ );
219  }
220 
225  template< class DiscreteFunction >
226  inline void exchange ( DiscreteFunction &discreteFunction ) const
227  {
228  // get type of default operation
229  typedef typename DiscreteFunction :: DiscreteFunctionSpaceType ::
230  template CommDataHandle< DiscreteFunction > :: OperationType DefaultOperationType;
231 
232  DefaultOperationType operation;
233  exchange( discreteFunction, operation );
234  }
235 
244  template< class DiscreteFunction, class Operation >
245  inline void exchange ( DiscreteFunction &discreteFunction,
246  const Operation &operation ) const
247  {
248  // on serial runs: do nothing
249  if( space_.gridPart().comm().size() <= 1 )
250  return;
251 
252  NonBlockingCommunicationType nbc( space_, interface_, dir_ );
253 
254  // send data (probably done in receive)
255  nbc.send( discreteFunction );
256 
257  exchangeTime_ = nbc.receive( discreteFunction, operation );
258  }
259 
268  template< class DiscreteFunction, class Operation >
269  inline void exchange ( const Space& space,
270  DiscreteFunction &discreteFunction,
271  const Operation &operation ) const
272  {
273 #ifndef USE_CACHED_COMM_MANAGER
274  DUNE_THROW(NotImplemented,"Communicating BlockVectorInterface and derived only works with cached communication!");
275 #endif
276  }
277  };
278 
279 
280 
281 #ifndef USE_CACHED_COMM_MANAGER
282  // if no ALUGrid found, supply default implementation
284  template <class SpaceImp>
286  : public DefaultCommunicationManager<SpaceImp>
287  {
290  public:
292  CommunicationManager(const SpaceImp & space,
293  const InterfaceType interface,
294  const CommunicationDirection dir)
295  : BaseType(space,interface,dir)
296  {}
298  CommunicationManager(const SpaceImp & space)
299  : BaseType(space,
300  space.communicationInterface(),
301  space.communicationDirection() )
302  {}
303  };
304 
305 
306 
309  {
311  class DiscreteFunctionCommunicatorInterface
312  {
313  protected:
314  DiscreteFunctionCommunicatorInterface () = default;
315  public:
316  virtual ~DiscreteFunctionCommunicatorInterface () = default;
317  virtual void exchange () const = 0;
318  virtual bool handles ( IsDiscreteFunction &df ) const = 0;
319  };
320 
322  template <class DiscreteFunctionImp, class Operation>
323  class DiscreteFunctionCommunicator
324  : public DiscreteFunctionCommunicatorInterface
325  {
326  typedef DiscreteFunctionImp DiscreteFunctionType;
327  typedef typename DiscreteFunctionType :: DiscreteFunctionSpaceType DiscreteFunctionSpaceType;
328 
329  typedef CommunicationManager<DiscreteFunctionSpaceType> CommunicationManagerType;
330 
332  CommunicationManagerType comm_;
333  const Operation& operation_;
334 
335  public:
337  DiscreteFunctionCommunicator(DiscreteFunctionType& df, const Operation& op)
338  : df_(df), comm_(df_.space()), operation_( op )
339  {
340  }
341 
342  // exchange discrete function
343  void exchange () const
344  {
345  comm_.exchange( df_, operation_ );
346  }
347 
348  bool handles ( IsDiscreteFunction &df ) const { return (&df_ == &df); }
349  };
350 
351  typedef DiscreteFunctionCommunicatorInterface CommObjIFType;
352  typedef std::list < std::unique_ptr< DiscreteFunctionCommunicatorInterface > > CommObjListType;
353  CommObjListType objList_;
354 
355  CommunicationManagerList(const CommunicationManagerList&);
356  public:
357  CommunicationManagerList () = default;
358 
360  template <class CombinedObjectType>
361  CommunicationManagerList(CombinedObjectType& cObj)
362  {
363  cObj.addToList(*this);
364  }
365 
367  template <class DiscreteFunctionImp, class Operation>
368  void addToList(DiscreteFunctionImp &df, const Operation& operation )
369  {
370  typedef DiscreteFunctionCommunicator<DiscreteFunctionImp, Operation> CommObjType;
371  CommObjType* obj = new CommObjType( df, operation );
372  objList_.push_back( std::unique_ptr< DiscreteFunctionCommunicatorInterface> (obj) );
373  }
374 
376  template <class DiscreteFunctionImp>
377  void addToList(DiscreteFunctionImp &df)
378  {
380  addToList( df, operation );
381  }
382 
383  template< class DiscreteFunction >
384  void removeFromList ( DiscreteFunction &df )
385  {
386  const auto handles = [ &df ] ( const std::unique_ptr< DiscreteFunctionCommunicatorInterface > &commObj ) { return commObj->handles( df ); };
387  CommObjListType::reverse_iterator pos = std::find_if( objList_.rbegin(), objList_.rend(), handles );
388  if( pos != objList_.rend() )
389  objList_.erase( --pos.base() );
390  else
391  DUNE_THROW( RangeError, "Trying to remove discrete function that was never added" );
392  }
393 
396  void exchange() const
397  {
398  typedef CommObjListType :: const_iterator iterator;
399  {
400  iterator end = objList_.end();
401  for(iterator it = objList_.begin(); it != end; ++it)
402  {
403  (*it)->exchange();
404  }
405  }
406  }
407  };
408 
409  // end toggle AULGrid yes/no
410 #endif
412 
413  } // namespace Fem
414 
415 } // namespace Dune
416 
417 #endif // #ifndef DUNE_FEM_COMMUNICATION_MANAGER_HH
Proxy class to DependencyCache which is singleton per space.
Definition: communicationmanager.hh:309
use Default CommunicationManager as Communication Manager
Definition: communicationmanager.hh:287
default communication manager using just the grids communicate method
Definition: communicationmanager.hh:78
A vector valued function space.
Definition: functionspace.hh:60
base class for determing whether a class is a discrete function or not
Definition: discretefunction.hh:53
forward declaration
Definition: discretefunction.hh:51
TupleDiscreteFunctionSpace< typename DiscreteFunctions::DiscreteFunctionSpaceType ... > DiscreteFunctionSpaceType
type for the discrete function space this function lives in
Definition: discretefunction.hh:69
Default exception for dummy implementations.
Definition: exceptions.hh:263
Default exception class for range errors.
Definition: exceptions.hh:254
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
Different resources needed by all grid implementations.
Describes the parallel communication interface class for MessageBuffers and DataHandles.
double buildTime() const
return time needed for last build
Definition: communicationmanager.hh:204
InterfaceType communicationInterface() const
return communication interface
Definition: communicationmanager.hh:190
double receive(DiscreteFunction &discreteFunction, const Operation &operation)
receive data for discrete function and given operation
Definition: communicationmanager.hh:131
DefaultCommunicationManager(const SpaceType &space, const InterfaceType interface, const CommunicationDirection dir)
constructor taking space and communication interface/direction
Definition: communicationmanager.hh:178
CommunicationManager(const SpaceImp &space)
constructor taking space
Definition: communicationmanager.hh:298
void exchange() const
Definition: communicationmanager.hh:396
void exchange(DiscreteFunction &discreteFunction) const
exchange data for a discrete function using the copy operation
Definition: communicationmanager.hh:226
CommunicationDirection communicationDirection() const
return communication direction
Definition: communicationmanager.hh:195
CommunicationManagerList(CombinedObjectType &cObj)
constructor
Definition: communicationmanager.hh:361
NonBlockingCommunicationType nonBlockingCommunication() const
return object for non-blocking communication
Definition: communicationmanager.hh:216
void send(const DiscreteFunction &discreteFunction)
send data for given discrete function
Definition: communicationmanager.hh:105
void exchange(DiscreteFunction &discreteFunction, const Operation &operation) const
exchange data for a discrete function using the given operation
Definition: communicationmanager.hh:245
void exchange(const Space &space, DiscreteFunction &discreteFunction, const Operation &operation) const
exchange data for a discrete function using the given operation
Definition: communicationmanager.hh:269
DiscreteFunctionCommunicator(DiscreteFunctionType &df, const Operation &op)
constructor taking disctete function
Definition: communicationmanager.hh:337
double exchangeTime() const
return time needed for last exchange of data
Definition: communicationmanager.hh:210
double receive(DiscreteFunction &discreteFunction)
receive method with default operation
Definition: communicationmanager.hh:155
CommunicationManager(const SpaceImp &space, const InterfaceType interface, const CommunicationDirection dir)
constructor taking space and communication interface/direction
Definition: communicationmanager.hh:292
void addToList(DiscreteFunctionImp &df, const Operation &operation)
add discrete function to communication list
Definition: communicationmanager.hh:368
void addToList(DiscreteFunctionImp &df)
add discrete function to communication list
Definition: communicationmanager.hh:377
double receive(PetscDiscreteFunction< DiscreteFunctionSpace > &discreteFunction, const Operation &operation)
receive data for discrete function and given operation
Definition: communicationmanager.hh:113
#define DUNE_THROW(E, m)
Definition: exceptions.hh:218
CommunicationDirection
Define a type for communication direction parameter.
Definition: gridenums.hh:170
InterfaceType
Parameter to be used for the communication functions.
Definition: gridenums.hh:86
Dune namespace.
Definition: alignedallocator.hh:13
just copy data
Definition: commoperations.hh:127
A simple timing class.
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden  |  generated with Hugo v0.80.0 (May 16, 22:29, 2024)