/[escript]/trunk/escript/src/DataAlgorithm.h
ViewVC logotype

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

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

trunk/esys2/escript/src/Data/DataAlgorithm.h revision 97 by jgs, Tue Dec 14 05:39:33 2004 UTC trunk/escript/src/DataAlgorithm.h revision 474 by jgs, Mon Jan 30 04:23:44 2006 UTC
# Line 16  Line 16 
16  #if !defined escript_DataAlgorithm_20040714_H  #if !defined escript_DataAlgorithm_20040714_H
17  #define escript_DataAlgorithm_20040714_H  #define escript_DataAlgorithm_20040714_H
18    
19  #include "escript/Data/DataExpanded.h"  #include "DataExpanded.h"
20  #include "escript/Data/DataTagged.h"  #include "DataTagged.h"
21  #include "escript/Data/DataConstant.h"  #include "DataConstant.h"
22  #include "escript/Data/DataArrayView.h"  #include "DataArrayView.h"
23    
24  #include <iostream>  #include <iostream>
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>
43    class DataAlgorithmAdapter {
44      public:
45        DataAlgorithmAdapter(double initialValue):
46          m_initialValue(initialValue),
47          m_currentValue(initialValue)
48        {
49        }
50        DataAlgorithmAdapter(const DataAlgorithmAdapter& other):
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     Adapt algorithms so they may be used by Data.     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     Adapt algorithms so they may be used by Data. The functor     \brief
131     maintains state, ie the currentValue retuned by the operation.     Return the length between the two given values.
132  */  */
133  template <class BinaryFunction>  struct Length : public std::binary_function<double,double,double>
134  class DataAlgorithmAdapter {  {
135   public:    inline double operator()(double x, double y) const
136      DataAlgorithmAdapter(double initialValue):    {
137        m_currentValue(initialValue)      return std::sqrt(std::pow(x,2)+std::pow(y,2));
138      {}    }
139      inline void operator()(double value)  };
140      {  
141        m_currentValue=operation(m_currentValue,value);  /**
142        return;     \brief
143      }     Return the trace of the two given values.
144      inline double getResult() const  */
145      {  struct Trace : public std::binary_function<double,double,double>
146        return m_currentValue;  {
147      }    inline double operator()(double x, double y) const
148   private:    {
149      //      return x+y;
150      // the current operation value    }
     double m_currentValue;  
     //  
     // The operation to perform  
     BinaryFunction operation;  
151  };  };
152    
153  /**  /**
154     \brief     \brief
155     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
156     result.     given Data object and return the final result.
157    
158     Description:     Calls DataArrayView::reductionOp
    Perform the given operation upon all Data elements and return a single  
    result.  
159  */  */
160  template <class UnaryFunction>  template <class BinaryFunction>
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;    double global_current_value=initial_value;
171  #pragma omp parallel private(resultLocal)    double local_current_value;
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++) {    // reducing the result for each data-point into the current_value variables
175        for (j=0;j<numDPPSample;j++) {    #pragma omp parallel private(local_current_value)
176      resultLocal=data.getPointDataView().algorithm(data.getPointOffset(i,j), operation);    {
177  #pragma omp critical (algorithm)        local_current_value=initial_value;
178      operation(resultLocal);        #pragma omp for private(i,j) schedule(static)
179          for (i=0;i<numSamples;i++) {
180            for (j=0;j<numDPPSample;j++) {
181              local_current_value=operation(local_current_value,dataView.reductionOp(data.getPointOffset(i,j),operation,initial_value));
182            }
183        }        }
184      }        #pragma omp critical
185          global_current_value=operation(global_current_value,local_current_value);
186    }    }
187    return operation.getResult();    return global_current_value;
188  }  }
189    
190  template <class UnaryFunction>  template <class BinaryFunction>
191  inline  inline
192  double  double
193  algorithm(DataTagged& data,  algorithm(DataTagged& data,
194            UnaryFunction operation)            BinaryFunction operation,
195          double initial_value)
196  {  {
   //  
   // perform the operation on each tagged value  
197    const DataTagged::DataMapType& lookup=data.getTagLookup();    const DataTagged::DataMapType& lookup=data.getTagLookup();
198    DataTagged::DataMapType::const_iterator i;    DataTagged::DataMapType::const_iterator i;
199    DataTagged::DataMapType::const_iterator lookupEnd=lookup.end();    DataTagged::DataMapType::const_iterator lookupEnd=lookup.end();
200    DataArrayView& dataView=data.getPointDataView();    DataArrayView& dataView=data.getPointDataView();
201      double current_value=initial_value;
202      // perform the operation on each tagged value
203    for (i=lookup.begin();i!=lookupEnd;i++) {    for (i=lookup.begin();i!=lookupEnd;i++) {
204      operation(dataView.algorithm(i->second,operation));      current_value=operation(current_value,dataView.reductionOp(i->second,operation,initial_value));
205    }    }
206    //    // perform the operation on the default value
207    // finally perform the operation on the default value    current_value=operation(current_value,data.getDefaultValue().reductionOp(operation,initial_value));
208    operation(data.getDefaultValue().algorithm(operation));    return current_value;
   return operation.getResult();  
209  }  }
210    
211  template <class UnaryFunction>  template <class BinaryFunction>
212  inline  inline
213  double  double
214  algorithm(DataConstant& data,  algorithm(DataConstant& data,
215            UnaryFunction operation)            BinaryFunction operation,
216          double initial_value)
217  {  {
218    return data.getPointDataView().algorithm(operation);    return data.getPointDataView().reductionOp(operation,initial_value);
219  }  }
220    
221  /**  /**
222     \brief     \brief
223     Perform the given data point reduction operation upon all data points     Perform the given data-point reduction operation on all data-points
224     in data, storing results in corresponding elements of result.     in data, storing results in corresponding data-points of result.
225    
226     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
227     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
228     points of rank n, result must have data points of rank 0.     data points of rank 0.
229    
230     Calls DataArrayView::dp_algorithm.     Calls DataArrayView::reductionOp
231  */  */
232  template <class UnaryFunction>  template <class BinaryFunction>
233  inline  inline
234  void  void
235  dp_algorithm(DataExpanded& result,  dp_algorithm(DataExpanded& data,
236               DataExpanded& data,               DataExpanded& result,
237               UnaryFunction operation)               BinaryFunction operation,
238             double initial_value)
239  {  {
240    int i,j;    int i,j;
241    DataArrayView::ValueType::size_type numDPPSample=data.getNumDPPSample();    int numSamples=data.getNumSamples();
242    DataArrayView::ValueType::size_type numSamples=data.getNumSamples();    int numDPPSample=data.getNumDPPSample();
243    {    DataArrayView dataView=data.getPointDataView();
244  #pragma omp for private(i,j) schedule(static)    DataArrayView resultView=result.getPointDataView();
245      for (i=0;i<numSamples;i++) {    // perform the operation on each data-point and assign
246        for (j=0;j<numDPPSample;j++) {    // this to the corresponding element in result
247          // assign this to corresponding element in result    #pragma omp parallel for private(i,j) schedule(static)
248      data.getPointDataView().dp_algorithm(data.getPointOffset(i,j),operation);    for (i=0;i<numSamples;i++) {
249  #pragma omp critical (dp_algorithm)      for (j=0;j<numDPPSample;j++) {
250        }        resultView.getData(result.getPointOffset(i,j)) =
251            dataView.reductionOp(data.getPointOffset(i,j),operation,initial_value);
252      }      }
253    }    }
254  }  }
255    
256  template <class UnaryFunction>  template <class BinaryFunction>
257  inline  inline
258  void  void
259  dp_algorithm(DataTagged& result,  dp_algorithm(DataTagged& data,
260               DataTagged& data,               DataTagged& result,
261               UnaryFunction operation)               BinaryFunction operation,
262             double initial_value)
263  {  {
   //  
   // perform the operation on each tagged value  
264    const DataTagged::DataMapType& lookup=data.getTagLookup();    const DataTagged::DataMapType& lookup=data.getTagLookup();
265    DataTagged::DataMapType::const_iterator i;    DataTagged::DataMapType::const_iterator i;
266    DataTagged::DataMapType::const_iterator lookupEnd=lookup.end();    DataTagged::DataMapType::const_iterator lookupEnd=lookup.end();
267      DataArrayView dataView=data.getPointDataView();
268      DataArrayView resultView=result.getPointDataView();
269      // perform the operation on each tagged data value
270      // and assign this to the corresponding element in result
271    for (i=lookup.begin();i!=lookupEnd;i++) {    for (i=lookup.begin();i!=lookupEnd;i++) {
272      // assign this to corresponding element in result      resultView.getData(i->second) =
273      data.getPointDataView().dp_algorithm(i->second,operation);        dataView.reductionOp(i->second,operation,initial_value);
274    }    }
275    //    // perform the operation on the default data value
276    // finally perform the operation on the default value    // and assign this to the default element in result
277    // assign this to corresponding element in result    resultView.getData(0) =
278    data.getDefaultValue().dp_algorithm(operation);      data.getDefaultValue().reductionOp(operation,initial_value);
279  }  }
280    
281  template <class UnaryFunction>  template <class BinaryFunction>
282  inline  inline
283  void  void
284  dp_algorithm(DataConstant& result,  dp_algorithm(DataConstant& data,
285               DataConstant& data,               DataConstant& result,
286               UnaryFunction operation)               BinaryFunction operation,
287  {           double initial_value)
288    //result.getPointDataView().getData()  {
289    // assign this to corresponding element in result    // perform the operation on the data value
290    data.getPointDataView().dp_algorithm(operation);    // and assign this to the element in result
291      result.getPointDataView().getData(0) =
292        data.getPointDataView().reductionOp(operation,initial_value);
293  }  }
294    
295  } // end of namespace  } // end of namespace
296    
297  #endif  #endif

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

  ViewVC Help
Powered by ViewVC 1.1.26