NeoN
A framework for CFD software
Loading...
Searching...
No Matches
environment.hpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2023 - 2026 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 <cstdlib>
12
13#include "NeoN/core/error.hpp"
14#include "NeoN/core/info.hpp"
15
16
17namespace NeoN
18{
19
20#ifdef NF_WITH_MPI_SUPPORT
21
22namespace mpi
23{
24
29struct Init
30{
37 Init(int argc, char** argv)
38 {
39#ifdef NF_REQUIRE_MPI_THREAD_SUPPORT
40 int provided;
41 MPI_Init_thread(&argc, &argv, MPI_THREAD_MULTIPLE, &provided);
43 provided == MPI_THREAD_MULTIPLE, "The MPI library does not have full thread support"
44 );
45#else
46 MPI_Init(&argc, &argv);
47#endif
48 }
49
53 ~Init() { MPI_Finalize(); }
54};
55
56
61class Environment
62{
63public:
64
70 Environment(MPI_Comm commGroup = MPI_COMM_WORLD) : communicator(commGroup)
71 {
72 MPI_Initialized(&mpiInitialized);
73 updateRankData();
74 if (std::getenv("NEON_FORCE_HOST_BUFFER") != nullptr) gpuAwareMpi_ = false;
75 }
76
80 ~Environment() = default;
81
87 bool isInitialized() const { return mpiInitialized == 1; }
88
94 size_t sizeRank() const { return static_cast<size_t>(mpiSize); }
95
101 size_t rank() const { return static_cast<size_t>(mpiRank); }
102
108 MPI_Comm comm() const { return communicator; }
109
116 bool gpuAwareMpi() const { return gpuAwareMpi_; }
117
121 bool& gpuAwareMpi() { return gpuAwareMpi_; }
122
123private:
124
125 MPI_Comm communicator {MPI_COMM_NULL}; // MPI communicator
126 int mpiInitialized {0};
127 int mpiRank {-1}; // Index of this rank
128 int mpiSize {-1}; // Number of ranks in this communicator group.
129 bool gpuAwareMpi_ {true};
130
134 void updateRankData()
135 {
136 if (mpiInitialized)
137 {
138 MPI_Comm_rank(communicator, &mpiRank);
139 MPI_Comm_size(communicator, &mpiSize);
140 }
141 }
142};
143
144} // namespace mpi
145
146#endif
147
148} // 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:118
Integer types used throughout NeoN.
Definition array.hpp:18