/[escript]/trunk-mpi-branch/escript/src/BinaryOp.h
ViewVC logotype

Annotation of /trunk-mpi-branch/escript/src/BinaryOp.h

Parent Directory Parent Directory | Revision Log Revision Log


Revision 82 - (hide annotations)
Tue Oct 26 06:53:54 2004 UTC (14 years, 10 months ago) by jgs
Original Path: trunk/esys2/escript/src/Data/BinaryOp.h
File MIME type: text/plain
File size: 6397 byte(s)
Initial revision

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     */
35     template <class BinaryFunction>
36     inline void binaryOp(DataTagged& left, const DataConstant& right,
37     BinaryFunction operation)
38     {
39     //
40     // perform the operation on each tagged value including the default
41     binaryOp(left,right.getPointDataView(),operation);
42     }
43    
44     template <class BinaryFunction>
45     inline void binaryOp(DataTagged& left, const DataArrayView& right,
46     BinaryFunction operation)
47     {
48     //
49     // perform the operation on each tagged value including the default
50     const DataTagged::DataMapType& lookup=left.getTagLookup();
51     DataTagged::DataMapType::const_iterator i;
52     DataTagged::DataMapType::const_iterator lookupEnd=lookup.end();
53     DataArrayView& leftView=left.getPointDataView();
54     if (right.getRank()==0) {
55     for (i=lookup.begin();i!=lookupEnd;++i) {
56     leftView.binaryOp(i->second,right(),operation);
57     }
58     } else {
59     for (i=lookup.begin();i!=lookupEnd;++i) {
60     leftView.binaryOp(i->second,right,right.getOffset(),operation);
61     }
62     }
63     //
64     // finally perform the operation on the default value
65     left.getDefaultValue().binaryOp(right,operation);
66     }
67    
68     template <class BinaryFunction>
69     inline void binaryOp(DataTagged& left, const DataTagged& right,
70     BinaryFunction operation)
71     {
72     //
73     // Add the right hand tag keys which can't currently be found on the left
74     const DataTagged::DataMapType& rightLookup=right.getTagLookup();
75     DataTagged::DataMapType::const_iterator i;
76     DataTagged::DataMapType::const_iterator rightLookupEnd=rightLookup.end();
77     for (i=rightLookup.begin();i!=rightLookupEnd;++i) {
78     //
79     // Add the right hand tag to the left hand tag list and assign
80     // the rights default value. If the tag already exists the
81     // attempt to add it again will be ignored.
82     left.addTaggedValue(i->first,right.getDefaultValue());
83     }
84     //
85     // Perform the operation. Any tags originally in the left which don't exist for
86     // the right hand side will use the right's default value as the right operand
87     const DataTagged::DataMapType& leftLookup=left.getTagLookup();
88     DataTagged::DataMapType::const_iterator leftLookupEnd=leftLookup.end();
89     for (i=leftLookup.begin();i!=leftLookupEnd;++i) {
90     left.getDataPointByTag(i->first).binaryOp(right.getDataPointByTag(i->first),
91     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)),operation);
137     }
138     }
139     } else {
140     #pragma omp parallel for private(i,j) schedule(static)
141     for (i=0;i<numSamples;++i) {
142     for (j=0;j<numDPPSample;++j) {
143     left.getPointDataView().binaryOp(left.getPointOffset(i,j),
144     right.getPointDataView(),
145     right.getPointOffset(i,j),
146     operation);
147     }
148     }
149     }
150     }
151    
152     template <class BinaryFunction>
153     inline void binaryOp(DataExpanded& left, const DataArrayView& right,
154     BinaryFunction operation)
155     {
156     int i,j;
157     DataArrayView::ValueType::size_type numDPPSample=left.getNumDPPSample();
158     DataArrayView::ValueType::size_type numSamples=left.getNumSamples();
159     if (right.getRank()==0) {
160     //
161     // This will call the double version of binaryOp
162     #pragma omp parallel for private(i,j) schedule(static)
163     for (i=0;i<numSamples;++i) {
164     for (j=0;j<numDPPSample;++j) {
165     left.getPointDataView().binaryOp(left.getPointOffset(i,j),
166     right(),operation);
167     }
168     }
169     } else {
170     #pragma omp parallel for private(i,j) schedule(static)
171     for (i=0;i<numSamples;++i) {
172     for (j=0;j<numDPPSample;++j) {
173     left.getPointDataView().binaryOp(left.getPointOffset(i,j),
174     right,0,
175     operation);
176     }
177     }
178     }
179     }
180    
181     } // end of namespace
182     #endif

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.26