/[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 559 by jgs, Thu Feb 23 01:43:44 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>  
 #include <vector>  
27    
28  namespace escript {  namespace escript {
29    
# Line 47  class DataAlgorithmAdapter { Line 45  class DataAlgorithmAdapter {
45        m_currentValue(initialValue)        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)      inline void operator()(double value)
55      {      {
56        m_currentValue=operation(m_currentValue,value);        m_currentValue=operation(m_currentValue,value);
# Line 110  struct AbsMax : public std::binary_funct Line 114  struct AbsMax : public std::binary_funct
114    
115  /**  /**
116     \brief     \brief
117       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    /**
128       \brief
129     Return the length between the two given values.     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>
# Line 139  struct Trace : public std::binary_functi Line 155  struct Trace : public std::binary_functi
155    
156     Calls DataArrayView::reductionOp     Calls DataArrayView::reductionOp
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    int numDPPSample=data.getNumDPPSample();    int numDPPSample=data.getNumDPPSample();
167    int numSamples=data.getNumSamples();    int numSamples=data.getNumSamples();
168    int resultVectorLength=numDPPSample*numSamples;    double global_current_value=initial_value;
169    std::vector<double> resultVector(resultVectorLength);    double local_current_value;
170    DataArrayView dataView=data.getPointDataView();    DataArrayView dataView=data.getPointDataView();
171    // calculate the reduction operation value for each data point    // calculate the reduction operation value for each data point
172    // storing the result for each data-point in successive entries    // reducing the result for each data-point into the current_value variables
173    // in resultVector    #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  #pragma omp critical (reductionOp)          for (j=0;j<numDPPSample;j++) {
179      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));
180            }
181        }        }
182      }        #pragma omp critical
183          global_current_value=operation(global_current_value,local_current_value);
184    }    }
185    // 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();  
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    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;  
196    // perform the operation on each tagged value    // perform the operation on each tagged value
197    for (i=lookup.begin();i!=lookupEnd;i++) {    DataArrayView& dataView=data.getPointDataView();
198      resultVector.push_back(dataView.reductionOp(i->second,operation));    const DataTagged::DataMapType& lookup=data.getTagLookup();
199      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    // perform the operation on the default value
203    resultVector.push_back(data.getDefaultValue().reductionOp(operation));    current_value=operation(current_value,data.getDefaultValue().reductionOp(operation,initial_value));
204    // 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();  
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().reductionOp(operation);    return data.getPointDataView().reductionOp(operation,initial_value);
215  }  }
216    
217  /**  /**
# Line 220  algorithm(DataConstant& data, Line 225  algorithm(DataConstant& data,
225    
226     Calls DataArrayView::reductionOp     Calls DataArrayView::reductionOp
227  */  */
228  template <class UnaryFunction>  template <class BinaryFunction>
229  inline  inline
230  void  void
231  dp_algorithm(DataExpanded& data,  dp_algorithm(DataExpanded& data,
232               DataExpanded& result,               DataExpanded& result,
233               UnaryFunction operation)               BinaryFunction operation,
234             double initial_value)
235  {  {
236    int i,j;    int i,j;
   int numDPPSample=data.getNumDPPSample();  
237    int numSamples=data.getNumSamples();    int numSamples=data.getNumSamples();
238      int numDPPSample=data.getNumDPPSample();
239    DataArrayView dataView=data.getPointDataView();    DataArrayView dataView=data.getPointDataView();
240    DataArrayView resultView=result.getPointDataView();    DataArrayView resultView=result.getPointDataView();
241    // perform the operation on each data-point and assign    // perform the operation on each data-point and assign
242    // this to the corresponding element in result    // this to the corresponding element in result
243    {    #pragma omp parallel for private(i,j) schedule(static)
244  #pragma omp for private(i,j) schedule(static)    for (i=0;i<numSamples;i++) {
245      for (i=0;i<numSamples;i++) {      for (j=0;j<numDPPSample;j++) {
246        for (j=0;j<numDPPSample;j++) {        resultView.getData(result.getPointOffset(i,j)) =
247  #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);  
       }  
248      }      }
249    }    }
250  }  }
251    
252  template <class UnaryFunction>  template <class BinaryFunction>
253  inline  inline
254  void  void
255  dp_algorithm(DataTagged& data,  dp_algorithm(DataTagged& data,
256               DataTagged& result,               DataTagged& result,
257               UnaryFunction operation)               BinaryFunction operation,
258             double initial_value)
259  {  {
260    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  
261    // and assign this to the corresponding element in result    // and assign this to the corresponding element in result
262    for (i=lookup.begin();i!=lookupEnd;i++) {    const DataTagged::DataMapType& lookup=data.getTagLookup();
263      resultView.getData(i->second) =    for (DataTagged::DataMapType::const_iterator i=lookup.begin(); i!=lookup.end(); i++) {
264        dataView.reductionOp(i->second,operation);      result.getDataPointByTag(i->first).getData(0) =
265          data.getDataPointByTag(i->first).reductionOp(operation,initial_value);
266    }    }
267    // perform the operation on the default data value    // perform the operation on the default data value
268    // and assign this to the default element in result    // and assign this to the default element in result
269    resultView.getData(0) =    result.getDefaultValue().getData(0) =
270      data.getDefaultValue().reductionOp(operation);      data.getDefaultValue().reductionOp(operation,initial_value);
271  }  }
272    
273  template <class UnaryFunction>  template <class BinaryFunction>
274  inline  inline
275  void  void
276  dp_algorithm(DataConstant& data,  dp_algorithm(DataConstant& data,
277               DataConstant& result,               DataConstant& result,
278               UnaryFunction operation)               BinaryFunction operation,
279             double initial_value)
280  {  {
281    // perform the operation on the data value    // perform the operation on the data value
282    // and assign this to the element in result    // and assign this to the element in result
283    result.getPointDataView().getData(0) =    result.getPointDataView().getData(0) =
284      data.getPointDataView().reductionOp(operation);      data.getPointDataView().reductionOp(operation,initial_value);
285  }  }
286    
287  } // end of namespace  } // end of namespace
288    
289  #endif  #endif

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

  ViewVC Help
Powered by ViewVC 1.1.26