From 86eb49b9df0d2d1374387b566834fbe01de3a419 Mon Sep 17 00:00:00 2001 From: Eyal Rozenberg Date: Tue, 9 Aug 2022 21:39:18 +0300 Subject: [PATCH] Added some primary context activation check game to the context management test program. --- .../context_management.cpp | 21 ++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/examples/by_driver_api_module/context_management.cpp b/examples/by_driver_api_module/context_management.cpp index 074d7c4a..be227b00 100644 --- a/examples/by_driver_api_module/context_management.cpp +++ b/examples/by_driver_api_module/context_management.cpp @@ -142,7 +142,7 @@ int main(int argc, char **argv) } // Being very cavalier about our command-line arguments here... - cuda::device::id_t device_id = (argc > 1) ? + cuda::device::id_t device_id = (argc > 1) ? ::std::stoi(argv[1]) : cuda::device::default_device_id; if (cuda::device::count() <= device_id) { @@ -153,10 +153,25 @@ int main(int argc, char **argv) ::std::cout << "Using CUDA device " << device.name() << " (having device ID " << device.id() << ")\n"; -// report_context_stack("Before anything is done"); + if (cuda::device::primary_context::is_active(device)) { + std::ostringstream oss; + oss << "The primary context is unexpectedly active before we've done anything with its device (" << device << ")\n"; + die_(oss.str()); + } + + auto original_pc = device.primary_context(); + + cuda::device::primary_context::detail_::decrease_refcount(device.id()); + + if (cuda::device::primary_context::is_active(device)) { + die_("The primary context is unexpectedly active after increasing, then decreasing, its refcount"); + } + auto pc = device.primary_context(); -// report_context_stack("After getting the primary context"); + // std::cout << "New PC handle = " << pc.handle() << " ; old PC handle = " << original_pc.handle() << "\n"; + + cuda::device::primary_context::detail_::increase_refcount(device.id()); cuda::context::current::push(pc); constexpr const bool is_primary = true;