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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 5603 - (show annotations)
Mon May 4 04:15:50 2015 UTC (3 years, 11 months ago) by jfenwick
File MIME type: text/plain
File size: 7057 byte(s)
Fix unintended revert in copyright
1
2 /*****************************************************************************
3 *
4 * Copyright (c) 2003-2015 by The University of Queensland
5 * http://www.uq.edu.au
6 *
7 * Primary Business: Queensland, Australia
8 * Licensed under the Open Software License version 3.0
9 * http://www.opensource.org/licenses/osl-3.0.php
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
28 /**
29 \file BinaryOp.h
30 \brief Describes binary operations performed on instances of DataAbstract.
31
32 For operations on DataVector see DataMaths.h.
33 For operations on double* see LocalOps.h.
34 */
35
36 namespace escript {
37 /**
38 \brief
39 Perform the given binary operation.
40 \param left Input/Output - The left hand side.
41 \param right Input - The right hand side.
42 \param operation Input - The operation to perform.
43 */
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
77 /**
78 \brief apply the binary op to each value in left and the single value right.
79
80 The value in right will be assumed to begin at offset 0
81 */
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;
179 DataTypes::ValueType::size_type numDPPSample=left.getNumDPPSample();
180 DataTypes::ValueType::size_type numSamples=left.getNumSamples();
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

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.26