EstervQrCode 1.1.1
Library for qr code manipulation
parallel_for.tbb.hpp
1 // This file is part of OpenCV project.
2 // It is subject to the license terms in the LICENSE file found in the top-level directory
3 // of this distribution and at http://opencv.org/license.html.
4 
5 #ifndef OPENCV_CORE_PARALLEL_FOR_TBB_HPP
6 #define OPENCV_CORE_PARALLEL_FOR_TBB_HPP
7 
8 #include "opencv2/core/parallel/parallel_backend.hpp"
9 #include <opencv2/core/utils/logger.hpp>
10 
11 #ifndef TBB_SUPPRESS_DEPRECATED_MESSAGES // supress warning
12 #define TBB_SUPPRESS_DEPRECATED_MESSAGES 1
13 #endif
14 #include "tbb/tbb.h"
15 #if !defined(TBB_INTERFACE_VERSION)
16 #error "Unknows/unsupported TBB version"
17 #endif
18 
19 #if TBB_INTERFACE_VERSION >= 8000
20 #include "tbb/task_arena.h"
21 #endif
22 
23 namespace cv { namespace parallel { namespace tbb {
24 
25 using namespace ::tbb;
26 
27 #if TBB_INTERFACE_VERSION >= 8000
28 static tbb::task_arena& getArena()
29 {
30  static tbb::task_arena tbbArena(tbb::task_arena::automatic);
31  return tbbArena;
32 }
33 #else
34 static tbb::task_scheduler_init& getScheduler()
35 {
36  static tbb::task_scheduler_init tbbScheduler(tbb::task_scheduler_init::deferred);
37  return tbbScheduler;
38 }
39 #endif
40 
47 {
48 protected:
51 public:
53  {
54  CV_LOG_INFO(NULL, "Initializing TBB parallel backend: TBB_INTERFACE_VERSION=" << TBB_INTERFACE_VERSION);
55  numThreads = 0;
56 #if TBB_INTERFACE_VERSION >= 8000
57  (void)getArena();
58 #else
59  (void)getScheduler();
60 #endif
61  }
62 
63  virtual ~ParallelForBackend() {}
64 
66  {
67  const FN_parallel_for_body_cb_t& callback;
68  void* const callback_data;
69  const int tasks;
70  public:
71  inline CallbackProxy(int tasks_, FN_parallel_for_body_cb_t& callback_, void* callback_data_)
72  : callback(callback_), callback_data(callback_data_), tasks(tasks_)
73  {
74  // nothing
75  }
76 
77  void operator()(const tbb::blocked_range<int>& range) const
78  {
79  this->callback(range.begin(), range.end(), callback_data);
80  }
81 
82  void operator()() const
83  {
84  tbb::parallel_for(tbb::blocked_range<int>(0, tasks), *this);
85  }
86  };
87 
88  virtual void parallel_for(int tasks, FN_parallel_for_body_cb_t body_callback, void* callback_data) CV_OVERRIDE
89  {
90  CallbackProxy task(tasks, body_callback, callback_data);
91 #if TBB_INTERFACE_VERSION >= 8000
92  getArena().execute(task);
93 #else
94  task();
95 #endif
96  }
97 
98  virtual int getThreadNum() const CV_OVERRIDE
99  {
100 #if TBB_INTERFACE_VERSION >= 9100
101  return tbb::this_task_arena::current_thread_index();
102 #elif TBB_INTERFACE_VERSION >= 8000
103  return tbb::task_arena::current_thread_index();
104 #else
105  return 0;
106 #endif
107  }
108 
109  virtual int getNumThreads() const CV_OVERRIDE
110  {
111 #if TBB_INTERFACE_VERSION >= 9100
112  return getArena().max_concurrency();
113 #elif TBB_INTERFACE_VERSION >= 8000
114  return numThreads > 0
115  ? numThreads
116  : tbb::task_scheduler_init::default_num_threads();
117 #else
118  return getScheduler().is_active()
119  ? numThreads
120  : tbb::task_scheduler_init::default_num_threads();
121 #endif
122  }
123 
124  virtual int setNumThreads(int nThreads) CV_OVERRIDE
125  {
126  int oldNumThreads = numThreads;
127  numThreads = nThreads;
128 
129 #if TBB_INTERFACE_VERSION >= 8000
130  auto& tbbArena = getArena();
131  if (tbbArena.is_active())
132  tbbArena.terminate();
133  if (numThreads > 0)
134  tbbArena.initialize(numThreads);
135 #else
136  auto& tbbScheduler = getScheduler();
137  if (tbbScheduler.is_active())
138  tbbScheduler.terminate();
139  if (numThreads > 0)
140  tbbScheduler.initialize(numThreads);
141 #endif
142  return oldNumThreads;
143  }
144 
145  const char* getName() const CV_OVERRIDE
146  {
147  return "tbb";
148  }
149 };
150 
151 }}} // namespace
152 
153 #endif // OPENCV_CORE_PARALLEL_FOR_TBB_HPP
Definition: parallel_backend.hpp:57
void(CV_API_CALL * FN_parallel_for_body_cb_t)(int start, int end, void *data)
Definition: parallel_backend.hpp:61
Definition: parallel_for.tbb.hpp:66
void operator()() const
Definition: parallel_for.tbb.hpp:82
CallbackProxy(int tasks_, FN_parallel_for_body_cb_t &callback_, void *callback_data_)
Definition: parallel_for.tbb.hpp:71
void operator()(const tbb::blocked_range< int > &range) const
Definition: parallel_for.tbb.hpp:77
Definition: parallel_for.tbb.hpp:47
virtual int setNumThreads(int nThreads) CV_OVERRIDE
Definition: parallel_for.tbb.hpp:124
virtual int getNumThreads() const CV_OVERRIDE
Definition: parallel_for.tbb.hpp:109
virtual void parallel_for(int tasks, FN_parallel_for_body_cb_t body_callback, void *callback_data) CV_OVERRIDE
Definition: parallel_for.tbb.hpp:88
ParallelForBackend()
Definition: parallel_for.tbb.hpp:52
virtual ~ParallelForBackend()
Definition: parallel_for.tbb.hpp:63
virtual int getThreadNum() const CV_OVERRIDE
Definition: parallel_for.tbb.hpp:98
int numThreads
Definition: parallel_for.tbb.hpp:49
int numThreadsMax
Definition: parallel_for.tbb.hpp:50
const char * getName() const CV_OVERRIDE
Definition: parallel_for.tbb.hpp:145
#define CV_LOG_INFO(tag,...)
Definition: logger.hpp:132
#define CV_OVERRIDE
Definition: cvdef.h:792
CvArr CvSize range
Definition: imgproc_c.h:781
static tbb::task_scheduler_init & getScheduler()
Definition: parallel_for.tbb.hpp:34
"black box" representation of the file storage associated with a file on disk.
Definition: calib3d.hpp:441