python/src/gpu.cpp
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "bindings.h" | ||
| 2 | #include "common.h" | ||
| 3 | |||
| 4 | #include "terraingen/gpu/backend.h" | ||
| 5 | |||
| 6 | #include <string> | ||
| 7 | |||
| 8 | namespace terraingen::python { | ||
| 9 | |||
| 10 | 44 | void BindGpu(nb::module_& m) { | |
| 11 | 88 | nb::enum_<Backend>(m, "Backend", "Where evaluation runs.") | |
| 12 | 44 | .value("AUTO", Backend::Auto, "A GPU when one is available, otherwise the CPU.") | |
| 13 | 44 | .value("CPU", Backend::Cpu, "The reference C++ implementation.") | |
| 14 | 44 | .value("VULKAN", Backend::Vulkan, "Vulkan compute on the device TERRAINGEN_DEVICE selects.") | |
| 15 | 44 | .value("GPU", Backend::Gpu, | |
| 16 | "The best GPU backend available, whichever it is; an error when there is none.") | ||
| 17 | 44 | .value("CUDA", Backend::Cuda, | |
| 18 | "CUDA on the first NVIDIA device, through the driver opened at run time.") | ||
| 19 | .value("METAL", Backend::Metal, "Metal on the system's default device; Apple only."); | ||
| 20 | 44 | nb::class_<BackendStatus>(m, "BackendStatus", | |
| 21 | "Whether a backend can run here, on which device, or why not.") | ||
| 22 | 44 | .def_ro("backend", &BackendStatus::backend) | |
| 23 | 44 | .def_ro("available", &BackendStatus::available) | |
| 24 | 44 | .def_ro("device", &BackendStatus::device) | |
| 25 | 88 | .def_ro("reason", &BackendStatus::reason) | |
| 26 | 44 | .def("__repr__", [](const BackendStatus& s) { | |
| 27 | ✗ | return "BackendStatus(" + std::string(BackendName(s.backend)) + ", " + | |
| 28 | ✗ | (s.available ? "available on " + s.device : "unavailable: " + s.reason) + ")"; | |
| 29 | }); | ||
| 30 | 44 | m.def("backends", &Backends, | |
| 31 | "The status of the CPU, Vulkan, CUDA and Metal backends, in that order; each GPU library " | ||
| 32 | "is opened on first use, never at import."); | ||
| 33 | 44 | m.def("backend_named", &BackendNamed, "name"_a, | |
| 34 | "The backend named 'auto', 'cpu', 'gpu', 'vulkan', 'cuda' or 'metal', or None."); | ||
| 35 | 44 | m.def("default_backend", &DefaultBackend, | |
| 36 | "The default backend evaluation uses unless told otherwise: what set_default_backend " | ||
| 37 | "set, otherwise TERRAINGEN_BACKEND, otherwise CPU."); | ||
| 38 | 44 | m.def("set_default_backend", &SetDefaultBackend, "backend"_a, | |
| 39 | "Sets the default backend, overriding TERRAINGEN_BACKEND."); | ||
| 40 | 44 | m.def("resolve_backend", &ResolveBackend, "backend"_a, | |
| 41 | "The backend `backend` resolves to: GPU becomes the best GPU backend available (METAL, " | ||
| 42 | "then CUDA, then VULKAN), AUTO the same or CPU when there is none. Raises RuntimeError " | ||
| 43 | "for a GPU backend that is unavailable."); | ||
| 44 | 44 | } | |
| 45 | |||
| 46 | } // namespace terraingen::python | ||
| 47 |