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 left's default value. |
78 |
if (!left.isCurrentTag(i->first)) { |
79 |
left.addTaggedValue(i->first,left.getDefaultValue()); |
80 |
} |
81 |
} |
82 |
// |
83 |
// Perform the operation. |
84 |
const DataTagged::DataMapType& leftLookup=left.getTagLookup(); |
85 |
DataTagged::DataMapType::const_iterator leftLookupEnd=leftLookup.end(); |
86 |
for (i=leftLookup.begin();i!=leftLookupEnd;i++) { |
87 |
left.getDataPointByTag(i->first).binaryOp(right.getDataPointByTag(i->first),operation); |
88 |
} |
89 |
// |
90 |
// finally perform the operation on the default value |
91 |
left.getDefaultValue().binaryOp(right.getDefaultValue(),operation); |
92 |
} |
93 |
|
94 |
template <class BinaryFunction> |
95 |
inline void binaryOp(DataConstant& left, const DataConstant& right, |
96 |
BinaryFunction operation) |
97 |
{ |
98 |
// |
99 |
// As DataConstant there is only one point data |
100 |
binaryOp(left,right.getPointDataView(),operation); |
101 |
} |
102 |
|
103 |
template <class BinaryFunction> |
104 |
inline void binaryOp(DataConstant& left, const DataArrayView& right, |
105 |
BinaryFunction operation) |
106 |
{ |
107 |
// |
108 |
// perform an operand check, this will throw on error |
109 |
if (right.getRank()==0) { |
110 |
// |
111 |
// special case of applying a single value to the entire array |
112 |
left.getPointDataView().binaryOp(right(),operation); |
113 |
} else { |
114 |
left.getPointDataView().binaryOp(right,operation); |
115 |
} |
116 |
} |
117 |
|
118 |
template <class BinaryFunction> |
119 |
inline void binaryOp(DataExpanded& left, const DataAbstract& right, |
120 |
BinaryFunction operation) |
121 |
{ |
122 |
int i,j; |
123 |
DataArrayView::ValueType::size_type numDPPSample=left.getNumDPPSample(); |
124 |
DataArrayView::ValueType::size_type numSamples=left.getNumSamples(); |
125 |
if (right.getPointDataView().getRank()==0) { |
126 |
// |
127 |
// This will call the double version of binaryOp |
128 |
#pragma omp parallel for private(i,j) schedule(static) |
129 |
for (i=0;i<numSamples;i++) { |
130 |
for (j=0;j<numDPPSample;j++) { |
131 |
left.getPointDataView().binaryOp(left.getPointOffset(i,j), |
132 |
right.getPointDataView().getData(right.getPointOffset(i,j)), |
133 |
operation); |
134 |
} |
135 |
} |
136 |
} else { |
137 |
#pragma omp parallel for private(i,j) schedule(static) |
138 |
for (i=0;i<numSamples;i++) { |
139 |
for (j=0;j<numDPPSample;j++) { |
140 |
left.getPointDataView().binaryOp(left.getPointOffset(i,j), |
141 |
right.getPointDataView(), |
142 |
right.getPointOffset(i,j), |
143 |
operation); |
144 |
} |
145 |
} |
146 |
} |
147 |
} |
148 |
|
149 |
template <class BinaryFunction> |
150 |
inline void binaryOp(DataExpanded& left, const DataArrayView& right, |
151 |
BinaryFunction operation) |
152 |
{ |
153 |
int i,j; |
154 |
DataArrayView::ValueType::size_type numDPPSample=left.getNumDPPSample(); |
155 |
DataArrayView::ValueType::size_type numSamples=left.getNumSamples(); |
156 |
if (right.getRank()==0) { |
157 |
// |
158 |
// This will call the double version of binaryOp |
159 |
#pragma omp parallel for private(i,j) schedule(static) |
160 |
for (i=0;i<numSamples;i++) { |
161 |
for (j=0;j<numDPPSample;j++) { |
162 |
left.getPointDataView().binaryOp(left.getPointOffset(i,j), |
163 |
right(), |
164 |
operation); |
165 |
} |
166 |
} |
167 |
} else { |
168 |
#pragma omp parallel for private(i,j) schedule(static) |
169 |
for (i=0;i<numSamples;i++) { |
170 |
for (j=0;j<numDPPSample;j++) { |
171 |
left.getPointDataView().binaryOp(left.getPointOffset(i,j), |
172 |
right, |
173 |
0, |
174 |
operation); |
175 |
} |
176 |
} |
177 |
} |
178 |
} |
179 |
|
180 |
} // end of namespace |
181 |
|
182 |
#endif |