/[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 122 - (show annotations)
Thu Jun 9 05:38:05 2005 UTC (14 years, 3 months ago) by jgs
Original Path: trunk/esys2/escript/src/Data/BinaryOp.h
File MIME type: text/plain
File size: 6582 byte(s)
Merge of development branch back to main trunk on 2005-06-09

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

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.26