OGS
mpi/partition.cpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: Copyright (c) OpenGeoSys Community (opengeosys.org)
2// SPDX-License-Identifier: BSD-3-Clause
3
4#include "../partition.h"
5
6#include <mpi.h>
7
8#include <numeric>
9
10#include "BaseLib/Algorithm.h"
11#include "BaseLib/Logging.h"
12#include "BaseLib/MPI.h"
14#include "getCommunicator.h"
15
16namespace MeshLib::IO
17{
19{
20 int mpi_rank;
21 MPI_Comm_rank(BaseLib::MPI::OGS_COMM_WORLD, &mpi_rank);
22 return mpi_rank == 0;
23}
24
25PartitionInfo getPartitionInfo(std::size_t const size,
26 unsigned int const n_files)
27{
29
30 std::vector<std::size_t> const partition_sizes =
31 BaseLib::MPI::allgather(size, mpi);
32
33 // the first partition's offset is zero, offsets for subsequent
34 // partitions are the accumulated sum of all preceding size.
35 std::vector<std::size_t> const partition_offsets =
36 BaseLib::sizesToOffsets(partition_sizes);
37
38 // chunked
39 std::size_t longest_partition =
40 *max_element(partition_sizes.begin(), partition_sizes.end());
41
42 // local_offset, local_length, longest_local_length, global_length
43 return {partition_offsets[mpi.rank], size, longest_partition,
44 partition_offsets.back()};
45}
46} // namespace MeshLib::IO
MPI_Comm OGS_COMM_WORLD
Definition MPI.cpp:9
static std::vector< T > allgather(T const &value, Mpi const &mpi)
Definition MPI.h:98
std::vector< ranges::range_value_t< R > > sizesToOffsets(R const &sizes)
Definition Algorithm.h:276
FileCommunicator getCommunicator(unsigned int const n_files)
PartitionInfo getPartitionInfo(std::size_t const size, unsigned int const n_files)
bool isFileManager()