From d787b30379c9b251ba749fdc1cea3dff63766c96 Mon Sep 17 00:00:00 2001 From: castano Date: Mon, 1 Dec 2008 07:56:00 +0000 Subject: [PATCH] Select fastest CUDA device. Merged from trunk. --- ChangeLog | 2 +- src/nvtt/Compressor.cpp | 8 ++++++++ src/nvtt/cuda/CudaUtils.cpp | 29 +++++++++++++++++++++++++++++ src/nvtt/cuda/CudaUtils.h | 1 + 4 files changed, 39 insertions(+), 1 deletion(-) diff --git a/ChangeLog b/ChangeLog index f09e57d..fc19c80 100644 --- a/ChangeLog +++ b/ChangeLog @@ -3,7 +3,7 @@ NVIDIA Texture Tools version 2.0.5 * Detect mismatch between CUDA runtime and driver, and disable CUDA in that case. * Fix cmake files when compiling NVTT as a shared library. * When linking nvtt dynamically on unix, link all libraries dynamically. - * Merge fixes from trunk. + * Select fastest CUDA device. NVIDIA Texture Tools version 2.0.4 * Fix error in RGB format output; reported by jonsoh. See issue 49. diff --git a/src/nvtt/Compressor.cpp b/src/nvtt/Compressor.cpp index d070d77..d497e8a 100644 --- a/src/nvtt/Compressor.cpp +++ b/src/nvtt/Compressor.cpp @@ -211,6 +211,10 @@ Compressor::Compressor() : m(*new Compressor::Private()) if (m.cudaEnabled) { + // Select fastest CUDA device. + int device = cuda::getFastestDevice(); + cuda::setDevice(device); + m.cuda = new CudaCompressor(); if (!m.cuda->isValid()) @@ -237,6 +241,10 @@ void Compressor::enableCudaAcceleration(bool enable) if (m.cudaEnabled && m.cuda == NULL) { + // Select fastest CUDA device. + int device = cuda::getFastestDevice(); + cuda::setDevice(device); + m.cuda = new CudaCompressor(); if (!m.cuda->isValid()) diff --git a/src/nvtt/cuda/CudaUtils.cpp b/src/nvtt/cuda/CudaUtils.cpp index 6cbdb74..784527f 100644 --- a/src/nvtt/cuda/CudaUtils.cpp +++ b/src/nvtt/cuda/CudaUtils.cpp @@ -151,6 +151,35 @@ int nv::cuda::deviceCount() return 0; } +int nv::cuda::getFastestDevice() +{ + int max_gflops_device = 0; +#if defined HAVE_CUDA + int max_gflops = 0; + + const int device_count = deviceCount(); + int current_device = 0; + while (current_device < device_count) + { + cudaDeviceProp device_properties; + cudaGetDeviceProperties(&device_properties, current_device); + int gflops = device_properties.multiProcessorCount * device_properties.clockRate; + + if (device_properties.major != -1 && device_properties.minor != -1) + { + if( gflops > max_gflops ) + { + max_gflops = gflops; + max_gflops_device = current_device; + } + } + + current_device++; + } +#endif + return max_gflops_device; +} + /// Activate the given devices. bool nv::cuda::setDevice(int i) { diff --git a/src/nvtt/cuda/CudaUtils.h b/src/nvtt/cuda/CudaUtils.h index 5a5bdab..6965960 100644 --- a/src/nvtt/cuda/CudaUtils.h +++ b/src/nvtt/cuda/CudaUtils.h @@ -31,6 +31,7 @@ namespace nv { bool isHardwarePresent(); int deviceCount(); + int getFastestDevice(); bool setDevice(int i); };