NeoFOAM
WIP Prototype of a modern OpenFOAM core
Loading...
Searching...
No Matches
environment.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
2// SPDX-FileCopyrightText: 2023 NeoFOAM authors
3#pragma once
4
5#ifdef NF_WITH_MPI_SUPPORT
6#include <mpi.h>
7#endif
8
10#include "NeoFOAM/core/info.hpp"
11
12
13namespace NeoFOAM
14{
15
16#ifdef NF_WITH_MPI_SUPPORT
17
18namespace mpi
19{
20
25struct MPIInit
26{
33 MPIInit(int argc, char** argv)
34 {
35#ifdef NF_REQUIRE_MPI_THREAD_SUPPORT
36 int provided;
37 MPI_Init_thread(&argc, &argv, MPI_THREAD_MULTIPLE, &provided);
39 provided == MPI_THREAD_MULTIPLE, "The MPI library does not have full thread support"
40 );
41#else
42 MPI_Init(&argc, &argv);
43#endif
44 }
45
49 ~MPIInit() { MPI_Finalize(); }
50};
51
52
57class MPIEnvironment
58{
59public:
60
66 MPIEnvironment(MPI_Comm commGroup = MPI_COMM_WORLD) : communicator(commGroup)
67 {
68 updateRankData();
69 }
70
74 ~MPIEnvironment() = default;
75
81 size_t sizeRank() const { return static_cast<size_t>(mpi_size); }
82
88 size_t rank() const { return static_cast<size_t>(mpi_rank); }
89
95 MPI_Comm comm() const { return communicator; }
96
97private:
98
99 MPI_Comm communicator {MPI_COMM_NULL}; // MPI communicator
100 int mpi_rank {-1}; // Index of this rank
101 int mpi_size {-1}; // Number of ranks in this communicator group.
102
106 void updateRankData()
107 {
108 NF_ASSERT(communicator != MPI_COMM_NULL, "Invalid communicator, is null.");
109 MPI_Comm_rank(communicator, &mpi_rank);
110 MPI_Comm_size(communicator, &mpi_size);
111 }
112};
113
114} // namespace mpi
115
116#endif
117
118} // namespace NeoFOAM
#define NF_ASSERT(condition, message)
Macro for asserting a condition and printing an error message if the condition is false.
Definition error.hpp:142