2010-07-14 23:55:16 +08:00
|
|
|
/*M///////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
|
|
|
|
//
|
|
|
|
// By downloading, copying, installing or using the software you agree to this license.
|
|
|
|
// If you do not agree to this license, do not download, install,
|
|
|
|
// copy or use the software.
|
|
|
|
//
|
|
|
|
//
|
|
|
|
// License Agreement
|
|
|
|
// For Open Source Computer Vision Library
|
|
|
|
//
|
|
|
|
// Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
|
|
|
|
// Copyright (C) 2009, Willow Garage Inc., all rights reserved.
|
|
|
|
// Third party copyrights are property of their respective owners.
|
|
|
|
//
|
|
|
|
// Redistribution and use in source and binary forms, with or without modification,
|
|
|
|
// are permitted provided that the following conditions are met:
|
|
|
|
//
|
|
|
|
// * Redistribution's of source code must retain the above copyright notice,
|
|
|
|
// this list of conditions and the following disclaimer.
|
|
|
|
//
|
|
|
|
// * Redistribution's in binary form must reproduce the above copyright notice,
|
|
|
|
// this list of conditions and the following disclaimer in the documentation
|
|
|
|
// and/or other materials provided with the distribution.
|
|
|
|
//
|
|
|
|
// * The name of the copyright holders may not be used to endorse or promote products
|
|
|
|
// derived from this software without specific prior written permission.
|
|
|
|
//
|
|
|
|
// This software is provided by the copyright holders and contributors "as is" and
|
|
|
|
// any express or implied warranties, including, but not limited to, the implied
|
|
|
|
// warranties of merchantability and fitness for a particular purpose are disclaimed.
|
|
|
|
// In no event shall the Intel Corporation or contributors be liable for any direct,
|
|
|
|
// indirect, incidental, special, exemplary, or consequential damages
|
|
|
|
// (including, but not limited to, procurement of substitute goods or services;
|
|
|
|
// loss of use, data, or profits; or business interruption) however caused
|
|
|
|
// and on any theory of liability, whether in contract, strict liability,
|
|
|
|
// or tort (including negligence or otherwise) arising in any way out of
|
|
|
|
// the use of this software, even if advised of the possibility of such damage.
|
|
|
|
//
|
|
|
|
//M*/
|
|
|
|
|
|
|
|
#include "precomp.hpp"
|
|
|
|
|
|
|
|
using namespace cv;
|
|
|
|
using namespace cv::gpu;
|
|
|
|
|
2010-07-19 17:31:12 +08:00
|
|
|
|
2011-01-27 20:17:56 +08:00
|
|
|
namespace
|
|
|
|
{
|
|
|
|
template <typename Comparer>
|
2011-01-28 19:59:26 +08:00
|
|
|
bool compareToSet(const std::string& set_as_str, int value, Comparer cmp)
|
2011-01-27 20:17:56 +08:00
|
|
|
{
|
2011-01-28 19:59:26 +08:00
|
|
|
if (set_as_str.find_first_not_of(" ") == string::npos)
|
2011-01-27 20:17:56 +08:00
|
|
|
return false;
|
|
|
|
|
2011-01-28 19:59:26 +08:00
|
|
|
std::stringstream stream(set_as_str);
|
|
|
|
int cur_value;
|
2011-01-27 20:17:56 +08:00
|
|
|
|
|
|
|
while (!stream.eof())
|
|
|
|
{
|
2011-01-28 19:59:26 +08:00
|
|
|
stream >> cur_value;
|
|
|
|
if (cmp(cur_value, value))
|
2011-01-27 20:17:56 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
CV_EXPORTS bool cv::gpu::TargetArchs::builtWith(cv::gpu::GpuFeature feature)
|
|
|
|
{
|
|
|
|
if (feature == NATIVE_DOUBLE)
|
2011-02-01 20:28:39 +08:00
|
|
|
return ::compareToSet(CUDA_ARCH_FEATURES, 13, std::greater_equal<int>());
|
2011-01-27 20:17:56 +08:00
|
|
|
if (feature == ATOMICS)
|
2011-02-01 20:28:39 +08:00
|
|
|
return ::compareToSet(CUDA_ARCH_FEATURES, 11, std::greater_equal<int>());
|
2011-01-27 20:17:56 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
CV_EXPORTS bool cv::gpu::TargetArchs::has(int major, int minor)
|
|
|
|
{
|
|
|
|
return hasPtx(major, minor) || hasBin(major, minor);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
CV_EXPORTS bool cv::gpu::TargetArchs::hasPtx(int major, int minor)
|
|
|
|
{
|
2011-01-28 19:59:26 +08:00
|
|
|
return ::compareToSet(CUDA_ARCH_PTX, major * 10 + minor, std::equal_to<int>());
|
2011-01-27 20:17:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
CV_EXPORTS bool cv::gpu::TargetArchs::hasBin(int major, int minor)
|
|
|
|
{
|
2011-01-28 19:59:26 +08:00
|
|
|
return ::compareToSet(CUDA_ARCH_BIN, major * 10 + minor, std::equal_to<int>());
|
2011-01-27 20:17:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
CV_EXPORTS bool cv::gpu::TargetArchs::hasEqualOrLessPtx(int major, int minor)
|
|
|
|
{
|
2011-01-28 19:59:26 +08:00
|
|
|
return ::compareToSet(CUDA_ARCH_PTX, major * 10 + minor,
|
2011-01-27 20:17:56 +08:00
|
|
|
std::less_equal<int>());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
CV_EXPORTS bool cv::gpu::TargetArchs::hasEqualOrGreater(int major, int minor)
|
|
|
|
{
|
|
|
|
return hasEqualOrGreaterPtx(major, minor) ||
|
|
|
|
hasEqualOrGreaterBin(major, minor);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
CV_EXPORTS bool cv::gpu::TargetArchs::hasEqualOrGreaterPtx(int major, int minor)
|
|
|
|
{
|
2011-01-28 19:59:26 +08:00
|
|
|
return ::compareToSet(CUDA_ARCH_PTX, major * 10 + minor,
|
2011-01-27 20:17:56 +08:00
|
|
|
std::greater_equal<int>());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
CV_EXPORTS bool cv::gpu::TargetArchs::hasEqualOrGreaterBin(int major, int minor)
|
|
|
|
{
|
2011-01-28 19:59:26 +08:00
|
|
|
return ::compareToSet(CUDA_ARCH_BIN, major * 10 + minor,
|
2011-01-27 20:17:56 +08:00
|
|
|
std::greater_equal<int>());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-07-19 17:31:12 +08:00
|
|
|
#if !defined (HAVE_CUDA)
|
2010-07-15 21:36:00 +08:00
|
|
|
|
|
|
|
CV_EXPORTS int cv::gpu::getCudaEnabledDeviceCount() { return 0; }
|
2011-01-28 19:59:26 +08:00
|
|
|
CV_EXPORTS void cv::gpu::setDevice(int) { throw_nogpu(); }
|
2010-07-19 17:31:12 +08:00
|
|
|
CV_EXPORTS int cv::gpu::getDevice() { throw_nogpu(); return 0; }
|
2011-01-28 19:59:26 +08:00
|
|
|
size_t cv::gpu::DeviceInfo::freeMemory() const { throw_nogpu(); return 0; }
|
|
|
|
size_t cv::gpu::DeviceInfo::totalMemory() const { throw_nogpu(); return 0; }
|
|
|
|
bool cv::gpu::DeviceInfo::has(cv::gpu::GpuFeature) const { throw_nogpu(); return false; }
|
|
|
|
bool cv::gpu::DeviceInfo::isCompatible() const { throw_nogpu(); return false; }
|
2011-01-28 20:30:08 +08:00
|
|
|
void cv::gpu::DeviceInfo::query() { throw_nogpu(); }
|
|
|
|
void cv::gpu::DeviceInfo::queryMemory(size_t&, size_t&) const { throw_nogpu(); }
|
2010-07-15 21:36:00 +08:00
|
|
|
|
2010-07-19 17:31:12 +08:00
|
|
|
#else /* !defined (HAVE_CUDA) */
|
2010-07-15 21:36:00 +08:00
|
|
|
|
2010-07-14 23:55:16 +08:00
|
|
|
CV_EXPORTS int cv::gpu::getCudaEnabledDeviceCount()
|
|
|
|
{
|
|
|
|
int count;
|
|
|
|
cudaSafeCall( cudaGetDeviceCount( &count ) );
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
2011-01-18 22:52:35 +08:00
|
|
|
|
2010-07-14 23:55:16 +08:00
|
|
|
CV_EXPORTS void cv::gpu::setDevice(int device)
|
|
|
|
{
|
|
|
|
cudaSafeCall( cudaSetDevice( device ) );
|
|
|
|
}
|
2011-01-18 22:52:35 +08:00
|
|
|
|
|
|
|
|
2010-07-19 17:31:12 +08:00
|
|
|
CV_EXPORTS int cv::gpu::getDevice()
|
|
|
|
{
|
|
|
|
int device;
|
|
|
|
cudaSafeCall( cudaGetDevice( &device ) );
|
|
|
|
return device;
|
|
|
|
}
|
2010-07-14 23:55:16 +08:00
|
|
|
|
2011-01-18 22:52:35 +08:00
|
|
|
|
2011-01-28 19:59:26 +08:00
|
|
|
size_t cv::gpu::DeviceInfo::freeMemory() const
|
2010-07-15 17:13:15 +08:00
|
|
|
{
|
2011-01-28 19:59:26 +08:00
|
|
|
size_t free_memory, total_memory;
|
|
|
|
queryMemory(free_memory, total_memory);
|
|
|
|
return free_memory;
|
2010-07-14 23:55:16 +08:00
|
|
|
}
|
|
|
|
|
2011-01-18 22:52:35 +08:00
|
|
|
|
2011-01-28 19:59:26 +08:00
|
|
|
size_t cv::gpu::DeviceInfo::totalMemory() const
|
2010-07-14 23:55:16 +08:00
|
|
|
{
|
2011-01-28 19:59:26 +08:00
|
|
|
size_t free_memory, total_memory;
|
|
|
|
queryMemory(free_memory, total_memory);
|
|
|
|
return total_memory;
|
2010-07-15 21:36:00 +08:00
|
|
|
}
|
|
|
|
|
2010-08-07 01:02:06 +08:00
|
|
|
|
2011-01-28 19:59:26 +08:00
|
|
|
bool cv::gpu::DeviceInfo::has(cv::gpu::GpuFeature feature) const
|
2010-08-07 01:02:06 +08:00
|
|
|
{
|
2011-01-28 19:59:26 +08:00
|
|
|
if (feature == NATIVE_DOUBLE)
|
|
|
|
return major() > 1 || (major() == 1 && minor() >= 3);
|
|
|
|
if (feature == ATOMICS)
|
|
|
|
return major() > 1 || (major() == 1 && minor() >= 1);
|
|
|
|
return false;
|
2010-08-07 01:02:06 +08:00
|
|
|
}
|
|
|
|
|
2011-01-18 22:52:35 +08:00
|
|
|
|
2011-01-28 19:59:26 +08:00
|
|
|
bool cv::gpu::DeviceInfo::isCompatible() const
|
2010-11-29 15:18:11 +08:00
|
|
|
{
|
2011-01-28 19:59:26 +08:00
|
|
|
// Check PTX compatibility
|
|
|
|
if (TargetArchs::hasEqualOrLessPtx(major(), minor()))
|
|
|
|
return true;
|
2010-11-29 15:18:11 +08:00
|
|
|
|
2011-01-28 19:59:26 +08:00
|
|
|
// Check BIN compatibility
|
|
|
|
for (int i = minor(); i >= 0; --i)
|
|
|
|
if (TargetArchs::hasBin(major(), i))
|
|
|
|
return true;
|
2011-01-18 22:52:35 +08:00
|
|
|
|
2011-01-28 19:59:26 +08:00
|
|
|
return false;
|
2010-11-29 15:18:11 +08:00
|
|
|
}
|
|
|
|
|
2011-01-18 22:52:35 +08:00
|
|
|
|
2011-01-28 19:59:26 +08:00
|
|
|
void cv::gpu::DeviceInfo::query()
|
2011-01-18 20:01:28 +08:00
|
|
|
{
|
2011-01-28 19:59:26 +08:00
|
|
|
cudaDeviceProp prop;
|
|
|
|
cudaSafeCall(cudaGetDeviceProperties(&prop, device_id_));
|
|
|
|
name_ = prop.name;
|
|
|
|
multi_processor_count_ = prop.multiProcessorCount;
|
|
|
|
major_ = prop.major;
|
|
|
|
minor_ = prop.minor;
|
|
|
|
}
|
2011-01-18 20:01:28 +08:00
|
|
|
|
|
|
|
|
2011-01-28 19:59:26 +08:00
|
|
|
void cv::gpu::DeviceInfo::queryMemory(size_t& free_memory, size_t& total_memory) const
|
|
|
|
{
|
|
|
|
int prev_device_id = getDevice();
|
|
|
|
if (prev_device_id != device_id_)
|
|
|
|
setDevice(device_id_);
|
2011-01-20 17:22:05 +08:00
|
|
|
|
2011-01-28 19:59:26 +08:00
|
|
|
cudaSafeCall(cudaMemGetInfo(&free_memory, &total_memory));
|
2011-01-20 17:22:05 +08:00
|
|
|
|
2011-01-28 19:59:26 +08:00
|
|
|
if (prev_device_id != device_id_)
|
|
|
|
setDevice(prev_device_id);
|
2011-01-18 20:01:28 +08:00
|
|
|
}
|
|
|
|
|
2010-07-19 17:31:12 +08:00
|
|
|
#endif
|
|
|
|
|