From e02418e9048480a2a12df29138a6bded1230610a Mon Sep 17 00:00:00 2001 From: Alexander Smorkalov Date: Tue, 30 Dec 2014 16:22:56 +0300 Subject: [PATCH] parallel_do replaced by parallel_for_ in gpu/multi.cpp sample to get rid of cvconfig.h --- samples/gpu/brox_optical_flow.cpp | 1 - samples/gpu/multi.cpp | 84 +++++++++++++++----------------------- samples/gpu/pyrlk_optical_flow.cpp | 1 - 3 files changed, 32 insertions(+), 54 deletions(-) diff --git a/samples/gpu/brox_optical_flow.cpp b/samples/gpu/brox_optical_flow.cpp index 7cd5089..ecbd4d9 100644 --- a/samples/gpu/brox_optical_flow.cpp +++ b/samples/gpu/brox_optical_flow.cpp @@ -3,7 +3,6 @@ #include #include -#include "cvconfig.h" #include "opencv2/core/core.hpp" #include "opencv2/highgui/highgui.hpp" #include "opencv2/gpu/gpu.hpp" diff --git a/samples/gpu/multi.cpp b/samples/gpu/multi.cpp index c3925fc..96ad588 100644 --- a/samples/gpu/multi.cpp +++ b/samples/gpu/multi.cpp @@ -7,34 +7,47 @@ #endif #include -#include "cvconfig.h" #include "opencv2/core/core.hpp" #include "opencv2/gpu/gpu.hpp" -#if !defined(HAVE_CUDA) || !defined(HAVE_TBB) +using namespace std; +using namespace cv; +using namespace cv::gpu; -int main() +struct Worker: public ParallelLoopBody { -#if !defined(HAVE_CUDA) - std::cout << "CUDA support is required (CMake key 'WITH_CUDA' must be true).\n"; -#endif + virtual void operator() (const Range& range) const + { + for (int device_id = range.start; device_id != range.end; ++device_id) + { + setDevice(device_id); -#if !defined(HAVE_TBB) - std::cout << "TBB support is required (CMake key 'WITH_TBB' must be true).\n"; -#endif + Mat src(1000, 1000, CV_32F); + Mat dst; - return 0; -} + RNG rng(0); + rng.fill(src, RNG::UNIFORM, 0, 1); -#else + // CPU works + transpose(src, dst); -#include "opencv2/core/internal.hpp" // For TBB wrappers + // GPU works + GpuMat d_src(src); + GpuMat d_dst; + transpose(d_src, d_dst); -using namespace std; -using namespace cv; -using namespace cv::gpu; + // Check results + bool passed = norm(dst - Mat(d_dst), NORM_INF) < 1e-3; + std::cout << "GPU #" << device_id << " (" << DeviceInfo().name() << "): " + << (passed ? "passed" : "FAILED") << endl; -struct Worker { void operator()(int device_id) const; }; + // Deallocate data here, otherwise deallocation will be performed + // after context is extracted from the stack + d_src.release(); + d_dst.release(); + } + } +}; int main() { @@ -58,41 +71,8 @@ int main() } } - // Execute calculation in two threads using two GPUs - int devices[] = {0, 1}; - parallel_do(devices, devices + 2, Worker()); + // Execute calculation in several threads, 1 GPU per thread + parallel_for_(cv::Range(0, num_devices, Worker()); return 0; } - - -void Worker::operator()(int device_id) const -{ - setDevice(device_id); - - Mat src(1000, 1000, CV_32F); - Mat dst; - - RNG rng(0); - rng.fill(src, RNG::UNIFORM, 0, 1); - - // CPU works - transpose(src, dst); - - // GPU works - GpuMat d_src(src); - GpuMat d_dst; - transpose(d_src, d_dst); - - // Check results - bool passed = norm(dst - Mat(d_dst), NORM_INF) < 1e-3; - std::cout << "GPU #" << device_id << " (" << DeviceInfo().name() << "): " - << (passed ? "passed" : "FAILED") << endl; - - // Deallocate data here, otherwise deallocation will be performed - // after context is extracted from the stack - d_src.release(); - d_dst.release(); -} - -#endif diff --git a/samples/gpu/pyrlk_optical_flow.cpp b/samples/gpu/pyrlk_optical_flow.cpp index 5ccd660..1c3eb20 100644 --- a/samples/gpu/pyrlk_optical_flow.cpp +++ b/samples/gpu/pyrlk_optical_flow.cpp @@ -1,7 +1,6 @@ #include #include -#include "cvconfig.h" #include "opencv2/core/core.hpp" #include "opencv2/imgproc/imgproc.hpp" #include "opencv2/highgui/highgui.hpp" -- 2.7.4