1 |
/* |
2 |
************************************************************ |
3 |
* Copyright 2006 by ACcESS MNRF * |
4 |
* * |
5 |
* http://www.access.edu.au * |
6 |
* Primary Business: Queensland, Australia * |
7 |
* Licensed under the Open Software License version 3.0 * |
8 |
* http://www.opensource.org/licenses/osl-3.0.php * |
9 |
* * |
10 |
************************************************************ |
11 |
|
12 |
*/ |
13 |
|
14 |
#if !defined escript_BinaryOp_20040315_H |
15 |
#define escript_BinaryOp_20040315_H |
16 |
|
17 |
#include "DataArrayView.h" |
18 |
#include "DataConstant.h" |
19 |
#include "DataTagged.h" |
20 |
#include "DataExpanded.h" |
21 |
|
22 |
namespace escript { |
23 |
/** |
24 |
\brief |
25 |
Perform the given binary operation. |
26 |
\param left Input/Output - The left hand side. |
27 |
\param right Input - The right hand side. |
28 |
\param operation Input - The operation to perform. |
29 |
*/ |
30 |
template <class BinaryFunction> |
31 |
inline void binaryOp(DataTagged& left, const DataConstant& right, |
32 |
BinaryFunction operation) |
33 |
{ |
34 |
binaryOp(left,right.getPointDataView(),operation); |
35 |
} |
36 |
|
37 |
template <class BinaryFunction> |
38 |
inline void binaryOp(DataTagged& left, const DataArrayView& right, |
39 |
BinaryFunction operation) |
40 |
{ |
41 |
// |
42 |
// perform the operation on each tagged value |
43 |
const DataTagged::DataMapType& lookup=left.getTagLookup(); |
44 |
DataTagged::DataMapType::const_iterator i; |
45 |
DataTagged::DataMapType::const_iterator lookupEnd=lookup.end(); |
46 |
DataArrayView& leftView=left.getPointDataView(); |
47 |
if (right.getRank()==0) { |
48 |
for (i=lookup.begin();i!=lookupEnd;i++) { |
49 |
leftView.binaryOp(i->second,right(),operation); |
50 |
} |
51 |
} else { |
52 |
for (i=lookup.begin();i!=lookupEnd;i++) { |
53 |
leftView.binaryOp(i->second,right,right.getOffset(),operation); |
54 |
} |
55 |
} |
56 |
// |
57 |
// finally perform the operation on the default value |
58 |
if (right.getRank()==0) { |
59 |
left.getDefaultValue().binaryOp(0,right(),operation); |
60 |
} else { |
61 |
left.getDefaultValue().binaryOp(right,operation); |
62 |
} |
63 |
} |
64 |
|
65 |
template <class BinaryFunction> |
66 |
inline void binaryOp(DataTagged& left, const DataTagged& right, |
67 |
BinaryFunction operation) |
68 |
{ |
69 |
int right_rank=right.getPointDataView().getRank(); |
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 |
if (right_rank==0) { |
90 |
left.getDataPointByTag(i->first).binaryOp(i->second,right.getDataPointByTag(i->first)(),operation); |
91 |
} else { |
92 |
left.getDataPointByTag(i->first).binaryOp(right.getDataPointByTag(i->first),operation); |
93 |
} |
94 |
} |
95 |
// |
96 |
// finally perform the operation on the default value |
97 |
if (right_rank==0) { |
98 |
left.getDefaultValue().binaryOp(0,right.getDefaultValue()(),operation); |
99 |
} else { |
100 |
left.getDefaultValue().binaryOp(right.getDefaultValue(),operation); |
101 |
} |
102 |
} |
103 |
|
104 |
template <class BinaryFunction> |
105 |
inline void binaryOp(DataConstant& left, const DataConstant& right, |
106 |
BinaryFunction operation) |
107 |
{ |
108 |
// |
109 |
// As DataConstant there is only one point data |
110 |
binaryOp(left,right.getPointDataView(),operation); |
111 |
} |
112 |
|
113 |
template <class BinaryFunction> |
114 |
inline void binaryOp(DataConstant& left, const DataArrayView& right, |
115 |
BinaryFunction operation) |
116 |
{ |
117 |
// |
118 |
// perform an operand check, this will throw on error |
119 |
if (right.getRank()==0) { |
120 |
// |
121 |
// special case of applying a single value to the entire array |
122 |
left.getPointDataView().binaryOp(right(),operation); |
123 |
} else { |
124 |
left.getPointDataView().binaryOp(right,operation); |
125 |
} |
126 |
} |
127 |
|
128 |
template <class BinaryFunction> |
129 |
inline void binaryOp(DataExpanded& left, const DataAbstract& right, |
130 |
BinaryFunction operation) |
131 |
{ |
132 |
int i,j; |
133 |
DataArrayView::ValueType::size_type numDPPSample=left.getNumDPPSample(); |
134 |
DataArrayView::ValueType::size_type numSamples=left.getNumSamples(); |
135 |
if (right.getPointDataView().getRank()==0) { |
136 |
// |
137 |
// This will call the double version of binaryOp |
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().getData(right.getPointOffset(i,j)), |
143 |
operation); |
144 |
} |
145 |
} |
146 |
} else { |
147 |
#pragma omp parallel for private(i,j) schedule(static) |
148 |
for (i=0;i<numSamples;i++) { |
149 |
for (j=0;j<numDPPSample;j++) { |
150 |
left.getPointDataView().binaryOp(left.getPointOffset(i,j), |
151 |
right.getPointDataView(), |
152 |
right.getPointOffset(i,j), |
153 |
operation); |
154 |
} |
155 |
} |
156 |
} |
157 |
} |
158 |
|
159 |
template <class BinaryFunction> |
160 |
inline void binaryOp(DataExpanded& left, const DataArrayView& right, |
161 |
BinaryFunction operation) |
162 |
{ |
163 |
int i,j; |
164 |
DataArrayView::ValueType::size_type numDPPSample=left.getNumDPPSample(); |
165 |
DataArrayView::ValueType::size_type numSamples=left.getNumSamples(); |
166 |
if (right.getRank()==0) { |
167 |
// |
168 |
// This will call the double version of binaryOp |
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 |
operation); |
175 |
} |
176 |
} |
177 |
} else { |
178 |
#pragma omp parallel for private(i,j) schedule(static) |
179 |
for (i=0;i<numSamples;i++) { |
180 |
for (j=0;j<numDPPSample;j++) { |
181 |
left.getPointDataView().binaryOp(left.getPointOffset(i,j), |
182 |
right, |
183 |
0, |
184 |
operation); |
185 |
} |
186 |
} |
187 |
} |
188 |
} |
189 |
|
190 |
} // end of namespace |
191 |
|
192 |
#endif |