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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 121 - (hide annotations)
Fri May 6 04:26:16 2005 UTC (14 years, 5 months ago) by jgs
Original Path: trunk/esys2/escript/src/Data/BinaryOp.h
File MIME type: text/plain
File size: 6451 byte(s)
Merge of development branch back to main trunk on 2005-05-06

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     // 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),
92     operation);
93     }
94     //
95     // finally perform the operation on the default value
96     left.getDefaultValue().binaryOp(right.getDefaultValue(),operation);
97     }
98    
99     template <class BinaryFunction>
100     inline void binaryOp(DataConstant& left, const DataConstant& right,
101     BinaryFunction operation)
102     {
103     //
104     // As DataConstant there is only one point data
105     binaryOp(left,right.getPointDataView(),operation);
106     }
107    
108     template <class BinaryFunction>
109     inline void binaryOp(DataConstant& left, const DataArrayView& right,
110     BinaryFunction operation)
111     {
112     //
113     // perform an operand check, this will throw on error
114     if (right.getRank()==0) {
115     //
116     // special case of applying a single value to the entire array
117     left.getPointDataView().binaryOp(right(), operation);
118     } else {
119     left.getPointDataView().binaryOp(right, operation);
120     }
121     }
122    
123     template <class BinaryFunction>
124     inline void binaryOp(DataExpanded& left, const DataAbstract& right,
125     BinaryFunction operation)
126     {
127     int i,j;
128     DataArrayView::ValueType::size_type numDPPSample=left.getNumDPPSample();
129     DataArrayView::ValueType::size_type numSamples=left.getNumSamples();
130     if (right.getPointDataView().getRank()==0) {
131     //
132     // This will call the double version of binaryOp
133     #pragma omp parallel for private(i,j) schedule(static)
134     for (i=0;i<numSamples;++i) {
135     for (j=0;j<numDPPSample;++j) {
136     left.getPointDataView().binaryOp(left.getPointOffset(i,j),
137     right.getPointDataView().getData(right.getPointOffset(i,j)),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(),operation);
168     }
169     }
170     } else {
171     #pragma omp parallel for private(i,j) schedule(static)
172     for (i=0;i<numSamples;++i) {
173     for (j=0;j<numDPPSample;++j) {
174     left.getPointDataView().binaryOp(left.getPointOffset(i,j),
175     right,0,
176     operation);
177     }
178     }
179     }
180     }
181    
182     } // end of namespace
183     #endif

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.26