Commit f93155b9 authored by Tomáš Oberhuber's avatar Tomáš Oberhuber
Browse files

Writing ArrayView documentation plus few changes in Array.

parent 8ec56883
Loading
Loading
Loading
Loading
+13 −2
Original line number Diff line number Diff line
@@ -6,10 +6,21 @@ ELSE()
   ADD_CUSTOM_COMMAND( COMMAND ArrayExample > ArrayExample.out OUTPUT ArrayExample.out )
ENDIF()

IF( BUILD_CUDA )
   CUDA_ADD_EXECUTABLE( ArrayViewExampleCuda ArrayViewExample.cu )
   ADD_CUSTOM_COMMAND( COMMAND ArrayExampleCuda > ArrayViewExampleCuda.out OUTPUT ArrayViewExampleCuda.out )
ELSE()
   ADD_EXECUTABLE( ArrayViewExample ArrayViewExample.cpp )
   ADD_CUSTOM_COMMAND( COMMAND ArrayViewExample > ArrayViewExample.out OUTPUT ArrayViewExample.out )
ENDIF()


IF( BUILD_CUDA )
ADD_CUSTOM_TARGET( RunContainersExamples-cuda ALL DEPENDS
   ArrayExampleCuda.out )
   ArrayExampleCuda.out
   ArrayViewExampleCuda.out )
ELSE()
ADD_CUSTOM_TARGET( RunContainersExamples ALL DEPENDS
   ArrayExample.out )
   ArrayExample.out
   ArrayViewExample.out )
ENDIF()
+28 −24
Original line number Diff line number Diff line
@@ -261,7 +261,7 @@ class Array : public Object
      /**
       * \brief Swaps this array with another.
       *
       * The swap is done by swaping the meta-data, i.e. pointers and sizes.
       * The swap is done in a shallow way, i.e. swapping only pointers and sizes.
       * 
       * \param array is the array to be swapped with this array.
       */
@@ -331,6 +331,9 @@ class Array : public Object
      /**
       * \brief Array elements getter - returns value of an element at position \e i.
       *
       * This method can be called only from the host system (CPU) but even for
       * arrays allocated on device (GPU).
       * 
       * \param i Index position of an element.
       * 
       * \return Copy of i-th element.
@@ -380,7 +383,7 @@ class Array : public Object
      Array& operator = ( Array&& array );

      /**
       * \brief Assigns either TNL array or single value.
       * \brief Assigns either array-like container or single value.
       *
       * If \e T is array type i.e. \ref Array, \ref ArrayView, \ref StaticArray,
       * \ref Vector, \ref VectorView, \ref StaticVector, \ref DistributedArray,
@@ -419,12 +422,12 @@ class Array : public Object
      Array& operator = ( const std::vector< InValue >& vector );

      /**
       * \brief This function checks whether this array is equal to \e array.
       * \brief Comparison operator with another array-like container.
       *
       * \tparam ArrayT is type of array.
       * \tparam ArrayT is type of an array-like container, i.e Array, ArrayView, Vector, VectorView, DistributedArray, DistributedVector etc.
       * \param array is reference to an array.
       * 
       * \return True if arrays are equal and false otherwise.
       * \return True if both arrays are equal element-wise and false otherwise.
       */
      template< typename ArrayT >
      bool operator == ( const ArrayT& array ) const;
@@ -435,7 +438,7 @@ class Array : public Object
       * \tparam ArrayT Type of array.
       * \param array Reference to an array.
       * 
       * \return True if arrays are not equal and false otherwise.
       * \return True if both arrays are not equal element-wise and false otherwise.
       */
      template< typename ArrayT >
      bool operator != ( const ArrayT& array ) const;
