/[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 106 by jgs, Thu Dec 23 07:20:12 2004 UTC trunk/escript/src/DataAlgorithm.h revision 478 by jgs, Tue Jan 31 02:21:49 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 "DataArrayView.h"
20  #include "escript/Data/DataTagged.h"  #include "DataExpanded.h"
21  #include "escript/Data/DataConstant.h"  #include "DataTagged.h"
22  #include "escript/Data/DataArrayView.h"  #include "DataConstant.h"
23    
24  #include <iostream>  #include <iostream>
25  #include <algorithm>  #include <algorithm>
26  #include <math.h>  #include <math.h>
 #include <limits>  
27    
28  namespace escript {  namespace escript {
29    
30  /**  /**
31     \brief     \brief
32     Return the maximum value.     Adapt binary algorithms so they may be used in DataArrayView reduction operations.
33    
34     Description:     Description:
35     Return the maximum value.     This functor adapts the given BinaryFunction operation by starting with the
36       given inital value applying this operation to successive values, storing the
37       rolling result in m_currentValue - which can be accessed or reset by getResult
38       and resetResult respectively.
39    */
40    template <class BinaryFunction>
41    class DataAlgorithmAdapter {
42      public:
43        DataAlgorithmAdapter(double initialValue):
44          m_initialValue(initialValue),
45          m_currentValue(initialValue)
46        {
47        }
48        DataAlgorithmAdapter(const DataAlgorithmAdapter& other):
49          m_initialValue(other.m_initialValue),
50          m_currentValue(other.m_initialValue),
51          operation(other.operation)
52        {
53        }
54        inline void operator()(double value)
55        {
56          m_currentValue=operation(m_currentValue,value);
57          return;
58        }
59        inline void resetResult()
60        {
61          m_currentValue=m_initialValue;
62        }
63        inline double getResult() const
64        {
65          return m_currentValue;
66        }
67      private:
68        //
69        // the initial operation value
70        double m_initialValue;
71        //
72        // the current operation value
73        double m_currentValue;
74        //
75        // The operation to perform
76        BinaryFunction operation;
77    };
78    
79    /**
80       \brief
81       Return the maximum value of the two given values.
82  */  */
83  struct FMax : public std::binary_function<double,double,double>  struct FMax : public std::binary_function<double,double,double>
84  {  {
# Line 45  struct FMax : public std::binary_functio Line 90  struct FMax : public std::binary_functio
90    
91  /**  /**
92     \brief     \brief
93     Return the minimum value.     Return the minimum value of the two given values.
   
    Description:  
    Return the minimum value.  
94  */  */
95  struct FMin : public std::binary_function<double,double,double>  struct FMin : public std::binary_function<double,double,double>
96  {  {
# Line 60  struct FMin : public std::binary_functio Line 102  struct FMin : public std::binary_functio
102    
103  /**  /**
104     \brief     \brief
105     Return the absolute maximum value.     Return the absolute maximum value of the two given values.
   
    Description:  
    Return the absolute maximum value.  
106  */  */
107  struct AbsMax : public std::binary_function<double,double,double>  struct AbsMax : public std::binary_function<double,double,double>
108  {  {
# Line 75  struct AbsMax : public std::binary_funct Line 114  struct AbsMax : public std::binary_funct
114    
115  /**  /**
116     \brief     \brief
117     Return the length.     Return the absolute minimum value of the two given values.
118    */
119    struct AbsMin : public std::binary_function<double,double,double>
120    {
121      inline double operator()(double x, double y) const
122      {
123        return std::min(fabs(x),fabs(y));
124      }
125    };
126    
127     Description:  /**
128     Return the length.     \brief
129       Return the length between the two given values.
130  */  */
131  struct Length : public std::binary_function<double,double,double>  struct Length : public std::binary_function<double,double,double>
132  {  {
# Line 90  struct Length : public std::binary_funct Line 138  struct Length : public std::binary_funct
138    
139  /**  /**
140     \brief     \brief
141     Return the trace.     Return the trace of the two given values.
   
    Description:  
    Return the trace.  
142  */  */
143  struct Trace : public std::binary_function<double,double,double>  struct Trace : public std::binary_function<double,double,double>
144  {  {
# Line 105  struct Trace : public std::binary_functi Line 150  struct Trace : public std::binary_functi
150    
151  /**  /**
152     \brief     \brief
153     Adapt algorithms so they may be used by Data.     Perform the given operation upon all values in all data-points in the
154       given Data object and return the final result.
155    
156     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.  
157  */  */
158  template <class BinaryFunction>  template <class BinaryFunction>
 class DataAlgorithmAdapter {  
  public:  
     DataAlgorithmAdapter(double initialValue):  
       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>  
159  inline  inline
160  double  double
161  algorithm(DataExpanded& data,  algorithm(DataExpanded& data,
162            UnaryFunction operation)            BinaryFunction operation,
163          double initial_value)
164  {  {
165    int i,j;    int i,j;
166    DataArrayView::ValueType::size_type numDPPSample=data.getNumDPPSample();    int numDPPSample=data.getNumDPPSample();
167    DataArrayView::ValueType::size_type numSamples=data.getNumSamples();    int numSamples=data.getNumSamples();
168    double resultLocal=0;    double global_current_value=initial_value;
169  #pragma omp parallel private(resultLocal)    double local_current_value;
170      DataArrayView dataView=data.getPointDataView();
171      // calculate the reduction operation value for each data point
172      // reducing the result for each data-point into the current_value variables
173      #pragma omp parallel private(local_current_value)
174    {    {
175  #pragma omp for private(i,j) schedule(static)        local_current_value=initial_value;
176      for (i=0;i<numSamples;i++) {        #pragma omp for private(i,j) schedule(static)
177        for (j=0;j<numDPPSample;j++) {        for (i=0;i<numSamples;i++) {
178      resultLocal=data.getPointDataView().algorithm(data.getPointOffset(i,j), operation);          for (j=0;j<numDPPSample;j++) {
179  #pragma omp critical (algorithm)            local_current_value=operation(local_current_value,dataView.reductionOp(data.getPointOffset(i,j),operation,initial_value));
180      operation(resultLocal);          }
181        }        }
182      }        #pragma omp critical
183          global_current_value=operation(global_current_value,local_current_value);
184    }    }
185    return operation.getResult();    return global_current_value;
186  }  }
187    
188  template <class UnaryFunction>  template <class BinaryFunction>
189  inline  inline
190  double  double
191  algorithm(DataTagged& data,  algorithm(DataTagged& data,
192            UnaryFunction operation)            BinaryFunction operation,
193          double initial_value)
194  {  {
   //  
   // perform the operation on each tagged value  
195    const DataTagged::DataMapType& lookup=data.getTagLookup();    const DataTagged::DataMapType& lookup=data.getTagLookup();
196    DataTagged::DataMapType::const_iterator i;    DataTagged::DataMapType::const_iterator i;
197    DataTagged::DataMapType::const_iterator lookupEnd=lookup.end();    DataTagged::DataMapType::const_iterator lookupEnd=lookup.end();
198    DataArrayView& dataView=data.getPointDataView();    DataArrayView& dataView=data.getPointDataView();
199      double current_value=initial_value;
200      // perform the operation on each tagged value
201    for (i=lookup.begin();i!=lookupEnd;i++) {    for (i=lookup.begin();i!=lookupEnd;i++) {
202      operation(dataView.algorithm(i->second,operation));      current_value=operation(current_value,dataView.reductionOp(i->second,operation,initial_value));
203    }    }
204    //    // perform the operation on the default value
205    // finally perform the operation on the default value    current_value=operation(current_value,data.getDefaultValue().reductionOp(operation,initial_value));
206    operation(data.getDefaultValue().algorithm(operation));    return current_value;
   return operation.getResult();  
207  }  }
208    
209  template <class UnaryFunction>  template <class BinaryFunction>
210  inline  inline
211  double  double
212  algorithm(DataConstant& data,  algorithm(DataConstant& data,
213            UnaryFunction operation)            BinaryFunction operation,
214          double initial_value)
215  {  {
216    return data.getPointDataView().algorithm(operation);    return data.getPointDataView().reductionOp(operation,initial_value);
217  }  }
218    
219  /**  /**
220     \brief     \brief
221     Perform the given data point reduction operation on all data points     Perform the given data-point reduction operation on all data-points
222     in data, storing results in corresponding elements of result.     in data, storing results in corresponding data-points of result.
223    
224     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
225     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
226     data points of rank 0.     data points of rank 0.
227    
228     Calls DataArrayView::dp_algorithm.     Calls DataArrayView::reductionOp
229  */  */
230  template <class UnaryFunction>  template <class BinaryFunction>
231  inline  inline
232  void  void
233  dp_algorithm(DataExpanded& data,  dp_algorithm(DataExpanded& data,
234               DataExpanded& result,               DataExpanded& result,
235               UnaryFunction operation)               BinaryFunction operation,
236             double initial_value)
237  {  {
   //  
   // perform the operation on each data value  
   // and assign this to the corresponding element in result  
238    int i,j;    int i,j;
239    DataArrayView::ValueType::size_type numDPPSample=data.getNumDPPSample();    int numSamples=data.getNumSamples();
240    DataArrayView::ValueType::size_type numSamples=data.getNumSamples();    int numDPPSample=data.getNumDPPSample();
241    {    DataArrayView dataView=data.getPointDataView();
242  #pragma omp for private(i,j) schedule(static)    DataArrayView resultView=result.getPointDataView();
243      for (i=0;i<numSamples;i++) {    // perform the operation on each data-point and assign
244        for (j=0;j<numDPPSample;j++) {    // this to the corresponding element in result
245  #pragma omp critical (dp_algorithm)    #pragma omp parallel for private(i,j) schedule(static)
246          result.getPointDataView().getData(data.getPointOffset(i,j)) =    for (i=0;i<numSamples;i++) {
247            data.getPointDataView().dp_algorithm(data.getPointOffset(i,j),operation);      for (j=0;j<numDPPSample;j++) {
248        }        resultView.getData(result.getPointOffset(i,j)) =
249            dataView.reductionOp(data.getPointOffset(i,j),operation,initial_value);
250      }      }
251    }    }
252  }  }
253    
254  template <class UnaryFunction>  template <class BinaryFunction>
255  inline  inline
256  void  void
257  dp_algorithm(DataTagged& data,  dp_algorithm(DataTagged& data,
258               DataTagged& result,               DataTagged& result,
259               UnaryFunction operation)               BinaryFunction operation,
260             double initial_value)
261  {  {
   //  
   // perform the operation on each tagged data value  
   // and assign this to the corresponding element in result  
262    const DataTagged::DataMapType& lookup=data.getTagLookup();    const DataTagged::DataMapType& lookup=data.getTagLookup();
263    DataTagged::DataMapType::const_iterator i;    DataTagged::DataMapType::const_iterator i;
264    DataTagged::DataMapType::const_iterator lookupEnd=lookup.end();    DataTagged::DataMapType::const_iterator lookupEnd=lookup.end();
265      DataArrayView dataView=data.getPointDataView();
266      DataArrayView resultView=result.getPointDataView();
267      // perform the operation on each tagged data value
268      // and assign this to the corresponding element in result
269    for (i=lookup.begin();i!=lookupEnd;i++) {    for (i=lookup.begin();i!=lookupEnd;i++) {
270      result.getPointDataView().getData(i->second) =      resultView.getData(i->second) =
271        data.getPointDataView().dp_algorithm(i->second,operation);        dataView.reductionOp(i->second,operation,initial_value);
272    }    }
273    //    // perform the operation on the default data value
   // finally perform the operation on the default data value  
274    // and assign this to the default element in result    // and assign this to the default element in result
275    result.getPointDataView().getData(0) =    resultView.getData(0) =
276      data.getDefaultValue().dp_algorithm(operation);      data.getDefaultValue().reductionOp(operation,initial_value);
277  }  }
278    
279  template <class UnaryFunction>  template <class BinaryFunction>
280  inline  inline
281  void  void
282  dp_algorithm(DataConstant& data,  dp_algorithm(DataConstant& data,
283               DataConstant& result,               DataConstant& result,
284               UnaryFunction operation)               BinaryFunction operation,
285             double initial_value)
286  {  {
287    //    // perform the operation on the data value
288    // 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  
289    result.getPointDataView().getData(0) =    result.getPointDataView().getData(0) =
290      data.getPointDataView().dp_algorithm(operation);      data.getPointDataView().reductionOp(operation,initial_value);
291  }  }
292    
293  } // end of namespace  } // end of namespace
294    
295  #endif  #endif

Legend:
Removed from v.106  
changed lines
  Added in v.478

  ViewVC Help
Powered by ViewVC 1.1.26