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

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

  ViewVC Help
Powered by ViewVC 1.1.26