/[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 2785 by lgao, Thu Nov 26 05:17:40 2009 UTC
# Line 1  Line 1 
1  // $Id$  
2  /*  /*******************************************************
3   ************************************************************  *
4   *          Copyright 2006 by ACcESS MNRF                   *  * Copyright (c) 2003-2009 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    #ifdef _OPENMP
36    #include <omp.h>
37    #endif
38    
39    #include "esysmpi.h"
40  #include <string>  #include <string>
41  #include <algorithm>  #include <algorithm>
42    #include <sstream>
43    
44  #include <boost/shared_ptr.hpp>  #include <boost/shared_ptr.hpp>
45  #include <boost/python/object.hpp>  #include <boost/python/object.hpp>
46  #include <boost/python/tuple.hpp>  #include <boost/python/tuple.hpp>
 #include <boost/python/numeric.hpp>  
47    
48  namespace escript {  namespace escript {
49    
# Line 44  namespace escript { Line 52  namespace escript {
52  class DataConstant;  class DataConstant;
53  class DataTagged;  class DataTagged;
54  class DataExpanded;  class DataExpanded;
55    class DataLazy;
56    
57  /**  /**
58     \brief     \brief
59     Data creates the appropriate Data object for the given construction     Data represents a collection of datapoints.
    arguments.  
60    
61     Description:     Description:
62     Data is essentially a factory class which creates the appropriate Data     Internally, the datapoints are actually stored by a DataAbstract object.
63     object for the given construction arguments. It retains control over     The specific instance of DataAbstract used may vary over the lifetime
64     the object created for the lifetime of the object.     of the Data object.
65     The type of Data object referred to may change during the lifetime of     Some methods on this class return references (eg getShape()).
66     the Data object.     These references should not be used after an operation which changes the underlying DataAbstract object.
67       Doing so will lead to invalid memory access.
68       This should not affect any methods exposed via boost::python.
69  */  */
70  class Data {  class Data {
71    
# Line 66  class Data { Line 76  class Data {
76    typedef double (*UnaryDFunPtr)(double);    typedef double (*UnaryDFunPtr)(double);
77    typedef double (*BinaryDFunPtr)(double,double);    typedef double (*BinaryDFunPtr)(double,double);
78    
79    
80    /**    /**
81       Constructors.       Constructors.
82    */    */
# Line 97  class Data { Line 108  class Data {
108         const FunctionSpace& what);         const FunctionSpace& what);
109    
110    /**    /**
111       \brief      \brief Copy Data from an existing vector
112       Constructor which copies data from a DataArrayView.    */
113    
      \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.  
   */  
114    ESCRIPT_DLL_API    ESCRIPT_DLL_API
115    Data(const DataArrayView& value,    Data(const DataTypes::ValueType& value,
116         const FunctionSpace& what=FunctionSpace(),           const DataTypes::ShapeType& shape,
117         bool expanded=false);                   const FunctionSpace& what=FunctionSpace(),
118                     bool expanded=false);
119    
120    /**    /**
121       \brief       \brief
122       Constructor which creates a Data from a DataArrayView shape.       Constructor which creates a Data with points having the specified shape.
123    
124       \param value - Input - Single value applied to all Data.       \param value - Input - Single value applied to all Data.
125       \param dataPointShape - Input - The shape of each data point.       \param dataPointShape - Input - The shape of each data point.
# Line 124  class Data { Line 130  class Data {
130    */    */
131    ESCRIPT_DLL_API    ESCRIPT_DLL_API
132    Data(double value,    Data(double value,
133         const DataArrayView::ShapeType& dataPointShape=DataArrayView::ShapeType(),         const DataTypes::ShapeType& dataPointShape=DataTypes::ShapeType(),
134         const FunctionSpace& what=FunctionSpace(),         const FunctionSpace& what=FunctionSpace(),
135         bool expanded=false);         bool expanded=false);
136    
# Line 137  class Data { Line 143  class Data {
143    */    */
144    ESCRIPT_DLL_API    ESCRIPT_DLL_API
145    Data(const Data& inData,    Data(const Data& inData,
146         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);  
147    
148    /**    /**
149       \brief       \brief
150       Constructor which copies data from a python numarray.       Constructor which copies data from any object that can be treated like a python array/sequence.
   
      \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);  
   
   /**  
      \brief  
      Constructor which copies data from any object that can be converted into  
      a python numarray.  
151    
152       \param value - Input - Input data.       \param value - Input - Input data.
153       \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 163  class Data {
163    /**    /**
164       \brief       \brief
165       Constructor which creates a DataConstant.       Constructor which creates a DataConstant.
166       Copies data from any object that can be converted       Copies data from any object that can be treated like a python array/sequence.
167       into a numarray. All other parameters are copied from other.       All other parameters are copied from other.
168    
169       \param value - Input - Input data.       \param value - Input - Input data.
170       \param other - Input - contains all other parameters.       \param other - Input - contains all other parameters.
# Line 209  class Data { Line 178  class Data {
178       Constructor which creates a DataConstant of "shape" with constant value.       Constructor which creates a DataConstant of "shape" with constant value.
179    */    */
180    ESCRIPT_DLL_API    ESCRIPT_DLL_API
181    Data(double value,    Data(double value,
182         const boost::python::tuple& shape=boost::python::make_tuple(),         const boost::python::tuple& shape=boost::python::make_tuple(),
183         const FunctionSpace& what=FunctionSpace(),         const FunctionSpace& what=FunctionSpace(),
184         bool expanded=false);         bool expanded=false);
185    
186    
187    
188      /**
189        \brief Create a Data using an existing DataAbstract. Warning: The new object assumes ownership of the pointer!
190        Once you have passed the pointer, do not delete it.
191      */
192      ESCRIPT_DLL_API
193      explicit Data(DataAbstract* underlyingdata);
194    
195      /**
196        \brief Create a Data based on the supplied DataAbstract
197      */
198      ESCRIPT_DLL_API
199      explicit Data(DataAbstract_ptr underlyingdata);
200    
201    /**    /**
202       \brief       \brief
203       Destructor       Destructor
# Line 221  class Data { Line 206  class Data {
206    ~Data();    ~Data();
207    
208    /**    /**
209       \brief       \brief Make this object a deep copy of "other".
      Perform a deep copy.  
210    */    */
211    ESCRIPT_DLL_API    ESCRIPT_DLL_API
212    void    void
213    copy(const Data& other);    copy(const Data& other);
214    
215    /**    /**
216         \brief Return a pointer to a deep copy of this object.
217      */
218      ESCRIPT_DLL_API
219      Data
220      copySelf();
221    
222    
223      /**
224         \brief produce a delayed evaluation version of this Data.
225      */
226      ESCRIPT_DLL_API
227      Data
228      delay();
229    
230      /**
231         \brief convert the current data into lazy data.
232      */
233      ESCRIPT_DLL_API
234      void
235      delaySelf();
236    
237    
238      /**
239       Member access methods.       Member access methods.
240    */    */
241    
242    /**    /**
243       \brief       \brief
244       Return the values of all data-points as a single python numarray object.       switches on update protection
245    
246    */    */
247    ESCRIPT_DLL_API    ESCRIPT_DLL_API
248    const boost::python::numeric::array    void
249    convertToNumArray();    setProtection();
250    
251    /**    /**
252       \brief       \brief
253       Return the values of all data-points for the given sample as a single python numarray object.       Returns true, if the data object is protected against update
254    
255    */    */
256    ESCRIPT_DLL_API    ESCRIPT_DLL_API
257    const boost::python::numeric::array    bool
258    convertToNumArrayFromSampleNo(int sampleNo);    isProtected() const;
259    
260    
261    /**
262       \brief
263       Return the value of a data point as a python tuple.
264    */
265      ESCRIPT_DLL_API
266      const boost::python::object
267      getValueOfDataPointAsTuple(int dataPointNo);
268    
269    /**    /**
270       \brief       \brief
271       Return the value of the specified data-point as a single python numarray object.       sets the values of a data-point from a python object on this process
272    */    */
273    ESCRIPT_DLL_API    ESCRIPT_DLL_API
274    const boost::python::numeric::array    void
275    convertToNumArrayFromDPNo(int sampleNo,    setValueOfDataPointToPyObject(int dataPointNo, const boost::python::object& py_object);
                             int dataPointNo);  
276    
277    /**    /**
278       \brief       \brief
279       Fills the expanded Data object from values of a python numarray object.       sets the values of a data-point from a array-like object on this process
280    */    */
281    ESCRIPT_DLL_API    ESCRIPT_DLL_API
282    void    void
283    fillFromNumArray(const boost::python::numeric::array);    setValueOfDataPointToArray(int dataPointNo, const boost::python::object&);
284    
285      /**
286         \brief
287         sets the values of a data-point on this process
288      */
289      ESCRIPT_DLL_API
290      void
291      setValueOfDataPoint(int dataPointNo, const double);
292    
293      /**
294         \brief Return a data point across all processors as a python tuple.
295      */
296      ESCRIPT_DLL_API
297      const boost::python::object
298      getValueOfGlobalDataPointAsTuple(int procNo, int dataPointNo);
299    
300    /**    /**
301       \brief       \brief
302       Return the tag number associated with the given data-point.       Return the tag number associated with the given data-point.
303    
      The data-point number here corresponds to the data-point number in the  
      numarray returned by convertToNumArray.  
304    */    */
305    ESCRIPT_DLL_API    ESCRIPT_DLL_API
306    int    int
# Line 284  class Data { Line 314  class Data {
314    escriptDataC    escriptDataC
315    getDataC();    getDataC();
316    
317    
318    
319    /**    /**
320       \brief       \brief
321       Return the C wrapper for the Data object - const version.       Return the C wrapper for the Data object - const version.
# Line 292  class Data { Line 324  class Data {
324    escriptDataC    escriptDataC
325    getDataC() const;    getDataC() const;
326    
   /**  
      \brief  
      Write the data as a string.  
   */  
   ESCRIPT_DLL_API  
   inline  
   std::string  
   toString() const  
   {  
     return m_data->toString();  
   }  
327    
328    /**    /**
329       \brief       \brief
330       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.  
331    */    */
332    ESCRIPT_DLL_API    ESCRIPT_DLL_API
333    inline    std::string
334    const DataArrayView&    toString() const;
   getPointDataView() const  
   {  
      return m_data->getPointDataView();  
   }  
335    
336    /**    /**
337       \brief       \brief
# Line 331  class Data { Line 346  class Data {
346       If possible convert this Data to DataTagged. This will only allow       If possible convert this Data to DataTagged. This will only allow
347       Constant data to be converted to tagged. An attempt to convert       Constant data to be converted to tagged. An attempt to convert
348       Expanded data to tagged will throw an exception.       Expanded data to tagged will throw an exception.
     ==>*  
349    */    */
350    ESCRIPT_DLL_API    ESCRIPT_DLL_API
351    void    void
352    tag();    tag();
353    
354    /**    /**
355        \brief If this data is lazy, then convert it to ready data.
356        What type of ready data depends on the expression. For example, Constant+Tagged==Tagged.
357      */
358      ESCRIPT_DLL_API
359      void
360      resolve();
361    
362    
363      /**
364       \brief Ensures data is ready for write access.
365      This means that the data will be resolved if lazy and will be copied if shared with another Data object.
366      \warning This method should only be called in single threaded sections of code. (It modifies m_data).
367      Do not create any Data objects from this one between calling requireWrite and getSampleDataRW.
368      Doing so might introduce additional sharing.
369      */
370      ESCRIPT_DLL_API
371      void
372      requireWrite();
373    
374      /**
375       \brief       \brief
376       Return true if this Data is expanded.       Return true if this Data is expanded.
377         \note To determine if a sample will contain separate values for each datapoint. Use actsExpanded instead.
378    */    */
379    ESCRIPT_DLL_API    ESCRIPT_DLL_API
380    bool    bool
# Line 347  class Data { Line 382  class Data {
382    
383    /**    /**
384       \brief       \brief
385         Return true if this Data is expanded or resolves to expanded.
386         That is, if it has a separate value for each datapoint in the sample.
387      */
388      ESCRIPT_DLL_API
389      bool
390      actsExpanded() const;
391      
392    
393      /**
394         \brief
395       Return true if this Data is tagged.       Return true if this Data is tagged.
396    */    */
397    ESCRIPT_DLL_API    ESCRIPT_DLL_API
# Line 362  class Data { Line 407  class Data {
407    isConstant() const;    isConstant() const;
408    
409    /**    /**
410         \brief Return true if this Data is lazy.
411      */
412      ESCRIPT_DLL_API
413      bool
414      isLazy() const;
415    
416      /**
417         \brief Return true if this data is ready.
418      */
419      ESCRIPT_DLL_API
420      bool
421      isReady() const;
422    
423      /**
424       \brief       \brief
425       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
426    contains datapoints.
427    */    */
428    ESCRIPT_DLL_API    ESCRIPT_DLL_API
429    bool    bool
# Line 395  class Data { Line 455  class Data {
455    */    */
456    ESCRIPT_DLL_API    ESCRIPT_DLL_API
457    inline    inline
458    const AbstractDomain&  //   const AbstractDomain&
459      const_Domain_ptr
460    getDomain() const    getDomain() const
461    {    {
462       return getFunctionSpace().getDomain();       return getFunctionSpace().getDomain();
463    }    }
464    
465    
466      /**
467         \brief
468         Return the domain.
469         TODO: For internal use only.   This should be removed.
470      */
471      ESCRIPT_DLL_API
472      inline
473    //   const AbstractDomain&
474      Domain_ptr
475      getDomainPython() const
476      {
477         return getFunctionSpace().getDomainPython();
478      }
479    
480    /**    /**
481       \brief       \brief
482       Return a copy of the domain.       Return a copy of the domain.
# Line 415  class Data { Line 491  class Data {
491    */    */
492    ESCRIPT_DLL_API    ESCRIPT_DLL_API
493    inline    inline
494    int    unsigned int
495    getDataPointRank() const    getDataPointRank() const
496    {    {
497      return m_data->getPointDataView().getRank();      return m_data->getRank();
498    }    }
499    
500    /**    /**
501       \brief       \brief
502         Return the number of data points
503      */
504      ESCRIPT_DLL_API
505      inline
506      int
507      getNumDataPoints() const
508      {
509        return getNumSamples() * getNumDataPointsPerSample();
510      }
511      /**
512         \brief
513       Return the number of samples.       Return the number of samples.
514    */    */
515    ESCRIPT_DLL_API    ESCRIPT_DLL_API
# Line 445  class Data { Line 532  class Data {
532      return m_data->getNumDPPSample();      return m_data->getNumDPPSample();
533    }    }
534    
535    
536      /**
537        \brief
538        Return the number of values in the shape for this object.
539      */
540      ESCRIPT_DLL_API
541      int
542      getNoValues() const
543      {
544        return m_data->getNoValues();
545      }
546    
547    
548      /**
549         \brief
550         dumps the object into a netCDF file
551      */
552      ESCRIPT_DLL_API
553      void
554      dump(const std::string fileName) const;
555    
556     /**
557      \brief returns the values of the object as a list of tuples (one for each datapoint).
558    
559      \param scalarastuple If true, scalar data will produce single valued tuples [(1,) (2,) ...]
560    If false, the result is a list of scalars [1, 2, ...]
561     */
562      ESCRIPT_DLL_API
563      const boost::python::object
564      toListOfTuples(bool scalarastuple=true);
565    
566    
567     /**
568        \brief
569        Return the sample data for the given sample no. This is not the
570        preferred interface but is provided for use by C code.
571        The bufferg parameter is only required for LazyData.
572        \param sampleNo - Input - the given sample no.
573        \return pointer to the sample data.
574    */
575      ESCRIPT_DLL_API
576      inline
577      const DataAbstract::ValueType::value_type*
578      getSampleDataRO(DataAbstract::ValueType::size_type sampleNo);
579    
580    
581    /**    /**
582       \brief       \brief
583       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
584       preferred interface but is provided for use by C code.       preferred interface but is provided for use by C code.
585       \param sampleNo - Input - the given sample no.       \param sampleNo - Input - the given sample no.
586         \return pointer to the sample data.
587    */    */
588    ESCRIPT_DLL_API    ESCRIPT_DLL_API
589    inline    inline
590    DataAbstract::ValueType::value_type*    DataAbstract::ValueType::value_type*
591    getSampleData(DataAbstract::ValueType::size_type sampleNo)    getSampleDataRW(DataAbstract::ValueType::size_type sampleNo);
592    {  
     return m_data->getSampleData(sampleNo);  
   }  
