1 /*=========================================================================
2  *
3  *  Copyright Insight Software Consortium
4  *
5  *  Licensed under the Apache License, Version 2.0 (the "License");
6  *  you may not use this file except in compliance with the License.
7  *  You may obtain a copy of the License at
8  *
9  *         http://www.apache.org/licenses/LICENSE-2.0.txt
10  *
11  *  Unless required by applicable law or agreed to in writing, software
12  *  distributed under the License is distributed on an "AS IS" BASIS,
13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *  See the License for the specific language governing permissions and
15  *  limitations under the License.
16  *
17  *=========================================================================*/
18 
19 #ifndef itkTBBMultiThreader_h
20 #define itkTBBMultiThreader_h
21 
22 #include "itkMultiThreaderBase.h"
23 
24 namespace itk
25 {
26 /** \class TBBMultiThreader
27  * \brief A class for performing multithreaded execution with a thread
28  * pool back end, uses the Intel Threading Building Blocks (TBB) library.
29  *
30  * This multi-threader implementation generates dynamically-sized
31  * thread-regions, and is expected to provide best performance.
32  *
33  * \ingroup OSSystemObjects
34  *
35  * \ingroup ITKCommon
36  */
37 
38 class ITKCommon_EXPORT TBBMultiThreader : public MultiThreaderBase
39 {
40 public:
41   ITK_DISALLOW_COPY_AND_ASSIGN(TBBMultiThreader);
42 
43   /** Standard class type aliases. */
44   using Self = TBBMultiThreader;
45   using Superclass = MultiThreaderBase;
46   using Pointer = SmartPointer<Self>;
47   using ConstPointer = SmartPointer<const Self>;
48 
49   /** Method for creation through the object factory. */
50   itkNewMacro(Self);
51 
52   /** Run-time type information (and related methods). */
53   itkTypeMacro(TBBMultiThreader, MultiThreaderBase);
54 
55   /** Get/Set the number of work units to create. TBBMultiThreader
56    * does not limit the number of work units. This number is
57    * only respected by SetSingleMethod/SingleMethodExecute. */
58   virtual void SetNumberOfWorkUnits( ThreadIdType numberOfWorkUnits ) override;
59 
60   /** Execute the SingleMethod (as define by SetSingleMethod) using
61    * m_NumberOfWorkUnits work units. As a side effect the m_NumberOfWorkUnits will be
62    * checked against the current m_GlobalMaximumNumberOfThreads and clamped if
63    * necessary. */
64   void SingleMethodExecute() override;
65 
66   /** Set the SingleMethod to f() and the UserData field of the
67    * WorkUnitInfo that is passed to it will be data.
68    * This method must be of type itkThreadFunctionType and
69    * must take a single argument of type void. */
70   void SetSingleMethod(ThreadFunctionType, void *data) override;
71 
72   /** Parallelize an operation over an array. If filter argument is not nullptr,
73    * this function will update its progress as each index is completed. */
74   void ParallelizeArray(
75     SizeValueType firstIndex,
76     SizeValueType lastIndexPlus1,
77     ArrayThreadingFunctorType aFunc,
78     ProcessObject* filter ) override;
79 
80   /** Break up region into smaller chunks, and call the function with chunks as parameters. */
81   void ParallelizeImageRegion(
82       unsigned int dimension,
83       const IndexValueType index[],
84       const SizeValueType size[],
85       ThreadingFunctorType funcP,
86       ProcessObject* filter) override;
87 
88 protected:
89   TBBMultiThreader();
90   ~TBBMultiThreader() override;
91   void PrintSelf(std::ostream & os, Indent indent) const override;
92 
93 private:
94   /** ProcessObject is a friend so that it can call PrintSelf() on its Multithreader. */
95   friend class ProcessObject;
96 };
97 
98 }  // end namespace itk
99 #endif
100