Template Numerical Library version\ main:c173bea
Loading...
Searching...
No Matches
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 >:
Collaboration diagram for TNL::Containers::DistributedVector< Real, Device, Index, Allocator >:

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 >>
DistributedVectoroperator%= (const Vector &vector)
 
template<typename Vector , typename... , typename >
DistributedVector< Real, Device, Index, Allocator > & operator%= (const Vector &vector)
 
template<typename Scalar , typename... , typename = std::enable_if_t< ! HasSubscriptOperator< Scalar >::value >>
DistributedVectoroperator%= (Scalar c)
 
template<typename Scalar , typename... , typename >
DistributedVector< Real, Device, Index, Allocator > & operator%= (Scalar c)
 
template<typename Vector , typename... , typename = std::enable_if_t< HasSubscriptOperator< Vector >::value >>
DistributedVectoroperator*= (const Vector &vector)
 
template<typename Vector , typename... , typename >
DistributedVector< Real, Device, Index, Allocator > & operator*= (const Vector &vector)
 
template<typename Scalar , typename... , typename = std::enable_if_t< ! HasSubscriptOperator< Scalar >::value >>
DistributedVectoroperator*= (Scalar c)
 
template<typename Scalar , typename... , typename >
DistributedVector< Real, Device, Index, Allocator > & operator*= (Scalar c)
 
template<typename Vector , typename... , typename = std::enable_if_t< HasSubscriptOperator< Vector >::value >>
DistributedVectoroperator+= (const Vector &vector)
 
template<typename Vector , typename... , typename >
DistributedVector< Real, Device, Index, Allocator > & operator+= (const Vector &vector)
 
template<typename Scalar , typename... , typename = std::enable_if_t< ! HasSubscriptOperator< Scalar >::value >>
DistributedVectoroperator+= (Scalar c)
 
template<typename Scalar , typename... , typename >
DistributedVector< Real, Device, Index, Allocator > & operator+= (Scalar c)
 
template<typename Vector , typename... , typename = std::enable_if_t< HasSubscriptOperator< Vector >::value >>
DistributedVectoroperator-= (const Vector &vector)
 
template<typename Vector , typename... , typename >
DistributedVector< Real, Device, Index, Allocator > & operator-= (const Vector &vector)
 
template<typename Scalar , typename... , typename = std::enable_if_t< ! HasSubscriptOperator< Scalar >::value >>
DistributedVectoroperator-= (Scalar c)
 
template<typename Scalar , typename... , typename >
DistributedVector< Real, Device, Index, Allocator > & operator-= (Scalar c)
 
template<typename Vector , typename... , typename = std::enable_if_t< HasSubscriptOperator< Vector >::value >>
DistributedVectoroperator/= (const Vector &vector)
 
template<typename Vector , typename... , typename >
DistributedVector< Real, Device, Index, Allocator > & operator/= (const Vector &vector)
 
template<typename Scalar , typename... , typename = std::enable_if_t< ! HasSubscriptOperator< Scalar >::value >>
DistributedVectoroperator/= (Scalar c)
 
template<typename Scalar , typename... , typename >
DistributedVector< Real, Device, Index, Allocator > & operator/= (Scalar c)
 
template<typename Array , typename... , typename = std::enable_if_t< HasSubscriptOperator< Array >::value >>
DistributedArrayoperator= (const Array &array)
 
DistributedArrayoperator= (const DistributedArray &array)
 
DistributedVectoroperator= (const DistributedVector &)=default
 Copy-assignment operator for copying data from another vector.
 
template<typename Vector , typename... , typename = std::enable_if_t< HasSubscriptOperator< Vector >::value >>
DistributedVectoroperator= (const Vector &vector)
 
template<typename Vector , typename... , typename >
DistributedVector< Real, Device, Index, Allocator > & operator= (const Vector &vector)
 
DistributedArrayoperator= (DistributedArray &&) noexcept(false)=default
 
DistributedVectoroperator= (DistributedVector &&) noexcept(false)=default
 Move-assignment operator for acquiring data from rvalues.
 
template<typename Scalar , typename... , typename = std::enable_if_t< ! HasSubscriptOperator< Scalar >::value >>
DistributedVectoroperator= (Scalar c)
 
template<typename Scalar , typename... , typename >
DistributedVector< Real, Device, Index, Allocator > & 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.
 
 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, int ghosts, int globalSize, const MPI::Comm &communicator, const AllocatorType &allocator=AllocatorType())
 
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).
 
void forElements (IndexType begin, IndexType end, Function &&f) const
 Process the lambda function f for each array element in interval [ begin, end) for constant instances of the array.
 
AllocatorType getAllocator () const
 Returns the allocator associated to the array.
 
const MPI::CommgetCommunicator () 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 LocalRangeTypegetLocalRange () 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)
 
void loadFromGlobalFile (File &file, 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
 
DistributedArrayoperator= (const Array &array)
 
DistributedArray< Real, Devices::Host, int, Allocator > & operator= (const Array &array)
 
DistributedArrayoperator= (const DistributedArray &array)
 
DistributedArrayoperator= (DistributedArray &&) noexcept(false)=default
 
bool operator== (const Array &array) const
 
__cuda_callable__ ValueTypeoperator[] (IndexType i)
 
__cuda_callable__ const ValueTypeoperator[] (IndexType i) const
 
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 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 << std::endl;
const int sum = TNL::sum( v );
if( communicator.rank() == 0 )
std::cout << "Global sum is " << sum << std::endl;
}
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 ..." << std::endl;
distributedVectorExample< TNL::Devices::Host >();
#ifdef __CUDACC__
TNL::MPI::Barrier();
if( TNL::MPI::GetRank() == 0 )
std::cout << "The second test runs on GPU ..." << std::endl;
distributedVectorExample< TNL::Devices::Cuda >();
#endif
}
#define __cuda_callable__
Definition Macros.h:49
DistributedVector extends DistributedArray with algebraic operations.
Definition DistributedVector.h:24
An RAII wrapper for custom MPI communicators.
Definition Comm.h:63
int size() const
Returns the size of the group associated with a communicator.
Definition Comm.h:223
int rank() const
Determines the rank of the calling process in the communicator.
Definition Comm.h:216
T endl(T... args)
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
Definition ScopedInitializer.h:63
Output
Rank 0: rank on node is 0, using GPU id 0 of 2
Environment:
CUDA_VISIBLE_DEVICES=
Rank 2: rank on node is 2, using GPU id 0 of 2
Environment:
CUDA_VISIBLE_DEVICES=
Rank 1: rank on node is 1, using GPU id 1 of 2
Environment:
CUDA_VISIBLE_DEVICES=
Rank 3: rank on node is 3, using GPU id 1 of 2
Environment:
CUDA_VISIBLE_DEVICES=
Rank 3 has subrange TNL::Containers::Subrange<int>( 73, 97 )
Rank 2 has subrange TNL::Containers::Subrange<int>( 49, 73 )
Rank 1 has subrange TNL::Containers::Subrange<int>( 25, 49 )
The first test runs on CPU ...
Rank 0 has subrange TNL::Containers::Subrange<int>( 0, 25 )
Global sum is 4656
The second test runs on GPU ...
Rank 0 has subrange TNL::Containers::Subrange<int>( 0, 25 )
Rank 1 has subrange TNL::Containers::Subrange<int>( 25, 49 )
Rank 2 has subrange TNL::Containers::Subrange<int>( 49, 73 )
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< Value, Device, Index, Allocator >::DistributedArray ( const AllocatorType & allocator)
explicit

Constructs an empty array and sets the provided allocator.

Parameters
allocatorThe 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< Value, Device, Index, Allocator >::DistributedArray ( const DistributedArray & array)
explicit

Copy constructor (makes a deep copy).

Parameters
arrayThe 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< Value, Device, Index, Allocator >::DistributedArray ( const DistributedArray & array,
const AllocatorType & allocator )
explicit

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

Parameters
arrayThe array to be copied.
allocatorThe allocator to be associated with this array.

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