Template Numerical Library: TNL::Containers::DistributedVector< Real, Device, Index, Allocator > Class Template Reference

Template Numerical Library version\ main:be7e16d88

Loading...
Searching...
No Matches
Public Types | Public Member Functions | List of all members
TNL::Containers::DistributedVector< Real, Device, Index, Allocator > Class Template Reference

DistributedVector extends DistributedArray with algebraic operations. More...

`#include <TNL/Containers/DistributedVector.h>`

Inheritance diagram for TNL::Containers::DistributedVector< Real, Device, Index, Allocator >:
[legend]
Collaboration diagram for TNL::Containers::DistributedVector< Real, Device, Index, Allocator >:
[legend]

## Public Types
using | AllocatorType = Allocator
using | ConstLocalViewType = Containers::VectorView< std::add_const_t< Real >, Device, Index >
using | ConstViewType = DistributedVectorView< std::add_const_t< Real >, Device, Index >
using | DeviceType = Device
using | IndexType = Index
using | LocalViewType = Containers::VectorView< Real, Device, Index >
using | RealType = Real
template<typename _Real, typename _Device = Device, typename _Index = Index, typename _Allocator = typename Allocators::Default< _Device >::template Allocator< _Real >>
using | Self = DistributedVector< _Real, _Device, _Index, _Allocator >
| A template which allows to quickly obtain a Vector type with changed template parameters.
using | ViewType = DistributedVectorView< Real, Device, Index >
Public Types inherited from TNL::Containers::DistributedArray< Real, Devices::Host, int, typename Allocators::Default< Devices::Host >::template Allocator< Real > >
using | AllocatorType
using | ConstLocalViewType
using | ConstViewType
using | DeviceType
using | IndexType
using | LocalRangeType
using | LocalViewType
using | Self
| A template which allows to quickly obtain a DistributedArray type with changed template parameters.
using | SynchronizerType
using | ValueType
using | ViewType

## Public Member Functions
| DistributedVector (const DistributedVector &)=default
| Copy constructor (makes a deep copy).
| DistributedVector (const DistributedVector &vector, const AllocatorType &allocator)
| Copy constructor with a specific allocator (makes a deep copy).
| DistributedVector (DistributedVector &&) noexcept=default
| Default move constructor.
| DistributedArray ()=default
| Constructs an empty array with zero size.
| DistributedArray (const AllocatorType &allocator)
| Constructs an empty array and sets the provided allocator.
| DistributedArray (const DistributedArray &array)
| Copy constructor (makes a deep copy).
| DistributedArray (const DistributedArray &array, const AllocatorType &allocator)
| Copy constructor with a specific allocator (makes a deep copy).
| DistributedArray (DistributedArray &&) noexcept=default
| DistributedArray (LocalRangeType localRange, Index ghosts, Index globalSize, const MPI::Comm &communicator, const AllocatorType &allocator=AllocatorType())
ConstLocalViewType | getConstLocalView () const
| Returns a non-modifiable view of the local part of the vector.
ConstLocalViewType | getConstLocalViewWithGhosts () const
| Returns a non-modifiable view of the local part of the vector, including ghost values.
ConstViewType | getConstView () const
| Returns a non-modifiable view of the vector.
LocalViewType | getLocalView ()
| Returns a modifiable view of the local part of the vector.
LocalViewType | getLocalViewWithGhosts ()
| Returns a modifiable view of the local part of the vector, including ghost values.
ViewType | getView ()
| Returns a modifiable view of the vector.
| operator ConstViewType () const
| Conversion operator to a non-modifiable view of the vector.
| operator ViewType ()
| Conversion operator to a modifiable view of the vector.
template<typename Vector, typename..., typename = std::enable_if_t< HasSubscriptOperator< Vector >::value >>
DistributedVector & | operator%= (const Vector &vector)
template<typename Scalar, typename..., typename = std::enable_if_t< ! HasSubscriptOperator< Scalar >::value >>
DistributedVector & | operator%= (Scalar c)
template<typename Vector, typename..., typename = std::enable_if_t< HasSubscriptOperator< Vector >::value >>
DistributedVector & | operator*= (const Vector &vector)
template<typename Scalar, typename..., typename = std::enable_if_t< ! HasSubscriptOperator< Scalar >::value >>
DistributedVector & | operator*= (Scalar c)
template<typename Vector, typename..., typename = std::enable_if_t< HasSubscriptOperator< Vector >::value >>
DistributedVector & | operator+= (const Vector &vector)
template<typename Scalar, typename..., typename = std::enable_if_t< ! HasSubscriptOperator< Scalar >::value >>
DistributedVector & | operator+= (Scalar c)
template<typename Vector, typename..., typename = std::enable_if_t< HasSubscriptOperator< Vector >::value >>
DistributedVector & | operator-= (const Vector &vector)
template<typename Scalar, typename..., typename = std::enable_if_t< ! HasSubscriptOperator< Scalar >::value >>
DistributedVector & | operator-= (Scalar c)
template<typename Vector, typename..., typename = std::enable_if_t< HasSubscriptOperator< Vector >::value >>
DistributedVector & | operator/= (const Vector &vector)
template<typename Scalar, typename..., typename = std::enable_if_t< ! HasSubscriptOperator< Scalar >::value >>
DistributedVector & | operator/= (Scalar c)
DistributedVector & | operator= (const DistributedVector &)=default
| Copy-assignment operator for copying data from another vector.
template<typename Vector, typename..., typename = std::enable_if_t< HasSubscriptOperator< Vector >::value >>
DistributedVector & | operator= (const Vector &vector)
DistributedVector & | operator= (DistributedVector &&) noexcept(false)=default
| Move-assignment operator for acquiring data from rvalues.
template<typename Scalar, typename..., typename = std::enable_if_t< ! HasSubscriptOperator< Scalar >::value >>
DistributedVector & | operator= (Scalar c)
Public Member Functions inherited from TNL::Containers::DistributedArray< Real, Devices::Host, int, typename Allocators::Default< Devices::Host >::template Allocator< Real > >
| DistributedArray ()=default
| Constructs an empty array with zero size.
void | copyFromGlobal (ConstLocalViewType globalArray)
bool | empty () const
void | forElements (IndexType begin, IndexType end, Function &&f)
| Process the lambda function f for each array element in interval [ begin, end).
AllocatorType | getAllocator () const
| Returns the allocator associated to the array.
const MPI::Comm & | getCommunicator () const
| Returns the MPI communicator associated to the array.
ConstLocalViewType | getConstLocalView () const
| Returns a non-modifiable view of the local part of the array.
ConstLocalViewType | getConstLocalViewWithGhosts () const
| Returns a non-modifiable view of the local part of the array, including ghost values.
ConstViewType | getConstView () const
| Returns a non-modifiable view of the array.
ValueType | getElement (IndexType i) const
IndexType | getGhosts () const
const LocalRangeType & | getLocalRange () const
| Returns the local range of the distributed array.
LocalViewType | getLocalView ()
| Returns a modifiable view of the local part of the array.
LocalViewType | getLocalViewWithGhosts ()
| Returns a modifiable view of the local part of the array, including ghost values.
IndexType | getSize () const
std::shared_ptr< SynchronizerType > | getSynchronizer () const
int | getValuesPerElement () const
ViewType | getView ()
| Returns a modifiable view of the array.
void | loadFromGlobalFile (const String &fileName, bool allowCasting=false)
| operator ConstViewType () const
| Conversion operator to a non-modifiable view of the array.
| operator ViewType ()
| Conversion operator to a modifiable view of the array.
bool | operator!= (const Array &array) const
DistributedArray & | operator= (const DistributedArray &array)
bool | operator== (const Array &array) const
__cuda_callable__ ValueType & | operator[] (IndexType i)
void | reset ()
void | setDistribution (LocalRangeType localRange, int ghosts, int globalSize, const MPI::Comm &communicator)
| Set new global size and distribution of the array.
void | setElement (IndexType i, ValueType value)
void | setLike (const Array &array)
void | setSynchronizer (std::shared_ptr< SynchronizerType > synchronizer, int valuesPerElement=1)
void | setValue (ValueType value)
void | startSynchronization ()
void | waitForSynchronization () const

## Additional Inherited Members
Protected Attributes inherited from TNL::Containers::DistributedArray< Real, Devices::Host, int, typename Allocators::Default< Devices::Host >::template Allocator< Real > >
LocalArrayType | localData
ViewType | view

## Detailed Description

template<typename Real, typename Device = Devices::Host, typename Index = int, typename Allocator = typename Allocators::Default< Device >::template Allocator< Real >>
class TNL::Containers::DistributedVector< Real, Device, Index, Allocator >

DistributedVector extends DistributedArray with algebraic operations.

