Shamrock 2025.10.0
Astrophysical Code
Loading...
Searching...
No Matches
worldInfo.cpp
Go to the documentation of this file.
1// -------------------------------------------------------//
2//
3// SHAMROCK code for hydrodynamics
4// Copyright (c) 2021-2026 Timothée David--Cléris <tim.shamrock@proton.me>
5// SPDX-License-Identifier: CeCILL Free Software License Agreement v2.1
6// Shamrock is licensed under the CeCILL 2.1 License, see LICENSE for more information
7//
8// -------------------------------------------------------//
9
20#include "shamcomm/mpi.hpp"
23#include "shamcomm/wrapper.hpp"
24#include <optional>
25
26namespace shamcomm {
27
28 i32 _world_rank;
29
30 i32 _world_size;
31
32 std::optional<i32> _max_tag = std::nullopt;
33
34 std::string _mpi_process_name;
35
36 i32 mpi_max_tag_value() { return (_max_tag) ? *_max_tag : shambase::get_max<i32>(); }
37
38 i32 world_size() { return _world_size; }
39
40 i32 world_rank() { return _world_rank; }
41
42 std::string mpi_process_name() { return _mpi_process_name; }
43
44 __attribute__((no_sanitize_address)) void fetch_mpi_process_name() {
45 char hn[MPI_MAX_PROCESSOR_NAME];
46 int hn_len;
47
48 MPICHECK(MPI_Get_processor_name(hn, &hn_len));
49
50 _mpi_process_name = std::string(hn, size_t(hn_len));
51 }
52
53 void set_callstack_process_identifier() {
54 std::optional<u32> local_rank = node_local_rank();
55
56 shambase::set_callstack_process_identifier(
57 shambase::format(
58 "{} (world rank: {}, local rank: {})",
59 _mpi_process_name,
60 world_rank(),
61 (local_rank) ? std::to_string(*local_rank) : "Unknown"));
62 }
63
65
66 MPICHECK(MPI_Comm_size(MPI_COMM_WORLD, &_world_size));
67 MPICHECK(MPI_Comm_rank(MPI_COMM_WORLD, &_world_rank));
68
69 fetch_mpi_process_name();
70 set_callstack_process_identifier();
71
72 {
73 void *max_tag;
74 int flag;
75 /* The address of a void pointer must be used! */
76 MPI_Comm_get_attr(MPI_COMM_WORLD, MPI_TAG_UB, &max_tag, &flag);
77 if (flag) {
78 _max_tag = *(int *) max_tag;
79 }
80 }
81
82 shamcomm::mpi::Barrier(MPI_COMM_WORLD);
83
84 shambase::profiling::chrome::set_time_offset(shambase::details::get_wtime());
85
86 shambase::profiling::chrome::set_chrome_pid(world_rank());
87 }
88
90 int flag = false;
91 MPICHECK(MPI_Initialized(&flag));
92 return flag;
93 }
94
95} // namespace shamcomm
std::int32_t i32
32 bit integer
Functions related to the MPI communicator.
Utility functions for MPI error checking.
#define MPICHECK(mpicall)
Shortcut macro to check MPI return codes.
Use this header to include MPI properly.
void throw_with_loc(std::string message, SourceLocation loc=SourceLocation{})
Throw an exception and append the source location to it.
namespace for communication related stuff
void fetch_world_info()
Gets the information about the MPI communicator.
Definition worldInfo.cpp:64
i32 world_rank()
Gives the rank of the current process in the MPI communicator.
Definition worldInfo.cpp:40
i32 world_size()
Gives the size of the MPI communicator.
Definition worldInfo.cpp:38
i32 mpi_max_tag_value()
Gets the maximum value of the MPI tag.
Definition worldInfo.cpp:36
bool is_mpi_initialized()
Check if MPI is initialized.
Definition worldInfo.cpp:89
std::string mpi_process_name()
Gets the name of the MPI process.
Definition worldInfo.cpp:42
This file contains the definition for the stacktrace related functionality.
Functions related to the MPI communicator.