TRIQS/mpi 1.3.0
C++ interface to MPI
Loading...
Searching...
No Matches
vector.hpp
Go to the documentation of this file.
1// Copyright (c) 2019-2024 Simons Foundation
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0.txt
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14//
15// Authors: Thomas Hahn, Olivier Parcollet, Nils Wentzell
16
22#pragma once
23
24#include "./communicator.hpp"
26#include "./ranges.hpp"
27#include "./utils.hpp"
28
29#include <mpi.h>
30
31#include <vector>
32
33namespace mpi {
34
51 template <typename T> void mpi_broadcast(std::vector<T> &v, communicator c = {}, int root = 0) {
52 auto bsize = v.size();
53 broadcast(bsize, c, root);
54 if (c.rank() != root) v.resize(bsize);
55 broadcast_range(v, c, root);
56 }
57
70 template <typename T> void mpi_reduce_in_place(std::vector<T> &v, communicator c = {}, int root = 0, bool all = false, MPI_Op op = MPI_SUM) {
71 reduce_in_place_range(v, c, root, all, op);
72 }
73
87 template <typename T>
88 auto mpi_reduce(std::vector<T> const &v, communicator c = {}, int root = 0, bool all = false, MPI_Op op = MPI_SUM) {
89 std::vector<regular_t<T>> res(c.rank() == root || all ? v.size() : 0);
90 reduce_range(v, res, c, root, all, op);
91 return res;
92 }
93
106 template <typename T> auto mpi_scatter(std::vector<T> const &v, communicator c = {}, int root = 0) {
107 auto bsize = v.size();
108 broadcast(bsize, c, root);
109 std::vector<T> res(chunk_length(bsize, c.size(), c.rank()));
110 scatter_range(v, res, bsize, c, root);
111 return res;
112 }
113
126 template <typename T> auto mpi_gather(std::vector<T> const &v, communicator c = {}, int root = 0, bool all = false) {
127 long bsize = mpi::all_reduce(v.size(), c);
128 std::vector<T> res(c.rank() == root || all ? bsize : 0);
129 gather_range(v, res, bsize, c, root, all);
130 return res;
131 }
132
135} // namespace mpi
C++ wrapper around MPI_Comm providing various convenience functions.
Provides a C++ wrapper class for an MPI_Comm object.
Provides generic implementations for a subset of collective MPI communications (broadcast,...
void reduce_in_place_range(R &&rg, communicator c={}, int root=0, bool all=false, MPI_Op op=MPI_SUM)
Implementation of an in-place MPI reduce for an mpi::contiguous_sized_range object.
Definition ranges.hpp:155
auto mpi_reduce(std::array< T, N > const &arr, communicator c={}, int root=0, bool all=false, MPI_Op op=MPI_SUM)
Implementation of an MPI reduce for a std::array.
Definition array.hpp:86
void reduce_range(R1 &&in_rg, R2 &&out_rg, communicator c={}, int root=0, bool all=false, MPI_Op op=MPI_SUM)
Implementation of an MPI reduce for an mpi::contiguous_sized_range.
Definition ranges.hpp:226
void scatter_range(R1 &&in_rg, R2 &&out_rg, long in_size, communicator c={}, int root=0, long chunk_size=1)
Implementation of an MPI scatter for an mpi::contiguous_sized_range.
Definition ranges.hpp:317
void broadcast_range(R &&rg, communicator c={}, int root=0)
Implementation of an MPI broadcast for an mpi::contiguous_sized_range object.
Definition ranges.hpp:93
void gather_range(R1 &&in_rg, R2 &&out_rg, long out_size, communicator c={}, int root=0, bool all=false)
Implementation of an MPI gather for an mpi::contiguous_sized_range.
Definition ranges.hpp:412
auto mpi_scatter(std::vector< T > const &v, communicator c={}, int root=0)
Implementation of an MPI scatter for a std::vector.
Definition vector.hpp:106
void mpi_broadcast(std::array< T, N > &arr, communicator c={}, int root=0)
Implementation of an MPI broadcast for a std::arr.
Definition array.hpp:51
decltype(auto) all_reduce(T &&x, communicator c={}, MPI_Op op=MPI_SUM)
Generic MPI all-reduce.
void broadcast(T &&x, communicator c={}, int root=0)
Generic MPI broadcast.
std::string mpi_gather(std::string const &s, communicator c={}, int root=0, bool all=false)
Implementation of an MPI gather for a std::string.
Definition string.hpp:65
void mpi_reduce_in_place(std::array< T, N > &arr, communicator c={}, int root=0, bool all=false, MPI_Op op=MPI_SUM)
Implementation of an in-place MPI reduce for a std::array.
Definition array.hpp:67
long chunk_length(long end, int nranges, int i, long min_size=1)
Get the length of the ith subrange after splitting the integer range [0, end) as evenly as possible a...
Definition chunk.hpp:50
Provides an MPI broadcast, reduce, scatter and gather for contiguous ranges.
Provides general utilities related to MPI.