/[escript]/trunk/esys2/escript/src/Data/DataAlgorithm.h
ViewVC logotype

Diff of /trunk/esys2/escript/src/Data/DataAlgorithm.h

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 108 by jgs, Thu Jan 27 06:21:59 2005 UTC revision 113 by jgs, Mon Feb 28 07:06:33 2005 UTC
# Line 25  Line 25 
25  #include <algorithm>  #include <algorithm>
26  #include <math.h>  #include <math.h>
27  #include <limits>  #include <limits>
28    #include <vector>
29    
30  namespace escript {  namespace escript {
31    
32  /**  /**
33     \brief     \brief
34     Return the maximum value.     Adapt binary algorithms so they may be used in DataArrayView reduction operations.
35    
36     Description:     Description:
37     Return the maximum value.     This functor adapts the given BinaryFunction operation by starting with the
38       given inital value applying this operation to successive values, storing the
39       rolling result in m_currentValue - which can be accessed or reset by getResult
40       and resetResult respectively.
41    */
42    template <class BinaryFunction>
43    class DataAlgorithmAdapter {
44      public:
45        DataAlgorithmAdapter(double initialValue):
46          m_initialValue(initialValue),
47          m_currentValue(initialValue)
48        {
49        }
50        inline void operator()(double value)
51        {
52          m_currentValue=operation(m_currentValue,value);
53          return;
54        }
55        inline void resetResult()
56        {
57          m_currentValue=m_initialValue;
58        }
59        inline double getResult() const
60        {
61          return m_currentValue;
62        }
63      private:
64        //
65        // the initial operation value
66        double m_initialValue;
67        //
68        // the current operation value
69        double m_currentValue;
70        //
71        // The operation to perform
72        BinaryFunction operation;
73    };
74    
75    /**
76       \brief
77       Return the maximum value of the two given values.
78  */  */
79  struct FMax : public std::binary_function<double,double,double>  struct FMax : public std::binary_function<double,double,double>
80  {  {
# Line 45  struct FMax : public std::binary_functio Line 86  struct FMax : public std::binary_functio
86    
87  /**  /**
88     \brief     \brief
89     Return the minimum value.     Return the minimum value of the two given values.
   
    Description:  
    Return the minimum value.  
90  */  */
91  struct FMin : public std::binary_function<double,double,double>  struct FMin : public std::binary_function<double,double,double>
92  {  {
# Line 60  struct FMin : public std::binary_functio Line 98  struct FMin : public std::binary_functio
98    
99  /**  /**
100     \brief     \brief
101     Return the absolute maximum value.     Return the absolute maximum value of the two given values.
   
    Description:  
    Return the absolute maximum value.  
102  */  */
103  struct AbsMax : public std::binary_function<double,double,double>  struct AbsMax : public std::binary_function<double,double,double>
104  {  {
# Line 75  struct AbsMax : public std::binary_funct Line 110  struct AbsMax : public std::binary_funct
110    
111  /**  /**
112     \brief     \brief
113     Return the length.     Return the length between the two given values.
   
    Description:  
    Return the length.  
114  */  */
115  struct Length : public std::binary_function<double,double,double>  struct Length : public std::binary_function<double,double,double>
116  {  {
# Line 90  struct Length : public std::binary_funct Line 122  struct Length : public std::binary_funct
122    
123  /**  /**
124     \brief     \brief
125     Return the trace.     Return the trace of the two given values.
   
    Description:  
    Return the trace.  
126  */  */
127  struct Trace : public std::binary_function<double,double,double>  struct Trace : public std::binary_function<double,double,double>
128  {  {
# Line 105  struct Trace : public std::binary_functi Line 134  struct Trace : public std::binary_functi
134    
135  /**  /**
136     \brief     \brief
137     Adapt algorithms so they may be used by Data.     Perform the given operation upon all values in all data-points in the
138       given Data object and return the final result.
    Description:  
    Adapt algorithms so they may be used by Data. The functor  
    maintains state, the currentValue returned by the operation,  
    and the initial value.  
 */  
 template <class BinaryFunction>  
 class DataAlgorithmAdapter {  
  public:  
     DataAlgorithmAdapter(double initialValue):  
       m_initialValue(initialValue),  
       m_currentValue(initialValue)  
     {}  
     inline void operator()(double value)  
     {  
       m_currentValue=operation(m_currentValue,value);  
       return;  
     }  
     inline void resetResult()  
     {  
       m_currentValue=m_initialValue;  
     }  
     inline double getResult() const  
     {  
       return m_currentValue;  
     }  
  private:  
     //  
     // the initial operation value  
     double m_initialValue;  
     //  
     // the current operation value  
     double m_currentValue;  
     //  
     // The operation to perform  
     BinaryFunction operation;  
 };  
   
 /**  
    \brief  
    Perform the given operation upon all Data elements and return a single  
    result.  
139    
140     Description:     Calls DataArrayView::reductionOp
    Perform the given operation upon all Data elements and return a single  
    result.  
141  */  */
142  template <class UnaryFunction>  template <class UnaryFunction>
143  inline  inline
# Line 160  algorithm(DataExpanded& data, Line 146  algorithm(DataExpanded& data,
146            UnaryFunction operation)            UnaryFunction operation)
147  {  {
148    int i,j;    int i,j;
149    DataArrayView::ValueType::size_type numDPPSample=data.getNumDPPSample();    int numDPPSample=data.getNumDPPSample();
150    DataArrayView::ValueType::size_type numSamples=data.getNumSamples();    int numSamples=data.getNumSamples();
151    double resultLocal=0;    int resultVectorLength=numDPPSample*numSamples;
152  #pragma omp parallel private(resultLocal)    std::vector<double> resultVector(resultVectorLength);
153      DataArrayView dataView=data.getPointDataView();
154      // calculate the reduction operation value for each data point
155      // storing the result for each data-point in successive entries
156      // in resultVector
157    {    {
158  #pragma omp for private(i,j) schedule(static)  #pragma omp for private(i,j) schedule(static)
159      for (i=0;i<numSamples;i++) {      for (i=0;i<numSamples;i++) {
160        for (j=0;j<numDPPSample;j++) {        for (j=0;j<numDPPSample;j++) {
161      resultLocal=data.getPointDataView().reductionOp(data.getPointOffset(i,j), operation);  #pragma omp critical (reductionOp)
162  #pragma omp critical (algorithm)      resultVector[j*numSamples+i]=dataView.reductionOp(data.getPointOffset(i,j),operation);
     operation(resultLocal);  
163        }        }
164      }      }
165    }    }
166      // now calculate the reduction operation value across the results
167      // for each data-point
168      operation.resetResult();
169      for (int l=0;l<resultVectorLength;l++) {
170        operation(resultVector[l]);
171      }
172    return operation.getResult();    return operation.getResult();
173  }  }
174    
# Line 183  double Line 178  double
178  algorithm(DataTagged& data,  algorithm(DataTagged& data,
179            UnaryFunction operation)            UnaryFunction operation)
180  {  {
   //  
   // perform the operation on each tagged value  
181    const DataTagged::DataMapType& lookup=data.getTagLookup();    const DataTagged::DataMapType& lookup=data.getTagLookup();
182    DataTagged::DataMapType::const_iterator i;    DataTagged::DataMapType::const_iterator i;
183    DataTagged::DataMapType::const_iterator lookupEnd=lookup.end();    DataTagged::DataMapType::const_iterator lookupEnd=lookup.end();
184    DataArrayView& dataView=data.getPointDataView();    DataArrayView& dataView=data.getPointDataView();
185      std::vector<double> resultVector;
186      int resultVectorLength;
187      // perform the operation on each tagged value
188    for (i=lookup.begin();i!=lookupEnd;i++) {    for (i=lookup.begin();i!=lookupEnd;i++) {
189      operation(dataView.reductionOp(i->second,operation));      resultVector.push_back(dataView.reductionOp(i->second,operation));
190      }
191      // perform the operation on the default value
192      resultVector.push_back(data.getDefaultValue().reductionOp(operation));
193      // now calculate the reduction operation value across the results
194      // for each tagged value
195      resultVectorLength=resultVector.size();
196      operation.resetResult();
197      for (int l=0;l<resultVectorLength;l++) {
198        operation(resultVector[l]);
199    }    }
   //  
   // finally perform the operation on the default value  
   operation(data.getDefaultValue().reductionOp(operation));  
200    return operation.getResult();    return operation.getResult();
201  }  }
202    
# Line 209  algorithm(DataConstant& data, Line 211  algorithm(DataConstant& data,
211    
212  /**  /**
213     \brief     \brief
214     Perform the given data point reduction operation on all data points     Perform the given data-point reduction operation on all data-points
215     in data, storing results in corresponding elements of result.     in data, storing results in corresponding data-points of result.
216    
217     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
218     of data points, but where data has data points of rank n, result must have     of data points, but where data has data points of rank n, result must have
219     data points of rank 0.     data points of rank 0.
220    
221     Calls DataArrayView::dp_algorithm.     Calls DataArrayView::reductionOp
222  */  */
223  template <class UnaryFunction>  template <class UnaryFunction>
224  inline  inline
# Line 225  dp_algorithm(DataExpanded& data, Line 227  dp_algorithm(DataExpanded& data,
227               DataExpanded& result,               DataExpanded& result,
228               UnaryFunction operation)               UnaryFunction operation)
229  {  {
   //  
   // perform the operation on each data value  
   // and assign this to the corresponding element in result  
230    int i,j;    int i,j;
231    DataArrayView::ValueType::size_type numDPPSample=data.getNumDPPSample();    int numDPPSample=data.getNumDPPSample();
232    DataArrayView::ValueType::size_type numSamples=data.getNumSamples();    int numSamples=data.getNumSamples();
233      DataArrayView dataView=data.getPointDataView();
234      DataArrayView resultView=result.getPointDataView();
235      // perform the operation on each data-point and assign
236      // this to the corresponding element in result
237    {    {
238  #pragma omp for private(i,j) schedule(static)  #pragma omp for private(i,j) schedule(static)
239      for (i=0;i<numSamples;i++) {      for (i=0;i<numSamples;i++) {
240        for (j=0;j<numDPPSample;j++) {        for (j=0;j<numDPPSample;j++) {
241  #pragma omp critical (dp_algorithm)  #pragma omp critical (reductionOp)
242          result.getPointDataView().getData(data.getPointOffset(i,j)) =          resultView.getData(data.getPointOffset(i,j)) =
243            data.getPointDataView().dp_reductionOp(data.getPointOffset(i,j),operation);            dataView.reductionOp(data.getPointOffset(i,j),operation);
244        }        }
245      }      }
246    }    }
# Line 250  dp_algorithm(DataTagged& data, Line 253  dp_algorithm(DataTagged& data,
253               DataTagged& result,               DataTagged& result,
254               UnaryFunction operation)               UnaryFunction operation)
255  {  {
   //  
   // perform the operation on each tagged data value  
   // and assign this to the corresponding element in result  
256    const DataTagged::DataMapType& lookup=data.getTagLookup();    const DataTagged::DataMapType& lookup=data.getTagLookup();
257    DataTagged::DataMapType::const_iterator i;    DataTagged::DataMapType::const_iterator i;
258    DataTagged::DataMapType::const_iterator lookupEnd=lookup.end();    DataTagged::DataMapType::const_iterator lookupEnd=lookup.end();
259      DataArrayView dataView=data.getPointDataView();
260      DataArrayView resultView=result.getPointDataView();
261      // perform the operation on each tagged data value
262      // and assign this to the corresponding element in result
263    for (i=lookup.begin();i!=lookupEnd;i++) {    for (i=lookup.begin();i!=lookupEnd;i++) {
264      result.getPointDataView().getData(i->second) =      resultView.getData(i->second) =
265        data.getPointDataView().dp_reductionOp(i->second,operation);        dataView.reductionOp(i->second,operation);
266    }    }
267    //    // perform the operation on the default data value
   // finally 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    result.getPointDataView().getData(0) =    resultView.getData(0) =
270      data.getDefaultValue().dp_reductionOp(operation);      data.getDefaultValue().reductionOp(operation);
271  }  }
272    
273  template <class UnaryFunction>  template <class UnaryFunction>
# Line 274  dp_algorithm(DataConstant& data, Line 277  dp_algorithm(DataConstant& data,
277               DataConstant& result,               DataConstant& result,
278               UnaryFunction operation)               UnaryFunction operation)
279  {  {
280    //    // perform the operation on the data value
281    // perform the operation on the default data value    // and assign this to the element in result
   // and assign this to the default element in result  
282    result.getPointDataView().getData(0) =    result.getPointDataView().getData(0) =
283      data.getPointDataView().dp_reductionOp(operation);      data.getPointDataView().reductionOp(operation);
284  }  }
285    
286  } // end of namespace  } // end of namespace

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

  ViewVC Help
Powered by ViewVC 1.1.26