Loading...
Searching...
No Matches
logger_mpi.hpp
Go to the documentation of this file.
1#pragma once
2
3/**
4 * MPI-aware logger initialization.
5 *
6 * Include this ONLY in main.cpp (or the MPI entry point).
7 * All other source files use "io/logger.hpp" (the serial baseline).
8 *
9 * Usage:
10 * #include "io/logger_mpi.hpp"
11 * q3::log::init_mpi(); // silences all ranks except rank 0
12 */
13
14#include "io/logger.hpp"
15#include <mpi.h>
16
17namespace q3 {
18namespace log {
19
20/**
21 * Initialize the logger for MPI runs.
22 *
23 * Calls the serial init() then silences stdout on all non-zero ranks.
24 * Each rank still writes to its own log file if log_file is provided.
25 *
26 * @param log_file Optional base path for per-rank file sinks.
27 * Rank 0 writes to log_file; rank N writes to log_file.N.
28 */
29inline void init_mpi(const std::string& log_file = "") {
30 int rank_index = 0;
31 MPI_Comm_rank(MPI_COMM_WORLD, &rank_index);
32
33 // Build per-rank file path for non-zero ranks
34 std::string effective_log_file = log_file;
35 if (!log_file.empty() && rank_index != 0) {
36 effective_log_file = log_file + "." + std::to_string(rank_index);
37 }
38
39 init(effective_log_file);
40
41 /**
42 * Silence console output on all non-root ranks via the logger wrapper —
43 * no direct spdlog access outside io/logger.hpp.
44 */
45 if (rank_index != 0) {
46 silence();
47 }
48}
49
50} // namespace log
51} // namespace q3
void silence()
Definition logger.hpp:146
void init_mpi(const std::string &log_file="")
void init(const std::string &log_file="")
Definition logger.hpp:59