/[escript]/branches/clazy/escriptcore/src/DataLazy.h
ViewVC logotype

Contents of /branches/clazy/escriptcore/src/DataLazy.h

Parent Directory Parent Directory | Revision Log Revision Log


Revision 6512 - (show annotations)
Fri Mar 3 02:01:57 2017 UTC (23 months, 2 weeks ago) by jfenwick
File MIME type: text/plain
File size: 9662 byte(s)
Experiments on starting complex lazy

1
2 /*****************************************************************************
3 *
4 * Copyright (c) 2003-2016 by The University of Queensland
5 * http://www.uq.edu.au
6 *
7 * Primary Business: Queensland, Australia
8 * Licensed under the Apache License, version 2.0
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Development until 2012 by Earth Systems Science Computational Center (ESSCC)
12 * Development 2012-2013 by School of Earth Sciences
13 * Development from 2014 by Centre for Geoscience Computing (GeoComp)
14 *
15 *****************************************************************************/
16
17 #ifndef __ESCRIPT_DATALAZY_H__
18 #define __ESCRIPT_DATALAZY_H__
19
20 #include "system_dep.h"
21 #include "DataAbstract.h"
22 #include "ArrayOps.h" // for tensor_binary_op
23 #include "DataVector.h" // for ElementType
24 #include "ES_optype.h"
25
26 #include <string>
27
28 //#define LAZY_NODE_STORAGE
29
30 namespace escript {
31
32
33 /**
34 \class escript::DataLazy
35 \brief Wraps an expression tree of other DataObjects.
36 The data will be evaluated when required.
37
38
39 NOTE: This class assumes that the Data being pointed at are immutable.
40 */
41
42 class DataLazy;
43
44 typedef POINTER_WRAPPER_CLASS(DataLazy) DataLazy_ptr;
45 typedef POINTER_WRAPPER_CLASS(const DataLazy) const_DataLazy_ptr;
46
47 class DataLazy : public DataAbstract
48 {
49
50 typedef DataAbstract parent;
51 typedef DataTypes::ShapeType ShapeType;
52
53 public:
54 /**
55 \brief Create an IDENTITY DataLazy for the given DataAbstract.
56 \param p DataAbstract to be wrapped.
57 \throws DataException if p is lazy data or it is not constant, tagged or expanded.
58 */
59 ESCRIPT_DLL_API
60 DataLazy(DataAbstract_ptr p);
61
62
63 /**
64 \brief Produce a DataLazy for a unary operation.
65 \param left DataAbstract to be operated on.
66 \param op unary operation to perform.
67 \throws DataException if op is not a unary operation or if p cannot be converted to a DataLazy.
68 Note that IDENTITY is not considered a unary operation.
69 */
70 ESCRIPT_DLL_API
71 DataLazy(DataAbstract_ptr left, ES_optype op);
72
73 /**
74 \brief Produce a DataLazy for a unary operation.
75 \param left DataAbstract to be operated on.
76 \param op unary operation to perform.
77 \param tol tolerance for operation
78 \throws DataException if op is not a unary operation or if p cannot be converted to a DataLazy.
79 Note that IDENTITY is not considered a unary operation.
80 */
81 ESCRIPT_DLL_API
82 DataLazy(DataAbstract_ptr left, ES_optype op, double tol);
83
84 /**
85 \brief Produce a DataLazy for a unary operation which requires a parameter.
86 \param left DataAbstract to be operated on.
87 \param op unary operation to perform.
88 \param axis_offset the parameter for the operation
89 \throws DataException if op is not a unary operation or if p cannot be converted to a DataLazy.
90 Note that IDENTITY is not considered a unary operation.
91 */
92 ESCRIPT_DLL_API
93 DataLazy(DataAbstract_ptr left, ES_optype op, int axis_offset);
94
95
96 /**
97 \brief Produce a DataLazy for a binary operation.
98 \param left left operand
99 \param right right operand
100 \param op unary operation to perform.
101 \throws DataException if op is not a binary operation or if left or right cannot be converted to a DataLazy.
102 */
103 ESCRIPT_DLL_API
104 DataLazy(DataAbstract_ptr left, DataAbstract_ptr right, ES_optype op);
105
106 /**
107 \brief Produce a DataLazy for a binary operation with additional paramters.
108 \param left left operand
109 \param right right operand
110 \param op unary operation to perform.
111 \param axis_offset
112 \param transpose
113 \throws DataException if op is not a binary operation requiring parameters or if left or right cannot be converted to a DataLazy.
114 */
115 ESCRIPT_DLL_API
116 DataLazy(DataAbstract_ptr left, DataAbstract_ptr right, ES_optype op, int axis_offset, int transpose);
117
118 /**
119 \brief Produce a DataLazy for a unary operation which requires two integer parameters.
120 \param left DataAbstract to be operated on.
121 \param op unary operation to perform.
122 \param axis0 the first parameter for the operation
123 \param axis1 the second parameter for the operation
124 \throws DataException if op is not a unary operation or if p cannot be converted to a DataLazy.
125 Note that IDENTITY is not considered a unary operation.
126 */
127 ESCRIPT_DLL_API
128 DataLazy(DataAbstract_ptr left, ES_optype op, const int axis0, const int axis1);
129
130 /**
131 \brief Produce a DataLazy for a unary operation which requires two integer parameters.
132 \param mask scalar mask to select values.
133 \param left DataAbstract to use for true mask.
134 \param right DataAbstract to use for false mask.
135 */
136 ESCRIPT_DLL_API
137 DataLazy(DataAbstract_ptr mask, DataAbstract_ptr left, DataAbstract_ptr right/*, double tol*/);
138
139 ESCRIPT_DLL_API
140 ~DataLazy();
141
142 /**
143 \brief Evaluate the lazy expression.
144 \return A DataReady with the value of the lazy expresion.
145 */
146 ESCRIPT_DLL_API
147 DataReady_ptr
148 resolve();
149
150 ESCRIPT_DLL_API
151 std::string
152 toString() const;
153
154 ESCRIPT_DLL_API
155 DataAbstract*
156 deepCopy() const;
157
158 ESCRIPT_DLL_API
159 DataAbstract*
160 zeroedCopy() const;
161
162 /**
163 \brief
164 This method throws an exception. It does not really make sense to ask this question of lazy data.
165 */
166 ESCRIPT_DLL_API
167 DataTypes::RealVectorType::size_type
168 getLength() const;
169
170
171 ESCRIPT_DLL_API
172 DataAbstract*
173 getSlice(const DataTypes::RegionType& region) const;
174
175
176 DataTypes::RealVectorType::size_type
177 getPointOffset(int sampleNo,
178 int dataPointNo) const;
179
180 DataTypes::RealVectorType::size_type
181 getPointOffset(int sampleNo,
182 int dataPointNo);
183
184 /**
185 \return the largest samplesize required to evaluate the expression.
186 */
187 ESCRIPT_DLL_API
188 size_t
189 getMaxSampleSize() const;
190
191 /**
192 \brief Compute the value of the expression for the given sample.
193 \return Vector which stores the value of the subexpression for the given sample.
194 \param sampleNo Sample number to evaluate.
195 \param roffset (output parameter) the offset in the return vector where the result begins.
196
197 The return value will be an existing vector so do not deallocate it.
198 */
199 ESCRIPT_DLL_API
200 const DataTypes::RealVectorType*
201 resolveSample(int sampleNo, size_t& roffset) const;
202
203 /**
204 \brief if resolve() was called would it produce expanded data.
205 */
206 ESCRIPT_DLL_API
207 bool
208 actsExpanded() const;
209
210 /**
211 \brief Produces an IDENTITY DataLazy containing zero.
212 The result will have the same shape and functionspace as before.
213 */
214 ESCRIPT_DLL_API
215 virtual void
216 setToZero();
217
218
219 ESCRIPT_DLL_API
220 void
221 resolveGroupWorker(std::vector<DataLazy*>& dats);
222
223
224 private:
225 bool m_iscompl;
226 mutable DataReady_ptr m_id; // For IDENTITY nodes, stores a wrapped value.
227 mutable DataLazy_ptr m_left, m_right, m_mask; // operands for operation.
228 mutable ES_optype m_op; // operation to perform.
229
230 size_t m_samplesize; // number of values required to store a sample
231
232 char m_readytype; // E for expanded, T for tagged, C for constant
233
234 int m_axis_offset; // required extra info for general tensor product
235 int m_transpose; // offset and transpose are used for swapaxes as well
236 int m_SL, m_SM, m_SR; // computed properties used in general tensor product
237
238
239 double m_tol; // required extra info for <>0 and ==0
240
241 size_t m_children;
242 size_t m_height;
243
244 int* m_sampleids; // may be NULL
245 mutable DataTypes::RealVectorType m_samples_r;
246 mutable DataTypes::CplxVectorType m_samples_c;
247
248 /**
249 Allocates sample storage at each node
250 */
251 void LazyNodeSetup();
252
253
254 const DataTypes::RealVectorType*
255 resolveNodeUnary(int tid, int sampleNo, size_t& roffset) const;
256
257
258 const DataTypes::RealVectorType*
259 resolveNodeReduction(int tid, int sampleNo, size_t& roffset) const;
260
261 const DataTypes::RealVectorType*
262 resolveNodeSample(int tid, int sampleNo, size_t& roffset) const;
263
264 const DataTypes::RealVectorType*
265 resolveNodeBinary(int tid, int sampleNo, size_t& roffset) const;
266
267 const DataTypes::RealVectorType*
268 resolveNodeNP1OUT(int tid, int sampleNo, size_t& roffset) const;
269
270 const DataTypes::RealVectorType*
271 resolveNodeNP1OUT_P(int tid, int sampleNo, size_t& roffset) const;
272
273 const DataTypes::RealVectorType*
274 resolveNodeTProd(int tid, int sampleNo, size_t& roffset) const;
275
276 const DataTypes::RealVectorType*
277 resolveNodeNP1OUT_2P(int tid, int sampleNo, size_t& roffset) const;
278
279 const DataTypes::RealVectorType*
280 resolveNodeCondEval(int tid, int sampleNo, size_t& roffset) const;
281
282 /**
283 Does the work for toString.
284 */
285 void
286 intoString(std::ostringstream& oss) const;
287
288 /**
289 \warning internal use only!!
290 */
291 void
292 intoTreeString(std::ostringstream& oss,std::string indent) const;
293
294 /**
295 \brief Converts the DataLazy into an IDENTITY storing the value of the expression.
296 This method uses the original methods on the Data class to evaluate the expressions.
297 For this reason, it should not be used on DataExpanded instances. (To do so would defeat
298 the purpose of using DataLazy in the first place).
299 */
300 void
301 collapse() const; // converts the node into an IDENTITY node
302
303
304 /**
305 \brief Evaluates the expression using methods on Data.
306 This does the work for the collapse method.
307 For reasons of efficiency do not call this method on DataExpanded nodes.
308 */
309 DataReady_ptr
310 collapseToReady() const;
311
312 /**
313 \brief resolve the expression can store it in the current node
314 The current node will be converted to an identity node.
315 */
316 void
317 resolveToIdentity();
318
319 /**
320 \brief helper method for resolveToIdentity and the identity constructor
321 */
322 void
323 makeIdentity(const DataReady_ptr& p);
324
325
326 /**
327 \brief resolve to a ReadyData object using storage at nodes
328 */
329 DataReady_ptr
330 resolveNodeWorker();
331
332 };
333
334 }
335
336 #endif // __ESCRIPT_DATALAZY_H__
337

  ViewVC Help
Powered by ViewVC 1.1.26