/[escript]/branches/schroedinger/escript/src/BinaryOp.h
ViewVC logotype

Contents of /branches/schroedinger/escript/src/BinaryOp.h

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1864 - (show annotations)
Thu Oct 9 03:09:30 2008 UTC (11 years, 7 months ago) by jfenwick
File MIME type: text/plain
File size: 7475 byte(s)
Branch commit
It compiles but doesn't do much.


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 /**
69 \brief apply the binary op to each value in left and the single value right.
70
71 The value in right will be assumed to begin at offset 0
72 */
73 template <class BinaryFunction>
74 inline void binaryOp(DataTagged& left, const DataTypes::ValueType& right,
75 const DataTypes::ShapeType& shape,
76 BinaryFunction operation)
77 {
78 //
79 // perform the operation on each tagged value
80 const DataTagged::DataMapType& lookup=left.getTagLookup();
81 DataTagged::DataMapType::const_iterator i;
82 DataTagged::DataMapType::const_iterator lookupEnd=lookup.end();
83 // DataArrayView& leftView=left.getPointDataView();
84 DataTypes::ValueType& lvec=left.getVector();
85 const DataTypes::ShapeType& lshape=left.getShape();
86 if (DataTypes::getRank(shape)==0) {
87 for (i=lookup.begin();i!=lookupEnd;i++) {
88 DataMaths::binaryOp(lvec, lshape,i->second,right[0],operation);
89 }
90 } else {
91 for (i=lookup.begin();i!=lookupEnd;i++) {
92 DataMaths::binaryOp(lvec, lshape, i->second,right,shape,0,operation);
93 }
94 }
95 //
96 // finally perform the operation on the default value
97 if (DataTypes::getRank(shape)==0) {
98 DataMaths::binaryOp(lvec,lshape,left.getDefaultOffset(),right[0],operation);
99 // left.getDefaultValue().binaryOp(0,right(),operation);
100 } else {
101 // left.getDefaultValue().binaryOp(right,operation);
102 DataMaths::binaryOp(lvec,lshape,left.getDefaultOffset(),right, shape,0,operation);
103 }
104 }
105
106
107
108
109 template <class BinaryFunction>
110 inline void binaryOp(DataTagged& left, const DataTagged& right,
111 BinaryFunction operation)
112 {
113 using namespace DataMaths;
114
115 int right_rank=right.getRank();
116 //
117 // Add the right hand tag keys which can't currently be found on the left
118 const DataTagged::DataMapType& rightLookup=right.getTagLookup();
119 DataTagged::DataMapType::const_iterator i;
120 DataTagged::DataMapType::const_iterator rightLookupEnd=rightLookup.end();
121 for (i=rightLookup.begin();i!=rightLookupEnd;i++) {
122 //
123 // If the left does not already have a value assigned to this tag,
124 // add the right hand tag to the left hand tag list and assign
125 // the left's default value.
126 if (!left.isCurrentTag(i->first)) {
127 left.addTag(i->first);
128 }
129 }
130 DataTypes::ValueType& leftVec=left.getVector();
131 const DataTypes::ShapeType& leftShape=left.getShape();
132 //
133 // Perform the operation.
134 const DataTagged::DataMapType& leftLookup=left.getTagLookup();
135 DataTagged::DataMapType::const_iterator leftLookupEnd=leftLookup.end();
136 for (i=leftLookup.begin();i!=leftLookupEnd;i++) {
137 if (right_rank==0) {
138 /* left.getDataPointByTag(i->first).binaryOp(i->second,right.getDataPointByTag(i->first)(),operation);*/
139 binaryOp(leftVec,leftShape,i->second, right.getDataByTag(i->first,0),operation);
140
141 } else { // rank>0
142 binaryOp(leftVec,leftShape,left.getOffsetForTag(i->first),right.getVector(), right.getShape(), right.getOffsetForTag(i->first), operation);
143 //left.getDataPointByTag(i->first).binaryOp(right.getDataPointByTag(i->first),operation);
144 }
145 }
146 //
147 // finally perform the operation on the default value
148 if (right_rank==0) {
149 //left.getDefaultValue().binaryOp(0,right.getDefaultValue()(),operation);
150 binaryOp(leftVec,leftShape, left.getDefaultOffset(), right.getVector()[0],operation);
151 } else {
152 //left.getDefaultValue().binaryOp(right.getDefaultValue(),operation);
153 binaryOp(leftVec,leftShape, left.getDefaultOffset(), right.getVector(), right.getShape(), right.getDefaultOffset(), operation);
154 }
155 }
156
157 template <class BinaryFunction>
158 inline void binaryOp(DataConstant& left, const DataConstant& right,
159 BinaryFunction operation)
160 {
161
162 DataMaths::binaryOp(left.getVector(), left.getShape(),0, right.getVector(),right.getShape(),0,operation);
163
164 }
165
166
167
168 template <class BinaryFunction>
169 inline void binaryOp(DataExpanded& left, const DataReady& right,
170 BinaryFunction operation)
171 {
172 int i,j;
173 DataTypes::ValueType::size_type numDPPSample=left.getNumDPPSample();
174 DataTypes::ValueType::size_type numSamples=left.getNumSamples();
175 if (right.getRank()==0) {
176
177 const DataTypes::ShapeType& leftShape=left.getShape();
178 DataTypes::ValueType& leftVec=left.getVector();
179 //
180 // This will call the double version of binaryOp
181 #pragma omp parallel for private(i,j) schedule(static)
182 for (i=0;i<numSamples;i++) {
183 for (j=0;j<numDPPSample;j++) {
184 // left.getPointDataView().binaryOp(left.getPointOffset(i,j),
185 // right.getPointDataView().getData(right.getPointOffset(i,j)),
186 // operation);
187 DataMaths::binaryOp(leftVec,leftShape,left.getPointOffset(i,j), right.getVector()[right.getPointOffset(i,j)] ,operation);
188 }
189 }
190 } else {
191 #pragma omp parallel for private(i,j) schedule(static)
192 for (i=0;i<numSamples;i++) {
193 for (j=0;j<numDPPSample;j++) {
194 // left.getPointDataView().binaryOp(left.getPointOffset(i,j),
195 // right.getPointDataView(),
196 // right.getPointOffset(i,j),
197 // operation);
198 DataMaths::binaryOp(left.getVector(),left.getShape(),left.getPointOffset(i,j), right.getVector(), right.getShape(),right.getPointOffset(i,j), operation);
199 }
200 }
201 }
202 }
203
204
205 } // end of namespace
206
207 #endif

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.26