/[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 100 by jgs, Wed Dec 15 03:48:48 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$
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 43  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 58  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 70  struct AbsMax : public std::binary_funct Line 109  struct AbsMax : public std::binary_funct
109      return std::max(fabs(x),fabs(y));      return std::max(fabs(x),fabs(y));
110    }    }
111  };  };
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      {}    }
     inline void operator()(double value)  
     {  
       m_currentValue=operation(m_currentValue,value);  
       return;  
     }  
     inline double getResult() const  
     {  
       return m_currentValue;  
     }  
  private:  
     //  
     // the current maximum value  
     double m_currentValue;  
     //  
     // The operation to perform  
     BinaryFunction operation;  
135  };  };
136    
137  /**  /**
138     \brief     \brief
139     Perform the given operation upon all DataElements and return a single     Return the trace of the two given values.
140     result.  */
141    struct Trace : public std::binary_function<double,double,double>
142    {
143      inline double operator()(double x, double y) const
144      {
145        return x+y;
146      }
147    };
148    
149     Description:  /**
150     Perform the given operation upon all DataElements and return a single     \brief
151     result.     Perform the given operation upon all values in all data-points in the
152       given Data object and return the final result.
153    
154       Calls DataArrayView::reductionOp
155  */  */
156  template <class UnaryFunction>  template <class BinaryFunction>
157  inline double algorithm(DataExpanded& data, UnaryFunction operation)  inline
158    double
159    algorithm(DataExpanded& data,
160              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;    double global_current_value=initial_value;
167    #pragma omp parallel private(resultLocal)    double local_current_value;
168    {    DataArrayView dataView=data.getPointDataView();
169      #pragma omp parallel 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),    {
173                                operation);        local_current_value=initial_value;
174      #pragma omp critical (algorithm)        #pragma omp for private(i,j) schedule(static)
175      operation(resultLocal);        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 double algorithm(DataTagged& data, UnaryFunction operation)  inline
188    double
189    algorithm(DataTagged& data,
190              BinaryFunction operation,
191          double initial_value)
192  {  {
193    //    double current_value=initial_value;
194    // perform the operation on each tagged value including the default    // 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      current_value=operation(current_value,data.getDefaultValue().reductionOp(operation,initial_value));
202      return current_value;
203    }
204    
205    template <class BinaryFunction>
206    inline
207    double
208    algorithm(DataConstant& data,
209              BinaryFunction operation,
210          double initial_value)
211    {
212      return data.getPointDataView().reductionOp(operation,initial_value);
213    }
214    
215    /**
216       \brief
217       Perform the given data-point reduction operation on all data-points
218       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
221       of data points, but where data has data points of rank n, result must have
222       data points of rank 0.
223    
224       Calls DataArrayView::reductionOp
225    */
226    template <class BinaryFunction>
227    inline
228    void
229    dp_algorithm(DataExpanded& data,
230                 DataExpanded& result,
231                 BinaryFunction operation,
232             double initial_value)
233    {
234      int i,j;
235      int numSamples=data.getNumSamples();
236      int numDPPSample=data.getNumDPPSample();
237      DataArrayView dataView=data.getPointDataView();
238      DataArrayView resultView=result.getPointDataView();
239      // perform the operation on each data-point and assign
240      // this to the corresponding element in result
241      #pragma omp parallel for private(i,j) schedule(static)
242      for (i=0;i<numSamples;i++) {
243        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    }    }
   //  
   // finally perform the operation on the default value  
   operation(data.getDefaultValue().algorithm(operation));  
   return operation.getResult();  
248  }  }
249    
250  template <class UnaryFunction>  template <class BinaryFunction>
251  inline double algorithm(DataConstant& data, UnaryFunction operation)  inline
252    void
253    dp_algorithm(DataTagged& data,
254                 DataTagged& result,
255                 BinaryFunction operation,
256             double initial_value)
257  {  {
258    return data.getPointDataView().algorithm(operation);    // perform the operation on each tagged value in data
259      // and assign this to the corresponding element in result
260      const DataTagged::DataMapType& lookup=data.getTagLookup();
261      for (DataTagged::DataMapType::const_iterator i=lookup.begin(); i!=lookup.end(); i++) {
262        result.getDataPointByTag(i->first).getData(0) =
263          data.getDataPointByTag(i->first).reductionOp(operation,initial_value);
264      }
265      // perform the operation on the default data value
266      // and assign this to the default element in result
267      result.getDefaultValue().getData(0) =
268        data.getDefaultValue().reductionOp(operation,initial_value);
269  }  }
270    
271    template <class BinaryFunction>
272    inline
273    void
274    dp_algorithm(DataConstant& data,
275                 DataConstant& result,
276                 BinaryFunction operation,
277             double initial_value)
278    {
279      // perform the operation on the data value
280      // 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.100  
changed lines
  Added in v.615

  ViewVC Help
Powered by ViewVC 1.1.26