From 6ec10fc4ab657904cd2b679a9c9d457cd4890a93 Mon Sep 17 00:00:00 2001 From: Axel Garcia Date: Tue, 7 Jul 2026 15:50:40 +0200 Subject: [PATCH] ENH: Avoid repeated CUDA device selection queries CudaDataManager calls CudaGetMaxFlopsDev during construction, so iterative pipelines can repeatedly query CUDA device properties when many temporary CUDA images or filters are created. Compute the max-FLOPS device once with function-local static initialization and reuse the selected device for subsequent CudaGetMaxFlopsDev calls. This avoids repeated cudaGetDeviceProperties and cudaDeviceGetAttribute --- include/itkCudaUtil.h | 2 +- src/itkCudaUtil.cxx | 15 ++++++++++++--- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/include/itkCudaUtil.h b/include/itkCudaUtil.h index 380e0fe..e7217ce 100644 --- a/include/itkCudaUtil.h +++ b/include/itkCudaUtil.h @@ -55,7 +55,7 @@ GetCudaComputeCapability(int device); int CudaGetAvailableDevices(std::vector & devices); -/** Get the device that has the maximum FLOPS in the current context */ +/** Get the device that has the maximum FLOPS in the current context. The result is cached for future calls. */ int CudaGetMaxFlopsDev(); diff --git a/src/itkCudaUtil.cxx b/src/itkCudaUtil.cxx index bad205b..6f4fcdf 100644 --- a/src/itkCudaUtil.cxx +++ b/src/itkCudaUtil.cxx @@ -69,18 +69,19 @@ CudaGetAvailableDevices(std::vector & devices) } // -// Get the device that has the maximum FLOPS +// Compute the device that has the maximum FLOPS // int -CudaGetMaxFlopsDev() +ComputeMaxFlopsDevice() { std::vector devices; int numAvailableDevices = CudaGetAvailableDevices(devices); + if (numAvailableDevices == 0) { - return -1; } + int max_flops = 0; int max_flops_device = 0; for (int i = 0; i < numAvailableDevices; ++i) @@ -98,6 +99,14 @@ CudaGetMaxFlopsDev() return max_flops_device; } +int +CudaGetMaxFlopsDev() +{ + static const int max_flops_device = ComputeMaxFlopsDevice(); + + return max_flops_device; +} + std::pair GetCudaComputeCapability(int device)