/[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 6517 - (show annotations)
Mon Mar 6 06:35:31 2017 UTC (2 years, 1 month ago) by jfenwick
File MIME type: text/plain
File size: 10769 byte(s)
Work towards 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 true if the components of datapoints are complex
144 */
145
146 /**
147 \brief Evaluate the lazy expression.
148 \return A DataReady with the value of the lazy expresion.
149 */
150 ESCRIPT_DLL_API
151 DataReady_ptr
152 resolve();
153
154 ESCRIPT_DLL_API
155 std::string
156 toString() const;
157
158 ESCRIPT_DLL_API
159 DataAbstract*
160 deepCopy() const;
161
162 ESCRIPT_DLL_API
163 DataAbstract*
164 zeroedCopy() const;
165
166 /**
167 \brief
168 This method throws an exception. It does not really make sense to ask this question of lazy data.
169 */
170 ESCRIPT_DLL_API
171 DataTypes::RealVectorType::size_type
172 getLength() const;
173
174
175 ESCRIPT_DLL_API
176 DataAbstract*
177 getSlice(const DataTypes::RegionType& region) const;
178
179
180 DataTypes::RealVectorType::size_type
181 getPointOffset(int sampleNo,
182 int dataPointNo) const;
183
184 DataTypes::RealVectorType::size_type
185 getPointOffset(int sampleNo,
186 int dataPointNo);
187
188 /**
189 \return the largest samplesize required to evaluate the expression.
190 */
191 ESCRIPT_DLL_API
192 size_t
193 getMaxSampleSize() const;
194
195 /**
196 \brief Compute the value of the expression for the given sample.
197 \return Vector which stores the value of the subexpression for the given sample.
198 \param sampleNo Sample number to evaluate.
199 \param roffset (output parameter) the offset in the return vector where the result begins.
200
201 The return value will be an existing vector so do not deallocate it.
202 */
203 ESCRIPT_DLL_API
204 const DataTypes::RealVectorType*
205 resolveSample(int sampleNo, size_t& roffset) const;
206
207 /**
208 \brief if resolve() was called would it produce expanded data.
209 */
210 ESCRIPT_DLL_API
211 bool
212 actsExpanded() const;
213
214 /**
215 \brief Produces an IDENTITY DataLazy containing zero.
216 The result will have the same shape and functionspace as before.
217 */
218 ESCRIPT_DLL_API
219 virtual void
220 setToZero();
221
222
223 ESCRIPT_DLL_API
224 void
225 resolveGroupWorker(std::vector<DataLazy*>& dats);
226
227
228 private:
229 mutable DataReady_ptr m_id; // For IDENTITY nodes, stores a wrapped value.
230 mutable DataLazy_ptr m_left, m_right, m_mask; // operands for operation.
231 mutable ES_optype m_op; // operation to perform.
232
233 size_t m_samplesize; // number of values required to store a sample
234
235 char m_readytype; // E for expanded, T for tagged, C for constant
236
237 int m_axis_offset; // required extra info for general tensor product
238 int m_transpose; // offset and transpose are used for swapaxes as well
239 int m_SL, m_SM, m_SR; // computed properties used in general tensor product
240
241
242 double m_tol; // required extra info for <>0 and ==0
243
244 size_t m_children;
245 size_t m_height;
246
247 int* m_sampleids; // may be NULL
248 mutable DataTypes::RealVectorType m_samples_r;
249 mutable DataTypes::CplxVectorType m_samples_c;
250
251 /**
252 Allocates sample storage at each node
253 */
254 void LazyNodeSetup();
255
256
257 const DataTypes::RealVectorType*
258 resolveNodeUnary(int tid, int sampleNo, size_t& roffset) const;
259
260 const DataTypes::CplxVectorType*
261 resolveNodeUnaryCplx(int tid, int sampleNo, size_t& roffset) const;
262
263
264 const DataTypes::RealVectorType*
265 resolveNodeReduction(int tid, int sampleNo, size_t& roffset) const;
266
267 const DataTypes::CplxVectorType*
268 resolveNodeReductionCplx(int tid, int sampleNo, size_t& roffset) const;
269
270 const DataTypes::RealVectorType*
271 resolveNodeSample(int tid, int sampleNo, size_t& roffset) const;
272
273 const DataTypes::CplxVectorType*
274 resolveNodeSampleCplx(int tid, int sampleNo, size_t& roffset) const;
275
276 const DataTypes::RealVectorType*
277 resolveNodeBinary(int tid, int sampleNo, size_t& roffset) const;
278
279 const DataTypes::CplxVectorType*
280 resolveNodeBinaryCplx(int tid, int sampleNo, size_t& roffset) const;
281
282
283 const DataTypes::RealVectorType*
284 resolveNodeNP1OUT(int tid, int sampleNo, size_t& roffset) const;
285
286 const DataTypes::CplxVectorType*
287 resolveNodeNP1OUTCplx(int tid, int sampleNo, size_t& roffset) const;
288
289 const DataTypes::RealVectorType*
290 resolveNodeNP1OUT_P(int tid, int sampleNo, size_t& roffset) const;
291
292 const DataTypes::CplxVectorType*
293 resolveNodeNP1OUT_PCplx(int tid, int sampleNo, size_t& roffset) const;
294
295
296 const DataTypes::RealVectorType*
297 resolveNodeTProd(int tid, int sampleNo, size_t& roffset) const;
298
299 const DataTypes::CplxVectorType*
300 resolveNodeTProdCplx(int tid, int sampleNo, size_t& roffset) const;
301
302
303 const DataTypes::RealVectorType*
304 resolveNodeNP1OUT_2P(int tid, int sampleNo, size_t& roffset) const;
305
306 const DataTypes::CplxVectorType*
307 resolveNodeNP1OUT_2PCplx(int tid, int sampleNo, size_t& roffset) const;
308
309 const DataTypes::RealVectorType*
310 resolveNodeCondEval(int tid, int sampleNo, size_t& roffset) const;
311
312 const DataTypes::CplxVectorType*
313 resolveNodeCondEvalCplx(int tid, int sampleNo, size_t& roffset) const;
314
315
316 /**
317 Does the work for toString.
318 */
319 void
320 intoString(std::ostringstream& oss) const;
321
322 /**
323 \warning internal use only!!
324 */
325 void
326 intoTreeString(std::ostringstream& oss,std::string indent) const;
327
328 /**
329 \brief Converts the DataLazy into an IDENTITY storing the value of the expression.
330 This method uses the original methods on the Data class to evaluate the expressions.
331 For this reason, it should not be used on DataExpanded instances. (To do so would defeat
332 the purpose of using DataLazy in the first place).
333 */
334 void
335 collapse() const; // converts the node into an IDENTITY node
336
337
338 /**
339 \brief Evaluates the expression using methods on Data.
340 This does the work for the collapse method.
341 For reasons of efficiency do not call this method on DataExpanded nodes.
342 */
343 DataReady_ptr
344 collapseToReady() const;
345
346 /**
347 \brief resolve the expression can store it in the current node
348 The current node will be converted to an identity node.
349 */
350 void
351 resolveToIdentity();
352
353 /**
354 \brief helper method for resolveToIdentity and the identity constructor
355 */
356 void
357 makeIdentity(const DataReady_ptr& p);
358
359
360 /**
361 \brief resolve to a ReadyData object using storage at nodes
362 */
363 DataReady_ptr
364 resolveNodeWorker();
365
366 DataReady_ptr
367 resolveNodeWorkerCplx();
368
369 };
370
371
372 }
373
374 #endif // __ESCRIPT_DATALAZY_H__
375

  ViewVC Help
Powered by ViewVC 1.1.26