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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 154 - (show annotations)
Mon Nov 7 05:51:17 2005 UTC (13 years, 11 months ago) by jgs
Original Path: trunk/esys2/escript/src/Data/BinaryOp.h
File MIME type: text/plain
File size: 6601 byte(s)
Merge of development branch dev-02 back to main trunk on 2005-11-07

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

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.26