/[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 2476 by jfenwick, Wed Jun 17 04:42:13 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
120       Constructor which creates a Data from a DataArrayView shape.       Constructor which creates a Data with points having the specified shape.
121    
122       \param value - Input - Single value applied to all Data.       \param value - Input - Single value applied to all Data.
123       \param dataPointShape - Input - The shape of each data point.       \param dataPointShape - Input - The shape of each data point.
# 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
246      void
247      setProtection();
248    
249      /**
250         \brief
251         Returns true, if the data object is protected against update
252    
253    */    */
254    ESCRIPT_DLL_API    ESCRIPT_DLL_API
255    const boost::python::numeric::array    bool
256    convertToNumArray();    isProtected() const;
257    
258    
259    /**
260       \brief
261       Return the 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 values of all data-points for the given sample 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    convertToNumArrayFromSampleNo(int sampleNo);    setValueOfDataPointToPyObject(int dataPointNo, const boost::python::object& py_object);
274    
275    /**    /**
276       \brief       \brief
277       Return the value of the specified data-point as a single 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    const boost::python::numeric::array    void
281    convertToNumArrayFromDPNo(int sampleNo,    setValueOfDataPointToArray(int dataPointNo, const boost::python::object&);
                             int dataPointNo);  
282    
283    /**    /**
284       \brief       \brief
285       Fills the expanded Data object from values of a python numarray object.       sets the values of a data-point on this process
286    */    */
287    ESCRIPT_DLL_API    ESCRIPT_DLL_API
288    void    void
289    fillFromNumArray(const boost::python::numeric::array);    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 returns the values of the object as a list of tuples (one for each datapoint).
564    
565      \param scalarastuple If true, scalar data will produce single valued tuples [(1,) (2,) ...]
566    If false, the result is a list of scalars [1, 2, ...]
567     */
568      ESCRIPT_DLL_API
569      const boost::python::object
570      toListOfTuples(bool scalarastuple=true);
571    
572    
573     /**
574        \brief
575        Return the sample data for the given sample no. This is not the
576        preferred interface but is provided for use by C code.
577        The bufferg parameter is only required for LazyData.
578        \param sampleNo - Input - the given sample no.
579        \param buffer - A buffer to to compute (and store) sample data in will be selected from this group.
580        \return pointer to the sample data.
581    */
582      ESCRIPT_DLL_API
583      inline
584      const DataAbstract::ValueType::value_type*
585      getSampleDataRO(DataAbstract::ValueType::size_type sampleNo, BufferGroup* bufferg=0);
586    
587    
588    /**    /**
589       \brief       \brief
590       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
591       preferred interface but is provided for use by C code.       preferred interface but is provided for use by C code.
592       \param sampleNo - Input - the given sample no.       \param sampleNo - Input - the given sample no.
593         \return pointer to the sample data.
594    */    */
595    ESCRIPT_DLL_API    ESCRIPT_DLL_API
596    inline    inline
597    DataAbstract::ValueType::value_type*    DataAbstract::ValueType::value_type*
598    getSampleData(DataAbstract::ValueType::size_type sampleNo)    getSampleDataRW(DataAbstract::ValueType::size_type sampleNo);
599    {  
     return m_data->getSampleData(sampleNo);  
   }  
600    
601    /**    /**
602       \brief       \brief
# Line 475  class Data { Line 614  class Data {
614    
615    /**    /**
616       \brief       \brief
617       Assign the given value to the data-points referenced by the given       Return a reference into the DataVector which points to the specified data point.
618       reference number.       \param sampleNo - Input -
619         \param dataPointNo - Input -
      The value supplied is a python numarray object.  The data from this numarray  
      is unpacked into a DataArray, and this is used to set the corresponding  
      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.  
620    */    */
621    ESCRIPT_DLL_API    ESCRIPT_DLL_API
622    void    DataTypes::ValueType::const_reference
623    setRefValue(int ref,    getDataPointRO(int sampleNo, int dataPointNo);
               const boost::python::numeric::array& value);  
624    
625    /**    /**
626       \brief       \brief
627       Return the values associated with the data-points referenced by the given       Return a reference into the DataVector which points to the specified data point.
628       reference number.       \param sampleNo - Input -
629         \param dataPointNo - Input -
      The value supplied is a python numarray object. The data from the corresponding  
      data-points in this Data object are packed into the given numarray object.  
   
      If the underlying Data object cannot be accessed via reference numbers, an  
      exception will be thrown.  
   
      \param ref - Input - reference number.  
      \param value - Output - object to receive values from data-points  
                              associated with the given reference number.  
630    */    */
631    ESCRIPT_DLL_API    ESCRIPT_DLL_API
632    void    DataTypes::ValueType::reference
633    getRefValue(int ref,    getDataPointRW(int sampleNo, int dataPointNo);
634                boost::python::numeric::array& value);  
635    
636    
637    /**    /**
638       \brief       \brief
639       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 -  
640    */    */
641    ESCRIPT_DLL_API    ESCRIPT_DLL_API
642    inline    inline
643    DataArrayView    DataTypes::ValueType::size_type
644    getDataPoint(int sampleNo,    getDataOffset(int sampleNo,
645                 int dataPointNo)                 int dataPointNo)
646    {    {
647      return m_data->getDataPoint(sampleNo,dataPointNo);        return m_data->getPointOffset(sampleNo,dataPointNo);
648    }    }
649    
650    /**    /**
# Line 536  class Data { Line 652  class Data {
652       Return a reference to the data point shape.       Return a reference to the data point shape.
653    */    */
654    ESCRIPT_DLL_API    ESCRIPT_DLL_API
655    const DataArrayView::ShapeType&    inline
656    getDataPointShape() const;    const DataTypes::ShapeType&
657      getDataPointShape() const
658      {
659        return m_data->getShape();
660      }
661    
662    /**    /**
663       \brief       \brief
# Line 561  class Data { Line 681  class Data {
681       Return the number of doubles stored for this Data.       Return the number of doubles stored for this Data.
682    */    */
683    ESCRIPT_DLL_API    ESCRIPT_DLL_API
684    DataArrayView::ValueType::size_type    DataTypes::ValueType::size_type
685    getLength() const;    getLength() const;
686    
687    
688    
689    /**    /**
690       \brief       \brief
691       Assign the given value to the tag. Implicitly converts this       Assign the given value to the tag assocciated with name. Implicitly converts this
692       object to type DataTagged. Throws an exception if this object       object to type DataTagged. Throws an exception if this object
693       cannot be converted to a DataTagged object.       cannot be converted to a DataTagged object or name cannot be mapped onto a tag key.
694         \param tagKey - Input - Integer key.
695         \param value - Input - Value to associate with given key.
696        ==>*
697      */
698      ESCRIPT_DLL_API
699      void
700      setTaggedValueByName(std::string name,
701                           const boost::python::object& value);
702    
703      /**
704         \brief
705         Assign the given value to the tag. Implicitly converts this
706         object to type DataTagged if it is constant.
707    
708       \param tagKey - Input - Integer key.       \param tagKey - Input - Integer key.
709       \param value - Input - Value to associate with given key.       \param value - Input - Value to associate with given key.
710      ==>*      ==>*
# Line 581  class Data { Line 717  class Data {
717    /**    /**
718       \brief       \brief
719       Assign the given value to the tag. Implicitly converts this       Assign the given value to the tag. Implicitly converts this
720       object to type DataTagged. Throws an exception if this object       object to type DataTagged if it is constant.
721       cannot be converted to a DataTagged object.  
722       \param tagKey - Input - Integer key.       \param tagKey - Input - Integer key.
723         \param pointshape - Input - The shape of the value parameter
724       \param value - Input - Value to associate with given key.       \param value - Input - Value to associate with given key.
725      ==>*       \param dataOffset - Input - Offset of the begining of the point within the value parameter
726    */    */
727    ESCRIPT_DLL_API    ESCRIPT_DLL_API
728    void    void
729    setTaggedValueFromCPP(int tagKey,    setTaggedValueFromCPP(int tagKey,
730                          const DataArrayView& value);              const DataTypes::ShapeType& pointshape,
731                            const DataTypes::ValueType& value,
732                int dataOffset=0);
733    
734    
735    
736    /**    /**
737      \brief      \brief
# Line 607  class Data { Line 748  class Data {
748    
749    /**    /**
750       \brief       \brief
751         set all values to zero
752         *
753      */
754      ESCRIPT_DLL_API
755      void
756      setToZero();
757    
758      /**
759         \brief
760       Interpolates this onto the given functionspace and returns       Interpolates this onto the given functionspace and returns
761       the result as a Data object.       the result as a Data object.
762       *       *
# Line 614  class Data { Line 764  class Data {
764    ESCRIPT_DLL_API    ESCRIPT_DLL_API
765    Data    Data
766    interpolate(const FunctionSpace& functionspace) const;    interpolate(const FunctionSpace& functionspace) const;
   
767    /**    /**
768       \brief       \brief
769       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 779  class Data {
779    grad() const;    grad() const;
780    
781    /**    /**
782        \brief
783         Calculate the integral over the function space domain as a python tuple.
784      */
785      ESCRIPT_DLL_API
786      boost::python::object
787      integrateToTuple_const() const;
788    
789    
790      /**
791        \brief
792         Calculate the integral over the function space domain as a python tuple.
793      */
794      ESCRIPT_DLL_API
795      boost::python::object
796      integrateToTuple();
797    
798    
799    
800      /**
801       \brief       \brief
802       Calculate the integral over the function space domain.       Returns 1./ Data object
803       *       *
804    */    */
805    ESCRIPT_DLL_API    ESCRIPT_DLL_API
806    boost::python::numeric::array    Data
807    integrate() const;    oneOver() const;
   
808    /**    /**
809       \brief       \brief
810       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 862  class Data {
862    /**    /**
863       \brief       \brief
864       Return the maximum absolute value of this Data object.       Return the maximum absolute value of this Data object.
865       *  
866         The method is not const because lazy data needs to be expanded before Lsup can be computed.
867         The _const form can be used when the Data object is const, however this will only work for
868         Data which is not Lazy.
869    
870         For Data which contain no samples (or tagged Data for which no tags in use have a value)
871         zero is returned.
872    */    */
873    ESCRIPT_DLL_API    ESCRIPT_DLL_API
874    double    double
875    Lsup() const;    Lsup();
876    
   /**  
      \brief  
      Return the minimum absolute value of this Data object.  
      *  
   */  
877    ESCRIPT_DLL_API    ESCRIPT_DLL_API
878    double    double
879    Linf() const;    Lsup_const() const;
880    
881    
882    /**    /**
883       \brief       \brief
884       Return the maximum value of this Data object.       Return the maximum value of this Data object.
885       *  
886         The method is not const because lazy data needs to be expanded before sup can be computed.
887         The _const form can be used when the Data object is const, however this will only work for
888         Data which is not Lazy.
889    
890         For Data which contain no samples (or tagged Data for which no tags in use have a value)
891         a large negative value is returned.
892    */    */
893    ESCRIPT_DLL_API    ESCRIPT_DLL_API
894    double    double
895    sup() const;    sup();
896    
897      ESCRIPT_DLL_API
898      double
899      sup_const() const;
900    
901    
902    /**    /**
903       \brief       \brief
904       Return the minimum value of this Data object.       Return the minimum value of this Data object.
905       *  
906         The method is not const because lazy data needs to be expanded before inf can be computed.
907         The _const form can be used when the Data object is const, however this will only work for
908         Data which is not Lazy.
909    
910         For Data which contain no samples (or tagged Data for which no tags in use have a value)
911         a large positive value is returned.
912    */    */
913    ESCRIPT_DLL_API    ESCRIPT_DLL_API
914    double    double
915    inf() const;    inf();
916    
917      ESCRIPT_DLL_API
918      double
919      inf_const() const;
920    
921    
922    
923    /**    /**
924       \brief       \brief
# Line 758  class Data { Line 950  class Data {
950    /**    /**
951       \brief       \brief
952       Return the (sample number, data-point number) of the data point with       Return the (sample number, data-point number) of the data point with
953       the minimum value in this Data object.       the minimum component value in this Data object.
954         \note If you are working in python, please consider using Locator
955    instead of manually manipulating process and point IDs.
956    */    */
957    ESCRIPT_DLL_API    ESCRIPT_DLL_API
958    const boost::python::tuple    const boost::python::tuple
959    mindp() const;    minGlobalDataPoint() const;
960    
961      /**
962         \brief
963         Return the (sample number, data-point number) of the data point with
964         the minimum component value in this Data object.
965         \note If you are working in python, please consider using Locator
966    instead of manually manipulating process and point IDs.
967      */
968    ESCRIPT_DLL_API    ESCRIPT_DLL_API
969    void    const boost::python::tuple
970    calc_mindp(int& SampleNo,    maxGlobalDataPoint() const;
971               int& DataPointNo) const;  
972    
973    
974    /**    /**
975       \brief       \brief
# Line 781  class Data { Line 983  class Data {
983    
984    /**    /**
985       \brief       \brief
986         Return the symmetric part of a matrix which is half the matrix plus its transpose.
987         *
988      */
989      ESCRIPT_DLL_API
990      Data
991      symmetric() const;
992    
993      /**
994         \brief
995         Return the nonsymmetric part of a matrix which is half the matrix minus its transpose.
996         *
997      */
998      ESCRIPT_DLL_API
999      Data
1000      nonsymmetric() const;
1001    
1002      /**
1003         \brief
1004         Return the trace of a matrix
1005         *
1006      */
1007      ESCRIPT_DLL_API
1008      Data
1009      trace(int axis_offset) const;
1010    
1011      /**
1012         \brief
1013         Transpose each data point of this Data object around the given axis.
1014         *
1015      */
1016      ESCRIPT_DLL_API
1017      Data
1018      transpose(int axis_offset) const;
1019    
1020      /**
1021         \brief
1022       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.
1023       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.
1024       *       *
# Line 792  class Data { Line 1030  class Data {
1030    /**    /**
1031       \brief       \brief
1032       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.
1033       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
1034       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
1035       first non-zero entry is positive.       first non-zero entry is positive.
1036       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
1037       *       *
# Line 804  class Data { Line 1042  class Data {
1042    
1043    /**    /**
1044       \brief       \brief
1045       Transpose each data point of this Data object around the given axis.       swaps the components axis0 and axis1
      --* not implemented yet *--  
1046       *       *
1047    */    */
1048    ESCRIPT_DLL_API    ESCRIPT_DLL_API
1049    Data    Data
1050    transpose(int axis) const;    swapaxes(const int axis0, const int axis1) const;
1051    
1052    /**    /**
1053       \brief       \brief
1054       Calculate the trace of each data point of this Data object.       Return the error function erf of each data point of this Data object.
1055       *       *
1056    */    */
1057    ESCRIPT_DLL_API    ESCRIPT_DLL_API
1058    Data    Data
1059    trace() const;    erf() const;
1060    
1061    /**    /**
1062       \brief       \brief
# Line 998  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 right Input - the power to raise the object to.       \param right Input - the power to raise the object to.
1240       *       *
1241     */     */
# Line 1009  class Data { Line 1246  class Data {
1246    /**    /**
1247       \brief       \brief
1248       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.
1249        
1250       \param left Input - the bases       \param left Input - the bases
1251       *       *
1252     */     */
# Line 1045  class Data { Line 1282  class Data {
1282    ESCRIPT_DLL_API    ESCRIPT_DLL_API
1283    Data& operator+=(const boost::python::object& right);    Data& operator+=(const boost::python::object& right);
1284    
1285      ESCRIPT_DLL_API
1286      Data& operator=(const Data& other);
1287    
1288    /**    /**
1289       \brief       \brief
1290       Overloaded operator -=       Overloaded operator -=
# Line 1137  class Data { Line 1377  class Data {
1377    ESCRIPT_DLL_API    ESCRIPT_DLL_API
1378    inline    inline
1379    void    void
1380    unaryOp(UnaryFunction operation);    unaryOp2(UnaryFunction operation);
1381    
1382    /**    /**
1383       \brief       \brief
# Line 1148  class Data { Line 1388  class Data {
1388    */    */
1389    ESCRIPT_DLL_API    ESCRIPT_DLL_API
1390    Data    Data
1391    getSlice(const DataArrayView::RegionType& region) const;    getSlice(const DataTypes::RegionType& region) const;
1392    
1393    /**    /**
1394       \brief       \brief
# Line 1161  class Data { Line 1401  class Data {
1401    ESCRIPT_DLL_API    ESCRIPT_DLL_API
1402    void    void
1403    setSlice(const Data& value,    setSlice(const Data& value,
1404             const DataArrayView::RegionType& region);             const DataTypes::RegionType& region);
1405    
1406    /**    /**
1407       \brief       \brief
1408       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.  
1409    */    */
1410    ESCRIPT_DLL_API    ESCRIPT_DLL_API
1411    void    void
1412    archiveData(const std::string fileName);          print(void);
1413    
1414    /**    /**
1415       \brief       \brief
1416       Extract the Data object archived in the given file, overwriting       return the MPI rank number of the local data
1417       the current Data object.                   MPI_COMM_WORLD is assumed and the result of MPI_Comm_size()
1418       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.  
1419    */    */
1420    ESCRIPT_DLL_API    ESCRIPT_DLL_API
1421    void          int
1422    extractData(const std::string fileName,          get_MPIRank(void) const;
1423                const FunctionSpace& fspace);  
1424      /**
1425         \brief
1426         return the MPI rank number of the local data
1427                     MPI_COMM_WORLD is assumed and the result of MPI_Comm_rank()
1428                     is returned
1429      */
1430      ESCRIPT_DLL_API
1431            int
1432            get_MPISize(void) const;
1433    
1434      /**
1435         \brief
1436         return the MPI rank number of the local data
1437                     MPI_COMM_WORLD is assumed and returned.
1438      */
1439      ESCRIPT_DLL_API
1440            MPI_Comm
1441            get_MPIComm(void) const;
1442    
1443      /**
1444         \brief
1445         return the object produced by the factory, which is a DataConstant or DataExpanded
1446        TODO Ownership of this object should be explained in doco.
1447      */
1448      ESCRIPT_DLL_API
1449            DataAbstract*
1450            borrowData(void) const;
1451    
1452      ESCRIPT_DLL_API
1453            DataAbstract_ptr
1454            borrowDataPtr(void) const;
1455    
1456      ESCRIPT_DLL_API
1457            DataReady_ptr
1458            borrowReadyPtr(void) const;
1459    
1460    
1461    
1462    /**    /**
1463       \brief       \brief
1464       print the data values to stdout. Used for debugging       Return a pointer to the beginning of the datapoint at the specified offset.
1465         TODO Eventually these should be inlined.
1466         \param i - position(offset) in the underlying datastructure
1467    */    */
1468    void print();  
1469      ESCRIPT_DLL_API
1470            DataTypes::ValueType::const_reference
1471            getDataAtOffsetRO(DataTypes::ValueType::size_type i);
1472    
1473    
1474      ESCRIPT_DLL_API
1475            DataTypes::ValueType::reference
1476            getDataAtOffsetRW(DataTypes::ValueType::size_type i);
1477    
1478    
1479    
1480    /**
1481       \brief Create a buffer for use by getSample
1482       Allocates a DataVector large enough for DataLazy::resolveSample to operate on for the current Data.
1483       Do not use this buffer for other Data instances (unless you are sure they will be the same size).
1484      
1485       In multi-threaded sections, this needs to be called on each thread.
1486    
1487       \return A BufferGroup* if Data is lazy, NULL otherwise.
1488       \warning This pointer must be deallocated using freeSampleBuffer to avoid cross library memory issues.
1489    */
1490      ESCRIPT_DLL_API
1491      BufferGroup*
1492      allocSampleBuffer() const;
1493    
1494    /**
1495       \brief Free a buffer allocated with allocSampleBuffer.
1496       \param buffer Input - pointer to the buffer to deallocate.
1497    */
1498    ESCRIPT_DLL_API void freeSampleBuffer(BufferGroup* buffer);
1499    
1500   protected:   protected:
1501    
1502   private:   private:
1503    
1504      double
1505      LsupWorker() const;
1506    
1507      double
1508      supWorker() const;
1509    
1510      double
1511      infWorker() const;
1512    
1513      boost::python::object
1514      integrateWorker() const;
1515    
1516      void
1517      calc_minGlobalDataPoint(int& ProcNo,  int& DataPointNo) const;
1518    
1519      void
1520      calc_maxGlobalDataPoint(int& ProcNo,  int& DataPointNo) const;
1521    
1522    
1523    /**    /**
1524       \brief       \brief
1525       Check *this and the right operand are compatible. Throws       Check *this and the right operand are compatible. Throws
# Line 1249  class Data { Line 1573  class Data {
1573    
1574    /**    /**
1575       \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  
1576       Convert the data type of the RHS to match this.       Convert the data type of the RHS to match this.
1577       \param right - Input - data type to match.       \param right - Input - data type to match.
1578    */    */
# Line 1278  class Data { Line 1591  class Data {
1591       \brief       \brief
1592       Construct a Data object of the appropriate type.       Construct a Data object of the appropriate type.
1593    */    */
1594    template <class IValueType>  
1595    void    void
1596    initialise(const IValueType& value,    initialise(const DataTypes::ValueType& value,
1597             const DataTypes::ShapeType& shape,
1598               const FunctionSpace& what,               const FunctionSpace& what,
1599               bool expanded);               bool expanded);
1600    
   /**  
      \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.  
   */  
1601    void    void
1602    reshapeDataPoint(const DataArrayView::ShapeType& shape);    initialise(const WrappedArray& value,
1603                     const FunctionSpace& what,
1604                     bool expanded);
1605    
1606      //
1607      // flag to protect the data object against any update
1608      bool m_protected;
1609      mutable bool m_shared;
1610      bool m_lazy;
1611    
1612    //    //
1613    // pointer to the actual data object    // pointer to the actual data object
1614    boost::shared_ptr<DataAbstract> m_data;  //   boost::shared_ptr<DataAbstract> m_data;
1615      DataAbstract_ptr m_data;
1616    
1617    // If possible please use getReadyPtr instead.
1618    // But see warning below.
1619      const DataReady*
1620      getReady() const;
1621    
1622      DataReady*
1623      getReady();
1624    
1625    
1626    // Be wary of using this for local operations since it (temporarily) increases reference count.
1627    // If you are just using this to call a method on DataReady instead of DataAbstract consider using
1628    // getReady() instead
1629      DataReady_ptr
1630      getReadyPtr();
1631    
1632      const_DataReady_ptr
1633      getReadyPtr() const;
1634    
1635    
1636      /**
1637       \brief Update the Data's shared flag
1638       This indicates that the DataAbstract used by this object is now shared (or no longer shared).
1639       For internal use only.
1640      */
1641      void updateShareStatus(bool nowshared) const
1642      {
1643        m_shared=nowshared;     // m_shared is mutable
1644      }
1645    
1646      // In the isShared() method below:
1647      // A problem would occur if m_data (the address pointed to) were being modified
1648      // while the call m_data->is_shared is being executed.
1649      //
1650      // Q: So why do I think this code can be thread safe/correct?
1651      // A: We need to make some assumptions.
1652      // 1. We assume it is acceptable to return true under some conditions when we aren't shared.
1653      // 2. We assume that no constructions or assignments which will share previously unshared
1654      //    will occur while this call is executing. This is consistent with the way Data:: and C are written.
1655    //    //
1656    // pointer to the internal profiling data    // This means that the only transition we need to consider, is when a previously shared object is
1657    struct profDataEntry *profData;    // not shared anymore. ie. the other objects have been destroyed or a deep copy has been made.
1658      // In those cases the m_shared flag changes to false after m_data has completed changing.
1659      // For any threads executing before the flag switches they will assume the object is still shared.
1660      bool isShared() const
1661      {
1662        return m_shared;
1663    /*  if (m_shared) return true;
1664        if (m_data->isShared())        
1665        {                  
1666            updateShareStatus(true);
1667            return true;
1668        }
1669        return false;*/
1670      }
1671    
1672      void forceResolve()
1673      {
1674        if (isLazy())
1675        {
1676            #ifdef _OPENMP
1677            if (omp_in_parallel())
1678            {   // Yes this is throwing an exception out of an omp thread which is forbidden.
1679            throw DataException("Please do not call forceResolve() in a parallel region.");
1680            }
1681            #endif
1682            resolve();
1683        }
1684      }
1685    
1686      /**
1687      \brief if another object is sharing out member data make a copy to work with instead.
1688      This code should only be called from single threaded sections of code.
1689      */
1690      void exclusiveWrite()
1691      {
1692    #ifdef _OPENMP
1693      if (omp_in_parallel())
1694      {
1695    // *((int*)0)=17;
1696        throw DataException("Programming error. Please do not run exclusiveWrite() in multi-threaded sections.");
1697      }
1698    #endif
1699        forceResolve();
1700        if (isShared())
1701        {
1702            DataAbstract* t=m_data->deepCopy();
1703            set_m_data(DataAbstract_ptr(t));
1704        }
1705      }
1706    
1707      /**
1708      \brief checks if caller can have exclusive write to the object
1709      */
1710      void checkExclusiveWrite()
1711      {
1712        if  (isLazy() || isShared())
1713        {
1714            throw DataException("Programming error. ExclusiveWrite required - please call requireWrite()");
1715        }
1716      }
1717    
1718      /**
1719      \brief Modify the data abstract hosted by this Data object
1720      For internal use only.
1721      Passing a pointer to null is permitted (do this in the destructor)
1722      \warning Only to be called in single threaded code or inside a single/critical section. This method needs to be atomic.
1723      */
1724      void set_m_data(DataAbstract_ptr p);
1725    
1726      friend class DataAbstract;        // To allow calls to updateShareStatus
1727    
1728  };  };
1729    
1730  template <class IValueType>  }   // end namespace escript
1731  void  
1732  Data::initialise(const IValueType& value,  
1733                   const FunctionSpace& what,  // No, this is not supposed to be at the top of the file
1734                   bool expanded)  // DataAbstact needs to be declared first, then DataReady needs to be fully declared
1735    // so that I can dynamic cast between them below.
1736    #include "DataReady.h"
1737    #include "DataLazy.h"
1738    
1739    namespace escript
1740  {  {
1741    //  
1742    // Construct a Data object of the appropriate type.  inline
1743    // Construct the object first as there seems to be a bug which causes  const DataReady*
1744    // undefined behaviour if an exception is thrown during construction  Data::getReady() const
1745    // within the shared_ptr constructor.  {
1746    if (expanded) {     const DataReady* dr=dynamic_cast<const DataReady*>(m_data.get());
1747      DataAbstract* temp=new DataExpanded(value,what);     EsysAssert((dr!=0), "Error - casting to DataReady.");
1748      boost::shared_ptr<DataAbstract> temp_data(temp);     return dr;
1749      m_data=temp_data;  }
1750    } else {  
1751      DataAbstract* temp=new DataConstant(value,what);  inline
1752      boost::shared_ptr<DataAbstract> temp_data(temp);  DataReady*
1753      m_data=temp_data;  Data::getReady()
1754    }  {
1755       DataReady* dr=dynamic_cast<DataReady*>(m_data.get());
1756       EsysAssert((dr!=0), "Error - casting to DataReady.");
1757       return dr;
1758    }
1759    
1760    // Be wary of using this for local operations since it (temporarily) increases reference count.
1761    // If you are just using this to call a method on DataReady instead of DataAbstract consider using
1762    // getReady() instead
1763    inline
1764    DataReady_ptr
1765    Data::getReadyPtr()
1766    {
1767       DataReady_ptr dr=boost::dynamic_pointer_cast<DataReady>(m_data);
1768       EsysAssert((dr.get()!=0), "Error - casting to DataReady.");
1769       return dr;
1770    }
1771    
1772    
1773    inline
1774    const_DataReady_ptr
1775    Data::getReadyPtr() const
1776    {
1777       const_DataReady_ptr dr=boost::dynamic_pointer_cast<const DataReady>(m_data);
1778       EsysAssert((dr.get()!=0), "Error - casting to DataReady.");
1779       return dr;
1780    }
1781    
1782    inline
1783    DataAbstract::ValueType::value_type*
1784    Data::getSampleDataRW(DataAbstract::ValueType::size_type sampleNo)
1785    {
1786    //    if (isLazy())
1787    //    {
1788    //  resolve();
1789    //    }
1790    //    exclusiveWrite();
1791       if (isLazy())
1792       {
1793        throw DataException("Error, attempt to acquire RW access to lazy data. Please call requireWrite() first.");
1794       }
1795       return getReady()->getSampleDataRW(sampleNo);
1796    }
1797    
1798    // inline
1799    // const DataAbstract::ValueType::value_type*
1800    // Data::getSampleDataRO(DataAbstract::ValueType::size_type sampleNo, DataTypes::ValueType* buffer)
1801    // {
1802    //    DataLazy* l=dynamic_cast<DataLazy*>(m_data.get());
1803    //    if (l!=0)
1804    //    {
1805    //  size_t offset=0;
1806    //  if (buffer==NULL)
1807    //  {
1808    //      throw DataException("Error, attempt to getSampleDataRO for lazy Data with buffer==NULL");
1809    //  }
1810    //  const DataTypes::ValueType* res=l->resolveSample(*buffer,0,sampleNo,offset);
1811    //  return &((*res)[offset]);
1812    //    }
1813    //    return getReady()->getSampleDataRO(sampleNo);
1814    // }
1815    
1816    inline
1817    const DataAbstract::ValueType::value_type*
1818    Data::getSampleDataRO(DataAbstract::ValueType::size_type sampleNo, BufferGroup* bufferg)
1819    {
1820       DataLazy* l=dynamic_cast<DataLazy*>(m_data.get());
1821       if (l!=0)
1822       {
1823        size_t offset=0;
1824        if (bufferg==NULL)
1825        {
1826            throw DataException("Error, attempt to getSampleDataRO for lazy Data with buffer==NULL");
1827        }
1828        const DataTypes::ValueType* res=l->resolveSample(*bufferg,sampleNo,offset);
1829        return &((*res)[offset]);
1830       }
1831       return getReady()->getSampleDataRO(sampleNo);
1832  }  }
1833    
1834    
1835    
1836    /**
1837       Modify a filename for MPI parallel output to multiple files
1838    */
1839    char *Escript_MPI_appendRankToFileName(const char *, int, int);
1840    
1841  /**  /**
1842     Binary Data object operators.     Binary Data object operators.
1843  */  */
1844    inline double rpow(double x,double y)
1845    {
1846        return pow(y,x);
1847    }
1848    
1849  /**  /**
1850    \brief    \brief
# Line 1422  ESCRIPT_DLL_API Data operator*(const boo Line 1938  ESCRIPT_DLL_API Data operator*(const boo
1938  */  */
1939  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);
1940    
1941    
1942    
1943  /**  /**
1944    \brief    \brief
1945    Output operator    Output operator
# Line 1430  ESCRIPT_DLL_API std::ostream& operator<< Line 1948  ESCRIPT_DLL_API std::ostream& operator<<
1948    
1949  /**  /**
1950    \brief    \brief
1951    Return true if operands are equivalent, else return false.    Compute a tensor product of two Data objects
1952    NB: this operator does very little at this point, and isn't to    \param arg0 - Input - Data object
1953    be relied on. Requires further implementation.    \param arg1 - Input - Data object
1954      \param axis_offset - Input - axis offset
1955      \param transpose - Input - 0: transpose neither, 1: transpose arg0, 2: transpose arg1
1956  */  */
1957  //ESCRIPT_DLL_API bool operator==(const Data& left, const Data& right);  ESCRIPT_DLL_API
1958    Data
1959    C_GeneralTensorProduct(Data& arg0,
1960                         Data& arg1,
1961                         int axis_offset=0,
1962                         int transpose=0);
1963    
1964  /**  /**
1965    \brief    \brief
# Line 1449  Data::binaryOp(const Data& right, Line 1974  Data::binaryOp(const Data& right,
1974  {  {
1975     //     //
1976     // 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
1977     if (getPointDataView().getRank()==0 && right.getPointDataView().getRank()!=0) {     if (getDataPointRank()==0 && right.getDataPointRank()!=0) {
1978       reshapeDataPoint(right.getPointDataView().getShape());       throw DataException("Error - attempt to update rank zero object with object with rank bigger than zero.");
1979       }
1980    
1981       if (isLazy() || right.isLazy())
1982       {
1983         throw DataException("Programmer error - attempt to call binaryOp with Lazy Data.");
1984     }     }
1985     //     //
1986     // initially make the temporary a shallow copy     // initially make the temporary a shallow copy
1987     Data tempRight(right);     Data tempRight(right);
1988    
1989     if (getFunctionSpace()!=right.getFunctionSpace()) {     if (getFunctionSpace()!=right.getFunctionSpace()) {
1990       if (right.probeInterpolation(getFunctionSpace())) {       if (right.probeInterpolation(getFunctionSpace())) {
1991         //         //
1992         // an interpolation is required so create a new Data         // an interpolation is required so create a new Data
1993         tempRight=Data(right,this->getFunctionSpace());         tempRight=Data(right,this->getFunctionSpace());
1994       } else if (probeInterpolation(right.getFunctionSpace())) {       } else if (probeInterpolation(right.getFunctionSpace())) {
1995         //         //
1996         // interpolate onto the RHS function space         // interpolate onto the RHS function space
1997         Data tempLeft(*this,right.getFunctionSpace());         Data tempLeft(*this,right.getFunctionSpace());
1998         m_data=tempLeft.m_data;  //        m_data=tempLeft.m_data;
1999           set_m_data(tempLeft.m_data);
2000       }       }
2001     }     }
2002     operandCheck(tempRight);     operandCheck(tempRight);
# Line 1480  Data::binaryOp(const Data& right, Line 2012  Data::binaryOp(const Data& right,
2012       // of any data type       // of any data type
2013       DataExpanded* leftC=dynamic_cast<DataExpanded*>(m_data.get());       DataExpanded* leftC=dynamic_cast<DataExpanded*>(m_data.get());
2014       EsysAssert((leftC!=0), "Programming error - casting to DataExpanded.");       EsysAssert((leftC!=0), "Programming error - casting to DataExpanded.");
2015       escript::binaryOp(*leftC,*(tempRight.m_data.get()),operation);       escript::binaryOp(*leftC,*(tempRight.getReady()),operation);
2016     } else if (isTagged()) {     } else if (isTagged()) {
2017       //       //
2018       // 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 2038  Data::binaryOp(const Data& right,
2038    
2039  /**  /**
2040    \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  
2041    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.
2042    Given operation combines each element of each data point, thus argument    Given operation combines each element of each data point, thus argument
2043    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 2060  Data::algorithm(BinaryFunction operation
2060      DataConstant* leftC=dynamic_cast<DataConstant*>(m_data.get());      DataConstant* leftC=dynamic_cast<DataConstant*>(m_data.get());
2061      EsysAssert((leftC!=0), "Programming error - casting to DataConstant.");      EsysAssert((leftC!=0), "Programming error - casting to DataConstant.");
2062      return escript::algorithm(*leftC,operation,initial_value);      return escript::algorithm(*leftC,operation,initial_value);
2063      } else if (isEmpty()) {
2064        throw DataException("Error - Operations not permitted on instances of DataEmpty.");
2065      } else if (isLazy()) {
2066        throw DataException("Error - Operations not permitted on instances of DataLazy.");
2067      } else {
2068        throw DataException("Error - Data encapsulates an unknown type.");
2069    }    }
   return 0;  
2070  }  }
2071    
2072  /**  /**
2073    \brief    \brief
2074    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.
2075    Given operation combines each element within each data point into a scalar,    Given operation combines each element within each data point into a scalar,
2076    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
2077    rank 0 Data object.    rank 0 Data object.
2078    Calls escript::dp_algorithm.    Calls escript::dp_algorithm.
2079  */  */
# Line 1631  inline Line 2082  inline
2082  Data  Data
2083  Data::dp_algorithm(BinaryFunction operation, double initial_value) const  Data::dp_algorithm(BinaryFunction operation, double initial_value) const
2084  {  {
2085    if (isExpanded()) {    if (isEmpty()) {
2086      Data result(0,DataArrayView::ShapeType(),getFunctionSpace(),isExpanded());      throw DataException("Error - Operations not permitted on instances of DataEmpty.");
2087      }
2088      else if (isExpanded()) {
2089        Data result(0,DataTypes::ShapeType(),getFunctionSpace(),isExpanded());
2090      DataExpanded* dataE=dynamic_cast<DataExpanded*>(m_data.get());      DataExpanded* dataE=dynamic_cast<DataExpanded*>(m_data.get());
2091      DataExpanded* resultE=dynamic_cast<DataExpanded*>(result.m_data.get());      DataExpanded* resultE=dynamic_cast<DataExpanded*>(result.m_data.get());
2092      EsysAssert((dataE!=0), "Programming error - casting data to DataExpanded.");      EsysAssert((dataE!=0), "Programming error - casting data to DataExpanded.");
2093      EsysAssert((resultE!=0), "Programming error - casting result to DataExpanded.");      EsysAssert((resultE!=0), "Programming error - casting result to DataExpanded.");
2094      escript::dp_algorithm(*dataE,*resultE,operation,initial_value);      escript::dp_algorithm(*dataE,*resultE,operation,initial_value);
2095      return result;      return result;
2096    } else if (isTagged()) {    }
2097      else if (isTagged()) {
2098      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());  
2099      EsysAssert((dataT!=0), "Programming error - casting data to DataTagged.");      EsysAssert((dataT!=0), "Programming error - casting data to DataTagged.");
2100      EsysAssert((resultT!=0), "Programming error - casting result to DataTagged.");      DataTypes::ValueType defval(1);
2101        defval[0]=0;
2102        DataTagged* resultT=new DataTagged(getFunctionSpace(), DataTypes::scalarShape, defval, dataT);
2103      escript::dp_algorithm(*dataT,*resultT,operation,initial_value);      escript::dp_algorithm(*dataT,*resultT,operation,initial_value);
2104      return result;      return Data(resultT);   // note: the Data object now owns the resultT pointer
2105    } else if (isConstant()) {    }
2106      Data result(0,DataArrayView::ShapeType(),getFunctionSpace(),isExpanded());    else if (isConstant()) {
2107        Data result(0,DataTypes::ShapeType(),getFunctionSpace(),isExpanded());
2108      DataConstant* dataC=dynamic_cast<DataConstant*>(m_data.get());      DataConstant* dataC=dynamic_cast<DataConstant*>(m_data.get());
2109      DataConstant* resultC=dynamic_cast<DataConstant*>(result.m_data.get());      DataConstant* resultC=dynamic_cast<DataConstant*>(result.m_data.get());
2110      EsysAssert((dataC!=0), "Programming error - casting data to DataConstant.");      EsysAssert((dataC!=0), "Programming error - casting data to DataConstant.");
2111      EsysAssert((resultC!=0), "Programming error - casting result to DataConstant.");      EsysAssert((resultC!=0), "Programming error - casting result to DataConstant.");
2112      escript::dp_algorithm(*dataC,*resultC,operation,initial_value);      escript::dp_algorithm(*dataC,*resultC,operation,initial_value);
2113      return result;      return result;
2114      } else if (isLazy()) {
2115        throw DataException("Error - Operations not permitted on instances of DataLazy.");
2116      } else {
2117        throw DataException("Error - Data encapsulates an unknown type.");
2118      }
2119    }
2120    
2121    /**
2122      \brief
2123      Compute a tensor operation with two Data objects
2124      \param arg0 - Input - Data object
2125      \param arg1 - Input - Data object
2126      \param operation - Input - Binary op functor
2127    */
2128    template <typename BinaryFunction>
2129    inline
2130    Data
2131    C_TensorBinaryOperation(Data const &arg_0,
2132                            Data const &arg_1,
2133                            BinaryFunction operation)
2134    {
2135      if (arg_0.isEmpty() || arg_1.isEmpty())
2136      {
2137         throw DataException("Error - Operations not permitted on instances of DataEmpty.");
2138      }
2139      if (arg_0.isLazy() || arg_1.isLazy())
2140      {
2141         throw DataException("Error - Operations not permitted on lazy data.");
2142      }
2143      // Interpolate if necessary and find an appropriate function space
2144      Data arg_0_Z, arg_1_Z;
2145      if (arg_0.getFunctionSpace()!=arg_1.getFunctionSpace()) {
2146        if (arg_0.probeInterpolation(arg_1.getFunctionSpace())) {
2147          arg_0_Z = arg_0.interpolate(arg_1.getFunctionSpace());
2148          arg_1_Z = Data(arg_1);
2149        }
2150        else if (arg_1.probeInterpolation(arg_0.getFunctionSpace())) {
2151          arg_1_Z=arg_1.interpolate(arg_0.getFunctionSpace());
2152          arg_0_Z =Data(arg_0);
2153        }
2154        else {
2155          throw DataException("Error - C_TensorBinaryOperation: arguments have incompatible function spaces.");
2156        }
2157      } else {
2158          arg_0_Z = Data(arg_0);
2159          arg_1_Z = Data(arg_1);
2160    }    }
2161    Data falseRetVal; // to keep compiler quiet    // Get rank and shape of inputs
2162    return falseRetVal;    int rank0 = arg_0_Z.getDataPointRank();
2163      int rank1 = arg_1_Z.getDataPointRank();
2164      DataTypes::ShapeType shape0 = arg_0_Z.getDataPointShape();
2165      DataTypes::ShapeType shape1 = arg_1_Z.getDataPointShape();
2166      int size0 = arg_0_Z.getDataPointSize();
2167      int size1 = arg_1_Z.getDataPointSize();
2168      // Declare output Data object
2169      Data res;
2170    
2171      if (shape0 == shape1) {
2172        if (arg_0_Z.isConstant()   && arg_1_Z.isConstant()) {
2173          res = Data(0.0, shape0, arg_1_Z.getFunctionSpace());      // DataConstant output
2174          const double *ptr_0 = &(arg_0_Z.getDataAtOffsetRO(0));
2175          const double *ptr_1 = &(arg_1_Z.getDataAtOffsetRO(0));
2176          double *ptr_2 = &(res.getDataAtOffsetRW(0));
2177    
2178          tensor_binary_operation(size0, ptr_0, ptr_1, ptr_2, operation);
2179        }
2180        else if (arg_0_Z.isConstant()   && arg_1_Z.isTagged()) {
2181    
2182          // Prepare the DataConstant input
2183          DataConstant* tmp_0=dynamic_cast<DataConstant*>(arg_0_Z.borrowData());
2184    
2185          // Borrow DataTagged input from Data object
2186          DataTagged* tmp_1=dynamic_cast<DataTagged*>(arg_1_Z.borrowData());
2187    
2188          // Prepare a DataTagged output 2
2189          res = Data(0.0, shape0, arg_1_Z.getFunctionSpace());      // DataTagged output
2190          res.tag();
2191          DataTagged* tmp_2=dynamic_cast<DataTagged*>(res.borrowData());
2192    
2193          // Prepare offset into DataConstant
2194          int offset_0 = tmp_0->getPointOffset(0,0);
2195          const double *ptr_0 = &(arg_0_Z.getDataAtOffsetRO(offset_0));
2196    
2197          // Get the pointers to the actual data
2198          const double *ptr_1 = &(tmp_1->getDefaultValueRO(0));
2199          double *ptr_2 = &(tmp_2->getDefaultValueRW(0));
2200    
2201          // Compute a result for the default
2202          tensor_binary_operation(size0, ptr_0, ptr_1, ptr_2, operation);
2203          // Compute a result for each tag
2204          const DataTagged::DataMapType& lookup_1=tmp_1->getTagLookup();
2205          DataTagged::DataMapType::const_iterator i; // i->first is a tag, i->second is an offset into memory
2206          for (i=lookup_1.begin();i!=lookup_1.end();i++) {
2207            tmp_2->addTag(i->first);
2208            const double *ptr_1 = &(tmp_1->getDataByTagRO(i->first,0));
2209            double *ptr_2 = &(tmp_2->getDataByTagRW(i->first,0));
2210    
2211            tensor_binary_operation(size0, ptr_0, ptr_1, ptr_2, operation);
2212          }
2213    
2214        }
2215        else if (arg_0_Z.isConstant()   && arg_1_Z.isExpanded()) {
2216          res = Data(0.0, shape0, arg_1_Z.getFunctionSpace(),true); // DataExpanded output
2217          DataConstant* tmp_0=dynamic_cast<DataConstant*>(arg_0_Z.borrowData());
2218          DataExpanded* tmp_1=dynamic_cast<DataExpanded*>(arg_1_Z.borrowData());
2219          DataExpanded* tmp_2=dynamic_cast<DataExpanded*>(res.borrowData());
2220    
2221          int sampleNo_1,dataPointNo_1;
2222          int numSamples_1 = arg_1_Z.getNumSamples();
2223          int numDataPointsPerSample_1 = arg_1_Z.getNumDataPointsPerSample();
2224          int offset_0 = tmp_0->getPointOffset(0,0);
2225          res.requireWrite();
2226          #pragma omp parallel for private(sampleNo_1,dataPointNo_1) schedule(static)
2227          for (sampleNo_1 = 0; sampleNo_1 < numSamples_1; sampleNo_1++) {
2228            for (dataPointNo_1 = 0; dataPointNo_1 < numDataPointsPerSample_1; dataPointNo_1++) {
2229              int offset_1 = tmp_1->getPointOffset(sampleNo_1,dataPointNo_1);
2230              int offset_2 = tmp_2->getPointOffset(sampleNo_1,dataPointNo_1);
2231              const double *ptr_0 = &(arg_0_Z.getDataAtOffsetRO(offset_0));
2232              const double *ptr_1 = &(arg_1_Z.getDataAtOffsetRO(offset_1));
2233              double *ptr_2 = &(res.getDataAtOffsetRW(offset_2));
2234              tensor_binary_operation(size0, ptr_0, ptr_1, ptr_2, operation);
2235            }
2236          }
2237    
2238        }
2239        else if (arg_0_Z.isTagged()     && arg_1_Z.isConstant()) {
2240          // Borrow DataTagged input from Data object
2241          DataTagged* tmp_0=dynamic_cast<DataTagged*>(arg_0_Z.borrowData());
2242    
2243          // Prepare the DataConstant input
2244          DataConstant* tmp_1=dynamic_cast<DataConstant*>(arg_1_Z.borrowData());
2245    
2246          // Prepare a DataTagged output 2
2247          res = Data(0.0, shape0, arg_0_Z.getFunctionSpace());      // DataTagged output
2248          res.tag();
2249          DataTagged* tmp_2=dynamic_cast<DataTagged*>(res.borrowData());
2250    
2251          // Prepare offset into DataConstant
2252          int offset_1 = tmp_1->getPointOffset(0,0);
2253    
2254          const double *ptr_1 = &(arg_1_Z.getDataAtOffsetRO(offset_1));
2255          // Get the pointers to the actual data
2256          const double *ptr_0 = &(tmp_0->getDefaultValueRO(0));
2257          double *ptr_2 = &(tmp_2->getDefaultValueRW(0));
2258          // Compute a result for the default
2259          tensor_binary_operation(size0, ptr_0, ptr_1, ptr_2, operation);
2260          // Compute a result for each tag
2261          const DataTagged::DataMapType& lookup_0=tmp_0->getTagLookup();
2262          DataTagged::DataMapType::const_iterator i; // i->first is a tag, i->second is an offset into memory
2263          for (i=lookup_0.begin();i!=lookup_0.end();i++) {
2264            tmp_2->addTag(i->first);
2265            const double *ptr_0 = &(tmp_0->getDataByTagRO(i->first,0));
2266            double *ptr_2 = &(tmp_2->getDataByTagRW(i->first,0));
2267            tensor_binary_operation(size0, ptr_0, ptr_1, ptr_2, operation);
2268          }
2269    
2270        }
2271        else if (arg_0_Z.isTagged()     && arg_1_Z.isTagged()) {
2272          // Borrow DataTagged input from Data object
2273          DataTagged* tmp_0=dynamic_cast<DataTagged*>(arg_0_Z.borrowData());
2274    
2275          // Borrow DataTagged input from Data object
2276          DataTagged* tmp_1=dynamic_cast<DataTagged*>(arg_1_Z.borrowData());
2277    
2278          // Prepare a DataTagged output 2
2279          res = Data(0.0, shape0, arg_1_Z.getFunctionSpace());
2280          res.tag();        // DataTagged output
2281          DataTagged* tmp_2=dynamic_cast<DataTagged*>(res.borrowData());
2282    
2283          // Get the pointers to the actual data
2284          const double *ptr_0 = &(tmp_0->getDefaultValueRO(0));
2285          const double *ptr_1 = &(tmp_1->getDefaultValueRO(0));
2286          double *ptr_2 = &(tmp_2->getDefaultValueRW(0));
2287    
2288          // Compute a result for the default
2289          tensor_binary_operation(size0, ptr_0, ptr_1, ptr_2, operation);
2290          // Merge the tags
2291          DataTagged::DataMapType::const_iterator i; // i->first is a tag, i->second is an offset into memory
2292          const DataTagged::DataMapType& lookup_0=tmp_0->getTagLookup();
2293          const DataTagged::DataMapType& lookup_1=tmp_1->getTagLookup();
2294          for (i=lookup_0.begin();i!=lookup_0.end();i++) {
2295            tmp_2->addTag(i->first); // use tmp_2 to get correct shape
2296          }
2297          for (i=lookup_1.begin();i!=lookup_1.end();i++) {
2298            tmp_2->addTag(i->first);
2299          }
2300          // Compute a result for each tag
2301          const DataTagged::DataMapType& lookup_2=tmp_2->getTagLookup();
2302          for (i=lookup_2.begin();i!=lookup_2.end();i++) {
2303    
2304            const double *ptr_0 = &(tmp_0->getDataByTagRO(i->first,0));
2305            const double *ptr_1 = &(tmp_1->getDataByTagRO(i->first,0));
2306            double *ptr_2 = &(tmp_2->getDataByTagRW(i->first,0));
2307    
2308            tensor_binary_operation(size0, ptr_0, ptr_1, ptr_2, operation);
2309          }
2310    
2311        }
2312        else if (arg_0_Z.isTagged()     && arg_1_Z.isExpanded()) {
2313          // After finding a common function space above the two inputs have the same numSamples and num DPPS
2314          res = Data(0.0, shape0, arg_1_Z.getFunctionSpace(),true); // DataExpanded output
2315          DataTagged*   tmp_0=dynamic_cast<DataTagged*>(arg_0_Z.borrowData());
2316          DataExpanded* tmp_1=dynamic_cast<DataExpanded*>(arg_1_Z.borrowData());
2317          DataExpanded* tmp_2=dynamic_cast<DataExpanded*>(res.borrowData());
2318    
2319          int sampleNo_0,dataPointNo_0;
2320          int numSamples_0 = arg_0_Z.getNumSamples();
2321          int numDataPointsPerSample_0 = arg_0_Z.getNumDataPointsPerSample();
2322          res.requireWrite();
2323          #pragma omp parallel for private(sampleNo_0,dataPointNo_0) schedule(static)
2324          for (sampleNo_0 = 0; sampleNo_0 < numSamples_0; sampleNo_0++) {
2325            int offset_0 = tmp_0->getPointOffset(sampleNo_0,0); // They're all the same, so just use #0
2326            const double *ptr_0 = &(arg_0_Z.getDataAtOffsetRO(offset_0));
2327            for (dataPointNo_0 = 0; dataPointNo_0 < numDataPointsPerSample_0; dataPointNo_0++) {
2328              int offset_1 = tmp_1->getPointOffset(sampleNo_0,dataPointNo_0);
2329              int offset_2 = tmp_2->getPointOffset(sampleNo_0,dataPointNo_0);
2330              const double *ptr_1 = &(arg_1_Z.getDataAtOffsetRO(offset_1));
2331              double *ptr_2 = &(res.getDataAtOffsetRW(offset_2));
2332              tensor_binary_operation(size0, ptr_0, ptr_1, ptr_2, operation);
2333            }
2334          }
2335    
2336        }
2337        else if (arg_0_Z.isExpanded()   && arg_1_Z.isConstant()) {
2338          res = Data(0.0, shape0, arg_1_Z.getFunctionSpace(),true); // DataExpanded output
2339          DataExpanded* tmp_0=dynamic_cast<DataExpanded*>(arg_0_Z.borrowData());
2340          DataConstant* tmp_1=dynamic_cast<DataConstant*>(arg_1_Z.borrowData());
2341          DataExpanded* tmp_2=dynamic_cast<DataExpanded*>(res.borrowData());
2342    
2343          int sampleNo_0,dataPointNo_0;
2344          int numSamples_0 = arg_0_Z.getNumSamples();
2345          int numDataPointsPerSample_0 = arg_0_Z.getNumDataPointsPerSample();
2346          int offset_1 = tmp_1->getPointOffset(0,0);
2347          res.requireWrite();
2348          #pragma omp parallel for private(sampleNo_0,dataPointNo_0) schedule(static)
2349          for (sampleNo_0 = 0; sampleNo_0 < numSamples_0; sampleNo_0++) {
2350            for (dataPointNo_0 = 0; dataPointNo_0 < numDataPointsPerSample_0; dataPointNo_0++) {
2351              int offset_0 = tmp_0->getPointOffset(sampleNo_0,dataPointNo_0);
2352              int offset_2 = tmp_2->getPointOffset(sampleNo_0,dataPointNo_0);
2353    
2354              const double *ptr_0 = &(arg_0_Z.getDataAtOffsetRO(offset_0));
2355              const double *ptr_1 = &(arg_1_Z.getDataAtOffsetRO(offset_1));
2356              double *ptr_2 = &(res.getDataAtOffsetRW(offset_2));
2357    
2358    
2359              tensor_binary_operation(size0, ptr_0, ptr_1, ptr_2, operation);
2360            }
2361          }
2362    
2363        }
2364        else if (arg_0_Z.isExpanded()   && arg_1_Z.isTagged()) {
2365          // After finding a common function space above the two inputs have the same numSamples and num DPPS
2366          res = Data(0.0, shape0, arg_1_Z.getFunctionSpace(),true); // DataExpanded output
2367          DataExpanded* tmp_0=dynamic_cast<DataExpanded*>(arg_0_Z.borrowData());
2368          DataTagged*   tmp_1=dynamic_cast<DataTagged*>(arg_1_Z.borrowData());
2369          DataExpanded* tmp_2=dynamic_cast<DataExpanded*>(res.borrowData());
2370    
2371          int sampleNo_0,dataPointNo_0;
2372          int numSamples_0 = arg_0_Z.getNumSamples();
2373          int numDataPointsPerSample_0 = arg_0_Z.getNumDataPointsPerSample();
2374          res.requireWrite();
2375          #pragma omp parallel for private(sampleNo_0,dataPointNo_0) schedule(static)
2376          for (sampleNo_0 = 0; sampleNo_0 < numSamples_0; sampleNo_0++) {
2377            int offset_1 = tmp_1->getPointOffset(sampleNo_0,0);
2378            const double *ptr_1 = &(arg_1_Z.getDataAtOffsetRO(offset_1));
2379            for (dataPointNo_0 = 0; dataPointNo_0 < numDataPointsPerSample_0; dataPointNo_0++) {
2380              int offset_0 = tmp_0->getPointOffset(sampleNo_0,dataPointNo_0);
2381              int offset_2 = tmp_2->getPointOffset(sampleNo_0,dataPointNo_0);
2382              const double *ptr_0 = &(arg_0_Z.getDataAtOffsetRO(offset_0));
2383              double *ptr_2 = &(res.getDataAtOffsetRW(offset_2));
2384              tensor_binary_operation(size0, ptr_0, ptr_1, ptr_2, operation);
2385            }
2386          }
2387    
2388        }
2389        else if (arg_0_Z.isExpanded()   && arg_1_Z.isExpanded()) {
2390          // After finding a common function space above the two inputs have the same numSamples and num DPPS
2391          res = Data(0.0, shape0, arg_1_Z.getFunctionSpace(),true); // DataExpanded output
2392          DataExpanded* tmp_0=dynamic_cast<DataExpanded*>(arg_0_Z.borrowData());
2393          DataExpanded* tmp_1=dynamic_cast<DataExpanded*>(arg_1_Z.borrowData());
2394          DataExpanded* tmp_2=dynamic_cast<DataExpanded*>(res.borrowData());
2395    
2396          int sampleNo_0,dataPointNo_0;
2397          int numSamples_0 = arg_0_Z.getNumSamples();
2398          int numDataPointsPerSample_0 = arg_0_Z.getNumDataPointsPerSample();
2399          res.requireWrite();
2400          #pragma omp parallel for private(sampleNo_0,dataPointNo_0) schedule(static)
2401          for (sampleNo_0 = 0; sampleNo_0 < numSamples_0; sampleNo_0++) {
2402            for (dataPointNo_0 = 0; dataPointNo_0 < numDataPointsPerSample_0; dataPointNo_0++) {
2403              int offset_0 = tmp_0->getPointOffset(sampleNo_0,dataPointNo_0);
2404              int offset_1 = tmp_1->getPointOffset(sampleNo_0,dataPointNo_0);
2405              int offset_2 = tmp_2->getPointOffset(sampleNo_0,dataPointNo_0);
2406              const double *ptr_0 = &(arg_0_Z.getDataAtOffsetRO(offset_0));
2407              const double *ptr_1 = &(arg_1_Z.getDataAtOffsetRO(offset_1));
2408              double *ptr_2 = &(res.getDataAtOffsetRW(offset_2));
2409              tensor_binary_operation(size0, ptr_0, ptr_1, ptr_2, operation);
2410            }
2411          }
2412    
2413        }
2414        else {
2415          throw DataException("Error - C_TensorBinaryOperation: unknown combination of inputs");
2416        }
2417    
2418      } else if (0 == rank0) {
2419        if (arg_0_Z.isConstant()   && arg_1_Z.isConstant()) {
2420          res = Data(0.0, shape1, arg_1_Z.getFunctionSpace());      // DataConstant output
2421          const double *ptr_0 = &(arg_0_Z.getDataAtOffsetRO(0));
2422          const double *ptr_1 = &(arg_1_Z.getDataAtOffsetRO(0));
2423          double *ptr_2 = &(res.getDataAtOffsetRW(0));
2424          tensor_binary_operation(size1, ptr_0[0], ptr_1, ptr_2, operation);
2425        }
2426        else if (arg_0_Z.isConstant()   && arg_1_Z.isTagged()) {
2427    
2428          // Prepare the DataConstant input
2429          DataConstant* tmp_0=dynamic_cast<DataConstant*>(arg_0_Z.borrowData());
2430    
2431          // Borrow DataTagged input from Data object
2432          DataTagged* tmp_1=dynamic_cast<DataTagged*>(arg_1_Z.borrowData());
2433    
2434          // Prepare a DataTagged output 2
2435          res = Data(0.0, shape1, arg_1_Z.getFunctionSpace());      // DataTagged output
2436          res.tag();
2437          DataTagged* tmp_2=dynamic_cast<DataTagged*>(res.borrowData());
2438    
2439          // Prepare offset into DataConstant
2440          int offset_0 = tmp_0->getPointOffset(0,0);
2441          const double *ptr_0 = &(arg_0_Z.getDataAtOffsetRO(offset_0));
2442    
2443          const double *ptr_1 = &(tmp_1->getDefaultValueRO(0));
2444          double *ptr_2 = &(tmp_2->getDefaultValueRW(0));
2445    
2446          // Compute a result for the default
2447          tensor_binary_operation(size1, ptr_0[0], ptr_1, ptr_2, operation);
2448          // Compute a result for each tag
2449          const DataTagged::DataMapType& lookup_1=tmp_1->getTagLookup();
2450          DataTagged::DataMapType::const_iterator i; // i->first is a tag, i->second is an offset into memory
2451          for (i=lookup_1.begin();i!=lookup_1.end();i++) {
2452            tmp_2->addTag(i->first);
2453            const double *ptr_1 = &(tmp_1->getDataByTagRO(i->first,0));
2454            double *ptr_2 = &(tmp_2->getDataByTagRW(i->first,0));
2455            tensor_binary_operation(size1, ptr_0[0], ptr_1, ptr_2, operation);
2456          }
2457    
2458        }
2459        else if (arg_0_Z.isConstant()   && arg_1_Z.isExpanded()) {
2460    
2461          res = Data(0.0, shape1, arg_1_Z.getFunctionSpace(),true); // DataExpanded output
2462          DataConstant* tmp_0=dynamic_cast<DataConstant*>(arg_0_Z.borrowData());
2463          DataExpanded* tmp_1=dynamic_cast<DataExpanded*>(arg_1_Z.borrowData());
2464          DataExpanded* tmp_2=dynamic_cast<DataExpanded*>(res.borrowData());
2465    
2466          int sampleNo_1,dataPointNo_1;
2467          int numSamples_1 = arg_1_Z.getNumSamples();
2468          int numDataPointsPerSample_1 = arg_1_Z.getNumDataPointsPerSample();
2469          int offset_0 = tmp_0->getPointOffset(0,0);
2470          res.requireWrite();
2471          #pragma omp parallel for private(sampleNo_1,dataPointNo_1) schedule(static)
2472          for (sampleNo_1 = 0; sampleNo_1 < numSamples_1; sampleNo_1++) {
2473            for (dataPointNo_1 = 0; dataPointNo_1 < numDataPointsPerSample_1; dataPointNo_1++) {
2474              int offset_1 = tmp_1->getPointOffset(sampleNo_1,dataPointNo_1);
2475              int offset_2 = tmp_2->getPointOffset(sampleNo_1,dataPointNo_1);
2476              const double *ptr_0 = &(arg_0_Z.getDataAtOffsetRO(offset_0));
2477              const double *ptr_1 = &(arg_1_Z.getDataAtOffsetRO(offset_1));
2478              double *ptr_2 = &(res.getDataAtOffsetRW(offset_2));
2479              tensor_binary_operation(size1, ptr_0[0], ptr_1, ptr_2, operation);
2480    
2481            }
2482          }
2483    
2484        }
2485        else if (arg_0_Z.isTagged()     && arg_1_Z.isConstant()) {
2486    
2487          // Borrow DataTagged input from Data object
2488          DataTagged* tmp_0=dynamic_cast<DataTagged*>(arg_0_Z.borrowData());
2489    
2490          // Prepare the DataConstant input
2491          DataConstant* tmp_1=dynamic_cast<DataConstant*>(arg_1_Z.borrowData());
2492    
2493          // Prepare a DataTagged output 2
2494          res = Data(0.0, shape1, arg_0_Z.getFunctionSpace());      // DataTagged output
2495          res.tag();
2496          DataTagged* tmp_2=dynamic_cast<DataTagged*>(res.borrowData());
2497    
2498          // Prepare offset into DataConstant
2499          int offset_1 = tmp_1->getPointOffset(0,0);
2500          const double *ptr_1 = &(arg_1_Z.getDataAtOffsetRO(offset_1));
2501    
2502          // Get the pointers to the actual data
2503          const double *ptr_0 = &(tmp_0->getDefaultValueRO(0));
2504          double *ptr_2 = &(tmp_2->getDefaultValueRW(0));
2505    
2506    
2507          // Compute a result for the default
2508          tensor_binary_operation(size1, ptr_0[0], ptr_1, ptr_2, operation);
2509          // Compute a result for each tag
2510          const DataTagged::DataMapType& lookup_0=tmp_0->getTagLookup();
2511          DataTagged::DataMapType::const_iterator i; // i->first is a tag, i->second is an offset into memory
2512          for (i=lookup_0.begin();i!=lookup_0.end();i++) {
2513            tmp_2->addTag(i->first);
2514            const double *ptr_0 = &(tmp_0->getDataByTagRO(i->first,0));
2515            double *ptr_2 = &(tmp_2->getDataByTagRW(i->first,0));
2516    
2517            tensor_binary_operation(size1, ptr_0[0], ptr_1, ptr_2, operation);
2518          }
2519    
2520        }
2521        else if (arg_0_Z.isTagged()     && arg_1_Z.isTagged()) {
2522    
2523          // Borrow DataTagged input from Data object
2524          DataTagged* tmp_0=dynamic_cast<DataTagged*>(arg_0_Z.borrowData());
2525    
2526          // Borrow DataTagged input from Data object
2527          DataTagged* tmp_1=dynamic_cast<DataTagged*>(arg_1_Z.borrowData());
2528    
2529          // Prepare a DataTagged output 2
2530          res = Data(0.0, shape1, arg_1_Z.getFunctionSpace());
2531          res.tag();        // DataTagged output
2532          DataTagged* tmp_2=dynamic_cast<DataTagged*>(res.borrowData());
2533    
2534          // Get the pointers to the actual data
2535          const double *ptr_0 = &(tmp_0->getDefaultValueRO(0));
2536          const double *ptr_1 = &(tmp_1->getDefaultValueRO(0));
2537          double *ptr_2 = &(tmp_2->getDefaultValueRW(0));
2538    
2539          // Compute a result for the default
2540          tensor_binary_operation(size1, ptr_0[0], ptr_1, ptr_2, operation);
2541          // Merge the tags
2542          DataTagged::DataMapType::const_iterator i; // i->first is a tag, i->second is an offset into memory
2543          const DataTagged::DataMapType& lookup_0=tmp_0->getTagLookup();
2544          const DataTagged::DataMapType& lookup_1=tmp_1->getTagLookup();
2545          for (i=lookup_0.begin();i!=lookup_0.end();i++) {
2546            tmp_2->addTag(i->first); // use tmp_2 to get correct shape
2547          }
2548          for (i=lookup_1.begin();i!=lookup_1.end();i++) {
2549            tmp_2->addTag(i->first);
2550          }
2551          // Compute a result for each tag
2552          const DataTagged::DataMapType& lookup_2=tmp_2->getTagLookup();
2553          for (i=lookup_2.begin();i!=lookup_2.end();i++) {
2554            const double *ptr_0 = &(tmp_0->getDataByTagRO(i->first,0));
2555            const double *ptr_1 = &(tmp_1->getDataByTagRO(i->first,0));
2556            double *ptr_2 = &(tmp_2->getDataByTagRW(i->first,0));
2557    
2558            tensor_binary_operation(size1, ptr_0[0], ptr_1, ptr_2, operation);
2559          }
2560    
2561        }
2562        else if (arg_0_Z.isTagged()     && arg_1_Z.isExpanded()) {
2563    
2564          // After finding a common function space above the two inputs have the same numSamples and num DPPS
2565          res = Data(0.0, shape1, arg_1_Z.getFunctionSpace(),true); // DataExpanded output
2566          DataTagged*   tmp_0=dynamic_cast<DataTagged*>(arg_0_Z.borrowData());
2567          DataExpanded* tmp_1=dynamic_cast<DataExpanded*>(arg_1_Z.borrowData());
2568          DataExpanded* tmp_2=dynamic_cast<DataExpanded*>(res.borrowData());
2569    
2570          int sampleNo_0,dataPointNo_0;
2571          int numSamples_0 = arg_0_Z.getNumSamples();
2572          int numDataPointsPerSample_0 = arg_0_Z.getNumDataPointsPerSample();
2573          res.requireWrite();
2574          #pragma omp parallel for private(sampleNo_0,dataPointNo_0) schedule(static)
2575          for (sampleNo_0 = 0; sampleNo_0 < numSamples_0; sampleNo_0++) {
2576            int offset_0 = tmp_0->getPointOffset(sampleNo_0,0); // They're all the same, so just use #0
2577            const double *ptr_0 = &(arg_0_Z.getDataAtOffsetRO(offset_0));
2578            for (dataPointNo_0 = 0; dataPointNo_0 < numDataPointsPerSample_0; dataPointNo_0++) {
2579              int offset_1 = tmp_1->getPointOffset(sampleNo_0,dataPointNo_0);
2580              int offset_2 = tmp_2->getPointOffset(sampleNo_0,dataPointNo_0);
2581              const double *ptr_1 = &(arg_1_Z.getDataAtOffsetRO(offset_1));
2582              double *ptr_2 = &(res.getDataAtOffsetRW(offset_2));
2583              tensor_binary_operation(size1, ptr_0[0], ptr_1, ptr_2, operation);
2584            }
2585          }
2586    
2587        }
2588        else if (arg_0_Z.isExpanded()   && arg_1_Z.isConstant()) {
2589          res = Data(0.0, shape1, arg_1_Z.getFunctionSpace(),true); // DataExpanded output
2590          DataExpanded* tmp_0=dynamic_cast<DataExpanded*>(arg_0_Z.borrowData());
2591          DataConstant* tmp_1=dynamic_cast<DataConstant*>(arg_1_Z.borrowData());
2592          DataExpanded* tmp_2=dynamic_cast<DataExpanded*>(res.borrowData());
2593    
2594          int sampleNo_0,dataPointNo_0;
2595          int numSamples_0 = arg_0_Z.getNumSamples();
2596          int numDataPointsPerSample_0 = arg_0_Z.getNumDataPointsPerSample();
2597          int offset_1 = tmp_1->getPointOffset(0,0);
2598          res.requireWrite();
2599          #pragma omp parallel for private(sampleNo_0,dataPointNo_0) schedule(static)
2600          for (sampleNo_0 = 0; sampleNo_0 < numSamples_0; sampleNo_0++) {
2601            for (dataPointNo_0 = 0; dataPointNo_0 < numDataPointsPerSample_0; dataPointNo_0++) {
2602              int offset_0 = tmp_0->getPointOffset(sampleNo_0,dataPointNo_0);
2603              int offset_2 = tmp_2->getPointOffset(sampleNo_0,dataPointNo_0);
2604              const double *ptr_0 = &(arg_0_Z.getDataAtOffsetRO(offset_0));
2605              const double *ptr_1 = &(arg_1_Z.getDataAtOffsetRO(offset_1));
2606              double *ptr_2 = &(res.getDataAtOffsetRW(offset_2));
2607              tensor_binary_operation(size1, ptr_0[0], ptr_1, ptr_2, operation);
2608            }
2609          }
2610    
2611    
2612        }
2613        else if (arg_0_Z.isExpanded()   && arg_1_Z.isTagged()) {
2614    
2615          // After finding a common function space above the two inputs have the same numSamples and num DPPS
2616          res = Data(0.0, shape1, arg_1_Z.getFunctionSpace(),true); // DataExpanded output
2617          DataExpanded* tmp_0=dynamic_cast<DataExpanded*>(arg_0_Z.borrowData());
2618          DataTagged*   tmp_1=dynamic_cast<DataTagged*>(arg_1_Z.borrowData());
2619          DataExpanded* tmp_2=dynamic_cast<DataExpanded*>(res.borrowData());
2620    
2621          int sampleNo_0,dataPointNo_0;
2622          int numSamples_0 = arg_0_Z.getNumSamples();
2623          int numDataPointsPerSample_0 = arg_0_Z.getNumDataPointsPerSample();
2624          res.requireWrite();
2625          #pragma omp parallel for private(sampleNo_0,dataPointNo_0) schedule(static)
2626          for (sampleNo_0 = 0; sampleNo_0 < numSamples_0; sampleNo_0++) {
2627            int offset_1 = tmp_1->getPointOffset(sampleNo_0,0);
2628            const double *ptr_1 = &(arg_1_Z.getDataAtOffsetRO(offset_1));
2629            for (dataPointNo_0 = 0; dataPointNo_0 < numDataPointsPerSample_0; dataPointNo_0++) {
2630              int offset_0 = tmp_0->getPointOffset(sampleNo_0,dataPointNo_0);
2631              int offset_2 = tmp_2->getPointOffset(sampleNo_0,dataPointNo_0);
2632              const double *ptr_0 = &(arg_0_Z.getDataAtOffsetRO(offset_0));
2633              double *ptr_2 = &(res.getDataAtOffsetRW(offset_2));
2634              tensor_binary_operation(size1, ptr_0[0], ptr_1, ptr_2, operation);
2635            }
2636          }
2637    
2638        }
2639        else if (arg_0_Z.isExpanded()   && arg_1_Z.isExpanded()) {
2640    
2641          // After finding a common function space above the two inputs have the same numSamples and num DPPS
2642          res = Data(0.0, shape1, arg_1_Z.getFunctionSpace(),true); // DataExpanded output
2643          DataExpanded* tmp_0=dynamic_cast<DataExpanded*>(arg_0_Z.borrowData());
2644          DataExpanded* tmp_1=dynamic_cast<DataExpanded*>(arg_1_Z.borrowData());
2645          DataExpanded* tmp_2=dynamic_cast<DataExpanded*>(res.borrowData());
2646    
2647          int sampleNo_0,dataPointNo_0;
2648          int numSamples_0 = arg_0_Z.getNumSamples();
2649          int numDataPointsPerSample_0 = arg_0_Z.getNumDataPointsPerSample();
2650          res.requireWrite();
2651          #pragma omp parallel for private(sampleNo_0,dataPointNo_0) schedule(static)
2652          for (sampleNo_0 = 0; sampleNo_0 < numSamples_0; sampleNo_0++) {
2653            for (dataPointNo_0 = 0; dataPointNo_0 < numDataPointsPerSample_0; dataPointNo_0++) {
2654              int offset_0 = tmp_0->getPointOffset(sampleNo_0,dataPointNo_0);
2655              int offset_1 = tmp_1->getPointOffset(sampleNo_0,dataPointNo_0);
2656              int offset_2 = tmp_2->getPointOffset(sampleNo_0,dataPointNo_0);
2657              const double *ptr_0 = &(arg_0_Z.getDataAtOffsetRO(offset_0));
2658              const double *ptr_1 = &(arg_1_Z.getDataAtOffsetRO(offset_1));
2659              double *ptr_2 = &(res.getDataAtOffsetRW(offset_2));
2660              tensor_binary_operation(size1, ptr_0[0], ptr_1, ptr_2, operation);
2661            }
2662          }
2663    
2664        }
2665        else {
2666          throw DataException("Error - C_TensorBinaryOperation: unknown combination of inputs");
2667        }
2668    
2669      } else if (0 == rank1) {
2670        if (arg_0_Z.isConstant()   && arg_1_Z.isConstant()) {
2671          res = Data(0.0, shape0, arg_1_Z.getFunctionSpace());      // DataConstant output
2672          const double *ptr_0 = &(arg_0_Z.getDataAtOffsetRO(0));
2673          const double *ptr_1 = &(arg_1_Z.getDataAtOffsetRO(0));
2674          double *ptr_2 = &(res.getDataAtOffsetRW(0));
2675          tensor_binary_operation(size0, ptr_0, ptr_1[0], ptr_2, operation);
2676        }
2677        else if (arg_0_Z.isConstant()   && arg_1_Z.isTagged()) {
2678    
2679          // Prepare the DataConstant input
2680          DataConstant* tmp_0=dynamic_cast<DataConstant*>(arg_0_Z.borrowData());
2681    
2682          // Borrow DataTagged input from Data object
2683          DataTagged* tmp_1=dynamic_cast<DataTagged*>(arg_1_Z.borrowData());
2684    
2685          // Prepare a DataTagged output 2
2686          res = Data(0.0, shape0, arg_1_Z.getFunctionSpace());      // DataTagged output
2687          res.tag();
2688          DataTagged* tmp_2=dynamic_cast<DataTagged*>(res.borrowData());
2689    
2690          // Prepare offset into DataConstant
2691          int offset_0 = tmp_0->getPointOffset(0,0);
2692          const double *ptr_0 = &(arg_0_Z.getDataAtOffsetRO(offset_0));
2693    
2694          //Get the pointers to the actual data
2695          const double *ptr_1 = &(tmp_1->getDefaultValueRO(0));
2696          double *ptr_2 = &(tmp_2->getDefaultValueRW(0));
2697    
2698          // Compute a result for the default
2699          tensor_binary_operation(size0, ptr_0, ptr_1[0], ptr_2, operation);
2700          // Compute a result for each tag
2701          const DataTagged::DataMapType& lookup_1=tmp_1->getTagLookup();
2702          DataTagged::DataMapType::const_iterator i; // i->first is a tag, i->second is an offset into memory
2703          for (i=lookup_1.begin();i!=lookup_1.end();i++) {
2704            tmp_2->addTag(i->first);
2705            const double *ptr_1 = &(tmp_1->getDataByTagRO(i->first,0));
2706            double *ptr_2 = &(tmp_2->getDataByTagRW(i->first,0));
2707            tensor_binary_operation(size0, ptr_0, ptr_1[0], ptr_2, operation);
2708          }
2709        }
2710        else if (arg_0_Z.isConstant()   && arg_1_Z.isExpanded()) {
2711    
2712          res = Data(0.0, shape0, arg_1_Z.getFunctionSpace(),true); // DataExpanded output
2713          DataConstant* tmp_0=dynamic_cast<DataConstant*>(arg_0_Z.borrowData());
2714          DataExpanded* tmp_1=dynamic_cast<DataExpanded*>(arg_1_Z.borrowData());
2715          DataExpanded* tmp_2=dynamic_cast<DataExpanded*>(res.borrowData());
2716    
2717          int sampleNo_1,dataPointNo_1;
2718          int numSamples_1 = arg_1_Z.getNumSamples();
2719          int numDataPointsPerSample_1 = arg_1_Z.getNumDataPointsPerSample();
2720          int offset_0 = tmp_0->getPointOffset(0,0);
2721          res.requireWrite();
2722          #pragma omp parallel for private(sampleNo_1,dataPointNo_1) schedule(static)
2723          for (sampleNo_1 = 0; sampleNo_1 < numSamples_1; sampleNo_1++) {
2724            for (dataPointNo_1 = 0; dataPointNo_1 < numDataPointsPerSample_1; dataPointNo_1++) {
2725              int offset_1 = tmp_1->getPointOffset(sampleNo_1,dataPointNo_1);
2726              int offset_2 = tmp_2->getPointOffset(sampleNo_1,dataPointNo_1);
2727              const double *ptr_0 = &(arg_0_Z.getDataAtOffsetRO(offset_0));
2728              const double *ptr_1 = &(arg_1_Z.getDataAtOffsetRO(offset_1));
2729              double *ptr_2 = &(res.getDataAtOffsetRW(offset_2));
2730              tensor_binary_operation(size0, ptr_0, ptr_1[0], ptr_2, operation);
2731            }
2732          }
2733    
2734        }
2735        else if (arg_0_Z.isTagged()     && arg_1_Z.isConstant()) {
2736    
2737          // Borrow DataTagged input from Data object
2738          DataTagged* tmp_0=dynamic_cast<DataTagged*>(arg_0_Z.borrowData());
2739    
2740          // Prepare the DataConstant input
2741          DataConstant* tmp_1=dynamic_cast<DataConstant*>(arg_1_Z.borrowData());
2742    
2743          // Prepare a DataTagged output 2
2744          res = Data(0.0, shape0, arg_0_Z.getFunctionSpace());      // DataTagged output
2745          res.tag();
2746          DataTagged* tmp_2=dynamic_cast<DataTagged*>(res.borrowData());
2747    
2748          // Prepare offset into DataConstant
2749          int offset_1 = tmp_1->getPointOffset(0,0);
2750          const double *ptr_1 = &(arg_1_Z.getDataAtOffsetRO(offset_1));
2751          // Get the pointers to the actual data
2752          const double *ptr_0 = &(tmp_0->getDefaultValueRO(0));
2753          double *ptr_2 = &(tmp_2->getDefaultValueRW(0));
2754          // Compute a result for the default
2755          tensor_binary_operation(size0, ptr_0, ptr_1[0], ptr_2, operation);
2756          // Compute a result for each tag
2757          const DataTagged::DataMapType& lookup_0=tmp_0->getTagLookup();
2758          DataTagged::DataMapType::const_iterator i; // i->first is a tag, i->second is an offset into memory
2759          for (i=lookup_0.begin();i!=lookup_0.end();i++) {
2760            tmp_2->addTag(i->first);
2761            const double *ptr_0 = &(tmp_0->getDataByTagRO(i->first,0));
2762            double *ptr_2 = &(tmp_2->getDataByTagRW(i->first,0));
2763            tensor_binary_operation(size0, ptr_0, ptr_1[0], ptr_2, operation);
2764          }
2765    
2766        }
2767        else if (arg_0_Z.isTagged()     && arg_1_Z.isTagged()) {
2768    
2769          // Borrow DataTagged input from Data object
2770          DataTagged* tmp_0=dynamic_cast<DataTagged*>(arg_0_Z.borrowData());
2771    
2772          // Borrow DataTagged input from Data object
2773          DataTagged* tmp_1=dynamic_cast<DataTagged*>(arg_1_Z.borrowData());
2774    
2775          // Prepare a DataTagged output 2
2776          res = Data(0.0, shape0, arg_1_Z.getFunctionSpace());
2777          res.tag();        // DataTagged output
2778          DataTagged* tmp_2=dynamic_cast<DataTagged*>(res.borrowData());
2779    
2780          // Get the pointers to the actual data
2781          const double *ptr_0 = &(tmp_0->getDefaultValueRO(0));
2782          const double *ptr_1 = &(tmp_1->getDefaultValueRO(0));
2783          double *ptr_2 = &(tmp_2->getDefaultValueRW(0));
2784    
2785          // Compute a result for the default
2786          tensor_binary_operation(size0, ptr_0, ptr_1[0], ptr_2, operation);
2787          // Merge the tags
2788          DataTagged::DataMapType::const_iterator i; // i->first is a tag, i->second is an offset into memory
2789          const DataTagged::DataMapType& lookup_0=tmp_0->getTagLookup();
2790          const DataTagged::DataMapType& lookup_1=tmp_1->getTagLookup();
2791          for (i=lookup_0.begin();i!=lookup_0.end();i++) {
2792            tmp_2->addTag(i->first); // use tmp_2 to get correct shape
2793          }
2794          for (i=lookup_1.begin();i!=lookup_1.end();i++) {
2795            tmp_2->addTag(i->first);
2796          }
2797          // Compute a result for each tag
2798          const DataTagged::DataMapType& lookup_2=tmp_2->getTagLookup();
2799          for (i=lookup_2.begin();i!=lookup_2.end();i++) {
2800            const double *ptr_0 = &(tmp_0->getDataByTagRO(i->first,0));
2801            const double *ptr_1 = &(tmp_1->getDataByTagRO(i->first,0));
2802            double *ptr_2 = &(tmp_2->getDataByTagRW(i->first,0));
2803            tensor_binary_operation(size0, ptr_0, ptr_1[0], ptr_2, operation);
2804          }
2805    
2806        }
2807        else if (arg_0_Z.isTagged()     && arg_1_Z.isExpanded()) {
2808    
2809          // After finding a common function space above the two inputs have the same numSamples and num DPPS
2810          res = Data(0.0, shape0, arg_1_Z.getFunctionSpace(),true); // DataExpanded output
2811          DataTagged*   tmp_0=dynamic_cast<DataTagged*>(arg_0_Z.borrowData());
2812          DataExpanded* tmp_1=dynamic_cast<DataExpanded*>(arg_1_Z.borrowData());
2813          DataExpanded* tmp_2=dynamic_cast<DataExpanded*>(res.borrowData());
2814    
2815          int sampleNo_0,dataPointNo_0;
2816          int numSamples_0 = arg_0_Z.getNumSamples();
2817          int numDataPointsPerSample_0 = arg_0_Z.getNumDataPointsPerSample();
2818          res.requireWrite();
2819          #pragma omp parallel for private(sampleNo_0,dataPointNo_0) schedule(static)
2820          for (sampleNo_0 = 0; sampleNo_0 < numSamples_0; sampleNo_0++) {
2821            int offset_0 = tmp_0->getPointOffset(sampleNo_0,0); // They're all the same, so just use #0
2822            const double *ptr_0 = &(arg_0_Z.getDataAtOffsetRO(offset_0));
2823            for (dataPointNo_0 = 0; dataPointNo_0 < numDataPointsPerSample_0; dataPointNo_0++) {
2824              int offset_1 = tmp_1->getPointOffset(sampleNo_0,dataPointNo_0);
2825              int offset_2 = tmp_2->getPointOffset(sampleNo_0,dataPointNo_0);
2826              const double *ptr_1 = &(arg_1_Z.getDataAtOffsetRO(offset_1));
2827              double *ptr_2 = &(res.getDataAtOffsetRW(offset_2));
2828              tensor_binary_operation(size0, ptr_0, ptr_1[0], ptr_2, operation);
2829            }
2830          }
2831    
2832        }
2833        else if (arg_0_Z.isExpanded()   && arg_1_Z.isConstant()) {
2834          res = Data(0.0, shape0, arg_1_Z.getFunctionSpace(),true); // DataExpanded output
2835          DataExpanded* tmp_0=dynamic_cast<DataExpanded*>(arg_0_Z.borrowData());
2836          DataConstant* tmp_1=dynamic_cast<DataConstant*>(arg_1_Z.borrowData());
2837          DataExpanded* tmp_2=dynamic_cast<DataExpanded*>(res.borrowData());
2838    
2839          int sampleNo_0,dataPointNo_0;
2840          int numSamples_0 = arg_0_Z.getNumSamples();
2841          int numDataPointsPerSample_0 = arg_0_Z.getNumDataPointsPerSample();
2842          int offset_1 = tmp_1->getPointOffset(0,0);
2843          res.requireWrite();
2844          #pragma omp parallel for private(sampleNo_0,dataPointNo_0) schedule(static)
2845          for (sampleNo_0 = 0; sampleNo_0 < numSamples_0; sampleNo_0++) {
2846            for (dataPointNo_0 = 0; dataPointNo_0 < numDataPointsPerSample_0; dataPointNo_0++) {
2847              int offset_0 = tmp_0->getPointOffset(sampleNo_0,dataPointNo_0);
2848              int offset_2 = tmp_2->getPointOffset(sampleNo_0,dataPointNo_0);
2849              const double *ptr_0 = &(arg_0_Z.getDataAtOffsetRO(offset_0));
2850              const double *ptr_1 = &(arg_1_Z.getDataAtOffsetRO(offset_1));
2851              double *ptr_2 = &(res.getDataAtOffsetRW(offset_2));
2852              tensor_binary_operation(size0, ptr_0, ptr_1[0], ptr_2, operation);
2853            }
2854          }
2855    
2856    
2857        }
2858        else if (arg_0_Z.isExpanded()   && arg_1_Z.isTagged()) {
2859    
2860          // After finding a common function space above the two inputs have the same numSamples and num DPPS
2861          res = Data(0.0, shape0, arg_1_Z.getFunctionSpace(),true); // DataExpanded output
2862          DataExpanded* tmp_0=dynamic_cast<DataExpanded*>(arg_0_Z.borrowData());
2863          DataTagged*   tmp_1=dynamic_cast<DataTagged*>(arg_1_Z.borrowData());
2864          DataExpanded* tmp_2=dynamic_cast<DataExpanded*>(res.borrowData());
2865    
2866          int sampleNo_0,dataPointNo_0;
2867          int numSamples_0 = arg_0_Z.getNumSamples();
2868          int numDataPointsPerSample_0 = arg_0_Z.getNumDataPointsPerSample();
2869          res.requireWrite();
2870          #pragma omp parallel for private(sampleNo_0,dataPointNo_0) schedule(static)
2871          for (sampleNo_0 = 0; sampleNo_0 < numSamples_0; sampleNo_0++) {
2872            int offset_1 = tmp_1->getPointOffset(sampleNo_0,0);
2873            const double *ptr_1 = &(arg_1_Z.getDataAtOffsetRO(offset_1));
2874            for (dataPointNo_0 = 0; dataPointNo_0 < numDataPointsPerSample_0; dataPointNo_0++) {
2875              int offset_0 = tmp_0->getPointOffset(sampleNo_0,dataPointNo_0);
2876              int offset_2 = tmp_2->getPointOffset(sampleNo_0,dataPointNo_0);
2877              const double *ptr_0 = &(arg_0_Z.getDataAtOffsetRO(offset_0));
2878              double *ptr_2 = &(res.getDataAtOffsetRW(offset_2));
2879              tensor_binary_operation(size0, ptr_0, ptr_1[0], ptr_2, operation);
2880            }
2881          }
2882    
2883        }
2884        else if (arg_0_Z.isExpanded()   && arg_1_Z.isExpanded()) {
2885    
2886          // After finding a common function space above the two inputs have the same numSamples and num DPPS
2887          res = Data(0.0, shape0, arg_1_Z.getFunctionSpace(),true); // DataExpanded output
2888          DataExpanded* tmp_0=dynamic_cast<DataExpanded*>(arg_0_Z.borrowData());
2889          DataExpanded* tmp_1=dynamic_cast<DataExpanded*>(arg_1_Z.borrowData());
2890          DataExpanded* tmp_2=dynamic_cast<DataExpanded*>(res.borrowData());
2891    
2892          int sampleNo_0,dataPointNo_0;
2893          int numSamples_0 = arg_0_Z.getNumSamples();
2894          int numDataPointsPerSample_0 = arg_0_Z.getNumDataPointsPerSample();
2895          res.requireWrite();
2896          #pragma omp parallel for private(sampleNo_0,dataPointNo_0) schedule(static)
2897          for (sampleNo_0 = 0; sampleNo_0 < numSamples_0; sampleNo_0++) {
2898            for (dataPointNo_0 = 0; dataPointNo_0 < numDataPointsPerSample_0; dataPointNo_0++) {
2899              int offset_0 = tmp_0->getPointOffset(sampleNo_0,dataPointNo_0);
2900              int offset_1 = tmp_1->getPointOffset(sampleNo_0,dataPointNo_0);
2901              int offset_2 = tmp_2->getPointOffset(sampleNo_0,dataPointNo_0);
2902              const double *ptr_0 = &(arg_0_Z.getDataAtOffsetRO(offset_0));
2903              const double *ptr_1 = &(arg_1_Z.getDataAtOffsetRO(offset_1));
2904              double *ptr_2 = &(res.getDataAtOffsetRW(offset_2));
2905              tensor_binary_operation(size0, ptr_0, ptr_1[0], ptr_2, operation);
2906            }
2907          }
2908    
2909        }
2910        else {
2911          throw DataException("Error - C_TensorBinaryOperation: unknown combination of inputs");
2912        }
2913    
2914      } else {
2915        throw DataException("Error - C_TensorBinaryOperation: arguments have incompatible shapes");
2916      }
2917    
2918      return res;
2919    }
2920    
2921    template <typename UnaryFunction>
2922    Data
2923    C_TensorUnaryOperation(Data const &arg_0,
2924                           UnaryFunction operation)
2925    {
2926      if (arg_0.isEmpty())  // do this before we attempt to interpolate
2927      {
2928         throw DataException("Error - Operations not permitted on instances of DataEmpty.");
2929      }
2930      if (arg_0.isLazy())
2931      {
2932         throw DataException("Error - Operations not permitted on lazy data.");
2933      }
2934      // Interpolate if necessary and find an appropriate function space
2935      Data arg_0_Z = Data(arg_0);
2936    
2937      // Get rank and shape of inputs
2938      const DataTypes::ShapeType& shape0 = arg_0_Z.getDataPointShape();
2939      int size0 = arg_0_Z.getDataPointSize();
2940    
2941      // Declare output Data object
2942      Data res;
2943    
2944      if (arg_0_Z.isConstant()) {
2945        res = Data(0.0, shape0, arg_0_Z.getFunctionSpace());      // DataConstant output
2946        const double *ptr_0 = &(arg_0_Z.getDataAtOffsetRO(0));
2947        double *ptr_2 = &(res.getDataAtOffsetRW(0));
2948        tensor_unary_operation(size0, ptr_0, ptr_2, operation);
2949      }
2950      else if (arg_0_Z.isTagged()) {
2951    
2952        // Borrow DataTagged input from Data object
2953        DataTagged* tmp_0=dynamic_cast<DataTagged*>(arg_0_Z.borrowData());
2954    
2955        // Prepare a DataTagged output 2
2956        res = Data(0.0, shape0, arg_0_Z.getFunctionSpace());   // DataTagged output
2957        res.tag();
2958        DataTagged* tmp_2=dynamic_cast<DataTagged*>(res.borrowData());
2959    
2960        // Get the pointers to the actual data
2961        const double *ptr_0 = &(tmp_0->getDefaultValueRO(0));
2962        double *ptr_2 = &(tmp_2->getDefaultValueRW(0));
2963        // Compute a result for the default
2964        tensor_unary_operation(size0, ptr_0, ptr_2, operation);
2965        // Compute a result for each tag
2966        const DataTagged::DataMapType& lookup_0=tmp_0->getTagLookup();
2967        DataTagged::DataMapType::const_iterator i; // i->first is a tag, i->second is an offset into memory
2968        for (i=lookup_0.begin();i!=lookup_0.end();i++) {
2969          tmp_2->addTag(i->first);
2970          const double *ptr_0 = &(tmp_0->getDataByTagRO(i->first,0));
2971          double *ptr_2 = &(tmp_2->getDataByTagRW(i->first,0));
2972          tensor_unary_operation(size0, ptr_0, ptr_2, operation);
2973        }
2974    
2975      }
2976      else if (arg_0_Z.isExpanded()) {
2977    
2978        res = Data(0.0, shape0, arg_0_Z.getFunctionSpace(),true); // DataExpanded output
2979        DataExpanded* tmp_0=dynamic_cast<DataExpanded*>(arg_0_Z.borrowData());
2980        DataExpanded* tmp_2=dynamic_cast<DataExpanded*>(res.borrowData());
2981    
2982        int sampleNo_0,dataPointNo_0;
2983        int numSamples_0 = arg_0_Z.getNumSamples();
2984        int numDataPointsPerSample_0 = arg_0_Z.getNumDataPointsPerSample();
2985        #pragma omp parallel for private(sampleNo_0,dataPointNo_0) schedule(static)
2986        for (sampleNo_0 = 0; sampleNo_0 < numSamples_0; sampleNo_0++) {
2987          for (dataPointNo_0 = 0; dataPointNo_0 < numDataPointsPerSample_0; dataPointNo_0++) {
2988            int offset_0 = tmp_0->getPointOffset(sampleNo_0,dataPointNo_0);
2989            int offset_2 = tmp_2->getPointOffset(sampleNo_0,dataPointNo_0);
2990            const double *ptr_0 = &(arg_0_Z.getDataAtOffsetRO(offset_0));
2991            double *ptr_2 = &(res.getDataAtOffsetRW(offset_2));
2992            tensor_unary_operation(size0, ptr_0, ptr_2, operation);
2993          }
2994        }
2995      }
2996      else {
2997        throw DataException("Error - C_TensorUnaryOperation: unknown combination of inputs");
2998      }
2999    
3000      return res;
3001  }  }
3002    
3003  }  }

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

  ViewVC Help
Powered by ViewVC 1.1.26