/[escript]/trunk/escript/src/BinaryOp.h
ViewVC logotype

Contents of /trunk/escript/src/BinaryOp.h

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1811 - (show annotations)
Thu Sep 25 23:11:13 2008 UTC (10 years, 11 months ago) by ksteube
File MIME type: text/plain
File size: 10072 byte(s)
Copyright updated in all files

1
2 /*******************************************************
3 *
4 * Copyright (c) 2003-2008 by University of Queensland
5 * Earth Systems Science Computational Center (ESSCC)
6 * http://www.uq.edu.au/esscc
7 *
8 * Primary Business: Queensland, Australia
9 * Licensed under the Open Software License version 3.0
10 * http://www.opensource.org/licenses/osl-3.0.php
11 *
12 *******************************************************/
13
14
15 #if !defined escript_BinaryOp_20040315_H
16 #define escript_BinaryOp_20040315_H
17 #include "system_dep.h"
18
19 #include "DataTypes.h"
20 #include "DataConstant.h"
21 #include "DataTagged.h"
22 #include "DataExpanded.h"
23 #include "DataMaths.h"
24
25 namespace escript {
26 /**
27 \brief
28 Perform the given binary operation.
29 \param left Input/Output - The left hand side.
30 \param right Input - The right hand side.
31 \param operation Input - The operation to perform.
32 */
33 template <class BinaryFunction>
34 inline void binaryOp(DataTagged& left, const DataConstant& right,
35 BinaryFunction operation)
36 {
37 // binaryOp(left,right.getPointDataView(),operation);
38 //
39 // perform the operation on each tagged value
40 const DataTagged::DataMapType& lookup=left.getTagLookup();
41 DataTagged::DataMapType::const_iterator i;
42 DataTagged::DataMapType::const_iterator lookupEnd=lookup.end();
43 // DataArrayView& leftView=left.getPointDataView();
44 DataTypes::ValueType& leftVec=left.getVector();
45 const DataTypes::ShapeType& leftShape=left.getShape();
46 const DataTypes::ShapeType& rightShape=right.getShape();
47 double rvalue=right.getVector()[0]; // for rank==0
48 const DataTypes::ValueType& rightVec=right.getVector(); // for rank>0
49 if (right.getRank()==0) {
50 for (i=lookup.begin();i!=lookupEnd;i++) {
51 //leftView.binaryOp(i->second,right(),operation);
52 DataMaths::binaryOp(leftVec,leftShape,i->second,rvalue,operation);
53 }
54 } else {
55 for (i=lookup.begin();i!=lookupEnd;i++) {
56 DataMaths::binaryOp(leftVec, leftShape, i->second,rightVec,rightShape,0,operation);
57 }
58 }
59 //
60 // finally perform the operation on the default value
61 if (right.getRank()==0) {
62 DataMaths::binaryOp(leftVec,leftShape,left.getDefaultOffset(),rvalue,operation);
63 } else {
64 DataMaths::binaryOp(leftVec,leftShape,left.getDefaultOffset(),rightVec,rightShape,0,operation);
65 }
66 }
67
68 // template <class BinaryFunction>
69 // inline void binaryOp(DataTagged& left, const DataArrayView& right,
70 // BinaryFunction operation)
71 // {
72 // //
73 // // perform the operation on each tagged value
74 // const DataTagged::DataMapType& lookup=left.getTagLookup();
75 // DataTagged::DataMapType::const_iterator i;
76 // DataTagged::DataMapType::const_iterator lookupEnd=lookup.end();
77 // DataArrayView& leftView=left.getPointDataView();
78 // if (right.getRank()==0) {
79 // for (i=lookup.begin();i!=lookupEnd;i++) {
80 // leftView.binaryOp(i->second,right(),operation);
81 // }
82 // } else {
83 // for (i=lookup.begin();i!=lookupEnd;i++) {
84 // leftView.binaryOp(i->second,right,right.getOffset(),operation);
85 // }
86 // }
87 // //
88 // // finally perform the operation on the default value
89 // if (right.getRank()==0) {
90 // left.getDefaultValue().binaryOp(0,right(),operation);
91 // } else {
92 // left.getDefaultValue().binaryOp(right,operation);
93 // }
94 // }
95
96 /**
97 \brief apply the binary op to each value in left and the single value right.
98
99 The value in right will be assumed to begin at offset 0
100 */
101 template <class BinaryFunction>
102 inline void binaryOp(DataTagged& left, const DataTypes::ValueType& right,
103 const DataTypes::ShapeType& shape,
104 BinaryFunction operation)
105 {
106 //
107 // perform the operation on each tagged value
108 const DataTagged::DataMapType& lookup=left.getTagLookup();
109 DataTagged::DataMapType::const_iterator i;
110 DataTagged::DataMapType::const_iterator lookupEnd=lookup.end();
111 // DataArrayView& leftView=left.getPointDataView();
112 DataTypes::ValueType& lvec=left.getVector();
113 const DataTypes::ShapeType& lshape=left.getShape();
114 if (DataTypes::getRank(shape)==0) {
115 for (i=lookup.begin();i!=lookupEnd;i++) {
116 DataMaths::binaryOp(lvec, lshape,i->second,right[0],operation);
117 }
118 } else {
119 for (i=lookup.begin();i!=lookupEnd;i++) {
120 DataMaths::binaryOp(lvec, lshape, i->second,right,shape,0,operation);
121 }
122 }
123 //
124 // finally perform the operation on the default value
125 if (DataTypes::getRank(shape)==0) {
126 DataMaths::binaryOp(lvec,lshape,left.getDefaultOffset(),right[0],operation);
127 // left.getDefaultValue().binaryOp(0,right(),operation);
128 } else {
129 // left.getDefaultValue().binaryOp(right,operation);
130 DataMaths::binaryOp(lvec,lshape,left.getDefaultOffset(),right, shape,0,operation);
131 }
132 }
133
134
135
136
137 template <class BinaryFunction>
138 inline void binaryOp(DataTagged& left, const DataTagged& right,
139 BinaryFunction operation)
140 {
141 using namespace DataMaths;
142
143 int right_rank=right.getRank();
144 //
145 // Add the right hand tag keys which can't currently be found on the left
146 const DataTagged::DataMapType& rightLookup=right.getTagLookup();
147 DataTagged::DataMapType::const_iterator i;
148 DataTagged::DataMapType::const_iterator rightLookupEnd=rightLookup.end();
149 for (i=rightLookup.begin();i!=rightLookupEnd;i++) {
150 //
151 // If the left does not already have a value assigned to this tag,
152 // add the right hand tag to the left hand tag list and assign
153 // the left's default value.
154 if (!left.isCurrentTag(i->first)) {
155 left.addTag(i->first);
156 }
157 }
158 DataTypes::ValueType& leftVec=left.getVector();
159 const DataTypes::ShapeType& leftShape=left.getShape();
160 //
161 // Perform the operation.
162 const DataTagged::DataMapType& leftLookup=left.getTagLookup();
163 DataTagged::DataMapType::const_iterator leftLookupEnd=leftLookup.end();
164 for (i=leftLookup.begin();i!=leftLookupEnd;i++) {
165 if (right_rank==0) {
166 /* left.getDataPointByTag(i->first).binaryOp(i->second,right.getDataPointByTag(i->first)(),operation);*/
167 binaryOp(leftVec,leftShape,i->second, right.getDataByTag(i->first,0),operation);
168
169 } else { // rank>0
170 binaryOp(leftVec,leftShape,left.getOffsetForTag(i->first),right.getVector(), right.getShape(), right.getOffsetForTag(i->first), operation);
171 //left.getDataPointByTag(i->first).binaryOp(right.getDataPointByTag(i->first),operation);
172 }
173 }
174 //
175 // finally perform the operation on the default value
176 if (right_rank==0) {
177 //left.getDefaultValue().binaryOp(0,right.getDefaultValue()(),operation);
178 binaryOp(leftVec,leftShape, left.getDefaultOffset(), right.getVector()[0],operation);
179 } else {
180 //left.getDefaultValue().binaryOp(right.getDefaultValue(),operation);
181 binaryOp(leftVec,leftShape, left.getDefaultOffset(), right.getVector(), right.getShape(), right.getDefaultOffset(), operation);
182 }
183 }
184
185 template <class BinaryFunction>
186 inline void binaryOp(DataConstant& left, const DataConstant& right,
187 BinaryFunction operation)
188 {
189 //
190 // As DataConstant there is only one point data
191 // binaryOp(left,right.getPointDataView(),operation);
192
193 DataMaths::binaryOp(left.getVector(), left.getShape(),0, right.getVector(),right.getShape(),0,operation);
194
195 }
196
197 // template <class BinaryFunction>
198 // inline void binaryOp(DataConstant& left, const DataArrayView& right,
199 // BinaryFunction operation)
200 // {
201 // //
202 // // perform an operand check, this will throw on error
203 // if (right.getRank()==0) {
204 // //
205 // // special case of applying a single value to the entire array
206 // left.getPointDataView().binaryOp(right(),operation);
207 // } else {
208 // left.getPointDataView().binaryOp(right,operation);
209 // }
210 // }
211
212
213 template <class BinaryFunction>
214 inline void binaryOp(DataExpanded& left, const DataAbstract& right,
215 BinaryFunction operation)
216 {
217 int i,j;
218 DataTypes::ValueType::size_type numDPPSample=left.getNumDPPSample();
219 DataTypes::ValueType::size_type numSamples=left.getNumSamples();
220 if (right.getRank()==0) {
221
222 const DataTypes::ShapeType& leftShape=left.getShape();
223 DataTypes::ValueType& leftVec=left.getVector();
224 //
225 // This will call the double version of binaryOp
226 #pragma omp parallel for private(i,j) schedule(static)
227 for (i=0;i<numSamples;i++) {
228 for (j=0;j<numDPPSample;j++) {
229 // left.getPointDataView().binaryOp(left.getPointOffset(i,j),
230 // right.getPointDataView().getData(right.getPointOffset(i,j)),
231 // operation);
232 DataMaths::binaryOp(leftVec,leftShape,left.getPointOffset(i,j), right.getVector()[right.getPointOffset(i,j)] ,operation);
233 }
234 }
235 } else {
236 #pragma omp parallel for private(i,j) schedule(static)
237 for (i=0;i<numSamples;i++) {
238 for (j=0;j<numDPPSample;j++) {
239 // left.getPointDataView().binaryOp(left.getPointOffset(i,j),
240 // right.getPointDataView(),
241 // right.getPointOffset(i,j),
242 // operation);
243 DataMaths::binaryOp(left.getVector(),left.getShape(),left.getPointOffset(i,j), right.getVector(), right.getShape(),right.getPointOffset(i,j), operation);
244 }
245 }
246 }
247 }
248
249 // template <class BinaryFunction>
250 // inline void binaryOp(DataExpanded& left, const DataArrayView& right,
251 // BinaryFunction operation)
252 // {
253 // int i,j;
254 // DataTypes::ValueType::size_type numDPPSample=left.getNumDPPSample();
255 // DataTypes::ValueType::size_type numSamples=left.getNumSamples();
256 // if (right.getRank()==0) {
257 // //
258 // // This will call the double version of binaryOp
259 // #pragma omp parallel for private(i,j) schedule(static)
260 // for (i=0;i<numSamples;i++) {
261 // for (j=0;j<numDPPSample;j++) {
262 // left.getPointDataView().binaryOp(left.getPointOffset(i,j),
263 // right(),
264 // operation);
265 // }
266 // }
267 // } else {
268 // #pragma omp parallel for private(i,j) schedule(static)
269 // for (i=0;i<numSamples;i++) {
270 // for (j=0;j<numDPPSample;j++) {
271 // left.getPointDataView().binaryOp(left.getPointOffset(i,j),
272 // right,
273 // 0,
274 // operation);
275 // }
276 // }
277 // }
278 // }
279
280 } // end of namespace
281
282 #endif

Properties

Name Value
svn:eol-style native
svn:keywords Author Date Id Revision

  ViewVC Help
Powered by ViewVC 1.1.26