/[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 96 by jgs, Tue Oct 26 06:53:54 2004 UTC revision 97 by jgs, Tue Dec 14 05:39:33 2004 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     \brief
78     Adapt algorithms so they may be used by Data.     Adapt algorithms so they may be used by Data.
# Line 95  class DataAlgorithmAdapter { Line 98  class DataAlgorithmAdapter {
98      }      }
99   private:   private:
100      //      //
101      // the current maximum value      // the current operation value
102      double m_currentValue;      double m_currentValue;
103      //      //
104      // The operation to perform      // The operation to perform
# Line 104  class DataAlgorithmAdapter { Line 107  class DataAlgorithmAdapter {
107    
108  /**  /**
109     \brief     \brief
110     Perform the given operation upon all DataElements and return a single     Perform the given operation upon all Data elements and return a single
111     result.     result.
112    
113     Description:     Description:
114     Perform the given operation upon all DataElements and return a single     Perform the given operation upon all Data elements and return a single
115     result.     result.
116  */  */
117  template <class UnaryFunction>  template <class UnaryFunction>
118  inline double algorithm(DataExpanded& data, UnaryFunction operation)  inline
119    double
120    algorithm(DataExpanded& data,
121              UnaryFunction operation)
122  {  {
123    int i,j;    int i,j;
124    DataArrayView::ValueType::size_type numDPPSample=data.getNumDPPSample();    DataArrayView::ValueType::size_type numDPPSample=data.getNumDPPSample();
125    DataArrayView::ValueType::size_type numSamples=data.getNumSamples();    DataArrayView::ValueType::size_type numSamples=data.getNumSamples();
126    double resultLocal;    double resultLocal=0;
127    #pragma omp parallel private(resultLocal)  #pragma omp parallel private(resultLocal)
128    {    {
129      #pragma omp parallel for private(i,j) schedule(static)  #pragma omp for private(i,j) schedule(static)
130      for (i=0;i<numSamples;++i) {      for (i=0;i<numSamples;i++) {
131        for (j=0;j<numDPPSample;++j) {        for (j=0;j<numDPPSample;j++) {
132      resultLocal=data.getPointDataView().algorithm(data.getPointOffset(i,j),      resultLocal=data.getPointDataView().algorithm(data.getPointOffset(i,j), operation);
133                                operation);  #pragma omp critical (algorithm)
     #pragma omp critical (algorithm)  
134      operation(resultLocal);      operation(resultLocal);
135        }        }
136      }      }
# Line 134  inline double algorithm(DataExpanded& da Line 139  inline double algorithm(DataExpanded& da
139  }  }
140    
141  template <class UnaryFunction>  template <class UnaryFunction>
142  inline double algorithm(DataTagged& data, UnaryFunction operation)  inline
143    double
144    algorithm(DataTagged& data,
145              UnaryFunction operation)
146  {  {
147    //    //
148    // perform the operation on each tagged value including the default    // perform the operation on each tagged value
149    const DataTagged::DataMapType& lookup=data.getTagLookup();    const DataTagged::DataMapType& lookup=data.getTagLookup();
150    DataTagged::DataMapType::const_iterator i;    DataTagged::DataMapType::const_iterator i;
151    DataTagged::DataMapType::const_iterator lookupEnd=lookup.end();    DataTagged::DataMapType::const_iterator lookupEnd=lookup.end();
152    DataArrayView& dataView=data.getPointDataView();    DataArrayView& dataView=data.getPointDataView();
153    for (i=lookup.begin();i!=lookupEnd;++i) {    for (i=lookup.begin();i!=lookupEnd;i++) {
154      operation(dataView.algorithm(i->second,operation));      operation(dataView.algorithm(i->second,operation));
155    }    }
156    //    //
# Line 152  inline double algorithm(DataTagged& data Line 160  inline double algorithm(DataTagged& data
160  }  }
161    
162  template <class UnaryFunction>  template <class UnaryFunction>
163  inline double algorithm(DataConstant& data, UnaryFunction operation)  inline
164    double
165    algorithm(DataConstant& data,
166              UnaryFunction operation)
167  {  {
168    return data.getPointDataView().algorithm(operation);    return data.getPointDataView().algorithm(operation);
169  }  }
170    
171    /**
172       \brief
173       Perform the given data point reduction operation upon all data points
174       in data, storing results in corresponding elements of result.
175    
176       Objects data and result must be of the same type, and have the same number
177       of samples and number of data points per sample, but where data has data
178       points of rank n, result must have data points of rank 0.
179    
180       Calls DataArrayView::dp_algorithm.
181    */
182    template <class UnaryFunction>
183    inline
184    void
185    dp_algorithm(DataExpanded& result,
186                 DataExpanded& data,
187                 UnaryFunction operation)
188    {
189      int i,j;
190      DataArrayView::ValueType::size_type numDPPSample=data.getNumDPPSample();
191      DataArrayView::ValueType::size_type numSamples=data.getNumSamples();
192      {
193    #pragma omp for private(i,j) schedule(static)
194        for (i=0;i<numSamples;i++) {
195          for (j=0;j<numDPPSample;j++) {
196            // assign this to corresponding element in result
197        data.getPointDataView().dp_algorithm(data.getPointOffset(i,j),operation);
198    #pragma omp critical (dp_algorithm)
199          }
200        }
201      }
202    }
203    
204    template <class UnaryFunction>
205    inline
206    void
207    dp_algorithm(DataTagged& result,
208                 DataTagged& data,
209                 UnaryFunction operation)
210    {
211      //
212      // perform the operation on each tagged value
213      const DataTagged::DataMapType& lookup=data.getTagLookup();
214      DataTagged::DataMapType::const_iterator i;
215      DataTagged::DataMapType::const_iterator lookupEnd=lookup.end();
216      for (i=lookup.begin();i!=lookupEnd;i++) {
217        // assign this to corresponding element in result
218        data.getPointDataView().dp_algorithm(i->second,operation);
219      }
220      //
221      // finally perform the operation on the default value
222      // assign this to corresponding element in result
223      data.getDefaultValue().dp_algorithm(operation);
224    }
225    
226    template <class UnaryFunction>
227    inline
228    void
229    dp_algorithm(DataConstant& result,
230                 DataConstant& data,
231                 UnaryFunction operation)
232    {
233      //result.getPointDataView().getData()
234      // assign this to corresponding element in result
235      data.getPointDataView().dp_algorithm(operation);
236    }
237    
238  } // end of namespace  } // end of namespace
239  #endif  #endif

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

  ViewVC Help
Powered by ViewVC 1.1.26