Debian Codegraph @

src/atlas/parallel/mpi/mpi.h

raw
1 /*
2 * (C) Copyright 2013 ECMWF.
3 *
4 * This software is licensed under the terms of the Apache Licence Version 2.0
5 * which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
6 * In applying this licence, ECMWF does not waive the privileges and immunities
7 * granted to it by virtue of its status as an intergovernmental organisation
8 * nor does it submit to any jurisdiction.
9 */
10
11 #pragma once
12
13 #include <string_view>
14
15 #include "eckit/mpi/Comm.h"
16 #include "atlas/parallel/mpi/Statistics.h"
17
18 namespace atlas::mpi {
19
20 using Comm = eckit::mpi::Comm;
21
22 inline const Comm& comm() {
23 return eckit::mpi::comm();
24 }
25
26 inline const Comm& comm(std::string_view name) {
27 return eckit::mpi::comm(name.data());
28 }
29
30 const Comm& comm(int communicator);
31
32 bool has_comm(std::string_view name);
33
34 const Comm& register_comm(std::string_view name, int communicator);
35 void unregister_comm(std::string_view name);
36
37 inline idx_t rank() {
38 return static_cast<idx_t>(comm().rank());
39 }
40
41 inline int size() {
42 return static_cast<idx_t>(comm().size());
43 }
44
45 void finalize();
46 void finalise();
47
48 namespace scope {
49 /// @brief Push a new scope for MPI communicators. The current default communicator is restored when the scope is destructed using pop()
50 void push();
51 /// @brief Push a new scope for MPI communicators. The given communicator is set as the default communicator for the duration of the scope. The previous default communicator is restored when the scope is destructed using pop()
52 void push(std::string_view name);
53 /// @brief Push a new scope for MPI communicators. The given communicator is set as the default communicator for the duration of the scope. The previous default communicator is restored when the scope is destructed using pop()
54 void push(const Comm& comm);
55 /// @brief Push a new scope for MPI communicators. The given communicator is set as the default communicator for the duration of the scope. The previous default communicator is restored when the scope is destructed using pop()
56 /// If the given communicator is not already registered, it will be registered with a generated name "int.<communicator>" for the duration of the scope, and unregistered when the scope is destructed.
57 void push(int communicator);
58 /// @brief Pop the current MPI communicator scope, restoring the previous default communicator
59 void pop();
60 }
61
62 /// @brief RAII helper class to manage MPI communicator scopes. The constructor pushes a new scope, and the destructor pops the scope, ensuring that the previous default communicator is restored even in case of exceptions.
63 struct Scope {
64 Scope() { scope::push(); }
65 Scope(std::string_view name) { scope::push(name); }
66 Scope(const Comm& comm) { scope::push(comm); };
67 /// @brief Constructor using integer value. If the given communicator is not already registered, it will be registered with a generated name "int.<communicator>" for the duration of the scope, and unregistered when the scope is destructed.
68 /// @param communicator The integer value of the MPI communicator to use for the scope
69 Scope(int communicator) { scope::push(communicator); }
70
71 Scope(const Scope&) = delete;
72 Scope(Scope&&) = delete;
73 Scope& operator=(const Scope&) = delete;
74 Scope& operator=(Scope&&) = delete;
75 ~Scope() { scope::pop(); }
76 };
77
78 [[deprecated("Use atlas::mpi::scope::push instead")]] inline void push() { scope::push(); }
79 [[deprecated("Use atlas::mpi::scope::pop instead")]] inline void pop() { scope::pop(); }
80 [[deprecated("Use atlas::mpi::scope::push instead")]] inline void push(std::string_view name) { scope::push(name); }
81 [[deprecated("Use atlas::mpi::scope::pop instead")]] inline void pop(std::string_view /*name*/) { scope::pop(); }
82
83 } // namespace atlas::mpi