escript  Revision_Unversioneddirectory
DataAlgorithm.h
Go to the documentation of this file.
1 
2 /*****************************************************************************
3 *
4 * Copyright (c) 2003-2016 by The University of Queensland
5 * http://www.uq.edu.au
6 *
7 * Primary Business: Queensland, Australia
8 * Licensed under the Apache License, version 2.0
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Development until 2012 by Earth Systems Science Computational Center (ESSCC)
12 * Development 2012-2013 by School of Earth Sciences
13 * Development from 2014 by Centre for Geoscience Computing (GeoComp)
14 *
15 *****************************************************************************/
16 
17 
18 #if !defined escript_DataAlgorithm_20040714_H
19 #define escript_DataAlgorithm_20040714_H
20 #include "system_dep.h"
21 
22 #include "DataExpanded.h"
23 #include "DataTagged.h"
24 #include "DataConstant.h"
25 
26 #include "DataMaths.h"
27 
28 #include <iostream>
29 #include <algorithm>
30 #include <list>
31 
32 namespace escript {
33 
44 template <class BinaryFunction>
46  public:
47  DataAlgorithmAdapter(double initialValue):
48  m_initialValue(initialValue),
49  m_currentValue(initialValue)
50  {
51  }
55  operation(other.operation)
56  {
57  }
58  inline void operator()(double value)
59  {
61  return;
62  }
63  inline void resetResult()
64  {
66  }
67  inline double getResult() const
68  {
69  return m_currentValue;
70  }
71  private:
72  //
73  // the initial operation value
75  //
76  // the current operation value
78  //
79  // The operation to perform
80  BinaryFunction operation;
81 };
82 
87 struct FMax : public std::binary_function<double,double,double>
88 {
89  inline double operator()(double x, double y) const
90  {
91  return std::max(x,y);
92  }
93 };
94 
99 struct FMin : public std::binary_function<double,double,double>
100 {
101  inline double operator()(double x, double y) const
102  {
103  return std::min(x,y);
104  }
105 };
106 
111 struct AbsMax : public std::binary_function<double,double,double>
112 {
113  inline double operator()(double x, double y) const
114  {
115  return std::max(fabs(x),fabs(y));
116  }
117 };
118 
123 struct AbsMin : public std::binary_function<double,double,double>
124 {
125  inline double operator()(double x, double y) const
126  {
127  return std::min(fabs(x),fabs(y));
128  }
129 };
130 
135 struct Length : public std::binary_function<double,double,double>
136 {
137  inline double operator()(double x, double y) const
138  {
139  return std::sqrt(std::pow(x,2)+std::pow(y,2));
140  }
141 };
142 
147 struct Trace : public std::binary_function<double,double,double>
148 {
149  inline double operator()(double x, double y) const
150  {
151  return x+y;
152  }
153 };
154 
158 struct AbsGT : public std::binary_function<double,double,double>
159 {
160  inline double operator()(double x, double y) const
161  {
162  return fabs(x)>y;
163  }
164 };
165 
169 struct AbsLTE : public std::binary_function<double,double,double>
170 {
171  inline double operator()(double x, double y) const
172  {
173  return fabs(x)<=y;
174  }
175 };
176 
177 
183 template <class BinaryFunction>
184 inline
185 double
187  BinaryFunction operation,
188  double initial_value)
189 {
190  int i,j;
191  int numDPPSample=data.getNumDPPSample();
192  int numSamples=data.getNumSamples();
193  double global_current_value=initial_value;
194  double local_current_value;
195 // DataArrayView dataView=data.getPointDataView();
196  const DataTypes::ValueType& vec=data.getVectorRO();
197  const DataTypes::ShapeType& shape=data.getShape();
198  // calculate the reduction operation value for each data point
199  // reducing the result for each data-point into the current_value variables
200  #pragma omp parallel private(local_current_value)
201  {
202  local_current_value=initial_value;
203  #pragma omp for private(i,j) schedule(static)
204  for (i=0;i<numSamples;i++) {
205  for (j=0;j<numDPPSample;j++) {
206 /* local_current_value=operation(local_current_value,dataView.reductionOp(data.getPointOffset(i,j),operation,initial_value));*/
207  local_current_value=operation(local_current_value,DataMaths::reductionOp(vec,shape,data.getPointOffset(i,j),operation,initial_value));
208 
209  }
210  }
211  #pragma omp critical
212  global_current_value=operation(global_current_value,local_current_value);
213  }
214  return global_current_value;
215 }
216 
217 // It is important that the algorithm only be applied to tags which are actually in use.
218 template <class BinaryFunction>
219 inline
220 double
222  BinaryFunction operation,
223  double initial_value)
224 {
225  double current_value=initial_value;
226 
227  const DataTypes::ValueType& vec=data.getVectorRO();
228  const DataTypes::ShapeType& shape=data.getShape();
229  const DataTagged::DataMapType& lookup=data.getTagLookup();
230  const std::list<int> used=data.getFunctionSpace().getListOfTagsSTL();
231  for (std::list<int>::const_iterator i=used.begin();i!=used.end();++i)
232  {
233  int tag=*i;
234  if (tag==0) // check for the default tag
235  {
236  current_value=operation(current_value,DataMaths::reductionOp(vec,shape,data.getDefaultOffset(),operation,initial_value));
237  }
238  else
239  {
240  DataTagged::DataMapType::const_iterator it=lookup.find(tag);
241  if (it!=lookup.end())
242  {
243  current_value=operation(current_value,DataMaths::reductionOp(vec,shape,it->second,operation,initial_value));
244  }
245  }
246  }
247  return current_value;
248 }
249 
250 template <class BinaryFunction>
251 inline
252 double
254  BinaryFunction operation,
255  double initial_value)
256 {
257  return DataMaths::reductionOp(data.getVectorRO(),data.getShape(),0,operation,initial_value);
258 }
259 
271 template <class BinaryFunction>
272 inline
273 void
275  DataExpanded& result,
276  BinaryFunction operation,
277  double initial_value)
278 {
279  int i,j;
280  int numSamples=data.getNumSamples();
281  int numDPPSample=data.getNumDPPSample();
282 // DataArrayView dataView=data.getPointDataView();
283 // DataArrayView resultView=result.getPointDataView();
284  const DataTypes::ValueType& dataVec=data.getVectorRO();
285  const DataTypes::ShapeType& shape=data.getShape();
286  DataTypes::ValueType& resultVec=result.getVectorRW();
287  // perform the operation on each data-point and assign
288  // this to the corresponding element in result
289  #pragma omp parallel for private(i,j) schedule(static)
290  for (i=0;i<numSamples;i++) {
291  for (j=0;j<numDPPSample;j++) {
292 /* resultView.getData(result.getPointOffset(i,j)) =
293  dataView.reductionOp(data.getPointOffset(i,j),operation,initial_value);*/
294  resultVec[result.getPointOffset(i,j)] =
295  DataMaths::reductionOp(dataVec, shape, data.getPointOffset(i,j),operation,initial_value);
296 
297  }
298  }
299 }
300 
301 template <class BinaryFunction>
302 inline
303 void
305  DataTagged& result,
306  BinaryFunction operation,
307  double initial_value)
308 {
309  // perform the operation on each tagged value in data
310  // and assign this to the corresponding element in result
311  const DataTypes::ShapeType& shape=data.getShape();
312  const DataTypes::ValueType& vec=data.getVectorRO();
313  const DataTagged::DataMapType& lookup=data.getTagLookup();
314  for (DataTagged::DataMapType::const_iterator i=lookup.begin(); i!=lookup.end(); i++) {
315  result.getDataByTagRW(i->first,0) =
316  DataMaths::reductionOp(vec,shape,data.getOffsetForTag(i->first),operation,initial_value);
317  }
318  // perform the operation on the default data value
319  // and assign this to the default element in result
320  result.getVectorRW()[result.getDefaultOffset()] = DataMaths::reductionOp(data.getVectorRO(),data.getShape(),data.getDefaultOffset(),operation,initial_value);
321 }
322 
323 template <class BinaryFunction>
324 inline
325 void
327  DataConstant& result,
328  BinaryFunction operation,
329  double initial_value)
330 {
331  // perform the operation on the data value
332  // and assign this to the element in result
333  result.getVectorRW()[0] =
334  DataMaths::reductionOp(data.getVectorRO(),data.getShape(),0,operation,initial_value);
335 }
336 
337 } // end of namespace
338 
339 #endif
DataVector implements an arbitrarily long vector of data values. DataVector is the underlying data co...
Definition: DataVector.h:44
double operator()(double x, double y) const
Definition: DataAlgorithm.h:171
DataAlgorithmAdapter(double initialValue)
Definition: DataAlgorithm.h:47
virtual DataTypes::ValueType::size_type getPointOffset(int sampleNo, int dataPointNo) const
Return the offset for the given given data point. This returns the offset in bytes for the given poin...
Definition: DataExpanded.cpp:305
void dp_algorithm(const DataExpanded &data, DataExpanded &result, BinaryFunction operation, double initial_value)
Perform the given data-point reduction operation on all data-points in data, storing results in corre...
Definition: DataAlgorithm.h:274
const DataTypes::ValueType & getVectorRO() const
Definition: DataExpanded.cpp:755
double m_initialValue
Definition: DataAlgorithm.h:74
DataTypes::ValueType::size_type getDefaultOffset() const
Returns the offset in the structure which stores the default value.
Definition: DataTagged.h:608
double operator()(double x, double y) const
Definition: DataAlgorithm.h:101
double operator()(double x, double y) const
Definition: DataAlgorithm.h:149
Return the absolute minimum value of the two given values.
Definition: DataAlgorithm.h:123
double m_currentValue
Definition: DataAlgorithm.h:77
BinaryFunction operation
Definition: DataAlgorithm.h:80
Definition: AbstractContinuousDomain.cpp:24
DataConstant stores a single data point which represents the entire function space.
Definition: DataConstant.h:37
void operator()(double value)
Definition: DataAlgorithm.h:58
Give a short description of what DataExpanded does.
Definition: DataExpanded.h:44
const DataMapType & getTagLookup() const
getTagLookup
Definition: DataTagged.h:629
void resetResult()
Definition: DataAlgorithm.h:63
Return the length between the two given values.
Definition: DataAlgorithm.h:135
std::vector< int > ShapeType
The shape of a single datapoint.
Definition: DataTypes.h:38
DataAlgorithmAdapter(const DataAlgorithmAdapter &other)
Definition: DataAlgorithm.h:52
Simulates a full dataset accessible via sampleNo and dataPointNo.
Definition: DataTagged.h:44
double reductionOp(const DataTypes::ValueType &left, const DataTypes::ShapeType &shape, DataTypes::ValueType::size_type offset, BinaryFunction operation, double initial_value)
Perform the given data point reduction operation on the data point specified by the given offset into...
Definition: DataMaths.h:880
DataTypes::ValueType::reference getDataByTagRW(int tag, DataTypes::ValueType::size_type i)
getDataByTag
Definition: DataTagged.cpp:574
DataTypes::ValueType & getVectorRW()
Return a reference to the underlying DataVector.
Definition: DataTagged.cpp:890
int getNumDPPSample() const
Return the number of data points per sample.
Definition: DataAbstract.h:562
Return the absolute maximum value of the two given values.
Definition: DataAlgorithm.h:111
double operator()(double x, double y) const
Definition: DataAlgorithm.h:137
std::map< int, int > DataMapType
Definition: DataTagged.h:57
const DataTypes::ValueType & getVectorRO() const
Definition: DataConstant.cpp:395
double operator()(double x, double y) const
Definition: DataAlgorithm.h:89
DataTypes::ValueType & getVectorRW()
Return a reference to the underlying DataVector.
Definition: DataConstant.cpp:388
Return the trace of the two given values.
Definition: DataAlgorithm.h:147
Return the minimum value of the two given values.
Definition: DataAlgorithm.h:99
double operator()(double x, double y) const
Definition: DataAlgorithm.h:160
Return the maximum value of the two given values.
Definition: DataAlgorithm.h:87
double algorithm(const DataExpanded &data, BinaryFunction operation, double initial_value)
Perform the given operation upon all values in all data-points in the given Data object and return th...
Definition: DataAlgorithm.h:186
int getNumSamples() const
Return the number of samples.
Definition: DataAbstract.h:573
const DataTypes::ShapeType & getShape() const
Return the shape information for the point data.
Definition: DataAbstract.h:592
DataTypes::ValueType & getVectorRW()
Return a a reference to the underlying DataVector.
Definition: DataExpanded.cpp:749
std::list< int > getListOfTagsSTL() const
Returns an stl list of the tags used in this function space.
Definition: FunctionSpace.cpp:291
const FunctionSpace & getFunctionSpace() const
Return the function space associated with this Data object.
Definition: DataAbstract.h:585
const DataTypes::ValueType & getVectorRO() const
Definition: DataTagged.cpp:897
Return 1 if abs(x)>y, otherwise return 0.
Definition: DataAlgorithm.h:158
Return 1 if abs(x)<=y, otherwise return 0.
Definition: DataAlgorithm.h:169
double operator()(double x, double y) const
Definition: DataAlgorithm.h:125
double operator()(double x, double y) const
Definition: DataAlgorithm.h:113
Adapt binary algorithms so they may be used in DataArrayView reduction operations.
Definition: DataAlgorithm.h:45
double getResult() const
Definition: DataAlgorithm.h:67
Describes binary operations performed on DataVector.
DataTypes::ValueType::size_type getOffsetForTag(int tag) const
getOffsetForTag
Definition: DataTagged.cpp:552