From 46b477a47e2f621932fe6b68214ead7d5640ec1e Mon Sep 17 00:00:00 2001 From: Gregor Daiss Date: Wed, 28 Jun 2023 22:57:46 -0500 Subject: [PATCH] Cal setdevice when creating events on-demand --- .../core/async_cuda/include/hpx/async_cuda/cuda_event.hpp | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/libs/core/async_cuda/include/hpx/async_cuda/cuda_event.hpp b/libs/core/async_cuda/include/hpx/async_cuda/cuda_event.hpp index 31c6cc768f85..5a13a911c857 100644 --- a/libs/core/async_cuda/include/hpx/async_cuda/cuda_event.hpp +++ b/libs/core/async_cuda/include/hpx/async_cuda/cuda_event.hpp @@ -54,7 +54,15 @@ namespace hpx { namespace cuda { namespace experimental { // pop an event off the pool, if that fails, create a new one while (!free_lists_[device].pop(event)) { + // Set correct device in case if required + int original_device = -1; + check_cuda_error(cudaGetDevice(&original_device)); + if (original_device != device) + check_cuda_error(cudaSetDevice(device)); add_event_to_pool(device); + // reset to original device if required + if (original_device != device) + check_cuda_error(cudaSetDevice(original_device)); } return true; }