Skip to content

MPI support#7

Open
TimThuering wants to merge 17 commits into
developfrom
feature/mpi-support
Open

MPI support#7
TimThuering wants to merge 17 commits into
developfrom
feature/mpi-support

Conversation

@TimThuering

Copy link
Copy Markdown
Member

Summary

This PR adds optional MPI support to hws, enabling MPI-aware hardware sampling across distributed multi-process jobs.

API extensions

  • system_hardware_sampler(MPI_Comm, mpi_sampling_mode, ...) : MPI-aware constructors that take a communicator and a sampling mode. Passing hws::detail::mpi_sampling_mode::whole_node ensures that every device visible to at least one sampler is sampled exactly once, even if multiple system_hardware_sampler instances from different ranks see the same device.
  • start_sampling(MPI_Comm) / stop_sampling(MPI_Comm): synchronized start/stop with MPI barriers to align sampling windows across ranks
  • dump_yaml_global(filename, MPI_Comm): gathers YAML output from all ranks on rank 0 and writes a single combined file; available on both hardware_sampler and system_hardware_sampler

Python bindings

  • mpi4py support
  • all new constructors, MPISamplingMode enum, start/stop with communicator overloads, and dump_yaml_global are exposed

CMake

  • new HWS_ENABLE_MPI_SUPPORT=AUTO|ON|OFF option (default AUTO). When MPI is found and Python bindings are enabled, mpi4py must be importable in the configured Python environment.

@TimThuering TimThuering requested a review from vancraar June 12, 2026 11:57
@TimThuering TimThuering added the enhancement New feature or request label Jun 12, 2026

@vancraar vancraar left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, Tim, for this comprehensive MPI addition! The implementation is well-structured overall. I commented to a few points that that caught my eye. What's your opinion on them?

Comment thread bindings/CMakeLists.txt
# Get mpi4py's C header location, simultaneously checking if mpi4py is importable in the current Python environment
execute_process(
COMMAND "${Python_EXECUTABLE}" -c
"import mpi4py, sys; sys.stdout.write(mpi4py.get_include())"

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it possible, that mpi4py is installed, but broken without include path? Should we additionally check for that?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we now have to link PUBLIC against CUDA?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as with CUDA why now PUBLIC?

Comment thread src/hws/hardware_sampler.cpp Outdated
#include <utility> // std::move

#if defined(HWS_MPI_SUPPORT_ENABLED)
#include <mpi.h> // MPI_Comm

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Formatting

Comment thread pyproject.toml
]
# optional dependencies
[project.optional-dependencies]
mpi = ["mpi4py>=4"]

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why check here for Version >=4 but not in the CMakeLists.txt? Should we do this consistent on both sides?

Comment thread include/hws/utility.hpp

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we really include the backend includes in the main utility.hpp, or wouldn't it be better to modularize them?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In my view it looks like lines 70-111: The block inside else if (mode == detail::mpi_sampling_mode::whole_node)
needs one more level of indentation. The #if defined preprocessor directives and
detail::free_hostname_comm(nc) should be indented consistently with the code inside
the conditional block.

Comment thread src/hws/utility.cpp Outdated
#include <vector> // std::vector

#if defined(HWS_MPI_SUPPORT_ENABLED)
#include <mpi.h> // MPI_Comm, MPI_Gatherv, MPI_Gather, MPI_Initialized, MPI_Comm_rank, MPI_Comm_size

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

formatting

@vancraar vancraar left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, Tim, for addressing all my review points! The additional CMake check for a broken mpi4py include path, the mpi4py version check, reverting the GPU library linkage back to PRIVATE, and the cleanup of the backend includes from utility.hpp all look good now. The PR is in great shape – happy to approve!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants