Do not use cuda API when CUDA not found.

Fix end of lines.
This commit is contained in:
castano 2008-11-10 21:54:03 +00:00
parent 36ba75b598
commit 1c5da0e341

View File

@ -1,163 +1,163 @@
// Copyright NVIDIA Corporation 2007 -- Ignacio Castano <icastano@nvidia.com> // Copyright NVIDIA Corporation 2007 -- Ignacio Castano <icastano@nvidia.com>
// //
// Permission is hereby granted, free of charge, to any person // Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation // obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without // files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use, // restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell // copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the // copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following // Software is furnished to do so, subject to the following
// conditions: // conditions:
// //
// The above copyright notice and this permission notice shall be // The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software. // included in all copies or substantial portions of the Software.
// //
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND // OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING // WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE. // OTHER DEALINGS IN THE SOFTWARE.
#include <nvcore/Debug.h> #include <nvcore/Debug.h>
#include <nvcore/Library.h> #include <nvcore/Library.h>
#include "CudaUtils.h" #include "CudaUtils.h"
#if defined HAVE_CUDA #if defined HAVE_CUDA
#include <cuda_runtime.h> #include <cuda_runtime.h>
#endif #endif
using namespace nv; using namespace nv;
using namespace cuda; using namespace cuda;
#if NV_OS_WIN32 #if NV_OS_WIN32
#define WINDOWS_LEAN_AND_MEAN #define WINDOWS_LEAN_AND_MEAN
#include <windows.h> #include <windows.h>
static bool isWindowsVista() static bool isWindowsVista()
{ {
OSVERSIONINFO osvi; OSVERSIONINFO osvi;
osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO); osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
::GetVersionEx(&osvi); ::GetVersionEx(&osvi);
return osvi.dwMajorVersion >= 6; return osvi.dwMajorVersion >= 6;
} }
typedef BOOL (WINAPI *LPFN_ISWOW64PROCESS) (HANDLE, PBOOL); typedef BOOL (WINAPI *LPFN_ISWOW64PROCESS) (HANDLE, PBOOL);
static bool isWow32() static bool isWow32()
{ {
LPFN_ISWOW64PROCESS fnIsWow64Process = (LPFN_ISWOW64PROCESS)GetProcAddress(GetModuleHandle("kernel32"), "IsWow64Process"); LPFN_ISWOW64PROCESS fnIsWow64Process = (LPFN_ISWOW64PROCESS)GetProcAddress(GetModuleHandle("kernel32"), "IsWow64Process");
BOOL bIsWow64 = FALSE; BOOL bIsWow64 = FALSE;
if (NULL != fnIsWow64Process) if (NULL != fnIsWow64Process)
{ {
if (!fnIsWow64Process(GetCurrentProcess(), &bIsWow64)) if (!fnIsWow64Process(GetCurrentProcess(), &bIsWow64))
{ {
// Assume 32 bits. // Assume 32 bits.
return true; return true;
} }
} }
return !bIsWow64; return !bIsWow64;
} }
#endif #endif
static bool isCudaDriverAvailable(uint version) static bool isCudaDriverAvailable(uint version)
{ {
#if NV_OS_WIN32 #if NV_OS_WIN32
Library nvcuda("nvcuda.dll"); Library nvcuda("nvcuda.dll");
#else #else
Library nvcuda(NV_LIBRARY_NAME("cuda")); Library nvcuda(NV_LIBRARY_NAME("cuda"));
#endif #endif
if (!nvcuda.isValid()) if (!nvcuda.isValid())
{ {
return false; return false;
} }
if (version > 2000) if (version > 2000)
{ {
void * address = nvcuda.bindSymbol("cuStreamCreate"); void * address = nvcuda.bindSymbol("cuStreamCreate");
if (address == NULL) return false; if (address == NULL) return false;
} }
if (version > 2010) if (version > 2010)
{ {
void * address = nvcuda.bindSymbol("cuLoadDataEx"); void * address = nvcuda.bindSymbol("cuLoadDataEx");
if (address == NULL) return false; if (address == NULL) return false;
} }
return true; return true;
} }
/// Determine if CUDA is available. /// Determine if CUDA is available.
bool nv::cuda::isHardwarePresent() bool nv::cuda::isHardwarePresent()
{ {
#if defined HAVE_CUDA #if defined HAVE_CUDA
#if NV_OS_WIN32 #if NV_OS_WIN32
//if (isWindowsVista()) return false; //if (isWindowsVista()) return false;
//if (isWindowsVista() || !isWow32()) return false; //if (isWindowsVista() || !isWow32()) return false;
#endif #endif
int count = deviceCount(); int count = deviceCount();
if (count == 1) if (count == 1)
{ {
// Make sure it's not an emulation device. // Make sure it's not an emulation device.
cudaDeviceProp deviceProp; cudaDeviceProp deviceProp;
cudaGetDeviceProperties(&deviceProp, 0); cudaGetDeviceProperties(&deviceProp, 0);
// deviceProp.name != Device Emulation (CPU) // deviceProp.name != Device Emulation (CPU)
if (deviceProp.major == -1 || deviceProp.minor == -1) if (deviceProp.major == -1 || deviceProp.minor == -1)
{ {
return false; return false;
} }
// Make sure that CUDA driver matches CUDA runtime. // Make sure that CUDA driver matches CUDA runtime.
if (!isCudaDriverAvailable(CUDART_VERSION)) if (!isCudaDriverAvailable(CUDART_VERSION))
{ {
return false; return false;
} }
// @@ Make sure that warp size == 32 // @@ Make sure that warp size == 32
} }
return count > 0; return count > 0;
#else #else
return false; return false;
#endif #endif
} }
/// Get number of CUDA enabled devices. /// Get number of CUDA enabled devices.
int nv::cuda::deviceCount() int nv::cuda::deviceCount()
{ {
#if defined HAVE_CUDA #if defined HAVE_CUDA
int gpuCount = 0; int gpuCount = 0;
cudaError_t result = cudaGetDeviceCount(&gpuCount); cudaError_t result = cudaGetDeviceCount(&gpuCount);
if (result == cudaSuccess) if (result == cudaSuccess)
{ {
return gpuCount; return gpuCount;
} }
#endif #endif
return 0; return 0;
} }
int nv::cuda::getFastestDevice() int nv::cuda::getFastestDevice()
{ {
const int device_count = deviceCount();
int max_gflops_device = 0; int max_gflops_device = 0;
#if defined HAVE_CUDA
int max_gflops = 0; int max_gflops = 0;
const int device_count = deviceCount();
int current_device = 0; int current_device = 0;
while (current_device < device_count) while (current_device < device_count)
{ {
@ -176,19 +176,19 @@ int nv::cuda::getFastestDevice()
current_device++; current_device++;
} }
#endif
return max_gflops_device; return max_gflops_device;
} }
/// Activate the given devices. /// Activate the given devices.
bool nv::cuda::setDevice(int i) bool nv::cuda::setDevice(int i)
{ {
nvCheck(i < deviceCount()); nvCheck(i < deviceCount());
#if defined HAVE_CUDA #if defined HAVE_CUDA
cudaError_t result = cudaSetDevice(i); cudaError_t result = cudaSetDevice(i);
return result == cudaSuccess; return result == cudaSuccess;
#else #else
return false; return false;
#endif #endif
} }