TRIQS/nda 1.3.0
Multi-dimensional array library for C++
Loading...
Searching...
No Matches
MPI support

Detailed Description

MPI support for nda::basic_array and nda::basic_array_view objects.

nda uses the TRIQS/mpi library to provide functions to broadcast, gather, reduce and scatter arrays and views over MPI processes.

The following example demonstrates some of these features:

#include <nda/mpi.hpp>
#include <nda/nda.hpp>
#include <iostream>
#include <mpi/mpi.hpp>
int main(int argc, char **argv) {
// initialize MPI environment
mpi::environment env(argc, argv);
mpi::communicator comm;
// create a 2x2 array on each process and fill it with its rank
A() = comm.rank();
// reduce the array over all processes, which returns an mpi::lazy proxy object
auto lazy_sum = mpi::reduce(A);
// since it satisfies the nda::ArrayInitializer concept, we can use it to initialize an nda::array
nda::array<int, 2> sum(lazy_sum);
// print the result
if (comm.rank() == 0) std::cout << sum << std::endl;
}
A generic multi-dimensional array.
Includes all MPI relevant headers.
Includes all relevant headers for the core nda library.

Running with 4 cores outputs:

[[6,6]
[6,6]]

Classes

struct  mpi::lazy< mpi::tag::gather, A >
 Specialization of the mpi::lazy class for nda::Array types and the mpi::tag::gather tag. More...
 
struct  mpi::lazy< mpi::tag::reduce, A >
 Specialization of the mpi::lazy class for nda::Array types and the mpi::tag::reduce tag. More...
 
struct  mpi::lazy< mpi::tag::scatter, A >
 Specialization of the mpi::lazy class for nda::Array types and the mpi::tag::scatter tag. More...
 

Functions

template<typename A >
requires (is_regular_or_view_v<A>)
void nda::mpi_broadcast (A &a, mpi::communicator comm={}, int root=0)
 Implementation of an MPI broadcast for nda::basic_array or nda::basic_array_view types.
 
template<typename A >
requires (is_regular_or_view_v<A>)
ArrayInitializer< std::remove_reference_t< A > > auto nda::mpi_gather (A &&a, mpi::communicator comm={}, int root=0, bool all=false)
 Implementation of an MPI gather for nda::basic_array or nda::basic_array_view types.
 
template<typename A >
requires (is_regular_or_view_v<A>)
ArrayInitializer< std::remove_reference_t< A > > auto nda::mpi_reduce (A &&a, mpi::communicator comm={}, int root=0, bool all=false, MPI_Op op=MPI_SUM)
 Implementation of an MPI reduce for nda::basic_array or nda::basic_array_view types.
 
template<typename A >
requires (is_regular_or_view_v<A>)
ArrayInitializer< std::remove_reference_t< A > > auto nda::mpi_scatter (A &&a, mpi::communicator comm={}, int root=0, bool all=false)
 Implementation of an MPI scatter for nda::basic_array or nda::basic_array_view types.
 

Function Documentation

◆ mpi_broadcast()

template<typename A >
requires (is_regular_or_view_v<A>)
void nda::mpi_broadcast ( A & a,
mpi::communicator comm = {},
int root = 0 )

#include <nda/mpi/broadcast.hpp>

Implementation of an MPI broadcast for nda::basic_array or nda::basic_array_view types.

For the root process, the array/view is broadcasted to all other processes. For non-root processes, the array/view is resized/checked to match the broadcasted dimensions and the data is written into the given array/view.

Throws an exception, if a given view does not have the correct shape.

// create an array on all processes
// ...
// fill array on root process
// ...
// broadcast the array to all processes
mpi::broadcast(arr);
Template Parameters
Anda::basic_array or nda::basic_array_view type.
Parameters
aArray or view to be broadcasted from/into.
commmpi::communicator object.
rootRank of the root process.

Definition at line 61 of file broadcast.hpp.

◆ mpi_gather()

template<typename A >
requires (is_regular_or_view_v<A>)
ArrayInitializer< std::remove_reference_t< A > > auto nda::mpi_gather ( A && a,
mpi::communicator comm = {},
int root = 0,
bool all = false )

#include <nda/mpi/gather.hpp>

Implementation of an MPI gather for nda::basic_array or nda::basic_array_view types.

Since the returned mpi::lazy object models an nda::ArrayInitializer, it can be used to initialize/assign to nda::basic_array and nda::basic_array_view objects:

// create an array on all processes
// ...
// fill array on each process
// ...
// gather the array to the root process
nda::array<int, 2> res = mpi::gather(arr);

Here, the array res will have a shape of (3 * comm.size(), 4).

Template Parameters
Anda::basic_array or nda::basic_array_view type.
Parameters
aArray or view to be gathered.
commmpi::communicator object.
rootRank of the root process.
allShould all processes receive the result of the gather.
Returns
An mpi::lazy object modelling an nda::ArrayInitializer.

Definition at line 166 of file gather.hpp.

◆ mpi_reduce()

template<typename A >
requires (is_regular_or_view_v<A>)
ArrayInitializer< std::remove_reference_t< A > > auto nda::mpi_reduce ( A && a,
mpi::communicator comm = {},
int root = 0,
bool all = false,
MPI_Op op = MPI_SUM )

#include <nda/mpi/reduce.hpp>

Implementation of an MPI reduce for nda::basic_array or nda::basic_array_view types.

Since the returned mpi::lazy object models an nda::ArrayInitializer, it can be used to initialize/assign to nda::basic_array and nda::basic_array_view objects:

// create an array on all processes
// ...
// fill array on each process
// ...
// reduce the array to the root process
nda::array<int, 2> res = mpi::reduce(arr);
Template Parameters
Anda::basic_array or nda::basic_array_view type.
Parameters
aArray or view to be gathered.
commmpi::communicator object.
rootRank of the root process.
allShould all processes receive the result of the gather.
opMPI reduction operation.
Returns
An mpi::lazy object modelling an nda::ArrayInitializer.

Definition at line 165 of file reduce.hpp.

◆ mpi_scatter()

template<typename A >
requires (is_regular_or_view_v<A>)
ArrayInitializer< std::remove_reference_t< A > > auto nda::mpi_scatter ( A && a,
mpi::communicator comm = {},
int root = 0,
bool all = false )

#include <nda/mpi/scatter.hpp>

Implementation of an MPI scatter for nda::basic_array or nda::basic_array_view types.

Since the returned mpi::lazy object models an nda::ArrayInitializer, it can be used to initialize/assign to nda::basic_array and nda::basic_array_view objects:

// create an array on all processes
nda::array<int, 2> arr(10, 4);
// ...
// fill array on root process
// ...
// scatter the array to all processes
nda::array<int, 2> res = mpi::scatter(arr);

Here, the array res will have a shape of (10 / comm.size(), 4).

Template Parameters
Anda::basic_array or nda::basic_array_view type.
Parameters
aArray or view to be scattered.
commmpi::communicator object.
rootRank of the root process.
allShould all processes receive the result of the scatter (not used).
Returns
An mpi::lazy object modelling an nda::ArrayInitializer.

Definition at line 156 of file scatter.hpp.