/[escript]/branches/schroedinger/escript/src/DataLazy.cpp
ViewVC logotype

Contents of /branches/schroedinger/escript/src/DataLazy.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1868 - (show annotations)
Thu Oct 9 06:30:49 2008 UTC (10 years, 6 months ago) by jfenwick
File size: 4351 byte(s)
Branch commit.
Bulk resolve for + - * /

1
2 /*******************************************************
3 *
4 * Copyright (c) 2003-2008 by University of Queensland
5 * Earth Systems Science Computational Center (ESSCC)
6 * http://www.uq.edu.au/esscc
7 *
8 * Primary Business: Queensland, Australia
9 * Licensed under the Open Software License version 3.0
10 * http://www.opensource.org/licenses/osl-3.0.php
11 *
12 *******************************************************/
13
14
15 #include "DataLazy.h"
16 #ifdef USE_NETCDF
17 #include <netcdfcpp.h>
18 #endif
19 #ifdef PASO_MPI
20 #include <mpi.h>
21 #endif
22 #include "FunctionSpace.h"
23 #include "DataTypes.h"
24 #include "Data.h"
25
26 using namespace std;
27 using namespace boost;
28
29 namespace escript
30 {
31
32 const std::string&
33 opToString(ES_optype op);
34
35 namespace
36 {
37
38 // return the FunctionSpace of the result of "left op right"
39 FunctionSpace
40 resultFS(DataAbstract_ptr left, DataAbstract_ptr right, ES_optype op)
41 {
42 // perhaps this should call interpolate and throw or something?
43 // maybe we need an interpolate node -
44 // that way, if interpolate is required in any other op we can just throw a
45 // programming error exception.
46 return left->getFunctionSpace();
47 }
48
49 // return the shape of the result of "left op right"
50 DataTypes::ShapeType
51 resultShape(DataAbstract_ptr left, DataAbstract_ptr right, ES_optype op)
52 {
53 return DataTypes::scalarShape;
54 }
55
56 size_t
57 resultLength(DataAbstract_ptr left, DataAbstract_ptr right, ES_optype op)
58 {
59 switch(op)
60 {
61 case IDENTITY: return left->getLength();
62 case ADD: // the length is preserved in these ops
63 case SUB:
64 case MUL:
65 case DIV: return left->getLength();
66 default:
67 throw DataException("Programmer Error - attempt to getLength() for operator "+opToString(op)+".");
68
69 }
70 }
71
72 string ES_opstrings[]={"UNKNOWN","IDENTITY","+","-","*","/"};
73 int ES_opcount=5;
74
75 } // end anonymous namespace
76
77
78 const std::string&
79 opToString(ES_optype op)
80 {
81 if (op<0 || op>=ES_opcount)
82 {
83 op=UNKNOWNOP;
84 }
85 return ES_opstrings[op];
86 }
87
88
89 DataLazy::DataLazy(DataAbstract_ptr p)
90 : parent(p->getFunctionSpace(),p->getShape()),
91 m_left(p),
92 m_op(IDENTITY)
93 {
94 length=resultLength(m_left,m_right,m_op);
95 }
96
97 DataLazy::DataLazy(DataAbstract_ptr left, DataAbstract_ptr right, ES_optype op)
98 : parent(resultFS(left,right,op), resultShape(left,right,op)),
99 m_left(left),
100 m_right(right),
101 m_op(op)
102 {
103 length=resultLength(m_left,m_right,m_op);
104 }
105
106 DataLazy::~DataLazy()
107 {
108 }
109
110 // If resolving records a pointer to the resolved Data we may need to rethink the const on this method
111 DataReady_ptr
112 DataLazy::resolve()
113 {
114 DataReady_ptr left;
115 DataReady_ptr right;
116 if (m_left.get()!=0)
117 {
118 left=m_left->resolve();
119 }
120 if (m_right.get()!=0)
121 {
122 right=m_right->resolve();
123 }
124 switch (m_op)
125 {
126 case IDENTITY: return left;
127 case ADD:
128 // Hmm we could get interpolation here, better be careful
129 return C_TensorBinaryOperation(Data(left),Data(right),plus<double>()).borrowReadyPtr();
130 case SUB:
131 return C_TensorBinaryOperation(Data(left),Data(right),minus<double>()).borrowReadyPtr();
132 case MUL:
133 return C_TensorBinaryOperation(Data(left),Data(right),multiplies<double>()).borrowReadyPtr();
134 case DIV:
135 return C_TensorBinaryOperation(Data(left),Data(right),divides<double>()).borrowReadyPtr();
136 default:
137 throw DataException("Programmer error - do not know how to resolve operator "+opToString(m_op)+".");
138 }
139 }
140
141 std::string
142 DataLazy::toString() const
143 {
144 return "Lazy evaluation object. No details available.";
145 }
146
147 // Note that in this case, deepCopy does not make copies of the leaves.
148 // Hopefully copy on write (or whatever we end up using) will take care of this.
149 DataAbstract*
150 DataLazy::deepCopy()
151 {
152 if (m_op==IDENTITY)
153 {
154 return new DataLazy(m_left); // we don't need to copy the child here
155 }
156 return new DataLazy(m_left->deepCopy()->getPtr(),m_right->deepCopy()->getPtr(),m_op);
157 }
158
159
160 DataTypes::ValueType::size_type
161 DataLazy::getLength() const
162 {
163 return length;
164 }
165
166
167 DataAbstract*
168 DataLazy::getSlice(const DataTypes::RegionType& region) const
169 {
170 // this seems like a really good one to include I just haven't added it yet
171 throw DataException("getSlice - not implemented for Lazy objects - yet.");
172 }
173
174 DataTypes::ValueType::size_type
175 DataLazy::getPointOffset(int sampleNo,
176 int dataPointNo) const
177 {
178 throw DataException("getPointOffset - not implemented for Lazy objects - yet.");
179 }
180
181 } // end namespace

  ViewVC Help
Powered by ViewVC 1.1.26