1 |
/* |
2 |
****************************************************************************** |
3 |
* * |
4 |
* COPYRIGHT ACcESS 2004 - All Rights Reserved * |
5 |
* * |
6 |
* This software is the property of ACcESS. No part of this code * |
7 |
* may be copied in any form or by any means without the expressed written * |
8 |
* consent of ACcESS. Copying, use or modification of this software * |
9 |
* by any unauthorised person is illegal unless that person has a software * |
10 |
* license agreement with ACcESS. * |
11 |
* * |
12 |
****************************************************************************** |
13 |
*/ |
14 |
|
15 |
#if !defined escript_BinaryOp_20040315_H |
16 |
#define escript_BinaryOp_20040315_H |
17 |
|
18 |
#include "DataArrayView.h" |
19 |
#include "DataConstant.h" |
20 |
#include "DataTagged.h" |
21 |
#include "DataExpanded.h" |
22 |
|
23 |
namespace escript { |
24 |
/** |
25 |
\brief |
26 |
Perform the given binary operation. |
27 |
\param left Input/Output - The left hand side. |
28 |
\param right Input - The right hand side. |
29 |
\param operation Input - The operation to perform. |
30 |
*/ |
31 |
template <class BinaryFunction> |
32 |
inline void binaryOp(DataTagged& left, const DataConstant& right, |
33 |
BinaryFunction operation) |
34 |
{ |
35 |
binaryOp(left,right.getPointDataView(),operation); |
36 |
} |
37 |
|
38 |
template <class BinaryFunction> |
39 |
inline void binaryOp(DataTagged& left, const DataArrayView& right, |
40 |
BinaryFunction operation) |
41 |
{ |
42 |
// |
43 |
// perform the operation on each tagged value |
44 |
const DataTagged::DataMapType& lookup=left.getTagLookup(); |
45 |
DataTagged::DataMapType::const_iterator i; |
46 |
DataTagged::DataMapType::const_iterator lookupEnd=lookup.end(); |
47 |
DataArrayView& leftView=left.getPointDataView(); |
48 |
if (right.getRank()==0) { |
49 |
for (i=lookup.begin();i!=lookupEnd;i++) { |
50 |
leftView.binaryOp(i->second,right(),operation); |
51 |
} |
52 |
} else { |
53 |
for (i=lookup.begin();i!=lookupEnd;i++) { |
54 |
leftView.binaryOp(i->second,right,right.getOffset(),operation); |
55 |
} |
56 |
} |
57 |
// |
58 |
// finally perform the operation on the default value |
59 |
if (right.getRank()==0) { |
60 |
left.getDefaultValue().binaryOp(0,right(),operation); |
61 |
} else { |
62 |
left.getDefaultValue().binaryOp(right,operation); |
63 |
} |
64 |
} |
65 |
|
66 |
template <class BinaryFunction> |
67 |
inline void binaryOp(DataTagged& left, const DataTagged& right, |
68 |
BinaryFunction operation) |
69 |
{ |
70 |
// |
71 |
// Add the right hand tag keys which can't currently be found on the left |
72 |
const DataTagged::DataMapType& rightLookup=right.getTagLookup(); |
73 |
DataTagged::DataMapType::const_iterator i; |
74 |
DataTagged::DataMapType::const_iterator rightLookupEnd=rightLookup.end(); |
75 |
for (i=rightLookup.begin();i!=rightLookupEnd;i++) { |
76 |
// |
77 |
// If the left does not already have a value assigned to this tag, |
78 |
// add the right hand tag to the left hand tag list and assign |
79 |
// the left's default value. |
80 |
if (!left.isCurrentTag(i->first)) { |
81 |
left.addTaggedValue(i->first,left.getDefaultValue()); |
82 |
} |
83 |
} |
84 |
// |
85 |
// Perform the operation. |
86 |
const DataTagged::DataMapType& leftLookup=left.getTagLookup(); |
87 |
DataTagged::DataMapType::const_iterator leftLookupEnd=leftLookup.end(); |
88 |
for (i=leftLookup.begin();i!=leftLookupEnd;i++) { |
89 |
left.getDataPointByTag(i->first).binaryOp(right.getDataPointByTag(i->first),operation); |
90 |
} |
91 |
// |
92 |
// finally perform the operation on the default value |
93 |
left.getDefaultValue().binaryOp(right.getDefaultValue(),operation); |
94 |
} |
95 |
|
96 |
template <class BinaryFunction> |
97 |
inline void binaryOp(DataConstant& left, const DataConstant& right, |
98 |
BinaryFunction operation) |
99 |
{ |
100 |
// |
101 |
// As DataConstant there is only one point data |
102 |
binaryOp(left,right.getPointDataView(),operation); |
103 |
} |
104 |
|
105 |
template <class BinaryFunction> |
106 |
inline void binaryOp(DataConstant& left, const DataArrayView& right, |
107 |
BinaryFunction operation) |
108 |
{ |
109 |
// |
110 |
// perform an operand check, this will throw on error |
111 |
if (right.getRank()==0) { |
112 |
// |
113 |
// special case of applying a single value to the entire array |
114 |
left.getPointDataView().binaryOp(right(),operation); |
115 |
} else { |
116 |
left.getPointDataView().binaryOp(right,operation); |
117 |
} |
118 |
} |
119 |
|
120 |
template <class BinaryFunction> |
121 |
inline void binaryOp(DataExpanded& left, const DataAbstract& right, |
122 |
BinaryFunction operation) |
123 |
{ |
124 |
int i,j; |
125 |
DataArrayView::ValueType::size_type numDPPSample=left.getNumDPPSample(); |
126 |
DataArrayView::ValueType::size_type numSamples=left.getNumSamples(); |
127 |
if (right.getPointDataView().getRank()==0) { |
128 |
// |
129 |
// This will call the double version of binaryOp |
130 |
#pragma omp parallel for private(i,j) schedule(static) |
131 |
for (i=0;i<numSamples;i++) { |
132 |
for (j=0;j<numDPPSample;j++) { |
133 |
left.getPointDataView().binaryOp(left.getPointOffset(i,j), |
134 |
right.getPointDataView().getData(right.getPointOffset(i,j)), |
135 |
operation); |
136 |
} |
137 |
} |
138 |
} else { |
139 |
#pragma omp parallel for private(i,j) schedule(static) |
140 |
for (i=0;i<numSamples;i++) { |
141 |
for (j=0;j<numDPPSample;j++) { |
142 |
left.getPointDataView().binaryOp(left.getPointOffset(i,j), |
143 |
right.getPointDataView(), |
144 |
right.getPointOffset(i,j), |
145 |
operation); |
146 |
} |
147 |
} |
148 |
} |
149 |
} |
150 |
|
151 |
template <class BinaryFunction> |
152 |
inline void binaryOp(DataExpanded& left, const DataArrayView& right, |
153 |
BinaryFunction operation) |
154 |
{ |
155 |
int i,j; |
156 |
DataArrayView::ValueType::size_type numDPPSample=left.getNumDPPSample(); |
157 |
DataArrayView::ValueType::size_type numSamples=left.getNumSamples(); |
158 |
if (right.getRank()==0) { |
159 |
// |
160 |
// This will call the double version of binaryOp |
161 |
#pragma omp parallel for private(i,j) schedule(static) |
162 |
for (i=0;i<numSamples;i++) { |
163 |
for (j=0;j<numDPPSample;j++) { |
164 |
left.getPointDataView().binaryOp(left.getPointOffset(i,j), |
165 |
right(), |
166 |
operation); |
167 |
} |
168 |
} |
169 |
} else { |
170 |
#pragma omp parallel for private(i,j) schedule(static) |
171 |
for (i=0;i<numSamples;i++) { |
172 |
for (j=0;j<numDPPSample;j++) { |
173 |
left.getPointDataView().binaryOp(left.getPointOffset(i,j), |
174 |
right, |
175 |
0, |
176 |
operation); |
177 |
} |
178 |
} |
179 |
} |
180 |
} |
181 |
|
182 |
} // end of namespace |
183 |
|
184 |
#endif |