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

revision 97 by jgs, Tue Dec 14 05:39:33 2004 UTC revision 100 by jgs, Wed Dec 15 03:48:48 2004 UTC
# Line 1  Line 1
// \$Id\$
1  /*  /*
2   ******************************************************************************   ******************************************************************************
3   *                                                                            *   *                                                                            *
# Line 13  Line 12
12   ******************************************************************************   ******************************************************************************
13  */  */
14
15  #if !defined escript_DataAlgorithm_20040714_H  #if !defined  escript_DataAlgorithm_20040714_H
16  #define escript_DataAlgorithm_20040714_H  #define escript_DataAlgorithm_20040714_H
17
18  #include "escript/Data/DataExpanded.h"  #include "escript/Data/DataExpanded.h"
# Line 27  Line 26
26  #include <limits>  #include <limits>
27
28  namespace escript {  namespace escript {

29  /**  /**
30     \brief     \brief
31     Return the maximum value.     Return the maximum value.
# Line 72  struct AbsMax : public std::binary_funct Line 70  struct AbsMax : public std::binary_funct
70      return std::max(fabs(x),fabs(y));      return std::max(fabs(x),fabs(y));
71    }    }
72  };  };

73  /**  /**
74     \brief     \brief
75     Adapt algorithms so they may be used by Data.     Adapt algorithms so they may be used by Data.
95      }      }
96   private:   private:
97      //      //
98      // the current operation value      // the current maximum value
99      double m_currentValue;      double m_currentValue;
100      //      //
101      // The operation to perform      // The operation to perform
104
105  /**  /**
106     \brief     \brief
107     Perform the given operation upon all Data elements and return a single     Perform the given operation upon all DataElements and return a single
108     result.     result.
109
110     Description:     Description:
111     Perform the given operation upon all Data elements and return a single     Perform the given operation upon all DataElements and return a single
112     result.     result.
113  */  */
114  template <class UnaryFunction>  template <class UnaryFunction>
115  inline  inline double algorithm(DataExpanded& data, UnaryFunction operation)
double
algorithm(DataExpanded& data,
UnaryFunction operation)
116  {  {
117    int i,j;    int i,j;
118    DataArrayView::ValueType::size_type numDPPSample=data.getNumDPPSample();    DataArrayView::ValueType::size_type numDPPSample=data.getNumDPPSample();
119    DataArrayView::ValueType::size_type numSamples=data.getNumSamples();    DataArrayView::ValueType::size_type numSamples=data.getNumSamples();
120    double resultLocal=0;    double resultLocal;
121  #pragma omp parallel private(resultLocal)    #pragma omp parallel private(resultLocal)
122    {    {
123  #pragma omp for private(i,j) schedule(static)      #pragma omp parallel for private(i,j) schedule(static)
124      for (i=0;i<numSamples;i++) {      for (i=0;i<numSamples;++i) {
125        for (j=0;j<numDPPSample;j++) {        for (j=0;j<numDPPSample;++j) {
126      resultLocal=data.getPointDataView().algorithm(data.getPointOffset(i,j), operation);      resultLocal=data.getPointDataView().algorithm(data.getPointOffset(i,j),
127  #pragma omp critical (algorithm)                                operation);
128        #pragma omp critical (algorithm)
129      operation(resultLocal);      operation(resultLocal);
130        }        }
131      }      }
# Line 139  algorithm(DataExpanded& data, Line 134  algorithm(DataExpanded& data,
134  }  }
135
136  template <class UnaryFunction>  template <class UnaryFunction>
137  inline  inline double algorithm(DataTagged& data, UnaryFunction operation)
double
algorithm(DataTagged& data,
UnaryFunction operation)
138  {  {
139    //    //
140    // perform the operation on each tagged value    // perform the operation on each tagged value including the default
141    const DataTagged::DataMapType& lookup=data.getTagLookup();    const DataTagged::DataMapType& lookup=data.getTagLookup();
142    DataTagged::DataMapType::const_iterator i;    DataTagged::DataMapType::const_iterator i;
143    DataTagged::DataMapType::const_iterator lookupEnd=lookup.end();    DataTagged::DataMapType::const_iterator lookupEnd=lookup.end();
144    DataArrayView& dataView=data.getPointDataView();    DataArrayView& dataView=data.getPointDataView();
145    for (i=lookup.begin();i!=lookupEnd;i++) {    for (i=lookup.begin();i!=lookupEnd;++i) {
146      operation(dataView.algorithm(i->second,operation));      operation(dataView.algorithm(i->second,operation));
147    }    }
148    //    //
# Line 160  algorithm(DataTagged& data, Line 152  algorithm(DataTagged& data,
152  }  }
153
154  template <class UnaryFunction>  template <class UnaryFunction>
155  inline  inline double algorithm(DataConstant& data, UnaryFunction operation)
double
algorithm(DataConstant& data,
UnaryFunction operation)
156  {  {
157    return data.getPointDataView().algorithm(operation);    return data.getPointDataView().algorithm(operation);
158  }  }
159
/**
\brief
Perform the given data point reduction operation upon all data points
in data, storing results in corresponding elements of result.

Objects data and result must be of the same type, and have the same number
of samples and number of data points per sample, but where data has data
points of rank n, result must have data points of rank 0.

Calls DataArrayView::dp_algorithm.
*/
template <class UnaryFunction>
inline
void
dp_algorithm(DataExpanded& result,
DataExpanded& data,
UnaryFunction operation)
{
int i,j;
DataArrayView::ValueType::size_type numDPPSample=data.getNumDPPSample();
DataArrayView::ValueType::size_type numSamples=data.getNumSamples();
{
#pragma omp for private(i,j) schedule(static)
for (i=0;i<numSamples;i++) {
for (j=0;j<numDPPSample;j++) {
// assign this to corresponding element in result
data.getPointDataView().dp_algorithm(data.getPointOffset(i,j),operation);
#pragma omp critical (dp_algorithm)
}
}
}
}

template <class UnaryFunction>
inline
void
dp_algorithm(DataTagged& result,
DataTagged& data,
UnaryFunction operation)
{
//
// perform the operation on each tagged value
const DataTagged::DataMapType& lookup=data.getTagLookup();
DataTagged::DataMapType::const_iterator i;
DataTagged::DataMapType::const_iterator lookupEnd=lookup.end();
for (i=lookup.begin();i!=lookupEnd;i++) {
// assign this to corresponding element in result
data.getPointDataView().dp_algorithm(i->second,operation);
}
//
// finally perform the operation on the default value
// assign this to corresponding element in result
data.getDefaultValue().dp_algorithm(operation);
}

template <class UnaryFunction>
inline
void
dp_algorithm(DataConstant& result,
DataConstant& data,
UnaryFunction operation)
{
//result.getPointDataView().getData()
// assign this to corresponding element in result
data.getPointDataView().dp_algorithm(operation);
}
160
161  } // end of namespace  } // end of namespace
162  #endif  #endif

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