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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 478 - (hide annotations)
Tue Jan 31 02:21:49 2006 UTC (13 years, 8 months ago) by jgs
File MIME type: text/plain
File size: 6453 byte(s)
rationalise #includes

1 jgs 82 /*
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 jgs 474 #include "DataArrayView.h"
19     #include "DataConstant.h"
20 jgs 478 #include "DataTagged.h"
21 jgs 474 #include "DataExpanded.h"
22 jgs 82
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 jgs 121 \param operation Input - The operation to perform.
30 jgs 82 */
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 jgs 154 // perform the operation on each tagged value
46 jgs 82 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 jgs 122 for (i=lookup.begin();i!=lookupEnd;i++) {
52 jgs 82 leftView.binaryOp(i->second,right(),operation);
53     }
54     } else {
55 jgs 122 for (i=lookup.begin();i!=lookupEnd;i++) {
56 jgs 82 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 jgs 122 for (i=rightLookup.begin();i!=rightLookupEnd;i++) {
74 jgs 82 //
75 jgs 154 // 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 right's default value.
78     if (!left.isCurrentTag(i->first)) {
79     left.addTaggedValue(i->first,right.getDefaultValue());
80     }
81 jgs 82 }
82     //
83     // Perform the operation. Any tags originally in the left which don't exist for
84     // the right hand side will use the right's default value as the right operand
85     const DataTagged::DataMapType& leftLookup=left.getTagLookup();
86     DataTagged::DataMapType::const_iterator leftLookupEnd=leftLookup.end();
87 jgs 122 for (i=leftLookup.begin();i!=leftLookupEnd;i++) {
88     left.getDataPointByTag(i->first).binaryOp(right.getDataPointByTag(i->first),operation);
89 jgs 82 }
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 jgs 122 left.getPointDataView().binaryOp(right(),operation);
114 jgs 82 } else {
115 jgs 122 left.getPointDataView().binaryOp(right,operation);
116 jgs 82 }
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 jgs 122 #pragma omp parallel for private(i,j) schedule(static)
130     for (i=0;i<numSamples;i++) {
131     for (j=0;j<numDPPSample;j++) {
132 jgs 82 left.getPointDataView().binaryOp(left.getPointOffset(i,j),
133 jgs 122 right.getPointDataView().getData(right.getPointOffset(i,j)),
134     operation);
135 jgs 82 }
136     }
137     } else {
138 jgs 122 #pragma omp parallel for private(i,j) schedule(static)
139     for (i=0;i<numSamples;i++) {
140     for (j=0;j<numDPPSample;j++) {
141 jgs 82 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 jgs 122 #pragma omp parallel for private(i,j) schedule(static)
161     for (i=0;i<numSamples;i++) {
162     for (j=0;j<numDPPSample;j++) {
163 jgs 82 left.getPointDataView().binaryOp(left.getPointOffset(i,j),
164 jgs 122 right(),
165     operation);
166 jgs 82 }
167     }
168     } else {
169 jgs 122 #pragma omp parallel for private(i,j) schedule(static)
170     for (i=0;i<numSamples;i++) {
171     for (j=0;j<numDPPSample;j++) {
172 jgs 82 left.getPointDataView().binaryOp(left.getPointOffset(i,j),
173 jgs 122 right,
174     0,
175 jgs 82 operation);
176     }
177     }
178     }
179     }
180    
181     } // end of namespace
182 jgs 122
183 jgs 82 #endif

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.26