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
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
50namespace 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 // for Petsc this is needed also in serial
117 // since it completes the vector setup
118
119 // get stopwatch
120 Dune::Timer exchangeT;
121
122 // PetscDiscreteFunction has it's own communication
123 discreteFunction.dofVector().communicateNow( operation );
124
125 return exchangeT.elapsed();
126 }
127
129 template < class DiscreteFunction, class Operation >
130 double receive( DiscreteFunction& discreteFunction, const Operation& operation )
131 {
132 // on serial runs: do nothing
133 if( space_.gridPart().comm().size() <= 1 )
134 return 0.0;
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 // get stopwatch
142 Dune::Timer exchangeT;
143
144 // communicate data
145 DataHandleType dataHandle = discreteFunction.dataHandle( operation );
146 space_.gridPart().communicate( dataHandle, interface_ , dir_ );
147
148 // store time
149 return exchangeT.elapsed();
150 }
151
153 template < class DiscreteFunction >
154 double receive( DiscreteFunction& discreteFunction )
155 {
156 // get type of default operation
157 typedef typename DiscreteFunction :: DiscreteFunctionSpaceType
158 :: template CommDataHandle< DiscreteFunction > :: OperationType DefaultOperationType;
159 DefaultOperationType operation;
160 return receive( discreteFunction, operation );
161 }
162
163 };
164
165 const SpaceType& space_;
166
167 const InterfaceType interface_;
168 const CommunicationDirection dir_;
169
170 mutable double exchangeTime_;
171
172 public:
173 typedef NonBlockingCommunication NonBlockingCommunicationType;
174
177 ( const SpaceType &space,
178 const InterfaceType interface,
179 const CommunicationDirection dir)
180 : space_( space ),
181 interface_( interface ),
182 dir_ ( dir ),
183 exchangeTime_(0.0)
184 {}
185
187
190 return interface_;
191 }
192
195 {
196 return dir_;
197 }
198
203 double buildTime() const { return 0.0; }
204
209 double exchangeTime() const { return exchangeTime_; }
210
215 NonBlockingCommunicationType nonBlockingCommunication() const
216 {
217 return NonBlockingCommunicationType( space_, interface_, dir_ );
218 }
219
224 template< class DiscreteFunction >
225 inline void exchange ( DiscreteFunction &discreteFunction ) const
226 {
227 // get type of default operation
228 typedef typename DiscreteFunction :: DiscreteFunctionSpaceType ::
229 template CommDataHandle< DiscreteFunction > :: OperationType DefaultOperationType;
230
231 DefaultOperationType operation;
232 exchange( discreteFunction, operation );
233 }
234
243 template< class DiscreteFunction, class Operation >
244 inline void exchange ( DiscreteFunction &discreteFunction,
245 const Operation &operation ) const
246 {
247 NonBlockingCommunicationType nbc( space_, interface_, dir_ );
248
249 // send data (probably done in receive)
250 nbc.send( discreteFunction );
251
252 exchangeTime_ = nbc.receive( discreteFunction, operation );
253 }
254
263 template< class DiscreteFunction, class Operation >
264 inline void exchange ( const Space& space,
265 DiscreteFunction &discreteFunction,
266 const Operation &operation ) const
267 {
268#ifndef USE_CACHED_COMM_MANAGER
269 DUNE_THROW(NotImplemented,"Communicating BlockVectorInterface and derived only works with cached communication!");
270#endif
271 }
272 };
273
274
275
276#ifndef USE_CACHED_COMM_MANAGER
277 // if no ALUGrid found, supply default implementation
279 template <class SpaceImp>
281 : public DefaultCommunicationManager<SpaceImp>
282 {
285 public:
287 CommunicationManager(const SpaceImp & space,
288 const InterfaceType interface,
289 const CommunicationDirection dir)
290 : BaseType(space,interface,dir)
291 {}
293 CommunicationManager(const SpaceImp & space)
294 : BaseType(space,
296 space.communicationDirection() )
297 {}
298 };
299
300
301
304 {
306 class DiscreteFunctionCommunicatorInterface
307 {
308 protected:
309 DiscreteFunctionCommunicatorInterface () = default;
310 public:
311 virtual ~DiscreteFunctionCommunicatorInterface () = default;
312 virtual void exchange () const = 0;
313 virtual bool handles ( IsDiscreteFunction &df ) const = 0;
314 };
315
317 template <class DiscreteFunctionImp, class Operation>
318 class DiscreteFunctionCommunicator
319 : public DiscreteFunctionCommunicatorInterface
320 {
321 typedef DiscreteFunctionImp DiscreteFunctionType;
322 typedef typename DiscreteFunctionType :: DiscreteFunctionSpaceType DiscreteFunctionSpaceType;
323
324 typedef CommunicationManager<DiscreteFunctionSpaceType> CommunicationManagerType;
325
327 CommunicationManagerType comm_;
328 const Operation& operation_;
329
330 public:
332 DiscreteFunctionCommunicator(DiscreteFunctionType& df, const Operation& op)
333 : df_(df), comm_(df_.space()), operation_( op )
334 {
335 }
336
337 // exchange discrete function
338 void exchange () const
339 {
340 comm_.exchange( df_, operation_ );
341 }
342
343 bool handles ( IsDiscreteFunction &df ) const { return (&df_ == &df); }
344 };
345
346 typedef DiscreteFunctionCommunicatorInterface CommObjIFType;
347 typedef std::list < std::unique_ptr< DiscreteFunctionCommunicatorInterface > > CommObjListType;
348 CommObjListType objList_;
349
350 CommunicationManagerList(const CommunicationManagerList&);
351 public:
352 CommunicationManagerList () = default;
353
355 template <class CombinedObjectType>
356 CommunicationManagerList(CombinedObjectType& cObj)
357 {
358 cObj.addToList(*this);
359 }
360
362 template <class DiscreteFunctionImp, class Operation>
363 void addToList(DiscreteFunctionImp &df, const Operation& operation )
364 {
365 typedef DiscreteFunctionCommunicator<DiscreteFunctionImp, Operation> CommObjType;
366 CommObjType* obj = new CommObjType( df, operation );
367 objList_.push_back( std::unique_ptr< DiscreteFunctionCommunicatorInterface> (obj) );
368 }
369
371 template <class DiscreteFunctionImp>
372 void addToList(DiscreteFunctionImp &df)
373 {
375 addToList( df, operation );
376 }
377
378 template< class DiscreteFunction >
379 void removeFromList ( DiscreteFunction &df )
380 {
381 const auto handles = [ &df ] ( const std::unique_ptr< DiscreteFunctionCommunicatorInterface > &commObj ) { return commObj->handles( df ); };
382 CommObjListType::reverse_iterator pos = std::find_if( objList_.rbegin(), objList_.rend(), handles );
383 if( pos != objList_.rend() )
384 objList_.erase( --pos.base() );
385 else
386 DUNE_THROW( RangeError, "Trying to remove discrete function that was never added" );
387 }
388
391 void exchange() const
392 {
393 typedef CommObjListType :: const_iterator iterator;
394 {
395 iterator end = objList_.end();
396 for(iterator it = objList_.begin(); it != end; ++it)
397 {
398 (*it)->exchange();
399 }
400 }
401 }
402 };
403
404 // end toggle AULGrid yes/no
405#endif
407
408 } // namespace Fem
409
410} // namespace Dune
411
412#endif // #ifndef DUNE_FEM_COMMUNICATION_MANAGER_HH
Proxy class to DependencyCache which is singleton per space.
Definition: communicationmanager.hh:304
use Default CommunicationManager as Communication Manager
Definition: communicationmanager.hh:282
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:203
InterfaceType communicationInterface() const
return communication interface
Definition: communicationmanager.hh:189
double receive(DiscreteFunction &discreteFunction, const Operation &operation)
receive data for discrete function and given operation
Definition: communicationmanager.hh:130
DefaultCommunicationManager(const SpaceType &space, const InterfaceType interface, const CommunicationDirection dir)
constructor taking space and communication interface/direction
Definition: communicationmanager.hh:177
CommunicationManager(const SpaceImp &space)
constructor taking space
Definition: communicationmanager.hh:293
void exchange() const
Definition: communicationmanager.hh:391
void exchange(DiscreteFunction &discreteFunction) const
exchange data for a discrete function using the copy operation
Definition: communicationmanager.hh:225
CommunicationDirection communicationDirection() const
return communication direction
Definition: communicationmanager.hh:194
CommunicationManagerList(CombinedObjectType &cObj)
constructor
Definition: communicationmanager.hh:356
NonBlockingCommunicationType nonBlockingCommunication() const
return object for non-blocking communication
Definition: communicationmanager.hh:215
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:244
void exchange(const Space &space, DiscreteFunction &discreteFunction, const Operation &operation) const
exchange data for a discrete function using the given operation
Definition: communicationmanager.hh:264
DiscreteFunctionCommunicator(DiscreteFunctionType &df, const Operation &op)
constructor taking disctete function
Definition: communicationmanager.hh:332
double exchangeTime() const
return time needed for last exchange of data
Definition: communicationmanager.hh:209
double receive(DiscreteFunction &discreteFunction)
receive method with default operation
Definition: communicationmanager.hh:154
CommunicationManager(const SpaceImp &space, const InterfaceType interface, const CommunicationDirection dir)
constructor taking space and communication interface/direction
Definition: communicationmanager.hh:287
void addToList(DiscreteFunctionImp &df, const Operation &operation)
add discrete function to communication list
Definition: communicationmanager.hh:363
void addToList(DiscreteFunctionImp &df)
add discrete function to communication list
Definition: communicationmanager.hh:372
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.111.3 (Jul 24, 22:29, 2024)