TRIQS/mpi 2.0.0
C++ interface to MPI
Loading...
Searching...
No Matches
environment.hpp
Go to the documentation of this file.
1// Copyright (c) 2024 Simons Foundation
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0.txt
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14//
15// Authors: Thomas Hahn, Alexander Hampel, Olivier Parcollet, Nils Wentzell
16
21
22#pragma once
23
24#include "./utils.hpp"
25
26#include <mpi.h>
27
28#include <cstdlib>
29
30namespace mpi {
31
36
41 [[nodiscard]] inline bool is_initialized() noexcept {
42 int flag = 0;
43 MPI_Initialized(&flag);
44 return flag;
45 }
46
51 [[nodiscard]] inline bool is_finalized() noexcept {
52 int flag = 0;
53 MPI_Finalized(&flag);
54 return flag;
55 }
56
65 static const bool has_env = []() {
66 if (std::getenv("OMPI_COMM_WORLD_RANK") != nullptr or std::getenv("PMI_RANK") != nullptr or std::getenv("PMIX_RANK") != nullptr
67 or std::getenv("CRAY_MPICH_VERSION") != nullptr or std::getenv("FORCE_MPI_INIT") != nullptr)
68 return true;
69 else
70 return false;
71 }();
72
79 struct environment {
91 environment(int argc, char *argv[]) { // NOLINT (C-style array is wanted here)
92 if (has_env && !is_initialized()) check_mpi_call(MPI_Init(&argc, &argv), "MPI_Init");
93 }
94
102 if (has_env && !is_finalized()) MPI_Finalize();
103 }
104 };
105
107
108} // namespace mpi
static const bool has_env
Boolean variable that checks if there is an active MPI runtime environment.
bool is_initialized() noexcept
Check if MPI has been initialized by calling MPI_Initialized.
bool is_finalized() noexcept
Check if MPI has been finalized by calling MPI_Finalized.
void check_mpi_call(int errcode, const std::string &mpi_routine)
Check the success of an MPI call.
Definition utils.hpp:48
~environment()
Destroy the mpi environment object by calling MPI_Finalize.
environment(int argc, char *argv[])
Construct a new mpi environment object by calling MPI_Init.
Provides general utilities related to MPI.