From d985c7dee1fc0f19f74cb5f4505be3e1dc47a905 Mon Sep 17 00:00:00 2001 From: Mikael Simberg Date: Fri, 24 May 2024 16:00:28 +0200 Subject: [PATCH 1/3] Don't print pool and thread numbers in hexadecimal in logs --- libs/pika/init_runtime/src/init_logging.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libs/pika/init_runtime/src/init_logging.cpp b/libs/pika/init_runtime/src/init_logging.cpp index 3a315caac..497f0f750 100644 --- a/libs/pika/init_runtime/src/init_logging.cpp +++ b/libs/pika/init_runtime/src/init_logging.cpp @@ -76,7 +76,7 @@ namespace pika::detail { { static void format_id(spdlog::memory_buf_t& dest, std::size_t i) { - if (i != std::size_t(-1)) { dest.append(fmt::format("{:04x}", i)); } + if (i != std::size_t(-1)) { dest.append(fmt::format("{:04}", i)); } else { dest.append(std::string_view("----")); } } From 36c5729b77c2c84f3ea5db0fcede17420ad32fc0 Mon Sep 17 00:00:00 2001 From: Mikael Simberg Date: Fri, 24 May 2024 16:01:18 +0200 Subject: [PATCH 2/3] Try to get MPI rank in logs with MPI_Comm_rank before guessing with environment variables --- libs/pika/init_runtime/CMakeLists.txt | 5 ++++ libs/pika/init_runtime/src/init_logging.cpp | 28 ++++++++++++++++----- 2 files changed, 27 insertions(+), 6 deletions(-) diff --git a/libs/pika/init_runtime/CMakeLists.txt b/libs/pika/init_runtime/CMakeLists.txt index b2a797fb8..026fec1a8 100644 --- a/libs/pika/init_runtime/CMakeLists.txt +++ b/libs/pika/init_runtime/CMakeLists.txt @@ -15,6 +15,10 @@ set(init_runtime_headers set(init_runtime_sources init_logging.cpp init_runtime.cpp scoped_finalize.cpp) +if(PIKA_WITH_MPI) + list(APPEND init_runtime_additional_module_dependencies pika_mpi_base) +endif() + include(pika_add_module) pika_add_module( pika init_runtime @@ -33,5 +37,6 @@ pika_add_module( pika_schedulers pika_threading_base pika_timing + ${init_runtime_additional_module_dependencies} CMAKE_SUBDIRS examples tests ) diff --git a/libs/pika/init_runtime/src/init_logging.cpp b/libs/pika/init_runtime/src/init_logging.cpp index 497f0f750..3cb864d5d 100644 --- a/libs/pika/init_runtime/src/init_logging.cpp +++ b/libs/pika/init_runtime/src/init_logging.cpp @@ -15,6 +15,10 @@ #include #include +#if defined(PIKA_HAVE_MPI) +# include +#endif + #include #include #include @@ -105,13 +109,25 @@ namespace pika::detail { { static PIKA_DETAIL_NS_DEBUG::hostname_print_helper helper{}; static std::string_view hostname_str = helper.get_hostname(); - dest.append(hostname_str); - if (int rank = helper.guess_rank(); rank != -1) - { - dest.append(hostname_str); - dest.append(fmt::format("/{}", rank)); - } + if (!hostname_str.empty()) { dest.append(hostname_str); } + else { dest.append(std::string_view("----")); } + + static int rank = [&] { + // First try to get the rank through MPI +#if defined(PIKA_HAVE_MPI) + int mpi_initialized = 0; + if (MPI_Initialized(&mpi_initialized) == MPI_SUCCESS && mpi_initialized) + { + int rank = 0; + if (MPI_Comm_rank(MPI_COMM_WORLD, &rank) == MPI_SUCCESS) { return rank; } + } +#endif + + // Otherwise guess based on environment variables + return helper.guess_rank(); + }(); + if (rank != -1) { dest.append(fmt::format("/{}", rank)); } else { dest.append(std::string_view("/----")); } } From 2b845945347ef9fa82b29dfab9f2940ff74cbfca Mon Sep 17 00:00:00 2001 From: Mikael Simberg Date: Fri, 24 May 2024 16:01:31 +0200 Subject: [PATCH 3/3] Guess MPI rank from environment variables differently - Change loop order so that environment variable suffixes are tested in order against all environment variables. This reordering means that the suffixes are given a priority, instead of the previous order which gave priority to the first environment variable matching any of the suffixes. - Change the list of suffixes to be more likely to match the world rank, rather than a node id or a local rank. --- libs/pika/debugging/src/print.cpp | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/libs/pika/debugging/src/print.cpp b/libs/pika/debugging/src/print.cpp index 4e8cabd9f..d7d3f2a07 100644 --- a/libs/pika/debugging/src/print.cpp +++ b/libs/pika/debugging/src/print.cpp @@ -273,12 +273,13 @@ namespace PIKA_DETAIL_NS_DEBUG { #else char** env = environ; #endif - std::vector env_strings{"_RANK=", "_NODEID="}; - for (char** current = env; *current; current++) + std::vector env_strings{"_PROCID=", "_WORLD_RANK=", "_RANK="}; + + for (auto s : env_strings) { - auto e = std::string(*current); - for (auto s : env_strings) + for (char** current = env; *current; current++) { + auto e = std::string(*current); auto pos = e.find(s); if (pos != std::string::npos) {