/[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

revision 100 by jgs, Wed Dec 15 03:48:48 2004 UTC revision 108 by jgs, Thu Jan 27 06:21:59 2005 UTC
# Line 1  Line 1 
1    // $Id$
2  /*  /*
3   ******************************************************************************   ******************************************************************************
4   *                                                                            *   *                                                                            *
# Line 12  Line 13 
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    
19  #include "escript/Data/DataExpanded.h"  #include "escript/Data/DataExpanded.h"
# Line 26  Line 27 
27  #include <limits>  #include <limits>
28    
29  namespace escript {  namespace escript {
30    
31  /**  /**
32     \brief     \brief
33     Return the maximum value.     Return the maximum value.
# Line 70  struct AbsMax : public std::binary_funct Line 72  struct AbsMax : public std::binary_funct
72      return std::max(fabs(x),fabs(y));      return std::max(fabs(x),fabs(y));
73    }    }
74  };  };
75    
76    /**
77       \brief
78       Return the length.
79    
80       Description:
81       Return the length.
82    */
83    struct Length : public std::binary_function<double,double,double>
84    {
85      inline double operator()(double x, double y) const
86      {
87        return std::sqrt(std::pow(x,2)+std::pow(y,2));
88      }
89    };
90    
91    /**
92       \brief
93       Return the trace.
94    
95       Description:
96       Return the trace.
97    */
98    struct Trace : public std::binary_function<double,double,double>
99    {
100      inline double operator()(double x, double y) const
101      {
102        return x+y;
103      }
104    };
105    
106  /**  /**
107     \brief     \brief
108     Adapt algorithms so they may be used by Data.     Adapt algorithms so they may be used by Data.
109    
110     Description:     Description:
111     Adapt algorithms so they may be used by Data. The functor     Adapt algorithms so they may be used by Data. The functor
112     maintains state, ie the currentValue retuned by the operation.     maintains state, the currentValue returned by the operation,
113       and the initial value.
114  */  */
115  template <class BinaryFunction>  template <class BinaryFunction>
116  class DataAlgorithmAdapter {  class DataAlgorithmAdapter {
117   public:   public:
118      DataAlgorithmAdapter(double initialValue):      DataAlgorithmAdapter(double initialValue):
119          m_initialValue(initialValue),
120        m_currentValue(initialValue)        m_currentValue(initialValue)
121      {}      {}
122      inline void operator()(double value)      inline void operator()(double value)
# Line 89  class DataAlgorithmAdapter { Line 124  class DataAlgorithmAdapter {
124        m_currentValue=operation(m_currentValue,value);        m_currentValue=operation(m_currentValue,value);
125        return;        return;
126      }      }
127        inline void resetResult()
128        {
129          m_currentValue=m_initialValue;
130        }
131      inline double getResult() const      inline double getResult() const
132      {      {
133        return m_currentValue;        return m_currentValue;
134      }      }
135   private:   private:
136      //      //
137      // the current maximum value      // the initial operation value
138        double m_initialValue;
139        //
140        // the current operation value
141      double m_currentValue;      double m_currentValue;
142      //      //
143      // The operation to perform      // The operation to perform
# Line 104  class DataAlgorithmAdapter { Line 146  class DataAlgorithmAdapter {
146    
147  /**  /**
148     \brief     \brief
149     Perform the given operation upon all DataElements and return a single     Perform the given operation upon all Data elements and return a single
150     result.     result.
151    
152     Description:     Description:
153     Perform the given operation upon all DataElements and return a single     Perform the given operation upon all Data elements and return a single
154     result.     result.
155  */  */
156  template <class UnaryFunction>  template <class UnaryFunction>
157  inline double algorithm(DataExpanded& data, UnaryFunction operation)  inline
158    double
159    algorithm(DataExpanded& data,
160              UnaryFunction operation)
161  {  {
162    int i,j;    int i,j;
163    DataArrayView::ValueType::size_type numDPPSample=data.getNumDPPSample();    DataArrayView::ValueType::size_type numDPPSample=data.getNumDPPSample();
164    DataArrayView::ValueType::size_type numSamples=data.getNumSamples();    DataArrayView::ValueType::size_type numSamples=data.getNumSamples();
165    double resultLocal;    double resultLocal=0;
166    #pragma omp parallel private(resultLocal)  #pragma omp parallel private(resultLocal)
167    {    {
168      #pragma omp parallel for private(i,j) schedule(static)  #pragma omp for private(i,j) schedule(static)
169      for (i=0;i<numSamples;++i) {      for (i=0;i<numSamples;i++) {
170        for (j=0;j<numDPPSample;++j) {        for (j=0;j<numDPPSample;j++) {
171      resultLocal=data.getPointDataView().algorithm(data.getPointOffset(i,j),      resultLocal=data.getPointDataView().reductionOp(data.getPointOffset(i,j), operation);
172                                operation);  #pragma omp critical (algorithm)
     #pragma omp critical (algorithm)  
173      operation(resultLocal);      operation(resultLocal);
174        }        }
175      }      }
# Line 134  inline double algorithm(DataExpanded& da Line 178  inline double algorithm(DataExpanded& da
178  }  }
179    
180  template <class UnaryFunction>  template <class UnaryFunction>
181  inline double algorithm(DataTagged& data, UnaryFunction operation)  inline
182    double
183    algorithm(DataTagged& data,
184              UnaryFunction operation)
185  {  {
186    //    //
187    // perform the operation on each tagged value including the default    // perform the operation on each tagged value
188    const DataTagged::DataMapType& lookup=data.getTagLookup();    const DataTagged::DataMapType& lookup=data.getTagLookup();
189    DataTagged::DataMapType::const_iterator i;    DataTagged::DataMapType::const_iterator i;
190    DataTagged::DataMapType::const_iterator lookupEnd=lookup.end();    DataTagged::DataMapType::const_iterator lookupEnd=lookup.end();
191    DataArrayView& dataView=data.getPointDataView();    DataArrayView& dataView=data.getPointDataView();
192    for (i=lookup.begin();i!=lookupEnd;++i) {    for (i=lookup.begin();i!=lookupEnd;i++) {
193      operation(dataView.algorithm(i->second,operation));      operation(dataView.reductionOp(i->second,operation));
194    }    }
195    //    //
196    // finally perform the operation on the default value    // finally perform the operation on the default value
197    operation(data.getDefaultValue().algorithm(operation));    operation(data.getDefaultValue().reductionOp(operation));
198    return operation.getResult();    return operation.getResult();
199  }  }
200    
201  template <class UnaryFunction>  template <class UnaryFunction>
202  inline double algorithm(DataConstant& data, UnaryFunction operation)  inline
203    double
204    algorithm(DataConstant& data,
205              UnaryFunction operation)
206    {
207      return data.getPointDataView().reductionOp(operation);
208    }
209    
210    /**
211       \brief
212       Perform the given data point reduction operation on all data points
213       in data, storing results in corresponding elements of result.
214    
215       Objects data and result must be of the same type, and have the same number
216       of data points, but where data has data points of rank n, result must have
217       data points of rank 0.
218    
219       Calls DataArrayView::dp_algorithm.
220    */
221    template <class UnaryFunction>
222    inline
223    void
224    dp_algorithm(DataExpanded& data,
225                 DataExpanded& result,
226                 UnaryFunction operation)
227  {  {
228    return data.getPointDataView().algorithm(operation);    //
229      // perform the operation on each data value
230      // and assign this to the corresponding element in result
231      int i,j;
232      DataArrayView::ValueType::size_type numDPPSample=data.getNumDPPSample();
233      DataArrayView::ValueType::size_type numSamples=data.getNumSamples();
234      {
235    #pragma omp for private(i,j) schedule(static)
236        for (i=0;i<numSamples;i++) {
237          for (j=0;j<numDPPSample;j++) {
238    #pragma omp critical (dp_algorithm)
239            result.getPointDataView().getData(data.getPointOffset(i,j)) =
240              data.getPointDataView().dp_reductionOp(data.getPointOffset(i,j),operation);
241          }
242        }
243      }
244  }  }
245    
246    template <class UnaryFunction>
247    inline
248    void
249    dp_algorithm(DataTagged& data,
250                 DataTagged& result,
251                 UnaryFunction operation)
252    {
253      //
254      // perform the operation on each tagged data value
255      // and assign this to the corresponding element in result
256      const DataTagged::DataMapType& lookup=data.getTagLookup();
257      DataTagged::DataMapType::const_iterator i;
258      DataTagged::DataMapType::const_iterator lookupEnd=lookup.end();
259      for (i=lookup.begin();i!=lookupEnd;i++) {
260        result.getPointDataView().getData(i->second) =
261          data.getPointDataView().dp_reductionOp(i->second,operation);
262      }
263      //
264      // finally perform the operation on the default data value
265      // and assign this to the default element in result
266      result.getPointDataView().getData(0) =
267        data.getDefaultValue().dp_reductionOp(operation);
268    }
269    
270    template <class UnaryFunction>
271    inline
272    void
273    dp_algorithm(DataConstant& data,
274                 DataConstant& result,
275                 UnaryFunction operation)
276    {
277      //
278      // perform the operation on the default data value
279      // and assign this to the default element in result
280      result.getPointDataView().getData(0) =
281        data.getPointDataView().dp_reductionOp(operation);
282    }
283    
284  } // end of namespace  } // end of namespace
285  #endif  #endif

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

  ViewVC Help
Powered by ViewVC 1.1.26