Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Minor logging fixes #1127

Merged
merged 3 commits into from
May 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions libs/pika/debugging/src/print.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -273,12 +273,13 @@ namespace PIKA_DETAIL_NS_DEBUG {
#else
char** env = environ;
#endif
std::vector<std::string> env_strings{"_RANK=", "_NODEID="};
for (char** current = env; *current; current++)
std::vector<std::string_view> 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)
{
Expand Down
5 changes: 5 additions & 0 deletions libs/pika/init_runtime/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -33,5 +37,6 @@ pika_add_module(
pika_schedulers
pika_threading_base
pika_timing
${init_runtime_additional_module_dependencies}
CMAKE_SUBDIRS examples tests
)
30 changes: 23 additions & 7 deletions libs/pika/init_runtime/src/init_logging.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@
#include <pika/string_util/from_string.hpp>
#include <pika/threading_base/thread_data.hpp>

#if defined(PIKA_HAVE_MPI)
# include <pika/mpi_base/mpi.hpp>
#endif

#include <fmt/format.h>
#include <fmt/ostream.h>
#include <spdlog/pattern_formatter.h>
Expand Down Expand Up @@ -76,7 +80,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("----")); }
}

Expand Down Expand Up @@ -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("/----")); }
}

Expand Down
Loading