TRIQS/mpi 1.3.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
22#pragma once
23
24#include "./utils.hpp"
25
26#include <mpi.h>
27
28#include <cstdlib>
29
30namespace mpi {
31
42 [[nodiscard]] inline bool is_initialized() noexcept {
43 int flag = 0;
44 check_mpi_call(MPI_Initialized(&flag), "MPI_Initialized");
45 return flag;
46 }
47
54 static const bool has_env = []() {
55 if (std::getenv("OMPI_COMM_WORLD_RANK") != nullptr or std::getenv("PMI_RANK") != nullptr or std::getenv("CRAY_MPICH_VERSION") != nullptr
56 or std::getenv("FORCE_MPI_INIT") != nullptr)
57 return true;
58 else
59 return false;
60 }();
61
70 struct environment {
80 environment(int argc, char *argv[]) { // NOLINT (C-style array is wanted here)
81 if (has_env && !is_initialized()) check_mpi_call(MPI_Init(&argc, &argv), "MPI_Init");
82 }
83
91 if (has_env) check_mpi_call(MPI_Finalize(), "MPI_Finalize");
92 }
93 };
94
97} // namespace mpi
static const bool has_env
Boolean variable that is true, if one of the environment variables OMPI_COMM_WORLD_RANK,...
bool is_initialized() noexcept
Check if MPI has been initialized.
void check_mpi_call(int errcode, const std::string &mpi_routine)
Check the success of an MPI call.
Definition utils.hpp:72
RAII class to initialize and finalize MPI.
~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.