dune-fem  2.4.1-rc
common/pass.hh
Go to the documentation of this file.
1 #ifndef DUNE_FEM_PASS_COMMON_PASS_HH
2 #define DUNE_FEM_PASS_COMMON_PASS_HH
3 
4 #include <limits>
5 #include <string>
6 #include <tuple>
7 #include <type_traits>
8 
9 #include <dune/common/deprecated.hh>
10 #include <dune/common/timer.hh>
11 #include <dune/common/tuples.hh>
12 
15 
16 namespace Dune
17 {
18 
19  namespace Fem
20  {
21 
22  // empty non-blocking communication for start pass as default argument
24  {
25  // initialize communication
26  template <class Destination>
27  void initComm( const Destination& ) const {}
28 
29  // receive data of previously initialized communication
30  template <class Destination>
31  void receiveComm( const Destination& ) const {}
32 
33  // cleanup overwritten data, i.e. ghost values
34  template <class Destination>
35  void finalizeComm( const Destination& ) const {}
36  };
37 
45  template < class ArgumentImp , int passIdImp,
46  class NonBlockingCommunication = EmptyNonBlockingComm >
47  class StartPass
48  {
49  NonBlockingCommunication nonBlockingComm_;
50  public:
52  static const int passNum = 0;
53  static const int passId = passIdImp;
54 
56  typedef std::tuple< std::integral_constant< int, passIdImp > > PassIds;
57 
59  typedef ArgumentImp GlobalArgumentType;
61  typedef std::tuple<> NextArgumentType;
62 
63  public:
65  StartPass() : nonBlockingComm_() {}
67  StartPass(const StartPass&) : nonBlockingComm_() {}
68 
69  //- Public methods
71  void pass( const GlobalArgumentType& arg ) const
72  {
73  nonBlockingComm_.initComm( arg );
74  }
75 
77  void receiveCommunication( const GlobalArgumentType& arg ) const
78  {
79  nonBlockingComm_.receiveComm( arg );
80  }
81 
83  void finalizeCommunication( const GlobalArgumentType& arg ) const
84  {
85  nonBlockingComm_.finalizeComm( arg );
86  }
87 
88  // dummy printTexInfo method
89  void printTexInfo(std::ostream& out) const DUNE_DEPRECATED {}
90 
92  NextArgumentType localArgument() const { return NextArgumentType(); }
93 
96 
98  void setTime(const double) {}
99 
101  double timeStepEstimate() const
102  {
104  }
105  };
106 
116  template <class DiscreteModelImp, class PreviousPassImp , int passIdImp>
117  class Pass :
118  public Operator<typename PreviousPassImp::GlobalArgumentType,
119  typename DiscreteModelImp::Traits::DestinationType>
120  {
121  template <class PT, class PP, int PI>
122  friend class Pass;
123  public:
126  template <class ObjectToDelete>
128  {
129  protected:
130  // don't create intances of this class
132  public:
134  virtual ~DeleteHandler () {}
136  virtual void freeLocalMemory(ObjectToDelete * obj)
137  {
138  delete obj;
139  }
140 
143  {
145  return mh;
146  }
147  };
148 
150  typedef PreviousPassImp PreviousPassType;
151 
153  static const int passNum = PreviousPassType::passNum + 1;
154  static const int passId = passIdImp;
155 
157  typedef typename Dune::PushBackTuple< typename PreviousPassType::PassIds, std::integral_constant< int, passIdImp > >::type PassIds;
158 
161  typedef typename DiscreteModelImp::Traits::DestinationType DestinationType;
162 
164  typedef DeleteHandler<DestinationType> DeleteHandlerType;
165 
166  typedef typename DestinationType :: DiscreteFunctionSpaceType :: CommunicationManagerType
167  :: NonBlockingCommunicationType NonBlockingCommunicationType;
168 
171  typedef typename PreviousPassType::GlobalArgumentType GlobalArgumentType;
173  typedef typename PreviousPassType::NextArgumentType LocalArgumentType;
177  typedef typename PushFrontTuple< LocalArgumentType, const GlobalArgumentType* >::type TotalArgumentType;
179  typedef typename PushBackTuple< LocalArgumentType, DestinationType* >::type NextArgumentType;
180 
181  public:
182  // return pass number
183  int passNumber() const { return passNum; }
184 
185  //- Public methods
188  Pass(PreviousPassType& pass) :
189  destination_(0),
190  deleteHandler_(0),
191  previousPass_(pass),
192  time_(0.0),
193  finalizeCommunication_( true )
194  {
195  // this ensures that the last pass doesn't allocate temporary memory
196  // (its destination discrete function is provided from outside).
197  previousPass_.allocateLocalMemory();
198  }
199 
201  virtual ~Pass()
202  {
203  // if deleteHandler was set by derived class,
204  // then use to delete destination_
205  if( deleteHandler_ ) deleteHandler_->freeLocalMemory(destination_);
206  destination_ = 0;
207  }
208 
210  void printTexInfo(std::ostream& out) const
211  {
212  previousPass_.printTexInfo(out);
213  }
214 
218  void operator()(const GlobalArgumentType& arg, DestinationType& dest) const
219  {
220  previousPass_.pass(arg);
221  LocalArgumentType prevArg = previousPass_.localArgument();
222  const TotalArgumentType totalArg = tuple_push_front( prevArg, &arg );
223  this->compute(totalArg, dest);
224 
225  // if initComm has not been called for this pass, we have to
226  // call finalizeCommunication for all previous passes
227  if( finalizeCommunication_ )
228  finalizeCommunication( arg );
229  }
233  virtual void allocateLocalMemory() = 0;
234 
236  void setTime(const double t)
237  {
238  previousPass_.setTime(t);
239  time_ = t;
240  }
241 
246  double timeStepEstimate() const
247  {
248  double ret= std::min( previousPass_.timeStepEstimate(),
249  this->timeStepEstimateImpl() );
250  return ret;
251  }
252 
254  double time() const { return time_; }
255 
257  const DestinationType & destination () const
258  {
259  assert(destination_);
260  return *destination_;
261  }
262 
263  public:
267  void pass(const GlobalArgumentType& arg) const
268  {
269  // send my destination data needed by the next pass
270  initComm();
271  // since initComm was called we are not the last pass
272  // and thus must not call finalizeCommunication
273  finalizeCommunication_ = false ;
274  operator()(arg, *destination_);
275  }
276 
278  NextArgumentType localArgument () const
279  {
280  typename PreviousPassType::NextArgumentType nextArg( previousPass_.localArgument() );
281  return tuple_push_back( nextArg, destination_ );
282  }
283 
288  void finalizeCommunication(const GlobalArgumentType& arg) const
289  {
290  // we only need the first argument
291  // the other argument are known to the previous passes
292  previousPass_.finalizeCommunication( arg );
293 
294  // this method has to be overloaded in the pass implementation
295  finalizeComm();
296  // reset finalizeCommunication flag
297  finalizeCommunication_ = true;
298  }
299 
304  void receiveCommunication(const GlobalArgumentType& arg) const
305  {
306  // we only need the first argument
307  // the other argument are known to the previous passes
308  previousPass_.receiveCommunication( arg );
309 
310  // this method has to be overloaded in the pass implementation
311  receiveComm();
312  }
313 
316  virtual double timeStepEstimateImpl() const
317  {
319  }
320 
324  virtual bool requireCommunication () const { return true; }
325 
326  protected:
329  virtual void compute(const TotalArgumentType& arg,
330  DestinationType& dest) const = 0;
331 
335  void finalizeCommunication( const TotalArgumentType& totalArg ) const
336  {
337  // this method is called on the last pass which needs no
338  // finalizing of communication, so call the correct method
339  // on the previous pass
340  // get<0> ( totalArg ) is the global argument type
341  previousPass_.finalizeCommunication( *(get<0> ( totalArg )) );
342  }
343 
347  void receiveCommunication( const TotalArgumentType& totalArg ) const
348  {
349  // this method is called on the last pass which needs no
350  // finalizing of communication, so call the correct method
351  // on the previous pass
352  // get<0> ( totalArg ) is the global argument type
353  previousPass_.receiveCommunication( *(get<0> ( totalArg )) );
354  }
355 
360  virtual void initComm() const {}
361 
366  virtual void finalizeComm() const {}
367 
373  virtual void receiveComm() const {}
374 
375  protected:
377  DestinationType* destination_;
378 
380  DeleteHandlerType* deleteHandler_;
381 
382  // previous pass
383  PreviousPassType& previousPass_;
384 
385  // current calculation time
386  double time_;
387 
388  // flag whether we are the last pass, i.e. we have to finalize the communication
389  mutable bool finalizeCommunication_ ;
390  }; // end class Pass
391 
392  } // namespace Fem
393 
394 } // namespace Dune
395 
396 #endif // #ifndef DUNE_FEM_PASS_COMMON_PASS_HH
double time_
Definition: common/pass.hh:386
DeleteHandlerType * deleteHandler_
object to delete destination_
Definition: common/pass.hh:380
StartPass(const StartPass &)
copy constructor
Definition: common/pass.hh:67
Pass(PreviousPassType &pass)
Definition: common/pass.hh:188
std::tuple< Args..., T > tuple_push_back(const std::tuple< Args... > &tup, T t)
Definition: tupleutility.hh:99
void receiveCommunication(const GlobalArgumentType &arg) const
receive data for previously initialized communication
Definition: common/pass.hh:77
static constexpr T min(T a)
Definition: utility.hh:81
void receiveCommunication(const TotalArgumentType &totalArg) const
receiveCommunication collects possbily initiated non-blocking communications for all passes ...
Definition: common/pass.hh:347
PreviousPassType::GlobalArgumentType GlobalArgumentType
Definition: common/pass.hh:171
void setTime(const double)
StartPass does not need a time.
Definition: common/pass.hh:98
std::tuple NextArgumentType
End marker for tuple of return types of the passes.
Definition: common/pass.hh:61
static constexpr T max(T a)
Definition: utility.hh:65
Definition: common/pass.hh:23
virtual void finalizeComm() const
finalizeCommunication of this pass, this will collect the communication of destination_ and has to be...
Definition: common/pass.hh:366
double timeStepEstimate() const
return time step estimate for explicit Runge Kutta solver, calls recursively the method timeStepEstim...
Definition: common/pass.hh:246
static DeleteHandler< ObjectToDelete > & instance()
return reference to default object deleter
Definition: common/pass.hh:142
double timeStepEstimate() const
return time step estimate
Definition: common/pass.hh:101
void pass(const GlobalArgumentType &arg) const
The pass method initialized the communication only.
Definition: common/pass.hh:71
PushBackTuple< LocalArgumentType, DestinationType * >::type NextArgumentType
Tuple containing destination types of all passes up to this one.
Definition: common/pass.hh:179
PreviousPassType & previousPass_
Definition: common/pass.hh:383
void printTexInfo(std::ostream &out) const
printTex info of operator
Definition: common/pass.hh:210
void printTexInfo(std::ostream &out) const
Definition: common/pass.hh:89
virtual void receiveComm() const
receiveCommunication of this pass, which will reset changes the communication did to the destination_...
Definition: common/pass.hh:373
ArgumentImp GlobalArgumentType
The argument (and destination) type of the overall operator.
Definition: common/pass.hh:59
std::tuple< T, Args... > tuple_push_front(const std::tuple< Args... > &tup, T t)
Definition: tupleutility.hh:110
void finalizeCommunication(const GlobalArgumentType &arg) const
cleanup of overwritten data. i.e. ghost values if neccessary
Definition: common/pass.hh:83
abstract operator
Definition: operator.hh:25
virtual ~Pass()
Destructor.
Definition: common/pass.hh:201
int passNumber() const
Definition: common/pass.hh:183
void pass(const GlobalArgumentType &arg) const
Definition: common/pass.hh:267
PreviousPassImp PreviousPassType
Type of the preceding pass.
Definition: common/pass.hh:150
Definition: coordinate.hh:4
void receiveCommunication(const GlobalArgumentType &arg) const
finalizeCommunication collects possbily initiated non-blocking communications for all passes includin...
Definition: common/pass.hh:304
void initComm(const Destination &) const
Definition: common/pass.hh:27
Definition: common/pass.hh:127
double time() const
return current time of calculation
Definition: common/pass.hh:254
PreviousPassType::NextArgumentType LocalArgumentType
Tuple containing destination types of all preceding passes.
Definition: common/pass.hh:173
NextArgumentType localArgument() const
Returns a compilation of the results of the preceding passes.
Definition: common/pass.hh:278
void finalizeCommunication(const TotalArgumentType &totalArg) const
finalizeCommunication collects possbily initiated non-blocking communications for all passes ...
Definition: common/pass.hh:335
void receiveComm(const Destination &) const
Definition: common/pass.hh:31
const DestinationType & destination() const
return reference to internal discrete function
Definition: common/pass.hh:257
virtual void freeLocalMemory(ObjectToDelete *obj)
default implementation just deletes obj
Definition: common/pass.hh:136
virtual void initComm() const
initializeCommunication of this pass, this will initialize the communication of destination_ and has ...
Definition: common/pass.hh:360
void operator()(const GlobalArgumentType &arg, DestinationType &dest) const
Application operator. The application operator is called by the client directly. It makes only sense ...
Definition: common/pass.hh:218
void finalizeComm(const Destination &) const
Definition: common/pass.hh:35
Base class for specific pass implementations.
Definition: local.hh:19
DeleteHandler()
Definition: common/pass.hh:131
StartPass()
empty constructor
Definition: common/pass.hh:65
DiscreteModelImp::Traits::DestinationType DestinationType
Definition: common/pass.hh:161
virtual double timeStepEstimateImpl() const
Definition: common/pass.hh:316
virtual bool requireCommunication() const
requireCommunication returns true if the pass needs communication at all
Definition: common/pass.hh:324
NextArgumentType localArgument() const
Returns the closure of the destination tuple.
Definition: common/pass.hh:92
PushFrontTuple< LocalArgumentType, const GlobalArgumentType * >::type TotalArgumentType
Definition: common/pass.hh:177
virtual ~DeleteHandler()
destructor
Definition: common/pass.hh:134
void finalizeCommunication(const GlobalArgumentType &arg) const
finalizeCommunication collects possbily initiated non-blocking communications for all passes includin...
Definition: common/pass.hh:288
DeleteHandler< DestinationType > DeleteHandlerType
type of mem handler, which deletes destination
Definition: common/pass.hh:164
End marker for a compile-time list of passes.
Definition: common/pass.hh:47
std::tuple< std::integral_constant< int, passIdImp > > PassIds
pass ids up to here (tuple of integral constants)
Definition: common/pass.hh:56
bool finalizeCommunication_
Definition: common/pass.hh:389
void setTime(const double t)
Set time provider (which gives you access to the global time).
Definition: common/pass.hh:236
DestinationType * destination_
destination (might be set from outside)
Definition: common/pass.hh:377
Dune::PushBackTuple< typename PreviousPassType::PassIds, std::integral_constant< int, passIdImp > >::type PassIds
pass ids up to here (tuple of integral constants)
Definition: common/pass.hh:157
DestinationType::DiscreteFunctionSpaceType::CommunicationManagerType::NonBlockingCommunicationType NonBlockingCommunicationType
Definition: common/pass.hh:167
void allocateLocalMemory()
No memory needs to be allocated.
Definition: common/pass.hh:95