OPAL (Object Oriented Parallel Accelerator Library)  2.2.0
OPAL
ReductionLoc.hpp
Go to the documentation of this file.
1 // -*- C++ -*-
2 /***************************************************************************
3  *
4  * The IPPL Framework
5  *
6  * This program was prepared by PSI.
7  * All rights in the program are reserved by PSI.
8  * Neither PSI nor the author(s)
9  * makes any warranty, express or implied, or assumes any liability or
10  * responsibility for the use of this software
11  *
12  * Visit www.amas.web.psi for more details
13  *
14  ***************************************************************************/
15 
16 // -*- C++ -*-
17 /***************************************************************************
18  *
19  * The IPPL Framework
20  *
21  *
22  * Visit http://people.web.psi.ch/adelmann/ for more details
23  *
24  ***************************************************************************/
25 
26 // include files
27 #include "Field/ReductionLoc.h"
28 #include "Index/NDIndex.h"
29 #include "Field/AssignDefs.h"
30 #include "Message/Message.h"
31 #include "Message/GlobalComm.h"
32 
33 
35 //
36 // Reduce and find the location.
37 //
39 
40 template<class T, class LOC>
41 struct ReduceLoc
42 {
43  T Val;
44  LOC Loc;
45  ReduceLoc() {}
46  ReduceLoc(const T& t, const LOC& l) : Val(t), Loc(l) {}
48  {
49  ::putMessage(mess,Val);
50  ::putMessage(mess,Loc);
51  return mess;
52  }
54  {
55  ::getMessage(mess,Val);
56  ::getMessage(mess,Loc);
57  return mess;
58  }
59  bool operator<(const ReduceLoc<T,LOC>& rhs) const
60  {
61  return Val < rhs.Val;
62  }
63 };
64 
65 template <class T1, class T2, class LOC>
66 inline
68 Min(const ReduceLoc<T1,LOC>& lhs, const ReduceLoc<T2,LOC>& rhs)
69 {
70  typedef typename PETEBinaryReturn<T1,T2,FnMin>::type T0;
71  if (lhs < rhs)
72  return ReduceLoc<T0,LOC>(lhs.Val,lhs.Loc);
73  else
74  return ReduceLoc<T0,LOC>(rhs.Val,rhs.Loc);
75 }
76 
77 template <class T1, class T2, class LOC>
78 inline
80 Max(const ReduceLoc<T1,LOC>& lhs, const ReduceLoc<T2,LOC>& rhs)
81 {
82  typedef typename PETEBinaryReturn<T1,T2,FnMax>::type T0;
83  if (lhs < rhs)
84  return ReduceLoc<T0,LOC>(rhs.Val,rhs.Loc);
85  else
86  return ReduceLoc<T0,LOC>(lhs.Val,lhs.Loc);
87 }
88 
89 
90 
91 
92 template<class T, class CompOp, class AccOp, unsigned D>
93 typename T::PETE_Return_t
94 Reduction(const PETE_Expr<T>& const_expr,
95  CompOp comp_op,
96  AccOp acc_op,
97  NDIndex<D>& loc)
98 {
99 
100  // Extract the expression.
101  typename T::PETE_Expr_t expr ( const_expr.PETE_unwrap().MakeExpression() );
102 
103  // The type and value that we will return.
104  typedef typename T::PETE_Return_t R;
105  ReduceLoc< R, NDIndex<D> > global_rloc;
106 
107  // Get the number of elements we will be looping over.
108  // The combiner makes sure each term has
109  // the same number of elements.
110  int n = for_each(expr,PETE_CountElems(),AssertEquals());
111  if (n > 0) {
112  // Get the first value.
113  R ret = for_each(expr,EvalFunctor_0());
114 
115  // Make an operator that will store the location in loc.
116  FindLocation<D> find_loc(loc);
117 
118  // Get the first location.
119  for_each(expr,find_loc,PETE_NullCombiner());
120 
121  // Loop over all the local elements.
122  for (int i=1; i<n; ++i) {
123  // Increment the pointers.
125  // check and see if this is the new min.
126  R val = for_each(expr,EvalFunctor_0());
127  if ( PETE_apply(comp_op,val,ret) ) {
128  // Record the value.
129  ret = val;
130  // Record the current location.
131  for_each(expr,find_loc,PETE_NullCombiner());
132  }
133  }
134 
135  // Do the cross processor reduction.
136  ReduceLoc< R, NDIndex<D> > rloc( ret , loc );
137  reduce_masked(rloc, global_rloc, acc_op, true);
138  } else {
139  // do reduction, but indicate we have no local contribution
140  reduce_masked(global_rloc, global_rloc, acc_op, false);
141  }
142 
143  // Return the calculated values
144  loc = global_rloc.Loc;
145  return global_rloc.Val;
146 }
147 
148 /***************************************************************************
149  * $RCSfile: ReductionLoc.cpp,v $ $Author: adelmann $
150  * $Revision: 1.1.1.1 $ $Date: 2003/01/23 07:40:26 $
151  * IPPL_VERSION_ID: $Id: ReductionLoc.cpp,v 1.1.1.1 2003/01/23 07:40:26 adelmann Exp $
152  ***************************************************************************/
T::PETE_Return_t Reduction(const PETE_Expr< T > &const_expr, CompOp comp_op, AccOp acc_op, NDIndex< D > &loc)
void PETE_apply(const OpPeriodic< T > &e, T &a, const T &b)
Definition: BCond.hpp:373
Definition: PETE.h:80
Definition: rbendmap.h:8
WrappedExpr & PETE_unwrap()
Definition: PETE.h:85
double Max(double a, double b)
double Min(double a, double b)
PETE_ComputeBinaryType< PETE_Type2Index< T1 >::val, PETE_Type2Index< T2 >::val, Op::tag >::type type
bool reduce_masked(Communicate &comm, T &input, T &output, const ReduceOp &op, bool IncludeVal)
Definition: GlobalComm.hpp:208
Message & getMessage(Message &mess)
bool for_each(const BareFieldIterator< T, D > &p, SameFieldID s, C)
Definition: AssignDefs.h:30
ReduceLoc(const T &t, const LOC &l)
Message & putMessage(Message &mess)