escript  Revision_
BinaryOp.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_BinaryOp_20040315_H
19 #define escript_BinaryOp_20040315_H
20 #include "system_dep.h"
21 
22 #include "DataTypes.h"
23 #include "DataConstant.h"
24 #include "DataTagged.h"
25 #include "DataExpanded.h"
26 #include "DataMaths.h"
27 
36 namespace escript {
44 template <class BinaryFunction>
45 inline void binaryOp(DataTagged& left, const DataConstant& right,
46  BinaryFunction operation)
47 {
48 // binaryOp(left,right.getPointDataView(),operation);
49  //
50  // perform the operation on each tagged value
51  const DataTagged::DataMapType& lookup=left.getTagLookup();
52  DataTagged::DataMapType::const_iterator i;
53  DataTagged::DataMapType::const_iterator lookupEnd=lookup.end();
54  DataTypes::ValueType& leftVec=left.getVectorRW();
55  const DataTypes::ShapeType& leftShape=left.getShape();
56  const DataTypes::ShapeType& rightShape=right.getShape();
57  double rvalue=right.getVectorRO()[0]; // for rank==0
58  const DataTypes::ValueType& rightVec=right.getVectorRO(); // for rank>0
59  if (right.getRank()==0) {
60  for (i=lookup.begin();i!=lookupEnd;i++) {
61  DataMaths::binaryOp(leftVec,leftShape,i->second,rvalue,operation);
62  }
63  } else {
64  for (i=lookup.begin();i!=lookupEnd;i++) {
65  DataMaths::binaryOp(leftVec, leftShape, i->second,rightVec,rightShape,0,operation);
66  }
67  }
68  //
69  // finally perform the operation on the default value
70  if (right.getRank()==0) {
71  DataMaths::binaryOp(leftVec,leftShape,left.getDefaultOffset(),rvalue,operation);
72  } else {
73  DataMaths::binaryOp(leftVec,leftShape,left.getDefaultOffset(),rightVec,rightShape,0,operation);
74  }
75 }
76 
82 template <class BinaryFunction>
83 inline void binaryOp(DataTagged& left, const DataTypes::ValueType& right,
84  const DataTypes::ShapeType& shape,
85  BinaryFunction operation)
86 {
87  //
88  // perform the operation on each tagged value
89  const DataTagged::DataMapType& lookup=left.getTagLookup();
90  DataTagged::DataMapType::const_iterator i;
91  DataTagged::DataMapType::const_iterator lookupEnd=lookup.end();
92  DataTypes::ValueType& lvec=left.getVectorRW();
93  const DataTypes::ShapeType& lshape=left.getShape();
94  if (DataTypes::getRank(shape)==0) {
95  for (i=lookup.begin();i!=lookupEnd;i++) {
96  DataMaths::binaryOp(lvec, lshape,i->second,right[0],operation);
97  }
98  } else {
99  for (i=lookup.begin();i!=lookupEnd;i++) {
100  DataMaths::binaryOp(lvec, lshape, i->second,right,shape,0,operation);
101  }
102  }
103  //
104  // finally perform the operation on the default value
105  if (DataTypes::getRank(shape)==0) {
106  DataMaths::binaryOp(lvec,lshape,left.getDefaultOffset(),right[0],operation);
107  } else {
108  DataMaths::binaryOp(lvec,lshape,left.getDefaultOffset(),right, shape,0,operation);
109  }
110 }
111 
112 
113 
114 
115 template <class BinaryFunction>
116 inline void binaryOp(DataTagged& left, const DataTagged& right,
117  BinaryFunction operation)
118 {
119  using namespace DataMaths;
120 
121  int right_rank=right.getRank();
122  //
123  // Add the right hand tag keys which can't currently be found on the left
124  const DataTagged::DataMapType& rightLookup=right.getTagLookup();
125  DataTagged::DataMapType::const_iterator i;
126  DataTagged::DataMapType::const_iterator rightLookupEnd=rightLookup.end();
127  for (i=rightLookup.begin();i!=rightLookupEnd;i++) {
128  //
129  // If the left does not already have a value assigned to this tag,
130  // add the right hand tag to the left hand tag list and assign
131  // the left's default value.
132  if (!left.isCurrentTag(i->first)) {
133  left.addTag(i->first);
134  }
135  }
136  DataTypes::ValueType& leftVec=left.getVectorRW();
137  const DataTypes::ShapeType& leftShape=left.getShape();
138  //
139  // Perform the operation.
140  const DataTagged::DataMapType& leftLookup=left.getTagLookup();
141  DataTagged::DataMapType::const_iterator leftLookupEnd=leftLookup.end();
142  for (i=leftLookup.begin();i!=leftLookupEnd;i++) {
143  if (right_rank==0) {
144  binaryOp(leftVec,leftShape,i->second, right.getDataByTagRO(i->first,0),operation);
145 
146  } else { // rank>0
147  binaryOp(leftVec,leftShape,left.getOffsetForTag(i->first),right.getVectorRO(), right.getShape(), right.getOffsetForTag(i->first), operation);
148  }
149  }
150  //
151  // finally perform the operation on the default value
152  if (right_rank==0) {
153  binaryOp(leftVec,leftShape, left.getDefaultOffset(), right.getVectorRO()[0],operation);
154  } else {
155  binaryOp(leftVec,leftShape, left.getDefaultOffset(), right.getVectorRO(), right.getShape(), right.getDefaultOffset(), operation);
156  }
157 }
158 
159 template <class BinaryFunction>
160 inline void binaryOp(DataConstant& left, const DataConstant& right,
161  BinaryFunction operation)
162 {
163  if (right.getRank()==0) {
164  double r=right.getVectorRO()[0];
165  DataMaths::binaryOp(left.getVectorRW(), left.getShape(),0, r,operation);
166  } else {
167  DataMaths::binaryOp(left.getVectorRW(), left.getShape(),0, right.getVectorRO(),right.getShape(),0,operation);
168  }
169 
170 }
171 
172 
173 
174 template <class BinaryFunction>
175 inline void binaryOp(DataExpanded& left, const DataReady& right,
176  BinaryFunction operation)
177 {
178  int i,j;
181  if (right.getRank()==0) {
182 
183  const DataTypes::ShapeType& leftShape=left.getShape();
184  DataTypes::ValueType& leftVec=left.getVectorRW();
185  //
186  // This will call the double version of binaryOp
187  #pragma omp parallel for private(i,j) schedule(static)
188  for (i=0;i<numSamples;i++) {
189  for (j=0;j<numDPPSample;j++) {
190  DataMaths::binaryOp(leftVec,leftShape,left.getPointOffset(i,j), right.getVectorRO()[right.getPointOffset(i,j)] ,operation);
191  }
192  }
193  } else {
194  #pragma omp parallel for private(i,j) schedule(static)
195  for (i=0;i<numSamples;i++) {
196  for (j=0;j<numDPPSample;j++) {
197  DataMaths::binaryOp(left.getVectorRW(),left.getShape(),left.getPointOffset(i,j), right.getVectorRO(), right.getShape(),right.getPointOffset(i,j), operation);
198  }
199  }
200  }
201 }
202 
203 
204 } // end of namespace
205 
206 #endif
DataVector implements an arbitrarily long vector of data values. DataVector is the underlying data co...
Definition: DataVector.h:44
void addTag(int tagKey)
addTag - does not modify the default value for this object. ** Not unit tested ** ...
Definition: DataTagged.cpp:432
bool isCurrentTag(int tag) const
isCurrentTag
Definition: DataTagged.h:600
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
DataTypes::ValueType::size_type getDefaultOffset() const
Returns the offset in the structure which stores the default value.
Definition: DataTagged.h:608
Definition: DataReady.h:35
Definition: AbstractContinuousDomain.cpp:24
void binaryOp(DataTypes::ValueType &left, const DataTypes::ShapeType &leftShape, DataTypes::ValueType::size_type leftOffset, const DataTypes::ValueType &right, const DataTypes::ShapeType &rightShape, DataTypes::ValueType::size_type rightOffset, BinaryFunction operation)
Perform the binary operation on the data points specified by the given offsets in the "left" and "rig...
Definition: DataMaths.h:842
DataConstant stores a single data point which represents the entire function space.
Definition: DataConstant.h:37
Give a short description of what DataExpanded does.
Definition: DataExpanded.h:44
const DataMapType & getTagLookup() const
getTagLookup
Definition: DataTagged.h:629
DataTypes::ValueType::const_reference getDataByTagRO(int tag, DataTypes::ValueType::size_type i) const
Definition: DataTagged.cpp:563
std::vector< int > ShapeType
The shape of a single datapoint.
Definition: DataTypes.h:38
Simulates a full dataset accessible via sampleNo and dataPointNo.
Definition: DataTagged.h:44
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
std::map< int, int > DataMapType
Definition: DataTagged.h:57
const DataTypes::ValueType & getVectorRO() const
Definition: DataConstant.cpp:395
DataTypes::ValueType & getVectorRW()
Return a reference to the underlying DataVector.
Definition: DataConstant.cpp:388
virtual const DataTypes::ValueType & getVectorRO() const =0
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
int getRank(const DataTypes::ShapeType &shape)
Return the rank (number of dimensions) of the given shape.
Definition: DataTypes.h:167
const DataTypes::ValueType & getVectorRO() const
Definition: DataTagged.cpp:897
long size_type
Definition: DataVector.h:60
virtual ValueType::size_type getPointOffset(int sampleNo, int dataPointNo) const =0
Return the offset for the given sample. This returns the offset for the given point into the containe...
unsigned int getRank() const
Return the rank information for the point data.
Definition: DataAbstract.h:603
Describes binary operations performed on DataVector.
void binaryOp(DataTagged &left, const DataConstant &right, BinaryFunction operation)
Perform the given binary operation.
Definition: BinaryOp.h:45
DataTypes::ValueType::size_type getOffsetForTag(int tag) const
getOffsetForTag
Definition: DataTagged.cpp:552