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 |
// |
36 |
// perform the operation on each tagged value including the default |
37 |
binaryOp(left,right.getPointDataView(),operation); |
38 |
} |
39 |
|
40 |
template <class BinaryFunction> |
41 |
inline void binaryOp(DataTagged& left, const DataArrayView& right, |
42 |
BinaryFunction operation) |
43 |
{ |
44 |
// |
45 |
// perform the operation on each tagged value |
46 |
const DataTagged::DataMapType& lookup=left.getTagLookup(); |
47 |
DataTagged::DataMapType::const_iterator i; |
48 |
DataTagged::DataMapType::const_iterator lookupEnd=lookup.end(); |
49 |
DataArrayView& leftView=left.getPointDataView(); |
50 |
if (right.getRank()==0) { |
51 |
for (i=lookup.begin();i!=lookupEnd;i++) { |
52 |
leftView.binaryOp(i->second,right(),operation); |
53 |
} |
54 |
} else { |
55 |
for (i=lookup.begin();i!=lookupEnd;i++) { |
56 |
leftView.binaryOp(i->second,right,right.getOffset(),operation); |
57 |
} |
58 |
} |
59 |
// |
60 |
// finally perform the operation on the default value |
61 |
left.getDefaultValue().binaryOp(right,operation); |
62 |
} |
63 |
|
64 |
template <class BinaryFunction> |
65 |
inline void binaryOp(DataTagged& left, const DataTagged& right, |
66 |
BinaryFunction operation) |
67 |
{ |
68 |
// |
69 |
// Add the right hand tag keys which can't currently be found on the left |
70 |
const DataTagged::DataMapType& rightLookup=right.getTagLookup(); |
71 |
DataTagged::DataMapType::const_iterator i; |
72 |
DataTagged::DataMapType::const_iterator rightLookupEnd=rightLookup.end(); |
73 |
for (i=rightLookup.begin();i!=rightLookupEnd;i++) { |
74 |
// |
75 |
// If the left does not already have a value assigned to this tag, |
76 |
// add the right hand tag to the left hand tag list and assign |
77 |
// the right's default value. |
78 |
if (!left.isCurrentTag(i->first)) { |
79 |
left.addTaggedValue(i->first,right.getDefaultValue()); |
80 |
} |
81 |
} |
82 |
// |
83 |
// Perform the operation. Any tags originally in the left which don't exist for |
84 |
// the right hand side will use the right's default value as the right operand |
85 |
const DataTagged::DataMapType& leftLookup=left.getTagLookup(); |
86 |
DataTagged::DataMapType::const_iterator leftLookupEnd=leftLookup.end(); |
87 |
for (i=leftLookup.begin();i!=leftLookupEnd;i++) { |
88 |
left.getDataPointByTag(i->first).binaryOp(right.getDataPointByTag(i->first),operation); |
89 |
} |
90 |
// |
91 |
// finally perform the operation on the default value |
92 |
left.getDefaultValue().binaryOp(right.getDefaultValue(),operation); |
93 |
} |
94 |
|
95 |
template <class BinaryFunction> |
96 |
inline void binaryOp(DataConstant& left, const DataConstant& right, |
97 |
BinaryFunction operation) |
98 |
{ |
99 |
// |
100 |
// As DataConstant there is only one point data |
101 |
binaryOp(left,right.getPointDataView(),operation); |
102 |
} |
103 |
|
104 |
template <class BinaryFunction> |
105 |
inline void binaryOp(DataConstant& left, const DataArrayView& right, |
106 |
BinaryFunction operation) |
107 |
{ |
108 |
// |
109 |
// perform an operand check, this will throw on error |
110 |
if (right.getRank()==0) { |
111 |
// |
112 |
// special case of applying a single value to the entire array |
113 |
left.getPointDataView().binaryOp(right(),operation); |
114 |
} else { |
115 |
left.getPointDataView().binaryOp(right,operation); |
116 |
} |
117 |
} |
118 |
|
119 |
template <class BinaryFunction> |
120 |
inline void binaryOp(DataExpanded& left, const DataAbstract& right, |
121 |
BinaryFunction operation) |
122 |
{ |
123 |
int i,j; |
124 |
DataArrayView::ValueType::size_type numDPPSample=left.getNumDPPSample(); |
125 |
DataArrayView::ValueType::size_type numSamples=left.getNumSamples(); |
126 |
if (right.getPointDataView().getRank()==0) { |
127 |
// |
128 |
// This will call the double version of binaryOp |
129 |
#pragma omp parallel for private(i,j) schedule(static) |
130 |
for (i=0;i<numSamples;i++) { |
131 |
for (j=0;j<numDPPSample;j++) { |
132 |
left.getPointDataView().binaryOp(left.getPointOffset(i,j), |
133 |
right.getPointDataView().getData(right.getPointOffset(i,j)), |
134 |
operation); |
135 |
} |
136 |
} |
137 |
} else { |
138 |
#pragma omp parallel for private(i,j) schedule(static) |
139 |
for (i=0;i<numSamples;i++) { |
140 |
for (j=0;j<numDPPSample;j++) { |
141 |
left.getPointDataView().binaryOp(left.getPointOffset(i,j), |
142 |
right.getPointDataView(), |
143 |
right.getPointOffset(i,j), |
144 |
operation); |
145 |
} |
146 |
} |
147 |
} |
148 |
} |
149 |
|
150 |
template <class BinaryFunction> |
151 |
inline void binaryOp(DataExpanded& left, const DataArrayView& right, |
152 |
BinaryFunction operation) |
153 |
{ |
154 |
int i,j; |
155 |
DataArrayView::ValueType::size_type numDPPSample=left.getNumDPPSample(); |
156 |
DataArrayView::ValueType::size_type numSamples=left.getNumSamples(); |
157 |
if (right.getRank()==0) { |
158 |
// |
159 |
// This will call the double version of binaryOp |
160 |
#pragma omp parallel for private(i,j) schedule(static) |
161 |
for (i=0;i<numSamples;i++) { |
162 |
for (j=0;j<numDPPSample;j++) { |
163 |
left.getPointDataView().binaryOp(left.getPointOffset(i,j), |
164 |
right(), |
165 |
operation); |
166 |
} |
167 |
} |
168 |
} else { |
169 |
#pragma omp parallel for private(i,j) schedule(static) |
170 |
for (i=0;i<numSamples;i++) { |
171 |
for (j=0;j<numDPPSample;j++) { |
172 |
left.getPointDataView().binaryOp(left.getPointOffset(i,j), |
173 |
right, |
174 |
0, |
175 |
operation); |
176 |
} |
177 |
} |
178 |
} |
179 |
} |
180 |
|
181 |
} // end of namespace |
182 |
|
183 |
#endif |