/[escript]/trunk/escriptcore/src/DataLazy.h
ViewVC logotype

Contents of /trunk/escriptcore/src/DataLazy.h

Parent Directory Parent Directory | Revision Log Revision Log


Revision 6542 - (show annotations)
Thu Mar 23 00:35:22 2017 UTC (8 months, 3 weeks ago) by jfenwick
File MIME type: text/plain
File size: 11366 byte(s)
Better version of earlier fix.

This version does not allow lazy non-expanded expressions of any depth other than 0.
Fixed missing POW operator collapsing.

Fixed height and child counts when collapsing (needed to make fields mutable)

1
2 /*****************************************************************************
3 *
4 * Copyright (c) 2003-2017 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 ESCRIPT_DLL_API
204 const DataTypes::CplxVectorType*
205 resolveTypedSample(int sampleNo, size_t& roffset, DataTypes::cplx_t dummy) const;
206
207 ESCRIPT_DLL_API
208 const DataTypes::RealVectorType*
209 resolveTypedSample(int sampleNo, size_t& roffset, DataTypes::real_t dummy) const;
210
211
212 /**
213 \brief if resolve() was called would it produce expanded data.
214 */
215 ESCRIPT_DLL_API
216 bool
217 actsExpanded() const;
218
219 /**
220 \brief Produces an IDENTITY DataLazy containing zero.
221 The result will have the same shape and functionspace as before.
222 */
223 ESCRIPT_DLL_API
224 virtual void
225 setToZero();
226
227
228 ESCRIPT_DLL_API
229 void
230 resolveGroupWorker(std::vector<DataLazy*>& dats);
231
232
233 private:
234 int* m_sampleids; // may be NULL
235 mutable DataTypes::RealVectorType m_samples_r;
236 mutable DataTypes::CplxVectorType m_samples_c;
237
238 mutable DataReady_ptr m_id; // For IDENTITY nodes, stores a wrapped value.
239 mutable DataLazy_ptr m_left, m_right, m_mask; // operands for operation.
240 mutable ES_optype m_op; // operation to perform.
241 mutable ES_opgroup m_opgroup; // type of operation to perform
242
243 size_t m_samplesize; // number of values required to store a sample
244
245 char m_readytype; // E for expanded, T for tagged, C for constant
246
247 int m_axis_offset; // required extra info for general tensor product
248 int m_transpose; // offset and transpose are used for swapaxes as well
249 int m_SL, m_SM, m_SR; // computed properties used in general tensor product
250
251
252 double m_tol; // required extra info for <>0 and ==0
253
254 mutable size_t m_children;
255 mutable size_t m_height;
256
257
258
259 /**
260 Allocates sample storage at each node
261 */
262 void LazyNodeSetup();
263
264
265 const DataTypes::RealVectorType*
266 resolveNodeUnary(int tid, int sampleNo, size_t& roffset) const;
267
268 const DataTypes::CplxVectorType*
269 resolveNodeUnaryCplx(int tid, int sampleNo, size_t& roffset) const;
270
271
272 const DataTypes::RealVectorType*
273 resolveNodeReduction(int tid, int sampleNo, size_t& roffset) const;
274
275 const DataTypes::CplxVectorType*
276 resolveNodeReductionCplx(int tid, int sampleNo, size_t& roffset) const;
277
278 const DataTypes::RealVectorType*
279 resolveNodeSample(int tid, int sampleNo, size_t& roffset) const;
280
281 const DataTypes::CplxVectorType*
282 resolveNodeSampleCplx(int tid, int sampleNo, size_t& roffset) const;
283
284 const DataTypes::RealVectorType*
285 resolveNodeBinary(int tid, int sampleNo, size_t& roffset) const;
286
287 const DataTypes::CplxVectorType*
288 resolveNodeBinaryCplx(int tid, int sampleNo, size_t& roffset) const;
289
290
291 const DataTypes::RealVectorType*
292 resolveNodeNP1OUT(int tid, int sampleNo, size_t& roffset) const;
293
294 const DataTypes::CplxVectorType*
295 resolveNodeNP1OUTCplx(int tid, int sampleNo, size_t& roffset) const;
296
297 const DataTypes::RealVectorType*
298 resolveNodeNP1OUT_P(int tid, int sampleNo, size_t& roffset) const;
299
300 const DataTypes::CplxVectorType*
301 resolveNodeNP1OUT_PCplx(int tid, int sampleNo, size_t& roffset) const;
302
303
304 const DataTypes::RealVectorType*
305 resolveNodeTProd(int tid, int sampleNo, size_t& roffset) const;
306
307 const DataTypes::CplxVectorType*
308 resolveNodeTProdCplx(int tid, int sampleNo, size_t& roffset) const;
309
310
311 const DataTypes::RealVectorType*
312 resolveNodeNP1OUT_2P(int tid, int sampleNo, size_t& roffset) const;
313
314 const DataTypes::CplxVectorType*
315 resolveNodeNP1OUT_2PCplx(int tid, int sampleNo, size_t& roffset) const;
316
317 const DataTypes::RealVectorType*
318 resolveNodeCondEval(int tid, int sampleNo, size_t& roffset) const;
319
320 const DataTypes::CplxVectorType*
321 resolveNodeCondEvalCplx(int tid, int sampleNo, size_t& roffset) const;
322
323
324 const DataTypes::CplxVectorType*
325 resolveNodeUnary_C(int tid, int sampleNo, size_t& roffset) const;
326
327 /**
328 Does the work for toString.
329 */
330 void
331 intoString(std::ostringstream& oss) const;
332
333 /**
334 \warning internal use only!!
335 */
336 void
337 intoTreeString(std::ostringstream& oss,std::string indent) const;
338
339 /**
340 \brief Converts the DataLazy into an IDENTITY storing the value of the expression.
341 This method uses the original methods on the Data class to evaluate the expressions.
342 For this reason, it should not be used on DataExpanded instances. (To do so would defeat
343 the purpose of using DataLazy in the first place).
344 */
345 void
346 collapse() const; // converts the node into an IDENTITY node
347
348
349 /**
350 \brief Evaluates the expression using methods on Data.
351 This does the work for the collapse method.
352 For reasons of efficiency do not call this method on DataExpanded nodes.
353 */
354 DataReady_ptr
355 collapseToReady() const;
356
357 /**
358 \brief resolve the expression can store it in the current node
359 The current node will be converted to an identity node.
360 */
361 void
362 resolveToIdentity();
363
364 /**
365 \brief helper method for resolveToIdentity and the identity constructor
366 */
367 void
368 makeIdentity(const DataReady_ptr& p);
369
370
371 /**
372 \brief resolve to a ReadyData object using storage at nodes
373 */
374 DataReady_ptr
375 resolveNodeWorker();
376
377 DataReady_ptr
378 resolveNodeWorkerCplx();
379
380 };
381
382 // If an expression is already complex, return the same expression.
383 // Otherwise, return the old expression with a promote operation
384 // above it
385 DataLazy_ptr makePromote(DataLazy_ptr p);
386
387 }
388
389 #endif // __ESCRIPT_DATALAZY_H__
390

  ViewVC Help
Powered by ViewVC 1.1.26