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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 615 - (show annotations)
Wed Mar 22 02:12:00 2006 UTC (13 years, 6 months ago) by elspeth
File MIME type: text/plain
File size: 6022 byte(s)
More copyright information.

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 //
70 // Add the right hand tag keys which can't currently be found on the left
71 const DataTagged::DataMapType& rightLookup=right.getTagLookup();
72 DataTagged::DataMapType::const_iterator i;
73 DataTagged::DataMapType::const_iterator rightLookupEnd=rightLookup.end();
74 for (i=rightLookup.begin();i!=rightLookupEnd;i++) {
75 //
76 // If the left does not already have a value assigned to this tag,
77 // add the right hand tag to the left hand tag list and assign
78 // the left's default value.
79 if (!left.isCurrentTag(i->first)) {
80 left.addTaggedValue(i->first,left.getDefaultValue());
81 }
82 }
83 //
84 // Perform the operation.
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

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.26