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"
16
17
18namespace NeoN
19{
20
21#ifdef NF_WITH_MPI_SUPPORT
22
23namespace mpi
24{
25
30struct Init
31{
38 Init(int argc, char** argv)
39 {
40#ifdef NF_REQUIRE_MPI_THREAD_SUPPORT
41 int provided;
42 MPI_Init_thread(&argc, &argv, MPI_THREAD_MULTIPLE, &provided);
44 provided == MPI_THREAD_MULTIPLE, "The MPI library does not have full thread support"
45 );
46#else
47 MPI_Init(&argc, &argv);
48#endif
49 }
50
54 ~Init() { MPI_Finalize(); }
55};
56
57
62class Environment
63{
64public:
65
71 Environment(MPI_Comm commGroup = MPI_COMM_WORLD) : communicator(commGroup)
72 {
73 MPI_Initialized(&mpiInitialized);
74 updateRankData();
75 if (std::getenv("NEON_FORCE_HOST_BUFFER") != nullptr) gpuAwareMpi_ = false;
76 }
77
81 ~Environment() = default;
82
88 bool isInitialized() const { return mpiInitialized == 1; }
89
95 sizeType sizeRank() const { return static_cast<sizeType>(mpiSize); }
96
102 sizeType rank() const { return static_cast<sizeType>(mpiRank); }
103
109 MPI_Comm comm() const { return communicator; }
110
117 bool gpuAwareMpi() const { return gpuAwareMpi_; }
118
122 bool& gpuAwareMpi() { return gpuAwareMpi_; }
123
124private:
125
126 MPI_Comm communicator {MPI_COMM_NULL}; // MPI communicator
127 int mpiInitialized {0};
128 int mpiRank {-1}; // Index of this rank
129 int mpiSize {-1}; // Number of ranks in this communicator group.
130 bool gpuAwareMpi_ {true};
131
135 void updateRankData()
136 {
137 if (mpiInitialized)
138 {
139 MPI_Comm_rank(communicator, &mpiRank);
140 MPI_Comm_size(communicator, &mpiSize);
141 }
142 }
143};
144
145} // namespace mpi
146
147#endif
148
149} // 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
std::size_t sizeType
Definition label.hpp:56