/[escript]/trunk/escript/src/Data.h
ViewVC logotype

Diff of /trunk/escript/src/Data.h

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 757 by woo409, Mon Jun 26 13:12:56 2006 UTC revision 2458 by jfenwick, Wed Jun 3 06:18:21 2009 UTC
# Line 1  Line 1 
1  // $Id$  
2  /*  /*******************************************************
3   ************************************************************  *
4   *          Copyright 2006 by ACcESS MNRF                   *  * Copyright (c) 2003-2008 by University of Queensland
5   *                                                          *  * Earth Systems Science Computational Center (ESSCC)
6   *              http://www.access.edu.au                    *  * http://www.uq.edu.au/esscc
7   *       Primary Business: Queensland, Australia            *  *
8   *  Licensed under the Open Software License version 3.0    *  * Primary Business: Queensland, Australia
9   *     http://www.opensource.org/licenses/osl-3.0.php       *  * Licensed under the Open Software License version 3.0
10   *                                                          *  * http://www.opensource.org/licenses/osl-3.0.php
11   ************************************************************  *
12  */  *******************************************************/
13    
14    
15  /** \file Data.h */  /** \file Data.h */
16    
# Line 17  Line 18 
18  #define DATA_H  #define DATA_H
19  #include "system_dep.h"  #include "system_dep.h"
20    
21    #include "DataTypes.h"
22  #include "DataAbstract.h"  #include "DataAbstract.h"
23  #include "DataAlgorithm.h"  #include "DataAlgorithm.h"
24  #include "FunctionSpace.h"  #include "FunctionSpace.h"
# Line 24  Line 26 
26  #include "UnaryOp.h"  #include "UnaryOp.h"
27  #include "DataException.h"  #include "DataException.h"
28    
29    
30  extern "C" {  extern "C" {
31  #include "DataC.h"  #include "DataC.h"
32  #include "paso/Paso.h"  //#include <omp.h>
33  }  }
34    
35    #include "esysmpi.h"
36  #include <string>  #include <string>
37  #include <algorithm>  #include <algorithm>
38    #include <sstream>
39    
40  #include <boost/shared_ptr.hpp>  #include <boost/shared_ptr.hpp>
41  #include <boost/python/object.hpp>  #include <boost/python/object.hpp>
42  #include <boost/python/tuple.hpp>  #include <boost/python/tuple.hpp>
43  #include <boost/python/numeric.hpp>  
44    #include "BufferGroup.h"
45    
46  namespace escript {  namespace escript {
47    
# Line 44  namespace escript { Line 50  namespace escript {
50  class DataConstant;  class DataConstant;
51  class DataTagged;  class DataTagged;
52  class DataExpanded;  class DataExpanded;
53    class DataLazy;
54    
55  /**  /**
56     \brief     \brief
57     Data creates the appropriate Data object for the given construction     Data represents a collection of datapoints.
    arguments.  
58    
59     Description:     Description:
60     Data is essentially a factory class which creates the appropriate Data     Internally, the datapoints are actually stored by a DataAbstract object.
61     object for the given construction arguments. It retains control over     The specific instance of DataAbstract used may vary over the lifetime
62     the object created for the lifetime of the object.     of the Data object.
63     The type of Data object referred to may change during the lifetime of     Some methods on this class return references (eg getShape()).
64     the Data object.     These references should not be used after an operation which changes the underlying DataAbstract object.
65       Doing so will lead to invalid memory access.
66       This should not affect any methods exposed via boost::python.
67  */  */
68  class Data {  class Data {
69    
# Line 66  class Data { Line 74  class Data {
74    typedef double (*UnaryDFunPtr)(double);    typedef double (*UnaryDFunPtr)(double);
75    typedef double (*BinaryDFunPtr)(double,double);    typedef double (*BinaryDFunPtr)(double,double);
76    
77    
78    /**    /**
79       Constructors.       Constructors.
80    */    */
# Line 97  class Data { Line 106  class Data {
106         const FunctionSpace& what);         const FunctionSpace& what);
107    
108    /**    /**
109       \brief      \brief Copy Data from an existing vector
110       Constructor which copies data from a DataArrayView.    */
111    
      \param value - Input - Data value for a single point.  
      \param what - Input - A description of what this data represents.  
      \param expanded - Input - Flag, if true fill the entire container with  
                        the value. Otherwise a more efficient storage  
                        mechanism will be used.  
   */  
112    ESCRIPT_DLL_API    ESCRIPT_DLL_API
113    Data(const DataArrayView& value,    Data(const DataTypes::ValueType& value,
114         const FunctionSpace& what=FunctionSpace(),           const DataTypes::ShapeType& shape,
115         bool expanded=false);                   const FunctionSpace& what=FunctionSpace(),
116                     bool expanded=false);
117    
118    /**    /**
119       \brief       \brief
# Line 124  class Data { Line 128  class Data {
128    */    */
129    ESCRIPT_DLL_API    ESCRIPT_DLL_API
130    Data(double value,    Data(double value,
131         const DataArrayView::ShapeType& dataPointShape=DataArrayView::ShapeType(),         const DataTypes::ShapeType& dataPointShape=DataTypes::ShapeType(),
132         const FunctionSpace& what=FunctionSpace(),         const FunctionSpace& what=FunctionSpace(),
133         bool expanded=false);         bool expanded=false);
134    
# Line 137  class Data { Line 141  class Data {
141    */    */
142    ESCRIPT_DLL_API    ESCRIPT_DLL_API
143    Data(const Data& inData,    Data(const Data& inData,
144         const DataArrayView::RegionType& region);         const DataTypes::RegionType& region);
   
   /**  
      \brief  
      Constructor which will create Tagged data if expanded is false.  
      No attempt is made to ensure the tag keys match the tag keys  
      within the function space.  
   
      \param tagKeys - Input - List of tag values.  
      \param values - Input - List of values, one for each tag.  
      \param defaultValue - Input - A default value, used if tag doesn't exist.  
      \param what - Input - A description of what this data represents.  
      \param expanded - Input - Flag, if true fill the entire container with  
                        the appropriate values.  
     ==>*  
   */  
   ESCRIPT_DLL_API  
   Data(const DataTagged::TagListType& tagKeys,  
        const DataTagged::ValueListType& values,  
        const DataArrayView& defaultValue,  
        const FunctionSpace& what=FunctionSpace(),  
        bool expanded=false);  
   
   /**  
      \brief  
      Constructor which copies data from a python numarray.  
   
      \param value - Input - Data value for a single point.  
      \param what - Input - A description of what this data represents.  
      \param expanded - Input - Flag, if true fill the entire container with  
                        the value. Otherwise a more efficient storage  
                        mechanism will be used.  
   */  
   ESCRIPT_DLL_API  
   Data(const boost::python::numeric::array& value,  
        const FunctionSpace& what=FunctionSpace(),  
        bool expanded=false);  
145    
146    /**    /**
147       \brief       \brief
148       Constructor which copies data from any object that can be converted into       Constructor which copies data from any object that can be treated like a python array/sequence.
      a python numarray.  
149    
150       \param value - Input - Input data.       \param value - Input - Input data.
151       \param what - Input - A description of what this data represents.       \param what - Input - A description of what this data represents.
# Line 194  class Data { Line 161  class Data {
161    /**    /**
162       \brief       \brief
163       Constructor which creates a DataConstant.       Constructor which creates a DataConstant.
164       Copies data from any object that can be converted       Copies data from any object that can be treated like a python array/sequence.
165       into a numarray. All other parameters are copied from other.       All other parameters are copied from other.
166    
167       \param value - Input - Input data.       \param value - Input - Input data.
168       \param other - Input - contains all other parameters.       \param other - Input - contains all other parameters.
# Line 209  class Data { Line 176  class Data {
176       Constructor which creates a DataConstant of "shape" with constant value.       Constructor which creates a DataConstant of "shape" with constant value.
177    */    */
178    ESCRIPT_DLL_API    ESCRIPT_DLL_API
179    Data(double value,    Data(double value,
180         const boost::python::tuple& shape=boost::python::make_tuple(),         const boost::python::tuple& shape=boost::python::make_tuple(),
181         const FunctionSpace& what=FunctionSpace(),         const FunctionSpace& what=FunctionSpace(),
182         bool expanded=false);         bool expanded=false);
183    
184    
185    
186      /**
187        \brief Create a Data using an existing DataAbstract. Warning: The new object assumes ownership of the pointer!
188        Once you have passed the pointer, do not delete it.
189      */
190      ESCRIPT_DLL_API
191      explicit Data(DataAbstract* underlyingdata);
192    
193      /**
194        \brief Create a Data based on the supplied DataAbstract
195      */
196      ESCRIPT_DLL_API
197      explicit Data(DataAbstract_ptr underlyingdata);
198    
199    /**    /**
200       \brief       \brief
201       Destructor       Destructor
# Line 221  class Data { Line 204  class Data {
204    ~Data();    ~Data();
205    
206    /**    /**
207       \brief       \brief Make this object a deep copy of "other".
      Perform a deep copy.  
208    */    */
209    ESCRIPT_DLL_API    ESCRIPT_DLL_API
210    void    void
211    copy(const Data& other);    copy(const Data& other);
212    
213    /**    /**
214         \brief Return a pointer to a deep copy of this object.
215      */
216      ESCRIPT_DLL_API
217      Data
218      copySelf();
219    
220    
221      /**
222         \brief produce a delayed evaluation version of this Data.
223      */
224      ESCRIPT_DLL_API
225      Data
226      delay();
227    
228      /**
229         \brief convert the current data into lazy data.
230      */
231      ESCRIPT_DLL_API
232      void
233      delaySelf();
234    
235    
236      /**
237       Member access methods.       Member access methods.
238    */    */
239    
240    /**    /**
241       \brief       \brief
242       Return the values of all data-points as a single python numarray object.       switches on update protection
243    
244    */    */
245    ESCRIPT_DLL_API    ESCRIPT_DLL_API
246    const boost::python::numeric::array    void
247    convertToNumArray();    setProtection();
248    
249    /**    /**
250       \brief       \brief
251       Return the values of all data-points for the given sample as a single python numarray object.       Returns trueif the data object is protected against update
252    
253    */    */
254    ESCRIPT_DLL_API    ESCRIPT_DLL_API
255    const boost::python::numeric::array    bool
256    convertToNumArrayFromSampleNo(int sampleNo);    isProtected() const;
257    
258    
259    /**
260       \brief
261       Return teh value of a data point as a python tuple.
262    */
263      ESCRIPT_DLL_API
264      const boost::python::object
265      getValueOfDataPointAsTuple(int dataPointNo);
266    
267    /**    /**
268       \brief       \brief
269       Return the value of the specified data-point as a single python numarray object.       sets the values of a data-point from a python object on this process
270    */    */
271    ESCRIPT_DLL_API    ESCRIPT_DLL_API
272    const boost::python::numeric::array    void
273    convertToNumArrayFromDPNo(int sampleNo,    setValueOfDataPointToPyObject(int dataPointNo, const boost::python::object& py_object);
                             int dataPointNo);  
274    
275    /**    /**
276       \brief       \brief
277       Fills the expanded Data object from values of a python numarray object.       sets the values of a data-point from a array-like object on this process
278    */    */
279    ESCRIPT_DLL_API    ESCRIPT_DLL_API
280    void    void
281    fillFromNumArray(const boost::python::numeric::array);    setValueOfDataPointToArray(int dataPointNo, const boost::python::object&);
282    
283      /**
284         \brief
285         sets the values of a data-point on this process
286      */
287      ESCRIPT_DLL_API
288      void
289      setValueOfDataPoint(int dataPointNo, const double);
290    
291      /**
292         \brief Return a data point across all processors as a python tuple.
293      */
294      ESCRIPT_DLL_API
295      const boost::python::object
296      getValueOfGlobalDataPointAsTuple(int procNo, int dataPointNo);
297    
298    /**    /**
299       \brief       \brief
300       Return the tag number associated with the given data-point.       Return the tag number associated with the given data-point.
301    
      The data-point number here corresponds to the data-point number in the  
      numarray returned by convertToNumArray.  
302    */    */
303    ESCRIPT_DLL_API    ESCRIPT_DLL_API
304    int    int
# Line 284  class Data { Line 312  class Data {
312    escriptDataC    escriptDataC
313    getDataC();    getDataC();
314    
315    
316    
317    /**    /**
318       \brief       \brief
319       Return the C wrapper for the Data object - const version.       Return the C wrapper for the Data object - const version.
# Line 293  class Data { Line 323  class Data {
323    getDataC() const;    getDataC() const;
324    
325    /**    /**
326       \brief      \brief How much space is required to evaulate a sample of the Data.
      Write the data as a string.  
327    */    */
328    ESCRIPT_DLL_API    ESCRIPT_DLL_API
329    inline    size_t
330    std::string    getSampleBufferSize() const;
331    toString() const  
332    {  
     return m_data->toString();  
   }  
333    
334    /**    /**
335       \brief       \brief
336       Return the DataArrayView of the point data. This essentially contains       Write the data as a string. For large amounts of data, a summary is printed.
      the shape information for each data point although it also may be used  
      to manipulate the point data.  
337    */    */
338    ESCRIPT_DLL_API    ESCRIPT_DLL_API
339    inline    std::string
340    const DataArrayView&    toString() const;
   getPointDataView() const  
   {  
      return m_data->getPointDataView();  
   }  
341    
342    /**    /**
343       \brief       \brief
# Line 331  class Data { Line 352  class Data {
352       If possible convert this Data to DataTagged. This will only allow       If possible convert this Data to DataTagged. This will only allow
353       Constant data to be converted to tagged. An attempt to convert       Constant data to be converted to tagged. An attempt to convert
354       Expanded data to tagged will throw an exception.       Expanded data to tagged will throw an exception.
     ==>*  
355    */    */
356    ESCRIPT_DLL_API    ESCRIPT_DLL_API
357    void    void
358    tag();    tag();
359    
360    /**    /**
361        \brief If this data is lazy, then convert it to ready data.
362        What type of ready data depends on the expression. For example, Constant+Tagged==Tagged.
363      */
364      ESCRIPT_DLL_API
365      void
366      resolve();
367    
368    
369      /**
370       \brief Ensures data is ready for write access.
371      This means that the data will be resolved if lazy and will be copied if shared with another Data object.
372      \warning This method should only be called in single threaded sections of code. (It modifies m_data).
373      Do not create any Data objects from this one between calling requireWrite and getSampleDataRW.
374      Doing so might introduce additional sharing.
375      */
376      ESCRIPT_DLL_API
377      void
378      requireWrite();
379    
380      /**
381       \brief       \brief
382       Return true if this Data is expanded.       Return true if this Data is expanded.
383         \note To determine if a sample will contain separate values for each datapoint. Use actsExpanded instead.
384    */    */
385    ESCRIPT_DLL_API    ESCRIPT_DLL_API
386    bool    bool
# Line 347  class Data { Line 388  class Data {
388    
389    /**    /**
390       \brief       \brief
391         Return true if this Data is expanded or resolves to expanded.
392         That is, if it has a separate value for each datapoint in the sample.
393      */
394      ESCRIPT_DLL_API
395      bool
396      actsExpanded() const;
397      
398    
399      /**
400         \brief
401       Return true if this Data is tagged.       Return true if this Data is tagged.
402    */    */
403    ESCRIPT_DLL_API    ESCRIPT_DLL_API
# Line 362  class Data { Line 413  class Data {
413    isConstant() const;    isConstant() const;
414    
415    /**    /**
416         \brief Return true if this Data is lazy.
417      */
418      ESCRIPT_DLL_API
419      bool
420      isLazy() const;
421    
422      /**
423         \brief Return true if this data is ready.
424      */
425      ESCRIPT_DLL_API
426      bool
427      isReady() const;
428    
429      /**
430       \brief       \brief
431       Return true if this Data is empty.       Return true if this Data holds an instance of DataEmpty. This is _not_ the same as asking if the object
432    contains datapoints.
433    */    */
434    ESCRIPT_DLL_API    ESCRIPT_DLL_API
435    bool    bool
# Line 395  class Data { Line 461  class Data {
461    */    */
462    ESCRIPT_DLL_API    ESCRIPT_DLL_API
463    inline    inline
464    const AbstractDomain&  //   const AbstractDomain&
465      const_Domain_ptr
466    getDomain() const    getDomain() const
467    {    {
468       return getFunctionSpace().getDomain();       return getFunctionSpace().getDomain();
469    }    }
470    
471    
472      /**
473         \brief
474         Return the domain.
475         TODO: For internal use only.   This should be removed.
476      */
477      ESCRIPT_DLL_API
478      inline
479    //   const AbstractDomain&
480      Domain_ptr
481      getDomainPython() const
482      {
483         return getFunctionSpace().getDomainPython();
484      }
485    
486    /**    /**
487       \brief       \brief
488       Return a copy of the domain.       Return a copy of the domain.
# Line 415  class Data { Line 497  class Data {
497    */    */
498    ESCRIPT_DLL_API    ESCRIPT_DLL_API
499    inline    inline
500    int    unsigned int
501    getDataPointRank() const    getDataPointRank() const
502    {    {
503      return m_data->getPointDataView().getRank();      return m_data->getRank();
504    }    }
505    
506    /**    /**
507       \brief       \brief
508         Return the number of data points
509      */
510      ESCRIPT_DLL_API
511      inline
512      int
513      getNumDataPoints() const
514      {
515        return getNumSamples() * getNumDataPointsPerSample();
516      }
517      /**
518         \brief
519       Return the number of samples.       Return the number of samples.
520    */    */
521    ESCRIPT_DLL_API    ESCRIPT_DLL_API
# Line 445  class Data { Line 538  class Data {
538      return m_data->getNumDPPSample();      return m_data->getNumDPPSample();
539    }    }
540    
541    
542      /**
543        \brief
544        Return the number of values in the shape for this object.
545      */
546      ESCRIPT_DLL_API
547      int
548      getNoValues() const
549      {
550        return m_data->getNoValues();
551      }
552    
553    
554      /**
555         \brief
556         dumps the object into a netCDF file
557      */
558      ESCRIPT_DLL_API
559      void
560      dump(const std::string fileName) const;
561    
562    //  /**
563    //     \brief
564    //     Return the sample data for the given sample no. This is not the
565    //     preferred interface but is provided for use by C code.
566    //     The buffer parameter is only required for LazyData.
567    //     \param sampleNo - Input - the given sample no.
568    //     \param buffer - Vector to compute (and store) sample data in.
569    //     \return pointer to the sample data.
570    //*/
571    //   ESCRIPT_DLL_API
572    //   inline
573    //   const DataAbstract::ValueType::value_type*
574    //   getSampleDataRO(DataAbstract::ValueType::size_type sampleNo, DataTypes::ValueType* buffer=0);
575    
576    
577     /**
578        \brief
579        Return the sample data for the given sample no. This is not the
580        preferred interface but is provided for use by C code.
581        The bufferg parameter is only required for LazyData.
582        \param sampleNo - Input - the given sample no.
583        \param buffer - A buffer to to compute (and store) sample data in will be selected from this group.
584        \return pointer to the sample data.
585    */
586      ESCRIPT_DLL_API
587      inline
588      const DataAbstract::ValueType::value_type*
589      getSampleDataRO(DataAbstract::ValueType::size_type sampleNo, BufferGroup* bufferg=0);
590    
591    
592    /**    /**
593       \brief       \brief
594       Return the sample data for the given sample no. This is not the       Return the sample data for the given sample no. This is not the
595       preferred interface but is provided for use by C code.       preferred interface but is provided for use by C code.
596       \param sampleNo - Input - the given sample no.       \param sampleNo - Input - the given sample no.
597         \return pointer to the sample data.
598    */    */
599    ESCRIPT_DLL_API    ESCRIPT_DLL_API
600    inline    inline
601    DataAbstract::ValueType::value_type*    DataAbstract::ValueType::value_type*
602    getSampleData(DataAbstract::ValueType::size_type sampleNo)    getSampleDataRW(DataAbstract::ValueType::size_type sampleNo);
603    {  
     return m_data->getSampleData(sampleNo);  
   }  
