/[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 615 by elspeth, Wed Mar 22 02:12:00 2006 UTC
# Line 1  Line 1 
1  // $Id$  // $Id$
2  /*  /*
3   ******************************************************************************   ************************************************************
4   *                                                                            *   *          Copyright 2006 by ACcESS MNRF                   *
5   *       COPYRIGHT  ACcESS 2004 -  All Rights Reserved                        *   *                                                          *
6   *                                                                            *   *              http://www.access.edu.au                    *
7   * This software is the property of ACcESS. No part of this code              *   *       Primary Business: Queensland, Australia            *
8   * may be copied in any form or by any means without the expressed written    *   *  Licensed under the Open Software License version 3.0    *
9   * consent of ACcESS.  Copying, use or modification of this software          *   *     http://www.opensource.org/licenses/osl-3.0.php       *
10   * by any unauthorised person is illegal unless that person has a software    *   *                                                          *
11   * license agreement with ACcESS.                                             *   ************************************************************
  *                                                                            *  
  ******************************************************************************  
12  */  */
13                                                                                                                                                        
14  #if !defined escript_DataAlgorithm_20040714_H  #if !defined escript_DataAlgorithm_20040714_H
15  #define escript_DataAlgorithm_20040714_H  #define escript_DataAlgorithm_20040714_H
16    
17  #include "escript/Data/DataExpanded.h"  #include "DataArrayView.h"
18  #include "escript/Data/DataTagged.h"  #include "DataExpanded.h"
19  #include "escript/Data/DataConstant.h"  #include "DataTagged.h"
20  #include "escript/Data/DataArrayView.h"  #include "DataConstant.h"
21    
22  #include <iostream>  #include <iostream>
23  #include <algorithm>  #include <algorithm>
24  #include <math.h>  #include <math.h>
 #include <limits>  
25    
26  namespace escript {  namespace escript {
27    
28  /**  /**
29     \brief     \brief
30     Return the maximum value.     Adapt binary algorithms so they may be used in DataArrayView reduction operations.
31    
32     Description:     Description:
33     Return the maximum value.     This functor adapts the given BinaryFunction operation by starting with the
34       given inital value applying this operation to successive values, storing the
35       rolling result in m_currentValue - which can be accessed or reset by getResult
36       and resetResult respectively.
37    */
38    template <class BinaryFunction>
39    class DataAlgorithmAdapter {
40      public:
41        DataAlgorithmAdapter(double initialValue):
42          m_initialValue(initialValue),
43          m_currentValue(initialValue)
44        {
45        }
46        DataAlgorithmAdapter(const DataAlgorithmAdapter& other):
47          m_initialValue(other.m_initialValue),
48          m_currentValue(other.m_initialValue),
49          operation(other.operation)
50        {
51        }
52        inline void operator()(double value)
53        {
54          m_currentValue=operation(m_currentValue,value);
55          return;
56        }
57        inline void resetResult()
58        {
59          m_currentValue=m_initialValue;
60        }
61        inline double getResult() const
62        {
63          return m_currentValue;
64        }
65      private:
66        //
67        // the initial operation value
68        double m_initialValue;
69        //
70        // the current operation value
71        double m_currentValue;
72        //
73        // The operation to perform
74        BinaryFunction operation;
75    };
76    
77    /**
78       \brief
79       Return the maximum value of the two given values.
80  */  */
81  struct FMax : public std::binary_function<double,double,double>  struct FMax : public std::binary_function<double,double,double>
82  {  {
# Line 45  struct FMax : public std::binary_functio Line 88  struct FMax : public std::binary_functio
88    
89  /**  /**
90     \brief     \brief
91     Return the minimum value.     Return the minimum value of the two given values.
   
    Description:  
    Return the minimum value.  
92  */  */
93  struct FMin : public std::binary_function<double,double,double>  struct FMin : public std::binary_function<double,double,double>
94  {  {
# Line 60  struct FMin : public std::binary_functio Line 100  struct FMin : public std::binary_functio
100    
101  /**  /**
102     \brief     \brief
103     Return the absolute maximum value.     Return the absolute maximum value of the two given values.
   
    Description:  
    Return the absolute maximum value.  
104  */  */
105  struct AbsMax : public std::binary_function<double,double,double>  struct AbsMax : public std::binary_function<double,double,double>
106  {  {
# Line 75  struct AbsMax : public std::binary_funct Line 112  struct AbsMax : public std::binary_funct
112    
113  /**  /**
114     \brief     \brief
115     Adapt algorithms so they may be used by Data.     Return the absolute minimum value of the two given values.
116    */
117    struct AbsMin : public std::binary_function<double,double,double>
118    {
119      inline double operator()(double x, double y) const
120      {
121        return std::min(fabs(x),fabs(y));
122      }
123    };
124    
125     Description:  /**
126     Adapt algorithms so they may be used by Data. The functor     \brief
127     maintains state, ie the currentValue retuned by the operation.     Return the length between the two given values.
128  */  */
129  template <class BinaryFunction>  struct Length : public std::binary_function<double,double,double>
130  class DataAlgorithmAdapter {  {
131   public:    inline double operator()(double x, double y) const
132      DataAlgorithmAdapter(double initialValue):    {
133        m_currentValue(initialValue)      return std::sqrt(std::pow(x,2)+std::pow(y,2));
134      {}    }
135      inline void operator()(double value)  };
136      {  
137        m_currentValue=operation(m_currentValue,value);  /**
138        return;     \brief
139      }     Return the trace of the two given values.
140      inline double getResult() const  */
141      {  struct Trace : public std::binary_function<double,double,double>
142        return m_currentValue;  {
143      }    inline double operator()(double x, double y) const
144   private:    {
145      //      return x+y;
146      // the current operation value    }
     double m_currentValue;  
     //  
     // The operation to perform  
     BinaryFunction operation;  
147  };  };
148    
149  /**  /**
150     \brief     \brief
151     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
152     result.     given Data object and return the final result.
153    
154     Description:     Calls DataArrayView::reductionOp
    Perform the given operation upon all Data elements and return a single  
    result.  
155  */  */
156  template <class UnaryFunction>  template <class BinaryFunction>
157  inline  inline
158  double  double
159  algorithm(DataExpanded& data,  algorithm(DataExpanded& data,
160            UnaryFunction operation)            BinaryFunction operation,
161          double initial_value)
162  {  {
163    int i,j;    int i,j;
164    DataArrayView::ValueType::size_type numDPPSample=data.getNumDPPSample();    int numDPPSample=data.getNumDPPSample();
165    DataArrayView::ValueType::size_type numSamples=data.getNumSamples();    int numSamples=data.getNumSamples();
166    double resultLocal=0;    double global_current_value=initial_value;
167  #pragma omp parallel private(resultLocal)    double local_current_value;
168    {    DataArrayView dataView=data.getPointDataView();
169  #pragma omp for private(i,j) schedule(static)    // calculate the reduction operation value for each data point
170      for (i=0;i<numSamples;i++) {    // reducing the result for each data-point into the current_value variables
171        for (j=0;j<numDPPSample;j++) {    #pragma omp parallel private(local_current_value)
172      resultLocal=data.getPointDataView().algorithm(data.getPointOffset(i,j), operation);    {
173  #pragma omp critical (algorithm)        local_current_value=initial_value;
174      operation(resultLocal);        #pragma omp for private(i,j) schedule(static)
175          for (i=0;i<numSamples;i++) {
176            for (j=0;j<numDPPSample;j++) {
177              local_current_value=operation(local_current_value,dataView.reductionOp(data.getPointOffset(i,j),operation,initial_value));
178            }
179        }        }
180      }        #pragma omp critical
181          global_current_value=operation(global_current_value,local_current_value);
182    }    }
183    return operation.getResult();    return global_current_value;
184  }  }
185    
186  template <class UnaryFunction>  template <class BinaryFunction>
187  inline  inline
188  double  double
189  algorithm(DataTagged& data,  algorithm(DataTagged& data,
190            UnaryFunction operation)            BinaryFunction operation,
191          double initial_value)
192  {  {
193    //    double current_value=initial_value;
194    // perform the operation on each tagged value    // perform the operation on each tagged value
   const DataTagged::DataMapType& lookup=data.getTagLookup();  
   DataTagged::DataMapType::const_iterator i;  
   DataTagged::DataMapType::const_iterator lookupEnd=lookup.end();  
195    DataArrayView& dataView=data.getPointDataView();    DataArrayView& dataView=data.getPointDataView();
196    for (i=lookup.begin();i!=lookupEnd;i++) {    const DataTagged::DataMapType& lookup=data.getTagLookup();
197      operation(dataView.algorithm(i->second,operation));    for (DataTagged::DataMapType::const_iterator i=lookup.begin(); i!=lookup.end(); i++) {
198        current_value=operation(current_value,dataView.reductionOp(i->second,operation,initial_value));
199    }    }
200    //    // perform the operation on the default value
201    // finally perform the operation on the default value    current_value=operation(current_value,data.getDefaultValue().reductionOp(operation,initial_value));
202    operation(data.getDefaultValue().algorithm(operation));    return current_value;
   return operation.getResult();  
203  }  }
204    
205  template <class UnaryFunction>  template <class BinaryFunction>
206  inline  inline
207  double  double
208  algorithm(DataConstant& data,  algorithm(DataConstant& data,
209            UnaryFunction operation)            BinaryFunction operation,
210          double initial_value)
211  {  {
212    return data.getPointDataView().algorithm(operation);    return data.getPointDataView().reductionOp(operation,initial_value);
213  }  }
214    
215  /**  /**
216     \brief     \brief
217     Perform the given data point reduction operation upon all data points     Perform the given data-point reduction operation on all data-points
218     in data, storing results in corresponding elements of result.     in data, storing results in corresponding data-points of result.
219    
220     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
221     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
222     points of rank n, result must have data points of rank 0.     data points of rank 0.
223    
224     Calls DataArrayView::dp_algorithm.     Calls DataArrayView::reductionOp
225  */  */
226  template <class UnaryFunction>  template <class BinaryFunction>
227  inline  inline
228  void  void
229  dp_algorithm(DataExpanded& result,  dp_algorithm(DataExpanded& data,
230               DataExpanded& data,               DataExpanded& result,
231               UnaryFunction operation)               BinaryFunction operation,
232             double initial_value)
233  {  {
234    int i,j;    int i,j;
235    DataArrayView::ValueType::size_type numDPPSample=data.getNumDPPSample();    int numSamples=data.getNumSamples();
236    DataArrayView::ValueType::size_type numSamples=data.getNumSamples();    int numDPPSample=data.getNumDPPSample();
237    {    DataArrayView dataView=data.getPointDataView();
238  #pragma omp for private(i,j) schedule(static)    DataArrayView resultView=result.getPointDataView();
239      for (i=0;i<numSamples;i++) {    // perform the operation on each data-point and assign
240        for (j=0;j<numDPPSample;j++) {    // this to the corresponding element in result
241          // assign this to corresponding element in result    #pragma omp parallel for private(i,j) schedule(static)
242      data.getPointDataView().dp_algorithm(data.getPointOffset(i,j),operation);    for (i=0;i<numSamples;i++) {
243  #pragma omp critical (dp_algorithm)      for (j=0;j<numDPPSample;j++) {
244        }        resultView.getData(result.getPointOffset(i,j)) =
245            dataView.reductionOp(data.getPointOffset(i,j),operation,initial_value);
246      }      }
247    }    }
248  }  }
249    
250  template <class UnaryFunction>  template <class BinaryFunction>
251  inline  inline
252  void  void
253  dp_algorithm(DataTagged& result,  dp_algorithm(DataTagged& data,
254               DataTagged& data,               DataTagged& result,
255               UnaryFunction operation)               BinaryFunction operation,
256             double initial_value)
257  {  {
258    //    // perform the operation on each tagged value in data
259    // perform the operation on each tagged value    // and assign this to the corresponding element in result
260    const DataTagged::DataMapType& lookup=data.getTagLookup();    const DataTagged::DataMapType& lookup=data.getTagLookup();
261    DataTagged::DataMapType::const_iterator i;    for (DataTagged::DataMapType::const_iterator i=lookup.begin(); i!=lookup.end(); i++) {
262    DataTagged::DataMapType::const_iterator lookupEnd=lookup.end();      result.getDataPointByTag(i->first).getData(0) =
263    for (i=lookup.begin();i!=lookupEnd;i++) {        data.getDataPointByTag(i->first).reductionOp(operation,initial_value);
264      // assign this to corresponding element in result    }
265      data.getPointDataView().dp_algorithm(i->second,operation);    // perform the operation on the default data value
266    }    // and assign this to the default element in result
267    //    result.getDefaultValue().getData(0) =
268    // finally perform the operation on the default value      data.getDefaultValue().reductionOp(operation,initial_value);
   // assign this to corresponding element in result  
   data.getDefaultValue().dp_algorithm(operation);  
269  }  }
270    
271  template <class UnaryFunction>  template <class BinaryFunction>
272  inline  inline
273  void  void
274  dp_algorithm(DataConstant& result,  dp_algorithm(DataConstant& data,
275               DataConstant& data,               DataConstant& result,
276               UnaryFunction operation)               BinaryFunction operation,
277  {           double initial_value)
278    //result.getPointDataView().getData()  {
279    // assign this to corresponding element in result    // perform the operation on the data value
280    data.getPointDataView().dp_algorithm(operation);    // and assign this to the element in result
281      result.getPointDataView().getData(0) =
282        data.getPointDataView().reductionOp(operation,initial_value);
283  }  }
284    
285  } // end of namespace  } // end of namespace
286    
287  #endif  #endif

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

  ViewVC Help
Powered by ViewVC 1.1.26