TRIQS/mpi 1.3.0
C++ interface to MPI
Loading...
Searching...
No Matches
chunk.hpp
Go to the documentation of this file.
1// Copyright (c) 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, Alexander Hampel, Olivier Parcollet, Nils Wentzell
16
22#pragma once
23
24#include "./communicator.hpp"
25
26#include <itertools/itertools.hpp>
27
28#include <iterator>
29#include <stdexcept>
30#include <utility>
31
32namespace mpi {
33
50 [[nodiscard]] inline long chunk_length(long end, int nranges, int i, long min_size = 1) {
51 if (min_size < 1 || end % min_size != 0) throw std::runtime_error("Error in mpi::chunk_length: min_size must be a divisor of end");
52 auto [node_begin, node_end] = itertools::chunk_range(0, end / min_size, nranges, i);
53 return (node_end - node_begin) * min_size;
54 }
55
66 template <typename R> [[nodiscard]] auto chunk(R &&rg, communicator c = {}) {
67 auto total_size = itertools::distance(std::cbegin(rg), std::cend(rg));
68 auto [start_idx, end_idx] = itertools::chunk_range(0, total_size, c.size(), c.rank());
69 return itertools::slice(std::forward<R>(rg), start_idx, end_idx);
70 }
71
72} // namespace mpi
C++ wrapper around MPI_Comm providing various convenience functions.
Provides a C++ wrapper class for an MPI_Comm object.
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
auto chunk(R &&rg, communicator c={})
Divide a given range as evenly as possible across the MPI processes in a communicator and get the sub...
Definition chunk.hpp:66