/[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 474 by jgs, Mon Jan 30 04:23:44 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 "DataException.h"
23    #include "DataVector.h"
24    
25    #include <boost/python/extract.hpp>
26  #include <boost/python/numeric.hpp>  #include <boost/python/numeric.hpp>
27  #include <boost/python/object.hpp>  #include <boost/python/object.hpp>
28  #include <boost/shared_ptr.hpp>  #include <boost/shared_ptr.hpp>
29    
30  #include <iostream>  #include <iostream>
31    #include <sstream>
32  #include <vector>  #include <vector>
33    #include <iostream>
34    
35  namespace escript {  namespace escript {
36    
# Line 42  namespace escript { Line 48  namespace escript {
48     The view provided represents a single n-dimensional data-point     The view provided represents a single n-dimensional data-point
49     comprised of values taken from the given data array, starting at the     comprised of values taken from the given data array, starting at the
50     specified offset and extending for as many values as are necessary to     specified offset and extending for as many values as are necessary to
51     satisfy the given shape.     satisfy the given shape. The default offset can be changed, or different
52       offsets specified, in order to provide views of other data-points in
53       the underlying data array.
54  */  */
55    
56  class DataArrayView {  class DataArrayView {
# Line 52  class DataArrayView { Line 60  class DataArrayView {
60    
61   public:   public:
62    
63    /**    //
64        Some basic types which define the data values and view shapes.    // Some basic types which define the data values and view shapes.
65    */    typedef DataVector                        ValueType;
66    typedef std::vector<double>               ValueType;    //typedef std::vector<double>               ValueType;
67    typedef std::vector<int>                  ShapeType;    typedef std::vector<int>                  ShapeType;
68    typedef std::vector<std::pair<int, int> > RegionType;    typedef std::vector<std::pair<int, int> > RegionType;
69    typedef std::vector<std::pair<int, int> > RegionLoopRangeType;    typedef std::vector<std::pair<int, int> > RegionLoopRangeType;
# Line 208  class DataArrayView { Line 216  class DataArrayView {
216    */    */
217    bool    bool
218    checkOffset() const;    checkOffset() const;
219    
220    bool    bool
221    checkOffset(ValueType::size_type offset) const;    checkOffset(ValueType::size_type offset) const;
222    
# Line 230  class DataArrayView { Line 239  class DataArrayView {
239       Calculate the number of values for the given shape or region.       Calculate the number of values for the given shape or region.
240       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.
241    */    */
242    static int    static
243      int
244    noValues(const ShapeType& shape);    noValues(const ShapeType& shape);
245    static int  
246      static
247      int
248    noValues(const RegionLoopRangeType& region);    noValues(const RegionLoopRangeType& region);
249    
250    /**    /**
# Line 254  class DataArrayView { Line 266  class DataArrayView {
266       \brief       \brief
267       Return the shape of this view.       Return the shape of this view.
268    */    */
269    const ShapeType&    const
270      ShapeType&
271    getShape() const;    getShape() const;
272    
273    /**    /**
274       \brief       \brief
275       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.  
276    */    */
277    bool    bool
278    checkShape(const DataArrayView::ShapeType& other) const;    checkShape(const ShapeType& other) const;
279    
280    /**    /**
281       \brief       \brief
282       Create a shape error message. Normally used when there is a shape       Create a shape error message. Normally used when there is a shape
283       mismatch.       mismatch between this shape and the other shape.
      This is purely a utility method and has no bearing on this view.  
284    
285       \param messagePrefix - Input -       \param messagePrefix - Input -
286                         First part of the error message.                         First part of the error message.
# Line 278  class DataArrayView { Line 289  class DataArrayView {
289    */    */
290    std::string    std::string
291    createShapeErrorMessage(const std::string& messagePrefix,    createShapeErrorMessage(const std::string& messagePrefix,
292                            const DataArrayView::ShapeType& other) const;                            const ShapeType& other) const;
293    
294    /**    /**
295       \brief       \brief
# Line 298  class DataArrayView { Line 309  class DataArrayView {
309    
310       \param shape - Input.       \param shape - Input.
311    */    */
312    static std::string    static
313    shapeToString(const DataArrayView::ShapeType& shape);    std::string
314      shapeToString(const ShapeType& shape);
315    
316    /**    /**
317      \brief      \brief
# Line 478  class DataArrayView { Line 490  class DataArrayView {
490       \param region - Input -       \param region - Input -
491                         Slice region.                         Slice region.
492    */    */
493    static DataArrayView::ShapeType    static
494      ShapeType
495    getResultSliceShape(const RegionType& region);    getResultSliceShape(const RegionType& region);
496    
497    /**    /**
# Line 488  class DataArrayView { Line 501  class DataArrayView {
501       \param key - Input -       \param key - Input -
502                      python slice object specifying region to be returned.                      python slice object specifying region to be returned.
503    
      \description  
   
504       The slice object is a tuple of n python slice specifiers, where       The slice object is a tuple of n python slice specifiers, where
505       n <= the rank of this Data object. Each slice specifier specifies the       n <= the rank of this Data object. Each slice specifier specifies the
506       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 548  class DataArrayView {
548           getSliceRegion(:1,0:2)      => < <0,1> <0,2> <0,6> >           getSliceRegion(:1,0:2)      => < <0,1> <0,2> <0,6> >
549    
550    */    */
551    DataArrayView::RegionType    RegionType
552    getSliceRegion(const boost::python::object& key) const;    getSliceRegion(const boost::python::object& key) const;
553    
554    /**    /**
# Line 733  class DataArrayView { Line 744  class DataArrayView {
744       the data point using the given operation, returning the result as a       the data point using the given operation, returning the result as a
745       scalar. Operation must be a pointer to a function.       scalar. Operation must be a pointer to a function.
746    
      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.  
   
747       Called by escript::algorithm.       Called by escript::algorithm.
748    
749       \param operation - Input -       \param operation - Input -
750                    Operation to apply. Must be a pointer to a function.                    Operation to apply. Must be a pointer to a function.
751    */    */
752    template <class UnaryFunction>    template <class BinaryFunction>
753    double    double
754    reductionOp(UnaryFunction operation) const;    reductionOp(BinaryFunction operation,
755                  double initial_value) const;
756    
757    /**    /**
758       \brief       \brief
759       Perform the given reduction operation on the data point       Perform the given data point reduction operation on the data point
760       specified by the given offset into the view. Reduces all elements of       specified by the given offset into the view. Reduces all elements of
761       the data point using the given operation, returning the result as a       the data point using the given operation, returning the result as a
762       scalar. Operation must be a pointer to a function.       scalar. Operation must be a pointer to a function.
# Line 791  class DataArrayView { Line 768  class DataArrayView {
768       \param operation - Input -       \param operation - Input -
769                    Operation to apply. Must be a pointer to a function.                    Operation to apply. Must be a pointer to a function.
770    */    */
771    template <class UnaryFunction>    template <class BinaryFunction>
772    double    double
773    reductionOp(ValueType::size_type offset,    reductionOp(ValueType::size_type offset,
774                UnaryFunction operation) const;                BinaryFunction operation,
775                  double initial_value) const;
776    
777    /**    /**
778       \brief       \brief
779       Perform matrix multiply.       Perform a matrix multiply of the given views.
780         This is purely a utility method and has no bearing on this view.
781    
782       Description:       NB: Only multiplies together the two given views, ie: two data-points,
783       Perform matrix multiply.       would need to call this over all data-points to multiply the entire
784         Data objects involved.
785    
786       \param left - Input - The left hand side.       \param left - Input - The left hand side.
787       \param right - Input - The right hand side.       \param right - Input - The right hand side.
788       \param result - Output - The result of the operation.       \param result - Output - The result of the operation.
789    */    */
790    static void    static
791      void
792    matMult(const DataArrayView& left,    matMult(const DataArrayView& left,
793            const DataArrayView& right,            const DataArrayView& right,
794            DataArrayView& result);            DataArrayView& result);
# Line 818  class DataArrayView { Line 799  class DataArrayView {
799       of the given views.       of the given views.
800       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.
801    */    */
802    static ShapeType    static
803      ShapeType
804    determineResultShape(const DataArrayView& left,    determineResultShape(const DataArrayView& left,
805                         const DataArrayView& right);                         const DataArrayView& right);
806    
# Line 833  class DataArrayView { Line 815  class DataArrayView {
815    //    //
816    // The data values for the view.    // The data values for the view.
817    // NOTE: This points to data external to the view.    // NOTE: This points to data external to the view.
818    // This is just a pointer to a simple STL vector of doubles.    // This is just a pointer to an array of ValueType.
819    ValueType* m_data;    ValueType* m_data;
820    
821    //    //
# Line 958  DataArrayView::binaryOp(ValueType::size_ Line 940  DataArrayView::binaryOp(ValueType::size_
940                          BinaryFunction operation)                          BinaryFunction operation)
941  {  {
942    EsysAssert((!isEmpty()&&checkOffset(offset)),    EsysAssert((!isEmpty()&&checkOffset(offset)),
943               "Error - Couldn't perform binaryOp due to insufficient storage.");               "Error - Couldn't perform binaryOp due to insufficient storage in left object.");
944    for (ValueType::size_type i=0;i<noValues();i++) {    for (ValueType::size_type i=0;i<noValues();i++) {
945      (*m_data)[offset+i]=operation((*m_data)[offset+i],right);      (*m_data)[offset+i]=operation((*m_data)[offset+i],right);
946    }    }
947  }  }
948    
949  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>  
950  inline  inline
951  double  double
952  DataArrayView::reductionOp(UnaryFunction operation) const  DataArrayView::reductionOp(BinaryFunction operation,
953                               double initial_value) const
954  {  {
955    return reductionOp(m_offset,operation);    return reductionOp(m_offset,operation,initial_value);
956  }  }
957    
958  template <class UnaryFunction>  template <class BinaryFunction>
959  inline  inline
960  double  double
961  DataArrayView::reductionOp(ValueType::size_type offset,  DataArrayView::reductionOp(ValueType::size_type offset,
962                             UnaryFunction operation) const                             BinaryFunction operation,
963                               double initial_value) const
964  {  {
965    EsysAssert((!isEmpty()&&checkOffset(offset)),    EsysAssert((!isEmpty()&&checkOffset(offset)),
966                 "Error - Couldn't perform reductionOp due to insufficient storage.");                 "Error - Couldn't perform reductionOp due to insufficient storage.");
967      double current_value=initial_value;
968    for (ValueType::size_type i=0;i<noValues();i++) {    for (ValueType::size_type i=0;i<noValues();i++) {
969      operation((*m_data)[offset+i]);      current_value=operation(current_value,(*m_data)[offset+i]);
970    }    }
971    return operation.getResult();    return current_value;
972  }  }
973    
974  inline  inline

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

  ViewVC Help
Powered by ViewVC 1.1.26