Example
  #include <iostream>
  #include <TNL/Containers/BlockPartitioning.h>
  #include <TNL/Containers/DistributedVector.h>
  #include <TNL/MPI/ScopedInitializer.h>
  /***
  * The following works for any device (CPU, GPU ...).
  */
  template< typename Device >
  void
  distributedVectorExample()
  {
  using VectorType = TNL::Containers::DistributedVector< int, Device >;
  using IndexType = typename VectorType::IndexType;
  using LocalRangeType = typename VectorType::LocalRangeType;
  const TNL::MPI::Comm communicator = MPI_COMM_WORLD;
  // We set the global vector size to a prime number to force non-uniform distribution.
  const int size = 97;
  const int ghosts = ( communicator.size() > 1 ) ? 4 : 0;
  const LocalRangeType localRange = TNL::Containers::splitRange< IndexType >( size, communicator );
  VectorType v( localRange, ghosts, size, communicator );
  v.forElements( 0,
  size,
  [] __cuda_callable__( int idx, int& value )
  {
  value = idx;
  } );
  std::cout << "Rank " << communicator.rank() << " has subrange " << localRange << '\n';
  const int sum = TNL::sum( v );
  if( communicator.rank() == 0 )
  std::cout << "Global sum is " << sum << '\n';
  }
  int
  main( int argc, char* argv[] )
  {
  TNL::MPI::ScopedInitializer mpi( argc, argv );
  if( TNL::MPI::GetRank() == 0 )
  std::cout << "The first test runs on CPU ...\n";
  distributedVectorExample< TNL::Devices::Host >();
  #ifdef __CUDACC__
  TNL::MPI::Barrier();
  if( TNL::MPI::GetRank() == 0 )
  std::cout << "The second test runs on GPU ...\n";
  distributedVectorExample< TNL::Devices::Cuda >();
  #endif
  }
  __cuda_callable__
  #define __cuda_callable__
  Definition Macros.h:49
  std::cout
  TNL::Containers::DistributedVector
  DistributedVector extends DistributedArray with algebraic operations.
  Definition DistributedVector.h:24
  TNL::MPI::Comm
  An RAII wrapper for custom MPI communicators.
  Definition Comm.h:63
  TNL::MPI::Comm::size
  int size() const
  Returns the size of the group associated with a communicator.
  Definition Comm.h:227
  TNL::MPI::Comm::rank
  int rank() const
  Determines the rank of the calling process in the communicator.
  Definition Comm.h:220
  TNL::Containers::splitRange
  Subrange< Index > splitRange(Index rangeBegin, Index rangeEnd, int rank, int num_subintervals)
  A helper function which splits a one-dimensional range.
  Definition BlockPartitioning.h:27
  TNL::MPI::ScopedInitializer
  Definition ScopedInitializer.h:63

Output
  Rank 1 detected 0 GPUs.
  Rank 1 has subrange TNL::Containers::Subrange<int>( 25, 49 )
  Rank 2 detected 0 GPUs.
  Rank 2 has subrange TNL::Containers::Subrange<int>( 49, 73 )
  Rank 0 detected 0 GPUs.
  The first test runs on CPU ...
  Rank 0 has subrange TNL::Containers::Subrange<int>( 0, 25 )
  Rank 3 detected 0 GPUs.
  Rank 3 has subrange TNL::Containers::Subrange<int>( 73, 97 )
  Global sum is 4656

## Member Function Documentation

## ◆ DistributedArray() [1/3]

template<typename Real, typename Device = Devices::Host, typename Index = int, typename Allocator = typename Allocators::Default< Device >::template Allocator< Real >>

TNL::Containers::DistributedArray< Real, Device, Index, Allocator >::DistributedArray | ( | const AllocatorType & | allocator | ) |
| explicit

Constructs an empty array and sets the provided allocator.

Parameters

  allocator | The allocator to be associated with this array.

## ◆ DistributedArray() [2/3]

template<typename Real, typename Device = Devices::Host, typename Index = int, typename Allocator = typename Allocators::Default< Device >::template Allocator< Real >>

TNL::Containers::DistributedArray< Real, Device, Index, Allocator >::DistributedArray | ( | const DistributedArray & | array | ) |
| explicit

Copy constructor (makes a deep copy).

Parameters

  array | The array to be copied.

## ◆ DistributedArray() [3/3]

template<typename Real, typename Device = Devices::Host, typename Index = int, typename Allocator = typename Allocators::Default< Device >::template Allocator< Real >>

TNL::Containers::DistributedArray< Real, Device, Index, Allocator >::DistributedArray | ( | const DistributedArray & | array,
|  | const AllocatorType & | allocator )
| explicit

Copy constructor with a specific allocator (makes a deep copy).

Parameters

  array | The array to be copied.
  allocator | The allocator to be associated with this array.

The documentation for this class was generated from the following file:

- src/TNL/Containers/DistributedVector.h