From aa2f5a09de019620d33c99db4c9d6150538a2263 Mon Sep 17 00:00:00 2001 From: Eyal Rozenberg Date: Mon, 18 Nov 2024 22:09:38 +0200 Subject: [PATCH] Fixes #698: * Added a `device::default_()` function for obtaining the default CUDA device * Added a bit of code using `device::default_()` to the `device_management` example program --- examples/by_api_module/device_management.cpp | 7 +++++++ src/cuda/api/device.hpp | 8 ++++++++ 2 files changed, 15 insertions(+) diff --git a/examples/by_api_module/device_management.cpp b/examples/by_api_module/device_management.cpp index 1cc3da4d..03b9dcbd 100644 --- a/examples/by_api_module/device_management.cpp +++ b/examples/by_api_module/device_management.cpp @@ -40,6 +40,13 @@ void basics(cuda::device::id_t device_id) die_("The device's reported ID and the ID for which we created the device differ: " + std::to_string(device.id()) + " !=" + std::to_string(device_id)); } + + if (device.id() == cuda::device::default_device_id) { + auto default_device = cuda::device::default_(); + if (device != default_device) { + die_("A device with the default device ID was not equal to the default CUDA device"); + } + } } void attributes_and_properties() diff --git a/src/cuda/api/device.hpp b/src/cuda/api/device.hpp index 90195d56..f9c4600c 100644 --- a/src/cuda/api/device.hpp +++ b/src/cuda/api/device.hpp @@ -845,6 +845,14 @@ inline device_t get(id_t id) return wrap(id); } +/** + * Obtains (a proxy for) the default CUDA device, being the device with the default CUDA device id. + */ +inline device_t default_() +{ + return get(cuda::device::default_device_id); +} + /** * A named constructor idiom for a "dummy" CUDA device representing the CPU. *