/[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 1388 by trankine, Fri Jan 11 07:45:58 2008 UTC
# Line 1  Line 1 
1  // $Id$  
2  /*  /* $Id$ */
3   ******************************************************************************  
4   *                                                                            *  /*******************************************************
5   *       COPYRIGHT  ACcESS 2004 -  All Rights Reserved                        *   *
6   *                                                                            *   *           Copyright 2003-2007 by ACceSS MNRF
7   * This software is the property of ACcESS. No part of this code              *   *       Copyright 2007 by University of Queensland
8   * may be copied in any form or by any means without the expressed written    *   *
9   * consent of ACcESS.  Copying, use or modification of this software          *   *                http://esscc.uq.edu.au
10   * by any unauthorised person is illegal unless that person has a software    *   *        Primary Business: Queensland, Australia
11   * license agreement with ACcESS.                                             *   *  Licensed under the Open Software License version 3.0
12   *                                                                            *   *     http://www.opensource.org/licenses/osl-3.0.php
13   ******************************************************************************   *
14  */   *******************************************************/
15                                                                              
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    #include "system_dep.h"
19    
20  #include "escript/Data/DataExpanded.h"  #include "DataArrayView.h"
21  #include "escript/Data/DataTagged.h"  #include "DataExpanded.h"
22  #include "escript/Data/DataConstant.h"  #include "DataTagged.h"
23  #include "escript/Data/DataArrayView.h"  #include "DataConstant.h"
24    
25  #include <iostream>  #include <iostream>
26  #include <algorithm>  #include <algorithm>
 #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     Adapt algorithms so they may be used by Data.     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     Adapt algorithms so they may be used by Data. The functor     \brief
129     maintains state, ie the currentValue retuned by the operation.     Return the length between the two given values.
130  */  */
131  template <class BinaryFunction>  struct Length : public std::binary_function<double,double,double>
132  class DataAlgorithmAdapter {  {
133   public:    inline double operator()(double x, double y) const
134      DataAlgorithmAdapter(double initialValue):    {
135        m_currentValue(initialValue)      return std::sqrt(std::pow(x,2)+std::pow(y,2));
136      {}    }
137      inline void operator()(double value)  };
138      {  
139        m_currentValue=operation(m_currentValue,value);  /**
140        return;     \brief
141      }     Return the trace of the two given values.
142      inline double getResult() const  */
143      {  struct Trace : public std::binary_function<double,double,double>
144        return m_currentValue;  {
145      }    inline double operator()(double x, double y) const
146   private:    {
147      //      return x+y;
148      // the current operation value    }
     double m_currentValue;  
     //  
     // The operation to perform  
     BinaryFunction operation;  
149  };  };
150    
151  /**  /**
152     \brief     \brief
153     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
154     result.     given Data object and return the final result.
155    
156     Description:     Calls DataArrayView::reductionOp
    Perform the given operation upon all Data elements and return a single  
    result.  
157  */  */
158  template <class UnaryFunction>  template <class BinaryFunction>
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  #pragma omp for private(i,j) schedule(static)    // calculate the reduction operation value for each data point
172      for (i=0;i<numSamples;i++) {    // reducing the result for each data-point into the current_value variables
173        for (j=0;j<numDPPSample;j++) {    #pragma omp parallel private(local_current_value)
174      resultLocal=data.getPointDataView().algorithm(data.getPointOffset(i,j), operation);    {
175  #pragma omp critical (algorithm)        local_current_value=initial_value;
176      operation(resultLocal);        #pragma omp for private(i,j) schedule(static)
177          for (i=0;i<numSamples;i++) {
178            for (j=0;j<numDPPSample;j++) {
179              local_current_value=operation(local_current_value,dataView.reductionOp(data.getPointOffset(i,j),operation,initial_value));
180            }
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  {  {
195    //    double current_value=initial_value;
196    // 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();  
197    DataArrayView& dataView=data.getPointDataView();    DataArrayView& dataView=data.getPointDataView();
198    for (i=lookup.begin();i!=lookupEnd;i++) {    const DataTagged::DataMapType& lookup=data.getTagLookup();
199      operation(dataView.algorithm(i->second,operation));    for (DataTagged::DataMapType::const_iterator i=lookup.begin(); i!=lookup.end(); i++) {
200        current_value=operation(current_value,dataView.reductionOp(i->second,operation,initial_value));
201    }    }
202    //    // perform the operation on the default value
203    // finally perform the operation on the default value    current_value=operation(current_value,data.getDefaultValue().reductionOp(operation,initial_value));
204    operation(data.getDefaultValue().algorithm(operation));    return current_value;
   return operation.getResult();  
205  }  }
206    
207  template <class UnaryFunction>  template <class BinaryFunction>
208  inline  inline
209  double  double
210  algorithm(DataConstant& data,  algorithm(DataConstant& data,
211            UnaryFunction operation)            BinaryFunction operation,
212          double initial_value)
213  {  {
214    return data.getPointDataView().algorithm(operation);    return data.getPointDataView().reductionOp(operation,initial_value);
215  }  }
216    
217  /**  /**
218     \brief     \brief
219     Perform the given data point reduction operation upon all data points     Perform the given data-point reduction operation on all data-points
220     in data, storing results in corresponding elements of result.     in data, storing results in corresponding data-points of result.
221    
222     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
223     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
224     points of rank n, result must have data points of rank 0.     data points of rank 0.
225    
226     Calls DataArrayView::dp_algorithm.     Calls DataArrayView::reductionOp
227  */  */
228  template <class UnaryFunction>  template <class BinaryFunction>
229  inline  inline
230  void  void
231  dp_algorithm(DataExpanded& result,  dp_algorithm(DataExpanded& data,
232               DataExpanded& data,               DataExpanded& result,
233               UnaryFunction operation)               BinaryFunction operation,
234             double initial_value)
235  {  {
236    int i,j;    int i,j;
237    DataArrayView::ValueType::size_type numDPPSample=data.getNumDPPSample();    int numSamples=data.getNumSamples();
238    DataArrayView::ValueType::size_type numSamples=data.getNumSamples();    int numDPPSample=data.getNumDPPSample();
239    {    DataArrayView dataView=data.getPointDataView();
240  #pragma omp for private(i,j) schedule(static)    DataArrayView resultView=result.getPointDataView();
241      for (i=0;i<numSamples;i++) {    // perform the operation on each data-point and assign
242        for (j=0;j<numDPPSample;j++) {    // this to the corresponding element in result
243          // assign this to corresponding element in result    #pragma omp parallel for private(i,j) schedule(static)
244      data.getPointDataView().dp_algorithm(data.getPointOffset(i,j),operation);    for (i=0;i<numSamples;i++) {
245  #pragma omp critical (dp_algorithm)      for (j=0;j<numDPPSample;j++) {
246        }        resultView.getData(result.getPointOffset(i,j)) =
247            dataView.reductionOp(data.getPointOffset(i,j),operation,initial_value);
248      }      }
249    }    }
250  }  }
251    
252  template <class UnaryFunction>  template <class BinaryFunction>
253  inline  inline
254  void  void
255  dp_algorithm(DataTagged& result,  dp_algorithm(DataTagged& data,
256               DataTagged& data,               DataTagged& result,
257               UnaryFunction operation)               BinaryFunction operation,
258             double initial_value)
259  {  {
260    //    // perform the operation on each tagged value in data
261    // perform the operation on each tagged 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;    for (DataTagged::DataMapType::const_iterator i=lookup.begin(); i!=lookup.end(); i++) {
264    DataTagged::DataMapType::const_iterator lookupEnd=lookup.end();      result.getDataPointByTag(i->first).getData(0) =
265    for (i=lookup.begin();i!=lookupEnd;i++) {        data.getDataPointByTag(i->first).reductionOp(operation,initial_value);
266      // assign this to corresponding element in result    }
267      data.getPointDataView().dp_algorithm(i->second,operation);    // perform the operation on the default data value
268    }    // and assign this to the default element in result
269    //    result.getDefaultValue().getData(0) =
270    // 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);  
271  }  }
272    
273  template <class UnaryFunction>  template <class BinaryFunction>
274  inline  inline
275  void  void
276  dp_algorithm(DataConstant& result,  dp_algorithm(DataConstant& data,
277               DataConstant& data,               DataConstant& result,
278               UnaryFunction operation)               BinaryFunction operation,
279  {           double initial_value)
280    //result.getPointDataView().getData()  {
281    // assign this to corresponding element in result    // perform the operation on the data value
282    data.getPointDataView().dp_algorithm(operation);    // and assign this to the element in result
283      result.getPointDataView().getData(0) =
284        data.getPointDataView().reductionOp(operation,initial_value);
285  }  }
286    
287  } // end of namespace  } // end of namespace
288    
289  #endif  #endif

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

  ViewVC Help
Powered by ViewVC 1.1.26