core: parallel backends API
- allow to replace parallel_for() backend
This commit is contained in:
@@ -30,6 +30,7 @@ if(NOT HAVE_opencv_cudaarithm OR NOT HAVE_opencv_cudafilters)
|
||||
ocv_list_filterout(cpp_samples "/gpu/")
|
||||
endif()
|
||||
ocv_list_filterout(cpp_samples "real_time_pose_estimation/")
|
||||
ocv_list_filterout(cpp_samples "parallel_backend/")
|
||||
foreach(sample_filename ${cpp_samples})
|
||||
set(package "cpp")
|
||||
if(sample_filename MATCHES "tutorial_code")
|
||||
@@ -57,3 +58,11 @@ foreach(sample_filename ${cpp_samples})
|
||||
endforeach()
|
||||
|
||||
include("tutorial_code/calib3d/real_time_pose_estimation/CMakeLists.txt" OPTIONAL)
|
||||
|
||||
# Standalone samples only
|
||||
if(OpenCV_FOUND AND NOT CMAKE_VERSION VERSION_LESS "3.1")
|
||||
add_subdirectory("example_cmake")
|
||||
endif()
|
||||
if(OpenCV_FOUND AND NOT CMAKE_VERSION VERSION_LESS "3.9")
|
||||
add_subdirectory("tutorial_code/core/parallel_backend")
|
||||
endif()
|
||||
|
||||
@@ -0,0 +1,26 @@
|
||||
cmake_minimum_required(VERSION 3.9)
|
||||
|
||||
find_package(OpenCV REQUIRED COMPONENTS opencv_core)
|
||||
|
||||
find_package(OpenMP)
|
||||
if(OpenMP_FOUND)
|
||||
project(opencv_example_openmp_backend)
|
||||
add_executable(opencv_example_openmp_backend example-openmp.cpp)
|
||||
target_link_libraries(opencv_example_openmp_backend PRIVATE
|
||||
opencv_core
|
||||
OpenMP::OpenMP_CXX
|
||||
)
|
||||
endif()
|
||||
|
||||
# TODO: find_package(TBB)
|
||||
find_path(TBB_INCLUDE_DIR NAMES "tbb/tbb.h")
|
||||
find_library(TBB_LIBRARY NAMES "tbb")
|
||||
if(TBB_INCLUDE_DIR AND TBB_LIBRARY AND NOT OPENCV_EXAMPLE_SKIP_TBB)
|
||||
project(opencv_example_tbb_backend)
|
||||
add_executable(opencv_example_tbb_backend example-tbb.cpp)
|
||||
target_include_directories(opencv_example_tbb_backend SYSTEM PRIVATE ${TBB_INCLUDE_DIR})
|
||||
target_link_libraries(opencv_example_tbb_backend PRIVATE
|
||||
opencv_core
|
||||
${TBB_LIBRARY}
|
||||
)
|
||||
endif()
|
||||
@@ -0,0 +1,44 @@
|
||||
#include "opencv2/core.hpp"
|
||||
#include <iostream>
|
||||
|
||||
#include <chrono>
|
||||
#include <thread>
|
||||
|
||||
//! [openmp_include]
|
||||
#include "opencv2/core/parallel/backend/parallel_for.openmp.hpp"
|
||||
//! [openmp_include]
|
||||
|
||||
namespace cv { // private.hpp
|
||||
CV_EXPORTS const char* currentParallelFramework();
|
||||
}
|
||||
|
||||
static
|
||||
std::string currentParallelFrameworkSafe()
|
||||
{
|
||||
const char* framework = cv::currentParallelFramework();
|
||||
if (framework)
|
||||
return framework;
|
||||
return std::string();
|
||||
}
|
||||
|
||||
using namespace cv;
|
||||
int main()
|
||||
{
|
||||
std::cout << "OpenCV builtin parallel framework: '" << currentParallelFrameworkSafe() << "' (nthreads=" << getNumThreads() << ")" << std::endl;
|
||||
|
||||
//! [openmp_backend]
|
||||
//omp_set_dynamic(1);
|
||||
cv::parallel::setParallelForBackend(std::make_shared<cv::parallel::openmp::ParallelForBackend>());
|
||||
//! [openmp_backend]
|
||||
|
||||
std::cout << "New parallel backend: '" << currentParallelFrameworkSafe() << "'" << "' (nthreads=" << getNumThreads() << ")" << std::endl;
|
||||
|
||||
parallel_for_(Range(0, 20), [&](const Range range)
|
||||
{
|
||||
std::ostringstream out;
|
||||
out << "Thread " << getThreadNum() << "(opencv=" << utils::getThreadID() << "): range " << range.start << "-" << range.end << std::endl;
|
||||
std::cout << out.str() << std::flush;
|
||||
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(100));
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
#include "opencv2/core.hpp"
|
||||
#include <iostream>
|
||||
|
||||
#include <chrono>
|
||||
#include <thread>
|
||||
|
||||
//! [tbb_include]
|
||||
#include "opencv2/core/parallel/backend/parallel_for.tbb.hpp"
|
||||
//! [tbb_include]
|
||||
|
||||
namespace cv { // private.hpp
|
||||
CV_EXPORTS const char* currentParallelFramework();
|
||||
}
|
||||
|
||||
static
|
||||
std::string currentParallelFrameworkSafe()
|
||||
{
|
||||
const char* framework = cv::currentParallelFramework();
|
||||
if (framework)
|
||||
return framework;
|
||||
return std::string();
|
||||
}
|
||||
|
||||
using namespace cv;
|
||||
int main()
|
||||
{
|
||||
std::cout << "OpenCV builtin parallel framework: '" << currentParallelFrameworkSafe() << "' (nthreads=" << getNumThreads() << ")" << std::endl;
|
||||
|
||||
//! [tbb_backend]
|
||||
cv::parallel::setParallelForBackend(std::make_shared<cv::parallel::tbb::ParallelForBackend>());
|
||||
//! [tbb_backend]
|
||||
|
||||
std::cout << "New parallel backend: '" << currentParallelFrameworkSafe() << "'" << "' (nthreads=" << getNumThreads() << ")" << std::endl;
|
||||
|
||||
parallel_for_(Range(0, 20), [&](const Range range)
|
||||
{
|
||||
std::ostringstream out;
|
||||
out << "Thread " << getThreadNum() << "(opencv=" << utils::getThreadID() << "): range " << range.start << "-" << range.end << std::endl;
|
||||
std::cout << out.str() << std::flush;
|
||||
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(100));
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user