Skip to content
Open
Show file tree
Hide file tree
Changes from 3 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
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
48 changes: 41 additions & 7 deletions cpp/tests/mg/test_opg_utils.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* SPDX-FileCopyrightText: Copyright (c) 2019-2024, NVIDIA CORPORATION.
* SPDX-FileCopyrightText: Copyright (c) 2019-2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
* SPDX-License-Identifier: Apache-2.0
*/

Expand All @@ -14,6 +14,32 @@ namespace MLCommon {
namespace Test {
namespace opg {

/**
* @brief This process's rank among the MPI ranks on its node, and how many of them there are.
*
* Ranks that share a node also share their device visibility, so the device index has to be
* node-local: with one rank per one-GPU node, every rank has global rank != 0 but each process
* still sees a single device.
*/
inline void get_node_local_rank_size(int& local_rank, int& local_size)
{
MPI_Comm node_comm{};
MPI_Comm_split_type(MPI_COMM_WORLD, MPI_COMM_TYPE_SHARED, 0, MPI_INFO_NULL, &node_comm);
MPI_Comm_rank(node_comm, &local_rank);
MPI_Comm_size(node_comm, &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 All @@ -26,17 +52,25 @@ class MPIEnvironment : public ::testing::Environment {
{
MPI_Init(NULL, NULL);

int rank, size;
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &size);
int local_rank, local_size;
get_node_local_rank_size(local_rank, local_size);

int nGpus;
RAFT_CUDA_TRY(cudaGetDeviceCount(&nGpus));

ASSERT(
nGpus >= size, "Number of GPUs are lesser than MPI ranks! ngpus=%d, nranks=%d", nGpus, size);
// A rank that leaves SetUp while the others enter a collective leaves the whole job
// hanging, so every rank has to agree on whether the topology can be used.
int insufficient_local_gpus = nGpus < local_size ? 1 : 0;
int insufficient_gpus = 0;
MPI_Allreduce(
&insufficient_local_gpus, &insufficient_gpus, 1, MPI_INT, MPI_MAX, MPI_COMM_WORLD);
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved

ASSERT(!insufficient_gpus,
"Number of GPUs are lesser than local MPI ranks! ngpus=%d, local_ranks=%d",
nGpus,
local_size);

RAFT_CUDA_TRY(cudaSetDevice(rank));
RAFT_CUDA_TRY(cudaSetDevice(local_rank));
}

void TearDown() { MPI_Finalize(); }
Expand Down
Loading