/[escript]/trunk-mpi-branch/escript/src/BinaryOp.h
ViewVC logotype

Contents of /trunk-mpi-branch/escript/src/BinaryOp.h

Parent Directory Parent Directory | Revision Log Revision Log


Revision 474 - (show annotations)
Mon Jan 30 04:23:44 2006 UTC (13 years, 7 months ago) by jgs
Original Path: trunk/escript/src/BinaryOp.h
File MIME type: text/plain
File size: 6550 byte(s)
restructure escript source tree
move src/Data/* -> src
remove inc
modify #includes and cpppath settings accordingly

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

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.26