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

Adding operator << for DistributedArray(View).

parent b6326d3d
Loading
Loading
Loading
Loading
+71 −0
Original line number Diff line number Diff line
@@ -193,6 +193,67 @@ public:
   template< typename Array >
   bool operator!=( const Array& array ) const;

      /**
       * \brief Process the lambda function \e f for each array element in interval [ \e begin, \e end).
       *
       * The lambda function is supposed to be declared as
       *
       * ```
       * f( IndexType elementIdx, ValueType& elementValue )
       * ```
       *
       * where
       *
       * - \e elementIdx is an index of the array element being currently processed
       * - \e elementValue is a value of the array element being currently processed
       *
       * This is performed at the same place where the array is allocated,
       * i.e. it is efficient even on GPU.
       *
       * \param begin The beginning of the array elements interval.
       * \param end The end of the array elements interval.
       * \param f The lambda function to be processed.
       *
       * \par Example
       * \include Containers/ArrayExample_forElements.cpp
       * \par Output
       * \include ArrayExample_forElements.out
       *
       */
      template< typename Function >
      void forElements( IndexType begin, IndexType end, Function&& f );

      /**
       * \brief Process the lambda function \e f for each array element in interval [ \e begin, \e end) for constant instances of the array.
       *
       * The lambda function is supposed to be declared as
       *
       * ```
       * f( IndexType elementIdx, ValueType& elementValue )
       * ```
       *
       * where
       *
       * - \e elementIdx is an index of the array element being currently processed
       * - \e elementValue is a value of the array element being currently processed
       *
       * This is performed at the same place where the array is allocated,
       * i.e. it is efficient even on GPU.
       *
       * \param begin The beginning of the array elements interval.
       * \param end The end of the array elements interval.
       * \param f The lambda function to be processed.
       *
       * \par Example
       * \include Containers/ArrayExample_forElements.cpp
       * \par Output
       * \include ArrayExample_forElements.out
       *
       */
      template< typename Function >
      void forElements( IndexType begin, IndexType end, Function&& f ) const;


   // Checks if there is an element with given value in this array
   bool containsValue( ValueType value ) const;

@@ -215,6 +276,16 @@ private:
   static void setSynchronizerHelper( ViewType& view, const Array& array ) {}
};

template< typename Value,
          typename Device,
          typename Index,
          typename Allocator >
std::ostream& operator<<( std::ostream& str, const DistributedArray< Value, Device, Index, Allocator >& array )
{
   return array.getConstView().print( str );
}


} // namespace Containers
} // namespace TNL

+24 −0
Original line number Diff line number Diff line
@@ -449,6 +449,30 @@ operator!=( const Array& array ) const
   return view != array;
}

template< typename Value,
          typename Device,
          typename Index,
          typename Allocator >
   template< typename Function >
void
DistributedArray< Value, Device, Index, Allocator >::
forElements( IndexType begin, IndexType end, Function&& f )
{
   this->view.forElements( begin, end, f );
}

template< typename Value,
          typename Device,
          typename Index,
          typename Allocator >
   template< typename Function >
void
DistributedArray< Value, Device, Index, Allocator >::
forElements( IndexType begin, IndexType end, Function&& f ) const
{
   this->view.forElements( begin, end, f );
}

template< typename Value,
          typename Device,
          typename Index,
