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
15
21#include "shamcomm/mpi.hpp"
24#include "shamcomm/wrapper.hpp"
25#include <optional>
26
27namespace shamcomm {
28
29 i32 _world_rank;
30
31 i32 _world_size;
32
33 std::optional<i32> _max_tag = std::nullopt;
34
35 std::string _mpi_process_name;
36
37 i32 mpi_max_tag_value() { return (_max_tag) ? *_max_tag : shambase::get_max<i32>(); }
38
39 i32 world_size() { return _world_size; }
40
41 i32 world_rank() { return _world_rank; }
42
43 std::string mpi_process_name() { return _mpi_process_name; }
44
45 __attribute__((no_sanitize_address)) void fetch_mpi_process_name() {
46 char hn[MPI_MAX_PROCESSOR_NAME];
47 int hn_len;
48
49 MPICHECK(MPI_Get_processor_name(hn, &hn_len));
50
51 _mpi_process_name = std::string(hn, size_t(hn_len));
52 }
53
54 void set_callstack_process_identifier() {
55 std::optional<u32> local_rank = node_local_rank();
56
57 shambase::set_callstack_process_identifier(
58 sham::format(
59 "{} (world rank: {}, local rank: {})",
60 _mpi_process_name,
61 world_rank(),
62 (local_rank) ? std::to_string(*local_rank) : "Unknown"));
63 }
64
66
67 MPICHECK(MPI_Comm_size(MPI_COMM_WORLD, &_world_size));
68 MPICHECK(MPI_Comm_rank(MPI_COMM_WORLD, &_world_rank));
69
70 fetch_mpi_process_name();
71 set_callstack_process_identifier();
72
73 {
74 void *max_tag;
75 int flag;
76 /* The address of a void pointer must be used! */
77 MPI_Comm_get_attr(MPI_COMM_WORLD, MPI_TAG_UB, &max_tag, &flag);
78 if (flag) {
79 _max_tag = *(int *) max_tag;
80 }
81 }
82
83 shamcomm::mpi::Barrier(MPI_COMM_WORLD);
84
86
88 }
89
91 int flag = false;
92 MPICHECK(MPI_Initialized(&flag));
93 return flag;
94 }
95
96} // namespace shamcomm
std::int32_t i32
32 bit integer
void set_time_offset(f64 offset)
Set the time offset used for Chrome tracing.
Definition chrome.cpp:30
void set_chrome_pid(u32 pid)
Set the Chrome tracing process id.
Definition chrome.cpp:32
Core formatting functions: format, vformat, and format_printf.
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.
namespace for communication related stuff
void fetch_world_info()
Gets the information about the MPI communicator.
Definition worldInfo.cpp:65
i32 world_rank()
Gives the rank of the current process in the MPI communicator.
Definition worldInfo.cpp:41
i32 world_size()
Gives the size of the MPI communicator.
Definition worldInfo.cpp:39
i32 mpi_max_tag_value()
Gets the maximum value of the MPI tag.
Definition worldInfo.cpp:37
bool is_mpi_initialized()
Check if MPI is initialized.
Definition worldInfo.cpp:90
std::string mpi_process_name()
Gets the name of the MPI process.
Definition worldInfo.cpp:43
This file contains the definition for the stacktrace related functionality.
f64 get_wtime()
Returns the current wall clock time in seconds.
Functions related to the MPI communicator.
void Barrier(MPI_Comm comm)
MPI wrapper for MPI_Barrier.
Definition wrapper.cpp:196