@@ -452,21 +455,9 @@ class Array : public Object
                     Index end = -1 );

      /**
       * \brief Sets the array elements using given lambda function.
       * \brief Checks if there is an element with value \e v.
       *
       * Sets all the array values to \e v.
       *
       * \param v Reference to a value.
       */
      template< typename Function >
      void evaluate( Function& f,
                     const Index begin = 0,
                     Index end = -1 );

      /**
       * \brief Checks if there is an element with value \e v in this array.
       *
       * By default, the methods checks all array elements. By setting indexes
       * By default, the method checks all array elements. By setting indexes
       * \e begin and \e end, only elements in given interval are checked.
       * 
       * \param v is reference to the value.
@@ -474,16 +465,24 @@ class Array : public Object
       * \param end is the last element to be checked. If \e end equals -1, its
       * value is replaces by the array size.
       * 
       * \return True if array contains only given value in given interval.
       * \return True if there is **at least one** array element in interval [\e begin, \e end ) having value \e v.
       */
      bool containsValue( const Value& v,
                          const Index begin = 0,
                          Index end = -1 ) const;

      /**
       * \brief Checks if all elements in this array have the same value \e v.
       * \brief Checks if all elements have the same value \e v.
       *
       * By default, the method checks all array elements. By setting indexes
       * \e begin and \e end, only elements in given interval are checked.
       * 
       * \param v Reference to a value.
       * \param begin is the first element to be checked
       * \param end is the last element to be checked. If \e end equals -1, its
       * value is replaces by the array size.
       * 
       * \return True if there **all** array elements in interval [\e begin, \e end ) have value \e v.
       */
      bool containsOnlyValue( const Value& v,
                              const Index begin = 0,
@@ -491,8 +490,13 @@ class Array : public Object

      /**
       * \brief Returns true if non-zero size is set.
       * 
       * This method can be called from device kernels.
       * 
       * \return Returns \e true if array view size is zero, \e false otherwise.
       */
      //operator bool() const;
      __cuda_callable__
      bool empty() const;
      
      /**
       * \brief Method for saving the object to a \e file as a binary data.
+7 −27
Original line number Diff line number Diff line
@@ -73,6 +73,7 @@ Array( const Array< Value, Device, Index >& array )
  allocationPointer( nullptr ),
  referenceCounter( 0 )
{
   //this->bind( array );
   this->setSize( array.getSize() );
   Algorithms::ArrayOperations< Device >::copyMemory( this->getData(), array.getData(), array.getSize() );
}
@@ -580,28 +581,6 @@ void Array< Value, Device, Index >::setValue( const ValueType& e,
   Algorithms::ArrayOperations< Device >::setMemory( &this->getData()[ begin ], e, end - begin );
}

template< typename Value,
          typename Device,
          typename Index >
   template< typename Function >
void Array< Value, Device, Index >::evaluate( Function& f,
                                              const Index begin,
                                              Index end )
{
   TNL_ASSERT_TRUE( this->getData(), "Attempted to set a value of an empty array." );

   ValueType* d = this->data;
   auto eval = [=] __cuda_callable__ ( Index i )
   {
      d[ i ] = f( i );
   };
   
   if( end == -1 )
      end = this->getSize();

   ParallelFor< DeviceType >::exec( begin, end, eval );
}

template< typename Value,
          typename Device,
          typename Index >
@@ -634,14 +613,15 @@ containsOnlyValue( const Value& v,
   return Algorithms::ArrayOperations< Device >::containsOnlyValue( &this->getData()[ begin ], end - begin, v );
}

/*template< typename Value,
template< typename Value,
          typename Device,
          typename Index >
Array< Value, Device, Index >::operator bool() const
bool
Array< Value, Device, Index >::
empty() const
{
   return data != 0;
}*/

   return data;
}

template< typename Value,
          typename Device,
