Line | Branch | Exec | Source |
---|---|---|---|
1 | // SPDX-FileCopyrightText: 2022-2023 Dennis Gläser <dennis.glaeser@iws.uni-stuttgart.de> | ||
2 | // SPDX-License-Identifier: MIT | ||
3 | /*! | ||
4 | * \file | ||
5 | * \ingroup Parallel | ||
6 | * \brief Helper functions for parallel computations. | ||
7 | */ | ||
8 | #ifndef GRIDFORMAT_PARALLEL_HELPERS_HPP_ | ||
9 | #define GRIDFORMAT_PARALLEL_HELPERS_HPP_ | ||
10 | |||
11 | #include <array> | ||
12 | #include <ranges> | ||
13 | |||
14 | #include <gridformat/parallel/concepts.hpp> | ||
15 | #include <gridformat/parallel/communication.hpp> | ||
16 | |||
17 | namespace GridFormat::Parallel { | ||
18 | |||
19 | //! \addtogroup Parallel | ||
20 | //! \{ | ||
21 | |||
22 | struct Index { | ||
23 | std::size_t i; | ||
24 | int rank; | ||
25 | }; | ||
26 | |||
27 | //! Access an entry in a vector containing N elements per process for the given rank and index | ||
28 | template<std::size_t N, Concepts::Communicator C, std::ranges::contiguous_range R> | ||
29 | 45534 | decltype(auto) access_gathered(R&& values, const C& comm, const Index& index) { | |
30 |
1/2✗ Branch 2 not taken.
✓ Branch 3 taken 22984 times.
|
45534 | if (std::ranges::size(values) != N*size(comm)) |
31 | ✗ | throw SizeError("Range size does not match number of processors times N"); | |
32 | 45534 | return std::ranges::data(std::forward<R>(values))[index.rank*N + index.i]; | |
33 | } | ||
34 | |||
35 | //! Get all entries from a vector containing N elements per process for the given rank | ||
36 | template<std::size_t N, Concepts::Communicator C, std::ranges::contiguous_range R> | ||
37 | 12588 | auto access_gathered(R&& values, const C& comm, int rank) { | |
38 |
3/4✓ Branch 2 taken 1422 times.
✓ Branch 3 taken 4958 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 1422 times.
|
12588 | if (std::ranges::size(values) != N*size(comm)) |
39 | ✗ | throw SizeError("Range size does not match number of processors times N"); | |
40 | std::array<std::ranges::range_value_t<R>, N> result; | ||
41 |
2/2✓ Branch 0 taken 17718 times.
✓ Branch 1 taken 6380 times.
|
47680 | for (std::size_t i = 0; i < N; ++i) |
42 | 35092 | result[i] = std::ranges::data(std::forward<R>(values))[rank*N + i]; | |
43 | 12588 | return result; | |
44 | } | ||
45 | |||
46 | //! Return a range over all ranks of the given communicator | ||
47 | template<Concepts::Communicator C> | ||
48 | 4272 | std::ranges::view auto ranks(const C& comm) { | |
49 |
2/5✓ Branch 1 taken 4272 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 4272 times.
✗ Branch 5 not taken.
|
4272 | return std::views::iota(0, size(comm)); |
50 | } | ||
51 | |||
52 | //! \} group Parallel | ||
53 | |||
54 | } // namespace GridFormat::Parallel | ||
55 | |||
56 | #endif // GRIDFORMAT_PARALLEL_HELPERS_HPP_ | ||
57 |