driver_api_stereo_multi sample reworked to use parallel_for_ instead of parallel_do
This commit is contained in:
		| @@ -13,25 +13,12 @@ | |||||||
| #include "opencv2/highgui/highgui.hpp" | #include "opencv2/highgui/highgui.hpp" | ||||||
| #include "opencv2/gpu/gpu.hpp" | #include "opencv2/gpu/gpu.hpp" | ||||||
|  |  | ||||||
| #if !defined(HAVE_CUDA) || !defined(HAVE_TBB) || defined(__arm__) | #if defined(__arm__) | ||||||
|  |  | ||||||
| int main() | 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; |     std::cout << "Unsupported for ARM CUDA library." << std::endl; | ||||||
| #endif |  | ||||||
|  |  | ||||||
|     return 0; |     return 0; | ||||||
| } | } | ||||||
|  |  | ||||||
| #else | #else | ||||||
|  |  | ||||||
| #include <cuda.h> | #include <cuda.h> | ||||||
| @@ -42,7 +29,6 @@ using namespace std; | |||||||
| using namespace cv; | using namespace cv; | ||||||
| using namespace cv::gpu; | using namespace cv::gpu; | ||||||
|  |  | ||||||
| struct Worker { void operator()(int device_id) const; }; |  | ||||||
| void destroyContexts(); | void destroyContexts(); | ||||||
|  |  | ||||||
| #define safeCall(expr) safeCall_(expr, #expr, __FILE__, __LINE__) | #define safeCall(expr) safeCall_(expr, #expr, __FILE__, __LINE__) | ||||||
| @@ -77,6 +63,27 @@ GpuMat d_right[2]; | |||||||
| StereoBM_GPU* bm[2]; | StereoBM_GPU* bm[2]; | ||||||
| GpuMat d_result[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() | static void printHelp() | ||||||
| { | { | ||||||
|     std::cout << "Usage: driver_api_stereo_multi_gpu --left <left_image> --right <right_image>\n"; |     std::cout << "Usage: driver_api_stereo_multi_gpu --left <left_image> --right <right_image>\n"; | ||||||
| @@ -162,8 +169,7 @@ int main(int argc, char** argv) | |||||||
|     contextOff(); |     contextOff(); | ||||||
|  |  | ||||||
|     // Execute calculation in two threads using two GPUs |     // Execute calculation in two threads using two GPUs | ||||||
|     int devices[] = {0, 1}; |     parallel_for_(cv::Range(0, 2, Worker()); | ||||||
|     parallel_do(devices, devices + 2, Worker()); |  | ||||||
|  |  | ||||||
|     // Release the first GPU resources |     // Release the first GPU resources | ||||||
|     contextOn(0); |     contextOn(0); | ||||||
| @@ -188,21 +194,6 @@ int main(int argc, char** argv) | |||||||
|     return 0; |     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() | void destroyContexts() | ||||||
| { | { | ||||||
|     safeCall(cuCtxDestroy(contexts[0])); |     safeCall(cuCtxDestroy(contexts[0])); | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user
	 Alexander Smorkalov
					Alexander Smorkalov