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

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

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

trunk/esys2/escript/src/Data/DataArrayView.h revision 108 by jgs, Thu Jan 27 06:21:59 2005 UTC trunk/escript/src/DataArrayView.h revision 478 by jgs, Tue Jan 31 02:21:49 2006 UTC
# Line 13  Line 13 
13   *                                                                            *   *                                                                            *
14   ******************************************************************************   ******************************************************************************
15  */  */
16                                                                              
17  #if !defined escript_DataArrayView_20040323_H  #if !defined escript_DataArrayView_20040323_H
18  #define escript_DataArrayView_20040323_H  #define escript_DataArrayView_20040323_H
19    
20  #include "esysUtils/EsysAssert.h"  #include "EsysAssert.h"
21    
22    #include "DataVector.h"
23    
24  #include <boost/python/numeric.hpp>  #include <boost/python/numeric.hpp>
25  #include <boost/python/object.hpp>  #include <boost/python/object.hpp>
 #include <boost/shared_ptr.hpp>  
26    
 #include <iostream>  
27  #include <vector>  #include <vector>
28    
29  namespace escript {  namespace escript {
# Line 42  namespace escript { Line 42  namespace escript {
42     The view provided represents a single n-dimensional data-point     The view provided represents a single n-dimensional data-point
43     comprised of values taken from the given data array, starting at the     comprised of values taken from the given data array, starting at the
44     specified offset and extending for as many values as are necessary to     specified offset and extending for as many values as are necessary to
45     satisfy the given shape.     satisfy the given shape. The default offset can be changed, or different
46       offsets specified, in order to provide views of other data-points in
47       the underlying data array.
48  */  */
49    
50  class DataArrayView {  class DataArrayView {
# Line 52  class DataArrayView { Line 54  class DataArrayView {
54    
55   public:   public:
56    
57    /**    //
58        Some basic types which define the data values and view shapes.    // Some basic types which define the data values and view shapes.
59    */    typedef DataVector                        ValueType;
60    typedef std::vector<double>               ValueType;    //typedef std::vector<double>               ValueType;
61    typedef std::vector<int>                  ShapeType;    typedef std::vector<int>                  ShapeType;
62    typedef std::vector<std::pair<int, int> > RegionType;    typedef std::vector<std::pair<int, int> > RegionType;
63    typedef std::vector<std::pair<int, int> > RegionLoopRangeType;    typedef std::vector<std::pair<int, int> > RegionLoopRangeType;
# Line 208  class DataArrayView { Line 210  class DataArrayView {
210    */    */
211    bool    bool
212    checkOffset() const;    checkOffset() const;
213    
214    bool    bool
215    checkOffset(ValueType::size_type offset) const;    checkOffset(ValueType::size_type offset) const;
216    
# Line 230  class DataArrayView { Line 233  class DataArrayView {
233       Calculate the number of values for the given shape or region.       Calculate the number of values for the given shape or region.
234       This is purely a utility method and has no bearing on this view.       This is purely a utility method and has no bearing on this view.
235    */    */
236    static int    static
237      int
238    noValues(const ShapeType& shape);    noValues(const ShapeType& shape);
239    static int  
240      static
241      int
242    noValues(const RegionLoopRangeType& region);    noValues(const RegionLoopRangeType& region);
243    
244    /**    /**
# Line 254  class DataArrayView { Line 260  class DataArrayView {
260       \brief       \brief
261       Return the shape of this view.       Return the shape of this view.
262    */    */
263    const ShapeType&    const
264      ShapeType&
265    getShape() const;    getShape() const;
266    
267    /**    /**
268       \brief       \brief
269       Return true if the given shape is the same as this view's shape.       Return true if the given shape is the same as this view's shape.
      This is purely a utility method and has no bearing on this view.  
270    */    */
271    bool    bool
272    checkShape(const DataArrayView::ShapeType& other) const;    checkShape(const ShapeType& other) const;
273    
274    /**    /**
275       \brief       \brief
276       Create a shape error message. Normally used when there is a shape       Create a shape error message. Normally used when there is a shape
277       mismatch.       mismatch between this shape and the other shape.
      This is purely a utility method and has no bearing on this view.  
278    
279       \param messagePrefix - Input -       \param messagePrefix - Input -
280                         First part of the error message.                         First part of the error message.
# Line 278  class DataArrayView { Line 283  class DataArrayView {
283    */    */
284    std::string    std::string
285    createShapeErrorMessage(const std::string& messagePrefix,    createShapeErrorMessage(const std::string& messagePrefix,
286                            const DataArrayView::ShapeType& other) const;                            const ShapeType& other) const;
287    
288    /**    /**
289       \brief       \brief
# Line 298  class DataArrayView { Line 303  class DataArrayView {
303    
304       \param shape - Input.       \param shape - Input.
305    */    */
306    static std::string    static
307    shapeToString(const DataArrayView::ShapeType& shape);    std::string
308      shapeToString(const ShapeType& shape);
309    
310    /**    /**
311      \brief      \brief
# Line 478  class DataArrayView { Line 484  class DataArrayView {
484       \param region - Input -       \param region - Input -
485                         Slice region.                         Slice region.
486    */    */
487    static DataArrayView::ShapeType    static
488      ShapeType
489    getResultSliceShape(const RegionType& region);    getResultSliceShape(const RegionType& region);
490    
491    /**    /**
# Line 488  class DataArrayView { Line 495  class DataArrayView {
495       \param key - Input -       \param key - Input -
496                      python slice object specifying region to be returned.                      python slice object specifying region to be returned.
497    
      \description  
   
498       The slice object is a tuple of n python slice specifiers, where       The slice object is a tuple of n python slice specifiers, where
499       n <= the rank of this Data object. Each slice specifier specifies the       n <= the rank of this Data object. Each slice specifier specifies the
500       range of indexes to be sliced from the corresponding dimension. The       range of indexes to be sliced from the corresponding dimension. The
# Line 537  class DataArrayView { Line 542  class DataArrayView {
542           getSliceRegion(:1,0:2)      => < <0,1> <0,2> <0,6> >           getSliceRegion(:1,0:2)      => < <0,1> <0,2> <0,6> >
543    
544    */    */
545    DataArrayView::RegionType    RegionType
546    getSliceRegion(const boost::python::object& key) const;    getSliceRegion(const boost::python::object& key) const;
547    
548    /**    /**
# Line 733  class DataArrayView { Line 738  class DataArrayView {
738       the data point using the given operation, returning the result as a       the data point using the given operation, returning the result as a
739       scalar. Operation must be a pointer to a function.       scalar. Operation must be a pointer to a function.
740    
      Called by escript::dp_algorithm.  
   
      \param operation - Input -  
                   Operation to apply. Must be a pointer to a function.  
   */  
   template <class UnaryFunction>  
   double  
   dp_reductionOp(UnaryFunction operation) const;  
   
   /**  
      \brief  
      Perform the given data point reduction operation on the data point  
      specified by the given offset into the view. Reduces all elements of  
      the data point using the given operation, returning the result as a  
      scalar. Operation must be a pointer to a function.  
   
      Called by escript::dp_algorithm.  
   
      \param offset - Input -  
                   Apply the operation to data starting at this offset in this view.  
      \param operation - Input -  
                   Operation to apply. Must be a pointer to a function.  
   */  
   template <class UnaryFunction>  
   double  
   dp_reductionOp(ValueType::size_type offset,  
                  UnaryFunction operation) const;  
   
   /**  
      \brief  
      Perform the given reduction operation on the data point  
      specified by the default offset into the view. Reduces all elements of  
      the data point using the given operation, returning the result as a  
      scalar. Operation must be a pointer to a function.  
   
741       Called by escript::algorithm.       Called by escript::algorithm.
742    
743       \param operation - Input -       \param operation - Input -
744                    Operation to apply. Must be a pointer to a function.                    Operation to apply. Must be a pointer to a function.
745    */    */
746    template <class UnaryFunction>    template <class BinaryFunction>
747    double    double
748    reductionOp(UnaryFunction operation) const;    reductionOp(BinaryFunction operation,
749                  double initial_value) const;
750    
751    /**    /**
752       \brief       \brief
753       Perform the given reduction operation on the data point       Perform the given data point reduction operation on the data point
754       specified by the given offset into the view. Reduces all elements of       specified by the given offset into the view. Reduces all elements of
755       the data point using the given operation, returning the result as a       the data point using the given operation, returning the result as a
756       scalar. Operation must be a pointer to a function.       scalar. Operation must be a pointer to a function.
# Line 791  class DataArrayView { Line 762  class DataArrayView {
762       \param operation - Input -       \param operation - Input -
763                    Operation to apply. Must be a pointer to a function.                    Operation to apply. Must be a pointer to a function.
764    */    */
765    template <class UnaryFunction>    template <class BinaryFunction>
766    double    double
767    reductionOp(ValueType::size_type offset,    reductionOp(ValueType::size_type offset,
768                UnaryFunction operation) const;                BinaryFunction operation,
769                  double initial_value) const;
770    
771    /**    /**
772       \brief       \brief
773       Perform matrix multiply.       Perform a matrix multiply of the given views.
774         This is purely a utility method and has no bearing on this view.
775    
776       Description:       NB: Only multiplies together the two given views, ie: two data-points,
777       Perform matrix multiply.       would need to call this over all data-points to multiply the entire
778         Data objects involved.
779    
780       \param left - Input - The left hand side.       \param left - Input - The left hand side.
781       \param right - Input - The right hand side.       \param right - Input - The right hand side.
782       \param result - Output - The result of the operation.       \param result - Output - The result of the operation.
783    */    */
784    static void    static
785      void
786    matMult(const DataArrayView& left,    matMult(const DataArrayView& left,
787            const DataArrayView& right,            const DataArrayView& right,
788            DataArrayView& result);            DataArrayView& result);
# Line 818  class DataArrayView { Line 793  class DataArrayView {
793       of the given views.       of the given views.
794       This is purely a utility method and has no bearing on this view.       This is purely a utility method and has no bearing on this view.
795    */    */
796    static ShapeType    static
797      ShapeType
798    determineResultShape(const DataArrayView& left,    determineResultShape(const DataArrayView& left,
799                         const DataArrayView& right);                         const DataArrayView& right);
800    
# Line 833  class DataArrayView { Line 809  class DataArrayView {
809    //    //
810    // The data values for the view.    // The data values for the view.
811    // NOTE: This points to data external to the view.    // NOTE: This points to data external to the view.
812    // This is just a pointer to a simple STL vector of doubles.    // This is just a pointer to an array of ValueType.
813    ValueType* m_data;    ValueType* m_data;
814    
815    //    //
# Line 958  DataArrayView::binaryOp(ValueType::size_ Line 934  DataArrayView::binaryOp(ValueType::size_
934                          BinaryFunction operation)                          BinaryFunction operation)
935  {  {
936    EsysAssert((!isEmpty()&&checkOffset(offset)),    EsysAssert((!isEmpty()&&checkOffset(offset)),
937               "Error - Couldn't perform binaryOp due to insufficient storage.");               "Error - Couldn't perform binaryOp due to insufficient storage in left object.");
938    for (ValueType::size_type i=0;i<noValues();i++) {    for (ValueType::size_type i=0;i<noValues();i++) {
939      (*m_data)[offset+i]=operation((*m_data)[offset+i],right);      (*m_data)[offset+i]=operation((*m_data)[offset+i],right);
940    }    }
941  }  }
942    
943  template <class UnaryFunction>  template <class BinaryFunction>
 inline  
 double  
 DataArrayView::dp_reductionOp(UnaryFunction operation) const  
 {  
   return dp_reductionOp(m_offset,operation);  
 }  
   
 template <class UnaryFunction>  
 inline  
 double  
 DataArrayView::dp_reductionOp(ValueType::size_type offset,  
                               UnaryFunction operation) const  
 {  
   EsysAssert((!isEmpty()&&checkOffset(offset)),  
                "Error - Couldn't perform dp_reductionOp due to insufficient storage.");  
   operation.resetResult();  
   for (ValueType::size_type i=0;i<noValues();i++) {  
     operation((*m_data)[offset+i]);  
   }  
   return operation.getResult();  
 }  
   
 template <class UnaryFunction>  
944  inline  inline
945  double  double
946  DataArrayView::reductionOp(UnaryFunction operation) const  DataArrayView::reductionOp(BinaryFunction operation,
947                               double initial_value) const
948  {  {
949    return reductionOp(m_offset,operation);    return reductionOp(m_offset,operation,initial_value);
950  }  }
951    
952  template <class UnaryFunction>  template <class BinaryFunction>
953  inline  inline
954  double  double
955  DataArrayView::reductionOp(ValueType::size_type offset,  DataArrayView::reductionOp(ValueType::size_type offset,
956                             UnaryFunction operation) const                             BinaryFunction operation,
957                               double initial_value) const
958  {  {
959    EsysAssert((!isEmpty()&&checkOffset(offset)),    EsysAssert((!isEmpty()&&checkOffset(offset)),
960                 "Error - Couldn't perform reductionOp due to insufficient storage.");                 "Error - Couldn't perform reductionOp due to insufficient storage.");
961      double current_value=initial_value;
962    for (ValueType::size_type i=0;i<noValues();i++) {    for (ValueType::size_type i=0;i<noValues();i++) {
963      operation((*m_data)[offset+i]);      current_value=operation(current_value,(*m_data)[offset+i]);
964    }    }
965    return operation.getResult();    return current_value;
966  }  }
967    
968  inline  inline

Legend:
Removed from v.108  
changed lines
  Added in v.478

  ViewVC Help
Powered by ViewVC 1.1.26