593    
594    /**    /**
595       \brief       \brief
# Line 475  class Data { Line 607  class Data {
607    
608    /**    /**
609       \brief       \brief
610       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.
611       reference number.       \param sampleNo - Input -
612         \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.  
613    */    */
614    ESCRIPT_DLL_API    ESCRIPT_DLL_API
615    void    DataTypes::ValueType::const_reference
616    setRefValue(int ref,    getDataPointRO(int sampleNo, int dataPointNo);
               const boost::python::numeric::array& value);  
617    
618    /**    /**
619       \brief       \brief
620       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.
621       reference number.       \param sampleNo - Input -
622         \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.  
623    */    */
624    ESCRIPT_DLL_API    ESCRIPT_DLL_API
625    void    DataTypes::ValueType::reference
626    getRefValue(int ref,    getDataPointRW(int sampleNo, int dataPointNo);
627                boost::python::numeric::array& value);  
628    
629    
630    /**    /**
631       \brief       \brief
632       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 -  
633    */    */
634    ESCRIPT_DLL_API    ESCRIPT_DLL_API
635    inline    inline
636    DataArrayView    DataTypes::ValueType::size_type
637    getDataPoint(int sampleNo,    getDataOffset(int sampleNo,
638                 int dataPointNo)                 int dataPointNo)
639    {    {
640      return m_data->getDataPoint(sampleNo,dataPointNo);        return m_data->getPointOffset(sampleNo,dataPointNo);
641    }    }
642    
643    /**    /**
# Line 536  class Data { Line 645  class Data {
645       Return a reference to the data point shape.       Return a reference to the data point shape.
646    */    */
647    ESCRIPT_DLL_API    ESCRIPT_DLL_API
648    const DataArrayView::ShapeType&    inline
649    getDataPointShape() const;    const DataTypes::ShapeType&
650      getDataPointShape() const
651      {
652        return m_data->getShape();
653      }
654    
655    /**    /**
656       \brief       \brief
# Line 561  class Data { Line 674  class Data {
674       Return the number of doubles stored for this Data.       Return the number of doubles stored for this Data.
675    */    */
676    ESCRIPT_DLL_API    ESCRIPT_DLL_API
677    DataArrayView::ValueType::size_type    DataTypes::ValueType::size_type
678    getLength() const;    getLength() const;
679    
680    /**    /**
681      \brief Return true if this object contains no samples.
682      This is not the same as isEmpty()
683      */
684      ESCRIPT_DLL_API
685      bool
686      hasNoSamples() const
687      {
688        return getLength()==0;
689      }
690    
691      /**
692       \brief       \brief
693       Assign the given value to the tag. Implicitly converts this       Assign the given value to the tag assocciated with name. Implicitly converts this
694       object to type DataTagged. Throws an exception if this object       object to type DataTagged. Throws an exception if this object
695       cannot be converted to a DataTagged object.       cannot be converted to a DataTagged object or name cannot be mapped onto a tag key.
696         \param name - Input - name of tag.
697         \param value - Input - Value to associate with given key.
698      */
699      ESCRIPT_DLL_API
700      void
701      setTaggedValueByName(std::string name,
702                           const boost::python::object& value);
703    
704      /**
705         \brief
706         Assign the given value to the tag. Implicitly converts this
707         object to type DataTagged if it is constant.
708    
709       \param tagKey - Input - Integer key.       \param tagKey - Input - Integer key.
710       \param value - Input - Value to associate with given key.       \param value - Input - Value to associate with given key.
711      ==>*      ==>*
# Line 581  class Data { Line 718  class Data {
718    /**    /**
719       \brief       \brief
720       Assign the given value to the tag. Implicitly converts this       Assign the given value to the tag. Implicitly converts this
721       object to type DataTagged. Throws an exception if this object       object to type DataTagged if it is constant.
722       cannot be converted to a DataTagged object.  
723       \param tagKey - Input - Integer key.       \param tagKey - Input - Integer key.
724         \param pointshape - Input - The shape of the value parameter
725       \param value - Input - Value to associate with given key.       \param value - Input - Value to associate with given key.
726      ==>*       \param dataOffset - Input - Offset of the begining of the point within the value parameter
727    */    */
728    ESCRIPT_DLL_API    ESCRIPT_DLL_API
729    void    void
730    setTaggedValueFromCPP(int tagKey,    setTaggedValueFromCPP(int tagKey,
731                          const DataArrayView& value);              const DataTypes::ShapeType& pointshape,
732                            const DataTypes::ValueType& value,
733                int dataOffset=0);
734    
735    
736    
737    /**    /**
738      \brief      \brief
# Line 607  class Data { Line 749  class Data {
749    
750    /**    /**
751       \brief       \brief
752         set all values to zero
753         *
754      */
755      ESCRIPT_DLL_API
756      void
757      setToZero();
758    
759      /**
760         \brief
761       Interpolates this onto the given functionspace and returns       Interpolates this onto the given functionspace and returns
762       the result as a Data object.       the result as a Data object.
763       *       *
# Line 615  class Data { Line 766  class Data {
766    Data    Data
767    interpolate(const FunctionSpace& functionspace) const;    interpolate(const FunctionSpace& functionspace) const;
768    
769    
770      ESCRIPT_DLL_API
771      Data
772      interpolateFromTable2D(const WrappedArray& table, double Amin, double Astep,
773                           double undef, Data& B, double Bmin, double Bstep,bool check_boundaries);
774    
775      ESCRIPT_DLL_API
776      Data
777      interpolateFromTable1D(const WrappedArray& table, double Amin, double Astep,
778                           double undef,bool check_boundaries);
779    
780    
781    
782    
783      ESCRIPT_DLL_API
784      Data
785      interpolateFromTable2DP(boost::python::object table, double Amin, double Astep,
786                            Data& B, double Bmin, double Bstep, double undef,bool check_boundaries);
787    
788      ESCRIPT_DLL_API
789      Data
790      interpolateFromTable1DP(boost::python::object table, double Amin, double Astep,
791                            double undef,bool check_boundaries);
792    
793    /**    /**
794       \brief       \brief
795       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 805  class Data {
805    grad() const;    grad() const;
806    
807    /**    /**
808        \brief
809         Calculate the integral over the function space domain as a python tuple.
810      */
811      ESCRIPT_DLL_API
812      boost::python::object
813      integrateToTuple_const() const;
814    
815    
816      /**
817        \brief
818         Calculate the integral over the function space domain as a python tuple.
819      */
820      ESCRIPT_DLL_API
821      boost::python::object
822      integrateToTuple();
823    
824    
825    
826      /**
827       \brief       \brief
828       Calculate the integral over the function space domain.       Returns 1./ Data object
829       *       *
830    */    */
831    ESCRIPT_DLL_API    ESCRIPT_DLL_API
832    boost::python::numeric::array    Data
833    integrate() const;    oneOver() const;
   
834    /**    /**
835       \brief       \brief
836       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 888  class Data {
888    /**    /**
889       \brief       \brief
890       Return the maximum absolute value of this Data object.       Return the maximum absolute value of this Data object.
891       *  
892         The method is not const because lazy data needs to be expanded before Lsup can be computed.
893         The _const form can be used when the Data object is const, however this will only work for
894         Data which is not Lazy.
895    
896         For Data which contain no samples (or tagged Data for which no tags in use have a value)
897         zero is returned.
898    */    */
899    ESCRIPT_DLL_API    ESCRIPT_DLL_API
900    double    double
901    Lsup() const;    Lsup();
902    
   /**  
      \brief  
      Return the minimum absolute value of this Data object.  
      *  
   */  
903    ESCRIPT_DLL_API    ESCRIPT_DLL_API
904    double    double
905    Linf() const;    Lsup_const() const;
906    
907    
908    /**    /**
909       \brief       \brief
910       Return the maximum value of this Data object.       Return the maximum value of this Data object.
911       *  
912         The method is not const because lazy data needs to be expanded before sup can be computed.
913         The _const form can be used when the Data object is const, however this will only work for
914         Data which is not Lazy.
915    
916         For Data which contain no samples (or tagged Data for which no tags in use have a value)
917         a large negative value is returned.
918    */    */
919    ESCRIPT_DLL_API    ESCRIPT_DLL_API
920    double    double
921    sup() const;    sup();
922    
923      ESCRIPT_DLL_API
924      double
925      sup_const() const;
926    
927    
928    /**    /**
929       \brief       \brief
930       Return the minimum value of this Data object.       Return the minimum value of this Data object.
931       *  
932         The method is not const because lazy data needs to be expanded before inf can be computed.
933         The _const form can be used when the Data object is const, however this will only work for
934         Data which is not Lazy.
935    
936         For Data which contain no samples (or tagged Data for which no tags in use have a value)
937         a large positive value is returned.
938    */    */
939    ESCRIPT_DLL_API    ESCRIPT_DLL_API
940    double    double
941    inf() const;    inf();
942    
943      ESCRIPT_DLL_API
944      double
945      inf_const() const;
946    
947    
948    
949    /**    /**
950       \brief       \brief
# Line 758  class Data { Line 976  class Data {
976    /**    /**
977       \brief       \brief
978       Return the (sample number, data-point number) of the data point with       Return the (sample number, data-point number) of the data point with
979       the minimum value in this Data object.       the minimum component value in this Data object.
980         \note If you are working in python, please consider using Locator
981    instead of manually manipulating process and point IDs.
982    */    */
983    ESCRIPT_DLL_API    ESCRIPT_DLL_API
984    const boost::python::tuple    const boost::python::tuple
985    mindp() const;    minGlobalDataPoint() const;
986    
987      /**
988         \brief
989         Return the (sample number, data-point number) of the data point with
990         the minimum component value in this Data object.
991         \note If you are working in python, please consider using Locator
992    instead of manually manipulating process and point IDs.
993      */
994    ESCRIPT_DLL_API    ESCRIPT_DLL_API
995    void    const boost::python::tuple
996    calc_mindp(int& SampleNo,    maxGlobalDataPoint() const;
997               int& DataPointNo) const;  
998    
999    
1000    /**    /**
1001       \brief       \brief
# Line 781  class Data { Line 1009  class Data {
1009    
1010    /**    /**
1011       \brief       \brief
1012         Return the symmetric part of a matrix which is half the matrix plus its transpose.
1013         *
1014      */
1015      ESCRIPT_DLL_API
1016      Data
1017      symmetric() const;
1018    
1019      /**
1020         \brief
1021         Return the nonsymmetric part of a matrix which is half the matrix minus its transpose.
1022         *
1023      */
1024      ESCRIPT_DLL_API
1025      Data
1026      nonsymmetric() const;
1027    
1028      /**
1029         \brief
1030         Return the trace of a matrix
1031         *
1032      */
1033      ESCRIPT_DLL_API
1034      Data
1035      trace(int axis_offset) const;
1036    
1037      /**
1038         \brief
1039         Transpose each data point of this Data object around the given axis.
1040         *
1041      */
1042      ESCRIPT_DLL_API
1043      Data
1044      transpose(int axis_offset) const;
1045    
1046      /**
1047         \brief
1048       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.
1049       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.
1050       *       *
# Line 792  class Data { Line 1056  class Data {
1056    /**    /**
1057       \brief       \brief
1058       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.
1059       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
1060       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
1061       first non-zero entry is positive.       first non-zero entry is positive.
1062       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
1063       *       *
# Line 804  class Data { Line 1068  class Data {
1068    
1069    /**    /**
1070       \brief       \brief
1071       Transpose each data point of this Data object around the given axis.       swaps the components axis0 and axis1
      --* not implemented yet *--  
1072       *       *
1073    */    */
1074    ESCRIPT_DLL_API    ESCRIPT_DLL_API
1075    Data    Data
1076    transpose(int axis) const;    swapaxes(const int axis0, const int axis1) const;
1077    
1078    /**    /**
1079       \brief       \brief
1080       Calculate the trace of each data point of this Data object.       Return the error function erf of each data point of this Data object.
1081       *       *
1082    */    */
1083    ESCRIPT_DLL_API    ESCRIPT_DLL_API
1084    Data    Data
1085    trace() const;    erf() const;
1086    
1087    /**    /**
1088       \brief       \brief
# Line 998  class Data { Line 1261  class Data {
1261    /**    /**
1262       \brief       \brief
1263       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.
1264        
1265       \param right Input - the power to raise the object to.       \param right Input - the power to raise the object to.
1266       *       *
1267     */     */
# Line 1009  class Data { Line 1272  class Data {
1272    /**    /**
1273       \brief       \brief
1274       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.
1275        
1276       \param left Input - the bases       \param left Input - the bases
1277       *       *
1278     */     */
# Line 1034  class Data { Line 1297  class Data {
1297    void    void
1298    saveVTK(std::string fileName) const;    saveVTK(std::string fileName) const;
1299    
1300    
1301    
1302    /**    /**
1303       \brief       \brief
1304       Overloaded operator +=       Overloaded operator +=
# Line 1045  class Data { Line 1310  class Data {
1310    ESCRIPT_DLL_API    ESCRIPT_DLL_API
1311    Data& operator+=(const boost::python::object& right);    Data& operator+=(const boost::python::object& right);
1312    
1313      ESCRIPT_DLL_API
1314      Data& operator=(const Data& other);
1315    
1316    /**    /**
1317       \brief       \brief
1318       Overloaded operator -=       Overloaded operator -=
# Line 1079  class Data { Line 1347  class Data {
1347    Data& operator/=(const boost::python::object& right);    Data& operator/=(const boost::python::object& right);
1348    
1349    /**    /**
1350        \brief return inverse of matricies.
1351      */
1352      ESCRIPT_DLL_API
1353      Data
1354      matrixInverse() const;
1355    
1356      /**
1357       \brief       \brief
1358       Returns true if this can be interpolated to functionspace.       Returns true if this can be interpolated to functionspace.
1359    */    */
# Line 1137  class Data { Line 1412  class Data {
1412    ESCRIPT_DLL_API    ESCRIPT_DLL_API
1413    inline    inline
1414    void    void
1415    unaryOp(UnaryFunction operation);    unaryOp2(UnaryFunction operation);
1416    
1417    /**    /**
1418       \brief       \brief
# Line 1148  class Data { Line 1423  class Data {
1423    */    */
1424    ESCRIPT_DLL_API    ESCRIPT_DLL_API
1425    Data    Data
1426    getSlice(const DataArrayView::RegionType& region) const;    getSlice(const DataTypes::RegionType& region) const;
1427    
1428    /**    /**
1429       \brief       \brief
# Line 1161  class Data { Line 1436  class Data {
1436    ESCRIPT_DLL_API    ESCRIPT_DLL_API
1437    void    void
1438    setSlice(const Data& value,    setSlice(const Data& value,
1439             const DataArrayView::RegionType& region);             const DataTypes::RegionType& region);
1440    
1441    /**    /**
1442       \brief       \brief
1443       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.  
1444    */    */
1445    ESCRIPT_DLL_API    ESCRIPT_DLL_API
1446    void    void
1447    archiveData(const std::string fileName);          print(void);
1448    
1449    /**    /**
1450       \brief       \brief
1451       Extract the Data object archived in the given file, overwriting       return the MPI rank number of the local data
1452       the current Data object.                   MPI_COMM_WORLD is assumed and the result of MPI_Comm_size()
1453       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.  
1454    */    */
1455    ESCRIPT_DLL_API    ESCRIPT_DLL_API
1456    void          int
1457    extractData(const std::string fileName,          get_MPIRank(void) const;
               const FunctionSpace& fspace);  
1458    
1459      /**
1460         \brief
1461         return the MPI rank number of the local data
1462                     MPI_COMM_WORLD is assumed and the result of MPI_Comm_rank()
1463                     is returned
1464      */
1465      ESCRIPT_DLL_API
1466            int
1467            get_MPISize(void) const;
1468    
1469    /**    /**
1470       \brief       \brief
1471       print the data values to stdout. Used for debugging       return the MPI rank number of the local data
1472                     MPI_COMM_WORLD is assumed and returned.
1473    */    */
1474    void print();    ESCRIPT_DLL_API
1475            MPI_Comm
1476            get_MPIComm(void) const;
1477    
1478      /**
1479         \brief
1480         return the object produced by the factory, which is a DataConstant or DataExpanded
1481        TODO Ownership of this object should be explained in doco.
1482      */
1483      ESCRIPT_DLL_API
1484            DataAbstract*
1485            borrowData(void) const;
1486    
1487      ESCRIPT_DLL_API
1488            DataAbstract_ptr
1489            borrowDataPtr(void) const;
1490    
1491      ESCRIPT_DLL_API
1492            DataReady_ptr
1493            borrowReadyPtr(void) const;
1494    
1495    
1496    
1497      /**
1498         \brief
1499         Return a pointer to the beginning of the datapoint at the specified offset.
1500         TODO Eventually these should be inlined.
1501         \param i - position(offset) in the underlying datastructure
1502      */
1503    
1504      ESCRIPT_DLL_API
1505            DataTypes::ValueType::const_reference
1506            getDataAtOffsetRO(DataTypes::ValueType::size_type i);
1507    
1508    
1509      ESCRIPT_DLL_API
1510            DataTypes::ValueType::reference
1511            getDataAtOffsetRW(DataTypes::ValueType::size_type i);
1512    
1513    
1514    
1515   protected:   protected:
1516    
1517   private:   private:
1518    
1519    template <class BinaryOp>
1520      double
1521    #ifdef PASO_MPI
1522      lazyAlgWorker(double init, MPI_Op mpiop_type);
1523    #else
1524      lazyAlgWorker(double init);
1525    #endif
1526    
1527      double
1528      LsupWorker() const;
1529    
1530      double
1531      supWorker() const;
1532    
1533      double
1534      infWorker() const;
1535    
1536      boost::python::object
1537      integrateWorker() const;
1538    
1539      void
1540      calc_minGlobalDataPoint(int& ProcNo,  int& DataPointNo) const;
1541    
1542      void
1543      calc_maxGlobalDataPoint(int& ProcNo,  int& DataPointNo) const;
1544    
1545      // For internal use in Data.cpp only!
1546      // other uses should call the main entry points and allow laziness
1547      Data
1548      minval_nonlazy() const;
1549    
1550      // For internal use in Data.cpp only!
1551      Data
1552      maxval_nonlazy() const;
1553    
1554    
1555    /**    /**
1556       \brief       \brief
1557       Check *this and the right operand are compatible. Throws       Check *this and the right operand are compatible. Throws
# Line 1249  class Data { Line 1605  class Data {
1605    
1606    /**    /**
1607       \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  
1608       Convert the data type of the RHS to match this.       Convert the data type of the RHS to match this.
1609       \param right - Input - data type to match.       \param right - Input - data type to match.
1610    */    */
# Line 1278  class Data { Line 1623  class Data {
1623       \brief       \brief
1624       Construct a Data object of the appropriate type.       Construct a Data object of the appropriate type.
1625    */    */
1626    template <class IValueType>  
1627    void    void
1628    initialise(const IValueType& value,    initialise(const DataTypes::ValueType& value,
1629             const DataTypes::ShapeType& shape,
1630               const FunctionSpace& what,               const FunctionSpace& what,
1631               bool expanded);               bool expanded);
1632    
   /**  
      \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.  
   */  
1633    void    void
1634    reshapeDataPoint(const DataArrayView::ShapeType& shape);    initialise(const WrappedArray& value,
1635                     const FunctionSpace& what,
1636                     bool expanded);
1637    
1638      //
1639      // flag to protect the data object against any update
1640      bool m_protected;
1641      mutable bool m_shared;
1642      bool m_lazy;
1643    
1644    //    //
1645    // pointer to the actual data object    // pointer to the actual data object
1646    boost::shared_ptr<DataAbstract> m_data;  //   boost::shared_ptr<DataAbstract> m_data;
1647      DataAbstract_ptr m_data;
1648    
1649    // If possible please use getReadyPtr instead.
1650    // But see warning below.
1651      const DataReady*
1652      getReady() const;
1653    
1654      DataReady*
1655      getReady();
1656    
1657    
1658    // Be wary of using this for local operations since it (temporarily) increases reference count.
1659    // If you are just using this to call a method on DataReady instead of DataAbstract consider using
1660    // getReady() instead
1661      DataReady_ptr
1662      getReadyPtr();
1663    
1664      const_DataReady_ptr
1665      getReadyPtr() const;
1666    
1667    
1668      /**
1669       \brief Update the Data's shared flag
1670       This indicates that the DataAbstract used by this object is now shared (or no longer shared).
1671       For internal use only.
1672      */
1673      void updateShareStatus(bool nowshared) const
1674      {
1675        m_shared=nowshared;     // m_shared is mutable
1676      }
1677    
1678      // In the isShared() method below:
1679      // A problem would occur if m_data (the address pointed to) were being modified
1680      // while the call m_data->is_shared is being executed.
1681      //
1682      // Q: So why do I think this code can be thread safe/correct?
1683      // A: We need to make some assumptions.
1684      // 1. We assume it is acceptable to return true under some conditions when we aren't shared.
1685      // 2. We assume that no constructions or assignments which will share previously unshared
1686      //    will occur while this call is executing. This is consistent with the way Data:: and C are written.
1687    //    //
1688    // pointer to the internal profiling data    // This means that the only transition we need to consider, is when a previously shared object is
1689    struct profDataEntry *profData;    // not shared anymore. ie. the other objects have been destroyed or a deep copy has been made.
1690      // In those cases the m_shared flag changes to false after m_data has completed changing.
1691      // For any threads executing before the flag switches they will assume the object is still shared.
1692      bool isShared() const
1693      {
1694        return m_shared;
1695    /*  if (m_shared) return true;
1696        if (m_data->isShared())        
1697        {                  
1698            updateShareStatus(true);
1699            return true;
1700        }
1701        return false;*/
1702      }
1703    
1704      void forceResolve()
1705      {
1706        if (isLazy())
1707        {
1708            #ifdef _OPENMP
1709            if (omp_in_parallel())
1710            {   // Yes this is throwing an exception out of an omp thread which is forbidden.
1711            throw DataException("Please do not call forceResolve() in a parallel region.");
1712            }
1713            #endif
1714            resolve();
1715        }
1716      }
1717    
1718      /**
1719      \brief if another object is sharing out member data make a copy to work with instead.
1720      This code should only be called from single threaded sections of code.
1721      */
1722      void exclusiveWrite()
1723      {
1724    #ifdef _OPENMP
1725      if (omp_in_parallel())
1726      {
1727    // *((int*)0)=17;
1728        throw DataException("Programming error. Please do not run exclusiveWrite() in multi-threaded sections.");
1729      }
1730    #endif
1731        forceResolve();
1732        if (isShared())
1733        {
1734            DataAbstract* t=m_data->deepCopy();
1735            set_m_data(DataAbstract_ptr(t));
1736        }
1737      }
1738    
1739      /**
1740      \brief checks if caller can have exclusive write to the object
1741      */
1742      void checkExclusiveWrite()
1743      {
1744        if  (isLazy() || isShared())
1745        {
1746            throw DataException("Programming error. ExclusiveWrite required - please call requireWrite()");
1747        }
1748      }
1749    
1750      /**
1751      \brief Modify the data abstract hosted by this Data object
1752      For internal use only.
1753      Passing a pointer to null is permitted (do this in the destructor)
1754      \warning Only to be called in single threaded code or inside a single/critical section. This method needs to be atomic.
1755      */
1756      void set_m_data(DataAbstract_ptr p);
1757    
1758      friend class DataAbstract;        // To allow calls to updateShareStatus
1759    
1760  };  };
1761    
1762  template <class IValueType>  }   // end namespace escript
1763  void  
1764  Data::initialise(const IValueType& value,  
1765                   const FunctionSpace& what,  // No, this is not supposed to be at the top of the file
1766                   bool expanded)  // DataAbstact needs to be declared first, then DataReady needs to be fully declared
1767    // so that I can dynamic cast between them below.
1768    #include "DataReady.h"
1769    #include "DataLazy.h"
1770    
1771    namespace escript
1772  {  {
1773    //  
1774    // Construct a Data object of the appropriate type.  inline
1775    // Construct the object first as there seems to be a bug which causes  const DataReady*
1776    // undefined behaviour if an exception is thrown during construction  Data::getReady() const
1777    // within the shared_ptr constructor.  {
1778    if (expanded) {     const DataReady* dr=dynamic_cast<const DataReady*>(m_data.get());
1779      DataAbstract* temp=new DataExpanded(value,what);     EsysAssert((dr!=0), "Error - casting to DataReady.");
1780      boost::shared_ptr<DataAbstract> temp_data(temp);     return dr;
1781      m_data=temp_data;  }
1782    } else {  
1783      DataAbstract* temp=new DataConstant(value,what);  inline
1784      boost::shared_ptr<DataAbstract> temp_data(temp);  DataReady*
1785      m_data=temp_data;  Data::getReady()
1786    }  {
1787       DataReady* dr=dynamic_cast<DataReady*>(m_data.get());
1788       EsysAssert((dr!=0), "Error - casting to DataReady.");
1789       return dr;
1790    }
1791    
1792    // Be wary of using this for local operations since it (temporarily) increases reference count.
1793    // If you are just using this to call a method on DataReady instead of DataAbstract consider using
1794    // getReady() instead
1795    inline
1796    DataReady_ptr
1797    Data::getReadyPtr()
1798    {
1799       DataReady_ptr dr=boost::dynamic_pointer_cast<DataReady>(m_data);
1800       EsysAssert((dr.get()!=0), "Error - casting to DataReady.");
1801       return dr;
1802    }
1803    
1804    
1805    inline
1806    const_DataReady_ptr
1807    Data::getReadyPtr() const
1808    {
1809       const_DataReady_ptr dr=boost::dynamic_pointer_cast<const DataReady>(m_data);
1810       EsysAssert((dr.get()!=0), "Error - casting to DataReady.");
1811       return dr;
1812    }
1813    
1814    inline
1815    DataAbstract::ValueType::value_type*
1816    Data::getSampleDataRW(DataAbstract::ValueType::size_type sampleNo)
1817    {
1818       if (isLazy())
1819       {
1820        throw DataException("Error, attempt to acquire RW access to lazy data. Please call requireWrite() first.");
1821       }
1822       return getReady()->getSampleDataRW(sampleNo);
1823    }
1824    
1825    inline
1826    const DataAbstract::ValueType::value_type*
1827    Data::getSampleDataRO(DataAbstract::ValueType::size_type sampleNo)
1828    {
1829       DataLazy* l=dynamic_cast<DataLazy*>(m_data.get());
1830       if (l!=0)
1831       {
1832        size_t offset=0;
1833        const DataTypes::ValueType* res=l->resolveSample(sampleNo,offset);
1834        return &((*res)[offset]);
1835       }
1836       return getReady()->getSampleDataRO(sampleNo);
1837  }  }
1838    
1839    
1840    
1841    /**
1842       Modify a filename for MPI parallel output to multiple files
1843    */
1844    char *Escript_MPI_appendRankToFileName(const char *, int, int);
1845    
1846  /**  /**
1847     Binary Data object operators.     Binary Data object operators.
1848  */  */
1849    inline double rpow(double x,double y)
1850    {
1851        return pow(y,x);
1852    }
1853    
1854  /**  /**
1855    \brief    \brief
# Line 1422  ESCRIPT_DLL_API Data operator*(const boo Line 1943  ESCRIPT_DLL_API Data operator*(const boo
1943  */  */
1944  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);
1945    
1946    
1947    
1948  /**  /**
1949    \brief    \brief
1950    Output operator    Output operator
# Line 1430  ESCRIPT_DLL_API std::ostream& operator<< Line 1953  ESCRIPT_DLL_API std::ostream& operator<<
1953    
1954  /**  /**
1955    \brief    \brief
1956    Return true if operands are equivalent, else return false.    Compute a tensor product of two Data objects
1957    NB: this operator does very little at this point, and isn't to    \param arg_0 - Input - Data object
1958    be relied on. Requires further implementation.    \param arg_1 - Input - Data object
1959      \param axis_offset - Input - axis offset
1960      \param transpose - Input - 0: transpose neither, 1: transpose arg0, 2: transpose arg1
1961  */  */
1962  //ESCRIPT_DLL_API bool operator==(const Data& left, const Data& right);  ESCRIPT_DLL_API
1963    Data
1964    C_GeneralTensorProduct(Data& arg_0,
1965                         Data& arg_1,
1966                         int axis_offset=0,
1967                         int transpose=0);
1968    
1969  /**  /**
1970    \brief    \brief
# Line 1449  Data::binaryOp(const Data& right, Line 1979  Data::binaryOp(const Data& right,
1979  {  {
1980     //     //
1981     // 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
1982     if (getPointDataView().getRank()==0 && right.getPointDataView().getRank()!=0) {     if (getDataPointRank()==0 && right.getDataPointRank()!=0) {
1983       reshapeDataPoint(right.getPointDataView().getShape());       throw DataException("Error - attempt to update rank zero object with object with rank bigger than zero.");
1984       }
1985    
1986       if (isLazy() || right.isLazy())
1987       {
1988         throw DataException("Programmer error - attempt to call binaryOp with Lazy Data.");
1989     }     }
1990     //     //
1991     // initially make the temporary a shallow copy     // initially make the temporary a shallow copy
1992     Data tempRight(right);     Data tempRight(right);
1993    
1994     if (getFunctionSpace()!=right.getFunctionSpace()) {     if (getFunctionSpace()!=right.getFunctionSpace()) {
1995       if (right.probeInterpolation(getFunctionSpace())) {       if (right.probeInterpolation(getFunctionSpace())) {
1996         //         //
1997         // an interpolation is required so create a new Data         // an interpolation is required so create a new Data
1998         tempRight=Data(right,this->getFunctionSpace());         tempRight=Data(right,this->getFunctionSpace());
1999       } else if (probeInterpolation(right.getFunctionSpace())) {       } else if (probeInterpolation(right.getFunctionSpace())) {
2000         //         //
2001         // interpolate onto the RHS function space         // interpolate onto the RHS function space
2002         Data tempLeft(*this,right.getFunctionSpace());         Data tempLeft(*this,right.getFunctionSpace());
2003         m_data=tempLeft.m_data;  //        m_data=tempLeft.m_data;
2004           set_m_data(tempLeft.m_data);
2005       }       }
2006     }     }
2007     operandCheck(tempRight);     operandCheck(tempRight);
# Line 1480  Data::binaryOp(const Data& right, Line 2017  Data::binaryOp(const Data& right,
2017       // of any data type       // of any data type
2018       DataExpanded* leftC=dynamic_cast<DataExpanded*>(m_data.get());       DataExpanded* leftC=dynamic_cast<DataExpanded*>(m_data.get());
2019       EsysAssert((leftC!=0), "Programming error - casting to DataExpanded.");       EsysAssert((leftC!=0), "Programming error - casting to DataExpanded.");
2020       escript::binaryOp(*leftC,*(tempRight.m_data.get()),operation);       escript::binaryOp(*leftC,*(tempRight.getReady()),operation);
2021     } else if (isTagged()) {     } else if (isTagged()) {
2022       //       //
2023       // 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 2043  Data::binaryOp(const Data& right,
2043    
2044  /**  /**
2045    \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  
2046    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.
2047    Given operation combines each element of each data point, thus argument    Given operation combines each element of each data point, thus argument
2048    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 2065  Data::algorithm(BinaryFunction operation
2065      DataConstant* leftC=dynamic_cast<DataConstant*>(m_data.get());      DataConstant* leftC=dynamic_cast<DataConstant*>(m_data.get());
2066      EsysAssert((leftC!=0), "Programming error - casting to DataConstant.");      EsysAssert((leftC!=0), "Programming error - casting to DataConstant.");
2067      return escript::algorithm(*leftC,operation,initial_value);      return escript::algorithm(*leftC,operation,initial_value);
2068      } else if (isEmpty()) {
2069        throw DataException("Error - Operations not permitted on instances of DataEmpty.");
2070      } else if (isLazy()) {
2071        throw DataException("Error - Operations not permitted on instances of DataLazy.");
2072      } else {
2073        throw DataException("Error - Data encapsulates an unknown type.");
2074    }    }
   return 0;  
2075  }  }
2076    
2077  /**  /**
2078    \brief    \brief
2079    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.
2080    Given operation combines each element within each data point into a scalar,    Given operation combines each element within each data point into a scalar,
2081    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
2082    rank 0 Data object.    rank 0 Data object.
2083    Calls escript::dp_algorithm.    Calls escript::dp_algorithm.
2084  */  */
# Line 1631  inline Line 2087  inline
2087  Data  Data
2088  Data::dp_algorithm(BinaryFunction operation, double initial_value) const  Data::dp_algorithm(BinaryFunction operation, double initial_value) const
2089  {  {
2090    if (isExpanded()) {    if (isEmpty()) {
2091      Data result(0,DataArrayView::ShapeType(),getFunctionSpace(),isExpanded());      throw DataException("Error - Operations not permitted on instances of DataEmpty.");
2092      }
2093      else if (isExpanded()) {
2094        Data result(0,DataTypes::ShapeType(),getFunctionSpace(),isExpanded());
2095      DataExpanded* dataE=dynamic_cast<DataExpanded*>(m_data.get());      DataExpanded* dataE=dynamic_cast<DataExpanded*>(m_data.get());
2096      DataExpanded* resultE=dynamic_cast<DataExpanded*>(result.m_data.get());      DataExpanded* resultE=dynamic_cast<DataExpanded*>(result.m_data.get());
2097      EsysAssert((dataE!=0), "Programming error - casting data to DataExpanded.");      EsysAssert((dataE!=0), "Programming error - casting data to DataExpanded.");
2098      EsysAssert((resultE!=0), "Programming error - casting result to DataExpanded.");      EsysAssert((resultE!=0), "Programming error - casting result to DataExpanded.");
2099      escript::dp_algorithm(*dataE,*resultE,operation,initial_value);      escript::dp_algorithm(*dataE,*resultE,operation,initial_value);
2100      return result;      return result;
2101    } else if (isTagged()) {    }
2102      else if (isTagged()) {
2103      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());  
2104      EsysAssert((dataT!=0), "Programming error - casting data to DataTagged.");      EsysAssert((dataT!=0), "Programming error - casting data to DataTagged.");
2105      EsysAssert((resultT!=0), "Programming error - casting result to DataTagged.");      DataTypes::ValueType defval(1);
2106        defval[0]=0;
2107        DataTagged* resultT=new DataTagged(getFunctionSpace(), DataTypes::scalarShape, defval, dataT);
2108      escript::dp_algorithm(*dataT,*resultT,operation,initial_value);      escript::dp_algorithm(*dataT,*resultT,operation,initial_value);
2109      return result;      return Data(resultT);   // note: the Data object now owns the resultT pointer
2110    } else if (isConstant()) {    }
2111      Data result(0,DataArrayView::ShapeType(),getFunctionSpace(),isExpanded());    else if (isConstant()) {
2112        Data result(0,DataTypes::ShapeType(),getFunctionSpace(),isExpanded());
2113      DataConstant* dataC=dynamic_cast<DataConstant*>(m_data.get());      DataConstant* dataC=dynamic_cast<DataConstant*>(m_data.get());
2114      DataConstant* resultC=dynamic_cast<DataConstant*>(result.m_data.get());      DataConstant* resultC=dynamic_cast<DataConstant*>(result.m_data.get());
2115      EsysAssert((dataC!=0), "Programming error - casting data to DataConstant.");      EsysAssert((dataC!=0), "Programming error - casting data to DataConstant.");
2116      EsysAssert((resultC!=0), "Programming error - casting result to DataConstant.");      EsysAssert((resultC!=0), "Programming error - casting result to DataConstant.");
2117      escript::dp_algorithm(*dataC,*resultC,operation,initial_value);      escript::dp_algorithm(*dataC,*resultC,operation,initial_value);
2118      return result;      return result;
2119      } else if (isLazy()) {
2120        throw DataException("Error - Operations not permitted on instances of DataLazy.");
2121      } else {
2122        throw DataException("Error - Data encapsulates an unknown type.");
2123      }
2124    }
2125    
2126    /**
2127      \brief
2128      Compute a tensor operation with two Data objects
2129      \param arg_0 - Input - Data object
2130      \param arg_1 - Input - Data object
2131      \param operation - Input - Binary op functor
2132    */
2133    template <typename BinaryFunction>
2134    inline
2135    Data
2136    C_TensorBinaryOperation(Data const &arg_0,
2137                            Data const &arg_1,
2138                            BinaryFunction operation)
2139    {
2140      if (arg_0.isEmpty() || arg_1.isEmpty())
2141      {
2142         throw DataException("Error - Operations not permitted on instances of DataEmpty.");
2143      }
2144      if (arg_0.isLazy() || arg_1.isLazy())
2145      {
2146         throw DataException("Error - Operations not permitted on lazy data.");
2147      }
2148      // Interpolate if necessary and find an appropriate function space
2149      Data arg_0_Z, arg_1_Z;
2150      if (arg_0.getFunctionSpace()!=arg_1.getFunctionSpace()) {
2151        if (arg_0.probeInterpolation(arg_1.getFunctionSpace())) {
2152          arg_0_Z = arg_0.interpolate(arg_1.getFunctionSpace());
2153          arg_1_Z = Data(arg_1);
2154        }
2155        else if (arg_1.probeInterpolation(arg_0.getFunctionSpace())) {
2156          arg_1_Z=arg_1.interpolate(arg_0.getFunctionSpace());
2157          arg_0_Z =Data(arg_0);
2158        }
2159        else {
2160          throw DataException("Error - C_TensorBinaryOperation: arguments have incompatible function spaces.");
2161        }
2162      } else {
2163          arg_0_Z = Data(arg_0);
2164          arg_1_Z = Data(arg_1);
2165      }
2166      // Get rank and shape of inputs
2167      int rank0 = arg_0_Z.getDataPointRank();
2168      int rank1 = arg_1_Z.getDataPointRank();
2169      DataTypes::ShapeType shape0 = arg_0_Z.getDataPointShape();
2170      DataTypes::ShapeType shape1 = arg_1_Z.getDataPointShape();
2171      int size0 = arg_0_Z.getDataPointSize();
2172      int size1 = arg_1_Z.getDataPointSize();
2173      // Declare output Data object
2174      Data res;
2175    
2176      if (shape0 == shape1) {
2177        if (arg_0_Z.isConstant()   && arg_1_Z.isConstant()) {
2178          res = Data(0.0, shape0, arg_1_Z.getFunctionSpace());      // DataConstant output
2179          const double *ptr_0 = &(arg_0_Z.getDataAtOffsetRO(0));
2180          const double *ptr_1 = &(arg_1_Z.getDataAtOffsetRO(0));
2181          double *ptr_2 = &(res.getDataAtOffsetRW(0));
2182    
2183          tensor_binary_operation(size0, ptr_0, ptr_1, ptr_2, operation);
2184        }
2185        else if (arg_0_Z.isConstant()   && arg_1_Z.isTagged()) {
2186    
2187          // Prepare the DataConstant input
2188          DataConstant* tmp_0=dynamic_cast<DataConstant*>(arg_0_Z.borrowData());
2189    
2190          // Borrow DataTagged input from Data object
2191          DataTagged* tmp_1=dynamic_cast<DataTagged*>(arg_1_Z.borrowData());
2192    
2193          // Prepare a DataTagged output 2
2194          res = Data(0.0, shape0, arg_1_Z.getFunctionSpace());      // DataTagged output
2195          res.tag();
2196          DataTagged* tmp_2=dynamic_cast<DataTagged*>(res.borrowData());
2197    
2198          // Prepare offset into DataConstant
2199          int offset_0 = tmp_0->getPointOffset(0,0);
2200          const double *ptr_0 = &(arg_0_Z.getDataAtOffsetRO(offset_0));
2201    
2202          // Get the pointers to the actual data
2203          const double *ptr_1 = &(tmp_1->getDefaultValueRO(0));
2204          double *ptr_2 = &(tmp_2->getDefaultValueRW(0));
2205    
2206          // Compute a result for the default
2207          tensor_binary_operation(size0, ptr_0, ptr_1, ptr_2, operation);
2208          // Compute a result for each tag
2209          const DataTagged::DataMapType& lookup_1=tmp_1->getTagLookup();
2210          DataTagged::DataMapType::const_iterator i; // i->first is a tag, i->second is an offset into memory
2211          for (i=lookup_1.begin();i!=lookup_1.end();i++) {
2212            tmp_2->addTag(i->first);
2213            const double *ptr_1 = &(tmp_1->getDataByTagRO(i->first,0));
2214            double *ptr_2 = &(tmp_2->getDataByTagRW(i->first,0));
2215    
2216            tensor_binary_operation(size0, ptr_0, ptr_1, ptr_2, operation);
2217          }
2218    
2219        }
2220        else if (arg_0_Z.isConstant()   && arg_1_Z.isExpanded()) {
2221          res = Data(0.0, shape0, arg_1_Z.getFunctionSpace(),true); // DataExpanded output
2222          DataConstant* tmp_0=dynamic_cast<DataConstant*>(arg_0_Z.borrowData());
2223          DataExpanded* tmp_1=dynamic_cast<DataExpanded*>(arg_1_Z.borrowData());
2224          DataExpanded* tmp_2=dynamic_cast<DataExpanded*>(res.borrowData());
2225    
2226          int sampleNo_1,dataPointNo_1;
2227          int numSamples_1 = arg_1_Z.getNumSamples();
2228          int numDataPointsPerSample_1 = arg_1_Z.getNumDataPointsPerSample();
2229          int offset_0 = tmp_0->getPointOffset(0,0);
2230          res.requireWrite();
2231          #pragma omp parallel for private(sampleNo_1,dataPointNo_1) schedule(static)
2232          for (sampleNo_1 = 0; sampleNo_1 < numSamples_1; sampleNo_1++) {
2233            for (dataPointNo_1 = 0; dataPointNo_1 < numDataPointsPerSample_1; dataPointNo_1++) {
2234              int offset_1 = tmp_1->getPointOffset(sampleNo_1,dataPointNo_1);
2235              int offset_2 = tmp_2->getPointOffset(sampleNo_1,dataPointNo_1);
2236              const double *ptr_0 = &(arg_0_Z.getDataAtOffsetRO(offset_0));
2237              const double *ptr_1 = &(arg_1_Z.getDataAtOffsetRO(offset_1));
2238              double *ptr_2 = &(res.getDataAtOffsetRW(offset_2));
2239              tensor_binary_operation(size0, ptr_0, ptr_1, ptr_2, operation);
2240            }
2241          }
2242    
2243        }
2244        else if (arg_0_Z.isTagged()     && arg_1_Z.isConstant()) {
2245          // Borrow DataTagged input from Data object
2246          DataTagged* tmp_0=dynamic_cast<DataTagged*>(arg_0_Z.borrowData());
2247    
2248          // Prepare the DataConstant input
2249          DataConstant* tmp_1=dynamic_cast<DataConstant*>(arg_1_Z.borrowData());
2250    
2251          // Prepare a DataTagged output 2
2252          res = Data(0.0, shape0, arg_0_Z.getFunctionSpace());      // DataTagged output
2253          res.tag();
2254          DataTagged* tmp_2=dynamic_cast<DataTagged*>(res.borrowData());
2255    
2256          // Prepare offset into DataConstant
2257          int offset_1 = tmp_1->getPointOffset(0,0);
2258    
2259          const double *ptr_1 = &(arg_1_Z.getDataAtOffsetRO(offset_1));
2260          // Get the pointers to the actual data
2261          const double *ptr_0 = &(tmp_0->getDefaultValueRO(0));
2262          double *ptr_2 = &(tmp_2->getDefaultValueRW(0));
2263          // Compute a result for the default
2264          tensor_binary_operation(size0, ptr_0, ptr_1, ptr_2, operation);
2265          // Compute a result for each tag
2266          const DataTagged::DataMapType& lookup_0=tmp_0->getTagLookup();
2267          DataTagged::DataMapType::const_iterator i; // i->first is a tag, i->second is an offset into memory
2268          for (i=lookup_0.begin();i!=lookup_0.end();i++) {
2269            tmp_2->addTag(i->first);
2270            const double *ptr_0 = &(tmp_0->getDataByTagRO(i->first,0));
2271            double *ptr_2 = &(tmp_2->getDataByTagRW(i->first,0));
2272            tensor_binary_operation(size0, ptr_0, ptr_1, ptr_2, operation);
2273          }
2274    
2275        }
2276        else if (arg_0_Z.isTagged()     && arg_1_Z.isTagged()) {
2277          // Borrow DataTagged input from Data object
2278          DataTagged* tmp_0=dynamic_cast<DataTagged*>(arg_0_Z.borrowData());
2279    
2280          // Borrow DataTagged input from Data object
2281          DataTagged* tmp_1=dynamic_cast<DataTagged*>(arg_1_Z.borrowData());
2282    
2283          // Prepare a DataTagged output 2
2284          res = Data(0.0, shape0, arg_1_Z.getFunctionSpace());
2285          res.tag();        // DataTagged output
2286          DataTagged* tmp_2=dynamic_cast<DataTagged*>(res.borrowData());
2287    
2288          // Get the pointers to the actual data
2289          const double *ptr_0 = &(tmp_0->getDefaultValueRO(0));
2290          const double *ptr_1 = &(tmp_1->getDefaultValueRO(0));
2291          double *ptr_2 = &(tmp_2->getDefaultValueRW(0));
2292    
2293          // Compute a result for the default
2294          tensor_binary_operation(size0, ptr_0, ptr_1, ptr_2, operation);
2295          // Merge the tags
2296          DataTagged::DataMapType::const_iterator i; // i->first is a tag, i->second is an offset into memory
2297          const DataTagged::DataMapType& lookup_0=tmp_0->getTagLookup();
2298          const DataTagged::DataMapType& lookup_1=tmp_1->getTagLookup();
2299          for (i=lookup_0.begin();i!=lookup_0.end();i++) {
2300            tmp_2->addTag(i->first); // use tmp_2 to get correct shape
2301          }
2302          for (i=lookup_1.begin();i!=lookup_1.end();i++) {
2303            tmp_2->addTag(i->first);
2304          }
2305          // Compute a result for each tag
2306          const DataTagged::DataMapType& lookup_2=tmp_2->getTagLookup();
2307          for (i=lookup_2.begin();i!=lookup_2.end();i++) {
2308    
2309            const double *ptr_0 = &(tmp_0->getDataByTagRO(i->first,0));
2310            const double *ptr_1 = &(tmp_1->getDataByTagRO(i->first,0));
2311            double *ptr_2 = &(tmp_2->getDataByTagRW(i->first,0));
2312    
2313            tensor_binary_operation(size0, ptr_0, ptr_1, ptr_2, operation);
2314          }
2315    
2316        }
2317        else if (arg_0_Z.isTagged()     && arg_1_Z.isExpanded()) {
2318          // After finding a common function space above the two inputs have the same numSamples and num DPPS
2319          res = Data(0.0, shape0, arg_1_Z.getFunctionSpace(),true); // DataExpanded output
2320          DataTagged*   tmp_0=dynamic_cast<DataTagged*>(arg_0_Z.borrowData());
2321          DataExpanded* tmp_1=dynamic_cast<DataExpanded*>(arg_1_Z.borrowData());
2322          DataExpanded* tmp_2=dynamic_cast<DataExpanded*>(res.borrowData());
2323    
2324          int sampleNo_0,dataPointNo_0;
2325          int numSamples_0 = arg_0_Z.getNumSamples();
2326          int numDataPointsPerSample_0 = arg_0_Z.getNumDataPointsPerSample();
2327          res.requireWrite();
2328          #pragma omp parallel for private(sampleNo_0,dataPointNo_0) schedule(static)
2329          for (sampleNo_0 = 0; sampleNo_0 < numSamples_0; sampleNo_0++) {
2330            int offset_0 = tmp_0->getPointOffset(sampleNo_0,0); // They're all the same, so just use #0
2331            const double *ptr_0 = &(arg_0_Z.getDataAtOffsetRO(offset_0));
2332            for (dataPointNo_0 = 0; dataPointNo_0 < numDataPointsPerSample_0; dataPointNo_0++) {
2333              int offset_1 = tmp_1->getPointOffset(sampleNo_0,dataPointNo_0);
2334              int offset_2 = tmp_2->getPointOffset(sampleNo_0,dataPointNo_0);
2335              const double *ptr_1 = &(arg_1_Z.getDataAtOffsetRO(offset_1));
2336              double *ptr_2 = &(res.getDataAtOffsetRW(offset_2));
2337              tensor_binary_operation(size0, ptr_0, ptr_1, ptr_2, operation);
2338            }
2339          }
2340    
2341        }
2342        else if (arg_0_Z.isExpanded()   && arg_1_Z.isConstant()) {
2343          res = Data(0.0, shape0, arg_1_Z.getFunctionSpace(),true); // DataExpanded output
2344          DataExpanded* tmp_0=dynamic_cast<DataExpanded*>(arg_0_Z.borrowData());
2345          DataConstant* tmp_1=dynamic_cast<DataConstant*>(arg_1_Z.borrowData());
2346          DataExpanded* tmp_2=dynamic_cast<DataExpanded*>(res.borrowData());
2347    
2348          int sampleNo_0,dataPointNo_0;
2349          int numSamples_0 = arg_0_Z.getNumSamples();
2350          int numDataPointsPerSample_0 = arg_0_Z.getNumDataPointsPerSample();
2351          int offset_1 = tmp_1->getPointOffset(0,0);
2352          res.requireWrite();
2353          #pragma omp parallel for private(sampleNo_0,dataPointNo_0) schedule(static)
2354          for (sampleNo_0 = 0; sampleNo_0 < numSamples_0; sampleNo_0++) {
2355            for (dataPointNo_0 = 0; dataPointNo_0 < numDataPointsPerSample_0; dataPointNo_0++) {
2356              int offset_0 = tmp_0->getPointOffset(sampleNo_0,dataPointNo_0);
2357              int offset_2 = tmp_2->getPointOffset(sampleNo_0,dataPointNo_0);
2358    
2359              const double *ptr_0 = &(arg_0_Z.getDataAtOffsetRO(offset_0));
2360              const double *ptr_1 = &(arg_1_Z.getDataAtOffsetRO(offset_1));
2361              double *ptr_2 = &(res.getDataAtOffsetRW(offset_2));
2362    
2363    
2364              tensor_binary_operation(size0, ptr_0, ptr_1, ptr_2, operation);
2365            }
2366          }
2367    
2368        }
2369        else if (arg_0_Z.isExpanded()   && arg_1_Z.isTagged()) {
2370          // After finding a common function space above the two inputs have the same numSamples and num DPPS
2371          res = Data(0.0, shape0, arg_1_Z.getFunctionSpace(),true); // DataExpanded output
2372          DataExpanded* tmp_0=dynamic_cast<DataExpanded*>(arg_0_Z.borrowData());
2373          DataTagged*   tmp_1=dynamic_cast<DataTagged*>(arg_1_Z.borrowData());
2374          DataExpanded* tmp_2=dynamic_cast<DataExpanded*>(res.borrowData());
2375    
2376          int sampleNo_0,dataPointNo_0;
2377          int numSamples_0 = arg_0_Z.getNumSamples();
2378          int numDataPointsPerSample_0 = arg_0_Z.getNumDataPointsPerSample();
2379          res.requireWrite();
2380          #pragma omp parallel for private(sampleNo_0,dataPointNo_0) schedule(static)
2381          for (sampleNo_0 = 0; sampleNo_0 < numSamples_0; sampleNo_0++) {
2382            int offset_1 = tmp_1->getPointOffset(sampleNo_0,0);
2383            const double *ptr_1 = &(arg_1_Z.getDataAtOffsetRO(offset_1));
2384            for (dataPointNo_0 = 0; dataPointNo_0 < numDataPointsPerSample_0; dataPointNo_0++) {
2385              int offset_0 = tmp_0->getPointOffset(sampleNo_0,dataPointNo_0);
2386              int offset_2 = tmp_2->getPointOffset(sampleNo_0,dataPointNo_0);
2387              const double *ptr_0 = &(arg_0_Z.getDataAtOffsetRO(offset_0));
2388              double *ptr_2 = &(res.getDataAtOffsetRW(offset_2));
2389              tensor_binary_operation(size0, ptr_0, ptr_1, ptr_2, operation);
2390            }
2391          }
2392    
2393        }
2394        else if (arg_0_Z.isExpanded()   && arg_1_Z.isExpanded()) {
2395          // After finding a common function space above the two inputs have the same numSamples and num DPPS
2396          res = Data(0.0, shape0, arg_1_Z.getFunctionSpace(),true); // DataExpanded output
2397          DataExpanded* tmp_0=dynamic_cast<DataExpanded*>(arg_0_Z.borrowData());
2398          DataExpanded* tmp_1=dynamic_cast<DataExpanded*>(arg_1_Z.borrowData());
2399          DataExpanded* tmp_2=dynamic_cast<DataExpanded*>(res.borrowData());
2400    
2401          int sampleNo_0,dataPointNo_0;
2402          int numSamples_0 = arg_0_Z.getNumSamples();
2403          int numDataPointsPerSample_0 = arg_0_Z.getNumDataPointsPerSample();
2404          res.requireWrite();
2405          #pragma omp parallel for private(sampleNo_0,dataPointNo_0) schedule(static)
2406          for (sampleNo_0 = 0; sampleNo_0 < numSamples_0; sampleNo_0++) {
2407          dataPointNo_0=0;
2408    //        for (dataPointNo_0 = 0; dataPointNo_0 < numDataPointsPerSample_0; dataPointNo_0++) {
2409              int offset_0 = tmp_0->getPointOffset(sampleNo_0,dataPointNo_0);
2410              int offset_1 = tmp_1->getPointOffset(sampleNo_0,dataPointNo_0);
2411              int offset_2 = tmp_2->getPointOffset(sampleNo_0,dataPointNo_0);
2412              const double *ptr_0 = &(arg_0_Z.getDataAtOffsetRO(offset_0));
2413              const double *ptr_1 = &(arg_1_Z.getDataAtOffsetRO(offset_1));
2414              double *ptr_2 = &(res.getDataAtOffsetRW(offset_2));
2415              tensor_binary_operation(size0*numDataPointsPerSample_0, ptr_0, ptr_1, ptr_2, operation);
2416    //       }
2417          }
2418    
2419        }
2420        else {
2421          throw DataException("Error - C_TensorBinaryOperation: unknown combination of inputs");
2422        }
2423    
2424      } else if (0 == rank0) {
2425        if (arg_0_Z.isConstant()   && arg_1_Z.isConstant()) {
2426          res = Data(0.0, shape1, arg_1_Z.getFunctionSpace());      // DataConstant output
2427          const double *ptr_0 = &(arg_0_Z.getDataAtOffsetRO(0));
2428          const double *ptr_1 = &(arg_1_Z.getDataAtOffsetRO(0));
2429          double *ptr_2 = &(res.getDataAtOffsetRW(0));
2430          tensor_binary_operation(size1, ptr_0[0], ptr_1, ptr_2, operation);
2431        }
2432        else if (arg_0_Z.isConstant()   && arg_1_Z.isTagged()) {
2433    
2434          // Prepare the DataConstant input
2435          DataConstant* tmp_0=dynamic_cast<DataConstant*>(arg_0_Z.borrowData());
2436    
2437          // Borrow DataTagged input from Data object
2438          DataTagged* tmp_1=dynamic_cast<DataTagged*>(arg_1_Z.borrowData());
2439    
2440          // Prepare a DataTagged output 2
2441          res = Data(0.0, shape1, arg_1_Z.getFunctionSpace());      // DataTagged output
2442          res.tag();
2443          DataTagged* tmp_2=dynamic_cast<DataTagged*>(res.borrowData());
2444    
2445          // Prepare offset into DataConstant
2446          int offset_0 = tmp_0->getPointOffset(0,0);
2447          const double *ptr_0 = &(arg_0_Z.getDataAtOffsetRO(offset_0));
2448    
2449          const double *ptr_1 = &(tmp_1->getDefaultValueRO(0));
2450          double *ptr_2 = &(tmp_2->getDefaultValueRW(0));
2451    
2452          // Compute a result for the default
2453          tensor_binary_operation(size1, ptr_0[0], ptr_1, ptr_2, operation);
2454          // Compute a result for each tag
2455          const DataTagged::DataMapType& lookup_1=tmp_1->getTagLookup();
2456          DataTagged::DataMapType::const_iterator i; // i->first is a tag, i->second is an offset into memory
2457          for (i=lookup_1.begin();i!=lookup_1.end();i++) {
2458            tmp_2->addTag(i->first);
2459            const double *ptr_1 = &(tmp_1->getDataByTagRO(i->first,0));
2460            double *ptr_2 = &(tmp_2->getDataByTagRW(i->first,0));
2461            tensor_binary_operation(size1, ptr_0[0], ptr_1, ptr_2, operation);
2462          }
2463    
2464        }
2465        else if (arg_0_Z.isConstant()   && arg_1_Z.isExpanded()) {
2466    
2467          res = Data(0.0, shape1, arg_1_Z.getFunctionSpace(),true); // DataExpanded output
2468          DataConstant* tmp_0=dynamic_cast<DataConstant*>(arg_0_Z.borrowData());
2469          DataExpanded* tmp_1=dynamic_cast<DataExpanded*>(arg_1_Z.borrowData());
2470          DataExpanded* tmp_2=dynamic_cast<DataExpanded*>(res.borrowData());
2471    
2472          int sampleNo_1;
2473          int numSamples_1 = arg_1_Z.getNumSamples();
2474          int numDataPointsPerSample_1 = arg_1_Z.getNumDataPointsPerSample();
2475          int offset_0 = tmp_0->getPointOffset(0,0);
2476          const double *ptr_src = &(arg_0_Z.getDataAtOffsetRO(offset_0));
2477          double ptr_0 = ptr_src[0];
2478          int size = size1*numDataPointsPerSample_1;
2479          res.requireWrite();
2480          #pragma omp parallel for private(sampleNo_1) schedule(static)
2481          for (sampleNo_1 = 0; sampleNo_1 < numSamples_1; sampleNo_1++) {
2482    //        for (dataPointNo_1 = 0; dataPointNo_1 < numDataPointsPerSample_1; dataPointNo_1++) {
2483              int offset_1 = tmp_1->getPointOffset(sampleNo_1,0);
2484              int offset_2 = tmp_2->getPointOffset(sampleNo_1,0);
2485    //          const double *ptr_0 = &(arg_0_Z.getDataAtOffsetRO(offset_0));
2486              const double *ptr_1 = &(arg_1_Z.getDataAtOffsetRO(offset_1));
2487              double *ptr_2 = &(res.getDataAtOffsetRW(offset_2));
2488              tensor_binary_operation(size, ptr_0, ptr_1, ptr_2, operation);
2489    
2490    //        }
2491          }
2492    
2493        }
2494        else if (arg_0_Z.isTagged()     && arg_1_Z.isConstant()) {
2495    
2496          // Borrow DataTagged input from Data object
2497          DataTagged* tmp_0=dynamic_cast<DataTagged*>(arg_0_Z.borrowData());
2498    
2499          // Prepare the DataConstant input
2500          DataConstant* tmp_1=dynamic_cast<DataConstant*>(arg_1_Z.borrowData());
2501    
2502          // Prepare a DataTagged output 2
2503          res = Data(0.0, shape1, arg_0_Z.getFunctionSpace());      // DataTagged output
2504          res.tag();
2505          DataTagged* tmp_2=dynamic_cast<DataTagged*>(res.borrowData());
2506    
2507          // Prepare offset into DataConstant
2508          int offset_1 = tmp_1->getPointOffset(0,0);
2509          const double *ptr_1 = &(arg_1_Z.getDataAtOffsetRO(offset_1));
2510    
2511          // Get the pointers to the actual data
2512          const double *ptr_0 = &(tmp_0->getDefaultValueRO(0));
2513          double *ptr_2 = &(tmp_2->getDefaultValueRW(0));
2514    
2515    
2516          // Compute a result for the default
2517          tensor_binary_operation(size1, ptr_0[0], ptr_1, ptr_2, operation);
2518          // Compute a result for each tag
2519          const DataTagged::DataMapType& lookup_0=tmp_0->getTagLookup();
2520          DataTagged::DataMapType::const_iterator i; // i->first is a tag, i->second is an offset into memory
2521          for (i=lookup_0.begin();i!=lookup_0.end();i++) {
2522            tmp_2->addTag(i->first);
2523            const double *ptr_0 = &(tmp_0->getDataByTagRO(i->first,0));
2524            double *ptr_2 = &(tmp_2->getDataByTagRW(i->first,0));
2525    
2526            tensor_binary_operation(size1, ptr_0[0], ptr_1, ptr_2, operation);
2527          }
2528    
2529        }
2530        else if (arg_0_Z.isTagged()     && arg_1_Z.isTagged()) {
2531    
2532          // Borrow DataTagged input from Data object
2533          DataTagged* tmp_0=dynamic_cast<DataTagged*>(arg_0_Z.borrowData());
2534    
2535          // Borrow DataTagged input from Data object
2536          DataTagged* tmp_1=dynamic_cast<DataTagged*>(arg_1_Z.borrowData());
2537    
2538          // Prepare a DataTagged output 2
2539          res = Data(0.0, shape1, arg_1_Z.getFunctionSpace());
2540          res.tag();        // DataTagged output
2541          DataTagged* tmp_2=dynamic_cast<DataTagged*>(res.borrowData());
2542    
2543          // Get the pointers to the actual data
2544          const double *ptr_0 = &(tmp_0->getDefaultValueRO(0));
2545          const double *ptr_1 = &(tmp_1->getDefaultValueRO(0));
2546          double *ptr_2 = &(tmp_2->getDefaultValueRW(0));
2547    
2548          // Compute a result for the default
2549          tensor_binary_operation(size1, ptr_0[0], ptr_1, ptr_2, operation);
2550          // Merge the tags
2551          DataTagged::DataMapType::const_iterator i; // i->first is a tag, i->second is an offset into memory
2552          const DataTagged::DataMapType& lookup_0=tmp_0->getTagLookup();
2553          const DataTagged::DataMapType& lookup_1=tmp_1->getTagLookup();
2554          for (i=lookup_0.begin();i!=lookup_0.end();i++) {
2555            tmp_2->addTag(i->first); // use tmp_2 to get correct shape
2556          }
2557          for (i=lookup_1.begin();i!=lookup_1.end();i++) {
2558            tmp_2->addTag(i->first);
2559          }
2560          // Compute a result for each tag
2561          const DataTagged::DataMapType& lookup_2=tmp_2->getTagLookup();
2562          for (i=lookup_2.begin();i!=lookup_2.end();i++) {
2563            const double *ptr_0 = &(tmp_0->getDataByTagRO(i->first,0));
2564            const double *ptr_1 = &(tmp_1->getDataByTagRO(i->first,0));
2565            double *ptr_2 = &(tmp_2->getDataByTagRW(i->first,0));
2566    
2567            tensor_binary_operation(size1, ptr_0[0], ptr_1, ptr_2, operation);
2568          }
2569    
2570        }
2571        else if (arg_0_Z.isTagged()     && arg_1_Z.isExpanded()) {
2572    
2573          // After finding a common function space above the two inputs have the same numSamples and num DPPS
2574          res = Data(0.0, shape1, arg_1_Z.getFunctionSpace(),true); // DataExpanded output
2575          DataTagged*   tmp_0=dynamic_cast<DataTagged*>(arg_0_Z.borrowData());
2576          DataExpanded* tmp_1=dynamic_cast<DataExpanded*>(arg_1_Z.borrowData());
2577          DataExpanded* tmp_2=dynamic_cast<DataExpanded*>(res.borrowData());
2578    
2579          int sampleNo_0,dataPointNo_0;
2580          int numSamples_0 = arg_0_Z.getNumSamples();
2581          int numDataPointsPerSample_0 = arg_0_Z.getNumDataPointsPerSample();
2582          res.requireWrite();
2583          #pragma omp parallel for private(sampleNo_0,dataPointNo_0) schedule(static)
2584          for (sampleNo_0 = 0; sampleNo_0 < numSamples_0; sampleNo_0++) {
2585            int offset_0 = tmp_0->getPointOffset(sampleNo_0,0); // They're all the same, so just use #0
2586            const double *ptr_0 = &(arg_0_Z.getDataAtOffsetRO(offset_0));
2587            for (dataPointNo_0 = 0; dataPointNo_0 < numDataPointsPerSample_0; dataPointNo_0++) {
2588              int offset_1 = tmp_1->getPointOffset(sampleNo_0,dataPointNo_0);
2589              int offset_2 = tmp_2->getPointOffset(sampleNo_0,dataPointNo_0);
2590              const double *ptr_1 = &(arg_1_Z.getDataAtOffsetRO(offset_1));
2591              double *ptr_2 = &(res.getDataAtOffsetRW(offset_2));
2592              tensor_binary_operation(size1, ptr_0[0], ptr_1, ptr_2, operation);
2593            }
2594          }
2595    
2596        }
2597        else if (arg_0_Z.isExpanded()   && arg_1_Z.isConstant()) {
2598          res = Data(0.0, shape1, arg_1_Z.getFunctionSpace(),true); // DataExpanded output
2599          DataExpanded* tmp_0=dynamic_cast<DataExpanded*>(arg_0_Z.borrowData());
2600          DataConstant* tmp_1=dynamic_cast<DataConstant*>(arg_1_Z.borrowData());
2601          DataExpanded* tmp_2=dynamic_cast<DataExpanded*>(res.borrowData());
2602    
2603          int sampleNo_0,dataPointNo_0;
2604          int numSamples_0 = arg_0_Z.getNumSamples();
2605          int numDataPointsPerSample_0 = arg_0_Z.getNumDataPointsPerSample();
2606          int offset_1 = tmp_1->getPointOffset(0,0);
2607          res.requireWrite();
2608          #pragma omp parallel for private(sampleNo_0,dataPointNo_0) schedule(static)
2609          for (sampleNo_0 = 0; sampleNo_0 < numSamples_0; sampleNo_0++) {
2610            for (dataPointNo_0 = 0; dataPointNo_0 < numDataPointsPerSample_0; dataPointNo_0++) {
2611              int offset_0 = tmp_0->getPointOffset(sampleNo_0,dataPointNo_0);
2612              int offset_2 = tmp_2->getPointOffset(sampleNo_0,dataPointNo_0);
2613              const double *ptr_0 = &(arg_0_Z.getDataAtOffsetRO(offset_0));
2614              const double *ptr_1 = &(arg_1_Z.getDataAtOffsetRO(offset_1));
2615              double *ptr_2 = &(res.getDataAtOffsetRW(offset_2));
2616              tensor_binary_operation(size1, ptr_0[0], ptr_1, ptr_2, operation);
2617            }
2618          }
2619    
2620    
2621        }
2622        else if (arg_0_Z.isExpanded()   && arg_1_Z.isTagged()) {
2623    
2624          // After finding a common function space above the two inputs have the same numSamples and num DPPS
2625          res = Data(0.0, shape1, arg_1_Z.getFunctionSpace(),true); // DataExpanded output
2626          DataExpanded* tmp_0=dynamic_cast<DataExpanded*>(arg_0_Z.borrowData());
2627          DataTagged*   tmp_1=dynamic_cast<DataTagged*>(arg_1_Z.borrowData());
2628          DataExpanded* tmp_2=dynamic_cast<DataExpanded*>(res.borrowData());
2629    
2630          int sampleNo_0,dataPointNo_0;
2631          int numSamples_0 = arg_0_Z.getNumSamples();
2632          int numDataPointsPerSample_0 = arg_0_Z.getNumDataPointsPerSample();
2633          res.requireWrite();
2634          #pragma omp parallel for private(sampleNo_0,dataPointNo_0) schedule(static)
2635          for (sampleNo_0 = 0; sampleNo_0 < numSamples_0; sampleNo_0++) {
2636            int offset_1 = tmp_1->getPointOffset(sampleNo_0,0);
2637            const double *ptr_1 = &(arg_1_Z.getDataAtOffsetRO(offset_1));
2638            for (dataPointNo_0 = 0; dataPointNo_0 < numDataPointsPerSample_0; dataPointNo_0++) {
2639              int offset_0 = tmp_0->getPointOffset(sampleNo_0,dataPointNo_0);
2640              int offset_2 = tmp_2->getPointOffset(sampleNo_0,dataPointNo_0);
2641              const double *ptr_0 = &(arg_0_Z.getDataAtOffsetRO(offset_0));
2642              double *ptr_2 = &(res.getDataAtOffsetRW(offset_2));
2643              tensor_binary_operation(size1, ptr_0[0], ptr_1, ptr_2, operation);
2644            }
2645          }
2646    
2647        }
2648        else if (arg_0_Z.isExpanded()   && arg_1_Z.isExpanded()) {
2649    
2650          // After finding a common function space above the two inputs have the same numSamples and num DPPS
2651          res = Data(0.0, shape1, arg_1_Z.getFunctionSpace(),true); // DataExpanded output
2652          DataExpanded* tmp_0=dynamic_cast<DataExpanded*>(arg_0_Z.borrowData());
2653          DataExpanded* tmp_1=dynamic_cast<DataExpanded*>(arg_1_Z.borrowData());
2654          DataExpanded* tmp_2=dynamic_cast<DataExpanded*>(res.borrowData());
2655    
2656          int sampleNo_0,dataPointNo_0;
2657          int numSamples_0 = arg_0_Z.getNumSamples();
2658          int numDataPointsPerSample_0 = arg_0_Z.getNumDataPointsPerSample();
2659          res.requireWrite();
2660          #pragma omp parallel for private(sampleNo_0,dataPointNo_0) schedule(static)
2661          for (sampleNo_0 = 0; sampleNo_0 < numSamples_0; sampleNo_0++) {
2662            for (dataPointNo_0 = 0; dataPointNo_0 < numDataPointsPerSample_0; dataPointNo_0++) {
2663              int offset_0 = tmp_0->getPointOffset(sampleNo_0,dataPointNo_0);
2664              int offset_1 = tmp_1->getPointOffset(sampleNo_0,dataPointNo_0);
2665              int offset_2 = tmp_2->getPointOffset(sampleNo_0,dataPointNo_0);
2666              const double *ptr_0 = &(arg_0_Z.getDataAtOffsetRO(offset_0));
2667              const double *ptr_1 = &(arg_1_Z.getDataAtOffsetRO(offset_1));
2668              double *ptr_2 = &(res.getDataAtOffsetRW(offset_2));
2669              tensor_binary_operation(size1, ptr_0[0], ptr_1, ptr_2, operation);
2670            }
2671          }
2672    
2673        }
2674        else {
2675          throw DataException("Error - C_TensorBinaryOperation: unknown combination of inputs");
2676        }
2677    
2678      } else if (0 == rank1) {
2679        if (arg_0_Z.isConstant()   && arg_1_Z.isConstant()) {
2680          res = Data(0.0, shape0, arg_1_Z.getFunctionSpace());      // DataConstant output
2681          const double *ptr_0 = &(arg_0_Z.getDataAtOffsetRO(0));
2682          const double *ptr_1 = &(arg_1_Z.getDataAtOffsetRO(0));
2683          double *ptr_2 = &(res.getDataAtOffsetRW(0));
2684          tensor_binary_operation(size0, ptr_0, ptr_1[0], ptr_2, operation);
2685        }
2686        else if (arg_0_Z.isConstant()   && arg_1_Z.isTagged()) {
2687    
2688          // Prepare the DataConstant input
2689          DataConstant* tmp_0=dynamic_cast<DataConstant*>(arg_0_Z.borrowData());
2690    
2691          // Borrow DataTagged input from Data object
2692          DataTagged* tmp_1=dynamic_cast<DataTagged*>(arg_1_Z.borrowData());
2693    
2694          // Prepare a DataTagged output 2
2695          res = Data(0.0, shape0, arg_1_Z.getFunctionSpace());      // DataTagged output
2696          res.tag();
2697          DataTagged* tmp_2=dynamic_cast<DataTagged*>(res.borrowData());
2698    
2699          // Prepare offset into DataConstant
2700          int offset_0 = tmp_0->getPointOffset(0,0);
2701          const double *ptr_0 = &(arg_0_Z.getDataAtOffsetRO(offset_0));
2702    
2703          //Get the pointers to the actual data
2704          const double *ptr_1 = &(tmp_1->getDefaultValueRO(0));
2705          double *ptr_2 = &(tmp_2->getDefaultValueRW(0));
2706    
2707          // Compute a result for the default
2708          tensor_binary_operation(size0, ptr_0, ptr_1[0], ptr_2, operation);
2709          // Compute a result for each tag
2710          const DataTagged::DataMapType& lookup_1=tmp_1->getTagLookup();
2711          DataTagged::DataMapType::const_iterator i; // i->first is a tag, i->second is an offset into memory
2712          for (i=lookup_1.begin();i!=lookup_1.end();i++) {
2713            tmp_2->addTag(i->first);
2714            const double *ptr_1 = &(tmp_1->getDataByTagRO(i->first,0));
2715            double *ptr_2 = &(tmp_2->getDataByTagRW(i->first,0));
2716            tensor_binary_operation(size0, ptr_0, ptr_1[0], ptr_2, operation);
2717          }
2718        }
2719        else if (arg_0_Z.isConstant()   && arg_1_Z.isExpanded()) {
2720    
2721          res = Data(0.0, shape0, arg_1_Z.getFunctionSpace(),true); // DataExpanded output
2722          DataConstant* tmp_0=dynamic_cast<DataConstant*>(arg_0_Z.borrowData());
2723          DataExpanded* tmp_1=dynamic_cast<DataExpanded*>(arg_1_Z.borrowData());
2724          DataExpanded* tmp_2=dynamic_cast<DataExpanded*>(res.borrowData());
2725    
2726          int sampleNo_1,dataPointNo_1;
2727          int numSamples_1 = arg_1_Z.getNumSamples();
2728          int numDataPointsPerSample_1 = arg_1_Z.getNumDataPointsPerSample();
2729          int offset_0 = tmp_0->getPointOffset(0,0);
2730          res.requireWrite();
2731          #pragma omp parallel for private(sampleNo_1,dataPointNo_1) schedule(static)
2732          for (sampleNo_1 = 0; sampleNo_1 < numSamples_1; sampleNo_1++) {
2733            for (dataPointNo_1 = 0; dataPointNo_1 < numDataPointsPerSample_1; dataPointNo_1++) {
2734              int offset_1 = tmp_1->getPointOffset(sampleNo_1,dataPointNo_1);
2735              int offset_2 = tmp_2->getPointOffset(sampleNo_1,dataPointNo_1);
2736              const double *ptr_0 = &(arg_0_Z.getDataAtOffsetRO(offset_0));
2737              const double *ptr_1 = &(arg_1_Z.getDataAtOffsetRO(offset_1));
2738              double *ptr_2 = &(res.getDataAtOffsetRW(offset_2));
2739              tensor_binary_operation(size0, ptr_0, ptr_1[0], ptr_2, operation);
2740            }
2741          }
2742    
2743        }
2744        else if (arg_0_Z.isTagged()     && arg_1_Z.isConstant()) {
2745    
2746          // Borrow DataTagged input from Data object
2747          DataTagged* tmp_0=dynamic_cast<DataTagged*>(arg_0_Z.borrowData());
2748    
2749          // Prepare the DataConstant input
2750          DataConstant* tmp_1=dynamic_cast<DataConstant*>(arg_1_Z.borrowData());
2751    
2752          // Prepare a DataTagged output 2
2753          res = Data(0.0, shape0, arg_0_Z.getFunctionSpace());      // DataTagged output
2754          res.tag();
2755          DataTagged* tmp_2=dynamic_cast<DataTagged*>(res.borrowData());
2756    
2757          // Prepare offset into DataConstant
2758          int offset_1 = tmp_1->getPointOffset(0,0);
2759          const double *ptr_1 = &(arg_1_Z.getDataAtOffsetRO(offset_1));
2760          // Get the pointers to the actual data
2761          const double *ptr_0 = &(tmp_0->getDefaultValueRO(0));
2762          double *ptr_2 = &(tmp_2->getDefaultValueRW(0));
2763          // Compute a result for the default
2764          tensor_binary_operation(size0, ptr_0, ptr_1[0], ptr_2, operation);
2765          // Compute a result for each tag
2766          const DataTagged::DataMapType& lookup_0=tmp_0->getTagLookup();
2767          DataTagged::DataMapType::const_iterator i; // i->first is a tag, i->second is an offset into memory
2768          for (i=lookup_0.begin();i!=lookup_0.end();i++) {
2769            tmp_2->addTag(i->first);
2770            const double *ptr_0 = &(tmp_0->getDataByTagRO(i->first,0));
2771            double *ptr_2 = &(tmp_2->getDataByTagRW(i->first,0));
2772            tensor_binary_operation(size0, ptr_0, ptr_1[0], ptr_2, operation);
2773          }
2774    
2775        }
2776        else if (arg_0_Z.isTagged()     && arg_1_Z.isTagged()) {
2777    
2778          // Borrow DataTagged input from Data object
2779          DataTagged* tmp_0=dynamic_cast<DataTagged*>(arg_0_Z.borrowData());
2780    
2781          // Borrow DataTagged input from Data object
2782          DataTagged* tmp_1=dynamic_cast<DataTagged*>(arg_1_Z.borrowData());
2783    
2784          // Prepare a DataTagged output 2
2785          res = Data(0.0, shape0, arg_1_Z.getFunctionSpace());
2786          res.tag();        // DataTagged output
2787          DataTagged* tmp_2=dynamic_cast<DataTagged*>(res.borrowData());
2788    
2789          // Get the pointers to the actual data
2790          const double *ptr_0 = &(tmp_0->getDefaultValueRO(0));
2791          const double *ptr_1 = &(tmp_1->getDefaultValueRO(0));
2792          double *ptr_2 = &(tmp_2->getDefaultValueRW(0));
2793    
2794          // Compute a result for the default
2795          tensor_binary_operation(size0, ptr_0, ptr_1[0], ptr_2, operation);
2796          // Merge the tags
2797          DataTagged::DataMapType::const_iterator i; // i->first is a tag, i->second is an offset into memory
2798          const DataTagged::DataMapType& lookup_0=tmp_0->getTagLookup();
2799          const DataTagged::DataMapType& lookup_1=tmp_1->getTagLookup();
2800          for (i=lookup_0.begin();i!=lookup_0.end();i++) {
2801            tmp_2->addTag(i->first); // use tmp_2 to get correct shape
2802          }
2803          for (i=lookup_1.begin();i!=lookup_1.end();i++) {
2804            tmp_2->addTag(i->first);
2805          }
2806          // Compute a result for each tag
2807          const DataTagged::DataMapType& lookup_2=tmp_2->getTagLookup();
2808          for (i=lookup_2.begin();i!=lookup_2.end();i++) {
2809            const double *ptr_0 = &(tmp_0->getDataByTagRO(i->first,0));
2810            const double *ptr_1 = &(tmp_1->getDataByTagRO(i->first,0));
2811            double *ptr_2 = &(tmp_2->getDataByTagRW(i->first,0));
2812            tensor_binary_operation(size0, ptr_0, ptr_1[0], ptr_2, operation);
2813          }
2814    
2815        }
2816        else if (arg_0_Z.isTagged()     && arg_1_Z.isExpanded()) {
2817    
2818          // After finding a common function space above the two inputs have the same numSamples and num DPPS
2819          res = Data(0.0, shape0, arg_1_Z.getFunctionSpace(),true); // DataExpanded output
2820          DataTagged*   tmp_0=dynamic_cast<DataTagged*>(arg_0_Z.borrowData());
2821          DataExpanded* tmp_1=dynamic_cast<DataExpanded*>(arg_1_Z.borrowData());
2822          DataExpanded* tmp_2=dynamic_cast<DataExpanded*>(res.borrowData());
2823    
2824          int sampleNo_0,dataPointNo_0;
2825          int numSamples_0 = arg_0_Z.getNumSamples();
2826          int numDataPointsPerSample_0 = arg_0_Z.getNumDataPointsPerSample();
2827          res.requireWrite();
2828          #pragma omp parallel for private(sampleNo_0,dataPointNo_0) schedule(static)
2829          for (sampleNo_0 = 0; sampleNo_0 < numSamples_0; sampleNo_0++) {
2830            int offset_0 = tmp_0->getPointOffset(sampleNo_0,0); // They're all the same, so just use #0
2831            const double *ptr_0 = &(arg_0_Z.getDataAtOffsetRO(offset_0));
2832            for (dataPointNo_0 = 0; dataPointNo_0 < numDataPointsPerSample_0; dataPointNo_0++) {
2833              int offset_1 = tmp_1->getPointOffset(sampleNo_0,dataPointNo_0);
2834              int offset_2 = tmp_2->getPointOffset(sampleNo_0,dataPointNo_0);
2835              const double *ptr_1 = &(arg_1_Z.getDataAtOffsetRO(offset_1));
2836              double *ptr_2 = &(res.getDataAtOffsetRW(offset_2));
2837              tensor_binary_operation(size0, ptr_0, ptr_1[0], ptr_2, operation);
2838            }
2839          }
2840    
2841        }
2842        else if (arg_0_Z.isExpanded()   && arg_1_Z.isConstant()) {
2843          res = Data(0.0, shape0, arg_1_Z.getFunctionSpace(),true); // DataExpanded output
2844          DataExpanded* tmp_0=dynamic_cast<DataExpanded*>(arg_0_Z.borrowData());
2845          DataConstant* tmp_1=dynamic_cast<DataConstant*>(arg_1_Z.borrowData());
2846          DataExpanded* tmp_2=dynamic_cast<DataExpanded*>(res.borrowData());
2847    
2848          int sampleNo_0;
2849          int numSamples_0 = arg_0_Z.getNumSamples();
2850          int numDataPointsPerSample_0 = arg_0_Z.getNumDataPointsPerSample();
2851          int offset_1 = tmp_1->getPointOffset(0,0);
2852          const double *ptr_src = &(arg_1_Z.getDataAtOffsetRO(offset_1));
2853          double ptr_1 = ptr_src[0];
2854          int size = size0 * numDataPointsPerSample_0;
2855          res.requireWrite();
2856          #pragma omp parallel for private(sampleNo_0) schedule(static)
2857          for (sampleNo_0 = 0; sampleNo_0 < numSamples_0; sampleNo_0++) {
2858    //        for (dataPointNo_0 = 0; dataPointNo_0 < numDataPointsPerSample_0; dataPointNo_0++) {
2859              int offset_0 = tmp_0->getPointOffset(sampleNo_0,0);
2860              int offset_2 = tmp_2->getPointOffset(sampleNo_0,0);
2861              const double *ptr_0 = &(arg_0_Z.getDataAtOffsetRO(offset_0));
2862    //          const double *ptr_1 = &(arg_1_Z.getDataAtOffsetRO(offset_1));
2863              double *ptr_2 = &(res.getDataAtOffsetRW(offset_2));
2864              tensor_binary_operation(size, ptr_0, ptr_1, ptr_2, operation);
2865    //        }
2866          }
2867    
2868    
2869        }
2870        else if (arg_0_Z.isExpanded()   && arg_1_Z.isTagged()) {
2871    
2872          // After finding a common function space above the two inputs have the same numSamples and num DPPS
2873          res = Data(0.0, shape0, arg_1_Z.getFunctionSpace(),true); // DataExpanded output
2874          DataExpanded* tmp_0=dynamic_cast<DataExpanded*>(arg_0_Z.borrowData());
2875          DataTagged*   tmp_1=dynamic_cast<DataTagged*>(arg_1_Z.borrowData());
2876          DataExpanded* tmp_2=dynamic_cast<DataExpanded*>(res.borrowData());
2877    
2878          int sampleNo_0,dataPointNo_0;
2879          int numSamples_0 = arg_0_Z.getNumSamples();
2880          int numDataPointsPerSample_0 = arg_0_Z.getNumDataPointsPerSample();
2881          res.requireWrite();
2882          #pragma omp parallel for private(sampleNo_0,dataPointNo_0) schedule(static)
2883          for (sampleNo_0 = 0; sampleNo_0 < numSamples_0; sampleNo_0++) {
2884            int offset_1 = tmp_1->getPointOffset(sampleNo_0,0);
2885            const double *ptr_1 = &(arg_1_Z.getDataAtOffsetRO(offset_1));
2886            for (dataPointNo_0 = 0; dataPointNo_0 < numDataPointsPerSample_0; dataPointNo_0++) {
2887              int offset_0 = tmp_0->getPointOffset(sampleNo_0,dataPointNo_0);
2888              int offset_2 = tmp_2->getPointOffset(sampleNo_0,dataPointNo_0);
2889              const double *ptr_0 = &(arg_0_Z.getDataAtOffsetRO(offset_0));
2890              double *ptr_2 = &(res.getDataAtOffsetRW(offset_2));
2891              tensor_binary_operation(size0, ptr_0, ptr_1[0], ptr_2, operation);
2892            }
2893          }
2894    
2895        }
2896        else if (arg_0_Z.isExpanded()   && arg_1_Z.isExpanded()) {
2897    
2898          // After finding a common function space above the two inputs have the same numSamples and num DPPS
2899          res = Data(0.0, shape0, arg_1_Z.getFunctionSpace(),true); // DataExpanded output
2900          DataExpanded* tmp_0=dynamic_cast<DataExpanded*>(arg_0_Z.borrowData());
2901          DataExpanded* tmp_1=dynamic_cast<DataExpanded*>(arg_1_Z.borrowData());
2902          DataExpanded* tmp_2=dynamic_cast<DataExpanded*>(res.borrowData());
2903    
2904          int sampleNo_0,dataPointNo_0;
2905          int numSamples_0 = arg_0_Z.getNumSamples();
2906          int numDataPointsPerSample_0 = arg_0_Z.getNumDataPointsPerSample();
2907          res.requireWrite();
2908          #pragma omp parallel for private(sampleNo_0,dataPointNo_0) schedule(static)
2909          for (sampleNo_0 = 0; sampleNo_0 < numSamples_0; sampleNo_0++) {
2910            for (dataPointNo_0 = 0; dataPointNo_0 < numDataPointsPerSample_0; dataPointNo_0++) {
2911              int offset_0 = tmp_0->getPointOffset(sampleNo_0,dataPointNo_0);
2912              int offset_1 = tmp_1->getPointOffset(sampleNo_0,dataPointNo_0);
2913              int offset_2 = tmp_2->getPointOffset(sampleNo_0,dataPointNo_0);
2914              const double *ptr_0 = &(arg_0_Z.getDataAtOffsetRO(offset_0));
2915              const double *ptr_1 = &(arg_1_Z.getDataAtOffsetRO(offset_1));
2916              double *ptr_2 = &(res.getDataAtOffsetRW(offset_2));
2917              tensor_binary_operation(size0, ptr_0, ptr_1[0], ptr_2, operation);
2918            }
2919          }
2920    
2921        }
2922        else {
2923          throw DataException("Error - C_TensorBinaryOperation: unknown combination of inputs");
2924        }
2925    
2926      } else {
2927        throw DataException("Error - C_TensorBinaryOperation: arguments have incompatible shapes");
2928    }    }
2929    Data falseRetVal; // to keep compiler quiet  
2930    return falseRetVal;    return res;
2931    }
2932    
2933    template <typename UnaryFunction>
2934    Data
2935    C_TensorUnaryOperation(Data const &arg_0,
2936                           UnaryFunction operation)
2937    {
2938      if (arg_0.isEmpty())  // do this before we attempt to interpolate
2939      {
2940         throw DataException("Error - Operations not permitted on instances of DataEmpty.");
2941      }
2942      if (arg_0.isLazy())
2943      {
2944         throw DataException("Error - Operations not permitted on lazy data.");
2945      }
2946      // Interpolate if necessary and find an appropriate function space
2947      Data arg_0_Z = Data(arg_0);
2948    
2949      // Get rank and shape of inputs
2950      const DataTypes::ShapeType& shape0 = arg_0_Z.getDataPointShape();
2951      int size0 = arg_0_Z.getDataPointSize();
2952    
2953      // Declare output Data object
2954      Data res;
2955    
2956      if (arg_0_Z.isConstant()) {
2957        res = Data(0.0, shape0, arg_0_Z.getFunctionSpace());      // DataConstant output
2958        const double *ptr_0 = &(arg_0_Z.getDataAtOffsetRO(0));
2959        double *ptr_2 = &(res.getDataAtOffsetRW(0));
2960        tensor_unary_operation(size0, ptr_0, ptr_2, operation);
2961      }
2962      else if (arg_0_Z.isTagged()) {
2963    
2964        // Borrow DataTagged input from Data object
2965        DataTagged* tmp_0=dynamic_cast<DataTagged*>(arg_0_Z.borrowData());
2966    
2967        // Prepare a DataTagged output 2
2968        res = Data(0.0, shape0, arg_0_Z.getFunctionSpace());   // DataTagged output
2969        res.tag();
2970        DataTagged* tmp_2=dynamic_cast<DataTagged*>(res.borrowData());
2971    
2972        // Get the pointers to the actual data
2973        const double *ptr_0 = &(tmp_0->getDefaultValueRO(0));
2974        double *ptr_2 = &(tmp_2->getDefaultValueRW(0));
2975        // Compute a result for the default
2976        tensor_unary_operation(size0, ptr_0, ptr_2, operation);
2977        // Compute a result for each tag
2978        const DataTagged::DataMapType& lookup_0=tmp_0->getTagLookup();
2979        DataTagged::DataMapType::const_iterator i; // i->first is a tag, i->second is an offset into memory
2980        for (i=lookup_0.begin();i!=lookup_0.end();i++) {
2981          tmp_2->addTag(i->first);
2982          const double *ptr_0 = &(tmp_0->getDataByTagRO(i->first,0));
2983          double *ptr_2 = &(tmp_2->getDataByTagRW(i->first,0));
2984          tensor_unary_operation(size0, ptr_0, ptr_2, operation);
2985        }
2986    
2987      }
2988      else if (arg_0_Z.isExpanded()) {
2989    
2990        res = Data(0.0, shape0, arg_0_Z.getFunctionSpace(),true); // DataExpanded output
2991        DataExpanded* tmp_0=dynamic_cast<DataExpanded*>(arg_0_Z.borrowData());
2992        DataExpanded* tmp_2=dynamic_cast<DataExpanded*>(res.borrowData());
2993    
2994        int sampleNo_0,dataPointNo_0;
2995        int numSamples_0 = arg_0_Z.getNumSamples();
2996        int numDataPointsPerSample_0 = arg_0_Z.getNumDataPointsPerSample();
2997        #pragma omp parallel for private(sampleNo_0,dataPointNo_0) schedule(static)
2998        for (sampleNo_0 = 0; sampleNo_0 < numSamples_0; sampleNo_0++) {
2999        dataPointNo_0=0;
3000    //      for (dataPointNo_0 = 0; dataPointNo_0 < numDataPointsPerSample_0; dataPointNo_0++) {
3001            int offset_0 = tmp_0->getPointOffset(sampleNo_0,dataPointNo_0);
3002            int offset_2 = tmp_2->getPointOffset(sampleNo_0,dataPointNo_0);
3003            const double *ptr_0 = &(arg_0_Z.getDataAtOffsetRO(offset_0));
3004            double *ptr_2 = &(res.getDataAtOffsetRW(offset_2));
3005            tensor_unary_operation(size0*numDataPointsPerSample_0, ptr_0, ptr_2, operation);
3006    //      }
3007        }
3008      }
3009      else {
3010        throw DataException("Error - C_TensorUnaryOperation: unknown combination of inputs");
3011      }
3012    
3013      return res;
3014  }  }
3015    
3016  }  }

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

  ViewVC Help
Powered by ViewVC 1.1.26