Skip to content

Commit d44e8b9

Browse files
committed
Reduce large allocation tests to 70% on WSL and probe max allocatable memory
1 parent 06f7ff3 commit d44e8b9

3 files changed

Lines changed: 67 additions & 12 deletions

File tree

cpp/tests/mr/arena_mr_tests.cpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
*/
55

66
#include "../byte_literals.hpp"
7+
#include "test_utils.hpp"
78

89
#include <rmm/aligned.hpp>
910
#include <rmm/cuda_device.hpp>
@@ -483,11 +484,13 @@ TEST_F(ArenaTest, SizeSmallerThanSuperblockSize) // NOLINT
483484
EXPECT_THROW(construct_small(), rmm::logic_error);
484485
}
485486

486-
TEST_F(ArenaTest, AllocateNinetyPercent) // NOLINT
487+
TEST_F(ArenaTest, AllocateMostOfFreeMemory) // NOLINT
487488
{
488-
EXPECT_NO_THROW([]() { // NOLINT(cppcoreguidelines-avoid-goto)
489-
auto const ninety_percent = rmm::percent_of_free_device_memory(90);
490-
arena_mr mr(rmm::mr::get_current_device_resource_ref(), ninety_percent);
489+
if (is_wsl()) { log_max_allocatable_memory(); }
490+
auto const percent = is_wsl() ? 70 : 90;
491+
EXPECT_NO_THROW([percent]() { // NOLINT(cppcoreguidelines-avoid-goto)
492+
auto const pool_size = rmm::percent_of_free_device_memory(percent);
493+
arena_mr mr(rmm::mr::get_current_device_resource_ref(), pool_size);
491494
}());
492495
}
493496

cpp/tests/mr/pool_mr_tests.cpp

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
* SPDX-License-Identifier: Apache-2.0
44
*/
55

6+
#include "test_utils.hpp"
7+
68
#include <rmm/cuda_device.hpp>
79
#include <rmm/detail/error.hpp>
810
#include <rmm/device_buffer.hpp>
@@ -45,15 +47,15 @@ TEST(PoolTest, ThrowMaxLessThanInitial)
4547
EXPECT_THROW(max_less_than_initial(), rmm::logic_error);
4648
}
4749

48-
TEST(PoolTest, AllocateNinetyPercent)
50+
TEST(PoolTest, AllocateMostOfFreeMemory)
4951
{
50-
auto allocate_ninety = []() {
51-
auto const [free, total] = rmm::available_device_memory();
52-
(void)total;
53-
auto const ninety_percent_pool = rmm::percent_of_free_device_memory(90);
54-
pool_mr mr{rmm::mr::get_current_device_resource_ref(), ninety_percent_pool};
52+
if (is_wsl()) { log_max_allocatable_memory(); }
53+
auto const percent = is_wsl() ? 70 : 90;
54+
auto allocate = [percent]() {
55+
auto const pool_size = rmm::percent_of_free_device_memory(percent);
56+
pool_mr mr{rmm::mr::get_current_device_resource_ref(), pool_size};
5557
};
56-
EXPECT_NO_THROW(allocate_ninety());
58+
EXPECT_NO_THROW(allocate());
5759
}
5860

5961
TEST(PoolTest, TwoLargeBuffers)

cpp/tests/mr/test_utils.hpp

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,67 @@
11
/*
2-
* SPDX-FileCopyrightText: Copyright (c) 2024-2025, NVIDIA CORPORATION.
2+
* SPDX-FileCopyrightText: Copyright (c) 2024-2026, NVIDIA CORPORATION.
33
* SPDX-License-Identifier: Apache-2.0
44
*/
55

66
#pragma once
77

88
#include <rmm/aligned.hpp>
9+
#include <rmm/cuda_device.hpp>
10+
#include <rmm/detail/format.hpp>
911
#include <rmm/mr/system_memory_resource.hpp>
1012

1113
#include <cuda_runtime_api.h>
1214

15+
#include <cstddef>
16+
#include <fstream>
17+
#include <iostream>
18+
#include <string>
19+
1320
namespace rmm::test {
1421

22+
/**
23+
* @brief Returns true if running under Windows Subsystem for Linux (WSL).
24+
*/
25+
inline bool is_wsl()
26+
{
27+
std::ifstream proc_version("/proc/version");
28+
if (proc_version.is_open()) {
29+
std::string line;
30+
std::getline(proc_version, line);
31+
return line.find("microsoft") != std::string::npos ||
32+
line.find("Microsoft") != std::string::npos;
33+
}
34+
return false;
35+
}
36+
37+
/**
38+
* @brief Probes and logs the maximum allocatable GPU memory percentage.
39+
*
40+
* Tries 90%, 80%, 70%, 60% of free memory in sequence, stopping at the first
41+
* successful cudaMalloc. Logs the result along with cudaMemGetInfo values.
42+
*/
43+
inline void log_max_allocatable_memory()
44+
{
45+
auto const [free, total] = rmm::available_device_memory();
46+
std::cout << "cudaMemGetInfo: free=" << rmm::detail::format_bytes(free)
47+
<< " total=" << rmm::detail::format_bytes(total) << std::endl;
48+
49+
for (int pct : {90, 80, 70, 60}) {
50+
auto const size = static_cast<std::size_t>(static_cast<double>(free) * pct / 100.0);
51+
void* ptr = nullptr;
52+
if (cudaMalloc(&ptr, size) == cudaSuccess) {
53+
cudaFree(ptr);
54+
std::cout << "Max allocatable: >= " << pct << "% of free (" << rmm::detail::format_bytes(size)
55+
<< ")" << std::endl;
56+
return;
57+
}
58+
cudaGetLastError();
59+
std::cout << "cudaMalloc failed at " << pct << "% of free (" << rmm::detail::format_bytes(size)
60+
<< ")" << std::endl;
61+
}
62+
std::cout << "cudaMalloc failed at all tested percentages" << std::endl;
63+
}
64+
1565
/**
1666
* @brief Returns if a pointer points to a device memory or managed memory
1767
* allocation.

0 commit comments

Comments
 (0)