Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Share the MPI local-rank helpers with the RF multi-GPU tests
rf_test.cu, rf_quantile_test.cu and rf_row_sampler.cu each carried their
own copy of initialize_mpi_once() and get_mpi_local_rank_size(). Move both
into test_opg_utils.h, next to MPIEnvironment and the node-local rank
helper it uses, and call them from there.

The three tests keep the same behaviour; they now share one definition of
how a rank maps to a node-local device.

Signed-off-by: 0z5a <dezhen.lu@student.uni-tuebingen.de>
  • Loading branch information
0z5a committed Nov 5, 2026
commit ee43062deb533ab75b41617f73dc80386f139a28
19 changes: 3 additions & 16 deletions cpp/tests/mg/rf_quantile_test.cu
Original file line number Diff line number Diff line change
Expand Up @@ -29,21 +29,8 @@ namespace ML {
namespace Test {
namespace opg {

void initialize_mpi_once()
{
int mpi_initialized = 0;
MPI_Initialized(&mpi_initialized);
if (!mpi_initialized) { MPI_Init(nullptr, nullptr); }
}

void get_mpi_local_rank_size(int& local_rank, int& local_size)
{
MPI_Comm local_comm{};
MPI_Comm_split_type(MPI_COMM_WORLD, MPI_COMM_TYPE_SHARED, 0, MPI_INFO_NULL, &local_comm);
MPI_Comm_rank(local_comm, &local_rank);
MPI_Comm_size(local_comm, &local_size);
MPI_Comm_free(&local_comm);
}
using MLCommon::Test::opg::get_node_local_rank_size;
using MLCommon::Test::opg::initialize_mpi_once;

template <typename T>
MPI_Datatype mpi_datatype()
Expand Down Expand Up @@ -111,7 +98,7 @@ class RfMgQuantileTest : public ::testing::Test {
MPI_Comm_size(MPI_COMM_WORLD, &size);
int local_rank = 0;
int local_size = 1;
get_mpi_local_rank_size(local_rank, local_size);
get_node_local_rank_size(local_rank, local_size);

int n_gpus = 0;
RAFT_CUDA_TRY(cudaGetDeviceCount(&n_gpus));
Expand Down
21 changes: 4 additions & 17 deletions cpp/tests/mg/rf_row_sampler.cu
Original file line number Diff line number Diff line change
Expand Up @@ -31,29 +31,16 @@ namespace ML {
namespace Test {
namespace opg {

using MLCommon::Test::opg::get_node_local_rank_size;
using MLCommon::Test::opg::initialize_mpi_once;

enum class WeightKind { ThreeRows, Uniform, WithZeros, Skewed };

struct RowSamplerTestParams {
PartitionKind partition_kind;
WeightKind weight_kind;
};

void initialize_mpi_once()
{
int mpi_initialized = 0;
MPI_Initialized(&mpi_initialized);
if (!mpi_initialized) { MPI_Init(nullptr, nullptr); }
}

void get_mpi_local_rank_size(int& local_rank, int& local_size)
{
MPI_Comm local_comm{};
MPI_Comm_split_type(MPI_COMM_WORLD, MPI_COMM_TYPE_SHARED, 0, MPI_INFO_NULL, &local_comm);
MPI_Comm_rank(local_comm, &local_rank);
MPI_Comm_size(local_comm, &local_size);
MPI_Comm_free(&local_comm);
}

std::vector<double> make_weights(WeightKind kind)
{
switch (kind) {
Expand All @@ -79,7 +66,7 @@ TEST_P(RfMgRowSamplerTest, SamplesGlobalWeightDistribution)

int local_rank = 0;
int local_size = 1;
get_mpi_local_rank_size(local_rank, local_size);
get_node_local_rank_size(local_rank, local_size);
int n_gpus = 0;
RAFT_CUDA_TRY(cudaGetDeviceCount(&n_gpus));
int insufficient_local_gpus = n_gpus < local_size;
Expand Down
21 changes: 4 additions & 17 deletions cpp/tests/mg/rf_test.cu
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ namespace ML {
namespace Test {
namespace opg {

using MLCommon::Test::opg::get_node_local_rank_size;
using MLCommon::Test::opg::initialize_mpi_once;

struct RfMgTestParams {
int n_rows;
int n_cols;
Expand Down Expand Up @@ -206,22 +209,6 @@ void expect_tree_limits(RandomForestMetaData<T, L> const& forest, RfMgTestParams
}
}

void initialize_mpi_once()
{
int mpi_initialized = 0;
MPI_Initialized(&mpi_initialized);
if (!mpi_initialized) { MPI_Init(nullptr, nullptr); }
}

void get_mpi_local_rank_size(int& local_rank, int& local_size)
{
MPI_Comm local_comm{};
MPI_Comm_split_type(MPI_COMM_WORLD, MPI_COMM_TYPE_SHARED, 0, MPI_INFO_NULL, &local_comm);
MPI_Comm_rank(local_comm, &local_rank);
MPI_Comm_size(local_comm, &local_size);
MPI_Comm_free(&local_comm);
}

template <typename DataT, typename LabelT>
class RfMgPropertyTestImpl {
public:
Expand All @@ -235,7 +222,7 @@ class RfMgPropertyTestImpl {

int local_rank = 0;
int local_size = 1;
get_mpi_local_rank_size(local_rank, local_size);
get_node_local_rank_size(local_rank, local_size);

int n_gpus = 0;
RAFT_CUDA_TRY(cudaGetDeviceCount(&n_gpus));
Expand Down
10 changes: 10 additions & 0 deletions cpp/tests/mg/test_opg_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,16 @@ inline void get_node_local_rank_size(int& local_rank, int& local_size)
MPI_Comm_free(&node_comm);
}

/**
* @brief Initialise MPI if this process has not already done so.
*/
inline void initialize_mpi_once()
{
int mpi_initialized = 0;
MPI_Initialized(&mpi_initialized);
if (!mpi_initialized) { MPI_Init(nullptr, nullptr); }
}

/**
*
* @brief Testing environment to handle googletest runs
Expand Down
Loading