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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 154 - (hide annotations)
Mon Nov 7 05:51:17 2005 UTC (14 years 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 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     #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 jgs 121 \param operation Input - The operation to perform.
35 jgs 82 */
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 jgs 154 // perform the operation on each tagged value
51 jgs 82 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 jgs 122 for (i=lookup.begin();i!=lookupEnd;i++) {
57 jgs 82 leftView.binaryOp(i->second,right(),operation);
58     }
59     } else {
60 jgs 122 for (i=lookup.begin();i!=lookupEnd;i++) {
61 jgs 82 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 jgs 122 for (i=rightLookup.begin();i!=rightLookupEnd;i++) {
79 jgs 82 //
80 jgs 154 // 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 jgs 82 }
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 jgs 122 for (i=leftLookup.begin();i!=leftLookupEnd;i++) {
93     left.getDataPointByTag(i->first).binaryOp(right.getDataPointByTag(i->first),operation);
94 jgs 82 }
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 jgs 122 left.getPointDataView().binaryOp(right(),operation);
119 jgs 82 } else {
120 jgs 122 left.getPointDataView().binaryOp(right,operation);
121 jgs 82 }
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 jgs 122 #pragma omp parallel for private(i,j) schedule(static)
135     for (i=0;i<numSamples;i++) {
136     for (j=0;j<numDPPSample;j++) {
137 jgs 82 left.getPointDataView().binaryOp(left.getPointOffset(i,j),
138 jgs 122 right.getPointDataView().getData(right.getPointOffset(i,j)),
139     operation);
140 jgs 82 }
141     }
142     } else {
143 jgs 122 #pragma omp parallel for private(i,j) schedule(static)
144     for (i=0;i<numSamples;i++) {
145     for (j=0;j<numDPPSample;j++) {
146 jgs 82 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 jgs 122 #pragma omp parallel for private(i,j) schedule(static)
166     for (i=0;i<numSamples;i++) {
167     for (j=0;j<numDPPSample;j++) {
168 jgs 82 left.getPointDataView().binaryOp(left.getPointOffset(i,j),
169 jgs 122 right(),
170     operation);
171 jgs 82 }
172     }
173     } else {
174 jgs 122 #pragma omp parallel for private(i,j) schedule(static)
175     for (i=0;i<numSamples;i++) {
176     for (j=0;j<numDPPSample;j++) {
177 jgs 82 left.getPointDataView().binaryOp(left.getPointOffset(i,j),
178 jgs 122 right,
179     0,
180 jgs 82 operation);
181     }
182     }
183     }
184     }
185    
186     } // end of namespace
187 jgs 122
188 jgs 82 #endif

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.26