604    
605    /**    /**
606       \brief       \brief
# Line 475  class Data { Line 618  class Data {
618    
619    /**    /**
620       \brief       \brief
621       Assign the given value to the data-points referenced by the given       Return a view into the data for the data point specified.
622       reference number.       NOTE: Construction of the DataArrayView is a relatively expensive
623         operation.
624       The value supplied is a python numarray object.  The data from this numarray       \param sampleNo - Input -
625       is unpacked into a DataArray, and this is used to set the corresponding       \param dataPointNo - Input -
      data-points in the underlying Data object.  
   
      If the underlying Data object cannot be accessed via reference numbers, an  
      exception will be thrown.  
   
      \param ref - Input - reference number.  
      \param value - Input - value to assign to data-points associated with  
                             the given reference number.  
626    */    */
627    ESCRIPT_DLL_API    ESCRIPT_DLL_API
628    void    DataTypes::ValueType::const_reference
629    setRefValue(int ref,    getDataPointRO(int sampleNo, int dataPointNo);
               const boost::python::numeric::array& value);  
630    
   /**  
      \brief  
      Return the values associated with the data-points referenced by the given  
      reference number.  
631    
632       The value supplied is a python numarray object. The data from the corresponding    ESCRIPT_DLL_API
633       data-points in this Data object are packed into the given numarray object.    DataTypes::ValueType::reference
634      getDataPointRW(int sampleNo, int dataPointNo);
635    
      If the underlying Data object cannot be accessed via reference numbers, an  
      exception will be thrown.  
636    
      \param ref - Input - reference number.  
      \param value - Output - object to receive values from data-points  
                              associated with the given reference number.  
   */  
   ESCRIPT_DLL_API  
   void  
   getRefValue(int ref,  
               boost::python::numeric::array& value);  
637    
638    /**    /**
639       \brief       \brief
640       Return a view into the data for the data point specified.       Return the offset for the given sample and point within the sample
      NOTE: Construction of the DataArrayView is a relatively expensive  
      operation.  
      \param sampleNo - Input -  
      \param dataPointNo - Input -  
641    */    */
642    ESCRIPT_DLL_API    ESCRIPT_DLL_API
643    inline    inline
644    DataArrayView    DataTypes::ValueType::size_type
645    getDataPoint(int sampleNo,    getDataOffset(int sampleNo,
646                 int dataPointNo)                 int dataPointNo)
647    {    {
648      return m_data->getDataPoint(sampleNo,dataPointNo);        return m_data->getPointOffset(sampleNo,dataPointNo);
649    }    }
650    
651    /**    /**
# Line 536  class Data { Line 653  class Data {
653       Return a reference to the data point shape.       Return a reference to the data point shape.
654    */    */
655    ESCRIPT_DLL_API    ESCRIPT_DLL_API
656    const DataArrayView::ShapeType&    inline
657    getDataPointShape() const;    const DataTypes::ShapeType&
658      getDataPointShape() const
659      {
660        return m_data->getShape();
661      }
662    
663    /**    /**
664       \brief       \brief
# Line 561  class Data { Line 682  class Data {
682       Return the number of doubles stored for this Data.       Return the number of doubles stored for this Data.
683    */    */
684    ESCRIPT_DLL_API    ESCRIPT_DLL_API
685    DataArrayView::ValueType::size_type    DataTypes::ValueType::size_type
686    getLength() const;    getLength() const;
687    
688    
689    
690    /**    /**
691       \brief       \brief
692       Assign the given value to the tag. Implicitly converts this       Assign the given value to the tag assocciated with name. Implicitly converts this
693       object to type DataTagged. Throws an exception if this object       object to type DataTagged. Throws an exception if this object
694       cannot be converted to a DataTagged object.       cannot be converted to a DataTagged object or name cannot be mapped onto a tag key.
695         \param tagKey - Input - Integer key.
696         \param value - Input - Value to associate with given key.
697        ==>*
698      */
699      ESCRIPT_DLL_API
700      void
701      setTaggedValueByName(std::string name,
702                           const boost::python::object& value);
703    
704      /**
705         \brief
706         Assign the given value to the tag. Implicitly converts this
707         object to type DataTagged if it is constant.
708    
709       \param tagKey - Input - Integer key.       \param tagKey - Input - Integer key.
710       \param value - Input - Value to associate with given key.       \param value - Input - Value to associate with given key.
711      ==>*      ==>*
# Line 581  class Data { Line 718  class Data {
718    /**    /**
719       \brief       \brief
720       Assign the given value to the tag. Implicitly converts this       Assign the given value to the tag. Implicitly converts this
721       object to type DataTagged. Throws an exception if this object       object to type DataTagged if it is constant.
722       cannot be converted to a DataTagged object.  
723       \param tagKey - Input - Integer key.       \param tagKey - Input - Integer key.
724         \param pointshape - Input - The shape of the value parameter
725       \param value - Input - Value to associate with given key.       \param value - Input - Value to associate with given key.
726      ==>*       \param dataOffset - Input - Offset of the begining of the point within the value parameter
727    */    */
728    ESCRIPT_DLL_API    ESCRIPT_DLL_API
729    void    void
730    setTaggedValueFromCPP(int tagKey,    setTaggedValueFromCPP(int tagKey,
731                          const DataArrayView& value);              const DataTypes::ShapeType& pointshape,
732                            const DataTypes::ValueType& value,
733                int dataOffset=0);
734    
735    
736    
737    /**    /**
738      \brief      \brief
# Line 607  class Data { Line 749  class Data {
749    
750    /**    /**
751       \brief       \brief
752         set all values to zero
753         *
754      */
755      ESCRIPT_DLL_API
756      void
757      setToZero();
758    
759      /**
760         \brief
761       Interpolates this onto the given functionspace and returns       Interpolates this onto the given functionspace and returns
762       the result as a Data object.       the result as a Data object.
763       *       *
# Line 614  class Data { Line 765  class Data {
765    ESCRIPT_DLL_API    ESCRIPT_DLL_API
766    Data    Data
767    interpolate(const FunctionSpace& functionspace) const;    interpolate(const FunctionSpace& functionspace) const;
   
768    /**    /**
769       \brief       \brief
770       Calculates the gradient of the data at the data points of functionspace.       Calculates the gradient of the data at the data points of functionspace.
# Line 630  class Data { Line 780  class Data {
780    grad() const;    grad() const;
781    
782    /**    /**
783        \brief
784         Calculate the integral over the function space domain as a python tuple.
785      */
786      ESCRIPT_DLL_API
787      boost::python::object
788      integrateToTuple_const() const;
789    
790    
791      /**
792        \brief
793         Calculate the integral over the function space domain as a python tuple.
794      */
795      ESCRIPT_DLL_API
796      boost::python::object
797      integrateToTuple();
798    
799    
800    
801      /**
802       \brief       \brief
803       Calculate the integral over the function space domain.       Returns 1./ Data object
804       *       *
805    */    */
806    ESCRIPT_DLL_API    ESCRIPT_DLL_API
807    boost::python::numeric::array    Data
808    integrate() const;    oneOver() const;
   
809    /**    /**
810       \brief       \brief
811       Return a Data with a 1 for +ive values and a 0 for 0 or -ive values.       Return a Data with a 1 for +ive values and a 0 for 0 or -ive values.
# Line 695  class Data { Line 863  class Data {
863    /**    /**
864       \brief       \brief
865       Return the maximum absolute value of this Data object.       Return the maximum absolute value of this Data object.
866       *  
867         The method is not const because lazy data needs to be expanded before Lsup can be computed.
868         The _const form can be used when the Data object is const, however this will only work for
869         Data which is not Lazy.
870    
871         For Data which contain no samples (or tagged Data for which no tags in use have a value)
872         zero is returned.
873    */    */
874    ESCRIPT_DLL_API    ESCRIPT_DLL_API
875    double    double
876    Lsup() const;    Lsup();
877    
   /**  
      \brief  
      Return the minimum absolute value of this Data object.  
      *  
   */  
878    ESCRIPT_DLL_API    ESCRIPT_DLL_API
879    double    double
880    Linf() const;    Lsup_const() const;
881    
882    
883    /**    /**
884       \brief       \brief
885       Return the maximum value of this Data object.       Return the maximum value of this Data object.
886       *  
887         The method is not const because lazy data needs to be expanded before sup can be computed.
888         The _const form can be used when the Data object is const, however this will only work for
889         Data which is not Lazy.
890    
891         For Data which contain no samples (or tagged Data for which no tags in use have a value)
892         a large negative value is returned.
893    */    */
894    ESCRIPT_DLL_API    ESCRIPT_DLL_API
895    double    double
896    sup() const;    sup();
897    
898      ESCRIPT_DLL_API
899      double
900      sup_const() const;
901    
902    
903    /**    /**
904       \brief       \brief
905       Return the minimum value of this Data object.       Return the minimum value of this Data object.
906       *  
907         The method is not const because lazy data needs to be expanded before inf can be computed.
908         The _const form can be used when the Data object is const, however this will only work for
909         Data which is not Lazy.
910    
911         For Data which contain no samples (or tagged Data for which no tags in use have a value)
912         a large positive value is returned.
913    */    */
914    ESCRIPT_DLL_API    ESCRIPT_DLL_API
915    double    double
916    inf() const;    inf();
917    
918      ESCRIPT_DLL_API
919      double
920      inf_const() const;
921    
922    
923    
924    /**    /**
925       \brief       \brief
# Line 762  class Data { Line 955  class Data {
955    */    */
956    ESCRIPT_DLL_API    ESCRIPT_DLL_API
957    const boost::python::tuple    const boost::python::tuple
958    mindp() const;    minGlobalDataPoint() const;
959    
960    ESCRIPT_DLL_API    ESCRIPT_DLL_API
961    void    void
962    calc_mindp(int& SampleNo,    calc_minGlobalDataPoint(int& ProcNo,  int& DataPointNo) const;
              int& DataPointNo) const;  
   
