include/terraingen/gpu/backend.h
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // SPDX-License-Identifier: BSD-3-Clause | ||
| 2 | |||
| 3 | /// @file | ||
| 4 | /// @brief Evaluation backends (the CPU, Vulkan, CUDA or Metal) and the process-wide default. | ||
| 5 | #pragma once | ||
| 6 | |||
| 7 | #include <array> | ||
| 8 | #include <optional> | ||
| 9 | #include <string> | ||
| 10 | #include <string_view> | ||
| 11 | #include <vector> | ||
| 12 | |||
| 13 | namespace terraingen { | ||
| 14 | |||
| 15 | /// Where evaluation runs. | ||
| 16 | enum class Backend { | ||
| 17 | Auto, ///< A GPU when one is available, otherwise the CPU. | ||
| 18 | Cpu, ///< The reference C++ implementation. | ||
| 19 | Vulkan, ///< Vulkan compute on the device `TERRAINGEN_DEVICE` selects. | ||
| 20 | Gpu, ///< The best GPU backend available, whichever it is; an error when there is none. | ||
| 21 | Cuda, ///< CUDA on the first NVIDIA device, through the driver opened at run time. | ||
| 22 | Metal, ///< Metal on the system's default device; Apple platforms only. | ||
| 23 | }; | ||
| 24 | |||
| 25 | /// Every backend name `BackendNamed` reads, in the order messages list them. | ||
| 26 | inline constexpr std::array<std::string_view, 6> kBackendNames{"auto", "cpu", "gpu", | ||
| 27 | "vulkan", "cuda", "metal"}; | ||
| 28 | |||
| 29 | /// The backend names as messages list them: "auto, cpu, gpu, vulkan, cuda or metal". | ||
| 30 | std::string BackendNameList(); | ||
| 31 | |||
| 32 | /// Looks up a backend by one of `kBackendNames`, ignoring case; `std::nullopt` for an unknown name. | ||
| 33 | std::optional<Backend> BackendNamed(std::string_view name); | ||
| 34 | |||
| 35 | /// The name of a backend, as `BackendNamed` reads it; "unknown" for an out-of-range value. | ||
| 36 | std::string_view BackendName(Backend backend); | ||
| 37 | |||
| 38 | /// A backend as messages show it: its name, and for an available GPU backend its device too, as in | ||
| 39 | /// "cuda (NVIDIA GeForce RTX 4090)". | ||
| 40 | std::string DescribeBackend(Backend backend); | ||
| 41 | |||
| 42 | /// Whether a backend can run here, on which device, or why not. | ||
| 43 | 433 | struct BackendStatus { | |
| 44 | Backend backend = Backend::Cpu; ///< The backend described. | ||
| 45 | bool available = false; ///< Whether it can run here. | ||
| 46 | std::string device; ///< The device used, when available. | ||
| 47 | std::string reason; ///< Why it is not available, otherwise. | ||
| 48 | }; | ||
| 49 | |||
| 50 | /// The status of every backend that runs on a device: `Cpu`, `Vulkan`, `Cuda` and `Metal`, in that | ||
| 51 | /// order. | ||
| 52 | /// | ||
| 53 | /// Each GPU library (the Vulkan loader, the NVIDIA driver, Metal) is opened the first time this or | ||
| 54 | /// an evaluation on it is asked for, never at load time, so one build runs anywhere. | ||
| 55 | std::vector<BackendStatus> Backends(); | ||
| 56 | |||
| 57 | /// The process-wide default backend, which evaluation uses unless told otherwise: what | ||
| 58 | /// `SetDefaultBackend` last set, otherwise the environment variable `TERRAINGEN_BACKEND` (a name | ||
| 59 | /// `BackendNamed` reads), otherwise `Cpu`, so results never change with the machine by default. | ||
| 60 | /// @throws std::invalid_argument for an unknown name in `TERRAINGEN_BACKEND`. | ||
| 61 | Backend DefaultBackend(); | ||
| 62 | |||
| 63 | /// Sets the process-wide default backend, overriding `TERRAINGEN_BACKEND`. | ||
| 64 | void SetDefaultBackend(Backend backend); | ||
| 65 | |||
| 66 | /// Resolves a requested backend to the one that runs, `Cpu` or a GPU backend: `Gpu` becomes the | ||
| 67 | /// best GPU backend available (`Metal`, then `Cuda`, then `Vulkan`), `Auto` the same or `Cpu` when | ||
| 68 | /// there is none, and the others themselves. | ||
| 69 | /// @throws std::runtime_error when a GPU backend is requested but unavailable, with the reason. | ||
| 70 | Backend ResolveBackend(Backend requested); | ||
| 71 | |||
| 72 | } // namespace terraingen | ||
| 73 |