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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1901 - (show annotations)
Wed Oct 22 02:44:34 2008 UTC (11 years, 9 months ago) by jfenwick
File size: 78699 byte(s)
Improved the api_doxygen target a bit.
Added some documentation.
Added FORCERESOLVE macro to a number of operations.

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 "Data.h"
16
17 #include "DataExpanded.h"
18 #include "DataConstant.h"
19 #include "DataTagged.h"
20 #include "DataEmpty.h"
21 #include "DataLazy.h"
22 #include "FunctionSpaceFactory.h"
23 #include "AbstractContinuousDomain.h"
24 #include "UnaryFuncs.h"
25 #include "FunctionSpaceException.h"
26
27 extern "C" {
28 #include "escript/blocktimer.h"
29 }
30
31 #include <fstream>
32 #include <algorithm>
33 #include <vector>
34 #include <functional>
35
36 #include <boost/python/dict.hpp>
37 #include <boost/python/extract.hpp>
38 #include <boost/python/long.hpp>
39
40 using namespace std;
41 using namespace boost::python;
42 using namespace boost;
43 using namespace escript;
44
45 // ensure the current object is not a DataLazy
46 #define FORCERESOLVE if (isLazy()) {resolve();}
47
48 Data::Data()
49 {
50 //
51 // Default data is type DataEmpty
52 DataAbstract* temp=new DataEmpty();
53 m_data=temp->getPtr();
54 m_protected=false;
55 }
56
57 Data::Data(double value,
58 const tuple& shape,
59 const FunctionSpace& what,
60 bool expanded)
61 {
62 DataTypes::ShapeType dataPointShape;
63 for (int i = 0; i < shape.attr("__len__")(); ++i) {
64 dataPointShape.push_back(extract<const int>(shape[i]));
65 }
66
67 int len = DataTypes::noValues(dataPointShape);
68 DataVector temp_data(len,value,len);
69 initialise(temp_data, dataPointShape, what, expanded);
70 m_protected=false;
71 }
72
73 Data::Data(double value,
74 const DataTypes::ShapeType& dataPointShape,
75 const FunctionSpace& what,
76 bool expanded)
77 {
78 int len = DataTypes::noValues(dataPointShape);
79
80 DataVector temp_data(len,value,len);
81 // DataArrayView temp_dataView(temp_data, dataPointShape);
82
83 // initialise(temp_dataView, what, expanded);
84 initialise(temp_data, dataPointShape, what, expanded);
85
86 m_protected=false;
87 }
88
89 Data::Data(const Data& inData)
90 {
91 m_data=inData.m_data;
92 m_protected=inData.isProtected();
93 }
94
95
96 Data::Data(const Data& inData,
97 const DataTypes::RegionType& region)
98 {
99 //
100 // Create Data which is a slice of another Data
101 DataAbstract* tmp = inData.m_data->getSlice(region);
102 m_data=DataAbstract_ptr(tmp);
103 m_protected=false;
104 }
105
106 Data::Data(const Data& inData,
107 const FunctionSpace& functionspace)
108 {
109 if (inData.isEmpty())
110 {
111 throw DataException("Error - will not interpolate for instances of DataEmpty.");
112 }
113 if (inData.isLazy())
114 {
115 throw DataException("Error - will not interpolate for instances of DataLazy - yet.");
116 }
117 if (inData.getFunctionSpace()==functionspace) {
118 m_data=inData.m_data;
119 }
120 else
121 { // this cast is safe for ref count because the class holds a reference
122 const DataReady* dr=dynamic_cast<const DataReady*>(inData.m_data.get());
123 if (inData.isConstant()) { // for a constant function, we just need to use the new function space
124 if (!inData.probeInterpolation(functionspace))
125 { // Even though this is constant, we still need to check whether interpolation is allowed
126 throw FunctionSpaceException("Call to probeInterpolation returned false for DataConstant.");
127 }
128 DataConstant* dc=new DataConstant(functionspace,inData.m_data->getShape(),dr->getVector());
129 m_data=DataAbstract_ptr(dc);
130 } else {
131 Data tmp(0,inData.getDataPointShape(),functionspace,true);
132 // Note: Must use a reference or pointer to a derived object
133 // in order to get polymorphic behaviour. Shouldn't really
134 // be able to create an instance of AbstractDomain but that was done
135 // as a boost:python work around which may no longer be required.
136 /*const AbstractDomain& inDataDomain=inData.getDomain();*/
137 const_Domain_ptr inDataDomain=inData.getDomain();
138 if (inDataDomain==functionspace.getDomain()) {
139 inDataDomain->interpolateOnDomain(tmp,inData);
140 } else {
141 inDataDomain->interpolateACross(tmp,inData);
142 }
143 m_data=tmp.m_data;
144 }
145 }
146 m_protected=false;
147 }
148
149 Data::Data(DataAbstract* underlyingdata)
150 {
151 // m_data=shared_ptr<DataAbstract>(underlyingdata);
152 m_data=underlyingdata->getPtr();
153 m_protected=false;
154 }
155
156 Data::Data(DataAbstract_ptr underlyingdata)
157 {
158 m_data=underlyingdata;
159 m_protected=false;
160 }
161
162
163 Data::Data(const numeric::array& value,
164 const FunctionSpace& what,
165 bool expanded)
166 {
167 initialise(value,what,expanded);
168 m_protected=false;
169 }
170 /*
171 Data::Data(const DataArrayView& value,
172 const FunctionSpace& what,
173 bool expanded)
174 {
175 initialise(value,what,expanded);
176 m_protected=false;
177 }*/
178
179 Data::Data(const DataTypes::ValueType& value,
180 const DataTypes::ShapeType& shape,
181 const FunctionSpace& what,
182 bool expanded)
183 {
184 initialise(value,shape,what,expanded);
185 m_protected=false;
186 }
187
188
189 Data::Data(const object& value,
190 const FunctionSpace& what,
191 bool expanded)
192 {
193 numeric::array asNumArray(value);
194 initialise(asNumArray,what,expanded);
195 m_protected=false;
196 }
197
198
199 Data::Data(const object& value,
200 const Data& other)
201 {
202 numeric::array asNumArray(value);
203
204 // extract the shape of the numarray
205 DataTypes::ShapeType tempShape=DataTypes::shapeFromNumArray(asNumArray);
206 // /* for (int i=0; i < asNumArray.getrank(); i++) {
207 // tempShape.push_back(extract<int>(asNumArray.getshape()[i]));
208 // }*/
209 // // get the space for the data vector
210 // int len = DataTypes::noValues(tempShape);
211 // DataVector temp_data(len, 0.0, len);
212 // /* DataArrayView temp_dataView(temp_data, tempShape);
213 // temp_dataView.copy(asNumArray);*/
214 // temp_data.copyFromNumArray(asNumArray);
215
216 //
217 // Create DataConstant using the given value and all other parameters
218 // copied from other. If value is a rank 0 object this Data
219 // will assume the point data shape of other.
220
221 if (DataTypes::getRank(tempShape)/*temp_dataView.getRank()*/==0) {
222
223
224 // get the space for the data vector
225 int len1 = DataTypes::noValues(tempShape);
226 DataVector temp_data(len1, 0.0, len1);
227 temp_data.copyFromNumArray(asNumArray);
228
229 int len = DataTypes::noValues(other.getDataPointShape());
230
231 DataVector temp2_data(len, temp_data[0]/*temp_dataView()*/, len);
232 //DataArrayView temp2_dataView(temp2_data, other.getPointDataView().getShape());
233 // initialise(temp2_dataView, other.getFunctionSpace(), false);
234
235 DataConstant* t=new DataConstant(other.getFunctionSpace(),other.getDataPointShape(),temp2_data);
236 // boost::shared_ptr<DataAbstract> sp(t);
237 // m_data=sp;
238 m_data=DataAbstract_ptr(t);
239
240 } else {
241 //
242 // Create a DataConstant with the same sample shape as other
243 // initialise(temp_dataView, other.getFunctionSpace(), false);
244 DataConstant* t=new DataConstant(asNumArray,other.getFunctionSpace());
245 // boost::shared_ptr<DataAbstract> sp(t);
246 // m_data=sp;
247 m_data=DataAbstract_ptr(t);
248 }
249 m_protected=false;
250 }
251
252 Data::~Data()
253 {
254
255 }
256
257
258
259 void
260 Data::initialise(const boost::python::numeric::array& value,
261 const FunctionSpace& what,
262 bool expanded)
263 {
264 //
265 // Construct a Data object of the appropriate type.
266 // Construct the object first as there seems to be a bug which causes
267 // undefined behaviour if an exception is thrown during construction
268 // within the shared_ptr constructor.
269 if (expanded) {
270 DataAbstract* temp=new DataExpanded(value, what);
271 // boost::shared_ptr<DataAbstract> temp_data(temp);
272 // m_data=temp_data;
273 m_data=temp->getPtr();
274 } else {
275 DataAbstract* temp=new DataConstant(value, what);
276 // boost::shared_ptr<DataAbstract> temp_data(temp);
277 // m_data=temp_data;
278 m_data=temp->getPtr();
279 }
280 }
281
282
283 void
284 Data::initialise(const DataTypes::ValueType& value,
285 const DataTypes::ShapeType& shape,
286 const FunctionSpace& what,
287 bool expanded)
288 {
289 //
290 // Construct a Data object of the appropriate type.
291 // Construct the object first as there seems to be a bug which causes
292 // undefined behaviour if an exception is thrown during construction
293 // within the shared_ptr constructor.
294 if (expanded) {
295 DataAbstract* temp=new DataExpanded(what, shape, value);
296 // boost::shared_ptr<DataAbstract> temp_data(temp);
297 // m_data=temp_data;
298 m_data=temp->getPtr();
299 } else {
300 DataAbstract* temp=new DataConstant(what, shape, value);
301 // boost::shared_ptr<DataAbstract> temp_data(temp);
302 // m_data=temp_data;
303 m_data=temp->getPtr();
304 }
305 }
306
307
308 // void
309 // Data::CompareDebug(const Data& rd)
310 // {
311 // using namespace std;
312 // bool mismatch=false;
313 // std::cout << "Comparing left and right" << endl;
314 // const DataTagged* left=dynamic_cast<DataTagged*>(m_data.get());
315 // const DataTagged* right=dynamic_cast<DataTagged*>(rd.m_data.get());
316 //
317 // if (left==0)
318 // {
319 // cout << "left arg is not a DataTagged\n";
320 // return;
321 // }
322 //
323 // if (right==0)
324 // {
325 // cout << "right arg is not a DataTagged\n";
326 // return;
327 // }
328 // cout << "Num elements=" << left->getVector().size() << ":" << right->getVector().size() << std::endl;
329 // cout << "Shapes ";
330 // if (left->getShape()==right->getShape())
331 // {
332 // cout << "ok\n";
333 // }
334 // else
335 // {
336 // cout << "Problem: shapes do not match\n";
337 // mismatch=true;
338 // }
339 // int lim=left->getVector().size();
340 // if (right->getVector().size()) lim=right->getVector().size();
341 // for (int i=0;i<lim;++i)
342 // {
343 // if (left->getVector()[i]!=right->getVector()[i])
344 // {
345 // cout << "[" << i << "] value mismatch " << left->getVector()[i] << ":" << right->getVector()[i] << endl;
346 // mismatch=true;
347 // }
348 // }
349 //
350 // // still need to check the tag map
351 // // also need to watch what is happening to function spaces, are they copied or what?
352 //
353 // const DataTagged::DataMapType& mapleft=left->getTagLookup();
354 // const DataTagged::DataMapType& mapright=right->getTagLookup();
355 //
356 // if (mapleft.size()!=mapright.size())
357 // {
358 // cout << "Maps are different sizes " << mapleft.size() << ":" << mapright.size() << endl;
359 // mismatch=true;
360 // cout << "Left map\n";
361 // DataTagged::DataMapType::const_iterator i,j;
362 // for (i=mapleft.begin();i!=mapleft.end();++i) {
363 // cout << "(" << i->first << "=>" << i->second << ")\n";
364 // }
365 // cout << "Right map\n";
366 // for (i=mapright.begin();i!=mapright.end();++i) {
367 // cout << "(" << i->first << "=>" << i->second << ")\n";
368 // }
369 // cout << "End map\n";
370 //
371 // }
372 //
373 // DataTagged::DataMapType::const_iterator i,j;
374 // for (i=mapleft.begin(),j=mapright.begin();i!=mapleft.end() && j!=mapright.end();++i,++j) {
375 // if ((i->first!=j->first) || (i->second!=j->second))
376 // {
377 // cout << "(" << i->first << "=>" << i->second << ")";
378 // cout << ":(" << j->first << "=>" << j->second << ") ";
379 // mismatch=true;
380 // }
381 // }
382 // if (mismatch)
383 // {
384 // cout << "#Mismatch\n";
385 // }
386 // }
387
388 escriptDataC
389 Data::getDataC()
390 {
391 escriptDataC temp;
392 temp.m_dataPtr=(void*)this;
393 return temp;
394 }
395
396 escriptDataC
397 Data::getDataC() const
398 {
399 escriptDataC temp;
400 temp.m_dataPtr=(void*)this;
401 return temp;
402 }
403
404 const boost::python::tuple
405 Data::getShapeTuple() const
406 {
407 const DataTypes::ShapeType& shape=getDataPointShape();
408 switch(getDataPointRank()) {
409 case 0:
410 return make_tuple();
411 case 1:
412 return make_tuple(long_(shape[0]));
413 case 2:
414 return make_tuple(long_(shape[0]),long_(shape[1]));
415 case 3:
416 return make_tuple(long_(shape[0]),long_(shape[1]),long_(shape[2]));
417 case 4:
418 return make_tuple(long_(shape[0]),long_(shape[1]),long_(shape[2]),long_(shape[3]));
419 default:
420 throw DataException("Error - illegal Data rank.");
421 }
422 }
423
424
425 // The different name is needed because boost has trouble with overloaded functions.
426 // It can't work out what type the function is based soley on its name.
427 // There are ways to fix this involving creating function pointer variables for each form
428 // but there doesn't seem to be a need given that the methods have the same name from the python point of view
429 Data*
430 Data::copySelf()
431 {
432 DataAbstract* temp=m_data->deepCopy();
433 return new Data(temp);
434 }
435
436 void
437 Data::copy(const Data& other)
438 {
439 DataAbstract* temp=other.m_data->deepCopy();
440 DataAbstract_ptr p=temp->getPtr();
441 m_data=p;
442 }
443
444
445 Data
446 Data::delay()
447 {
448 DataLazy* dl=new DataLazy(m_data);
449 return Data(dl);
450 }
451
452 void
453 Data::setToZero()
454 {
455 if (isEmpty())
456 {
457 throw DataException("Error - Operations not permitted on instances of DataEmpty.");
458 }
459 {
460 DataExpanded* temp=dynamic_cast<DataExpanded*>(m_data.get());
461 if (temp!=0) {
462 temp->setToZero();
463 return;
464 }
465 }
466 {
467 DataTagged* temp=dynamic_cast<DataTagged*>(m_data.get());
468 if (temp!=0) {
469 temp->setToZero();
470 return;
471 }
472 }
473 {
474 DataConstant* temp=dynamic_cast<DataConstant*>(m_data.get());
475 if (temp!=0) {
476 temp->setToZero();
477 return;
478 }
479 }
480 throw DataException("Error - Data can not be set to zero.");
481 }
482
483
484 // This implementation of this operation should not need any special treatment of LazyData.
485 void
486 Data::copyWithMask(const Data& other,
487 const Data& mask)
488 {
489 if (other.isEmpty() || mask.isEmpty())
490 {
491 throw DataException("Error - copyWithMask not permitted using instances of DataEmpty.");
492 }
493 Data mask1;
494 Data mask2;
495 mask1 = mask.wherePositive();
496
497 mask2.copy(mask1);
498 mask1 *= other;
499
500 mask2 *= *this;
501 mask2 = *this - mask2;
502 *this = mask1 + mask2;
503 }
504
505 bool
506 Data::isExpanded() const
507 {
508 DataExpanded* temp=dynamic_cast<DataExpanded*>(m_data.get());
509 return (temp!=0);
510 }
511
512 bool
513 Data::isTagged() const
514 {
515 DataTagged* temp=dynamic_cast<DataTagged*>(m_data.get());
516 return (temp!=0);
517 }
518
519 bool
520 Data::isEmpty() const
521 {
522 DataEmpty* temp=dynamic_cast<DataEmpty*>(m_data.get());
523 return (temp!=0);
524 }
525
526 bool
527 Data::isConstant() const
528 {
529 DataConstant* temp=dynamic_cast<DataConstant*>(m_data.get());
530 return (temp!=0);
531 }
532
533 bool
534 Data::isLazy() const
535 {
536 return m_data->isLazy();
537 }
538
539 // at the moment this is synonymous with !isLazy() but that could change
540 bool
541 Data::isReady() const
542 {
543 return (dynamic_cast<DataReady*>(m_data.get())!=0);
544 }
545
546
547 void
548 Data::setProtection()
549 {
550 m_protected=true;
551 }
552
553 bool
554 Data::isProtected() const
555 {
556 return m_protected;
557 }
558
559
560
561 void
562 Data::expand()
563 {
564 if (isConstant()) {
565 DataConstant* tempDataConst=dynamic_cast<DataConstant*>(m_data.get());
566 DataAbstract* temp=new DataExpanded(*tempDataConst);
567 // shared_ptr<DataAbstract> temp_data(temp);
568 // m_data=temp_data;
569 m_data=temp->getPtr();
570 } else if (isTagged()) {
571 DataTagged* tempDataTag=dynamic_cast<DataTagged*>(m_data.get());
572 DataAbstract* temp=new DataExpanded(*tempDataTag);
573 // shared_ptr<DataAbstract> temp_data(temp);
574 // m_data=temp_data;
575 m_data=temp->getPtr();
576 } else if (isExpanded()) {
577 //
578 // do nothing
579 } else if (isEmpty()) {
580 throw DataException("Error - Expansion of DataEmpty not possible.");
581 } else if (isLazy()) {
582 resolve();
583 expand(); // resolve might not give us expanded data
584 } else {
585 throw DataException("Error - Expansion not implemented for this Data type.");
586 }
587 }
588
589 void
590 Data::tag()
591 {
592 if (isConstant()) {
593 DataConstant* tempDataConst=dynamic_cast<DataConstant*>(m_data.get());
594 DataAbstract* temp=new DataTagged(*tempDataConst);
595 // shared_ptr<DataAbstract> temp_data(temp);
596 // m_data=temp_data;
597 m_data=temp->getPtr();
598 } else if (isTagged()) {
599 // do nothing
600 } else if (isExpanded()) {
601 throw DataException("Error - Creating tag data from DataExpanded not possible.");
602 } else if (isEmpty()) {
603 throw DataException("Error - Creating tag data from DataEmpty not possible.");
604 } else if (isLazy()) {
605 DataAbstract_ptr res=m_data->resolve();
606 if (m_data->isExpanded())
607 {
608 throw DataException("Error - data would resolve to DataExpanded, tagging is not possible.");
609 }
610 m_data=res;
611 tag();
612 } else {
613 throw DataException("Error - Tagging not implemented for this Data type.");
614 }
615 }
616
617 void
618 Data::resolve()
619 {
620 if (isLazy())
621 {
622 m_data=m_data->resolve();
623 }
624 }
625
626
627 Data
628 Data::oneOver() const
629 {
630 if (isLazy())
631 {
632 DataLazy* c=new DataLazy(borrowDataPtr(),RECIP);
633 return Data(c);
634 }
635 return C_TensorUnaryOperation(*this, bind1st(divides<double>(),1.));
636 }
637
638 Data
639 Data::wherePositive() const
640 {
641 if (isLazy())
642 {
643 DataLazy* c=new DataLazy(borrowDataPtr(),GZ);
644 return Data(c);
645 }
646 return C_TensorUnaryOperation(*this, bind2nd(greater<double>(),0.0));
647 }
648
649 Data
650 Data::whereNegative() const
651 {
652 if (isLazy())
653 {
654 DataLazy* c=new DataLazy(borrowDataPtr(),LZ);
655 return Data(c);
656 }
657 return C_TensorUnaryOperation(*this, bind2nd(less<double>(),0.0));
658 }
659
660 Data
661 Data::whereNonNegative() const
662 {
663 if (isLazy())
664 {
665 DataLazy* c=new DataLazy(borrowDataPtr(),GEZ);
666 return Data(c);
667 }
668 return C_TensorUnaryOperation(*this, bind2nd(greater_equal<double>(),0.0));
669 }
670
671 Data
672 Data::whereNonPositive() const
673 {
674 if (isLazy())
675 {
676 DataLazy* c=new DataLazy(borrowDataPtr(),LEZ);
677 return Data(c);
678 }
679 return C_TensorUnaryOperation(*this, bind2nd(less_equal<double>(),0.0));
680 }
681
682 Data
683 Data::whereZero(double tol) const
684 {
685 Data dataAbs=abs();
686 return C_TensorUnaryOperation(dataAbs, bind2nd(less_equal<double>(),tol));
687 }
688
689 Data
690 Data::whereNonZero(double tol) const
691 {
692 Data dataAbs=abs();
693 return C_TensorUnaryOperation(dataAbs, bind2nd(greater<double>(),tol));
694 }
695
696 Data
697 Data::interpolate(const FunctionSpace& functionspace) const
698 {
699 return Data(*this,functionspace);
700 }
701
702 bool
703 Data::probeInterpolation(const FunctionSpace& functionspace) const
704 {
705 if (getFunctionSpace()==functionspace) {
706 return true;
707 } else {
708 const_Domain_ptr domain=getDomain();
709 if (*domain==*functionspace.getDomain()) {
710 return domain->probeInterpolationOnDomain(getFunctionSpace().getTypeCode(),functionspace.getTypeCode());
711 } else {
712 return domain->probeInterpolationACross(getFunctionSpace().getTypeCode(),*(functionspace.getDomain()),functionspace.getTypeCode());
713 }
714 }
715 }
716
717 Data
718 Data::gradOn(const FunctionSpace& functionspace) const
719 {
720 if (isEmpty())
721 {
722 throw DataException("Error - operation not permitted on instances of DataEmpty.");
723 }
724 double blocktimer_start = blocktimer_time();
725 if (functionspace.getDomain()!=getDomain())
726 throw DataException("Error - gradient cannot be calculated on different domains.");
727 DataTypes::ShapeType grad_shape=getDataPointShape();
728 grad_shape.push_back(functionspace.getDim());
729 Data out(0.0,grad_shape,functionspace,true);
730 getDomain()->setToGradient(out,*this);
731 blocktimer_increment("grad()", blocktimer_start);
732 return out;
733 }
734
735 Data
736 Data::grad() const
737 {
738 if (isEmpty())
739 {
740 throw DataException("Error - operation not permitted on instances of DataEmpty.");
741 }
742 return gradOn(escript::function(*getDomain()));
743 }
744
745 int
746 Data::getDataPointSize() const
747 {
748 return m_data->getNoValues();
749 }
750
751 DataTypes::ValueType::size_type
752 Data::getLength() const
753 {
754 return m_data->getLength();
755 }
756
757 const
758 boost::python::numeric::array
759 Data:: getValueOfDataPoint(int dataPointNo)
760 {
761 size_t length=0;
762 int i, j, k, l;
763
764 FORCERESOLVE;
765
766 //
767 // determine the rank and shape of each data point
768 int dataPointRank = getDataPointRank();
769 const DataTypes::ShapeType& dataPointShape = getDataPointShape();
770
771 //
772 // create the numeric array to be returned
773 boost::python::numeric::array numArray(0.0);
774
775 //
776 // the shape of the returned numeric array will be the same
777 // as that of the data point
778 int arrayRank = dataPointRank;
779 const DataTypes::ShapeType& arrayShape = dataPointShape;
780
781 //
782 // resize the numeric array to the shape just calculated
783 if (arrayRank==0) {
784 numArray.resize(1);
785 }
786 if (arrayRank==1) {
787 numArray.resize(arrayShape[0]);
788 }
789 if (arrayRank==2) {
790 numArray.resize(arrayShape[0],arrayShape[1]);
791 }
792 if (arrayRank==3) {
793 numArray.resize(arrayShape[0],arrayShape[1],arrayShape[2]);
794 }
795 if (arrayRank==4) {
796 numArray.resize(arrayShape[0],arrayShape[1],arrayShape[2],arrayShape[3]);
797 }
798
799 if (getNumDataPointsPerSample()>0) {
800 int sampleNo = dataPointNo/getNumDataPointsPerSample();
801 int dataPointNoInSample = dataPointNo - sampleNo * getNumDataPointsPerSample();
802 //
803 // Check a valid sample number has been supplied
804 if ((sampleNo >= getNumSamples()) || (sampleNo < 0 )) {
805 throw DataException("Error - Data::convertToNumArray: invalid sampleNo.");
806 }
807
808 //
809 // Check a valid data point number has been supplied
810 if ((dataPointNoInSample >= getNumDataPointsPerSample()) || (dataPointNoInSample < 0)) {
811 throw DataException("Error - Data::convertToNumArray: invalid dataPointNoInSample.");
812 }
813 // TODO: global error handling
814 // create a view of the data if it is stored locally
815 // DataArrayView dataPointView = getDataPoint(sampleNo, dataPointNoInSample);
816 DataTypes::ValueType::size_type offset=getDataOffset(sampleNo, dataPointNoInSample);
817
818
819 switch( dataPointRank ){
820 case 0 :
821 numArray[0] = getDataAtOffset(offset);
822 break;
823 case 1 :
824 for( i=0; i<dataPointShape[0]; i++ )
825 numArray[i]=getDataAtOffset(offset+DataTypes::getRelIndex(dataPointShape, i));
826 break;
827 case 2 :
828 for( i=0; i<dataPointShape[0]; i++ )
829 for( j=0; j<dataPointShape[1]; j++)
830 numArray[make_tuple(i,j)]=getDataAtOffset(offset+DataTypes::getRelIndex(dataPointShape, i,j));
831 break;
832 case 3 :
833 for( i=0; i<dataPointShape[0]; i++ )
834 for( j=0; j<dataPointShape[1]; j++ )
835 for( k=0; k<dataPointShape[2]; k++)
836 numArray[make_tuple(i,j,k)]=getDataAtOffset(offset+DataTypes::getRelIndex(dataPointShape, i,j,k));
837 break;
838 case 4 :
839 for( i=0; i<dataPointShape[0]; i++ )
840 for( j=0; j<dataPointShape[1]; j++ )
841 for( k=0; k<dataPointShape[2]; k++ )
842 for( l=0; l<dataPointShape[3]; l++)
843 numArray[make_tuple(i,j,k,l)]=getDataAtOffset(offset+DataTypes::getRelIndex(dataPointShape, i,j,k,l));
844 break;
845 }
846 }
847 //
848 // return the array
849 return numArray;
850
851 }
852
853 void
854 Data::setValueOfDataPointToPyObject(int dataPointNo, const boost::python::object& py_object)
855 {
856 // this will throw if the value cannot be represented
857 boost::python::numeric::array num_array(py_object);
858 setValueOfDataPointToArray(dataPointNo,num_array);
859 }
860
861 void
862 Data::setValueOfDataPointToArray(int dataPointNo, const boost::python::numeric::array& num_array)
863 {
864 if (isProtected()) {
865 throw DataException("Error - attempt to update protected Data object.");
866 }
867 FORCERESOLVE;
868 //
869 // check rank
870 if (num_array.getrank()<getDataPointRank())
871 throw DataException("Rank of numarray does not match Data object rank");
872
873 //
874 // check shape of num_array
875 for (int i=0; i<getDataPointRank(); i++) {
876 if (extract<int>(num_array.getshape()[i])!=getDataPointShape()[i])
877 throw DataException("Shape of numarray does not match Data object rank");
878 }
879 //
880 // make sure data is expanded:
881 if (!isExpanded()) {
882 expand();
883 }
884 if (getNumDataPointsPerSample()>0) {
885 int sampleNo = dataPointNo/getNumDataPointsPerSample();
886 int dataPointNoInSample = dataPointNo - sampleNo * getNumDataPointsPerSample();
887 m_data->copyToDataPoint(sampleNo, dataPointNoInSample,num_array);
888 } else {
889 m_data->copyToDataPoint(-1, 0,num_array);
890 }
891 }
892
893 void
894 Data::setValueOfDataPoint(int dataPointNo, const double value)
895 {
896 if (isProtected()) {
897 throw DataException("Error - attempt to update protected Data object.");
898 }
899 //
900 // make sure data is expanded:
901 FORCERESOLVE;
902 if (!isExpanded()) {
903 expand();
904 }
905 if (getNumDataPointsPerSample()>0) {
906 int sampleNo = dataPointNo/getNumDataPointsPerSample();
907 int dataPointNoInSample = dataPointNo - sampleNo * getNumDataPointsPerSample();
908 m_data->copyToDataPoint(sampleNo, dataPointNoInSample,value);
909 } else {
910 m_data->copyToDataPoint(-1, 0,value);
911 }
912 }
913
914 const
915 boost::python::numeric::array
916 Data::getValueOfGlobalDataPoint(int procNo, int dataPointNo)
917 {
918 size_t length=0;
919 int i, j, k, l, pos;
920 FORCERESOLVE;
921 //
922 // determine the rank and shape of each data point
923 int dataPointRank = getDataPointRank();
924 const DataTypes::ShapeType& dataPointShape = getDataPointShape();
925
926 //
927 // create the numeric array to be returned
928 boost::python::numeric::array numArray(0.0);
929
930 //
931 // the shape of the returned numeric array will be the same
932 // as that of the data point
933 int arrayRank = dataPointRank;
934 const DataTypes::ShapeType& arrayShape = dataPointShape;
935
936 //
937 // resize the numeric array to the shape just calculated
938 if (arrayRank==0) {
939 numArray.resize(1);
940 }
941 if (arrayRank==1) {
942 numArray.resize(arrayShape[0]);
943 }
944 if (arrayRank==2) {
945 numArray.resize(arrayShape[0],arrayShape[1]);
946 }
947 if (arrayRank==3) {
948 numArray.resize(arrayShape[0],arrayShape[1],arrayShape[2]);
949 }
950 if (arrayRank==4) {
951 numArray.resize(arrayShape[0],arrayShape[1],arrayShape[2],arrayShape[3]);
952 }
953
954 // added for the MPI communication
955 length=1;
956 for( i=0; i<arrayRank; i++ ) length *= arrayShape[i];
957 double *tmpData = new double[length];
958
959 //
960 // load the values for the data point into the numeric array.
961
962 // updated for the MPI case
963 if( get_MPIRank()==procNo ){
964 if (getNumDataPointsPerSample()>0) {
965 int sampleNo = dataPointNo/getNumDataPointsPerSample();
966 int dataPointNoInSample = dataPointNo - sampleNo * getNumDataPointsPerSample();
967 //
968 // Check a valid sample number has been supplied
969 if ((sampleNo >= getNumSamples()) || (sampleNo < 0 )) {
970 throw DataException("Error - Data::convertToNumArray: invalid sampleNo.");
971 }
972
973 //
974 // Check a valid data point number has been supplied
975 if ((dataPointNoInSample >= getNumDataPointsPerSample()) || (dataPointNoInSample < 0)) {
976 throw DataException("Error - Data::convertToNumArray: invalid dataPointNoInSample.");
977 }
978 // TODO: global error handling
979 // create a view of the data if it is stored locally
980 //DataArrayView dataPointView = getDataPoint(sampleNo, dataPointNoInSample);
981 DataTypes::ValueType::size_type offset=getDataOffset(sampleNo, dataPointNoInSample);
982
983 // pack the data from the view into tmpData for MPI communication
984 pos=0;
985 switch( dataPointRank ){
986 case 0 :
987 tmpData[0] = getDataAtOffset(offset);
988 break;
989 case 1 :
990 for( i=0; i<dataPointShape[0]; i++ )
991 tmpData[i]=getDataAtOffset(offset+DataTypes::getRelIndex(dataPointShape, i));
992 break;
993 case 2 :
994 for( i=0; i<dataPointShape[0]; i++ )
995 for( j=0; j<dataPointShape[1]; j++, pos++ )
996 tmpData[pos]=getDataAtOffset(offset+DataTypes::getRelIndex(dataPointShape, i,j));
997 break;
998 case 3 :
999 for( i=0; i<dataPointShape[0]; i++ )
1000 for( j=0; j<dataPointShape[1]; j++ )
1001 for( k=0; k<dataPointShape[2]; k++, pos++ )
1002 tmpData[pos]=getDataAtOffset(offset+DataTypes::getRelIndex(dataPointShape, i,j,k));
1003 break;
1004 case 4 :
1005 for( i=0; i<dataPointShape[0]; i++ )
1006 for( j=0; j<dataPointShape[1]; j++ )
1007 for( k=0; k<dataPointShape[2]; k++ )
1008 for( l=0; l<dataPointShape[3]; l++, pos++ )
1009 tmpData[pos]=getDataAtOffset(offset+DataTypes::getRelIndex(dataPointShape, i,j,k,l));
1010 break;
1011 }
1012 }
1013 }
1014 #ifdef PASO_MPI
1015 // broadcast the data to all other processes
1016 MPI_Bcast( tmpData, length, MPI_DOUBLE, procNo, get_MPIComm() );
1017 #endif
1018
1019 // unpack the data
1020 switch( dataPointRank ){
1021 case 0 :
1022 numArray[0]=tmpData[0];
1023 break;
1024 case 1 :
1025 for( i=0; i<dataPointShape[0]; i++ )
1026 numArray[i]=tmpData[i];
1027 break;
1028 case 2 :
1029 for( i=0; i<dataPointShape[0]; i++ )
1030 for( j=0; j<dataPointShape[1]; j++ )
1031 numArray[make_tuple(i,j)]=tmpData[i+j*dataPointShape[0]];
1032 break;
1033 case 3 :
1034 for( i=0; i<dataPointShape[0]; i++ )
1035 for( j=0; j<dataPointShape[1]; j++ )
1036 for( k=0; k<dataPointShape[2]; k++ )
1037 numArray[make_tuple(i,j,k)]=tmpData[i+dataPointShape[0]*(j*+k*dataPointShape[1])];
1038 break;
1039 case 4 :
1040 for( i=0; i<dataPointShape[0]; i++ )
1041 for( j=0; j<dataPointShape[1]; j++ )
1042 for( k=0; k<dataPointShape[2]; k++ )
1043 for( l=0; l<dataPointShape[3]; l++ )
1044 numArray[make_tuple(i,j,k,l)]=tmpData[i+dataPointShape[0]*(j*+dataPointShape[1]*(k+l*dataPointShape[2]))];
1045 break;
1046 }
1047
1048 delete [] tmpData;
1049 //
1050 // return the loaded array
1051 return numArray;
1052 }
1053
1054
1055 boost::python::numeric::array
1056 Data::integrate_const() const
1057 {
1058 if (isLazy())
1059 {
1060 throw DataException("Error - cannot integrate for constant lazy data.");
1061 }
1062 return integrateWorker();
1063 }
1064
1065 boost::python::numeric::array
1066 Data::integrate()
1067 {
1068 if (isLazy())
1069 {
1070 expand();
1071 }
1072 return integrateWorker();
1073 }
1074
1075
1076
1077 boost::python::numeric::array
1078 Data::integrateWorker() const
1079 {
1080 int index;
1081 int rank = getDataPointRank();
1082 DataTypes::ShapeType shape = getDataPointShape();
1083 int dataPointSize = getDataPointSize();
1084
1085 //
1086 // calculate the integral values
1087 vector<double> integrals(dataPointSize);
1088 vector<double> integrals_local(dataPointSize);
1089 #ifdef PASO_MPI
1090 AbstractContinuousDomain::asAbstractContinuousDomain(getDomain()).setToIntegrals(integrals_local,*this);
1091 // Global sum: use an array instead of a vector because elements of array are guaranteed to be contiguous in memory
1092 double *tmp = new double[dataPointSize];
1093 double *tmp_local = new double[dataPointSize];
1094 for (int i=0; i<dataPointSize; i++) { tmp_local[i] = integrals_local[i]; }
1095 MPI_Allreduce( &tmp_local[0], &tmp[0], dataPointSize, MPI_DOUBLE, MPI_SUM, MPI_COMM_WORLD );
1096 for (int i=0; i<dataPointSize; i++) { integrals[i] = tmp[i]; }
1097 delete[] tmp;
1098 delete[] tmp_local;
1099 #else
1100 AbstractContinuousDomain::asAbstractContinuousDomain(*getDomain()).setToIntegrals(integrals,*this);
1101 #endif
1102
1103 //
1104 // create the numeric array to be returned
1105 // and load the array with the integral values
1106 boost::python::numeric::array bp_array(1.0);
1107 if (rank==0) {
1108 bp_array.resize(1);
1109 index = 0;
1110 bp_array[0] = integrals[index];
1111 }
1112 if (rank==1) {
1113 bp_array.resize(shape[0]);
1114 for (int i=0; i<shape[0]; i++) {
1115 index = i;
1116 bp_array[i] = integrals[index];
1117 }
1118 }
1119 if (rank==2) {
1120 bp_array.resize(shape[0],shape[1]);
1121 for (int i=0; i<shape[0]; i++) {
1122 for (int j=0; j<shape[1]; j++) {
1123 index = i + shape[0] * j;
1124 bp_array[make_tuple(i,j)] = integrals[index];
1125 }
1126 }
1127 }
1128 if (rank==3) {
1129 bp_array.resize(shape[0],shape[1],shape[2]);
1130 for (int i=0; i<shape[0]; i++) {
1131 for (int j=0; j<shape[1]; j++) {
1132 for (int k=0; k<shape[2]; k++) {
1133 index = i + shape[0] * ( j + shape[1] * k );
1134 bp_array[make_tuple(i,j,k)] = integrals[index];
1135 }
1136 }
1137 }
1138 }
1139 if (rank==4) {
1140 bp_array.resize(shape[0],shape[1],shape[2],shape[3]);
1141 for (int i=0; i<shape[0]; i++) {
1142 for (int j=0; j<shape[1]; j++) {
1143 for (int k=0; k<shape[2]; k++) {
1144 for (int l=0; l<shape[3]; l++) {
1145 index = i + shape[0] * ( j + shape[1] * ( k + shape[2] * l ) );
1146 bp_array[make_tuple(i,j,k,l)] = integrals[index];
1147 }
1148 }
1149 }
1150 }
1151 }
1152
1153 //
1154 // return the loaded array
1155 return bp_array;
1156 }
1157
1158 Data
1159 Data::sin() const
1160 {
1161 if (isLazy())
1162 {
1163 DataLazy* c=new DataLazy(borrowDataPtr(),SIN);
1164 return Data(c);
1165 }
1166 return C_TensorUnaryOperation<double (*)(double)>(*this, ::sin);
1167 }
1168
1169 Data
1170 Data::cos() const
1171 {
1172 if (isLazy())
1173 {
1174 DataLazy* c=new DataLazy(borrowDataPtr(),COS);
1175 return Data(c);
1176 }
1177 return C_TensorUnaryOperation<double (*)(double)>(*this, ::cos);
1178 }
1179
1180 Data
1181 Data::tan() const
1182 {
1183 if (isLazy())
1184 {
1185 DataLazy* c=new DataLazy(borrowDataPtr(),TAN);
1186 return Data(c);
1187 }
1188 return C_TensorUnaryOperation<double (*)(double)>(*this, ::tan);
1189 }
1190
1191 Data
1192 Data::asin() const
1193 {
1194 if (isLazy())
1195 {
1196 DataLazy* c=new DataLazy(borrowDataPtr(),ASIN);
1197 return Data(c);
1198 }
1199 return C_TensorUnaryOperation<double (*)(double)>(*this, ::asin);
1200 }
1201
1202 Data
1203 Data::acos() const
1204 {
1205 if (isLazy())
1206 {
1207 DataLazy* c=new DataLazy(borrowDataPtr(),ACOS);
1208 return Data(c);
1209 }
1210 return C_TensorUnaryOperation<double (*)(double)>(*this, ::acos);
1211 }
1212
1213
1214 Data
1215 Data::atan() const
1216 {
1217 if (isLazy())
1218 {
1219 DataLazy* c=new DataLazy(borrowDataPtr(),ATAN);
1220 return Data(c);
1221 }
1222 return C_TensorUnaryOperation<double (*)(double)>(*this, ::atan);
1223 }
1224
1225 Data
1226 Data::sinh() const
1227 {
1228 if (isLazy())
1229 {
1230 DataLazy* c=new DataLazy(borrowDataPtr(),SINH);
1231 return Data(c);
1232 }
1233 return C_TensorUnaryOperation<double (*)(double)>(*this, ::sinh);
1234 }
1235
1236 Data
1237 Data::cosh() const
1238 {
1239 if (isLazy())
1240 {
1241 DataLazy* c=new DataLazy(borrowDataPtr(),COSH);
1242 return Data(c);
1243 }
1244 return C_TensorUnaryOperation<double (*)(double)>(*this, ::cosh);
1245 }
1246
1247 Data
1248 Data::tanh() const
1249 {
1250 if (isLazy())
1251 {
1252 DataLazy* c=new DataLazy(borrowDataPtr(),TANH);
1253 return Data(c);
1254 }
1255 return C_TensorUnaryOperation<double (*)(double)>(*this, ::tanh);
1256 }
1257
1258
1259 Data
1260 Data::erf() const
1261 {
1262 #ifdef _WIN32
1263 throw DataException("Error - Data:: erf function is not supported on _WIN32 platforms.");
1264 #else
1265 if (isLazy())
1266 {
1267 DataLazy* c=new DataLazy(borrowDataPtr(),ERF);
1268 return Data(c);
1269 }
1270 return C_TensorUnaryOperation(*this, ::erf);
1271 #endif
1272 }
1273
1274 Data
1275 Data::asinh() const
1276 {
1277 if (isLazy())
1278 {
1279 DataLazy* c=new DataLazy(borrowDataPtr(),ASINH);
1280 return Data(c);
1281 }
1282 #ifdef _WIN32
1283 return C_TensorUnaryOperation(*this, escript::asinh_substitute);
1284 #else
1285 return C_TensorUnaryOperation(*this, ::asinh);
1286 #endif
1287 }
1288
1289 Data
1290 Data::acosh() const
1291 {
1292 if (isLazy())
1293 {
1294 DataLazy* c=new DataLazy(borrowDataPtr(),ACOSH);
1295 return Data(c);
1296 }
1297 #ifdef _WIN32
1298 return C_TensorUnaryOperation(*this, escript::acosh_substitute);
1299 #else
1300 return C_TensorUnaryOperation(*this, ::acosh);
1301 #endif
1302 }
1303
1304 Data
1305 Data::atanh() const
1306 {
1307 if (isLazy())
1308 {
1309 DataLazy* c=new DataLazy(borrowDataPtr(),ATANH);
1310 return Data(c);
1311 }
1312 #ifdef _WIN32
1313 return C_TensorUnaryOperation(*this, escript::atanh_substitute);
1314 #else
1315 return C_TensorUnaryOperation(*this, ::atanh);
1316 #endif
1317 }
1318
1319 Data
1320 Data::log10() const
1321 { if (isLazy())
1322 {
1323 DataLazy* c=new DataLazy(borrowDataPtr(),LOG10);
1324 return Data(c);
1325 }
1326 return C_TensorUnaryOperation<double (*)(double)>(*this, ::log10);
1327 }
1328
1329 Data
1330 Data::log() const
1331 {
1332 if (isLazy())
1333 {
1334 DataLazy* c=new DataLazy(borrowDataPtr(),LOG);
1335 return Data(c);
1336 }
1337 return C_TensorUnaryOperation<double (*)(double)>(*this, ::log);
1338 }
1339
1340 Data
1341 Data::sign() const
1342 {
1343 if (isLazy())
1344 {
1345 DataLazy* c=new DataLazy(borrowDataPtr(),SIGN);
1346 return Data(c);
1347 }
1348 return C_TensorUnaryOperation(*this, escript::fsign);
1349 }
1350
1351 Data
1352 Data::abs() const
1353 {
1354 if (isLazy())
1355 {
1356 DataLazy* c=new DataLazy(borrowDataPtr(),ABS);
1357 return Data(c);
1358 }
1359 return C_TensorUnaryOperation<double (*)(double)>(*this, ::fabs);
1360 }
1361
1362 Data
1363 Data::neg() const
1364 {
1365 if (isLazy())
1366 {
1367 DataLazy* c=new DataLazy(borrowDataPtr(),NEG);
1368 return Data(c);
1369 }
1370 return C_TensorUnaryOperation(*this, negate<double>());
1371 }
1372
1373 Data
1374 Data::pos() const
1375 {
1376 // not doing lazy check here is deliberate.
1377 // since a deep copy of lazy data should be cheap, I'll just let it happen now
1378 Data result;
1379 // perform a deep copy
1380 result.copy(*this);
1381 return result;
1382 }
1383
1384 Data
1385 Data::exp() const
1386 {
1387 if (isLazy())
1388 {
1389 DataLazy* c=new DataLazy(borrowDataPtr(),EXP);
1390 return Data(c);
1391 }
1392 return C_TensorUnaryOperation<double (*)(double)>(*this, ::exp);
1393 }
1394
1395 Data
1396 Data::sqrt() const
1397 {
1398 if (isLazy())
1399 {
1400 DataLazy* c=new DataLazy(borrowDataPtr(),SQRT);
1401 return Data(c);
1402 }
1403 return C_TensorUnaryOperation<double (*)(double)>(*this, ::sqrt);
1404 }
1405
1406 double
1407 Data::Lsup_const() const
1408 {
1409 if (isLazy())
1410 {
1411 throw DataException("Error - cannot compute Lsup for constant lazy data.");
1412 }
1413 return LsupWorker();
1414 }
1415
1416 double
1417 Data::Lsup()
1418 {
1419 if (isLazy())
1420 {
1421 expand();
1422 }
1423 return LsupWorker();
1424 }
1425
1426 double
1427 Data::sup_const() const
1428 {
1429 if (isLazy())
1430 {
1431 throw DataException("Error - cannot compute sup for constant lazy data.");
1432 }
1433 return supWorker();
1434 }
1435
1436 double
1437 Data::sup()
1438 {
1439 if (isLazy())
1440 {
1441 expand();
1442 }
1443 return supWorker();
1444 }
1445
1446 double
1447 Data::inf_const() const
1448 {
1449 if (isLazy())
1450 {
1451 throw DataException("Error - cannot compute inf for constant lazy data.");
1452 }
1453 return infWorker();
1454 }
1455
1456 double
1457 Data::inf()
1458 {
1459 if (isLazy())
1460 {
1461 expand();
1462 }
1463 return infWorker();
1464 }
1465
1466 double
1467 Data::LsupWorker() const
1468 {
1469 double localValue;
1470 //
1471 // set the initial absolute maximum value to zero
1472
1473 AbsMax abs_max_func;
1474 localValue = algorithm(abs_max_func,0);
1475 #ifdef PASO_MPI
1476 double globalValue;
1477 MPI_Allreduce( &localValue, &globalValue, 1, MPI_DOUBLE, MPI_MAX, MPI_COMM_WORLD );
1478 return globalValue;
1479 #else
1480 return localValue;
1481 #endif
1482 }
1483
1484 double
1485 Data::supWorker() const
1486 {
1487 double localValue;
1488 //
1489 // set the initial maximum value to min possible double
1490 FMax fmax_func;
1491 localValue = algorithm(fmax_func,numeric_limits<double>::max()*-1);
1492 #ifdef PASO_MPI
1493 double globalValue;
1494 MPI_Allreduce( &localValue, &globalValue, 1, MPI_DOUBLE, MPI_MAX, MPI_COMM_WORLD );
1495 return globalValue;
1496 #else
1497 return localValue;
1498 #endif
1499 }
1500
1501 double
1502 Data::infWorker() const
1503 {
1504 double localValue;
1505 //
1506 // set the initial minimum value to max possible double
1507 FMin fmin_func;
1508 localValue = algorithm(fmin_func,numeric_limits<double>::max());
1509 #ifdef PASO_MPI
1510 double globalValue;
1511 MPI_Allreduce( &localValue, &globalValue, 1, MPI_DOUBLE, MPI_MIN, MPI_COMM_WORLD );
1512 return globalValue;
1513 #else
1514 return localValue;
1515 #endif
1516 }
1517
1518 /* TODO */
1519 /* global reduction */
1520 Data
1521 Data::maxval() const
1522 {
1523 //
1524 // set the initial maximum value to min possible double
1525 FMax fmax_func;
1526 return dp_algorithm(fmax_func,numeric_limits<double>::max()*-1);
1527 }
1528
1529 Data
1530 Data::minval() const
1531 {
1532 //
1533 // set the initial minimum value to max possible double
1534 FMin fmin_func;
1535 return dp_algorithm(fmin_func,numeric_limits<double>::max());
1536 }
1537
1538 Data
1539 Data::swapaxes(const int axis0, const int axis1) const
1540 {
1541 int axis0_tmp,axis1_tmp;
1542 DataTypes::ShapeType s=getDataPointShape();
1543 DataTypes::ShapeType ev_shape;
1544 // Here's the equivalent of python s_out=s[axis_offset:]+s[:axis_offset]
1545 // which goes thru all shape vector elements starting with axis_offset (at index=rank wrap around to 0)
1546 int rank=getDataPointRank();
1547 if (rank<2) {
1548 throw DataException("Error - Data::swapaxes argument must have at least rank 2.");
1549 }
1550 if (axis0<0 || axis0>rank-1) {
1551 throw DataException("Error - Data::swapaxes: axis0 must be between 0 and rank-1=" + rank-1);
1552 }
1553 if (axis1<0 || axis1>rank-1) {
1554 throw DataException("Error - Data::swapaxes: axis1 must be between 0 and rank-1=" + rank-1);
1555 }
1556 if (axis0 == axis1) {
1557 throw DataException("Error - Data::swapaxes: axis indices must be different.");
1558 }
1559 if (axis0 > axis1) {
1560 axis0_tmp=axis1;
1561 axis1_tmp=axis0;
1562 } else {
1563 axis0_tmp=axis0;
1564 axis1_tmp=axis1;
1565 }
1566 for (int i=0; i<rank; i++) {
1567 if (i == axis0_tmp) {
1568 ev_shape.push_back(s[axis1_tmp]);
1569 } else if (i == axis1_tmp) {
1570 ev_shape.push_back(s[axis0_tmp]);
1571 } else {
1572 ev_shape.push_back(s[i]);
1573 }
1574 }
1575 Data ev(0.,ev_shape,getFunctionSpace());
1576 ev.typeMatchRight(*this);
1577 m_data->swapaxes(ev.m_data.get(), axis0_tmp, axis1_tmp);
1578 return ev;
1579
1580 }
1581
1582 Data
1583 Data::symmetric() const
1584 {
1585 // check input
1586 DataTypes::ShapeType s=getDataPointShape();
1587 if (getDataPointRank()==2) {
1588 if(s[0] != s[1])
1589 throw DataException("Error - Data::symmetric can only be calculated for rank 2 object with equal first and second dimension.");
1590 }
1591 else if (getDataPointRank()==4) {
1592 if(!(s[0] == s[2] && s[1] == s[3]))
1593 throw DataException("Error - Data::symmetric can only be calculated for rank 4 object with dim0==dim2 and dim1==dim3.");
1594 }
1595 else {
1596 throw DataException("Error - Data::symmetric can only be calculated for rank 2 or 4 object.");
1597 }
1598 Data ev(0.,getDataPointShape(),getFunctionSpace());
1599 ev.typeMatchRight(*this);
1600 m_data->symmetric(ev.m_data.get());
1601 return ev;
1602 }
1603
1604 Data
1605 Data::nonsymmetric() const
1606 {
1607 // check input
1608 DataTypes::ShapeType s=getDataPointShape();
1609 if (getDataPointRank()==2) {
1610 if(s[0] != s[1])
1611 throw DataException("Error - Data::nonsymmetric can only be calculated for rank 2 object with equal first and second dimension.");
1612 DataTypes::ShapeType ev_shape;
1613 ev_shape.push_back(s[0]);
1614 ev_shape.push_back(s[1]);
1615 Data ev(0.,ev_shape,getFunctionSpace());
1616 ev.typeMatchRight(*this);
1617 m_data->nonsymmetric(ev.m_data.get());
1618 return ev;
1619 }
1620 else if (getDataPointRank()==4) {
1621 if(!(s[0] == s[2] && s[1] == s[3]))
1622 throw DataException("Error - Data::nonsymmetric can only be calculated for rank 4 object with dim0==dim2 and dim1==dim3.");
1623 DataTypes::ShapeType ev_shape;
1624 ev_shape.push_back(s[0]);
1625 ev_shape.push_back(s[1]);
1626 ev_shape.push_back(s[2]);
1627 ev_shape.push_back(s[3]);
1628 Data ev(0.,ev_shape,getFunctionSpace());
1629 ev.typeMatchRight(*this);
1630 m_data->nonsymmetric(ev.m_data.get());
1631 return ev;
1632 }
1633 else {
1634 throw DataException("Error - Data::nonsymmetric can only be calculated for rank 2 or 4 object.");
1635 }
1636 }
1637
1638 Data
1639 Data::trace(int axis_offset) const
1640 {
1641 DataTypes::ShapeType s=getDataPointShape();
1642 if (getDataPointRank()==2) {
1643 DataTypes::ShapeType ev_shape;
1644 Data ev(0.,ev_shape,getFunctionSpace());
1645 ev.typeMatchRight(*this);
1646 m_data->trace(ev.m_data.get(), axis_offset);
1647 return ev;
1648 }
1649 if (getDataPointRank()==3) {
1650 DataTypes::ShapeType ev_shape;
1651 if (axis_offset==0) {
1652 int s2=s[2];
1653 ev_shape.push_back(s2);
1654 }
1655 else if (axis_offset==1) {
1656 int s0=s[0];
1657 ev_shape.push_back(s0);
1658 }
1659 Data ev(0.,ev_shape,getFunctionSpace());
1660 ev.typeMatchRight(*this);
1661 m_data->trace(ev.m_data.get(), axis_offset);
1662 return ev;
1663 }
1664 if (getDataPointRank()==4) {
1665 DataTypes::ShapeType ev_shape;
1666 if (axis_offset==0) {
1667 ev_shape.push_back(s[2]);
1668 ev_shape.push_back(s[3]);
1669 }
1670 else if (axis_offset==1) {
1671 ev_shape.push_back(s[0]);
1672 ev_shape.push_back(s[3]);
1673 }
1674 else if (axis_offset==2) {
1675 ev_shape.push_back(s[0]);
1676 ev_shape.push_back(s[1]);
1677 }
1678 Data ev(0.,ev_shape,getFunctionSpace());
1679 ev.typeMatchRight(*this);
1680 m_data->trace(ev.m_data.get(), axis_offset);
1681 return ev;
1682 }
1683 else {
1684 throw DataException("Error - Data::trace can only be calculated for rank 2, 3 or 4 object.");
1685 }
1686 }
1687
1688 Data
1689 Data::transpose(int axis_offset) const
1690 {
1691 DataTypes::ShapeType s=getDataPointShape();
1692 DataTypes::ShapeType ev_shape;
1693 // Here's the equivalent of python s_out=s[axis_offset:]+s[:axis_offset]
1694 // which goes thru all shape vector elements starting with axis_offset (at index=rank wrap around to 0)
1695 int rank=getDataPointRank();
1696 if (axis_offset<0 || axis_offset>rank) {
1697 throw DataException("Error - Data::transpose must have 0 <= axis_offset <= rank=" + rank);
1698 }
1699 for (int i=0; i<rank; i++) {
1700 int index = (axis_offset+i)%rank;
1701 ev_shape.push_back(s[index]); // Append to new shape
1702 }
1703 Data ev(0.,ev_shape,getFunctionSpace());
1704 ev.typeMatchRight(*this);
1705 m_data->transpose(ev.m_data.get(), axis_offset);
1706 return ev;
1707 }
1708
1709 Data
1710 Data::eigenvalues() const
1711 {
1712 // check input
1713 DataTypes::ShapeType s=getDataPointShape();
1714 if (getDataPointRank()!=2)
1715 throw DataException("Error - Data::eigenvalues can only be calculated for rank 2 object.");
1716 if(s[0] != s[1])
1717 throw DataException("Error - Data::eigenvalues can only be calculated for object with equal first and second dimension.");
1718 // create return
1719 DataTypes::ShapeType ev_shape(1,s[0]);
1720 Data ev(0.,ev_shape,getFunctionSpace());
1721 ev.typeMatchRight(*this);
1722 m_data->eigenvalues(ev.m_data.get());
1723 return ev;
1724 }
1725
1726 const boost::python::tuple
1727 Data::eigenvalues_and_eigenvectors(const double tol) const
1728 {
1729 DataTypes::ShapeType s=getDataPointShape();
1730 if (getDataPointRank()!=2)
1731 throw DataException("Error - Data::eigenvalues and eigenvectors can only be calculated for rank 2 object.");
1732 if(s[0] != s[1])
1733 throw DataException("Error - Data::eigenvalues and eigenvectors can only be calculated for object with equal first and second dimension.");
1734 // create return
1735 DataTypes::ShapeType ev_shape(1,s[0]);
1736 Data ev(0.,ev_shape,getFunctionSpace());
1737 ev.typeMatchRight(*this);
1738 DataTypes::ShapeType V_shape(2,s[0]);
1739 Data V(0.,V_shape,getFunctionSpace());
1740 V.typeMatchRight(*this);
1741 m_data->eigenvalues_and_eigenvectors(ev.m_data.get(),V.m_data.get(),tol);
1742 return make_tuple(boost::python::object(ev),boost::python::object(V));
1743 }
1744
1745 const boost::python::tuple
1746 Data::minGlobalDataPoint() const
1747 {
1748 // NB: calc_minGlobalDataPoint( had to be split off from minGlobalDataPoint( as boost::make_tuple causes an
1749 // abort (for unknown reasons) if there are openmp directives with it in the
1750 // surrounding function
1751
1752 int DataPointNo;
1753 int ProcNo;
1754 calc_minGlobalDataPoint(ProcNo,DataPointNo);
1755 return make_tuple(ProcNo,DataPointNo);
1756 }
1757
1758 void
1759 Data::calc_minGlobalDataPoint(int& ProcNo,
1760 int& DataPointNo) const
1761 {
1762 int i,j;
1763 int lowi=0,lowj=0;
1764 double min=numeric_limits<double>::max();
1765
1766 Data temp=minval();
1767
1768 int numSamples=temp.getNumSamples();
1769 int numDPPSample=temp.getNumDataPointsPerSample();
1770
1771 double next,local_min;
1772 int local_lowi,local_lowj;
1773
1774 #pragma omp parallel private(next,local_min,local_lowi,local_lowj)
1775 {
1776 local_min=min;
1777 #pragma omp for private(i,j) schedule(static)
1778 for (i=0; i<numSamples; i++) {
1779 for (j=0; j<numDPPSample; j++) {
1780 next=temp.getDataAtOffset(temp.getDataOffset(i,j));
1781 if (next<local_min) {
1782 local_min=next;
1783 local_lowi=i;
1784 local_lowj=j;
1785 }
1786 }
1787 }
1788 #pragma omp critical
1789 if (local_min<min) {
1790 min=local_min;
1791 lowi=local_lowi;
1792 lowj=local_lowj;
1793 }
1794 }
1795
1796 #ifdef PASO_MPI
1797 // determine the processor on which the minimum occurs
1798 next = temp.getDataPoint(lowi,lowj);
1799 int lowProc = 0;
1800 double *globalMins = new double[get_MPISize()+1];
1801 int error = MPI_Gather ( &next, 1, MPI_DOUBLE, globalMins, 1, MPI_DOUBLE, 0, get_MPIComm() );
1802
1803 if( get_MPIRank()==0 ){
1804 next = globalMins[lowProc];
1805 for( i=1; i<get_MPISize(); i++ )
1806 if( next>globalMins[i] ){
1807 lowProc = i;
1808 next = globalMins[i];
1809 }
1810 }
1811 MPI_Bcast( &lowProc, 1, MPI_DOUBLE, 0, get_MPIComm() );
1812
1813 delete [] globalMins;
1814 ProcNo = lowProc;
1815 #else
1816 ProcNo = 0;
1817 #endif
1818 DataPointNo = lowj + lowi * numDPPSample;
1819 }
1820
1821 void
1822 Data::saveDX(std::string fileName) const
1823 {
1824 if (isEmpty())
1825 {
1826 throw DataException("Error - Operations not permitted on instances of DataEmpty.");
1827 }
1828 boost::python::dict args;
1829 args["data"]=boost::python::object(this);
1830 getDomain()->saveDX(fileName,args);
1831 return;
1832 }
1833
1834 void
1835 Data::saveVTK(std::string fileName) const
1836 {
1837 if (isEmpty())
1838 {
1839 throw DataException("Error - Operations not permitted on instances of DataEmpty.");
1840 }
1841 boost::python::dict args;
1842 args["data"]=boost::python::object(this);
1843 getDomain()->saveVTK(fileName,args);
1844 return;
1845 }
1846
1847 Data&
1848 Data::operator+=(const Data& right)
1849 {
1850 if (isProtected()) {
1851 throw DataException("Error - attempt to update protected Data object.");
1852 }
1853 binaryOp(right,plus<double>());
1854 return (*this);
1855 }
1856
1857 Data&
1858 Data::operator+=(const boost::python::object& right)
1859 {
1860 Data tmp(right,getFunctionSpace(),false);
1861 binaryOp(tmp,plus<double>());
1862 return (*this);
1863 }
1864 Data&
1865 Data::operator=(const Data& other)
1866 {
1867 copy(other);
1868 return (*this);
1869 }
1870
1871 Data&
1872 Data::operator-=(const Data& right)
1873 {
1874 if (isProtected()) {
1875 throw DataException("Error - attempt to update protected Data object.");
1876 }
1877 binaryOp(right,minus<double>());
1878 return (*this);
1879 }
1880
1881 Data&
1882 Data::operator-=(const boost::python::object& right)
1883 {
1884 Data tmp(right,getFunctionSpace(),false);
1885 binaryOp(tmp,minus<double>());
1886 return (*this);
1887 }
1888
1889 Data&
1890 Data::operator*=(const Data& right)
1891 {
1892 if (isProtected()) {
1893 throw DataException("Error - attempt to update protected Data object.");
1894 }
1895 binaryOp(right,multiplies<double>());
1896 return (*this);
1897 }
1898
1899 Data&
1900 Data::operator*=(const boost::python::object& right)
1901 {
1902 Data tmp(right,getFunctionSpace(),false);
1903 binaryOp(tmp,multiplies<double>());
1904 return (*this);
1905 }
1906
1907 Data&
1908 Data::operator/=(const Data& right)
1909 {
1910 if (isProtected()) {
1911 throw DataException("Error - attempt to update protected Data object.");
1912 }
1913 binaryOp(right,divides<double>());
1914 return (*this);
1915 }
1916
1917 Data&
1918 Data::operator/=(const boost::python::object& right)
1919 {
1920 Data tmp(right,getFunctionSpace(),false);
1921 binaryOp(tmp,divides<double>());
1922 return (*this);
1923 }
1924
1925 Data
1926 Data::rpowO(const boost::python::object& left) const
1927 {
1928 Data left_d(left,*this);
1929 return left_d.powD(*this);
1930 }
1931
1932 Data
1933 Data::powO(const boost::python::object& right) const
1934 {
1935 Data tmp(right,getFunctionSpace(),false);
1936 return powD(tmp);
1937 }
1938
1939 Data
1940 Data::powD(const Data& right) const
1941 {
1942 return C_TensorBinaryOperation<double (*)(double, double)>(*this, right, ::pow);
1943 }
1944
1945 //
1946 // NOTE: It is essential to specify the namespace this operator belongs to
1947 Data
1948 escript::operator+(const Data& left, const Data& right)
1949 {
1950 if (left.isLazy() || right.isLazy())
1951 {
1952 DataLazy* c=new DataLazy(left.borrowDataPtr(),right.borrowDataPtr(),ADD);
1953 return Data(c);
1954 }
1955 return C_TensorBinaryOperation(left, right, plus<double>());
1956 }
1957
1958 //
1959 // NOTE: It is essential to specify the namespace this operator belongs to
1960 Data
1961 escript::operator-(const Data& left, const Data& right)
1962 {
1963 if (left.isLazy() || right.isLazy())
1964 {
1965 DataLazy* c=new DataLazy(left.borrowDataPtr(),right.borrowDataPtr(),SUB);
1966 return Data(c);
1967 }
1968 return C_TensorBinaryOperation(left, right, minus<double>());
1969 }
1970
1971 //
1972 // NOTE: It is essential to specify the namespace this operator belongs to
1973 Data
1974 escript::operator*(const Data& left, const Data& right)
1975 {
1976 if (left.isLazy() || right.isLazy())
1977 {
1978 DataLazy* c=new DataLazy(left.borrowDataPtr(),right.borrowDataPtr(),MUL);
1979 return Data(c);
1980 }
1981 return C_TensorBinaryOperation(left, right, multiplies<double>());
1982 }
1983
1984 //
1985 // NOTE: It is essential to specify the namespace this operator belongs to
1986 Data
1987 escript::operator/(const Data& left, const Data& right)
1988 {
1989 if (left.isLazy() || right.isLazy())
1990 {
1991 DataLazy* c=new DataLazy(left.borrowDataPtr(),right.borrowDataPtr(),DIV);
1992 return Data(c);
1993 }
1994 return C_TensorBinaryOperation(left, right, divides<double>());
1995 }
1996
1997 //
1998 // NOTE: It is essential to specify the namespace this operator belongs to
1999 Data
2000 escript::operator+(const Data& left, const boost::python::object& right)
2001 {
2002 return left+Data(right,left.getFunctionSpace(),false);
2003 }
2004
2005 //
2006 // NOTE: It is essential to specify the namespace this operator belongs to
2007 Data
2008 escript::operator-(const Data& left, const boost::python::object& right)
2009 {
2010 return left-Data(right,left.getFunctionSpace(),false);
2011 }
2012
2013 //
2014 // NOTE: It is essential to specify the namespace this operator belongs to
2015 Data
2016 escript::operator*(const Data& left, const boost::python::object& right)
2017 {
2018 return left*Data(right,left.getFunctionSpace(),false);
2019 }
2020
2021 //
2022 // NOTE: It is essential to specify the namespace this operator belongs to
2023 Data
2024 escript::operator/(const Data& left, const boost::python::object& right)
2025 {
2026 return left/Data(right,left.getFunctionSpace(),false);
2027 }
2028
2029 //
2030 // NOTE: It is essential to specify the namespace this operator belongs to
2031 Data
2032 escript::operator+(const boost::python::object& left, const Data& right)
2033 {
2034 return Data(left,right.getFunctionSpace(),false)+right;
2035 }
2036
2037 //
2038 // NOTE: It is essential to specify the namespace this operator belongs to
2039 Data
2040 escript::operator-(const boost::python::object& left, const Data& right)
2041 {
2042 return Data(left,right.getFunctionSpace(),false)-right;
2043 }
2044
2045 //
2046 // NOTE: It is essential to specify the namespace this operator belongs to
2047 Data
2048 escript::operator*(const boost::python::object& left, const Data& right)
2049 {
2050 return Data(left,right.getFunctionSpace(),false)*right;
2051 }
2052
2053 //
2054 // NOTE: It is essential to specify the namespace this operator belongs to
2055 Data
2056 escript::operator/(const boost::python::object& left, const Data& right)
2057 {
2058 return Data(left,right.getFunctionSpace(),false)/right;
2059 }
2060
2061
2062 /* TODO */
2063 /* global reduction */
2064 Data
2065 Data::getItem(const boost::python::object& key) const
2066 {
2067 // const DataArrayView& view=getPointDataView();
2068
2069 DataTypes::RegionType slice_region=DataTypes::getSliceRegion(getDataPointShape(),key);
2070
2071 if (slice_region.size()!=getDataPointRank()) {
2072 throw DataException("Error - slice size does not match Data rank.");
2073 }
2074
2075 return getSlice(slice_region);
2076 }
2077
2078 /* TODO */
2079 /* global reduction */
2080 Data
2081 Data::getSlice(const DataTypes::RegionType& region) const
2082 {
2083 return Data(*this,region);
2084 }
2085
2086 /* TODO */
2087 /* global reduction */
2088 void
2089 Data::setItemO(const boost::python::object& key,
2090 const boost::python::object& value)
2091 {
2092 Data tempData(value,getFunctionSpace());
2093 setItemD(key,tempData);
2094 }
2095
2096 void
2097 Data::setItemD(const boost::python::object& key,
2098 const Data& value)
2099 {
2100 // const DataArrayView& view=getPointDataView();
2101
2102 DataTypes::RegionType slice_region=DataTypes::getSliceRegion(getDataPointShape(),key);
2103 if (slice_region.size()!=getDataPointRank()) {
2104 throw DataException("Error - slice size does not match Data rank.");
2105 }
2106 if (getFunctionSpace()!=value.getFunctionSpace()) {
2107 setSlice(Data(value,getFunctionSpace()),slice_region);
2108 } else {
2109 setSlice(value,slice_region);
2110 }
2111 }
2112
2113 void
2114 Data::setSlice(const Data& value,
2115 const DataTypes::RegionType& region)
2116 {
2117 if (isProtected()) {
2118 throw DataException("Error - attempt to update protected Data object.");
2119 }
2120 if (isLazy())
2121 {
2122 throw DataException("Error - setSlice not permitted on lazy data.");
2123 }
2124 Data tempValue(value);
2125 typeMatchLeft(tempValue);
2126 typeMatchRight(tempValue);
2127 getReady()->setSlice(tempValue.m_data.get(),region);
2128 }
2129
2130 void
2131 Data::typeMatchLeft(Data& right) const
2132 {
2133 if (isExpanded()){
2134 right.expand();
2135 } else if (isTagged()) {
2136 if (right.isConstant()) {
2137 right.tag();
2138 }
2139 }
2140 }
2141
2142 void
2143 Data::typeMatchRight(const Data& right)
2144 {
2145 if (isTagged()) {
2146 if (right.isExpanded()) {
2147 expand();
2148 }
2149 } else if (isConstant()) {
2150 if (right.isExpanded()) {
2151 expand();
2152 } else if (right.isTagged()) {
2153 tag();
2154 }
2155 }
2156 }
2157
2158 void
2159 Data::setTaggedValueByName(std::string name,
2160 const boost::python::object& value)
2161 {
2162 if (getFunctionSpace().getDomain()->isValidTagName(name)) {
2163 FORCERESOLVE;
2164 int tagKey=getFunctionSpace().getDomain()->getTag(name);
2165 setTaggedValue(tagKey,value);
2166 }
2167 }
2168 void
2169 Data::setTaggedValue(int tagKey,
2170 const boost::python::object& value)
2171 {
2172 if (isProtected()) {
2173 throw DataException("Error - attempt to update protected Data object.");
2174 }
2175 //
2176 // Ensure underlying data object is of type DataTagged
2177 FORCERESOLVE;
2178 if (isConstant()) tag();
2179 numeric::array asNumArray(value);
2180
2181 // extract the shape of the numarray
2182 DataTypes::ShapeType tempShape;
2183 for (int i=0; i < asNumArray.getrank(); i++) {
2184 tempShape.push_back(extract<int>(asNumArray.getshape()[i]));
2185 }
2186
2187 DataVector temp_data2;
2188 temp_data2.copyFromNumArray(asNumArray);
2189
2190 m_data->setTaggedValue(tagKey,tempShape, temp_data2);
2191 }
2192
2193
2194 void
2195 Data::setTaggedValueFromCPP(int tagKey,
2196 const DataTypes::ShapeType& pointshape,
2197 const DataTypes::ValueType& value,
2198 int dataOffset)
2199 {
2200 if (isProtected()) {
2201 throw DataException("Error - attempt to update protected Data object.");
2202 }
2203 //
2204 // Ensure underlying data object is of type DataTagged
2205 FORCERESOLVE;
2206 if (isConstant()) tag();
2207 //
2208 // Call DataAbstract::setTaggedValue
2209 m_data->setTaggedValue(tagKey,pointshape, value, dataOffset);
2210 }
2211
2212 int
2213 Data::getTagNumber(int dpno)
2214 {
2215 if (isEmpty())
2216 {
2217 throw DataException("Error - operation not permitted on instances of DataEmpty.");
2218 }
2219 return getFunctionSpace().getTagFromDataPointNo(dpno);
2220 }
2221
2222
2223 ostream& escript::operator<<(ostream& o, const Data& data)
2224 {
2225 o << data.toString();
2226 return o;
2227 }
2228
2229 Data
2230 escript::C_GeneralTensorProduct(Data& arg_0,
2231 Data& arg_1,
2232 int axis_offset,
2233 int transpose)
2234 {
2235 // General tensor product: res(SL x SR) = arg_0(SL x SM) * arg_1(SM x SR)
2236 // SM is the product of the last axis_offset entries in arg_0.getShape().
2237
2238 // Interpolate if necessary and find an appropriate function space
2239 Data arg_0_Z, arg_1_Z;
2240 if (arg_0.getFunctionSpace()!=arg_1.getFunctionSpace()) {
2241 if (arg_0.probeInterpolation(arg_1.getFunctionSpace())) {
2242 arg_0_Z = arg_0.interpolate(arg_1.getFunctionSpace());
2243 arg_1_Z = Data(arg_1);
2244 }
2245 else if (arg_1.probeInterpolation(arg_0.getFunctionSpace())) {
2246 arg_1_Z=arg_1.interpolate(arg_0.getFunctionSpace());
2247 arg_0_Z =Data(arg_0);
2248 }
2249 else {
2250 throw DataException("Error - C_GeneralTensorProduct: arguments have incompatible function spaces.");
2251 }
2252 } else {
2253 arg_0_Z = Data(arg_0);
2254 arg_1_Z = Data(arg_1);
2255 }
2256 // Get rank and shape of inputs
2257 int rank0 = arg_0_Z.getDataPointRank();
2258 int rank1 = arg_1_Z.getDataPointRank();
2259 const DataTypes::ShapeType& shape0 = arg_0_Z.getDataPointShape();
2260 const DataTypes::ShapeType& shape1 = arg_1_Z.getDataPointShape();
2261
2262 // Prepare for the loops of the product and verify compatibility of shapes
2263 int start0=0, start1=0;
2264 if (transpose == 0) {}
2265 else if (transpose == 1) { start0 = axis_offset; }
2266 else if (transpose == 2) { start1 = rank1-axis_offset; }
2267 else { throw DataException("C_GeneralTensorProduct: Error - transpose should be 0, 1 or 2"); }
2268
2269
2270 // Adjust the shapes for transpose
2271 DataTypes::ShapeType tmpShape0(rank0); // pre-sizing the vectors rather
2272 DataTypes::ShapeType tmpShape1(rank1); // than using push_back
2273 for (int i=0; i<rank0; i++) { tmpShape0[i]=shape0[(i+start0)%rank0]; }
2274 for (int i=0; i<rank1; i++) { tmpShape1[i]=shape1[(i+start1)%rank1]; }
2275
2276 #if 0
2277 // For debugging: show shape after transpose
2278 char tmp[100];
2279 std::string shapeStr;
2280 shapeStr = "(";
2281 for (int i=0; i<rank0; i++) { sprintf(tmp, "%d,", tmpShape0[i]); shapeStr += tmp; }
2282 shapeStr += ")";
2283 cout << "C_GeneralTensorProduct: Shape of arg0 is " << shapeStr << endl;
2284 shapeStr = "(";
2285 for (int i=0; i<rank1; i++) { sprintf(tmp, "%d,", tmpShape1[i]); shapeStr += tmp; }
2286 shapeStr += ")";
2287 cout << "C_GeneralTensorProduct: Shape of arg1 is " << shapeStr << endl;
2288 #endif
2289
2290 // Prepare for the loops of the product
2291 int SL=1, SM=1, SR=1;
2292 for (int i=0; i<rank0-axis_offset; i++) {
2293 SL *= tmpShape0[i];
2294 }
2295 for (int i=rank0-axis_offset; i<rank0; i++) {
2296 if (tmpShape0[i] != tmpShape1[i-(rank0-axis_offset)]) {
2297 throw DataException("C_GeneralTensorProduct: Error - incompatible shapes");
2298 }
2299 SM *= tmpShape0[i];
2300 }
2301 for (int i=axis_offset; i<rank1; i++) {
2302 SR *= tmpShape1[i];
2303 }
2304
2305 // Define the shape of the output (rank of shape is the sum of the loop ranges below)
2306 DataTypes::ShapeType shape2(rank0+rank1-2*axis_offset);
2307 { // block to limit the scope of out_index
2308 int out_index=0;
2309 for (int i=0; i<rank0-axis_offset; i++, ++out_index) { shape2[out_index]=tmpShape0[i]; } // First part of arg_0_Z
2310 for (int i=axis_offset; i<rank1; i++, ++out_index) { shape2[out_index]=tmpShape1[i]; } // Last part of arg_1_Z
2311 }
2312
2313 // Declare output Data object
2314 Data res;
2315
2316 if (arg_0_Z.isConstant() && arg_1_Z.isConstant()) {
2317 res = Data(0.0, shape2, arg_1_Z.getFunctionSpace()); // DataConstant output
2318 double *ptr_0 = &(arg_0_Z.getDataAtOffset(0));
2319 double *ptr_1 = &(arg_1_Z.getDataAtOffset(0));
2320 double *ptr_2 = &(res.getDataAtOffset(0));
2321 matrix_matrix_product(SL, SM, SR, ptr_0, ptr_1, ptr_2, transpose);
2322 }
2323 else if (arg_0_Z.isConstant() && arg_1_Z.isTagged()) {
2324
2325 // Prepare the DataConstant input
2326 DataConstant* tmp_0=dynamic_cast<DataConstant*>(arg_0_Z.borrowData());
2327 if (tmp_0==0) { throw DataException("GTP Programming error - casting to DataConstant."); }
2328
2329 // Borrow DataTagged input from Data object
2330 DataTagged* tmp_1=dynamic_cast<DataTagged*>(arg_1_Z.borrowData());
2331 if (tmp_1==0) { throw DataException("GTP_1 Programming error - casting to DataTagged."); }
2332
2333 // Prepare a DataTagged output 2
2334 res = Data(0.0, shape2, arg_1_Z.getFunctionSpace()); // DataTagged output
2335 res.tag();
2336 DataTagged* tmp_2=dynamic_cast<DataTagged*>(res.borrowData());
2337 if (tmp_2==0) { throw DataException("GTP Programming error - casting to DataTagged."); }
2338
2339 // Prepare offset into DataConstant
2340 int offset_0 = tmp_0->getPointOffset(0,0);
2341 double *ptr_0 = &(arg_0_Z.getDataAtOffset(offset_0));
2342 // Get the views
2343 // DataArrayView view_1 = tmp_1->getDefaultValue();
2344 // DataArrayView view_2 = tmp_2->getDefaultValue();
2345 // // Get the pointers to the actual data
2346 // double *ptr_1 = &((view_1.getData())[0]);
2347 // double *ptr_2 = &((view_2.getData())[0]);
2348
2349 double *ptr_1 = &(tmp_1->getDefaultValue(0));
2350 double *ptr_2 = &(tmp_2->getDefaultValue(0));
2351
2352
2353 // Compute an MVP for the default
2354 matrix_matrix_product(SL, SM, SR, ptr_0, ptr_1, ptr_2, transpose);
2355 // Compute an MVP for each tag
2356 const DataTagged::DataMapType& lookup_1=tmp_1->getTagLookup();
2357 DataTagged::DataMapType::const_iterator i; // i->first is a tag, i->second is an offset into memory
2358 for (i=lookup_1.begin();i!=lookup_1.end();i++) {
2359 tmp_2->addTag(i->first);
2360 // DataArrayView view_1 = tmp_1->getDataPointByTag(i->first);
2361 // DataArrayView view_2 = tmp_2->getDataPointByTag(i->first);
2362 // double *ptr_1 = &view_1.getData(0);
2363 // double *ptr_2 = &view_2.getData(0);
2364
2365 double *ptr_1 = &(tmp_1->getDataByTag(i->first,0));
2366 double *ptr_2 = &(tmp_2->getDataByTag(i->first,0));
2367
2368 matrix_matrix_product(SL, SM, SR, ptr_0, ptr_1, ptr_2, transpose);
2369 }
2370
2371 }
2372 else if (arg_0_Z.isConstant() && arg_1_Z.isExpanded()) {
2373
2374 res = Data(0.0, shape2, arg_1_Z.getFunctionSpace(),true); // DataExpanded output
2375 DataConstant* tmp_0=dynamic_cast<DataConstant*>(arg_0_Z.borrowData());
2376 DataExpanded* tmp_1=dynamic_cast<DataExpanded*>(arg_1_Z.borrowData());
2377 DataExpanded* tmp_2=dynamic_cast<DataExpanded*>(res.borrowData());
2378 if (tmp_0==0) { throw DataException("GTP Programming error - casting to DataConstant."); }
2379 if (tmp_1==0) { throw DataException("GTP Programming error - casting to DataExpanded."); }
2380 if (tmp_2==0) { throw DataException("GTP Programming error - casting to DataExpanded."); }
2381 int sampleNo_1,dataPointNo_1;
2382 int numSamples_1 = arg_1_Z.getNumSamples();
2383 int numDataPointsPerSample_1 = arg_1_Z.getNumDataPointsPerSample();
2384 int offset_0 = tmp_0->getPointOffset(0,0);
2385 #pragma omp parallel for private(sampleNo_1,dataPointNo_1) schedule(static)
2386 for (sampleNo_1 = 0; sampleNo_1 < numSamples_1; sampleNo_1++) {
2387 for (dataPointNo_1 = 0; dataPointNo_1 < numDataPointsPerSample_1; dataPointNo_1++) {
2388 int offset_1 = tmp_1->getPointOffset(sampleNo_1,dataPointNo_1);
2389 int offset_2 = tmp_2->getPointOffset(sampleNo_1,dataPointNo_1);
2390 double *ptr_0 = &(arg_0_Z.getDataAtOffset(offset_0));
2391 double *ptr_1 = &(arg_1_Z.getDataAtOffset(offset_1));
2392 double *ptr_2 = &(res.getDataAtOffset(offset_2));
2393 matrix_matrix_product(SL, SM, SR, ptr_0, ptr_1, ptr_2, transpose);
2394 }
2395 }
2396
2397 }
2398 else if (arg_0_Z.isTagged() && arg_1_Z.isConstant()) {
2399
2400 // Borrow DataTagged input from Data object
2401 DataTagged* tmp_0=dynamic_cast<DataTagged*>(arg_0_Z.borrowData());
2402 if (tmp_0==0) { throw DataException("GTP_0 Programming error - casting to DataTagged."); }
2403
2404 // Prepare the DataConstant input
2405 DataConstant* tmp_1=dynamic_cast<DataConstant*>(arg_1_Z.borrowData());
2406 if (tmp_1==0) { throw DataException("GTP Programming error - casting to DataConstant."); }
2407
2408 // Prepare a DataTagged output 2
2409 res = Data(0.0, shape2, arg_0_Z.getFunctionSpace()); // DataTagged output
2410 res.tag();
2411 DataTagged* tmp_2=dynamic_cast<DataTagged*>(res.borrowData());
2412 if (tmp_2==0) { throw DataException("GTP Programming error - casting to DataTagged."); }
2413
2414 // Prepare offset into DataConstant
2415 int offset_1 = tmp_1->getPointOffset(0,0);
2416 double *ptr_1 = &(arg_1_Z.getDataAtOffset(offset_1));
2417 // Get the views
2418 // DataArrayView view_0 = tmp_0->getDefaultValue();
2419 // DataArrayView view_2 = tmp_2->getDefaultValue();
2420 // // Get the pointers to the actual data
2421 // double *ptr_0 = &((view_0.getData())[0]);
2422 // double *ptr_2 = &((view_2.getData())[0]);
2423
2424 double *ptr_0 = &(tmp_0->getDefaultValue(0));
2425 double *ptr_2 = &(tmp_2->getDefaultValue(0));
2426
2427 // Compute an MVP for the default
2428 matrix_matrix_product(SL, SM, SR, ptr_0, ptr_1, ptr_2, transpose);
2429 // Compute an MVP for each tag
2430 const DataTagged::DataMapType& lookup_0=tmp_0->getTagLookup();
2431 DataTagged::DataMapType::const_iterator i; // i->first is a tag, i->second is an offset into memory
2432 for (i=lookup_0.begin();i!=lookup_0.end();i++) {
2433 // tmp_2->addTaggedValue(i->first,tmp_2->getDefaultValue());
2434 // DataArrayView view_0 = tmp_0->getDataPointByTag(i->first);
2435 // DataArrayView view_2 = tmp_2->getDataPointByTag(i->first);
2436 // double *ptr_0 = &view_0.getData(0);
2437 // double *ptr_2 = &view_2.getData(0);
2438
2439 tmp_2->addTag(i->first);
2440 double *ptr_0 = &(tmp_0->getDataByTag(i->first,0));
2441 double *ptr_2 = &(tmp_2->getDataByTag(i->first,0));
2442 matrix_matrix_product(SL, SM, SR, ptr_0, ptr_1, ptr_2, transpose);
2443 }
2444
2445 }
2446 else if (arg_0_Z.isTagged() && arg_1_Z.isTagged()) {
2447
2448 // Borrow DataTagged input from Data object
2449 DataTagged* tmp_0=dynamic_cast<DataTagged*>(arg_0_Z.borrowData());
2450 if (tmp_0==0) { throw DataException("GTP Programming error - casting to DataTagged."); }
2451
2452 // Borrow DataTagged input from Data object
2453 DataTagged* tmp_1=dynamic_cast<DataTagged*>(arg_1_Z.borrowData());
2454 if (tmp_1==0) { throw DataException("GTP Programming error - casting to DataTagged."); }
2455
2456 // Prepare a DataTagged output 2
2457 res = Data(0.0, shape2, arg_1_Z.getFunctionSpace());
2458 res.tag(); // DataTagged output
2459 DataTagged* tmp_2=dynamic_cast<DataTagged*>(res.borrowData());
2460 if (tmp_2==0) { throw DataException("GTP Programming error - casting to DataTagged."); }
2461
2462 // // Get the views
2463 // DataArrayView view_0 = tmp_0->getDefaultValue();
2464 // DataArrayView view_1 = tmp_1->getDefaultValue();
2465 // DataArrayView view_2 = tmp_2->getDefaultValue();
2466 // // Get the pointers to the actual data
2467 // double *ptr_0 = &((view_0.getData())[0]);
2468 // double *ptr_1 = &((view_1.getData())[0]);
2469 // double *ptr_2 = &((view_2.getData())[0]);
2470
2471 double *ptr_0 = &(tmp_0->getDefaultValue(0));
2472 double *ptr_1 = &(tmp_1->getDefaultValue(0));
2473 double *ptr_2 = &(tmp_2->getDefaultValue(0));
2474
2475
2476 // Compute an MVP for the default
2477 matrix_matrix_product(SL, SM, SR, ptr_0, ptr_1, ptr_2, transpose);
2478 // Merge the tags
2479 DataTagged::DataMapType::const_iterator i; // i->first is a tag, i->second is an offset into memory
2480 const DataTagged::DataMapType& lookup_0=tmp_0->getTagLookup();
2481 const DataTagged::DataMapType& lookup_1=tmp_1->getTagLookup();
2482 for (i=lookup_0.begin();i!=lookup_0.end();i++) {
2483 tmp_2->addTag(i->first); // use tmp_2 to get correct shape
2484 }
2485 for (i=lookup_1.begin();i!=lookup_1.end();i++) {
2486 tmp_2->addTag(i->first);
2487 }
2488 // Compute an MVP for each tag
2489 const DataTagged::DataMapType& lookup_2=tmp_2->getTagLookup();
2490 for (i=lookup_2.begin();i!=lookup_2.end();i++) {
2491 // DataArrayView view_0 = tmp_0->getDataPointByTag(i->first);
2492 // DataArrayView view_1 = tmp_1->getDataPointByTag(i->first);
2493 // DataArrayView view_2 = tmp_2->getDataPointByTag(i->first);
2494 // double *ptr_0 = &view_0.getData(0);
2495 // double *ptr_1 = &view_1.getData(0);
2496 // double *ptr_2 = &view_2.getData(0);
2497
2498 double *ptr_0 = &(tmp_0->getDataByTag(i->first,0));
2499 double *ptr_1 = &(tmp_1->getDataByTag(i->first,0));
2500 double *ptr_2 = &(tmp_2->getDataByTag(i->first,0));
2501
2502 matrix_matrix_product(SL, SM, SR, ptr_0, ptr_1, ptr_2, transpose);
2503 }
2504
2505 }
2506 else if (arg_0_Z.isTagged() && arg_1_Z.isExpanded()) {
2507
2508 // After finding a common function space above the two inputs have the same numSamples and num DPPS
2509 res = Data(0.0, shape2, arg_1_Z.getFunctionSpace(),true); // DataExpanded output
2510 DataTagged* tmp_0=dynamic_cast<DataTagged*>(arg_0_Z.borrowData());
2511 DataExpanded* tmp_1=dynamic_cast<DataExpanded*>(arg_1_Z.borrowData());
2512 DataExpanded* tmp_2=dynamic_cast<DataExpanded*>(res.borrowData());
2513 if (tmp_0==0) { throw DataException("GTP Programming error - casting to DataTagged."); }
2514 if (tmp_1==0) { throw DataException("GTP Programming error - casting to DataExpanded."); }
2515 if (tmp_2==0) { throw DataException("GTP Programming error - casting to DataExpanded."); }
2516 int sampleNo_0,dataPointNo_0;
2517 int numSamples_0 = arg_0_Z.getNumSamples();
2518 int numDataPointsPerSample_0 = arg_0_Z.getNumDataPointsPerSample();
2519 #pragma omp parallel for private(sampleNo_0,dataPointNo_0) schedule(static)
2520 for (sampleNo_0 = 0; sampleNo_0 < numSamples_0; sampleNo_0++) {
2521 int offset_0 = tmp_0->getPointOffset(sampleNo_0,0); // They're all the same, so just use #0
2522 double *ptr_0 = &(arg_0_Z.getDataAtOffset(offset_0));
2523 for (dataPointNo_0 = 0; dataPointNo_0 < numDataPointsPerSample_0; dataPointNo_0++) {
2524 int offset_1 = tmp_1->getPointOffset(sampleNo_0,dataPointNo_0);
2525 int offset_2 = tmp_2->getPointOffset(sampleNo_0,dataPointNo_0);
2526 double *ptr_1 = &(arg_1_Z.getDataAtOffset(offset_1));
2527 double *ptr_2 = &(res.getDataAtOffset(offset_2));
2528 matrix_matrix_product(SL, SM, SR, ptr_0, ptr_1, ptr_2, transpose);
2529 }
2530 }
2531
2532 }
2533 else if (arg_0_Z.isExpanded() && arg_1_Z.isConstant()) {
2534
2535 res = Data(0.0, shape2, arg_1_Z.getFunctionSpace(),true); // DataExpanded output
2536 DataExpanded* tmp_0=dynamic_cast<DataExpanded*>(arg_0_Z.borrowData());
2537 DataConstant* tmp_1=dynamic_cast<DataConstant*>(arg_1_Z.borrowData());
2538 DataExpanded* tmp_2=dynamic_cast<DataExpanded*>(res.borrowData());
2539 if (tmp_0==0) { throw DataException("GTP Programming error - casting to DataExpanded."); }
2540 if (tmp_1==0) { throw DataException("GTP Programming error - casting to DataConstant."); }
2541 if (tmp_2==0) { throw DataException("GTP Programming error - casting to DataExpanded."); }
2542 int sampleNo_0,dataPointNo_0;
2543 int numSamples_0 = arg_0_Z.getNumSamples();
2544 int numDataPointsPerSample_0 = arg_0_Z.getNumDataPointsPerSample();
2545 int offset_1 = tmp_1->getPointOffset(0,0);
2546 #pragma omp parallel for private(sampleNo_0,dataPointNo_0) schedule(static)
2547 for (sampleNo_0 = 0; sampleNo_0 < numSamples_0; sampleNo_0++) {
2548 for (dataPointNo_0 = 0; dataPointNo_0 < numDataPointsPerSample_0; dataPointNo_0++) {
2549 int offset_0 = tmp_0->getPointOffset(sampleNo_0,dataPointNo_0);
2550 int offset_2 = tmp_2->getPointOffset(sampleNo_0,dataPointNo_0);
2551 double *ptr_0 = &(arg_0_Z.getDataAtOffset(offset_0));
2552 double *ptr_1 = &(arg_1_Z.getDataAtOffset(offset_1));
2553 double *ptr_2 = &(res.getDataAtOffset(offset_2));
2554 matrix_matrix_product(SL, SM, SR, ptr_0, ptr_1, ptr_2, transpose);
2555 }
2556 }
2557
2558
2559 }
2560 else if (arg_0_Z.isExpanded() && arg_1_Z.isTagged()) {
2561
2562 // After finding a common function space above the two inputs have the same numSamples and num DPPS
2563 res = Data(0.0, shape2, arg_1_Z.getFunctionSpace(),true); // DataExpanded output
2564 DataExpanded* tmp_0=dynamic_cast<DataExpanded*>(arg_0_Z.borrowData());
2565 DataTagged* tmp_1=dynamic_cast<DataTagged*>(arg_1_Z.borrowData());
2566 DataExpanded* tmp_2=dynamic_cast<DataExpanded*>(res.borrowData());
2567 if (tmp_0==0) { throw DataException("GTP Programming error - casting to DataExpanded."); }
2568 if (tmp_1==0) { throw DataException("GTP Programming error - casting to DataTagged."); }
2569 if (tmp_2==0) { throw DataException("GTP Programming error - casting to DataExpanded."); }
2570 int sampleNo_0,dataPointNo_0;
2571 int numSamples_0 = arg_0_Z.getNumSamples();
2572 int numDataPointsPerSample_0 = arg_0_Z.getNumDataPointsPerSample();
2573 #pragma omp parallel for private(sampleNo_0,dataPointNo_0) schedule(static)
2574 for (sampleNo_0 = 0; sampleNo_0 < numSamples_0; sampleNo_0++) {
2575 int offset_1 = tmp_1->getPointOffset(sampleNo_0,0);
2576 double *ptr_1 = &(arg_1_Z.getDataAtOffset(offset_1));
2577 for (dataPointNo_0 = 0; dataPointNo_0 < numDataPointsPerSample_0; dataPointNo_0++) {
2578 int offset_0 = tmp_0->getPointOffset(sampleNo_0,dataPointNo_0);
2579 int offset_2 = tmp_2->getPointOffset(sampleNo_0,dataPointNo_0);
2580 double *ptr_0 = &(arg_0_Z.getDataAtOffset(offset_0));
2581 double *ptr_2 = &(res.getDataAtOffset(offset_2));
2582 matrix_matrix_product(SL, SM, SR, ptr_0, ptr_1, ptr_2, transpose);
2583 }
2584 }
2585
2586 }
2587 else if (arg_0_Z.isExpanded() && arg_1_Z.isExpanded()) {
2588
2589 // After finding a common function space above the two inputs have the same numSamples and num DPPS
2590 res = Data(0.0, shape2, arg_1_Z.getFunctionSpace(),true); // DataExpanded output
2591 DataExpanded* tmp_0=dynamic_cast<DataExpanded*>(arg_0_Z.borrowData());
2592 DataExpanded* tmp_1=dynamic_cast<DataExpanded*>(arg_1_Z.borrowData());
2593 DataExpanded* tmp_2=dynamic_cast<DataExpanded*>(res.borrowData());
2594 if (tmp_0==0) { throw DataException("GTP Programming error - casting to DataExpanded."); }
2595 if (tmp_1==0) { throw DataException("GTP Programming error - casting to DataExpanded."); }
2596 if (tmp_2==0) { throw DataException("GTP Programming error - casting to DataExpanded."); }
2597 int sampleNo_0,dataPointNo_0;
2598 int numSamples_0 = arg_0_Z.getNumSamples();
2599 int numDataPointsPerSample_0 = arg_0_Z.getNumDataPointsPerSample();
2600 #pragma omp parallel for private(sampleNo_0,dataPointNo_0) schedule(static)
2601 for (sampleNo_0 = 0; sampleNo_0 < numSamples_0; sampleNo_0++) {
2602 for (dataPointNo_0 = 0; dataPointNo_0 < numDataPointsPerSample_0; dataPointNo_0++) {
2603 int offset_0 = tmp_0->getPointOffset(sampleNo_0,dataPointNo_0);
2604 int offset_1 = tmp_1->getPointOffset(sampleNo_0,dataPointNo_0);
2605 int offset_2 = tmp_2->getPointOffset(sampleNo_0,dataPointNo_0);
2606 double *ptr_0 = &(arg_0_Z.getDataAtOffset(offset_0));
2607 double *ptr_1 = &(arg_1_Z.getDataAtOffset(offset_1));
2608 double *ptr_2 = &(res.getDataAtOffset(offset_2));
2609 matrix_matrix_product(SL, SM, SR, ptr_0, ptr_1, ptr_2, transpose);
2610 }
2611 }
2612
2613 }
2614 else {
2615 throw DataException("Error - C_GeneralTensorProduct: unknown combination of inputs");
2616 }
2617
2618 return res;
2619 }
2620
2621 DataAbstract*
2622 Data::borrowData() const
2623 {
2624 return m_data.get();
2625 }
2626
2627 // Not all that happy about returning a non-const from a const
2628 DataAbstract_ptr
2629 Data::borrowDataPtr() const
2630 {
2631 return m_data;
2632 }
2633
2634 // Not all that happy about returning a non-const from a const
2635 DataReady_ptr
2636 Data::borrowReadyPtr() const
2637 {
2638 DataReady_ptr dr=dynamic_pointer_cast<DataReady>(m_data);
2639 EsysAssert((dr!=0), "Error - casting to DataReady.");
2640 return dr;
2641 }
2642
2643 std::string
2644 Data::toString() const
2645 {
2646 static const DataTypes::ValueType::size_type TOO_MANY_POINTS=80;
2647 if (isLazy())
2648 { // This needs to change back to printing out something useful once the summary ops
2649 return m_data->toString(); // are defined
2650 }
2651 if (getNumDataPoints()*getDataPointSize()>TOO_MANY_POINTS)
2652 {
2653 stringstream temp;
2654 temp << "Summary: inf="<< inf_const() << " sup=" << sup_const() << " data points=" << getNumDataPoints();
2655 return temp.str();
2656 }
2657 return m_data->toString();
2658 }
2659
2660
2661
2662 DataTypes::ValueType::const_reference
2663 Data::getDataAtOffset(DataTypes::ValueType::size_type i) const
2664 {
2665 if (isLazy())
2666 {
2667 throw DataException("getDataAtOffset not permitted on lazy data.");
2668 }
2669 return getReady()->getDataAtOffset(i);
2670 }
2671
2672
2673 DataTypes::ValueType::reference
2674 Data::getDataAtOffset(DataTypes::ValueType::size_type i)
2675 {
2676 if (isLazy())
2677 {
2678 throw DataException("getDataAtOffset not permitted on lazy data.");
2679 }
2680 return getReady()->getDataAtOffset(i);
2681 }
2682
2683 DataTypes::ValueType::const_reference
2684 Data::getDataPoint(int sampleNo, int dataPointNo) const
2685 {
2686 if (!isReady())
2687 {
2688 throw DataException("Programmer error - getDataPoint() not permitted on Lazy Data.");
2689 }
2690 else
2691 {
2692 const DataReady* dr=getReady();
2693 return dr->getDataAtOffset(dr->getPointOffset(sampleNo, dataPointNo));
2694 }
2695 }
2696
2697
2698 DataTypes::ValueType::reference
2699 Data::getDataPoint(int sampleNo, int dataPointNo)
2700 {
2701 if (!isReady())
2702 {
2703 throw DataException("Programmer error - getDataPoint() not permitted on Lazy Data.");
2704 }
2705 else
2706 {
2707 DataReady* dr=getReady();
2708 return dr->getDataAtOffset(dr->getPointOffset(sampleNo, dataPointNo));
2709 }
2710 }
2711
2712
2713 /* Member functions specific to the MPI implementation */
2714
2715 void
2716 Data::print()
2717 {
2718 int i,j;
2719
2720 printf( "Data is %dX%d\n", getNumSamples(), getNumDataPointsPerSample() );
2721 for( i=0; i<getNumSamples(); i++ )
2722 {
2723 printf( "[%6d]", i );
2724 for( j=0; j<getNumDataPointsPerSample(); j++ )
2725 printf( "\t%10.7g", (getSampleData(i))[j] );
2726 printf( "\n" );
2727 }
2728 }
2729 void
2730 Data::dump(const std::string fileName) const
2731 {
2732 try
2733 {
2734 if (isLazy())
2735 {
2736 Data temp(*this); // this is to get a non-const object which we can resolve
2737 temp.resolve();
2738 temp.dump(fileName);
2739 }
2740 else
2741 {
2742 return m_data->dump(fileName);
2743 }
2744 }
2745 catch (exception& e)
2746 {
2747 cout << e.what() << endl;
2748 }
2749 }
2750
2751 int
2752 Data::get_MPISize() const
2753 {
2754 int size;
2755 #ifdef PASO_MPI
2756 int error;
2757 error = MPI_Comm_size( get_MPIComm(), &size );
2758 #else
2759 size = 1;
2760 #endif
2761 return size;
2762 }
2763
2764 int
2765 Data::get_MPIRank() const
2766 {
2767 int rank;
2768 #ifdef PASO_MPI
2769 int error;
2770 error = MPI_Comm_rank( get_MPIComm(), &rank );
2771 #else
2772 rank = 0;
2773 #endif
2774 return rank;
2775 }
2776
2777 MPI_Comm
2778 Data::get_MPIComm() const
2779 {
2780 #ifdef PASO_MPI
2781 return MPI_COMM_WORLD;
2782 #else
2783 return -1;
2784 #endif
2785 }
2786
2787

Properties

Name Value
svn:eol-style native
svn:keywords Author Date Id Revision

  ViewVC Help
Powered by ViewVC 1.1.26