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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1796 - (show annotations)
Wed Sep 17 01:45:46 2008 UTC (10 years, 7 months ago) by jfenwick
File MIME type: text/plain
File size: 10107 byte(s)
Merged noarrayview branch onto trunk.


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

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.26