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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 474 - (hide annotations)
Mon Jan 30 04:23:44 2006 UTC (13 years, 8 months ago) by jgs
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 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 "DataException.h"
19     #include "DataArrayView.h"
20     #include "DataConstant.h"
21     #include "DataExpanded.h"
22 jgs 82
23 jgs 474 #include <boost/scoped_ptr.hpp>
24    
25 jgs 82 #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 jgs 121 \param operation Input - The operation to perform.
36 jgs 82 */
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 jgs 154 // perform the operation on each tagged value
52 jgs 82 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 jgs 122 for (i=lookup.begin();i!=lookupEnd;i++) {
58 jgs 82 leftView.binaryOp(i->second,right(),operation);
59     }
60     } else {
61 jgs 122 for (i=lookup.begin();i!=lookupEnd;i++) {
62 jgs 82 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 jgs 122 for (i=rightLookup.begin();i!=rightLookupEnd;i++) {
80 jgs 82 //
81 jgs 154 // 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 jgs 82 }
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 jgs 122 for (i=leftLookup.begin();i!=leftLookupEnd;i++) {
94     left.getDataPointByTag(i->first).binaryOp(right.getDataPointByTag(i->first),operation);
95 jgs 82 }
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 jgs 122 left.getPointDataView().binaryOp(right(),operation);
120 jgs 82 } else {
121 jgs 122 left.getPointDataView().binaryOp(right,operation);
122 jgs 82 }
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 jgs 122 #pragma omp parallel for private(i,j) schedule(static)
136     for (i=0;i<numSamples;i++) {
137     for (j=0;j<numDPPSample;j++) {
138 jgs 82 left.getPointDataView().binaryOp(left.getPointOffset(i,j),
139 jgs 122 right.getPointDataView().getData(right.getPointOffset(i,j)),
140     operation);
141 jgs 82 }
142     }
143     } else {
144 jgs 122 #pragma omp parallel for private(i,j) schedule(static)
145     for (i=0;i<numSamples;i++) {
146     for (j=0;j<numDPPSample;j++) {
147 jgs 82 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 jgs 122 #pragma omp parallel for private(i,j) schedule(static)
167     for (i=0;i<numSamples;i++) {
168     for (j=0;j<numDPPSample;j++) {
169 jgs 82 left.getPointDataView().binaryOp(left.getPointOffset(i,j),
170 jgs 122 right(),
171     operation);
172 jgs 82 }
173     }
174     } else {
175 jgs 122 #pragma omp parallel for private(i,j) schedule(static)
176     for (i=0;i<numSamples;i++) {
177     for (j=0;j<numDPPSample;j++) {
178 jgs 82 left.getPointDataView().binaryOp(left.getPointOffset(i,j),
179 jgs 122 right,
180     0,
181 jgs 82 operation);
182     }
183     }
184     }
185     }
186    
187     } // end of namespace
188 jgs 122
189 jgs 82 #endif

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.26