From 65b9f3bc108850722a3be1e165e9ddeda4cab0d9 Mon Sep 17 00:00:00 2001 From: Alexey Spizhevoy Date: Thu, 27 Jan 2011 12:17:56 +0000 Subject: [PATCH] fixed TargetArchs implementation in case when HAVE_CUDA=false, added initial structure for multi_gpu sample --- modules/gpu/src/initialization.cpp | 165 ++++++++++++++--------------- samples/gpu/CMakeLists.txt | 4 + samples/gpu/multi_gpu.cpp | 34 ++++++ 3 files changed, 117 insertions(+), 86 deletions(-) create mode 100644 samples/gpu/multi_gpu.cpp diff --git a/modules/gpu/src/initialization.cpp b/modules/gpu/src/initialization.cpp index a1b7fcd763..cab5f626c0 100644 --- a/modules/gpu/src/initialization.cpp +++ b/modules/gpu/src/initialization.cpp @@ -46,6 +46,85 @@ using namespace cv; using namespace cv::gpu; +namespace +{ + template + bool compare(const std::string& str, int x, Comparer cmp) + { + if (str.find_first_not_of(" ") == string::npos) + return false; + + std::stringstream stream(str); + int val; + + while (!stream.eof()) + { + stream >> val; + if (cmp(val, x)) + return true; + } + + return false; + } +} + + +CV_EXPORTS bool cv::gpu::TargetArchs::builtWith(cv::gpu::GpuFeature feature) +{ + if (feature == NATIVE_DOUBLE) + return hasEqualOrGreater(1, 3); + if (feature == ATOMICS) + return hasEqualOrGreater(1, 1); + 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) +{ + return ::compare(CUDA_ARCH_PTX, major * 10 + minor, std::equal_to()); +} + + +CV_EXPORTS bool cv::gpu::TargetArchs::hasBin(int major, int minor) +{ + return ::compare(CUDA_ARCH_BIN, major * 10 + minor, std::equal_to()); +} + + +CV_EXPORTS bool cv::gpu::TargetArchs::hasEqualOrLessPtx(int major, int minor) +{ + return ::compare(CUDA_ARCH_PTX, major * 10 + minor, + std::less_equal()); +} + + +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) +{ + return ::compare(CUDA_ARCH_PTX, major * 10 + minor, + std::greater_equal()); +} + + +CV_EXPORTS bool cv::gpu::TargetArchs::hasEqualOrGreaterBin(int major, int minor) +{ + return ::compare(CUDA_ARCH_BIN, major * 10 + minor, + std::greater_equal()); +} + + #if !defined (HAVE_CUDA) CV_EXPORTS int cv::gpu::getCudaEnabledDeviceCount() { return 0; } @@ -57,13 +136,6 @@ CV_EXPORTS int cv::gpu::getNumberOfSMs(int /*device*/) { throw_nogpu(); return 0 CV_EXPORTS void cv::gpu::getGpuMemInfo(size_t& /*free*/, size_t& /*total*/) { throw_nogpu(); } CV_EXPORTS bool cv::gpu::hasNativeDoubleSupport(int /*device*/) { throw_nogpu(); return false; } CV_EXPORTS bool cv::gpu::hasAtomicsSupport(int /*device*/) { throw_nogpu(); return false; } -CV_EXPORTS bool cv::gpu::hasPtxVersion(int major, int minor) { throw_nogpu(); return false; } -CV_EXPORTS bool cv::gpu::hasLessOrEqualPtxVersion(int major, int minor) { return false; } -CV_EXPORTS bool cv::gpu::hasGreaterOrEqualPtxVersion(int major, int minor) { return false; } -CV_EXPORTS bool cv::gpu::hasCubinVersion(int major, int minor) { return false; } -CV_EXPORTS bool cv::gpu::hasGreaterOrEqualCubinVersion(int major, int minor) { return false; } -CV_EXPORTS bool cv::gpu::hasVersion(int major, int minor) { return false; } -CV_EXPORTS bool cv::gpu::hasGreaterOrEqualVersion(int major, int minor) { return false; } CV_EXPORTS bool cv::gpu::isCompatibleWith(int device) { throw_nogpu(); return false; } @@ -139,85 +211,6 @@ CV_EXPORTS bool cv::gpu::hasAtomicsSupport(int device) } -namespace -{ - template - bool compare(const std::string& str, int x, Comparer cmp) - { - if (str.find_first_not_of(" ") == string::npos) - return false; - - std::stringstream stream(str); - int val; - - while (!stream.eof()) - { - stream >> val; - if (cmp(val, x)) - return true; - } - - return false; - } -} - - -CV_EXPORTS bool cv::gpu::TargetArchs::builtWith(cv::gpu::GpuFeature feature) -{ - if (feature == NATIVE_DOUBLE) - return hasEqualOrGreater(1, 3); - if (feature == ATOMICS) - return hasEqualOrGreater(1, 1); - 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) -{ - return ::compare(CUDA_ARCH_PTX, major * 10 + minor, std::equal_to()); -} - - -CV_EXPORTS bool cv::gpu::TargetArchs::hasBin(int major, int minor) -{ - return ::compare(CUDA_ARCH_BIN, major * 10 + minor, std::equal_to()); -} - - -CV_EXPORTS bool cv::gpu::TargetArchs::hasEqualOrLessPtx(int major, int minor) -{ - return ::compare(CUDA_ARCH_PTX, major * 10 + minor, - std::less_equal()); -} - - -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) -{ - return ::compare(CUDA_ARCH_PTX, major * 10 + minor, - std::greater_equal()); -} - - -CV_EXPORTS bool cv::gpu::TargetArchs::hasEqualOrGreaterBin(int major, int minor) -{ - return ::compare(CUDA_ARCH_BIN, major * 10 + minor, - std::greater_equal()); -} - - CV_EXPORTS bool cv::gpu::isCompatibleWith(int device) { // According to the CUDA C Programming Guide Version 3.2: "PTX code diff --git a/samples/gpu/CMakeLists.txt b/samples/gpu/CMakeLists.txt index c65c2e60b2..de9fe6ef80 100644 --- a/samples/gpu/CMakeLists.txt +++ b/samples/gpu/CMakeLists.txt @@ -15,6 +15,10 @@ if (BUILD_EXAMPLES) "${CMAKE_SOURCE_DIR}/modules/contrib/include" "${CMAKE_SOURCE_DIR}/modules/gpu/include" ) + + if(HAVE_CUDA) + include_directories(${CUDA_INCLUDE_DIRS}) + endif() if(CMAKE_COMPILER_IS_GNUCXX) set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wno-unused-function") diff --git a/samples/gpu/multi_gpu.cpp b/samples/gpu/multi_gpu.cpp new file mode 100644 index 0000000000..262f6e03f4 --- /dev/null +++ b/samples/gpu/multi_gpu.cpp @@ -0,0 +1,34 @@ +// Disable some warnings which are caused with CUDA headers +#pragma warning(disable: 4201 4408 4100) + +#include +#include +#include +#include + +#ifdef HAVE_CUDA +#include +#endif + +using namespace std; +using namespace cv; + +int main() +{ + bool can_run = true; + +#if !defined(HAVE_CUDA) + cout << "CUDA support is required (CMake key 'WITH_CUDA' must be true).\n"; + can_run = false; +#endif + +#if !defined(HAVE_TBB) + cout << "TBB support is required (CMake key 'WITH_TBB' must be true).\n"; + can_run = false; +#endif + + if (!can_run) + return -1; + + return 0; +} \ No newline at end of file