|
1 | 1 | /* |
2 | | - * SPDX-FileCopyrightText: Copyright (c) 2024-2025, NVIDIA CORPORATION. |
| 2 | + * SPDX-FileCopyrightText: Copyright (c) 2024-2026, NVIDIA CORPORATION. |
3 | 3 | * SPDX-License-Identifier: Apache-2.0 |
4 | 4 | */ |
5 | 5 |
|
6 | 6 | #pragma once |
7 | 7 |
|
8 | 8 | #include <rmm/aligned.hpp> |
| 9 | +#include <rmm/cuda_device.hpp> |
| 10 | +#include <rmm/detail/format.hpp> |
9 | 11 | #include <rmm/mr/system_memory_resource.hpp> |
10 | 12 |
|
11 | 13 | #include <cuda_runtime_api.h> |
12 | 14 |
|
| 15 | +#include <cstddef> |
| 16 | +#include <fstream> |
| 17 | +#include <iostream> |
| 18 | +#include <string> |
| 19 | + |
13 | 20 | namespace rmm::test { |
14 | 21 |
|
| 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 | + |
15 | 65 | /** |
16 | 66 | * @brief Returns if a pointer points to a device memory or managed memory |
17 | 67 | * allocation. |
|
0 commit comments