Skip to content

Commit 7d6f6fc

Browse files
Thoemi09Wentzell
authored andcommitted
Use universal references in generic communciations
1 parent 42d76f7 commit 7d6f6fc

File tree

2 files changed

+14
-9
lines changed

2 files changed

+14
-9
lines changed

c++/mpi/generic_communication.hpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,9 @@ namespace mpi {
7373
* @param c mpi::communicator.
7474
* @param root Rank of the root process.
7575
*/
76-
template <typename T> [[gnu::always_inline]] void broadcast(T &x, communicator c = {}, int root = 0) {
76+
template <typename T> [[gnu::always_inline]] void broadcast(T &&x, communicator c = {}, int root = 0) {
7777
static_assert(not std::is_const_v<T>, "mpi::broadcast cannot be called on const objects");
78-
if (has_env) mpi_broadcast(x, c, root);
78+
if (has_env) mpi_broadcast(std::forward<T>(x), c, root);
7979
}
8080

8181
/**
@@ -121,9 +121,9 @@ namespace mpi {
121121
* @param op `MPI_Op` used in the reduction.
122122
*/
123123
template <typename T>
124-
[[gnu::always_inline]] inline void reduce_in_place(T &x, communicator c = {}, int root = 0, bool all = false, MPI_Op op = MPI_SUM) {
124+
[[gnu::always_inline]] inline void reduce_in_place(T &&x, communicator c = {}, int root = 0, bool all = false, MPI_Op op = MPI_SUM) {
125125
static_assert(not std::is_const_v<T>, "In-place mpi functions cannot be called on const objects");
126-
if (has_env) mpi_reduce_in_place(x, c, root, all, op);
126+
if (has_env) mpi_reduce_in_place(std::forward<T>(x), c, root, all, op);
127127
}
128128

129129
/**

c++/mpi/ranges.hpp

+10-5
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ namespace mpi {
9090
* @param c mpi::communicator.
9191
* @param root Rank of the root process.
9292
*/
93-
template <contiguous_sized_range R> void broadcast_range(R &&rg, communicator c = {}, int root = 0) {
93+
template <contiguous_sized_range R> void broadcast_range(R &&rg, communicator c = {}, int root = 0) { // NOLINT (ranges need not be forwarded)
9494
// check the sizes of all ranges
9595
using value_t = std::ranges::range_value_t<R>;
9696
auto const size = std::ranges::size(rg);
@@ -151,7 +151,9 @@ namespace mpi {
151151
* @param all Should all processes receive the result of the reduction.
152152
* @param op `MPI_Op` used in the reduction.
153153
*/
154-
template <contiguous_sized_range R> void reduce_in_place_range(R &&rg, communicator c = {}, int root = 0, bool all = false, MPI_Op op = MPI_SUM) {
154+
template <contiguous_sized_range R>
155+
void reduce_in_place_range(R &&rg, communicator c = {}, int root = 0, bool all = false, // NOLINT (ranges need not be forwarded)
156+
MPI_Op op = MPI_SUM) {
155157
// check the sizes of all ranges
156158
using value_t = std::ranges::range_value_t<R>;
157159
auto const size = std::ranges::size(rg);
@@ -221,7 +223,8 @@ namespace mpi {
221223
* @param op `MPI_Op` used in the reduction.
222224
*/
223225
template <contiguous_sized_range R1, contiguous_sized_range R2>
224-
void reduce_range(R1 &&in_rg, R2 &&out_rg, communicator c = {}, int root = 0, bool all = false, MPI_Op op = MPI_SUM) {
226+
void reduce_range(R1 &&in_rg, R2 &&out_rg, communicator c = {}, int root = 0, bool all = false, // NOLINT (ranges need not be forwarded)
227+
MPI_Op op = MPI_SUM) {
225228
// check input and output ranges
226229
auto const in_size = std::ranges::size(in_rg);
227230
EXPECTS_WITH_MESSAGE(all_equal(in_size, c), "Input range sizes are not equal across all processes in mpi::reduce_range");
@@ -311,7 +314,8 @@ namespace mpi {
311314
*/
312315
template <contiguous_sized_range R1, contiguous_sized_range R2>
313316
requires(std::same_as<std::ranges::range_value_t<R1>, std::ranges::range_value_t<R2>>)
314-
void scatter_range(R1 &&in_rg, R2 &&out_rg, long in_size, communicator c = {}, int root = 0, long chunk_size = 1) {
317+
void scatter_range(R1 &&in_rg, R2 &&out_rg, long in_size, communicator c = {}, int root = 0, // NOLINT (ranges need not be forwarded)
318+
long chunk_size = 1) {
315319
// check the sizes of the input and output ranges
316320
if (c.rank() == root) {
317321
EXPECTS_WITH_MESSAGE(in_size == std::ranges::size(in_rg), "Input range size not equal to provided size in mpi::scatter_range");
@@ -405,7 +409,8 @@ namespace mpi {
405409
* @param all Should all processes receive the result of the reduction.
406410
*/
407411
template <contiguous_sized_range R1, contiguous_sized_range R2>
408-
void gather_range(R1 &&in_rg, R2 &&out_rg, long out_size, communicator c = {}, int root = 0, bool all = false) {
412+
void gather_range(R1 &&in_rg, R2 &&out_rg, long out_size, communicator c = {}, int root = 0, // NOLINT (ranges need not be forwarded)
413+
bool all = false) {
409414
// check the sizes of the input and output ranges
410415
auto const in_size = std::ranges::size(in_rg);
411416
EXPECTS_WITH_MESSAGE(out_size = all_reduce(in_size, c), "Input range sizes don't add up to output range size in mpi::gather_range");

0 commit comments

Comments
 (0)