From c0d76ef9841e66dc50590e0b9b21541cddb9af97 Mon Sep 17 00:00:00 2001 From: Alexander Smorkalov Date: Tue, 13 Jan 2015 13:00:06 +0300 Subject: [PATCH] driver_api_stereo_multi sample reworked to use parallel_for_ instead of parallel_do --- samples/gpu/driver_api_stereo_multi.cpp | 55 +++++++++++-------------- 1 file changed, 23 insertions(+), 32 deletions(-) diff --git a/samples/gpu/driver_api_stereo_multi.cpp b/samples/gpu/driver_api_stereo_multi.cpp index d4d0af451..c02348cf0 100644 --- a/samples/gpu/driver_api_stereo_multi.cpp +++ b/samples/gpu/driver_api_stereo_multi.cpp @@ -13,25 +13,12 @@ #include "opencv2/highgui/highgui.hpp" #include "opencv2/gpu/gpu.hpp" -#if !defined(HAVE_CUDA) || !defined(HAVE_TBB) || defined(__arm__) - +#if defined(__arm__) int main() { -#if !defined(HAVE_CUDA) - std::cout << "CUDA support is required (CMake key 'WITH_CUDA' must be true).\n"; -#endif - -#if !defined(HAVE_TBB) - std::cout << "TBB support is required (CMake key 'WITH_TBB' must be true).\n"; -#endif - -#if defined(__arm__) std::cout << "Unsupported for ARM CUDA library." << std::endl; -#endif - return 0; } - #else #include @@ -42,7 +29,6 @@ using namespace std; using namespace cv; using namespace cv::gpu; -struct Worker { void operator()(int device_id) const; }; void destroyContexts(); #define safeCall(expr) safeCall_(expr, #expr, __FILE__, __LINE__) @@ -77,6 +63,27 @@ GpuMat d_right[2]; StereoBM_GPU* bm[2]; GpuMat d_result[2]; + +struct Worker: public ParallelLoopBody +{ + virtual void operator() (const Range& range) const + { + for (int device_id = range.start; device_id != range.end; ++device_id) + { + contextOn(device_id); + + bm[device_id]->operator()(d_left[device_id], d_right[device_id], + d_result[device_id]); + + std::cout << "GPU #" << device_id << " (" << DeviceInfo().name() + << "): finished\n"; + + contextOff(); + + } + } +}; + static void printHelp() { std::cout << "Usage: driver_api_stereo_multi_gpu --left --right \n"; @@ -162,8 +169,7 @@ int main(int argc, char** argv) contextOff(); // Execute calculation in two threads using two GPUs - int devices[] = {0, 1}; - parallel_do(devices, devices + 2, Worker()); + parallel_for_(cv::Range(0, 2, Worker()); // Release the first GPU resources contextOn(0); @@ -188,21 +194,6 @@ int main(int argc, char** argv) return 0; } - -void Worker::operator()(int device_id) const -{ - contextOn(device_id); - - bm[device_id]->operator()(d_left[device_id], d_right[device_id], - d_result[device_id]); - - std::cout << "GPU #" << device_id << " (" << DeviceInfo().name() - << "): finished\n"; - - contextOff(); -} - - void destroyContexts() { safeCall(cuCtxDestroy(contexts[0]));