TraceMaker
C++ API of the engine
Loading...
Searching...
No Matches
device.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: GPL-3.0-or-later
2#pragma once
3// GPU discovery and memory admission control (design doc 07 ยง4).
4//
5// The GPUs on the development machine are shared with other jobs, so free memory is scarce and changes
6// over time. Callers ask `has_free_memory` before large allocations and fall back to the CPU reference
7// path when it fails; results are identical either way.
8#include <cstddef>
9#include <optional>
10#include <string>
11#include <string_view>
12#include <vector>
13
14namespace tmk::gpu {
15
16enum class Backend { Cpu, Cuda, Metal };
17
18struct DeviceInfo {
19 int index = -1; // backend runtime index; select by uuid/name for stability
20 std::string name;
21 std::string uuid; // CUDA UUID or Metal registry ID
22 int cc_major = 0, cc_minor = 0;
23 std::size_t total_bytes = 0;
24 std::size_t free_bytes = 0; // CUDA free memory or Metal working-set headroom
25};
26
27// Backend compiled into this build. CPU references are always available.
29
30// All visible devices with current memory budget. Empty without an available GPU.
31std::vector<DeviceInfo> list_devices();
32
33// Finds a device by exact UUID or by a case-sensitive substring of its name ("V100", "P100").
34std::optional<DeviceInfo> find_device(std::string_view uuid_or_name);
35
36// True when `bytes` plus a safety margin fit in the device's free memory right now.
37bool has_free_memory(int device_index, std::size_t bytes);
38
39// Safety margin kept free on every device for other processes and runtime overheads.
40inline constexpr std::size_t kDeviceMemoryMargin = std::size_t{256} << 20;
41
42} // namespace tmk::gpu
std::optional< DeviceInfo > find_device(std::string_view uuid_or_name)
Finds a device by exact UUID or by a case-sensitive substring of its name ("V100",...
constexpr std::size_t kDeviceMemoryMargin
Safety margin kept free on every device for other processes and runtime overheads.
Definition device.hpp:40
Backend compiled_backend()
Backend compiled into this build. CPU references are always available.
bool has_free_memory(int device_index, std::size_t bytes)
True when bytes plus a safety margin fit in the device's free memory right now.
std::vector< DeviceInfo > list_devices()
All visible devices with current memory budget. Empty without an available GPU.
std::size_t total_bytes
Definition device.hpp:23
std::size_t free_bytes
CUDA free memory or Metal working-set headroom.
Definition device.hpp:24
std::string uuid
CUDA UUID or Metal registry ID.
Definition device.hpp:21
int index
backend runtime index; select by uuid/name for stability
Definition device.hpp:19
std::string name
Definition device.hpp:20