# Diff of /trunk/escript/src/DataAlgorithm.h

revision 97 by jgs, Tue Dec 14 05:39:33 2004 UTC revision 117 by jgs, Fri Apr 1 05:48:57 2005 UTC
# Line 25  Line 25
25  #include <algorithm>  #include <algorithm>
26  #include <math.h>  #include <math.h>
27  #include <limits>  #include <limits>
28    #include <vector>
29
30  namespace escript {  namespace escript {
31
32  /**  /**
33     \brief     \brief
34     Return the maximum value.     Adapt binary algorithms so they may be used in DataArrayView reduction operations.
35
36     Description:     Description:
37     Return the maximum value.     This functor adapts the given BinaryFunction operation by starting with the
38       given inital value applying this operation to successive values, storing the
39       rolling result in m_currentValue - which can be accessed or reset by getResult
40       and resetResult respectively.
41    */
42    template <class BinaryFunction>
44      public:
46          m_initialValue(initialValue),
47          m_currentValue(initialValue)
48        {
49        }
50        inline void operator()(double value)
51        {
52          m_currentValue=operation(m_currentValue,value);
53          return;
54        }
55        inline void resetResult()
56        {
57          m_currentValue=m_initialValue;
58        }
59        inline double getResult() const
60        {
61          return m_currentValue;
62        }
63      private:
64        //
65        // the initial operation value
66        double m_initialValue;
67        //
68        // the current operation value
69        double m_currentValue;
70        //
71        // The operation to perform
72        BinaryFunction operation;
73    };
74
75    /**
76       \brief
77       Return the maximum value of the two given values.
78  */  */
79  struct FMax : public std::binary_function<double,double,double>  struct FMax : public std::binary_function<double,double,double>
80  {  {
# Line 45  struct FMax : public std::binary_functio Line 86  struct FMax : public std::binary_functio
86
87  /**  /**
88     \brief     \brief
89     Return the minimum value.     Return the minimum value of the two given values.

Description:
Return the minimum value.
90  */  */
91  struct FMin : public std::binary_function<double,double,double>  struct FMin : public std::binary_function<double,double,double>
92  {  {
# Line 60  struct FMin : public std::binary_functio Line 98  struct FMin : public std::binary_functio
98
99  /**  /**
100     \brief     \brief
101     Return the absolute maximum value.     Return the absolute maximum value of the two given values.

Description:
Return the absolute maximum value.
102  */  */
103  struct AbsMax : public std::binary_function<double,double,double>  struct AbsMax : public std::binary_function<double,double,double>
104  {  {
# Line 75  struct AbsMax : public std::binary_funct Line 110  struct AbsMax : public std::binary_funct
110
111  /**  /**
112     \brief     \brief
113     Adapt algorithms so they may be used by Data.     Return the absolute minimum value of the two given values.
114    */
115    struct AbsMin : public std::binary_function<double,double,double>
116    {
117      inline double operator()(double x, double y) const
118      {
119        return std::min(fabs(x),fabs(y));
120      }
121    };
122
123     Description:  /**
124     Adapt algorithms so they may be used by Data. The functor     \brief
125     maintains state, ie the currentValue retuned by the operation.     Return the length between the two given values.
126  */  */
127  template <class BinaryFunction>  struct Length : public std::binary_function<double,double,double>
129   public:    inline double operator()(double x, double y) const
131        m_currentValue(initialValue)      return std::sqrt(std::pow(x,2)+std::pow(y,2));
132      {}    }
133      inline void operator()(double value)  };
134      {
135        m_currentValue=operation(m_currentValue,value);  /**
136        return;     \brief
137      }     Return the trace of the two given values.
138      inline double getResult() const  */
139      {  struct Trace : public std::binary_function<double,double,double>
140        return m_currentValue;  {
141      }    inline double operator()(double x, double y) const
142   private:    {
143      //      return x+y;
144      // the current operation value    }
double m_currentValue;
//
// The operation to perform
BinaryFunction operation;
145  };  };
146
147  /**  /**
148     \brief     \brief
149     Perform the given operation upon all Data elements and return a single     Perform the given operation upon all values in all data-points in the
150     result.     given Data object and return the final result.
151
152     Description:     Calls DataArrayView::reductionOp
Perform the given operation upon all Data elements and return a single
result.
153  */  */
154  template <class UnaryFunction>  template <class UnaryFunction>
155  inline  inline
# Line 121  algorithm(DataExpanded& data, Line 158  algorithm(DataExpanded& data,
158            UnaryFunction operation)            UnaryFunction operation)
159  {  {
160    int i,j;    int i,j;
161    DataArrayView::ValueType::size_type numDPPSample=data.getNumDPPSample();    int numDPPSample=data.getNumDPPSample();
162    DataArrayView::ValueType::size_type numSamples=data.getNumSamples();    int numSamples=data.getNumSamples();
163    double resultLocal=0;    int resultVectorLength=numDPPSample*numSamples;
164  #pragma omp parallel private(resultLocal)    std::vector<double> resultVector(resultVectorLength);
165      DataArrayView dataView=data.getPointDataView();
166      // calculate the reduction operation value for each data point
167      // storing the result for each data-point in successive entries
168      // in resultVector
169    {    {
170  #pragma omp for private(i,j) schedule(static)  #pragma omp for private(i,j) schedule(static)
171      for (i=0;i<numSamples;i++) {      for (i=0;i<numSamples;i++) {
172        for (j=0;j<numDPPSample;j++) {        for (j=0;j<numDPPSample;j++) {
173      resultLocal=data.getPointDataView().algorithm(data.getPointOffset(i,j), operation);  #pragma omp critical (reductionOp)
174  #pragma omp critical (algorithm)      resultVector[j*numSamples+i]=dataView.reductionOp(data.getPointOffset(i,j),operation);
operation(resultLocal);
175        }        }
176      }      }
177    }    }
178      // now calculate the reduction operation value across the results
179      // for each data-point
180      operation.resetResult();
181      for (int l=0;l<resultVectorLength;l++) {
182        operation(resultVector[l]);
183      }
184    return operation.getResult();    return operation.getResult();
185  }  }
186
# Line 144  double Line 190  double
190  algorithm(DataTagged& data,  algorithm(DataTagged& data,
191            UnaryFunction operation)            UnaryFunction operation)
192  {  {
//
// perform the operation on each tagged value
193    const DataTagged::DataMapType& lookup=data.getTagLookup();    const DataTagged::DataMapType& lookup=data.getTagLookup();
194    DataTagged::DataMapType::const_iterator i;    DataTagged::DataMapType::const_iterator i;
195    DataTagged::DataMapType::const_iterator lookupEnd=lookup.end();    DataTagged::DataMapType::const_iterator lookupEnd=lookup.end();
196    DataArrayView& dataView=data.getPointDataView();    DataArrayView& dataView=data.getPointDataView();
197      std::vector<double> resultVector;
198      int resultVectorLength;
199      // perform the operation on each tagged value
200    for (i=lookup.begin();i!=lookupEnd;i++) {    for (i=lookup.begin();i!=lookupEnd;i++) {
201      operation(dataView.algorithm(i->second,operation));      resultVector.push_back(dataView.reductionOp(i->second,operation));
202      }
203      // perform the operation on the default value
204      resultVector.push_back(data.getDefaultValue().reductionOp(operation));
205      // now calculate the reduction operation value across the results
206      // for each tagged value
207      resultVectorLength=resultVector.size();
208      operation.resetResult();
209      for (int l=0;l<resultVectorLength;l++) {
210        operation(resultVector[l]);
211    }    }
//
// finally perform the operation on the default value
operation(data.getDefaultValue().algorithm(operation));
212    return operation.getResult();    return operation.getResult();
213  }  }
214
# Line 165  double Line 218  double
218  algorithm(DataConstant& data,  algorithm(DataConstant& data,
219            UnaryFunction operation)            UnaryFunction operation)
220  {  {
221    return data.getPointDataView().algorithm(operation);    return data.getPointDataView().reductionOp(operation);
222  }  }
223
224  /**  /**
225     \brief     \brief
226     Perform the given data point reduction operation upon all data points     Perform the given data-point reduction operation on all data-points
227     in data, storing results in corresponding elements of result.     in data, storing results in corresponding data-points of result.
228
229     Objects data and result must be of the same type, and have the same number     Objects data and result must be of the same type, and have the same number
230     of samples and number of data points per sample, but where data has data     of data points, but where data has data points of rank n, result must have
231     points of rank n, result must have data points of rank 0.     data points of rank 0.
232
233     Calls DataArrayView::dp_algorithm.     Calls DataArrayView::reductionOp
234  */  */
235  template <class UnaryFunction>  template <class UnaryFunction>
236  inline  inline
237  void  void
238  dp_algorithm(DataExpanded& result,  dp_algorithm(DataExpanded& data,
239               DataExpanded& data,               DataExpanded& result,
240               UnaryFunction operation)               UnaryFunction operation)
241  {  {
242    int i,j;    int i,j;
243    DataArrayView::ValueType::size_type numDPPSample=data.getNumDPPSample();    int numDPPSample=data.getNumDPPSample();
244    DataArrayView::ValueType::size_type numSamples=data.getNumSamples();    int numSamples=data.getNumSamples();
245      DataArrayView dataView=data.getPointDataView();
246      DataArrayView resultView=result.getPointDataView();
247      // perform the operation on each data-point and assign
248      // this to the corresponding element in result
249    {    {
250  #pragma omp for private(i,j) schedule(static)  #pragma omp for private(i,j) schedule(static)
251      for (i=0;i<numSamples;i++) {      for (i=0;i<numSamples;i++) {
252        for (j=0;j<numDPPSample;j++) {        for (j=0;j<numDPPSample;j++) {
253          // assign this to corresponding element in result  #pragma omp critical (reductionOp)
254      data.getPointDataView().dp_algorithm(data.getPointOffset(i,j),operation);          resultView.getData(data.getPointOffset(i,j)) =
255  #pragma omp critical (dp_algorithm)            dataView.reductionOp(data.getPointOffset(i,j),operation);
256        }        }
257      }      }
258    }    }
# Line 204  dp_algorithm(DataExpanded& result, Line 261  dp_algorithm(DataExpanded& result,
261  template <class UnaryFunction>  template <class UnaryFunction>
262  inline  inline
263  void  void
264  dp_algorithm(DataTagged& result,  dp_algorithm(DataTagged& data,
265               DataTagged& data,               DataTagged& result,
266               UnaryFunction operation)               UnaryFunction operation)
267  {  {
//
// perform the operation on each tagged value
268    const DataTagged::DataMapType& lookup=data.getTagLookup();    const DataTagged::DataMapType& lookup=data.getTagLookup();
269    DataTagged::DataMapType::const_iterator i;    DataTagged::DataMapType::const_iterator i;
270    DataTagged::DataMapType::const_iterator lookupEnd=lookup.end();    DataTagged::DataMapType::const_iterator lookupEnd=lookup.end();
271      DataArrayView dataView=data.getPointDataView();
272      DataArrayView resultView=result.getPointDataView();
273      // perform the operation on each tagged data value
274      // and assign this to the corresponding element in result
275    for (i=lookup.begin();i!=lookupEnd;i++) {    for (i=lookup.begin();i!=lookupEnd;i++) {
276      // assign this to corresponding element in result      resultView.getData(i->second) =
277      data.getPointDataView().dp_algorithm(i->second,operation);        dataView.reductionOp(i->second,operation);
278    }    }
279    //    // perform the operation on the default data value
280    // finally perform the operation on the default value    // and assign this to the default element in result
281    // assign this to corresponding element in result    resultView.getData(0) =
282    data.getDefaultValue().dp_algorithm(operation);      data.getDefaultValue().reductionOp(operation);
283  }  }
284
285  template <class UnaryFunction>  template <class UnaryFunction>
286  inline  inline
287  void  void
288  dp_algorithm(DataConstant& result,  dp_algorithm(DataConstant& data,
289               DataConstant& data,               DataConstant& result,
290               UnaryFunction operation)               UnaryFunction operation)
291  {  {
292    //result.getPointDataView().getData()    // perform the operation on the data value
293    // assign this to corresponding element in result    // and assign this to the element in result
294    data.getPointDataView().dp_algorithm(operation);    result.getPointDataView().getData(0) =
295        data.getPointDataView().reductionOp(operation);
296  }  }
297
298  } // end of namespace  } // end of namespace

Legend:
 Removed from v.97 changed lines Added in v.117