+274 −21
Original line number Diff line number Diff line
@@ -72,6 +72,13 @@ public:
   using HostType = ArrayView< Value, Devices::Host, Index >;
   using CudaType = ArrayView< Value, Devices::Cuda, Index >;

   /**
    * \brief Returns type of array view in C++ style.
    * 
    * \return String with array view type.
    */
   static String getType();   
   
   /**
    * \brief Basic constructor for empty ArrayView.
    * 
@@ -141,93 +148,339 @@ public:
   __cuda_callable__
   ArrayView( Array< Value_, Device, Index >& array );

   template< int Size, typename Value_ >  // template catches both const and non-const qualified Value
   /**
    * \brief Constructor for initialization with static array.
    * 
    * This method can be called from device kernels.
    * 
    * \tparam Size is size of the static array.
    * \tparam Value_ can be both const and non-const qualified Value.
    * 
    * \param array is a static array the array view is initialized with.
    */
   template< int Size, typename Value_ >
   __cuda_callable__
   ArrayView( StaticArray< Size, Value_ >& array );

   // these constructors will be used only when Value is const-qualified
   // (const views are initializable by const references)
   template< typename Value_ >  // template catches both const and non-const qualified Value
   /**
    * \brief Copy constructor from constant Array.
    * 
    * This constructor will be used only when Value is const-qualified
    * (const views are initializable by const references).
    * 
    * This method can be called from device kernels.
    * 
    * \tparam Value_ can be both const and non-const qualified Value
    * \param array is an array the array view is initialized with.
    */
   template< typename Value_ >
   __cuda_callable__
   ArrayView( const Array< Value_, Device, Index >& array );

   /**
    * \brief Constructor for initialization with static array.
    * 
    * This method can be called from device kernels.
    * 
    * \tparam Size is size of the static array.
    * \tparam Value_ can be both const and non-const qualified Value.
    * 
    * \param array is a static array the array view is initialized with.
    */
   template< int Size, typename Value_ >  // template catches both const and non-const qualified Value
   __cuda_callable__
   ArrayView( const StaticArray< Size, Value_ >& array );


   // methods for rebinding (reinitialization)
   /**
    * \brief Method for rebinding (reinitialization).
    * 
    * This method can be called from device kernels.
    * 
    * \param data is pointer to data to be bound to the array view.
    * \param size is the number of elements to be managed by the array view.
    */
   __cuda_callable__
   void bind( Value* data, const Index size );

   // Note that you can also bind directly to Array and other types implicitly
   // convertible to ArrayView.
   /**
    * \brief Method for rebinding (reinitialization) with another ArrayView.
    * 
    * Note that you can also bind directly to Array and other types implicitly
    * convertible to ArrayView.
    * 
    * This method can be called from device kernels.
    * 
    * \param view is array view to be bound.
    */   
   __cuda_callable__
   void bind( ArrayView view );


   // Copy-assignment does deep copy, just like regular array, but the sizes
   // must match (i.e. copy-assignment cannot resize).
   /**
    * \brief Assignment operator.
    * 
    * Copy-assignment does deep copy, just like regular array, but the sizes
    * must match (i.e. copy-assignment cannot resize).
    * 
    * \param view is array view to be copied
    */
   ArrayView& operator=( const ArrayView& view );

   template< typename Array >
   ArrayView& operator=( const Array& array );

   static String getType();
   /**
    * \brief Assignment operator for array-like containers or single value.
    *
    * If \e T is array type i.e. \ref Array, \ref ArrayView, \ref StaticArray,
    * \ref Vector, \ref VectorView, \ref StaticVector, \ref DistributedArray,
    * \ref DistributedArrayView, \ref DistributedVector or 
    * \ref DistributedVectorView, its elements are copied into this array. If
    * it is other type convertibly to ArrayView::ValueType, all array elements are 
    * set to the value \e data.
    * 
    * \tparam T is type of array or value type.
    * 
    * \param data is a reference to array or value.
    * 
    * \return Reference to this array.
    */
   template< typename T >
   ArrayView& operator=( const T& array );

   /**
    * \brief Swaps this array view content with another.
    *
    * The swap is done in a shallow way, i.e. swapping only  pointers and sizes.
    * 
    * This method can be called from device kernels.
    * 
    * \param view is the array view to be swapped with this array view.
    */   
   __cuda_callable__
   void swap( ArrayView& view );

   /***
    * \brief Resets the array view.
    * 
    * The array view behaves like being empty after calling this method.
    * 
    * This method can be called from device kernels.
    */
   __cuda_callable__
   void reset();

   /**
    * \brief Returns constant pointer to data managed by the array view.
    * 
    * This method can be called from device kernels.
    * 
    * \return Pointer to array data.
    */
   __cuda_callable__
   const Value* getData() const;

   /**
    * \brief Returns pointer to data managed by the array view.
    * 
    * This method can be called from device kernels.
    * 
    * \return Pointer to array data.
    */
   __cuda_callable__
   Value* getData();

   /**
    * \brief Returns constant pointer to data managed by the array view.
    * 
    * Use this method in algorithms where you want to emphasize that
    * C-style array pointer is required. 
    * 
    * This method can be called from device kernels.
    * 
    * \return Pointer to array data.
    */   
   __cuda_callable__
   const Value* getArrayData() const;

   /**
    * \brief Returns pointer to data managed by the array view.
    * 
    * Use this method in algorithms where you want to emphasize that
    * C-style array pointer is required. 
    * 
    * This method can be called from device kernels.
    * 
    * \return Pointer to array data.
    */   
   __cuda_callable__
   Value* getArrayData();

   /**
    * \brief Returns the array view size, i.e. number of elements being managed by the array view.
    * 
    * This method can be called from device kernels.
    * 
    * \return The array view size.
    */
   __cuda_callable__
   Index getSize() const;

   /**
    * \brief Array view elements setter - change value of an element at position \e i.
    *
    * This method can be called only from the host system (CPU) but even for
    * array views managing data allocated on device (GPU).
    * 
    * \param i is element index.
    * \param v is the new value of the element.
    */   
   void setElement( Index i, Value value );

   /**
    * \brief Array view elements getter - returns value of an element at position \e i.
    *
    * This method can be called only from the host system (CPU) but even for
    * array views managing data allocated on device (GPU).
    *
    * \param i Index position of an element.
    * 
    * \return Copy of i-th element.
    */
   Value getElement( Index i ) const;

   /**
    * \brief Accesses specified element at the position \e i.
    *
    * This method can be called from device (GPU) kernels if the array view
    * manages data allocated on the device. In this case, it cannot be called
    * from the host (CPU.)
    * 
    * \param i is position of the element.
    * 
    * \return Reference to i-th element.
    */   
   __cuda_callable__
   Value& operator[]( Index i );

   /**
    * \brief Returns constant reference to an element at a position \e i.
    *
    * This method can be called from device (GPU) kernels if the array view
    * manages data allocated on the device. In this case, it cannot be called
    * from the host (CPU.)
    * 
    * \param i is position of the element.
    * 
    * \return Reference to i-th element.
    */   
   __cuda_callable__
   const Value& operator[]( Index i ) const;

   /**
    * \brief Comparison operator with another array view \e view.
    *
    * \tparam Value_ is the value type of the right-hand-side array view.
    * \tparam Device_ is the device type of the right-hand-side array view.
    * \tparam Index_ is the index type of the right-hand-side array view.
    * \param  view is reference to the right-hand-side array view.
    * 
    * \return True if both array views are equal element-wise and false otherwise.
    */   
   template< typename Value_, typename Device_, typename Index_ >
   bool operator==( const ArrayView< Value_, Device_, Index_ >& view ) const;

   /**
    * \brief Comparison operator with another array-like container \e array.
    *
    * \tparam ArrayT is type of an array-like container, i.e Array, ArrayView, Vector, VectorView, DistributedArray, DistributedVector etc.
    * \param array is reference to an array.
    * 
    * \return True if both array views are equal element-wise and false otherwise.
    */   
   template< typename ArrayT >
   bool operator == ( const ArrayT& array ) const;   

   /**
    * \brief Comparison negation operator with another array view \e view.
    *
    * \tparam Value_ is the value type of the right-hand-side array view.
    * \tparam Device_ is the device type of the right-hand-side array view.
    * \tparam Index_ is the index type of the right-hand-side array view.
    * \param  view is reference to the right-hand-side array view.
    * 
    * \return True if both array views are not equal element-wise and false otherwise.
    */
   template< typename Value_, typename Device_, typename Index_ >
   bool operator!=( const ArrayView< Value_, Device_, Index_ >& view ) const;

   /**
    * \brief Comparison negation operator with another array-like container \e array.
    *
    * \tparam ArrayT is type of an array-like container, i.e Array, ArrayView, Vector, VectorView, DistributedArray, DistributedVector etc.
    * \param array is reference to an array.
    * 
    * \return True if both array views are not equal element-wise and false otherwise.
    */      
   template< typename ArrayT >
   bool operator != ( const ArrayT& array ) const;

   /**
    * \brief Sets the array view elements to given value.
    *
    * Sets all the array values to \e v.
    *
    * \param v Reference to a value.
    */
   void setValue( Value value );
   
   // Checks if there is an element with given value in this array
   /**
    * \brief Sets the array elements using given lambda function.
    *
    * Sets all the array values to \e v.
    *
    * \param v Reference to a value.
    */
   template< typename Function >
   void evaluate( Function& f,
                  const Index begin = 0,
                  Index end = -1 );   

   /**
    * \brief Checks if there is an element with value \e v.
    *
    * By default, the method checks all array view elements. By setting indexes
    * \e begin and \e end, only elements in given interval are checked.
    * 
    * \param v is reference to the value.
    * \param begin is the first element to be checked
    * \param end is the last element to be checked. If \e end equals -1, its
    * value is replaces by the array size.
    * 
    * \return True if there is **at least one** array element in interval [\e begin, \e end ) having value \e v.
    */   
   bool containsValue( Value value ) const;

   // Checks if all elements in this array have the same given value
   /**
    * \brief Checks if all elements have the same value \e v.
    *
    * By default, the method checks all array view elements. By setting indexes
    * \e begin and \e end, only elements in given interval are checked.
    * 
    * \param v Reference to a value.
    * \param begin is the first element to be checked
    * \param end is the last element to be checked. If \e end equals -1, its
    * value is replaces by the array size.
    * 
    * \return True if there **all** array elements in interval [\e begin, \e end ) have value \e v.
    */
   bool containsOnlyValue( Value value ) const;

   //! Returns true if non-zero size is set.
   operator bool() const;
   /**
    * \brief Returns true if non-zero size is set.
    * 
    * This method can be called from device kernels.
    * 
    * \return Returns \e true if array view size is zero, \e false otherwise.
    */
   __cuda_callable__
   bool empty() const;

protected:
   //! Pointer to allocated data
@@ -243,4 +496,4 @@ std::ostream& operator<<( std::ostream& str, const ArrayView< Value, Device, Ind
} // namespace Containers
} // namespace TNL

#include <TNL/Containers/ArrayView_impl.h>
#include <TNL/Containers/ArrayView.hpp>
+0 −392

File deleted.

Preview size limit exceeded, changes collapsed.

Loading