DOLFINx 0.12.0.0
DOLFINx C++
Loading...
Searching...
No Matches
local_range.h
1
2// Copyright (C) 2007-2023 Magnus Vikstrøm, Garth N. Wells and Paul T. Kühner
3//
4// This file is part of DOLFINx (https://www.fenicsproject.org)
5//
6// SPDX-License-Identifier: LGPL-3.0-or-later
7
8#pragma once
9
10#include <array>
11#include <cstdint>
12#include <stdexcept>
13
14namespace dolfinx::common
15{
26constexpr std::array<std::int64_t, 2> local_range(int index, std::int64_t N,
27 int size)
28{
29 if (index < 0)
30 throw std::invalid_argument("local_range: index must be non-negative");
31 if (N < 0)
32 throw std::invalid_argument("local_range: N must be non-negative");
33 if (size <= 0)
34 throw std::invalid_argument("local_range: size must be positive");
35
36 // Compute number of items per rank and remainder
37 const std::int64_t n = N / size;
38 const std::int64_t r = N % size;
39
40 // Compute local range
41 if (index < r)
42 return {index * (n + 1), index * (n + 1) + n + 1};
43 else
44 return {index * n + r, index * n + r + n};
45}
46} // namespace dolfinx::common
Miscellaneous classes, functions and types.
Definition dolfinx_common.h:8
constexpr std::array< std::int64_t, 2 > local_range(int index, std::int64_t N, int size)
Partition a global range [0, N - 1] across callers into non-overlapping sub-partitions of almost equa...
Definition local_range.h:26