/[escript]/trunk/escript/test/DataLazyTestCase.cpp
ViewVC logotype

Contents of /trunk/escript/test/DataLazyTestCase.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2199 - (show annotations)
Thu Jan 8 06:10:52 2009 UTC (10 years, 9 months ago) by jfenwick
File size: 10873 byte(s)
Misc fixes:
Added some svn:ignore properties for output files that were cluttering things up.

Lazy fixes:
Fixed shape calculations for TRACE and TRANSPOSE for rank>2.
Adjusted unit test accordingly.

As a Temporary change to DataC.cpp to test for lazy data in DataC's expanded check.
This is wrong but would only affect people using lazy data.
The proper fix will come when the numarray removal code moves over from the branch.

Made tensor product AUTOLAZY capable.
Fixed some bugs resolving tensor products (incorrect offsets in buffers).
Macro'd some stray couts.

- It appears that AUTOLAZY now passes all unit tests.
- It will not be _really_ safe for general use until I can add COW. 
- (Everything's better with COW)
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 "escript/DataConstant.h"
16 #include "escript/DataLazy.h"
17 #include "escript/FunctionSpace.h"
18 #include "esysUtils/EsysException.h"
19
20 #include "DataLazyTestCase.h"
21
22 #include <iostream>
23 #include <boost/shared_ptr.hpp> // for the cast operator
24
25 using namespace CppUnitTest;
26 using namespace escript;
27 using namespace std;
28 using namespace esysUtils;
29 using namespace escript::DataTypes;
30 using namespace boost;
31
32 void DataLazyTestCase::setUp() {
33 //
34 // This is called before each test is run
35 }
36
37 void DataLazyTestCase::tearDown() {
38 //
39 // This is called after each test has been run
40 }
41
42
43 // This test file checks the basic properties of lazy data.
44 // It does not check the correctness of particular operations
45
46
47 namespace
48 {
49
50 ValueType::reference
51 getRef(DataReady& data,int i, int j, int k)
52 {
53 return data.getVector()[getRelIndex(data.getShape(),i,j,k)];
54 }
55
56
57 DataReady_ptr
58 resolveAndDelete(DataAbstract* p)
59 {
60 DataReady_ptr p2=p->resolve();
61 if (p!=p2.get())
62 {
63 delete p;
64 }
65 return p2;
66 }
67
68
69 DataAbstract_ptr
70 getLazy(DataTypes::ShapeType& shape,bool minus=false)
71 {
72 int pts=DataTypes::noValues(shape);
73 DataTypes::ValueType data(pts,0);
74 for (int i=0;i<pts;++i)
75 {
76 data[i]=minus?-(i+1):i+1;
77 }
78 DataConstant* p=new DataConstant(FunctionSpace(),shape,data);
79 DataAbstract_ptr pp(p);
80 DataLazy* l=new DataLazy(pp);
81 return DataAbstract_ptr(l);
82 }
83
84 DataAbstract_ptr
85 getLazyU(DataTypes::ShapeType& shape, ES_optype typ)
86 {
87 int pts=DataTypes::noValues(shape);
88 DataTypes::ValueType data(pts,0);
89 for (int i=0;i<pts;++i)
90 {
91 data[i]=(i+1);
92 }
93 DataConstant* p=new DataConstant(FunctionSpace(),shape,data);
94 DataAbstract_ptr pp(p);
95 DataLazy* l=new DataLazy(pp,typ);
96 return DataAbstract_ptr(l);
97 }
98
99 DataAbstract_ptr
100 getLazyUP(DataTypes::ShapeType& shape, ES_optype typ, int par)
101 {
102 int pts=DataTypes::noValues(shape);
103 DataTypes::ValueType data(pts,0);
104 for (int i=0;i<pts;++i)
105 {
106 data[i]=(i+1);
107 }
108 DataConstant* p=new DataConstant(FunctionSpace(),shape,data);
109 DataAbstract_ptr pp(p);
110 DataLazy* l=new DataLazy(pp,typ,par);
111 return DataAbstract_ptr(l);
112 }
113
114
115 DataAbstract_ptr
116 getLazyB(DataTypes::ShapeType& shape, ES_optype typ)
117 {
118 int pts=DataTypes::noValues(shape);
119 DataTypes::ValueType data(pts,0);
120 DataTypes::ValueType data2(pts,0);
121 for (int i=0;i<pts;++i)
122 {
123 data[i]=(i+1);
124 data2[i]=-(i+1);
125 }
126 DataConstant* p=new DataConstant(FunctionSpace(),shape,data);
127 DataConstant* p2=new DataConstant(FunctionSpace(),shape,data2);
128 DataAbstract_ptr pp(p);
129 DataAbstract_ptr pp2(p2);
130 DataLazy* l=new DataLazy(pp,pp2,typ);
131 return DataAbstract_ptr(l);
132 }
133
134 DataAbstract_ptr
135 getLazyGTP(DataTypes::ShapeType& shape, ES_optype typ, int ax, int tr)
136 {
137 int pts=DataTypes::noValues(shape);
138 DataTypes::ValueType data(pts,0);
139 DataTypes::ValueType data2(pts,0);
140 for (int i=0;i<pts;++i)
141 {
142 data[i]=(i+1);
143 data2[i]=-(i+1);
144 }
145 DataConstant* p=new DataConstant(FunctionSpace(),shape,data);
146 DataConstant* p2=new DataConstant(FunctionSpace(),shape,data2);
147 DataAbstract_ptr pp(p);
148 DataAbstract_ptr pp2(p2);
149 DataLazy* l=new DataLazy(pp,pp2,typ,ax,tr);
150 return DataAbstract_ptr(l);
151 }
152
153
154
155 #define TESTOP(X,V) { DataAbstract_ptr d1=getLazy(shape); assert(d1->X()==V); assert(d1->isLazy());}
156
157 }
158
159 // This method tests the identity constructor
160 void DataLazyTestCase::testLazy1()
161 {
162 cout << endl;
163 cout << "\tTesting IDENTITY constructor\n";
164
165 DataTypes::ShapeType shape;
166 DataAbstract_ptr d1=getLazy(shape);
167 assert(d1->isLazy());
168 assert(dynamic_pointer_cast<DataLazy>(d1)->getBuffsRequired()==1);
169
170 for (int i=0;i<5;++i)
171 {
172 TESTOP(getRank,i);
173 TESTOP(getNoValues,DataTypes::noValues(shape));
174 TESTOP(getShape,shape);
175 TESTOP(getNumDPPSample,1);
176 TESTOP(getNumSamples,1);
177 shape.push_back(3);
178 }
179 }
180
181 #define TESTOPU(X,V,O) { DataAbstract_ptr d1=getLazyU(shape,O); assert(dynamic_pointer_cast<DataLazy>(d1)->X()==V); assert(d1->isLazy());}
182 // This method tests the unary op constructor
183 // We aren't checking the correctness of the results here, just that they have the right properties
184 void DataLazyTestCase::testLazy2()
185 {
186 cout << endl;
187 cout << "\tTesting UNARY constructor (basic checks only)\n";
188
189 DataTypes::ShapeType shape;
190 DataAbstract_ptr d1=getLazyU(shape,LOG);
191 assert(d1->isLazy());
192
193 for (int j=SIN;j<=LEZ;++j)
194 {
195 shape=DataTypes::scalarShape;
196 ES_optype op=(ES_optype)(j); // not even reinterpret_cast works here
197 // if other compilers object I'll write a switch
198 cout << "\t" << opToString(op) << endl;
199 for (int i=0;i<5;++i)
200 {
201 TESTOPU(getRank,i,op);
202 TESTOPU(getNoValues,DataTypes::noValues(shape),op);
203 TESTOPU(getShape,shape,op);
204 TESTOPU(getNumDPPSample,1,op);
205 TESTOPU(getNumSamples,1,op);
206 TESTOPU(getBuffsRequired,1,op);
207 shape.push_back(3);
208 }
209 }
210 }
211
212 #define TESTOPUP(X,V,O) { DataAbstract_ptr d1=getLazyUP(shape,O,0); assert(dynamic_pointer_cast<DataLazy>(d1)->X()==V); assert(d1->isLazy());}
213 // This method tests the unary op constructor
214 // We aren't checking the correctness of the results here, just that they have the right properties
215 void DataLazyTestCase::testLazy2p()
216 {
217 cout << endl;
218 cout << "\tTesting UNARY (with arg) constructor (basic checks only)\n";
219
220 DataTypes::ShapeType shape;
221 DataTypes::ShapeType traceshape;
222 DataAbstract_ptr d1=getLazyUP(shape,TRANS,0);
223 assert(d1->isLazy());
224
225 for (int j=TRANS;j<=TRACE;++j)
226 {
227 shape=DataTypes::scalarShape; // traceshape is only used once so not initialised here
228
229 ES_optype op=(ES_optype)(j); // not even reinterpret_cast works here
230 // if other compilers object I'll write a switch
231 cout << "\t" << opToString(op) << endl;
232 for (int i=0;i<5;++i)
233 {
234 if (op==TRACE)
235 {
236 if (i>1) // trace only works 2 and up
237 {
238 TESTOPUP(getRank,i-2,op);
239 TESTOPUP(getNoValues, DataTypes::noValues(traceshape),op);
240 TESTOPUP(getShape,traceshape,op);
241
242 TESTOPUP(getNumDPPSample,1,op);
243 TESTOPUP(getNumSamples,1,op);
244 TESTOPUP(getBuffsRequired,2,op);
245 traceshape.push_back(3);
246 }
247 }
248 else
249 {
250 TESTOPUP(getRank,i,op);
251 TESTOPUP(getNoValues,DataTypes::noValues(shape),op);
252 TESTOPUP(getShape,shape,op);
253 TESTOPUP(getNumDPPSample,1,op);
254 TESTOPUP(getNumSamples,1,op);
255 TESTOPUP(getBuffsRequired,2,op);
256 }
257 shape.push_back(3);
258 }
259 }
260 }
261
262 #define TESTOPB(X,V,O) { DataAbstract_ptr d1=getLazyB(shape,O); assert(dynamic_pointer_cast<DataLazy>(d1)->X()==V); assert(dynamic_pointer_cast<DataLazy>(d1)->isLazy());}
263 // This method tests the binary op constructor
264 // We aren't checking the correctness of the results here, just that they have the right properties
265 void DataLazyTestCase::testLazy3()
266 {
267 cout << endl;
268 cout << "\tTesting BINARY constructor (basic checks only)\n";
269
270 DataTypes::ShapeType shape;
271 DataAbstract_ptr d1=getLazyB(shape,ADD);
272 assert(d1->isLazy());
273
274 for (int j=ADD;j<=POW;++j)
275 {
276 shape=DataTypes::scalarShape;
277 ES_optype op=(ES_optype)(j); // not even reinterpret_cast works here
278 // if other compilers object I'll write a switch
279 cout << "\t" << opToString(op) << endl;
280 for (int i=0;i<5;++i)
281 {
282 TESTOPB(getRank,i,op);
283 TESTOPB(getNoValues,DataTypes::noValues(shape),op);
284 TESTOPB(getShape,shape,op);
285 TESTOPB(getNumDPPSample,1,op);
286 TESTOPB(getNumSamples,1,op);
287 TESTOPB(getBuffsRequired,3,op);
288 shape.push_back(3);
289 }
290 }
291 }
292
293
294
295
296 #define TESTOPGTP(X,V,O) { DataAbstract_ptr d1=getLazyGTP(shape,O,0,0); assert(dynamic_pointer_cast<DataLazy>(d1)->X()==V); assert(dynamic_pointer_cast<DataLazy>(d1)->isLazy());}
297
298 // This method tests the GeneralTensorproduct constructor
299 // We aren't checking the correctness of the results here, just that they have the right properties
300 void DataLazyTestCase::testLazy4()
301 {
302 cout << endl;
303 cout << "\tTesting GTP constructor (basic checks only)\n";
304
305 DataTypes::ShapeType shape;
306 DataTypes::ShapeType prodshape;
307
308 DataAbstract_ptr d1=getLazyGTP(shape,PROD,0,0);
309 assert(d1->isLazy());
310
311 for (int j=PROD;j<=PROD;++j)
312 {
313 shape=DataTypes::scalarShape;
314 ES_optype op=(ES_optype)(j); // not even reinterpret_cast works here
315 // if other compilers object I'll write a switch
316 cout << "\t" << opToString(op) << endl;
317 for (int i=0;i<3;++i)
318 {
319 ShapeType ns;
320 for (int k=0;k<i;++k)
321 {
322 ns.push_back(3);
323 ns.push_back(3);
324 }
325 TESTOPGTP(getRank,i*2,op);
326 TESTOPGTP(getNoValues,DataTypes::noValues(ns),op);
327 TESTOPGTP(getShape,ns,op);
328 TESTOPGTP(getNumDPPSample,1,op);
329 TESTOPGTP(getNumSamples,1,op);
330 TESTOPGTP(getBuffsRequired,3,op);
331 shape.push_back(3);
332 }
333 }
334 }
335
336
337
338 void DataLazyTestCase::testBuffers()
339 {
340 cout << endl;
341 cout << "\tTesting Buffs required\n";
342 DataTypes::ShapeType shape;
343 DataAbstract_ptr p=(new DataLazy(getLazy(shape),getLazy(shape),ADD))->getPtr();
344 DataAbstract_ptr p2=(new DataLazy(p,SIN))->getPtr();
345 DataAbstract_ptr p3=(new DataLazy(p2,COS))->getPtr();
346 DataAbstract_ptr p4=(new DataLazy(p3,getLazy(shape),ADD))->getPtr();
347 assert(dynamic_pointer_cast<DataLazy>(p4)->getBuffsRequired()==4);
348 DataAbstract_ptr p5=(new DataLazy(p2,p4,ADD))->getPtr();
349 assert(dynamic_pointer_cast<DataLazy>(p5)->getBuffsRequired()==6);
350 DataAbstract_ptr p6=(new DataLazy(p5,TRANS,0))->getPtr();
351 assert(dynamic_pointer_cast<DataLazy>(p6)->getBuffsRequired()==7);
352 DataAbstract_ptr p7=(new DataLazy(p6,p6,PROD,0,0))->getPtr();
353 assert(dynamic_pointer_cast<DataLazy>(p7)->getBuffsRequired()==9);
354
355 DataTypes::ShapeType r2;
356 r2.push_back(4);
357 r2.push_back(4);
358 DataAbstract_ptr p8=(new DataLazy(getLazy(r2),TRACE,0))->getPtr();
359 assert(dynamic_pointer_cast<DataLazy>(p8)->getBuffsRequired()==2);
360 }
361
362
363 TestSuite* DataLazyTestCase::suite ()
364 {
365 //
366 // create the suite of tests to perform.
367 TestSuite *testSuite = new TestSuite ("DataLazyTestCase");
368
369 testSuite->addTest (new TestCaller< DataLazyTestCase>("Identity",&DataLazyTestCase::testLazy1));
370 testSuite->addTest (new TestCaller< DataLazyTestCase>("Unary",&DataLazyTestCase::testLazy2));
371 testSuite->addTest (new TestCaller< DataLazyTestCase>("Unary (params)",&DataLazyTestCase::testLazy2p));
372 testSuite->addTest (new TestCaller< DataLazyTestCase>("Binary",&DataLazyTestCase::testLazy3));
373 testSuite->addTest (new TestCaller< DataLazyTestCase>("GTP",&DataLazyTestCase::testLazy4));
374 testSuite->addTest (new TestCaller< DataLazyTestCase>("Buffers",&DataLazyTestCase::testBuffers));
375 return testSuite;
376 }

  ViewVC Help
Powered by ViewVC 1.1.26