963    /**    /**
964       \brief       \brief
965       Return the sign of each data point of this Data object.       Return the sign of each data point of this Data object.
# Line 781  class Data { Line 972  class Data {
972    
973    /**    /**
974       \brief       \brief
975         Return the symmetric part of a matrix which is half the matrix plus its transpose.
976         *
977      */
978      ESCRIPT_DLL_API
979      Data
980      symmetric() const;
981    
982      /**
983         \brief
984         Return the nonsymmetric part of a matrix which is half the matrix minus its transpose.
985         *
986      */
987      ESCRIPT_DLL_API
988      Data
989      nonsymmetric() const;
990    
991      /**
992         \brief
993         Return the trace of a matrix
994         *
995      */
996      ESCRIPT_DLL_API
997      Data
998      trace(int axis_offset) const;
999    
1000      /**
1001         \brief
1002         Transpose each data point of this Data object around the given axis.
1003         *
1004      */
1005      ESCRIPT_DLL_API
1006      Data
1007      transpose(int axis_offset) const;
1008    
1009      /**
1010         \brief
1011       Return the eigenvalues of the symmetric part at each data point of this Data object in increasing values.       Return the eigenvalues of the symmetric part at each data point of this Data object in increasing values.
1012       Currently this function is restricted to rank 2, square shape, and dimension 3.       Currently this function is restricted to rank 2, square shape, and dimension 3.
1013       *       *
# Line 792  class Data { Line 1019  class Data {
1019    /**    /**
1020       \brief       \brief
1021       Return the eigenvalues and corresponding eigenvcetors of the symmetric part at each data point of this Data object.       Return the eigenvalues and corresponding eigenvcetors of the symmetric part at each data point of this Data object.
1022       the eigenvalues are ordered in increasing size where eigenvalues with relative difference less than       the eigenvalues are ordered in increasing size where eigenvalues with relative difference less than
1023       tol are treated as equal. The eigenvectors are orthogonal, normalized and the sclaed such that the       tol are treated as equal. The eigenvectors are orthogonal, normalized and the sclaed such that the
1024       first non-zero entry is positive.       first non-zero entry is positive.
1025       Currently this function is restricted to rank 2, square shape, and dimension 3       Currently this function is restricted to rank 2, square shape, and dimension 3
1026       *       *
# Line 804  class Data { Line 1031  class Data {
1031    
1032    /**    /**
1033       \brief       \brief
1034       Transpose each data point of this Data object around the given axis.       swaps the components axis0 and axis1
      --* not implemented yet *--  
1035       *       *
1036    */    */
1037    ESCRIPT_DLL_API    ESCRIPT_DLL_API
1038    Data    Data
1039    transpose(int axis) const;    swapaxes(const int axis0, const int axis1) const;
1040    
1041    /**    /**
1042       \brief       \brief
1043       Calculate the trace of each data point of this Data object.       Return the error function erf of each data point of this Data object.
1044       *       *
1045    */    */
1046    ESCRIPT_DLL_API    ESCRIPT_DLL_API
1047    Data    Data
1048    trace() const;    erf() const;
1049    
1050    /**    /**
1051       \brief       \brief
# Line 998  class Data { Line 1224  class Data {
1224    /**    /**
1225       \brief       \brief
1226       Return the given power of each data point of this boost python object.       Return the given power of each data point of this boost python object.
1227        
1228       \param right Input - the power to raise the object to.       \param right Input - the power to raise the object to.
1229       *       *
1230     */     */
# Line 1009  class Data { Line 1235  class Data {
1235    /**    /**
1236       \brief       \brief
1237       Return the given power of each data point of this boost python object.       Return the given power of each data point of this boost python object.
1238        
1239       \param left Input - the bases       \param left Input - the bases
1240       *       *
1241     */     */
# Line 1045  class Data { Line 1271  class Data {
1271    ESCRIPT_DLL_API    ESCRIPT_DLL_API
1272    Data& operator+=(const boost::python::object& right);    Data& operator+=(const boost::python::object& right);
1273    
1274      ESCRIPT_DLL_API
1275      Data& operator=(const Data& other);
1276    
1277    /**    /**
1278       \brief       \brief
1279       Overloaded operator -=       Overloaded operator -=
# Line 1137  class Data { Line 1366  class Data {
1366    ESCRIPT_DLL_API    ESCRIPT_DLL_API
1367    inline    inline
1368    void    void
1369    unaryOp(UnaryFunction operation);    unaryOp2(UnaryFunction operation);
1370    
1371    /**    /**
1372       \brief       \brief
# Line 1148  class Data { Line 1377  class Data {
1377    */    */
1378    ESCRIPT_DLL_API    ESCRIPT_DLL_API
1379    Data    Data
1380    getSlice(const DataArrayView::RegionType& region) const;    getSlice(const DataTypes::RegionType& region) const;
1381    
1382    /**    /**
1383       \brief       \brief
# Line 1161  class Data { Line 1390  class Data {
1390    ESCRIPT_DLL_API    ESCRIPT_DLL_API
1391    void    void
1392    setSlice(const Data& value,    setSlice(const Data& value,
1393             const DataArrayView::RegionType& region);             const DataTypes::RegionType& region);
1394    
1395    /**    /**
1396       \brief       \brief
1397       Archive the current Data object to the given file.       print the data values to stdout. Used for debugging
      \param fileName - Input - file to archive to.  
1398    */    */
1399    ESCRIPT_DLL_API    ESCRIPT_DLL_API
1400    void    void
1401    archiveData(const std::string fileName);          print(void);
1402    
1403    /**    /**
1404       \brief       \brief
1405       Extract the Data object archived in the given file, overwriting       return the MPI rank number of the local data
1406       the current Data object.                   MPI_COMM_WORLD is assumed and the result of MPI_Comm_size()
1407       Note - the current object must be of type DataEmpty.                   is returned
      \param fileName - Input - file to extract from.  
      \param fspace - Input - a suitable FunctionSpace descibing the data.  
1408    */    */
1409    ESCRIPT_DLL_API    ESCRIPT_DLL_API
1410    void          int
1411    extractData(const std::string fileName,          get_MPIRank(void) const;
               const FunctionSpace& fspace);  
1412    
1413      /**
1414         \brief
1415         return the MPI rank number of the local data
1416                     MPI_COMM_WORLD is assumed and the result of MPI_Comm_rank()
1417                     is returned
1418      */
1419      ESCRIPT_DLL_API
1420            int
1421            get_MPISize(void) const;
1422    
1423    /**    /**
1424       \brief       \brief
1425       print the data values to stdout. Used for debugging       return the MPI rank number of the local data
1426                     MPI_COMM_WORLD is assumed and returned.
1427    */    */
1428    void print();    ESCRIPT_DLL_API
1429            MPI_Comm
1430            get_MPIComm(void) const;
1431    
1432      /**
1433         \brief
1434         return the object produced by the factory, which is a DataConstant or DataExpanded
1435        TODO Ownership of this object should be explained in doco.
1436      */
1437      ESCRIPT_DLL_API
1438            DataAbstract*
1439            borrowData(void) const;
1440    
1441      ESCRIPT_DLL_API
1442            DataAbstract_ptr
1443            borrowDataPtr(void) const;
1444    
1445      ESCRIPT_DLL_API
1446            DataReady_ptr
1447            borrowReadyPtr(void) const;
1448    
1449    
1450    
1451      /**
1452         \brief
1453         Return a pointer to the beginning of the datapoint at the specified offset.
1454         TODO Eventually these should be inlined.
1455         \param i - position(offset) in the underlying datastructure
1456      */
1457    
1458      ESCRIPT_DLL_API
1459            DataTypes::ValueType::const_reference
1460            getDataAtOffsetRO(DataTypes::ValueType::size_type i);
1461    
1462    
1463      ESCRIPT_DLL_API
1464            DataTypes::ValueType::reference
1465            getDataAtOffsetRW(DataTypes::ValueType::size_type i);
1466    
1467    
1468    
1469    /**
1470       \brief Create a buffer for use by getSample
1471       Allocates a DataVector large enough for DataLazy::resolveSample to operate on for the current Data.
1472       Do not use this buffer for other Data instances (unless you are sure they will be the same size).
1473      
1474       In multi-threaded sections, this needs to be called on each thread.
1475    
1476       \return A BufferGroup* if Data is lazy, NULL otherwise.
1477       \warning This pointer must be deallocated using freeSampleBuffer to avoid cross library memory issues.
1478    */
1479      ESCRIPT_DLL_API
1480      BufferGroup*
1481      allocSampleBuffer() const;
1482    
1483    /**
1484       \brief Free a buffer allocated with allocSampleBuffer.
1485       \param buffer Input - pointer to the buffer to deallocate.
1486    */
1487    ESCRIPT_DLL_API void freeSampleBuffer(BufferGroup* buffer);
1488    
1489   protected:   protected:
1490    
1491   private:   private:
1492    
1493      double
1494      LsupWorker() const;
1495    
1496      double
1497      supWorker() const;
1498    
1499      double
1500      infWorker() const;
1501    
1502      boost::python::object
1503      integrateWorker() const;
1504    
1505    /**    /**
1506       \brief       \brief
1507       Check *this and the right operand are compatible. Throws       Check *this and the right operand are compatible. Throws
# Line 1249  class Data { Line 1555  class Data {
1555    
1556    /**    /**
1557       \brief       \brief
      Perform the given binary operation on all of the data's elements.  
      RHS is a boost::python object.  
   */  
   template <class BinaryFunction>  
   inline  
   void  
   binaryOp(const boost::python::object& right,  
            BinaryFunction operation);  
   
   /**  
      \brief  
1558       Convert the data type of the RHS to match this.       Convert the data type of the RHS to match this.
1559       \param right - Input - data type to match.       \param right - Input - data type to match.
1560    */    */
# Line 1278  class Data { Line 1573  class Data {
1573       \brief       \brief
1574       Construct a Data object of the appropriate type.       Construct a Data object of the appropriate type.
1575    */    */
1576    template <class IValueType>  
1577    void    void
1578    initialise(const IValueType& value,    initialise(const DataTypes::ValueType& value,
1579             const DataTypes::ShapeType& shape,
1580               const FunctionSpace& what,               const FunctionSpace& what,
1581               bool expanded);               bool expanded);
1582    
   /**  
      \brief  
      Reshape the data point if the data point is currently rank 0.  
      Will throw an exception if the data points are not rank 0.  
      The original data point value is used for all values of the new  
      data point.  
   */  
