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

revision 108 by jgs, Thu Jan 27 06:21:59 2005 UTC revision 147 by jgs, Fri Aug 12 01:45:47 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        }
51          m_initialValue(other.m_initialValue),
52          m_currentValue(other.m_initialValue),
53          operation(other.operation)
54        {
55        }
56        inline void operator()(double value)
57        {
58          m_currentValue=operation(m_currentValue,value);
59          return;
60        }
61        inline void resetResult()
62        {
63          m_currentValue=m_initialValue;
64        }
65        inline double getResult() const
66        {
67          return m_currentValue;
68        }
69      private:
70        //
71        // the initial operation value
72        double m_initialValue;
73        //
74        // the current operation value
75        double m_currentValue;
76        //
77        // The operation to perform
78        BinaryFunction operation;
79    };
80
81    /**
82       \brief
83       Return the maximum value of the two given values.
84  */  */
85  struct FMax : public std::binary_function<double,double,double>  struct FMax : public std::binary_function<double,double,double>
86  {  {
# Line 45  struct FMax : public std::binary_functio Line 92  struct FMax : public std::binary_functio
92
93  /**  /**
94     \brief     \brief
95     Return the minimum value.     Return the minimum value of the two given values.

Description:
Return the minimum value.
96  */  */
97  struct FMin : public std::binary_function<double,double,double>  struct FMin : public std::binary_function<double,double,double>
98  {  {
# Line 60  struct FMin : public std::binary_functio Line 104  struct FMin : public std::binary_functio
104
105  /**  /**
106     \brief     \brief
107     Return the absolute maximum value.     Return the absolute maximum value of the two given values.

Description:
Return the absolute maximum value.
108  */  */
109  struct AbsMax : public std::binary_function<double,double,double>  struct AbsMax : public std::binary_function<double,double,double>
110  {  {
# Line 75  struct AbsMax : public std::binary_funct Line 116  struct AbsMax : public std::binary_funct
116
117  /**  /**
118     \brief     \brief
119     Return the length.     Return the absolute minimum value of the two given values.
120    */
121    struct AbsMin : public std::binary_function<double,double,double>
122    {
123      inline double operator()(double x, double y) const
124      {
125        return std::min(fabs(x),fabs(y));
126      }
127    };
128
129     Description:  /**
130     Return the length.     \brief
131       Return the length between the two given values.
132  */  */
133  struct Length : public std::binary_function<double,double,double>  struct Length : public std::binary_function<double,double,double>
134  {  {
# Line 90  struct Length : public std::binary_funct Line 140  struct Length : public std::binary_funct
140
141  /**  /**
142     \brief     \brief
143     Return the trace.     Return the trace of the two given values.

Description:
Return the trace.
144  */  */
145  struct Trace : public std::binary_function<double,double,double>  struct Trace : public std::binary_function<double,double,double>
146  {  {
# Line 105  struct Trace : public std::binary_functi Line 152  struct Trace : public std::binary_functi
152
153  /**  /**
154     \brief     \brief
155     Adapt algorithms so they may be used by Data.     Perform the given operation upon all values in all data-points in the
156       given Data object and return the final result.
157
158     Description:     Calls DataArrayView::reductionOp
Adapt algorithms so they may be used by Data. The functor
maintains state, the currentValue returned by the operation,
and the initial value.
159  */  */
160  template <class BinaryFunction>  template <class BinaryFunction>
public:
m_initialValue(initialValue),
m_currentValue(initialValue)
{}
inline void operator()(double value)
{
m_currentValue=operation(m_currentValue,value);
return;
}
inline void resetResult()
{
m_currentValue=m_initialValue;
}
inline double getResult() const
{
return m_currentValue;
}
private:
//
// the initial operation value
double m_initialValue;
//
// the current operation value
double m_currentValue;
//
// The operation to perform
BinaryFunction operation;
};

/**
\brief
Perform the given operation upon all Data elements and return a single
result.

Description:
Perform the given operation upon all Data elements and return a single
result.
*/
template <class UnaryFunction>
161  inline  inline
162  double  double
163  algorithm(DataExpanded& data,  algorithm(DataExpanded& data,
164            UnaryFunction operation)            BinaryFunction operation,
165          double initial_value)
166  {  {
167    int i,j;    int i,j;
168    DataArrayView::ValueType::size_type numDPPSample=data.getNumDPPSample();    int numDPPSample=data.getNumDPPSample();
169    DataArrayView::ValueType::size_type numSamples=data.getNumSamples();    int numSamples=data.getNumSamples();
170    double resultLocal=0;    int resultVectorLength=numDPPSample*numSamples;
171  #pragma omp parallel private(resultLocal)    std::vector<double> resultVector(resultVectorLength);
172    {    DataArrayView dataView=data.getPointDataView();
173  #pragma omp for private(i,j) schedule(static)    // calculate the reduction operation value for each data point
174      for (i=0;i<numSamples;i++) {    // storing the result for each data-point in successive entries
175        for (j=0;j<numDPPSample;j++) {    // in resultVector
176      resultLocal=data.getPointDataView().reductionOp(data.getPointOffset(i,j), operation);    #pragma omp parallel for private(i,j) schedule(static)
177  #pragma omp critical (algorithm)    for (i=0;i<numSamples;i++) {
178      operation(resultLocal);      for (j=0;j<numDPPSample;j++) {
179        }        resultVector[j*numSamples+i]=dataView.reductionOp(data.getPointOffset(i,j),operation,initial_value);
180      }      }
181    }    }
182    return operation.getResult();    // now calculate the reduction operation value across the results
183      // for each data-point
184      double current_value=initial_value;
185      for (int l=0;l<resultVectorLength;l++) {
186        current_value=operation(current_value,resultVector[l]);
187      }
188      return current_value;
189  }  }
190
191  template <class UnaryFunction>  template <class BinaryFunction>
192  inline  inline
193  double  double
194  algorithm(DataTagged& data,  algorithm(DataTagged& data,
195            UnaryFunction operation)            BinaryFunction operation,
196          double initial_value)
197  {  {
//
// perform the operation on each tagged value
198    const DataTagged::DataMapType& lookup=data.getTagLookup();    const DataTagged::DataMapType& lookup=data.getTagLookup();
199    DataTagged::DataMapType::const_iterator i;    DataTagged::DataMapType::const_iterator i;
200    DataTagged::DataMapType::const_iterator lookupEnd=lookup.end();    DataTagged::DataMapType::const_iterator lookupEnd=lookup.end();
201    DataArrayView& dataView=data.getPointDataView();    DataArrayView& dataView=data.getPointDataView();
202      std::vector<double> resultVector;
203      int resultVectorLength;
204      // perform the operation on each tagged value
205    for (i=lookup.begin();i!=lookupEnd;i++) {    for (i=lookup.begin();i!=lookupEnd;i++) {
206      operation(dataView.reductionOp(i->second,operation));      resultVector.push_back(dataView.reductionOp(i->second,operation,initial_value));
207    }    }
208    //    // perform the operation on the default value
209    // finally perform the operation on the default value    resultVector.push_back(data.getDefaultValue().reductionOp(operation,initial_value));
210    operation(data.getDefaultValue().reductionOp(operation));    // now calculate the reduction operation value across the results
211    return operation.getResult();    // for each tagged value
212      resultVectorLength=resultVector.size();
213      double current_value=initial_value;
214      for (int l=0;l<resultVectorLength;l++) {
215        current_value=operation(current_value,resultVector[l]);
216      }
217      return current_value;
218  }  }
219
220  template <class UnaryFunction>  template <class BinaryFunction>
221  inline  inline
222  double  double
223  algorithm(DataConstant& data,  algorithm(DataConstant& data,
224            UnaryFunction operation)            BinaryFunction operation,
225          double initial_value)
226  {  {
227    return data.getPointDataView().reductionOp(operation);    return data.getPointDataView().reductionOp(operation,initial_value);
228  }  }
229
230  /**  /**
231     \brief     \brief
232     Perform the given data point reduction operation on all data points     Perform the given data-point reduction operation on all data-points
233     in data, storing results in corresponding elements of result.     in data, storing results in corresponding data-points of result.
234
235     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
236     of data points, but where data has data points of rank n, result must have     of data points, but where data has data points of rank n, result must have
237     data points of rank 0.     data points of rank 0.
238
239     Calls DataArrayView::dp_algorithm.     Calls DataArrayView::reductionOp
240  */  */
241  template <class UnaryFunction>  template <class BinaryFunction>
242  inline  inline
243  void  void
244  dp_algorithm(DataExpanded& data,  dp_algorithm(DataExpanded& data,
245               DataExpanded& result,               DataExpanded& result,
246               UnaryFunction operation)               BinaryFunction operation,
247             double initial_value)
248  {  {
//
// perform the operation on each data value
// and assign this to the corresponding element in result
249    int i,j;    int i,j;
250    DataArrayView::ValueType::size_type numDPPSample=data.getNumDPPSample();    int numSamples=data.getNumSamples();
251    DataArrayView::ValueType::size_type numSamples=data.getNumSamples();    int numDPPSample=data.getNumDPPSample();
252    {    DataArrayView dataView=data.getPointDataView();
253  #pragma omp for private(i,j) schedule(static)    DataArrayView resultView=result.getPointDataView();
254      for (i=0;i<numSamples;i++) {    // perform the operation on each data-point and assign
255        for (j=0;j<numDPPSample;j++) {    // this to the corresponding element in result
256  #pragma omp critical (dp_algorithm)    #pragma omp parallel for private(i,j) schedule(static)
257          result.getPointDataView().getData(data.getPointOffset(i,j)) =    for (i=0;i<numSamples;i++) {
258            data.getPointDataView().dp_reductionOp(data.getPointOffset(i,j),operation);      for (j=0;j<numDPPSample;j++) {
259        }        resultView.getData(result.getPointOffset(i,j)) =
260            dataView.reductionOp(data.getPointOffset(i,j),operation,initial_value);
261      }      }
262    }    }
263  }  }
264
265  template <class UnaryFunction>  template <class BinaryFunction>
266  inline  inline
267  void  void
268  dp_algorithm(DataTagged& data,  dp_algorithm(DataTagged& data,
269               DataTagged& result,               DataTagged& result,
270               UnaryFunction operation)               BinaryFunction operation,
271             double initial_value)
272  {  {
//
// perform the operation on each tagged data value
// and assign this to the corresponding element in result
273    const DataTagged::DataMapType& lookup=data.getTagLookup();    const DataTagged::DataMapType& lookup=data.getTagLookup();
274    DataTagged::DataMapType::const_iterator i;    DataTagged::DataMapType::const_iterator i;
275    DataTagged::DataMapType::const_iterator lookupEnd=lookup.end();    DataTagged::DataMapType::const_iterator lookupEnd=lookup.end();
276      DataArrayView dataView=data.getPointDataView();
277      DataArrayView resultView=result.getPointDataView();
278      // perform the operation on each tagged data value
279      // and assign this to the corresponding element in result
280    for (i=lookup.begin();i!=lookupEnd;i++) {    for (i=lookup.begin();i!=lookupEnd;i++) {
281      result.getPointDataView().getData(i->second) =      resultView.getData(i->second) =
282        data.getPointDataView().dp_reductionOp(i->second,operation);        dataView.reductionOp(i->second,operation,initial_value);
283    }    }
284    //    // perform the operation on the default data value
// finally perform the operation on the default data value
285    // and assign this to the default element in result    // and assign this to the default element in result
286    result.getPointDataView().getData(0) =    resultView.getData(0) =
287      data.getDefaultValue().dp_reductionOp(operation);      data.getDefaultValue().reductionOp(operation,initial_value);
288  }  }
289
290  template <class UnaryFunction>  template <class BinaryFunction>
291  inline  inline
292  void  void
293  dp_algorithm(DataConstant& data,  dp_algorithm(DataConstant& data,
294               DataConstant& result,               DataConstant& result,
295               UnaryFunction operation)               BinaryFunction operation,
296             double initial_value)
297  {  {
298    //    // perform the operation on the data value
299    // perform the operation on the default data value    // and assign this to the element in result
// and assign this to the default element in result
300    result.getPointDataView().getData(0) =    result.getPointDataView().getData(0) =
301      data.getPointDataView().dp_reductionOp(operation);      data.getPointDataView().reductionOp(operation,initial_value);
302  }  }
303
304  } // end of namespace  } // end of namespace
305
306  #endif  #endif

Legend:
 Removed from v.108 changed lines Added in v.147