NeoN
A framework for CFD software
Loading...
Searching...
No Matches
environment.hpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2023 - 2025 NeoN authors
2//
3// SPDX-License-Identifier: MIT
4
5#pragma once
6
7#ifdef NF_WITH_MPI_SUPPORT
8#include <mpi.h>
9#endif
10
11#include "NeoN/core/error.hpp"
12#include "NeoN/core/info.hpp"
13
14
15namespace NeoN
16{
17
18#ifdef NF_WITH_MPI_SUPPORT
19
20namespace mpi
21{
22
27struct MPIInit
28{
35 MPIInit(int argc, char** argv)
36 {
37#ifdef NF_REQUIRE_MPI_THREAD_SUPPORT
38 int provided;
39 MPI_Init_thread(&argc, &argv, MPI_THREAD_MULTIPLE, &provided);
41 provided == MPI_THREAD_MULTIPLE, "The MPI library does not have full thread support"
42 );
43#else
44 MPI_Init(&argc, &argv);
45#endif
46 }
47
51 ~MPIInit() { MPI_Finalize(); }
52};
53
54
59class MPIEnvironment
60{
61public:
62
68 MPIEnvironment(MPI_Comm commGroup = MPI_COMM_WORLD) : communicator(commGroup)
69 {
70 updateRankData();
71 }
72
76 ~MPIEnvironment() = default;
77
83 size_t sizeRank() const { return static_cast<size_t>(mpi_size); }
84
90 size_t rank() const { return static_cast<size_t>(mpi_rank); }
91
97 MPI_Comm comm() const { return communicator; }
98
99private:
100
101 MPI_Comm communicator {MPI_COMM_NULL}; // MPI communicator
102 int mpi_rank {-1}; // Index of this rank
103 int mpi_size {-1}; // Number of ranks in this communicator group.
104
108 void updateRankData()
109 {
110 NF_ASSERT(communicator != MPI_COMM_NULL, "Invalid communicator, is null.");
111 MPI_Comm_rank(communicator, &mpi_rank);
112 MPI_Comm_size(communicator, &mpi_size);
113 }
114};
115
116} // namespace mpi
117
118#endif
119
120} // namespace NeoN
#define NF_ASSERT(condition, message)
Macro for asserting a condition and printing an error message if the condition is false.
Definition error.hpp:144
Definition array.hpp:20