1
0
mirror of https://github.com/opencv/opencv.git synced 2026-07-30 07:43:03 +04:00

core(parallel): plugins support

This commit is contained in:
Alexander Alekhin
2021-02-03 22:04:35 +00:00
parent e5d78960c6
commit cc73c36e32
35 changed files with 1509 additions and 354 deletions
@@ -27,6 +27,14 @@ Using this approach OpenCV provides some basic low level functionality for exter
#define CV_API_CALL
#endif
#ifndef CV_PLUGIN_EXPORTS
#if (defined _WIN32 || defined WINCE || defined __CYGWIN__)
# define CV_PLUGIN_EXPORTS __declspec(dllexport)
#elif defined __GNUC__ && __GNUC__ >= 4
# define CV_PLUGIN_EXPORTS __attribute__ ((visibility ("default")))
#endif
#endif
typedef enum cvResult
{
CV_ERROR_FAIL = -1, //!< Some error occurred (TODO Require to fill exception information)
@@ -19,9 +19,10 @@ namespace cv { namespace parallel {
*
* Applications can replace OpenCV `parallel_for()` backend with own implementation (to reuse Application's thread pool).
*
* @note This call is not thread-safe. Consider calling this function from the `main()` before any other OpenCV processing functions (and without any other created threads).
*
* #### Intel TBB usage example:
* ### Backend API usage examples
*
* #### Intel TBB
*
* - include header with simple implementation of TBB backend:
* @snippet parallel_backend/example-tbb.cpp tbb_include
@@ -29,13 +30,22 @@ namespace cv { namespace parallel {
* @snippet parallel_backend/example-tbb.cpp tbb_backend
* - configuration of compiler/linker options is responsibility of Application's scripts
*
* #### OpenMP usage example:
* #### OpenMP
*
* - include header with simple implementation of OpenMP backend:
* @snippet parallel_backend/example-openmp.cpp openmp_include
* - execute backend replacement code:
* @snippet parallel_backend/example-openmp.cpp openmp_backend
* - Configuration of compiler/linker options is responsibility of Application's scripts
*
*
* ### Plugins support
*
* Runtime configuration options:
* - change backend priority: `OPENCV_PARALLEL_PRIORITY_<backend>=9999`
* - disable backend: `OPENCV_PARALLEL_PRIORITY_<backend>=0`
* - specify list of backends with high priority (>100000): `OPENCV_PARALLEL_PRIORITY_LIST=TBB,OPENMP`. Unknown backends are registered as new plugins.
*
*/
/** Interface for parallel_for backends implementations
@@ -68,6 +78,12 @@ public:
*/
CV_EXPORTS void setParallelForBackend(const std::shared_ptr<ParallelForAPI>& api, bool propagateNumThreads = true);
/** @brief Change OpenCV parallel_for backend
*
* @note This call is not thread-safe. Consider calling this function from the `main()` before any other OpenCV processing functions (and without any other created threads).
*/
CV_EXPORTS_W bool setParallelForBackend(const std::string& backendName, bool propagateNumThreads = true);
//! @}
}} // namespace
#endif // OPENCV_CORE_PARALLEL_BACKEND_HPP
@@ -0,0 +1,163 @@
// This file is part of OpenCV project.
// It is subject to the license terms in the LICENSE file found in the top-level directory
// of this distribution and at http://opencv.org/license.html.
#ifndef OPENCV_UTILS_PLUGIN_LOADER_HPP
#define OPENCV_UTILS_PLUGIN_LOADER_HPP
#include "opencv2/core/utils/filesystem.hpp"
#include "opencv2/core/utils/filesystem.private.hpp"
#if OPENCV_HAVE_FILESYSTEM_SUPPORT
#if defined(_WIN32)
#include <windows.h>
#elif defined(__linux__) || defined(__APPLE__) || defined(__OpenBSD__) || defined(__FreeBSD__) || defined(__HAIKU__) || defined(__GLIBC__)
#include <dlfcn.h>
#endif
namespace cv { namespace plugin { namespace impl {
#if defined(_WIN32)
typedef HMODULE LibHandle_t;
typedef wchar_t FileSystemChar_t;
typedef std::wstring FileSystemPath_t;
// TODO wchar_t <=> UTF-8
static inline
FileSystemPath_t toFileSystemPath(const std::string& p)
{
FileSystemPath_t result;
result.resize(p.size());
for (size_t i = 0; i < p.size(); i++)
result[i] = (wchar_t)p[i];
return result;
}
// TODO wchar_t <=> UTF-8
static inline
std::string toPrintablePath(const FileSystemPath_t& p)
{
std::string result;
result.resize(p.size());
for (size_t i = 0; i < p.size(); i++)
{
wchar_t ch = p[i];
if ((int)ch >= ' ' && (int)ch < 128)
result[i] = (char)ch;
else
result[i] = '?';
}
return result;
}
#else // !_WIN32
typedef void* LibHandle_t;
typedef char FileSystemChar_t;
typedef std::string FileSystemPath_t;
static inline FileSystemPath_t toFileSystemPath(const std::string& p) { return p; }
static inline std::string toPrintablePath(const FileSystemPath_t& p) { return p; }
#endif
static inline
void* getSymbol_(LibHandle_t h, const char* symbolName)
{
#if defined(_WIN32)
return (void*)GetProcAddress(h, symbolName);
#elif defined(__linux__) || defined(__APPLE__) || defined(__OpenBSD__) || defined(__FreeBSD__) || defined(__HAIKU__) || defined(__GLIBC__)
return dlsym(h, symbolName);
#endif
}
static inline
LibHandle_t libraryLoad_(const FileSystemPath_t& filename)
{
#if defined(_WIN32)
# ifdef WINRT
return LoadPackagedLibrary(filename.c_str(), 0);
# else
return LoadLibraryW(filename.c_str());
#endif
#elif defined(__linux__) || defined(__APPLE__) || defined(__OpenBSD__) || defined(__FreeBSD__) || defined(__HAIKU__) || defined(__GLIBC__)
return dlopen(filename.c_str(), RTLD_LAZY);
#endif
}
static inline
void libraryRelease_(LibHandle_t h)
{
#if defined(_WIN32)
FreeLibrary(h);
#elif defined(__linux__) || defined(__APPLE__) || defined(__OpenBSD__) || defined(__FreeBSD__) || defined(__HAIKU__) || defined(__GLIBC__)
dlclose(h);
#endif
}
static inline
std::string libraryPrefix()
{
#if defined(_WIN32)
return "";
#else
return "lib";
#endif
}
static inline
std::string librarySuffix()
{
#if defined(_WIN32)
const char* suffix = ""
CVAUX_STR(CV_MAJOR_VERSION) CVAUX_STR(CV_MINOR_VERSION) CVAUX_STR(CV_SUBMINOR_VERSION)
#if (defined _MSC_VER && defined _M_X64) || (defined __GNUC__ && defined __x86_64__)
"_64"
#endif
#if defined(_DEBUG) && defined(DEBUG_POSTFIX)
CVAUX_STR(DEBUG_POSTFIX)
#endif
".dll";
return suffix;
#else
return ".so";
#endif
}
//============================
class CV_EXPORTS DynamicLib
{
private:
LibHandle_t handle;
const FileSystemPath_t fname;
bool disableAutoUnloading_;
public:
DynamicLib(const FileSystemPath_t& filename);
~DynamicLib();
/** Do not automatically unload library in destructor */
inline void disableAutomaticLibraryUnloading()
{
disableAutoUnloading_ = true;
}
inline bool isLoaded() const
{
return handle != NULL;
}
void* getSymbol(const char* symbolName) const;
const std::string getName() const;
private:
void libraryLoad(const FileSystemPath_t& filename);
void libraryRelease();
private:
DynamicLib(const DynamicLib &) = delete;
DynamicLib &operator=(const DynamicLib &) = delete;
};
}}} // namespace
#endif // OPENCV_HAVE_FILESYSTEM_SUPPORT
#endif // OPENCV_UTILS_PLUGIN_LOADER_HPP