1583    void    void
1584    reshapeDataPoint(const DataArrayView::ShapeType& shape);    initialise(const WrappedArray& value,
1585                     const FunctionSpace& what,
1586                     bool expanded);
1587    
1588      //
1589      // flag to protect the data object against any update
1590      bool m_protected;
1591      mutable bool m_shared;
1592      bool m_lazy;
1593    
1594    //    //
1595    // pointer to the actual data object    // pointer to the actual data object
1596    boost::shared_ptr<DataAbstract> m_data;  //   boost::shared_ptr<DataAbstract> m_data;
1597      DataAbstract_ptr m_data;
1598    
1599    // If possible please use getReadyPtr instead.
1600    // But see warning below.
1601      const DataReady*
1602      getReady() const;
1603    
1604      DataReady*
1605      getReady();
1606    
1607    
1608    // Be wary of using this for local operations since it (temporarily) increases reference count.
1609    // If you are just using this to call a method on DataReady instead of DataAbstract consider using
1610    // getReady() instead
1611      DataReady_ptr
1612      getReadyPtr();
1613    
1614      const_DataReady_ptr
1615      getReadyPtr() const;
1616    
1617    
1618      /**
1619       \brief Update the Data's shared flag
1620       This indicates that the DataAbstract used by this object is now shared (or no longer shared).
1621       For internal use only.
1622      */
1623      void updateShareStatus(bool nowshared) const
1624      {
1625        m_shared=nowshared;     // m_shared is mutable
1626      }
1627    
1628      // In the isShared() method below:
1629      // A problem would occur if m_data (the address pointed to) were being modified
1630      // while the call m_data->is_shared is being executed.
1631      //
1632      // Q: So why do I think this code can be thread safe/correct?
1633      // A: We need to make some assumptions.
1634      // 1. We assume it is acceptable to return true under some conditions when we aren't shared.
1635      // 2. We assume that no constructions or assignments which will share previously unshared
1636      //    will occur while this call is executing. This is consistent with the way Data:: and C are written.
1637    //    //
1638    // pointer to the internal profiling data    // This means that the only transition we need to consider, is when a previously shared object is
1639    struct profDataEntry *profData;    // not shared anymore. ie. the other objects have been destroyed or a deep copy has been made.
1640      // In those cases the m_shared flag changes to false after m_data has completed changing.
1641      // For any threads executing before the flag switches they will assume the object is still shared.
1642      bool isShared() const
1643      {
1644        return m_shared;
1645    /*  if (m_shared) return true;
1646        if (m_data->isShared())        
1647        {                  
1648            updateShareStatus(true);
1649            return true;
1650        }
1651        return false;*/
1652      }
1653    
1654      void forceResolve()
1655      {
1656        if (isLazy())
1657        {
1658            #ifdef _OPENMP
1659            if (omp_in_parallel())
1660            {   // Yes this is throwing an exception out of an omp thread which is forbidden.
1661            throw DataException("Please do not call forceResolve() in a parallel region.");
1662            }
1663            #endif
1664            resolve();
1665        }
1666      }
1667    
1668      /**
1669      \brief if another object is sharing out member data make a copy to work with instead.
1670      This code should only be called from single threaded sections of code.
1671      */
1672      void exclusiveWrite()
1673      {
1674    #ifdef _OPENMP
1675      if (omp_in_parallel())
1676      {
1677    // *((int*)0)=17;
1678        throw DataException("Programming error. Please do not run exclusiveWrite() in multi-threaded sections.");
1679      }
1680    #endif
1681        forceResolve();
1682        if (isShared())
1683        {
1684            DataAbstract* t=m_data->deepCopy();
1685            set_m_data(DataAbstract_ptr(t));
1686        }
1687      }
1688    
1689      /**
1690      \brief checks if caller can have exclusive write to the object
1691      */
1692      void checkExclusiveWrite()
1693      {
1694        if  (isLazy() || isShared())
1695        {
1696            throw DataException("Programming error. ExclusiveWrite required - please call requireWrite()");
1697        }
1698      }
1699    
1700      /**
1701      \brief Modify the data abstract hosted by this Data object
1702      For internal use only.
1703      Passing a pointer to null is permitted (do this in the destructor)
1704      \warning Only to be called in single threaded code or inside a single/critical section. This method needs to be atomic.
1705      */
1706      void set_m_data(DataAbstract_ptr p);
1707    
1708      friend class DataAbstract;        // To allow calls to updateShareStatus
1709    
1710  };  };
1711    
1712  template <class IValueType>  }   // end namespace escript
1713  void  
1714  Data::initialise(const IValueType& value,  
1715                   const FunctionSpace& what,  // No, this is not supposed to be at the top of the file
1716                   bool expanded)  // DataAbstact needs to be declared first, then DataReady needs to be fully declared
1717    // so that I can dynamic cast between them below.
1718    #include "DataReady.h"
1719    #include "DataLazy.h"
1720    
1721    namespace escript
1722  {  {
1723    //  
1724    // Construct a Data object of the appropriate type.  inline
1725    // Construct the object first as there seems to be a bug which causes  const DataReady*
1726    // undefined behaviour if an exception is thrown during construction  Data::getReady() const
1727    // within the shared_ptr constructor.  {
1728    if (expanded) {     const DataReady* dr=dynamic_cast<const DataReady*>(m_data.get());
1729      DataAbstract* temp=new DataExpanded(value,what);     EsysAssert((dr!=0), "Error - casting to DataReady.");
1730      boost::shared_ptr<DataAbstract> temp_data(temp);     return dr;
1731      m_data=temp_data;  }
1732    } else {  
1733      DataAbstract* temp=new DataConstant(value,what);  inline
1734      boost::shared_ptr<DataAbstract> temp_data(temp);  DataReady*
1735      m_data=temp_data;  Data::getReady()
1736    }  {
1737       DataReady* dr=dynamic_cast<DataReady*>(m_data.get());
1738       EsysAssert((dr!=0), "Error - casting to DataReady.");
1739       return dr;
1740  }  }
1741    
1742    // Be wary of using this for local operations since it (temporarily) increases reference count.
1743    // If you are just using this to call a method on DataReady instead of DataAbstract consider using
1744    // getReady() instead
1745    inline
1746    DataReady_ptr
1747    Data::getReadyPtr()
1748    {
1749       DataReady_ptr dr=boost::dynamic_pointer_cast<DataReady>(m_data);
1750       EsysAssert((dr.get()!=0), "Error - casting to DataReady.");
1751       return dr;
1752    }
1753    
1754    
1755    inline
1756    const_DataReady_ptr
1757    Data::getReadyPtr() const
1758    {
1759       const_DataReady_ptr dr=boost::dynamic_pointer_cast<const DataReady>(m_data);
1760       EsysAssert((dr.get()!=0), "Error - casting to DataReady.");
1761       return dr;
1762    }
1763    
1764    inline
1765    DataAbstract::ValueType::value_type*
1766    Data::getSampleDataRW(DataAbstract::ValueType::size_type sampleNo)
1767    {
1768    //    if (isLazy())
1769    //    {
1770    //  resolve();
1771    //    }
1772    //    exclusiveWrite();
1773       if (isLazy())
1774       {
1775        throw DataException("Error, attempt to acquire RW access to lazy data. Please call requireWrite() first.");
1776       }
1777       return getReady()->getSampleDataRW(sampleNo);
1778    }
1779    
1780    // inline
1781    // const DataAbstract::ValueType::value_type*
1782    // Data::getSampleDataRO(DataAbstract::ValueType::size_type sampleNo, DataTypes::ValueType* buffer)
1783    // {
1784    //    DataLazy* l=dynamic_cast<DataLazy*>(m_data.get());
1785    //    if (l!=0)
1786    //    {
1787    //  size_t offset=0;
1788    //  if (buffer==NULL)
1789    //  {
1790    //      throw DataException("Error, attempt to getSampleDataRO for lazy Data with buffer==NULL");
1791    //  }
1792    //  const DataTypes::ValueType* res=l->resolveSample(*buffer,0,sampleNo,offset);
1793    //  return &((*res)[offset]);
1794    //    }
1795    //    return getReady()->getSampleDataRO(sampleNo);
1796    // }
1797    
1798    inline
1799    const DataAbstract::ValueType::value_type*
1800    Data::getSampleDataRO(DataAbstract::ValueType::size_type sampleNo, BufferGroup* bufferg)
1801    {
1802       DataLazy* l=dynamic_cast<DataLazy*>(m_data.get());
1803       if (l!=0)
1804       {
1805        size_t offset=0;
1806        if (bufferg==NULL)
1807        {
1808            throw DataException("Error, attempt to getSampleDataRO for lazy Data with buffer==NULL");
1809        }
1810        const DataTypes::ValueType* res=l->resolveSample(*bufferg,sampleNo,offset);
1811        return &((*res)[offset]);
1812       }
1813       return getReady()->getSampleDataRO(sampleNo);
1814    }
1815    
1816    
1817    
1818    /**
1819       Modify a filename for MPI parallel output to multiple files
1820    */
1821    char *Escript_MPI_appendRankToFileName(const char *, int, int);
1822    
1823  /**  /**
1824     Binary Data object operators.     Binary Data object operators.
1825  */  */
1826    inline double rpow(double x,double y)
1827    {
1828        return pow(y,x);
1829    }
1830    
1831  /**  /**
1832    \brief    \brief
# Line 1422  ESCRIPT_DLL_API Data operator*(const boo Line 1920  ESCRIPT_DLL_API Data operator*(const boo
1920  */  */
1921  ESCRIPT_DLL_API Data operator/(const boost::python::object& left, const Data& right);  ESCRIPT_DLL_API Data operator/(const boost::python::object& left, const Data& right);
1922    
1923    
1924    
1925  /**  /**
1926    \brief    \brief
1927    Output operator    Output operator
# Line 1430  ESCRIPT_DLL_API std::ostream& operator<< Line 1930  ESCRIPT_DLL_API std::ostream& operator<<
1930    
1931  /**  /**
1932    \brief    \brief
1933    Return true if operands are equivalent, else return false.    Compute a tensor product of two Data objects
1934    NB: this operator does very little at this point, and isn't to    \param arg0 - Input - Data object
1935    be relied on. Requires further implementation.    \param arg1 - Input - Data object
1936      \param axis_offset - Input - axis offset
1937      \param transpose - Input - 0: transpose neither, 1: transpose arg0, 2: transpose arg1
1938  */  */
1939  //ESCRIPT_DLL_API bool operator==(const Data& left, const Data& right);  ESCRIPT_DLL_API
1940    Data
1941    C_GeneralTensorProduct(Data& arg0,
1942                         Data& arg1,
1943                         int axis_offset=0,
1944                         int transpose=0);
1945    
1946  /**  /**
1947    \brief    \brief
# Line 1449  Data::binaryOp(const Data& right, Line 1956  Data::binaryOp(const Data& right,
1956  {  {
1957     //     //
1958     // if this has a rank of zero promote it to the rank of the RHS     // if this has a rank of zero promote it to the rank of the RHS
1959     if (getPointDataView().getRank()==0 && right.getPointDataView().getRank()!=0) {     if (getDataPointRank()==0 && right.getDataPointRank()!=0) {
1960       reshapeDataPoint(right.getPointDataView().getShape());       throw DataException("Error - attempt to update rank zero object with object with rank bigger than zero.");
1961       }
1962    
1963       if (isLazy() || right.isLazy())
1964       {
1965         throw DataException("Programmer error - attempt to call binaryOp with Lazy Data.");
1966     }     }
1967     //     //
1968     // initially make the temporary a shallow copy     // initially make the temporary a shallow copy
1969     Data tempRight(right);     Data tempRight(right);
1970    
1971     if (getFunctionSpace()!=right.getFunctionSpace()) {     if (getFunctionSpace()!=right.getFunctionSpace()) {
1972       if (right.probeInterpolation(getFunctionSpace())) {       if (right.probeInterpolation(getFunctionSpace())) {
1973         //         //
1974         // an interpolation is required so create a new Data         // an interpolation is required so create a new Data
1975         tempRight=Data(right,this->getFunctionSpace());         tempRight=Data(right,this->getFunctionSpace());
1976       } else if (probeInterpolation(right.getFunctionSpace())) {       } else if (probeInterpolation(right.getFunctionSpace())) {
1977         //         //
1978         // interpolate onto the RHS function space         // interpolate onto the RHS function space
1979         Data tempLeft(*this,right.getFunctionSpace());         Data tempLeft(*this,right.getFunctionSpace());
1980         m_data=tempLeft.m_data;  //        m_data=tempLeft.m_data;
1981           set_m_data(tempLeft.m_data);
1982       }       }
1983     }     }
1984     operandCheck(tempRight);     operandCheck(tempRight);
# Line 1480  Data::binaryOp(const Data& right, Line 1994  Data::binaryOp(const Data& right,
1994       // of any data type       // of any data type
1995       DataExpanded* leftC=dynamic_cast<DataExpanded*>(m_data.get());       DataExpanded* leftC=dynamic_cast<DataExpanded*>(m_data.get());
1996       EsysAssert((leftC!=0), "Programming error - casting to DataExpanded.");       EsysAssert((leftC!=0), "Programming error - casting to DataExpanded.");
1997       escript::binaryOp(*leftC,*(tempRight.m_data.get()),operation);       escript::binaryOp(*leftC,*(tempRight.getReady()),operation);
1998     } else if (isTagged()) {     } else if (isTagged()) {
1999       //       //
2000       // Tagged data is operated on serially, the right hand side can be       // Tagged data is operated on serially, the right hand side can be
# Line 1506  Data::binaryOp(const Data& right, Line 2020  Data::binaryOp(const Data& right,
2020    
2021  /**  /**
2022    \brief    \brief
   Perform the given binary operation with this and right as operands.  
   Right is a boost::python object.  
 */  
 template <class BinaryFunction>  
 inline  
 void  
 Data::binaryOp(const boost::python::object& right,  
                BinaryFunction operation)  
 {  
    DataArray temp(right);  
    //  
    // if this has a rank of zero promote it to the rank of the RHS.  
    if (getPointDataView().getRank()==0 && temp.getView().getRank()!=0) {  
       reshapeDataPoint(temp.getView().getShape());  
    }  
    //  
    // Always allow scalar values for the RHS but check other shapes  
    if (temp.getView().getRank()!=0) {  
      if (!getPointDataView().checkShape(temp.getView().getShape())) {  
        throw DataException(getPointDataView().createShapeErrorMessage(  
                   "Error - RHS shape doesn't match LHS shape.",temp.getView().getShape()));  
      }  
    }  
    if (isExpanded()) {  
      DataExpanded* leftC=dynamic_cast<DataExpanded*>(m_data.get());  
      EsysAssert((leftC!=0),"Programming error - casting to DataExpanded.");  
      escript::binaryOp(*leftC,temp.getView(),operation);  
    } else if (isTagged()) {  
      DataTagged* leftC=dynamic_cast<DataTagged*>(m_data.get());  
      EsysAssert((leftC!=0), "Programming error - casting to DataTagged.");  
      escript::binaryOp(*leftC,temp.getView(),operation);  
    } else if (isConstant()) {  
      DataConstant* leftC=dynamic_cast<DataConstant*>(m_data.get());  
      EsysAssert((leftC!=0),"Programming error - casting to DataConstant.");  
      escript::binaryOp(*leftC,temp.getView(),operation);  
    }  
 }  
   
 /**  
   \brief  
   Perform the given unary operation on other and return the result.  
   Given operation is performed on each element of each data point, thus  
   argument object is a rank n Data object, and returned object is a rank n  
   Data object.  
   Calls Data::unaryOp.  
 */  
 template <class UnaryFunction>  
 inline  
 Data  
 unaryOp(const Data& other,  
         UnaryFunction operation)  
 {  
   Data result;  
   result.copy(other);  
   result.unaryOp(operation);  
   return result;  
 }  
   
 /**  
   \brief  
   Perform the given unary operation on this.  
   Given operation is performed on each element of each data point.  
   Calls escript::unaryOp.  
 */  
 template <class UnaryFunction>  
 inline  
 void  
 Data::unaryOp(UnaryFunction operation)  
 {  
   if (isExpanded()) {  
     DataExpanded* leftC=dynamic_cast<DataExpanded*>(m_data.get());  
     EsysAssert((leftC!=0), "Programming error - casting to DataExpanded.");  
     escript::unaryOp(*leftC,operation);  
   } else if (isTagged()) {  
     DataTagged* leftC=dynamic_cast<DataTagged*>(m_data.get());  
     EsysAssert((leftC!=0), "Programming error - casting to DataTagged.");  
     escript::unaryOp(*leftC,operation);  
   } else if (isConstant()) {  
     DataConstant* leftC=dynamic_cast<DataConstant*>(m_data.get());  
     EsysAssert((leftC!=0), "Programming error - casting to DataConstant.");  
     escript::unaryOp(*leftC,operation);  
   }  
 }  
   
 /**  
   \brief  
2023    Perform the given Data object reduction algorithm on this and return the result.    Perform the given Data object reduction algorithm on this and return the result.
2024    Given operation combines each element of each data point, thus argument    Given operation combines each element of each data point, thus argument
2025    object (*this) is a rank n Data object, and returned object is a scalar.    object (*this) is a rank n Data object, and returned object is a scalar.
# Line 1614  Data::algorithm(BinaryFunction operation Line 2042  Data::algorithm(BinaryFunction operation
2042      DataConstant* leftC=dynamic_cast<DataConstant*>(m_data.get());      DataConstant* leftC=dynamic_cast<DataConstant*>(m_data.get());
2043      EsysAssert((leftC!=0), "Programming error - casting to DataConstant.");      EsysAssert((leftC!=0), "Programming error - casting to DataConstant.");
2044      return escript::algorithm(*leftC,operation,initial_value);      return escript::algorithm(*leftC,operation,initial_value);
2045      } else if (isEmpty()) {
2046        throw DataException("Error - Operations not permitted on instances of DataEmpty.");
2047      } else if (isLazy()) {
2048        throw DataException("Error - Operations not permitted on instances of DataLazy.");
2049      } else {
2050        throw DataException("Error - Data encapsulates an unknown type.");
2051    }    }
   return 0;  
2052  }  }
2053    
2054  /**  /**
2055    \brief    \brief
2056    Perform the given data point reduction algorithm on data and return the result.    Perform the given data point reduction algorithm on data and return the result.
2057    Given operation combines each element within each data point into a scalar,    Given operation combines each element within each data point into a scalar,
2058    thus argument object is a rank n Data object, and returned object is a    thus argument object is a rank n Data object, and returned object is a
2059    rank 0 Data object.    rank 0 Data object.
2060    Calls escript::dp_algorithm.    Calls escript::dp_algorithm.
2061  */  */
# Line 1631  inline Line 2064  inline
2064  Data  Data
2065  Data::dp_algorithm(BinaryFunction operation, double initial_value) const  Data::dp_algorithm(BinaryFunction operation, double initial_value) const
2066  {  {
2067    if (isExpanded()) {    if (isEmpty()) {
2068      Data result(0,DataArrayView::ShapeType(),getFunctionSpace(),isExpanded());      throw DataException("Error - Operations not permitted on instances of DataEmpty.");
2069      }
2070      else if (isExpanded()) {
2071        Data result(0,DataTypes::ShapeType(),getFunctionSpace(),isExpanded());
2072      DataExpanded* dataE=dynamic_cast<DataExpanded*>(m_data.get());      DataExpanded* dataE=dynamic_cast<DataExpanded*>(m_data.get());
2073      DataExpanded* resultE=dynamic_cast<DataExpanded*>(result.m_data.get());      DataExpanded* resultE=dynamic_cast<DataExpanded*>(result.m_data.get());
2074      EsysAssert((dataE!=0), "Programming error - casting data to DataExpanded.");      EsysAssert((dataE!=0), "Programming error - casting data to DataExpanded.");
2075      EsysAssert((resultE!=0), "Programming error - casting result to DataExpanded.");      EsysAssert((resultE!=0), "Programming error - casting result to DataExpanded.");
2076      escript::dp_algorithm(*dataE,*resultE,operation,initial_value);      escript::dp_algorithm(*dataE,*resultE,operation,initial_value);
2077      return result;      return result;
2078    } else if (isTagged()) {    }
2079      else if (isTagged()) {
2080      DataTagged* dataT=dynamic_cast<DataTagged*>(m_data.get());      DataTagged* dataT=dynamic_cast<DataTagged*>(m_data.get());
     DataArrayView::ShapeType viewShape;  
     DataArrayView::ValueType viewData(1);  
     viewData[0]=0;  
     DataArrayView defaultValue(viewData,viewShape);  
     DataTagged::TagListType keys;  
     DataTagged::ValueListType values;  
     DataTagged::DataMapType::const_iterator i;  
     for (i=dataT->getTagLookup().begin();i!=dataT->getTagLookup().end();i++) {  
       keys.push_back(i->first);  
       values.push_back(defaultValue);  
     }  
     Data result(keys,values,defaultValue,getFunctionSpace());  
     DataTagged* resultT=dynamic_cast<DataTagged*>(result.m_data.get());  
2081      EsysAssert((dataT!=0), "Programming error - casting data to DataTagged.");      EsysAssert((dataT!=0), "Programming error - casting data to DataTagged.");
2082      EsysAssert((resultT!=0), "Programming error - casting result to DataTagged.");      DataTypes::ValueType defval(1);
2083        defval[0]=0;
2084        DataTagged* resultT=new DataTagged(getFunctionSpace(), DataTypes::scalarShape, defval, dataT);
2085      escript::dp_algorithm(*dataT,*resultT,operation,initial_value);      escript::dp_algorithm(*dataT,*resultT,operation,initial_value);
2086      return result;      return Data(resultT);   // note: the Data object now owns the resultT pointer
2087    } else if (isConstant()) {    }
2088      Data result(0,DataArrayView::ShapeType(),getFunctionSpace(),isExpanded());    else if (isConstant()) {
2089        Data result(0,DataTypes::ShapeType(),getFunctionSpace(),isExpanded());
2090      DataConstant* dataC=dynamic_cast<DataConstant*>(m_data.get());      DataConstant* dataC=dynamic_cast<DataConstant*>(m_data.get());
2091      DataConstant* resultC=dynamic_cast<DataConstant*>(result.m_data.get());      DataConstant* resultC=dynamic_cast<DataConstant*>(result.m_data.get());
2092      EsysAssert((dataC!=0), "Programming error - casting data to DataConstant.");      EsysAssert((dataC!=0), "Programming error - casting data to DataConstant.");
2093      EsysAssert((resultC!=0), "Programming error - casting result to DataConstant.");      EsysAssert((resultC!=0), "Programming error - casting result to DataConstant.");
2094      escript::dp_algorithm(*dataC,*resultC,operation,initial_value);      escript::dp_algorithm(*dataC,*resultC,operation,initial_value);
2095      return result;      return result;
2096      } else if (isLazy()) {
2097        throw DataException("Error - Operations not permitted on instances of DataLazy.");
2098      } else {
2099        throw DataException("Error - Data encapsulates an unknown type.");
2100      }
2101    }
2102    
2103    /**
2104      \brief
2105      Compute a tensor operation with two Data objects
2106      \param arg0 - Input - Data object
2107      \param arg1 - Input - Data object
2108      \param operation - Input - Binary op functor
2109    */
2110    template <typename BinaryFunction>
2111    inline
2112    Data
2113    C_TensorBinaryOperation(Data const &arg_0,
2114                            Data const &arg_1,
2115                            BinaryFunction operation)
2116    {
2117      if (arg_0.isEmpty() || arg_1.isEmpty())
2118      {
2119         throw DataException("Error - Operations not permitted on instances of DataEmpty.");
2120      }
2121      if (arg_0.isLazy() || arg_1.isLazy())
2122      {
2123         throw DataException("Error - Operations not permitted on lazy data.");
2124      }
2125      // Interpolate if necessary and find an appropriate function space
2126      Data arg_0_Z, arg_1_Z;
2127      if (arg_0.getFunctionSpace()!=arg_1.getFunctionSpace()) {
2128        if (arg_0.probeInterpolation(arg_1.getFunctionSpace())) {
2129          arg_0_Z = arg_0.interpolate(arg_1.getFunctionSpace());
2130          arg_1_Z = Data(arg_1);
2131        }
2132        else if (arg_1.probeInterpolation(arg_0.getFunctionSpace())) {
2133          arg_1_Z=arg_1.interpolate(arg_0.getFunctionSpace());
2134          arg_0_Z =Data(arg_0);
2135        }
2136        else {
2137          throw DataException("Error - C_TensorBinaryOperation: arguments have incompatible function spaces.");
2138        }
2139      } else {
2140          arg_0_Z = Data(arg_0);
2141          arg_1_Z = Data(arg_1);
2142      }
2143      // Get rank and shape of inputs
2144      int rank0 = arg_0_Z.getDataPointRank();
2145      int rank1 = arg_1_Z.getDataPointRank();
2146      DataTypes::ShapeType shape0 = arg_0_Z.getDataPointShape();
2147      DataTypes::ShapeType shape1 = arg_1_Z.getDataPointShape();
2148      int size0 = arg_0_Z.getDataPointSize();
2149      int size1 = arg_1_Z.getDataPointSize();
2150      // Declare output Data object
2151      Data res;
2152    
2153      if (shape0 == shape1) {
2154        if (arg_0_Z.isConstant()   && arg_1_Z.isConstant()) {
2155          res = Data(0.0, shape0, arg_1_Z.getFunctionSpace());      // DataConstant output
2156          const double *ptr_0 = &(arg_0_Z.getDataAtOffsetRO(0));
2157          const double *ptr_1 = &(arg_1_Z.getDataAtOffsetRO(0));
2158          double *ptr_2 = &(res.getDataAtOffsetRW(0));
2159    
2160          tensor_binary_operation(size0, ptr_0, ptr_1, ptr_2, operation);
2161        }
2162        else if (arg_0_Z.isConstant()   && arg_1_Z.isTagged()) {
2163    
2164          // Prepare the DataConstant input
2165          DataConstant* tmp_0=dynamic_cast<DataConstant*>(arg_0_Z.borrowData());
2166    
2167          // Borrow DataTagged input from Data object
2168          DataTagged* tmp_1=dynamic_cast<DataTagged*>(arg_1_Z.borrowData());
2169    
2170          // Prepare a DataTagged output 2
2171          res = Data(0.0, shape0, arg_1_Z.getFunctionSpace());      // DataTagged output
2172          res.tag();
2173          DataTagged* tmp_2=dynamic_cast<DataTagged*>(res.borrowData());
2174    
2175          // Prepare offset into DataConstant
2176          int offset_0 = tmp_0->getPointOffset(0,0);
2177          const double *ptr_0 = &(arg_0_Z.getDataAtOffsetRO(offset_0));
2178    
2179          // Get the pointers to the actual data
2180          const double *ptr_1 = &(tmp_1->getDefaultValueRO(0));
2181          double *ptr_2 = &(tmp_2->getDefaultValueRW(0));
2182    
2183          // Compute a result for the default
2184          tensor_binary_operation(size0, ptr_0, ptr_1, ptr_2, operation);
2185          // Compute a result for each tag
2186          const DataTagged::DataMapType& lookup_1=tmp_1->getTagLookup();
2187          DataTagged::DataMapType::const_iterator i; // i->first is a tag, i->second is an offset into memory
2188          for (i=lookup_1.begin();i!=lookup_1.end();i++) {
2189            tmp_2->addTag(i->first);
2190            const double *ptr_1 = &(tmp_1->getDataByTagRO(i->first,0));
2191            double *ptr_2 = &(tmp_2->getDataByTagRW(i->first,0));
2192    
2193            tensor_binary_operation(size0, ptr_0, ptr_1, ptr_2, operation);
2194          }
2195    
2196        }
2197        else if (arg_0_Z.isConstant()   && arg_1_Z.isExpanded()) {
2198          res = Data(0.0, shape0, arg_1_Z.getFunctionSpace(),true); // DataExpanded output
2199          DataConstant* tmp_0=dynamic_cast<DataConstant*>(arg_0_Z.borrowData());
2200          DataExpanded* tmp_1=dynamic_cast<DataExpanded*>(arg_1_Z.borrowData());
2201          DataExpanded* tmp_2=dynamic_cast<DataExpanded*>(res.borrowData());
2202    
2203          int sampleNo_1,dataPointNo_1;
2204          int numSamples_1 = arg_1_Z.getNumSamples();
2205          int numDataPointsPerSample_1 = arg_1_Z.getNumDataPointsPerSample();
2206          int offset_0 = tmp_0->getPointOffset(0,0);
2207          res.requireWrite();
2208          #pragma omp parallel for private(sampleNo_1,dataPointNo_1) schedule(static)
2209          for (sampleNo_1 = 0; sampleNo_1 < numSamples_1; sampleNo_1++) {
2210            for (dataPointNo_1 = 0; dataPointNo_1 < numDataPointsPerSample_1; dataPointNo_1++) {
2211              int offset_1 = tmp_1->getPointOffset(sampleNo_1,dataPointNo_1);
2212              int offset_2 = tmp_2->getPointOffset(sampleNo_1,dataPointNo_1);
2213              const double *ptr_0 = &(arg_0_Z.getDataAtOffsetRO(offset_0));
2214              const double *ptr_1 = &(arg_1_Z.getDataAtOffsetRO(offset_1));
2215              double *ptr_2 = &(res.getDataAtOffsetRW(offset_2));
2216              tensor_binary_operation(size0, ptr_0, ptr_1, ptr_2, operation);
2217            }
2218          }
2219    
2220        }
2221        else if (arg_0_Z.isTagged()     && arg_1_Z.isConstant()) {
2222          // Borrow DataTagged input from Data object
2223          DataTagged* tmp_0=dynamic_cast<DataTagged*>(arg_0_Z.borrowData());
2224    
2225          // Prepare the DataConstant input
2226          DataConstant* tmp_1=dynamic_cast<DataConstant*>(arg_1_Z.borrowData());
2227    
2228          // Prepare a DataTagged output 2
2229          res = Data(0.0, shape0, arg_0_Z.getFunctionSpace());      // DataTagged output
2230          res.tag();
2231          DataTagged* tmp_2=dynamic_cast<DataTagged*>(res.borrowData());
2232    
2233          // Prepare offset into DataConstant
2234          int offset_1 = tmp_1->getPointOffset(0,0);
2235    
2236          const double *ptr_1 = &(arg_1_Z.getDataAtOffsetRO(offset_1));
2237          // Get the pointers to the actual data
2238          const double *ptr_0 = &(tmp_0->getDefaultValueRO(0));
2239          double *ptr_2 = &(tmp_2->getDefaultValueRW(0));
2240          // Compute a result for the default
2241          tensor_binary_operation(size0, ptr_0, ptr_1, ptr_2, operation);
2242          // Compute a result for each tag
2243          const DataTagged::DataMapType& lookup_0=tmp_0->getTagLookup();
2244          DataTagged::DataMapType::const_iterator i; // i->first is a tag, i->second is an offset into memory
2245          for (i=lookup_0.begin();i!=lookup_0.end();i++) {
2246            tmp_2->addTag(i->first);
2247            const double *ptr_0 = &(tmp_0->getDataByTagRO(i->first,0));
2248            double *ptr_2 = &(tmp_2->getDataByTagRW(i->first,0));
2249            tensor_binary_operation(size0, ptr_0, ptr_1, ptr_2, operation);
2250          }
2251    
2252        }
2253        else if (arg_0_Z.isTagged()     && arg_1_Z.isTagged()) {
2254          // Borrow DataTagged input from Data object
2255          DataTagged* tmp_0=dynamic_cast<DataTagged*>(arg_0_Z.borrowData());
2256    
2257          // Borrow DataTagged input from Data object
2258          DataTagged* tmp_1=dynamic_cast<DataTagged*>(arg_1_Z.borrowData());
2259    
2260          // Prepare a DataTagged output 2
2261          res = Data(0.0, shape0, arg_1_Z.getFunctionSpace());
2262          res.tag();        // DataTagged output
2263          DataTagged* tmp_2=dynamic_cast<DataTagged*>(res.borrowData());
2264    
2265          // Get the pointers to the actual data
2266          const double *ptr_0 = &(tmp_0->getDefaultValueRO(0));
2267          const double *ptr_1 = &(tmp_1->getDefaultValueRO(0));
2268          double *ptr_2 = &(tmp_2->getDefaultValueRW(0));
2269    
2270          // Compute a result for the default
2271          tensor_binary_operation(size0, ptr_0, ptr_1, ptr_2, operation);
2272          // Merge the tags
2273          DataTagged::DataMapType::const_iterator i; // i->first is a tag, i->second is an offset into memory
2274          const DataTagged::DataMapType& lookup_0=tmp_0->getTagLookup();
2275          const DataTagged::DataMapType& lookup_1=tmp_1->getTagLookup();
2276          for (i=lookup_0.begin();i!=lookup_0.end();i++) {
2277            tmp_2->addTag(i->first); // use tmp_2 to get correct shape
2278          }
2279          for (i=lookup_1.begin();i!=lookup_1.end();i++) {
2280            tmp_2->addTag(i->first);
2281          }
2282          // Compute a result for each tag
2283          const DataTagged::DataMapType& lookup_2=tmp_2->getTagLookup();
2284          for (i=lookup_2.begin();i!=lookup_2.end();i++) {
2285    
2286            const double *ptr_0 = &(tmp_0->getDataByTagRO(i->first,0));
2287            const double *ptr_1 = &(tmp_1->getDataByTagRO(i->first,0));
2288            double *ptr_2 = &(tmp_2->getDataByTagRW(i->first,0));
2289    
2290            tensor_binary_operation(size0, ptr_0, ptr_1, ptr_2, operation);
2291          }
2292    
2293        }
2294        else if (arg_0_Z.isTagged()     && arg_1_Z.isExpanded()) {
2295          // After finding a common function space above the two inputs have the same numSamples and num DPPS
2296          res = Data(0.0, shape0, arg_1_Z.getFunctionSpace(),true); // DataExpanded output
2297          DataTagged*   tmp_0=dynamic_cast<DataTagged*>(arg_0_Z.borrowData());
2298          DataExpanded* tmp_1=dynamic_cast<DataExpanded*>(arg_1_Z.borrowData());
2299          DataExpanded* tmp_2=dynamic_cast<DataExpanded*>(res.borrowData());
2300    
2301          int sampleNo_0,dataPointNo_0;
2302          int numSamples_0 = arg_0_Z.getNumSamples();
2303          int numDataPointsPerSample_0 = arg_0_Z.getNumDataPointsPerSample();
2304          res.requireWrite();
2305          #pragma omp parallel for private(sampleNo_0,dataPointNo_0) schedule(static)
2306          for (sampleNo_0 = 0; sampleNo_0 < numSamples_0; sampleNo_0++) {
2307            int offset_0 = tmp_0->getPointOffset(sampleNo_0,0); // They're all the same, so just use #0
2308            const double *ptr_0 = &(arg_0_Z.getDataAtOffsetRO(offset_0));
2309            for (dataPointNo_0 = 0; dataPointNo_0 < numDataPointsPerSample_0; dataPointNo_0++) {
2310              int offset_1 = tmp_1->getPointOffset(sampleNo_0,dataPointNo_0);
2311              int offset_2 = tmp_2->getPointOffset(sampleNo_0,dataPointNo_0);
2312              const double *ptr_1 = &(arg_1_Z.getDataAtOffsetRO(offset_1));
2313              double *ptr_2 = &(res.getDataAtOffsetRW(offset_2));
2314              tensor_binary_operation(size0, ptr_0, ptr_1, ptr_2, operation);
2315            }
2316          }
2317    
2318        }
2319        else if (arg_0_Z.isExpanded()   && arg_1_Z.isConstant()) {
2320          res = Data(0.0, shape0, arg_1_Z.getFunctionSpace(),true); // DataExpanded output
2321          DataExpanded* tmp_0=dynamic_cast<DataExpanded*>(arg_0_Z.borrowData());
2322          DataConstant* tmp_1=dynamic_cast<DataConstant*>(arg_1_Z.borrowData());
2323          DataExpanded* tmp_2=dynamic_cast<DataExpanded*>(res.borrowData());
2324    
2325          int sampleNo_0,dataPointNo_0;
2326          int numSamples_0 = arg_0_Z.getNumSamples();
2327          int numDataPointsPerSample_0 = arg_0_Z.getNumDataPointsPerSample();
2328          int offset_1 = tmp_1->getPointOffset(0,0);
2329          res.requireWrite();
2330          #pragma omp parallel for private(sampleNo_0,dataPointNo_0) schedule(static)
2331          for (sampleNo_0 = 0; sampleNo_0 < numSamples_0; sampleNo_0++) {
2332            for (dataPointNo_0 = 0; dataPointNo_0 < numDataPointsPerSample_0; dataPointNo_0++) {
2333              int offset_0 = tmp_0->getPointOffset(sampleNo_0,dataPointNo_0);
2334              int offset_2 = tmp_2->getPointOffset(sampleNo_0,dataPointNo_0);
2335    
2336              const double *ptr_0 = &(arg_0_Z.getDataAtOffsetRO(offset_0));
2337              const double *ptr_1 = &(arg_1_Z.getDataAtOffsetRO(offset_1));
2338              double *ptr_2 = &(res.getDataAtOffsetRW(offset_2));
2339    
2340    
2341              tensor_binary_operation(size0, ptr_0, ptr_1, ptr_2, operation);
2342            }
2343          }
2344    
2345        }
2346        else if (arg_0_Z.isExpanded()   && arg_1_Z.isTagged()) {
2347          // After finding a common function space above the two inputs have the same numSamples and num DPPS
2348          res = Data(0.0, shape0, arg_1_Z.getFunctionSpace(),true); // DataExpanded output
2349          DataExpanded* tmp_0=dynamic_cast<DataExpanded*>(arg_0_Z.borrowData());
2350          DataTagged*   tmp_1=dynamic_cast<DataTagged*>(arg_1_Z.borrowData());
2351          DataExpanded* tmp_2=dynamic_cast<DataExpanded*>(res.borrowData());
2352    
2353          int sampleNo_0,dataPointNo_0;
2354          int numSamples_0 = arg_0_Z.getNumSamples();
2355          int numDataPointsPerSample_0 = arg_0_Z.getNumDataPointsPerSample();
2356          res.requireWrite();
2357          #pragma omp parallel for private(sampleNo_0,dataPointNo_0) schedule(static)
2358          for (sampleNo_0 = 0; sampleNo_0 < numSamples_0; sampleNo_0++) {
2359            int offset_1 = tmp_1->getPointOffset(sampleNo_0,0);
2360            const double *ptr_1 = &(arg_1_Z.getDataAtOffsetRO(offset_1));
2361            for (dataPointNo_0 = 0; dataPointNo_0 < numDataPointsPerSample_0; dataPointNo_0++) {
2362              int offset_0 = tmp_0->getPointOffset(sampleNo_0,dataPointNo_0);
2363              int offset_2 = tmp_2->getPointOffset(sampleNo_0,dataPointNo_0);
2364              const double *ptr_0 = &(arg_0_Z.getDataAtOffsetRO(offset_0));
2365              double *ptr_2 = &(res.getDataAtOffsetRW(offset_2));
2366              tensor_binary_operation(size0, ptr_0, ptr_1, ptr_2, operation);
2367            }
2368          }
2369    
2370        }
2371        else if (arg_0_Z.isExpanded()   && arg_1_Z.isExpanded()) {
2372          // After finding a common function space above the two inputs have the same numSamples and num DPPS
2373          res = Data(0.0, shape0, arg_1_Z.getFunctionSpace(),true); // DataExpanded output
2374          DataExpanded* tmp_0=dynamic_cast<DataExpanded*>(arg_0_Z.borrowData());
2375          DataExpanded* tmp_1=dynamic_cast<DataExpanded*>(arg_1_Z.borrowData());
2376          DataExpanded* tmp_2=dynamic_cast<DataExpanded*>(res.borrowData());
2377    
2378          int sampleNo_0,dataPointNo_0;
2379          int numSamples_0 = arg_0_Z.getNumSamples();
2380          int numDataPointsPerSample_0 = arg_0_Z.getNumDataPointsPerSample();
2381          res.requireWrite();
2382          #pragma omp parallel for private(sampleNo_0,dataPointNo_0) schedule(static)
2383          for (sampleNo_0 = 0; sampleNo_0 < numSamples_0; sampleNo_0++) {
2384            for (dataPointNo_0 = 0; dataPointNo_0 < numDataPointsPerSample_0; dataPointNo_0++) {
2385              int offset_0 = tmp_0->getPointOffset(sampleNo_0,dataPointNo_0);
2386              int offset_1 = tmp_1->getPointOffset(sampleNo_0,dataPointNo_0);
2387              int offset_2 = tmp_2->getPointOffset(sampleNo_0,dataPointNo_0);
2388              const double *ptr_0 = &(arg_0_Z.getDataAtOffsetRO(offset_0));
2389              const double *ptr_1 = &(arg_1_Z.getDataAtOffsetRO(offset_1));
2390              double *ptr_2 = &(res.getDataAtOffsetRW(offset_2));
2391              tensor_binary_operation(size0, ptr_0, ptr_1, ptr_2, operation);
2392            }
2393          }
2394    
2395        }
2396        else {
2397          throw DataException("Error - C_TensorBinaryOperation: unknown combination of inputs");
2398        }
2399    
2400      } else if (0 == rank0) {
2401        if (arg_0_Z.isConstant()   && arg_1_Z.isConstant()) {
2402          res = Data(0.0, shape1, arg_1_Z.getFunctionSpace());      // DataConstant output
2403          const double *ptr_0 = &(arg_0_Z.getDataAtOffsetRO(0));
2404          const double *ptr_1 = &(arg_1_Z.getDataAtOffsetRO(0));
2405          double *ptr_2 = &(res.getDataAtOffsetRW(0));
2406          tensor_binary_operation(size1, ptr_0[0], ptr_1, ptr_2, operation);
2407        }
2408        else if (arg_0_Z.isConstant()   && arg_1_Z.isTagged()) {
2409    
2410          // Prepare the DataConstant input
2411          DataConstant* tmp_0=dynamic_cast<DataConstant*>(arg_0_Z.borrowData());
2412    
2413          // Borrow DataTagged input from Data object
2414          DataTagged* tmp_1=dynamic_cast<DataTagged*>(arg_1_Z.borrowData());
2415    
2416          // Prepare a DataTagged output 2
2417          res = Data(0.0, shape1, arg_1_Z.getFunctionSpace());      // DataTagged output
2418          res.tag();
2419          DataTagged* tmp_2=dynamic_cast<DataTagged*>(res.borrowData());
2420    
2421          // Prepare offset into DataConstant
2422          int offset_0 = tmp_0->getPointOffset(0,0);
2423          const double *ptr_0 = &(arg_0_Z.getDataAtOffsetRO(offset_0));
2424    
2425          const double *ptr_1 = &(tmp_1->getDefaultValueRO(0));
2426          double *ptr_2 = &(tmp_2->getDefaultValueRW(0));
2427    
2428          // Compute a result for the default
2429          tensor_binary_operation(size1, ptr_0[0], ptr_1, ptr_2, operation);
2430          // Compute a result for each tag
2431          const DataTagged::DataMapType& lookup_1=tmp_1->getTagLookup();
2432          DataTagged::DataMapType::const_iterator i; // i->first is a tag, i->second is an offset into memory
2433          for (i=lookup_1.begin();i!=lookup_1.end();i++) {
2434            tmp_2->addTag(i->first);
2435            const double *ptr_1 = &(tmp_1->getDataByTagRO(i->first,0));
2436            double *ptr_2 = &(tmp_2->getDataByTagRW(i->first,0));
2437            tensor_binary_operation(size1, ptr_0[0], ptr_1, ptr_2, operation);
2438          }
2439    
2440        }
2441        else if (arg_0_Z.isConstant()   && arg_1_Z.isExpanded()) {
2442    
2443          res = Data(0.0, shape1, arg_1_Z.getFunctionSpace(),true); // DataExpanded output
2444          DataConstant* tmp_0=dynamic_cast<DataConstant*>(arg_0_Z.borrowData());
2445          DataExpanded* tmp_1=dynamic_cast<DataExpanded*>(arg_1_Z.borrowData());
2446          DataExpanded* tmp_2=dynamic_cast<DataExpanded*>(res.borrowData());
2447    
2448          int sampleNo_1,dataPointNo_1;
2449          int numSamples_1 = arg_1_Z.getNumSamples();
2450          int numDataPointsPerSample_1 = arg_1_Z.getNumDataPointsPerSample();
2451          int offset_0 = tmp_0->getPointOffset(0,0);
2452          res.requireWrite();
2453          #pragma omp parallel for private(sampleNo_1,dataPointNo_1) schedule(static)
2454          for (sampleNo_1 = 0; sampleNo_1 < numSamples_1; sampleNo_1++) {
2455            for (dataPointNo_1 = 0; dataPointNo_1 < numDataPointsPerSample_1; dataPointNo_1++) {
2456              int offset_1 = tmp_1->getPointOffset(sampleNo_1,dataPointNo_1);
2457              int offset_2 = tmp_2->getPointOffset(sampleNo_1,dataPointNo_1);
2458              const double *ptr_0 = &(arg_0_Z.getDataAtOffsetRO(offset_0));
2459              const double *ptr_1 = &(arg_1_Z.getDataAtOffsetRO(offset_1));
2460              double *ptr_2 = &(res.getDataAtOffsetRW(offset_2));
2461              tensor_binary_operation(size1, ptr_0[0], ptr_1, ptr_2, operation);
2462    
2463            }
2464          }
2465    
2466        }
2467        else if (arg_0_Z.isTagged()     && arg_1_Z.isConstant()) {
2468    
2469          // Borrow DataTagged input from Data object
2470          DataTagged* tmp_0=dynamic_cast<DataTagged*>(arg_0_Z.borrowData());
2471    
2472          // Prepare the DataConstant input
2473          DataConstant* tmp_1=dynamic_cast<DataConstant*>(arg_1_Z.borrowData());
2474    
2475          // Prepare a DataTagged output 2
2476          res = Data(0.0, shape1, arg_0_Z.getFunctionSpace());      // DataTagged output
2477          res.tag();
2478          DataTagged* tmp_2=dynamic_cast<DataTagged*>(res.borrowData());
2479    
2480          // Prepare offset into DataConstant
2481          int offset_1 = tmp_1->getPointOffset(0,0);
2482          const double *ptr_1 = &(arg_1_Z.getDataAtOffsetRO(offset_1));
2483    
2484          // Get the pointers to the actual data
2485          const double *ptr_0 = &(tmp_0->getDefaultValueRO(0));
2486          double *ptr_2 = &(tmp_2->getDefaultValueRW(0));
2487    
2488    
2489          // Compute a result for the default
2490          tensor_binary_operation(size1, ptr_0[0], ptr_1, ptr_2, operation);
2491          // Compute a result for each tag
2492          const DataTagged::DataMapType& lookup_0=tmp_0->getTagLookup();
2493          DataTagged::DataMapType::const_iterator i; // i->first is a tag, i->second is an offset into memory
2494          for (i=lookup_0.begin();i!=lookup_0.end();i++) {
2495            tmp_2->addTag(i->first);
2496            const double *ptr_0 = &(tmp_0->getDataByTagRO(i->first,0));
2497            double *ptr_2 = &(tmp_2->getDataByTagRW(i->first,0));
2498    
2499            tensor_binary_operation(size1, ptr_0[0], ptr_1, ptr_2, operation);
2500          }
2501    
2502        }
2503        else if (arg_0_Z.isTagged()     && arg_1_Z.isTagged()) {
2504    
2505          // Borrow DataTagged input from Data object
2506          DataTagged* tmp_0=dynamic_cast<DataTagged*>(arg_0_Z.borrowData());
2507    
2508          // Borrow DataTagged input from Data object
2509          DataTagged* tmp_1=dynamic_cast<DataTagged*>(arg_1_Z.borrowData());
2510    
2511          // Prepare a DataTagged output 2
2512          res = Data(0.0, shape1, arg_1_Z.getFunctionSpace());
2513          res.tag();        // DataTagged output
2514          DataTagged* tmp_2=dynamic_cast<DataTagged*>(res.borrowData());
2515    
2516          // Get the pointers to the actual data
2517          const double *ptr_0 = &(tmp_0->getDefaultValueRO(0));
2518          const double *ptr_1 = &(tmp_1->getDefaultValueRO(0));
2519          double *ptr_2 = &(tmp_2->getDefaultValueRW(0));
2520    
2521          // Compute a result for the default
2522          tensor_binary_operation(size1, ptr_0[0], ptr_1, ptr_2, operation);
2523          // Merge the tags
2524          DataTagged::DataMapType::const_iterator i; // i->first is a tag, i->second is an offset into memory
2525          const DataTagged::DataMapType& lookup_0=tmp_0->getTagLookup();
2526          const DataTagged::DataMapType& lookup_1=tmp_1->getTagLookup();
2527          for (i=lookup_0.begin();i!=lookup_0.end();i++) {
2528            tmp_2->addTag(i->first); // use tmp_2 to get correct shape
2529          }
2530          for (i=lookup_1.begin();i!=lookup_1.end();i++) {
2531            tmp_2->addTag(i->first);
2532          }
2533          // Compute a result for each tag
2534          const DataTagged::DataMapType& lookup_2=tmp_2->getTagLookup();
2535          for (i=lookup_2.begin();i!=lookup_2.end();i++) {
2536    
2537    /*        DataArrayView view_0 = tmp_0->getDataPointByTag(i->first);
2538            DataArrayView view_1 = tmp_1->getDataPointByTag(i->first);
2539            DataArrayView view_2 = tmp_2->getDataPointByTag(i->first);
2540            double *ptr_0 = &view_0.getData(0);
2541            double *ptr_1 = &view_1.getData(0);
2542            double *ptr_2 = &view_2.getData(0);*/
2543    
2544            const double *ptr_0 = &(tmp_0->getDataByTagRO(i->first,0));
2545            const double *ptr_1 = &(tmp_1->getDataByTagRO(i->first,0));
2546            double *ptr_2 = &(tmp_2->getDataByTagRW(i->first,0));
2547    
2548            tensor_binary_operation(size1, ptr_0[0], ptr_1, ptr_2, operation);
2549          }
2550    
2551        }
2552        else if (arg_0_Z.isTagged()     && arg_1_Z.isExpanded()) {
2553    
2554          // After finding a common function space above the two inputs have the same numSamples and num DPPS
2555          res = Data(0.0, shape1, arg_1_Z.getFunctionSpace(),true); // DataExpanded output
2556          DataTagged*   tmp_0=dynamic_cast<DataTagged*>(arg_0_Z.borrowData());
2557          DataExpanded* tmp_1=dynamic_cast<DataExpanded*>(arg_1_Z.borrowData());
2558          DataExpanded* tmp_2=dynamic_cast<DataExpanded*>(res.borrowData());
2559    
2560          int sampleNo_0,dataPointNo_0;
2561          int numSamples_0 = arg_0_Z.getNumSamples();
2562          int numDataPointsPerSample_0 = arg_0_Z.getNumDataPointsPerSample();
2563          res.requireWrite();
2564          #pragma omp parallel for private(sampleNo_0,dataPointNo_0) schedule(static)
2565          for (sampleNo_0 = 0; sampleNo_0 < numSamples_0; sampleNo_0++) {
2566            int offset_0 = tmp_0->getPointOffset(sampleNo_0,0); // They're all the same, so just use #0
2567            const double *ptr_0 = &(arg_0_Z.getDataAtOffsetRO(offset_0));
2568            for (dataPointNo_0 = 0; dataPointNo_0 < numDataPointsPerSample_0; dataPointNo_0++) {
2569              int offset_1 = tmp_1->getPointOffset(sampleNo_0,dataPointNo_0);
2570              int offset_2 = tmp_2->getPointOffset(sampleNo_0,dataPointNo_0);
2571              const double *ptr_1 = &(arg_1_Z.getDataAtOffsetRO(offset_1));
2572              double *ptr_2 = &(res.getDataAtOffsetRW(offset_2));
2573              tensor_binary_operation(size1, ptr_0[0], ptr_1, ptr_2, operation);
2574            }
2575          }
2576    
2577        }
2578        else if (arg_0_Z.isExpanded()   && arg_1_Z.isConstant()) {
2579          res = Data(0.0, shape1, arg_1_Z.getFunctionSpace(),true); // DataExpanded output
2580          DataExpanded* tmp_0=dynamic_cast<DataExpanded*>(arg_0_Z.borrowData());
2581          DataConstant* tmp_1=dynamic_cast<DataConstant*>(arg_1_Z.borrowData());
2582          DataExpanded* tmp_2=dynamic_cast<DataExpanded*>(res.borrowData());
2583    
2584          int sampleNo_0,dataPointNo_0;
2585          int numSamples_0 = arg_0_Z.getNumSamples();
2586          int numDataPointsPerSample_0 = arg_0_Z.getNumDataPointsPerSample();
2587          int offset_1 = tmp_1->getPointOffset(0,0);
2588          res.requireWrite();
2589          #pragma omp parallel for private(sampleNo_0,dataPointNo_0) schedule(static)
2590          for (sampleNo_0 = 0; sampleNo_0 < numSamples_0; sampleNo_0++) {
2591            for (dataPointNo_0 = 0; dataPointNo_0 < numDataPointsPerSample_0; dataPointNo_0++) {
2592              int offset_0 = tmp_0->getPointOffset(sampleNo_0,dataPointNo_0);
2593              int offset_2 = tmp_2->getPointOffset(sampleNo_0,dataPointNo_0);
2594              const double *ptr_0 = &(arg_0_Z.getDataAtOffsetRO(offset_0));
2595              const double *ptr_1 = &(arg_1_Z.getDataAtOffsetRO(offset_1));
2596              double *ptr_2 = &(res.getDataAtOffsetRW(offset_2));
2597              tensor_binary_operation(size1, ptr_0[0], ptr_1, ptr_2, operation);
2598            }
2599          }
2600    
2601    
2602        }
2603        else if (arg_0_Z.isExpanded()   && arg_1_Z.isTagged()) {
2604    
2605          // After finding a common function space above the two inputs have the same numSamples and num DPPS
2606          res = Data(0.0, shape1, arg_1_Z.getFunctionSpace(),true); // DataExpanded output
2607          DataExpanded* tmp_0=dynamic_cast<DataExpanded*>(arg_0_Z.borrowData());
2608          DataTagged*   tmp_1=dynamic_cast<DataTagged*>(arg_1_Z.borrowData());
2609          DataExpanded* tmp_2=dynamic_cast<DataExpanded*>(res.borrowData());
2610    
2611          int sampleNo_0,dataPointNo_0;
2612          int numSamples_0 = arg_0_Z.getNumSamples();
2613          int numDataPointsPerSample_0 = arg_0_Z.getNumDataPointsPerSample();
2614          res.requireWrite();
2615          #pragma omp parallel for private(sampleNo_0,dataPointNo_0) schedule(static)
2616          for (sampleNo_0 = 0; sampleNo_0 < numSamples_0; sampleNo_0++) {
2617            int offset_1 = tmp_1->getPointOffset(sampleNo_0,0);
2618            const double *ptr_1 = &(arg_1_Z.getDataAtOffsetRO(offset_1));
2619            for (dataPointNo_0 = 0; dataPointNo_0 < numDataPointsPerSample_0; dataPointNo_0++) {
2620              int offset_0 = tmp_0->getPointOffset(sampleNo_0,dataPointNo_0);
2621              int offset_2 = tmp_2->getPointOffset(sampleNo_0,dataPointNo_0);
2622              const double *ptr_0 = &(arg_0_Z.getDataAtOffsetRO(offset_0));
2623              double *ptr_2 = &(res.getDataAtOffsetRW(offset_2));
2624              tensor_binary_operation(size1, ptr_0[0], ptr_1, ptr_2, operation);
2625            }
2626          }
2627    
2628        }
2629        else if (arg_0_Z.isExpanded()   && arg_1_Z.isExpanded()) {
2630    
2631          // After finding a common function space above the two inputs have the same numSamples and num DPPS
2632          res = Data(0.0, shape1, arg_1_Z.getFunctionSpace(),true); // DataExpanded output
2633          DataExpanded* tmp_0=dynamic_cast<DataExpanded*>(arg_0_Z.borrowData());
2634          DataExpanded* tmp_1=dynamic_cast<DataExpanded*>(arg_1_Z.borrowData());
2635          DataExpanded* tmp_2=dynamic_cast<DataExpanded*>(res.borrowData());
2636    
2637          int sampleNo_0,dataPointNo_0;
2638          int numSamples_0 = arg_0_Z.getNumSamples();
2639          int numDataPointsPerSample_0 = arg_0_Z.getNumDataPointsPerSample();
2640          res.requireWrite();
2641          #pragma omp parallel for private(sampleNo_0,dataPointNo_0) schedule(static)
2642          for (sampleNo_0 = 0; sampleNo_0 < numSamples_0; sampleNo_0++) {
2643            for (dataPointNo_0 = 0; dataPointNo_0 < numDataPointsPerSample_0; dataPointNo_0++) {
2644              int offset_0 = tmp_0->getPointOffset(sampleNo_0,dataPointNo_0);
2645              int offset_1 = tmp_1->getPointOffset(sampleNo_0,dataPointNo_0);
2646              int offset_2 = tmp_2->getPointOffset(sampleNo_0,dataPointNo_0);
2647              const double *ptr_0 = &(arg_0_Z.getDataAtOffsetRO(offset_0));
2648              const double *ptr_1 = &(arg_1_Z.getDataAtOffsetRO(offset_1));
2649              double *ptr_2 = &(res.getDataAtOffsetRW(offset_2));
2650              tensor_binary_operation(size1, ptr_0[0], ptr_1, ptr_2, operation);
2651            }
2652          }
2653    
2654        }
2655        else {
2656          throw DataException("Error - C_TensorBinaryOperation: unknown combination of inputs");
2657        }
2658    
2659      } else if (0 == rank1) {
2660        if (arg_0_Z.isConstant()   && arg_1_Z.isConstant()) {
2661          res = Data(0.0, shape0, arg_1_Z.getFunctionSpace());      // DataConstant output
2662          const double *ptr_0 = &(arg_0_Z.getDataAtOffsetRO(0));
2663          const double *ptr_1 = &(arg_1_Z.getDataAtOffsetRO(0));
2664          double *ptr_2 = &(res.getDataAtOffsetRW(0));
2665          tensor_binary_operation(size0, ptr_0, ptr_1[0], ptr_2, operation);
2666        }
2667        else if (arg_0_Z.isConstant()   && arg_1_Z.isTagged()) {
2668    
2669          // Prepare the DataConstant input
2670          DataConstant* tmp_0=dynamic_cast<DataConstant*>(arg_0_Z.borrowData());
2671    
2672          // Borrow DataTagged input from Data object
2673          DataTagged* tmp_1=dynamic_cast<DataTagged*>(arg_1_Z.borrowData());
2674    
2675          // Prepare a DataTagged output 2
2676          res = Data(0.0, shape0, arg_1_Z.getFunctionSpace());      // DataTagged output
2677          res.tag();
2678          DataTagged* tmp_2=dynamic_cast<DataTagged*>(res.borrowData());
2679    
2680          // Prepare offset into DataConstant
2681          int offset_0 = tmp_0->getPointOffset(0,0);
2682          const double *ptr_0 = &(arg_0_Z.getDataAtOffsetRO(offset_0));
2683    
2684          //Get the pointers to the actual data
2685          const double *ptr_1 = &(tmp_1->getDefaultValueRO(0));
2686          double *ptr_2 = &(tmp_2->getDefaultValueRW(0));
2687    
2688          // Compute a result for the default
2689          tensor_binary_operation(size0, ptr_0, ptr_1[0], ptr_2, operation);
2690          // Compute a result for each tag
2691          const DataTagged::DataMapType& lookup_1=tmp_1->getTagLookup();
2692          DataTagged::DataMapType::const_iterator i; // i->first is a tag, i->second is an offset into memory
2693          for (i=lookup_1.begin();i!=lookup_1.end();i++) {
2694            tmp_2->addTag(i->first);
2695            const double *ptr_1 = &(tmp_1->getDataByTagRO(i->first,0));
2696            double *ptr_2 = &(tmp_2->getDataByTagRW(i->first,0));
2697            tensor_binary_operation(size0, ptr_0, ptr_1[0], ptr_2, operation);
2698          }
2699        }
2700        else if (arg_0_Z.isConstant()   && arg_1_Z.isExpanded()) {
2701    
2702          res = Data(0.0, shape0, arg_1_Z.getFunctionSpace(),true); // DataExpanded output
2703          DataConstant* tmp_0=dynamic_cast<DataConstant*>(arg_0_Z.borrowData());
2704          DataExpanded* tmp_1=dynamic_cast<DataExpanded*>(arg_1_Z.borrowData());
2705          DataExpanded* tmp_2=dynamic_cast<DataExpanded*>(res.borrowData());
2706    
2707          int sampleNo_1,dataPointNo_1;
2708          int numSamples_1 = arg_1_Z.getNumSamples();
2709          int numDataPointsPerSample_1 = arg_1_Z.getNumDataPointsPerSample();
2710          int offset_0 = tmp_0->getPointOffset(0,0);
2711          res.requireWrite();
2712          #pragma omp parallel for private(sampleNo_1,dataPointNo_1) schedule(static)
2713          for (sampleNo_1 = 0; sampleNo_1 < numSamples_1; sampleNo_1++) {
2714            for (dataPointNo_1 = 0; dataPointNo_1 < numDataPointsPerSample_1; dataPointNo_1++) {
2715              int offset_1 = tmp_1->getPointOffset(sampleNo_1,dataPointNo_1);
2716              int offset_2 = tmp_2->getPointOffset(sampleNo_1,dataPointNo_1);
2717              const double *ptr_0 = &(arg_0_Z.getDataAtOffsetRO(offset_0));
2718              const double *ptr_1 = &(arg_1_Z.getDataAtOffsetRO(offset_1));
2719              double *ptr_2 = &(res.getDataAtOffsetRW(offset_2));
2720              tensor_binary_operation(size0, ptr_0, ptr_1[0], ptr_2, operation);
2721            }
2722          }
2723    
2724        }
2725        else if (arg_0_Z.isTagged()     && arg_1_Z.isConstant()) {
2726    
2727          // Borrow DataTagged input from Data object
2728          DataTagged* tmp_0=dynamic_cast<DataTagged*>(arg_0_Z.borrowData());
2729    
2730          // Prepare the DataConstant input
2731          DataConstant* tmp_1=dynamic_cast<DataConstant*>(arg_1_Z.borrowData());
2732    
2733          // Prepare a DataTagged output 2
2734          res = Data(0.0, shape0, arg_0_Z.getFunctionSpace());      // DataTagged output
2735          res.tag();
2736          DataTagged* tmp_2=dynamic_cast<DataTagged*>(res.borrowData());
2737    
2738          // Prepare offset into DataConstant
2739          int offset_1 = tmp_1->getPointOffset(0,0);
2740          const double *ptr_1 = &(arg_1_Z.getDataAtOffsetRO(offset_1));
2741          // Get the pointers to the actual data
2742          const double *ptr_0 = &(tmp_0->getDefaultValueRO(0));
2743          double *ptr_2 = &(tmp_2->getDefaultValueRW(0));
2744          // Compute a result for the default
2745          tensor_binary_operation(size0, ptr_0, ptr_1[0], ptr_2, operation);
2746          // Compute a result for each tag
2747          const DataTagged::DataMapType& lookup_0=tmp_0->getTagLookup();
2748          DataTagged::DataMapType::const_iterator i; // i->first is a tag, i->second is an offset into memory
2749          for (i=lookup_0.begin();i!=lookup_0.end();i++) {
2750            tmp_2->addTag(i->first);
2751    /*        DataArrayView view_0 = tmp_0->getDataPointByTag(i->first);
2752            DataArrayView view_2 = tmp_2->getDataPointByTag(i->first);
2753            double *ptr_0 = &view_0.getData(0);
2754            double *ptr_2 = &view_2.getData(0);*/
2755            const double *ptr_0 = &(tmp_0->getDataByTagRO(i->first,0));
2756            double *ptr_2 = &(tmp_2->getDataByTagRW(i->first,0));
2757            tensor_binary_operation(size0, ptr_0, ptr_1[0], ptr_2, operation);
2758          }
2759    
2760        }
2761        else if (arg_0_Z.isTagged()     && arg_1_Z.isTagged()) {
2762    
2763          // Borrow DataTagged input from Data object
2764          DataTagged* tmp_0=dynamic_cast<DataTagged*>(arg_0_Z.borrowData());
2765    
2766          // Borrow DataTagged input from Data object
2767          DataTagged* tmp_1=dynamic_cast<DataTagged*>(arg_1_Z.borrowData());
2768    
2769          // Prepare a DataTagged output 2
2770          res = Data(0.0, shape0, arg_1_Z.getFunctionSpace());
2771          res.tag();        // DataTagged output
2772          DataTagged* tmp_2=dynamic_cast<DataTagged*>(res.borrowData());
2773    
2774          // Get the pointers to the actual data
2775          const double *ptr_0 = &(tmp_0->getDefaultValueRO(0));
2776          const double *ptr_1 = &(tmp_1->getDefaultValueRO(0));
2777          double *ptr_2 = &(tmp_2->getDefaultValueRW(0));
2778    
2779          // Compute a result for the default
2780          tensor_binary_operation(size0, ptr_0, ptr_1[0], ptr_2, operation);
2781          // Merge the tags
2782          DataTagged::DataMapType::const_iterator i; // i->first is a tag, i->second is an offset into memory
2783          const DataTagged::DataMapType& lookup_0=tmp_0->getTagLookup();
2784          const DataTagged::DataMapType& lookup_1=tmp_1->getTagLookup();
2785          for (i=lookup_0.begin();i!=lookup_0.end();i++) {
2786            tmp_2->addTag(i->first); // use tmp_2 to get correct shape
2787          }
2788          for (i=lookup_1.begin();i!=lookup_1.end();i++) {
2789            tmp_2->addTag(i->first);
2790          }
2791          // Compute a result for each tag
2792          const DataTagged::DataMapType& lookup_2=tmp_2->getTagLookup();
2793          for (i=lookup_2.begin();i!=lookup_2.end();i++) {
2794    //         DataArrayView view_0 = tmp_0->getDataPointByTag(i->first);
2795    //         DataArrayView view_1 = tmp_1->getDataPointByTag(i->first);
2796    //         DataArrayView view_2 = tmp_2->getDataPointByTag(i->first);
2797    //         double *ptr_0 = &view_0.getData(0);
2798    //         double *ptr_1 = &view_1.getData(0);
2799    //         double *ptr_2 = &view_2.getData(0);
2800    
2801            const double *ptr_0 = &(tmp_0->getDataByTagRO(i->first,0));
2802            const double *ptr_1 = &(tmp_1->getDataByTagRO(i->first,0));
2803            double *ptr_2 = &(tmp_2->getDataByTagRW(i->first,0));
2804            tensor_binary_operation(size0, ptr_0, ptr_1[0], ptr_2, operation);
2805          }
2806    
2807        }
2808        else if (arg_0_Z.isTagged()     && arg_1_Z.isExpanded()) {
2809    
2810          // After finding a common function space above the two inputs have the same numSamples and num DPPS
2811          res = Data(0.0, shape0, arg_1_Z.getFunctionSpace(),true); // DataExpanded output
2812          DataTagged*   tmp_0=dynamic_cast<DataTagged*>(arg_0_Z.borrowData());
2813          DataExpanded* tmp_1=dynamic_cast<DataExpanded*>(arg_1_Z.borrowData());
2814          DataExpanded* tmp_2=dynamic_cast<DataExpanded*>(res.borrowData());
2815    
2816          int sampleNo_0,dataPointNo_0;
2817          int numSamples_0 = arg_0_Z.getNumSamples();
2818          int numDataPointsPerSample_0 = arg_0_Z.getNumDataPointsPerSample();
2819          res.requireWrite();
2820          #pragma omp parallel for private(sampleNo_0,dataPointNo_0) schedule(static)
2821          for (sampleNo_0 = 0; sampleNo_0 < numSamples_0; sampleNo_0++) {
2822            int offset_0 = tmp_0->getPointOffset(sampleNo_0,0); // They're all the same, so just use #0
2823            const double *ptr_0 = &(arg_0_Z.getDataAtOffsetRO(offset_0));
2824            for (dataPointNo_0 = 0; dataPointNo_0 < numDataPointsPerSample_0; dataPointNo_0++) {
2825              int offset_1 = tmp_1->getPointOffset(sampleNo_0,dataPointNo_0);
2826              int offset_2 = tmp_2->getPointOffset(sampleNo_0,dataPointNo_0);
2827              const double *ptr_1 = &(arg_1_Z.getDataAtOffsetRO(offset_1));
2828              double *ptr_2 = &(res.getDataAtOffsetRW(offset_2));
2829              tensor_binary_operation(size0, ptr_0, ptr_1[0], ptr_2, operation);
2830            }
2831          }
2832    
2833        }
2834        else if (arg_0_Z.isExpanded()   && arg_1_Z.isConstant()) {
2835          res = Data(0.0, shape0, arg_1_Z.getFunctionSpace(),true); // DataExpanded output
2836          DataExpanded* tmp_0=dynamic_cast<DataExpanded*>(arg_0_Z.borrowData());
2837          DataConstant* tmp_1=dynamic_cast<DataConstant*>(arg_1_Z.borrowData());
2838          DataExpanded* tmp_2=dynamic_cast<DataExpanded*>(res.borrowData());
2839    
2840          int sampleNo_0,dataPointNo_0;
2841          int numSamples_0 = arg_0_Z.getNumSamples();
2842          int numDataPointsPerSample_0 = arg_0_Z.getNumDataPointsPerSample();
2843          int offset_1 = tmp_1->getPointOffset(0,0);
2844          res.requireWrite();
2845          #pragma omp parallel for private(sampleNo_0,dataPointNo_0) schedule(static)
2846          for (sampleNo_0 = 0; sampleNo_0 < numSamples_0; sampleNo_0++) {
2847            for (dataPointNo_0 = 0; dataPointNo_0 < numDataPointsPerSample_0; dataPointNo_0++) {
2848              int offset_0 = tmp_0->getPointOffset(sampleNo_0,dataPointNo_0);
2849              int offset_2 = tmp_2->getPointOffset(sampleNo_0,dataPointNo_0);
2850              const double *ptr_0 = &(arg_0_Z.getDataAtOffsetRO(offset_0));
2851              const double *ptr_1 = &(arg_1_Z.getDataAtOffsetRO(offset_1));
2852              double *ptr_2 = &(res.getDataAtOffsetRW(offset_2));
2853              tensor_binary_operation(size0, ptr_0, ptr_1[0], ptr_2, operation);
2854            }
2855          }
2856    
2857    
2858        }
2859        else if (arg_0_Z.isExpanded()   && arg_1_Z.isTagged()) {
2860    
2861          // After finding a common function space above the two inputs have the same numSamples and num DPPS
2862          res = Data(0.0, shape0, arg_1_Z.getFunctionSpace(),true); // DataExpanded output
2863          DataExpanded* tmp_0=dynamic_cast<DataExpanded*>(arg_0_Z.borrowData());
2864          DataTagged*   tmp_1=dynamic_cast<DataTagged*>(arg_1_Z.borrowData());
2865          DataExpanded* tmp_2=dynamic_cast<DataExpanded*>(res.borrowData());
2866    
2867          int sampleNo_0,dataPointNo_0;
2868          int numSamples_0 = arg_0_Z.getNumSamples();
2869          int numDataPointsPerSample_0 = arg_0_Z.getNumDataPointsPerSample();
2870          res.requireWrite();
2871          #pragma omp parallel for private(sampleNo_0,dataPointNo_0) schedule(static)
2872          for (sampleNo_0 = 0; sampleNo_0 < numSamples_0; sampleNo_0++) {
2873            int offset_1 = tmp_1->getPointOffset(sampleNo_0,0);
2874            const double *ptr_1 = &(arg_1_Z.getDataAtOffsetRO(offset_1));
2875            for (dataPointNo_0 = 0; dataPointNo_0 < numDataPointsPerSample_0; dataPointNo_0++) {
2876              int offset_0 = tmp_0->getPointOffset(sampleNo_0,dataPointNo_0);
2877              int offset_2 = tmp_2->getPointOffset(sampleNo_0,dataPointNo_0);
2878              const double *ptr_0 = &(arg_0_Z.getDataAtOffsetRO(offset_0));
2879              double *ptr_2 = &(res.getDataAtOffsetRW(offset_2));
2880              tensor_binary_operation(size0, ptr_0, ptr_1[0], ptr_2, operation);
2881            }
2882          }
2883    
2884        }
2885        else if (arg_0_Z.isExpanded()   && arg_1_Z.isExpanded()) {
2886    
2887          // After finding a common function space above the two inputs have the same numSamples and num DPPS
2888          res = Data(0.0, shape0, arg_1_Z.getFunctionSpace(),true); // DataExpanded output
2889          DataExpanded* tmp_0=dynamic_cast<DataExpanded*>(arg_0_Z.borrowData());
2890          DataExpanded* tmp_1=dynamic_cast<DataExpanded*>(arg_1_Z.borrowData());
2891          DataExpanded* tmp_2=dynamic_cast<DataExpanded*>(res.borrowData());
2892    
2893          int sampleNo_0,dataPointNo_0;
2894          int numSamples_0 = arg_0_Z.getNumSamples();
2895          int numDataPointsPerSample_0 = arg_0_Z.getNumDataPointsPerSample();
2896          res.requireWrite();
2897          #pragma omp parallel for private(sampleNo_0,dataPointNo_0) schedule(static)
2898          for (sampleNo_0 = 0; sampleNo_0 < numSamples_0; sampleNo_0++) {
2899            for (dataPointNo_0 = 0; dataPointNo_0 < numDataPointsPerSample_0; dataPointNo_0++) {
2900              int offset_0 = tmp_0->getPointOffset(sampleNo_0,dataPointNo_0);
2901              int offset_1 = tmp_1->getPointOffset(sampleNo_0,dataPointNo_0);
2902              int offset_2 = tmp_2->getPointOffset(sampleNo_0,dataPointNo_0);
2903              const double *ptr_0 = &(arg_0_Z.getDataAtOffsetRO(offset_0));
2904              const double *ptr_1 = &(arg_1_Z.getDataAtOffsetRO(offset_1));
2905              double *ptr_2 = &(res.getDataAtOffsetRW(offset_2));
2906              tensor_binary_operation(size0, ptr_0, ptr_1[0], ptr_2, operation);
2907            }
2908          }
2909    
2910        }
2911        else {
2912          throw DataException("Error - C_TensorBinaryOperation: unknown combination of inputs");
2913        }
2914    
2915      } else {
2916        throw DataException("Error - C_TensorBinaryOperation: arguments have incompatible shapes");
2917    }    }
2918    Data falseRetVal; // to keep compiler quiet  
2919    return falseRetVal;    return res;
2920    }
2921    
2922    template <typename UnaryFunction>
2923    Data
2924    C_TensorUnaryOperation(Data const &arg_0,
2925                           UnaryFunction operation)
2926    {
2927      if (arg_0.isEmpty())  // do this before we attempt to interpolate
2928      {
2929         throw DataException("Error - Operations not permitted on instances of DataEmpty.");
2930      }
2931      if (arg_0.isLazy())
2932      {
2933         throw DataException("Error - Operations not permitted on lazy data.");
2934      }
2935      // Interpolate if necessary and find an appropriate function space
2936      Data arg_0_Z = Data(arg_0);
2937    
2938      // Get rank and shape of inputs
2939    //  int rank0 = arg_0_Z.getDataPointRank();
2940      const DataTypes::ShapeType& shape0 = arg_0_Z.getDataPointShape();
2941      int size0 = arg_0_Z.getDataPointSize();
2942    
2943      // Declare output Data object
2944      Data res;
2945    
2946      if (arg_0_Z.isConstant()) {
2947        res = Data(0.0, shape0, arg_0_Z.getFunctionSpace());      // DataConstant output
2948    //     double *ptr_0 = &((arg_0_Z.getPointDataView().getData())[0]);
2949    //     double *ptr_2 = &((res.getPointDataView().getData())[0]);
2950        const double *ptr_0 = &(arg_0_Z.getDataAtOffsetRO(0));
2951        double *ptr_2 = &(res.getDataAtOffsetRW(0));
2952        tensor_unary_operation(size0, ptr_0, ptr_2, operation);
2953      }
2954      else if (arg_0_Z.isTagged()) {
2955    
2956        // Borrow DataTagged input from Data object
2957        DataTagged* tmp_0=dynamic_cast<DataTagged*>(arg_0_Z.borrowData());
2958    
2959        // Prepare a DataTagged output 2
2960        res = Data(0.0, shape0, arg_0_Z.getFunctionSpace());   // DataTagged output
2961        res.tag();
2962        DataTagged* tmp_2=dynamic_cast<DataTagged*>(res.borrowData());
2963    
2964        // Get the pointers to the actual data
2965        const double *ptr_0 = &(tmp_0->getDefaultValueRO(0));
2966        double *ptr_2 = &(tmp_2->getDefaultValueRW(0));
2967        // Compute a result for the default
2968        tensor_unary_operation(size0, ptr_0, ptr_2, operation);
2969        // Compute a result for each tag
2970        const DataTagged::DataMapType& lookup_0=tmp_0->getTagLookup();
2971        DataTagged::DataMapType::const_iterator i; // i->first is a tag, i->second is an offset into memory
2972        for (i=lookup_0.begin();i!=lookup_0.end();i++) {
2973          tmp_2->addTag(i->first);
2974          const double *ptr_0 = &(tmp_0->getDataByTagRO(i->first,0));
2975          double *ptr_2 = &(tmp_2->getDataByTagRW(i->first,0));
2976          tensor_unary_operation(size0, ptr_0, ptr_2, operation);
2977        }
2978    
2979      }
2980      else if (arg_0_Z.isExpanded()) {
2981    
2982        res = Data(0.0, shape0, arg_0_Z.getFunctionSpace(),true); // DataExpanded output
2983        DataExpanded* tmp_0=dynamic_cast<DataExpanded*>(arg_0_Z.borrowData());
2984        DataExpanded* tmp_2=dynamic_cast<DataExpanded*>(res.borrowData());
2985    
2986        int sampleNo_0,dataPointNo_0;
2987        int numSamples_0 = arg_0_Z.getNumSamples();
2988        int numDataPointsPerSample_0 = arg_0_Z.getNumDataPointsPerSample();
2989        #pragma omp parallel for private(sampleNo_0,dataPointNo_0) schedule(static)
2990        for (sampleNo_0 = 0; sampleNo_0 < numSamples_0; sampleNo_0++) {
2991          for (dataPointNo_0 = 0; dataPointNo_0 < numDataPointsPerSample_0; dataPointNo_0++) {
2992            int offset_0 = tmp_0->getPointOffset(sampleNo_0,dataPointNo_0);
2993            int offset_2 = tmp_2->getPointOffset(sampleNo_0,dataPointNo_0);
2994            const double *ptr_0 = &(arg_0_Z.getDataAtOffsetRO(offset_0));
2995            double *ptr_2 = &(res.getDataAtOffsetRW(offset_2));
2996            tensor_unary_operation(size0, ptr_0, ptr_2, operation);
2997          }
2998        }
2999      }
3000      else {
3001        throw DataException("Error - C_TensorUnaryOperation: unknown combination of inputs");
3002      }
3003    
3004      return res;
3005  }  }
3006    
3007  }  }

Legend:
Removed from v.757  
changed lines
  Added in v.2458

  ViewVC Help
Powered by ViewVC 1.1.26