+71 −0
Original line number Diff line number Diff line
@@ -170,12 +170,73 @@ public:
   template< typename Array >
   bool operator!=( const Array& array ) const;

      /**
       * \brief Process the lambda function \e f for each array element in interval [ \e begin, \e end).
       *
       * The lambda function is supposed to be declared as
       *
       * ```
       * f( IndexType elementIdx, ValueType& elementValue )
       * ```
       *
       * where
       *
       * - \e elementIdx is an index of the array element being currently processed
       * - \e elementValue is a value of the array element being currently processed
       *
       * This is performed at the same place where the array is allocated,
       * i.e. it is efficient even on GPU.
       *
       * \param begin The beginning of the array elements interval.
       * \param end The end of the array elements interval.
       * \param f The lambda function to be processed.
       *
       * \par Example
       * \include Containers/ArrayExample_forElements.cpp
       * \par Output
       * \include ArrayExample_forElements.out
       *
       */
      template< typename Function >
      void forElements( IndexType begin, IndexType end, Function&& f );

      /**
       * \brief Process the lambda function \e f for each array element in interval [ \e begin, \e end) for constant instances of the array.
       *
       * The lambda function is supposed to be declared as
       *
       * ```
       * f( IndexType elementIdx, ValueType& elementValue )
       * ```
       *
       * where
       *
       * - \e elementIdx is an index of the array element being currently processed
       * - \e elementValue is a value of the array element being currently processed
       *
       * This is performed at the same place where the array is allocated,
       * i.e. it is efficient even on GPU.
       *
       * \param begin The beginning of the array elements interval.
       * \param end The end of the array elements interval.
       * \param f The lambda function to be processed.
       *
       * \par Example
       * \include Containers/ArrayExample_forElements.cpp
       * \par Output
       * \include ArrayExample_forElements.out
       *
       */
      template< typename Function >
      void forElements( IndexType begin, IndexType end, Function&& f ) const;

   // Checks if there is an element with given value in this array
   bool containsValue( ValueType value ) const;

   // Checks if all elements in this array have the same given value
   bool containsOnlyValue( ValueType value ) const;

   std::ostream& print( std::ostream& str ) const;
protected:
   LocalRangeType localRange;
   IndexType ghosts = 0;
@@ -187,6 +248,16 @@ protected:
   int valuesPerElement = 1;
};


template< typename Value,
          typename Device = Devices::Host,
          typename Index = int >
std::ostream& operator<<( std::ostream& str, const DistributedArrayView< Value, Device, Index >& view )
{
   return view.print( str );
}


} // namespace Containers
} // namespace TNL

+57 −0
Original line number Diff line number Diff line
@@ -435,6 +435,37 @@ operator!=( const Array& array ) const
   return ! (*this == array);
}

template< typename Value,
          typename Device,
          typename Index >
   template< typename Function >
void
DistributedArrayView< Value, Device, Index >::
forElements( IndexType begin, IndexType end, Function&& f )
{
   IndexType localBegin = max( begin, localRange.getBegin() );
   IndexType localEnd = min( end, localRange.getEnd() );
   auto local_f = [=] __cuda_callable__ ( const IndexType& idx, ValueType& value ) mutable {
      f( idx + localRange.getBegin(), value );
   };
   this->localData.forElements( localBegin - localRange.getBegin(),
                                localEnd - localRange.getBegin(),
                                local_f );

}

template< typename Value,
          typename Device,
          typename Index >
   template< typename Function >
void
DistributedArrayView< Value, Device, Index >::
forElements( IndexType begin, IndexType end, Function&& f ) const
{

}


template< typename Value,
          typename Device,
          typename Index >
@@ -465,5 +496,31 @@ containsOnlyValue( ValueType value ) const
   return result;
}

template< typename Value,
          typename Device,
          typename Index >
std::ostream&
DistributedArrayView< Value, Device, Index >::
print( std::ostream& str ) const
{
   // The following does not work properly
   /*if( MPI::GetRank( group ) == 0 )
   {
      str << "[ ";
      for( IndexType i = 0; i < localData.getSize(); i++ )
         str << ", " << localData.getElement( i );
      for( int proc = 1; proc < MPI::GetSize( group ); proc++ )
      {
         Array< std::remove_const_t< Value >, Device, Index > localArray;
         receive( localArray, proc, 0, group );
         for( IndexType i = 0; i < localArray.getSize(); i++ )
            str << ", " << localArray.getElement( i );
      }
      str << " ]";
   }
   else send( this->localData, 0, 0, this->group );
   return str;*/
}

} // namespace Containers
} // namespace TNL