0002-fix-support-for-pthreads-parallel_for.patch 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. Backport from https://github.com/Itseez/opencv/commit/a482dcce464acbd5368fb93c6c3d52ba8401776a
  2. From a482dcce464acbd5368fb93c6c3d52ba8401776a Mon Sep 17 00:00:00 2001
  3. From: Alexander Alekhin <alexander.alekhin@itseez.com>
  4. Date: Thu, 11 Jun 2015 16:53:07 +0300
  5. Subject: [PATCH] fix support for pthreads parallel_for
  6. Signed-off-by: Samuel Martin <s.martin49@gmail.com>
  7. ---
  8. CMakeLists.txt | 29 ++++++++++++++++++++++-------
  9. cmake/OpenCVFindLibsPerf.cmake | 14 +++++++++-----
  10. cmake/templates/cvconfig.h.in | 6 ++++++
  11. modules/core/src/parallel.cpp | 13 ++++++++-----
  12. modules/core/src/parallel_pthreads.cpp | 2 +-
  13. modules/core/src/precomp.hpp | 6 ------
  14. 6 files changed, 46 insertions(+), 24 deletions(-)
  15. diff --git a/CMakeLists.txt b/CMakeLists.txt
  16. index d9a17b3..27d8470 100644
  17. --- a/CMakeLists.txt
  18. +++ b/CMakeLists.txt
  19. @@ -188,7 +188,7 @@ OCV_OPTION(WITH_QUICKTIME "Use QuickTime for Video I/O insted of QTKit" OFF
  20. OCV_OPTION(WITH_TBB "Include Intel TBB support" OFF IF (NOT IOS AND NOT WINRT) )
  21. OCV_OPTION(WITH_OPENMP "Include OpenMP support" OFF)
  22. OCV_OPTION(WITH_CSTRIPES "Include C= support" OFF IF (WIN32 AND NOT WINRT) )
  23. -OCV_OPTION(WITH_PTHREADS_PF "Use pthreads-based parallel_for" OFF IF (NOT WIN32) )
  24. +OCV_OPTION(WITH_PTHREADS_PF "Use pthreads-based parallel_for" ON IF (NOT WIN32) )
  25. OCV_OPTION(WITH_TIFF "Include TIFF support" ON IF (NOT IOS) )
  26. OCV_OPTION(WITH_UNICAP "Include Unicap support (GPL)" OFF IF (UNIX AND NOT APPLE AND NOT ANDROID) )
  27. OCV_OPTION(WITH_V4L "Include Video 4 Linux support" ON IF (UNIX AND NOT ANDROID) )
  28. @@ -1026,6 +1026,27 @@ if(DEFINED WITH_GPHOTO2)
  29. endif(DEFINED WITH_GPHOTO2)
  30. +# Order is similar to CV_PARALLEL_FRAMEWORK in core/src/parallel.cpp
  31. +ocv_clear_vars(CV_PARALLEL_FRAMEWORK)
  32. +if(HAVE_TBB)
  33. + set(CV_PARALLEL_FRAMEWORK "TBB (ver ${TBB_VERSION_MAJOR}.${TBB_VERSION_MINOR} interface ${TBB_INTERFACE_VERSION})")
  34. +elseif(HAVE_CSTRIPES)
  35. + set(CV_PARALLEL_FRAMEWORK "C=")
  36. +elseif(HAVE_OPENMP)
  37. + set(CV_PARALLEL_FRAMEWORK "OpenMP")
  38. +elseif(HAVE_GCD)
  39. + set(CV_PARALLEL_FRAMEWORK "GCD")
  40. +elseif(WINRT OR HAVE_CONCURRENCY)
  41. + set(CV_PARALLEL_FRAMEWORK "Concurrency")
  42. +elseif(HAVE_PTHREADS_PF)
  43. + set(CV_PARALLEL_FRAMEWORK "pthreads")
  44. +else()
  45. + set(CV_PARALLEL_FRAMEWORK "none")
  46. +endif()
  47. +status("")
  48. +status(" Parallel framework:" TRUE THEN "${CV_PARALLEL_FRAMEWORK}" ELSE NO)
  49. +
  50. +
  51. # ========================== Other third-party libraries ==========================
  52. status("")
  53. status(" Other third-party libraries:")
  54. @@ -1045,12 +1066,6 @@ status(" Use IPP Async:" HAVE_IPP_A THEN "YES" ELSE NO)
  55. endif(DEFINED WITH_IPP_A)
  56. status(" Use Eigen:" HAVE_EIGEN THEN "YES (ver ${EIGEN_WORLD_VERSION}.${EIGEN_MAJOR_VERSION}.${EIGEN_MINOR_VERSION})" ELSE NO)
  57. -status(" Use TBB:" HAVE_TBB THEN "YES (ver ${TBB_VERSION_MAJOR}.${TBB_VERSION_MINOR} interface ${TBB_INTERFACE_VERSION})" ELSE NO)
  58. -status(" Use OpenMP:" HAVE_OPENMP THEN YES ELSE NO)
  59. -status(" Use GCD" HAVE_GCD THEN YES ELSE NO)
  60. -status(" Use Concurrency" HAVE_CONCURRENCY THEN YES ELSE NO)
  61. -status(" Use C=:" HAVE_CSTRIPES THEN YES ELSE NO)
  62. -status(" Use pthreads for parallel for:" HAVE_PTHREADS_PF THEN YES ELSE NO)
  63. status(" Use Cuda:" HAVE_CUDA THEN "YES (ver ${CUDA_VERSION_STRING})" ELSE NO)
  64. status(" Use OpenCL:" HAVE_OPENCL THEN YES ELSE NO)
  65. diff --git a/cmake/OpenCVFindLibsPerf.cmake b/cmake/OpenCVFindLibsPerf.cmake
  66. index bda5d79..d1bc541 100644
  67. --- a/cmake/OpenCVFindLibsPerf.cmake
  68. +++ b/cmake/OpenCVFindLibsPerf.cmake
  69. @@ -120,12 +120,16 @@ if(WITH_OPENMP)
  70. set(HAVE_OPENMP "${OPENMP_FOUND}")
  71. endif()
  72. -if(UNIX OR ANDROID)
  73. -if(NOT APPLE AND NOT HAVE_TBB AND NOT HAVE_OPENMP)
  74. - set(HAVE_PTHREADS_PF 1)
  75. -else()
  76. - set(HAVE_PTHREADS_PF 0)
  77. +if(NOT MSVC AND NOT DEFINED HAVE_PTHREADS)
  78. + set(_fname "${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/pthread_test.cpp")
  79. + file(WRITE "${_fname}" "#include <pthread.h>\nint main() { (void)pthread_self(); return 0; }\n")
  80. + try_compile(HAVE_PTHREADS "${CMAKE_BINARY_DIR}" "${_fname}")
  81. + file(REMOVE "${_fname}")
  82. endif()
  83. +
  84. +ocv_clear_vars(HAVE_PTHREADS_PF)
  85. +if(WITH_PTHREADS_PF)
  86. + set(HAVE_PTHREADS_PF ${HAVE_PTHREADS})
  87. else()
  88. set(HAVE_PTHREADS_PF 0)
  89. endif()
  90. diff --git a/cmake/templates/cvconfig.h.in b/cmake/templates/cvconfig.h.in
  91. index 4a1d1c6..3330774 100644
  92. --- a/cmake/templates/cvconfig.h.in
  93. +++ b/cmake/templates/cvconfig.h.in
  94. @@ -139,6 +139,12 @@
  95. /* PNG codec */
  96. #cmakedefine HAVE_PNG
  97. +/* Posix threads (pthreads) */
  98. +#cmakedefine HAVE_PTHREADS
  99. +
  100. +/* parallel_for with pthreads */
  101. +#cmakedefine HAVE_PTHREADS_PF
  102. +
  103. /* Qt support */
  104. #cmakedefine HAVE_QT
  105. diff --git a/modules/core/src/parallel.cpp b/modules/core/src/parallel.cpp
  106. index 0b593ee..caa8129 100644
  107. --- a/modules/core/src/parallel.cpp
  108. +++ b/modules/core/src/parallel.cpp
  109. @@ -80,6 +80,7 @@
  110. 4. HAVE_GCD - system wide, used automatically (APPLE only)
  111. 5. WINRT - system wide, used automatically (Windows RT only)
  112. 6. HAVE_CONCURRENCY - part of runtime, used automatically (Windows only - MSVS 10, MSVS 11)
  113. + 7. HAVE_PTHREADS_PF - pthreads if available
  114. */
  115. #if defined HAVE_TBB
  116. @@ -125,14 +126,14 @@
  117. # define CV_PARALLEL_FRAMEWORK "winrt-concurrency"
  118. #elif defined HAVE_CONCURRENCY
  119. # define CV_PARALLEL_FRAMEWORK "ms-concurrency"
  120. -#elif defined HAVE_PTHREADS
  121. +#elif defined HAVE_PTHREADS_PF
  122. # define CV_PARALLEL_FRAMEWORK "pthreads"
  123. #endif
  124. namespace cv
  125. {
  126. ParallelLoopBody::~ParallelLoopBody() {}
  127. -#if defined HAVE_PTHREADS && HAVE_PTHREADS
  128. +#ifdef HAVE_PTHREADS_PF
  129. void parallel_for_pthreads(const cv::Range& range, const cv::ParallelLoopBody& body, double nstripes);
  130. size_t parallel_pthreads_get_threads_num();
  131. void parallel_pthreads_set_threads_num(int num);
  132. @@ -306,7 +307,7 @@ void cv::parallel_for_(const cv::Range& range, const cv::ParallelLoopBody& body,
  133. Concurrency::CurrentScheduler::Detach();
  134. }
  135. -#elif defined HAVE_PTHREADS
  136. +#elif defined HAVE_PTHREADS_PF
  137. parallel_for_pthreads(range, body, nstripes);
  138. @@ -365,7 +366,7 @@ int cv::getNumThreads(void)
  139. ? Concurrency::CurrentScheduler::Get()->GetNumberOfVirtualProcessors()
  140. : pplScheduler->GetNumberOfVirtualProcessors());
  141. -#elif defined HAVE_PTHREADS
  142. +#elif defined HAVE_PTHREADS_PF
  143. return parallel_pthreads_get_threads_num();
  144. @@ -426,7 +427,7 @@ void cv::setNumThreads( int threads )
  145. Concurrency::MaxConcurrency, threads-1));
  146. }
  147. -#elif defined HAVE_PTHREADS
  148. +#elif defined HAVE_PTHREADS_PF
  149. parallel_pthreads_set_threads_num(threads);
  150. @@ -452,6 +453,8 @@ int cv::getThreadNum(void)
  151. return 0;
  152. #elif defined HAVE_CONCURRENCY
  153. return std::max(0, (int)Concurrency::Context::VirtualProcessorId()); // zero for master thread, unique number for others but not necessary 1,2,3,...
  154. +#elif defined HAVE_PTHREADS_PF
  155. + return (int)(size_t)(void*)pthread_self(); // no zero-based indexing
  156. #else
  157. return 0;
  158. #endif
  159. diff --git a/modules/core/src/parallel_pthreads.cpp b/modules/core/src/parallel_pthreads.cpp
  160. index 8c34959..091ea2d 100644
  161. --- a/modules/core/src/parallel_pthreads.cpp
  162. +++ b/modules/core/src/parallel_pthreads.cpp
  163. @@ -42,7 +42,7 @@
  164. #include "precomp.hpp"
  165. -#if defined HAVE_PTHREADS && HAVE_PTHREADS
  166. +#ifdef HAVE_PTHREADS_PF
  167. #include <algorithm>
  168. #include <pthread.h>
  169. diff --git a/modules/core/src/precomp.hpp b/modules/core/src/precomp.hpp
  170. index d463126..88b60e4 100644
  171. --- a/modules/core/src/precomp.hpp
  172. +++ b/modules/core/src/precomp.hpp
  173. @@ -292,12 +292,6 @@ TLSData<CoreTLSData>& getCoreTlsData();
  174. #define CL_RUNTIME_EXPORT
  175. #endif
  176. -#ifndef HAVE_PTHREADS
  177. -#if !(defined WIN32 || defined _WIN32 || defined WINCE || defined HAVE_WINRT)
  178. -#define HAVE_PTHREADS 1
  179. -#endif
  180. -#endif
  181. -
  182. extern bool __termination; // skip some cleanups, because process is terminating
  183. // (for example, if ExitProcess() was already called)
  184. --
  185. 2.4.4