/[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 113 by jgs, Mon Feb 28 07:06:33 2005 UTC trunk/escript/src/DataAlgorithm.h revision 757 by woo409, Mon Jun 26 13:12:56 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    #include "system_dep.h"
17    
18  #include "escript/Data/DataExpanded.h"  #include "DataArrayView.h"
19  #include "escript/Data/DataTagged.h"  #include "DataExpanded.h"
20  #include "escript/Data/DataConstant.h"  #include "DataTagged.h"
21  #include "escript/Data/DataArrayView.h"  #include "DataConstant.h"
22    
23  #include <iostream>  #include <iostream>
24  #include <algorithm>  #include <algorithm>
 #include <math.h>  
 #include <limits>  
 #include <vector>  
25    
26  namespace escript {  namespace escript {
27    
# Line 47  class DataAlgorithmAdapter { Line 43  class DataAlgorithmAdapter {
43        m_currentValue(initialValue)        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)      inline void operator()(double value)
53      {      {
54        m_currentValue=operation(m_currentValue,value);        m_currentValue=operation(m_currentValue,value);
# Line 110  struct AbsMax : public std::binary_funct Line 112  struct AbsMax : public std::binary_funct
112    
113  /**  /**
114     \brief     \brief
115       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    /**
126       \brief
127     Return the length between the two given values.     Return the length between the two given values.
128  */  */
129  struct Length : public std::binary_function<double,double,double>  struct Length : public std::binary_function<double,double,double>
# Line 139  struct Trace : public std::binary_functi Line 153  struct Trace : public std::binary_functi
153    
154     Calls DataArrayView::reductionOp     Calls DataArrayView::reductionOp
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    int numDPPSample=data.getNumDPPSample();    int numDPPSample=data.getNumDPPSample();
165    int numSamples=data.getNumSamples();    int numSamples=data.getNumSamples();
166    int resultVectorLength=numDPPSample*numSamples;    double global_current_value=initial_value;
167    std::vector<double> resultVector(resultVectorLength);    double local_current_value;
168    DataArrayView dataView=data.getPointDataView();    DataArrayView dataView=data.getPointDataView();
169    // calculate the reduction operation value for each data point    // calculate the reduction operation value for each data point
170    // storing the result for each data-point in successive entries    // reducing the result for each data-point into the current_value variables
171    // in resultVector    #pragma omp parallel private(local_current_value)
172    {    {
173  #pragma omp for private(i,j) schedule(static)        local_current_value=initial_value;
174      for (i=0;i<numSamples;i++) {        #pragma omp for private(i,j) schedule(static)
175        for (j=0;j<numDPPSample;j++) {        for (i=0;i<numSamples;i++) {
176  #pragma omp critical (reductionOp)          for (j=0;j<numDPPSample;j++) {
177      resultVector[j*numSamples+i]=dataView.reductionOp(data.getPointOffset(i,j),operation);            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    // now calculate the reduction operation value across the results    return global_current_value;
   // for each data-point  
   operation.resetResult();  
   for (int l=0;l<resultVectorLength;l++) {  
     operation(resultVector[l]);  
   }  
   return operation.getResult();  
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    const DataTagged::DataMapType& lookup=data.getTagLookup();    double current_value=initial_value;
   DataTagged::DataMapType::const_iterator i;  
   DataTagged::DataMapType::const_iterator lookupEnd=lookup.end();  
   DataArrayView& dataView=data.getPointDataView();  
   std::vector<double> resultVector;  
   int resultVectorLength;  
194    // perform the operation on each tagged value    // perform the operation on each tagged value
195    for (i=lookup.begin();i!=lookupEnd;i++) {    DataArrayView& dataView=data.getPointDataView();
196      resultVector.push_back(dataView.reductionOp(i->second,operation));    const DataTagged::DataMapType& lookup=data.getTagLookup();
197      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    // perform the operation on the default value
201    resultVector.push_back(data.getDefaultValue().reductionOp(operation));    current_value=operation(current_value,data.getDefaultValue().reductionOp(operation,initial_value));
202    // now calculate the reduction operation value across the results    return current_value;
   // for each tagged value  
   resultVectorLength=resultVector.size();  
   operation.resetResult();  
   for (int l=0;l<resultVectorLength;l++) {  
     operation(resultVector[l]);  
   }  
   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().reductionOp(operation);    return data.getPointDataView().reductionOp(operation,initial_value);
213  }  }
214    
215  /**  /**
# Line 220  algorithm(DataConstant& data, Line 223  algorithm(DataConstant& data,
223    
224     Calls DataArrayView::reductionOp     Calls DataArrayView::reductionOp
225  */  */
226  template <class UnaryFunction>  template <class BinaryFunction>
227  inline  inline
228  void  void
229  dp_algorithm(DataExpanded& data,  dp_algorithm(DataExpanded& data,
230               DataExpanded& result,               DataExpanded& result,
231               UnaryFunction operation)               BinaryFunction operation,
232             double initial_value)
233  {  {
234    int i,j;    int i,j;
   int numDPPSample=data.getNumDPPSample();  
235    int numSamples=data.getNumSamples();    int numSamples=data.getNumSamples();
236      int numDPPSample=data.getNumDPPSample();
237    DataArrayView dataView=data.getPointDataView();    DataArrayView dataView=data.getPointDataView();
238    DataArrayView resultView=result.getPointDataView();    DataArrayView resultView=result.getPointDataView();
239    // perform the operation on each data-point and assign    // perform the operation on each data-point and assign
240    // this to the corresponding element in result    // this to the corresponding element in result
241    {    #pragma omp parallel for private(i,j) schedule(static)
242  #pragma omp for private(i,j) schedule(static)    for (i=0;i<numSamples;i++) {
243      for (i=0;i<numSamples;i++) {      for (j=0;j<numDPPSample;j++) {
244        for (j=0;j<numDPPSample;j++) {        resultView.getData(result.getPointOffset(i,j)) =
245  #pragma omp critical (reductionOp)          dataView.reductionOp(data.getPointOffset(i,j),operation,initial_value);
         resultView.getData(data.getPointOffset(i,j)) =  
           dataView.reductionOp(data.getPointOffset(i,j),operation);  
       }  
246      }      }
247    }    }
248  }  }
249    
250  template <class UnaryFunction>  template <class BinaryFunction>
251  inline  inline
252  void  void
253  dp_algorithm(DataTagged& data,  dp_algorithm(DataTagged& data,
254               DataTagged& result,               DataTagged& result,
255               UnaryFunction operation)               BinaryFunction operation,
256             double initial_value)
257  {  {
258    const DataTagged::DataMapType& lookup=data.getTagLookup();    // perform the operation on each tagged value in data
   DataTagged::DataMapType::const_iterator i;  
   DataTagged::DataMapType::const_iterator lookupEnd=lookup.end();  
   DataArrayView dataView=data.getPointDataView();  
   DataArrayView resultView=result.getPointDataView();  
   // perform the operation on each tagged data value  
259    // and assign this to the corresponding element in result    // and assign this to the corresponding element in result
260    for (i=lookup.begin();i!=lookupEnd;i++) {    const DataTagged::DataMapType& lookup=data.getTagLookup();
261      resultView.getData(i->second) =    for (DataTagged::DataMapType::const_iterator i=lookup.begin(); i!=lookup.end(); i++) {
262        dataView.reductionOp(i->second,operation);      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    // perform the operation on the default data value
266    // and assign this to the default element in result    // and assign this to the default element in result
267    resultView.getData(0) =    result.getDefaultValue().getData(0) =
268      data.getDefaultValue().reductionOp(operation);      data.getDefaultValue().reductionOp(operation,initial_value);
269  }  }
270    
271  template <class UnaryFunction>  template <class BinaryFunction>
272  inline  inline
273  void  void
274  dp_algorithm(DataConstant& data,  dp_algorithm(DataConstant& data,
275               DataConstant& result,               DataConstant& result,
276               UnaryFunction operation)               BinaryFunction operation,
277             double initial_value)
278  {  {
279    // perform the operation on the data value    // perform the operation on the data value
280    // and assign this to the element in result    // and assign this to the element in result
281    result.getPointDataView().getData(0) =    result.getPointDataView().getData(0) =
282      data.getPointDataView().reductionOp(operation);      data.getPointDataView().reductionOp(operation,initial_value);
283  }  }
284    
285  } // end of namespace  } // end of namespace
286    
287  #endif  #endif

Legend:
Removed from v.113  
changed lines
  Added in v.757

  ViewVC Help
Powered by ViewVC 1.1.26