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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2271 - (show annotations)
Mon Feb 16 05:08:29 2009 UTC (10 years, 8 months ago) by jfenwick
File MIME type: text/plain
File size: 6708 byte(s)
Merging version 2269 to trunk

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 /**
26 \file BinaryOp.h
27 \brief Describes binary operations performed on instances of DataAbstract.
28
29 For operations on DataVector see DataMaths.h.
30 For operations on double* see LocalOps.h.
31 */
32
33 namespace escript {
34 /**
35 \brief
36 Perform the given binary operation.
37 \param left Input/Output - The left hand side.
38 \param right Input - The right hand side.
39 \param operation Input - The operation to perform.
40 */
41 template <class BinaryFunction>
42 inline void binaryOp(DataTagged& left, const DataConstant& right,
43 BinaryFunction operation)
44 {
45 // binaryOp(left,right.getPointDataView(),operation);
46 //
47 // perform the operation on each tagged value
48 const DataTagged::DataMapType& lookup=left.getTagLookup();
49 DataTagged::DataMapType::const_iterator i;
50 DataTagged::DataMapType::const_iterator lookupEnd=lookup.end();
51 DataTypes::ValueType& leftVec=left.getVectorRW();
52 const DataTypes::ShapeType& leftShape=left.getShape();
53 const DataTypes::ShapeType& rightShape=right.getShape();
54 double rvalue=right.getVectorRO()[0]; // for rank==0
55 const DataTypes::ValueType& rightVec=right.getVectorRO(); // for rank>0
56 if (right.getRank()==0) {
57 for (i=lookup.begin();i!=lookupEnd;i++) {
58 DataMaths::binaryOp(leftVec,leftShape,i->second,rvalue,operation);
59 }
60 } else {
61 for (i=lookup.begin();i!=lookupEnd;i++) {
62 DataMaths::binaryOp(leftVec, leftShape, i->second,rightVec,rightShape,0,operation);
63 }
64 }
65 //
66 // finally perform the operation on the default value
67 if (right.getRank()==0) {
68 DataMaths::binaryOp(leftVec,leftShape,left.getDefaultOffset(),rvalue,operation);
69 } else {
70 DataMaths::binaryOp(leftVec,leftShape,left.getDefaultOffset(),rightVec,rightShape,0,operation);
71 }
72 }
73
74 /**
75 \brief apply the binary op to each value in left and the single value right.
76
77 The value in right will be assumed to begin at offset 0
78 */
79 template <class BinaryFunction>
80 inline void binaryOp(DataTagged& left, const DataTypes::ValueType& right,
81 const DataTypes::ShapeType& shape,
82 BinaryFunction operation)
83 {
84 //
85 // perform the operation on each tagged value
86 const DataTagged::DataMapType& lookup=left.getTagLookup();
87 DataTagged::DataMapType::const_iterator i;
88 DataTagged::DataMapType::const_iterator lookupEnd=lookup.end();
89 DataTypes::ValueType& lvec=left.getVectorRW();
90 const DataTypes::ShapeType& lshape=left.getShape();
91 if (DataTypes::getRank(shape)==0) {
92 for (i=lookup.begin();i!=lookupEnd;i++) {
93 DataMaths::binaryOp(lvec, lshape,i->second,right[0],operation);
94 }
95 } else {
96 for (i=lookup.begin();i!=lookupEnd;i++) {
97 DataMaths::binaryOp(lvec, lshape, i->second,right,shape,0,operation);
98 }
99 }
100 //
101 // finally perform the operation on the default value
102 if (DataTypes::getRank(shape)==0) {
103 DataMaths::binaryOp(lvec,lshape,left.getDefaultOffset(),right[0],operation);
104 } else {
105 DataMaths::binaryOp(lvec,lshape,left.getDefaultOffset(),right, shape,0,operation);
106 }
107 }
108
109
110
111
112 template <class BinaryFunction>
113 inline void binaryOp(DataTagged& left, const DataTagged& right,
114 BinaryFunction operation)
115 {
116 using namespace DataMaths;
117
118 int right_rank=right.getRank();
119 //
120 // Add the right hand tag keys which can't currently be found on the left
121 const DataTagged::DataMapType& rightLookup=right.getTagLookup();
122 DataTagged::DataMapType::const_iterator i;
123 DataTagged::DataMapType::const_iterator rightLookupEnd=rightLookup.end();
124 for (i=rightLookup.begin();i!=rightLookupEnd;i++) {
125 //
126 // If the left does not already have a value assigned to this tag,
127 // add the right hand tag to the left hand tag list and assign
128 // the left's default value.
129 if (!left.isCurrentTag(i->first)) {
130 left.addTag(i->first);
131 }
132 }
133 DataTypes::ValueType& leftVec=left.getVectorRW();
134 const DataTypes::ShapeType& leftShape=left.getShape();
135 //
136 // Perform the operation.
137 const DataTagged::DataMapType& leftLookup=left.getTagLookup();
138 DataTagged::DataMapType::const_iterator leftLookupEnd=leftLookup.end();
139 for (i=leftLookup.begin();i!=leftLookupEnd;i++) {
140 if (right_rank==0) {
141 binaryOp(leftVec,leftShape,i->second, right.getDataByTagRO(i->first,0),operation);
142
143 } else { // rank>0
144 binaryOp(leftVec,leftShape,left.getOffsetForTag(i->first),right.getVectorRO(), right.getShape(), right.getOffsetForTag(i->first), operation);
145 }
146 }
147 //
148 // finally perform the operation on the default value
149 if (right_rank==0) {
150 binaryOp(leftVec,leftShape, left.getDefaultOffset(), right.getVectorRO()[0],operation);
151 } else {
152 binaryOp(leftVec,leftShape, left.getDefaultOffset(), right.getVectorRO(), right.getShape(), right.getDefaultOffset(), operation);
153 }
154 }
155
156 template <class BinaryFunction>
157 inline void binaryOp(DataConstant& left, const DataConstant& right,
158 BinaryFunction operation)
159 {
160
161 DataMaths::binaryOp(left.getVectorRW(), left.getShape(),0, right.getVectorRO(),right.getShape(),0,operation);
162
163 }
164
165
166
167 template <class BinaryFunction>
168 inline void binaryOp(DataExpanded& left, const DataReady& right,
169 BinaryFunction operation)
170 {
171 int i,j;
172 DataTypes::ValueType::size_type numDPPSample=left.getNumDPPSample();
173 DataTypes::ValueType::size_type numSamples=left.getNumSamples();
174 if (right.getRank()==0) {
175
176 const DataTypes::ShapeType& leftShape=left.getShape();
177 DataTypes::ValueType& leftVec=left.getVectorRW();
178 //
179 // This will call the double version of binaryOp
180 #pragma omp parallel for private(i,j) schedule(static)
181 for (i=0;i<numSamples;i++) {
182 for (j=0;j<numDPPSample;j++) {
183 DataMaths::binaryOp(leftVec,leftShape,left.getPointOffset(i,j), right.getVectorRO()[right.getPointOffset(i,j)] ,operation);
184 }
185 }
186 } else {
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(left.getVectorRW(),left.getShape(),left.getPointOffset(i,j), right.getVectorRO(), right.getShape(),right.getPointOffset(i,j), operation);
191 }
192 }
193 }
194 }
195
196
197 } // end of namespace
198
199 #endif

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.26