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  *
20  *  Portions of this file are subject to the VTK Toolkit Version 3 copyright.
21  *
22  *  Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
23  *
24  *  For complete copyright, license and disclaimer of warranty information
25  *  please refer to the NOTICE file at the top of the ITK source tree.
26  *
27  *=========================================================================*/
28 #ifndef itkPoolMultiThreader_h
29 #define itkPoolMultiThreader_h
30 
31 #include "itkMultiThreaderBase.h"
32 #include "itkThreadPool.h"
33 
34 namespace itk
35 {
36 /** \class PoolMultiThreader
37  * \brief A class for performing multithreaded execution with a thread
38  * pool back end
39  *
40  * \ingroup OSSystemObjects
41  *
42  * \ingroup ITKCommon
43  */
44 
45 class ITKCommon_EXPORT PoolMultiThreader : public MultiThreaderBase
46 {
47 public:
48   ITK_DISALLOW_COPY_AND_ASSIGN(PoolMultiThreader);
49 
50   /** Standard class type aliases. */
51   using Self = PoolMultiThreader;
52   using Superclass = MultiThreaderBase;
53   using Pointer = SmartPointer<Self>;
54   using ConstPointer = SmartPointer<const Self>;
55 
56   /** Method for creation through the object factory. */
57   itkNewMacro(Self);
58 
59   /** Run-time type information (and related methods). */
60   itkTypeMacro(PoolMultiThreader, MultiThreaderBase);
61 
62 
63   /** Execute the SingleMethod (as define by SetSingleMethod) using
64    * m_NumberOfWorkUnits work units. As a side effect the m_NumberOfWorkUnits will be
65    * checked against the current m_GlobalMaximumNumberOfThreads and clamped if
66    * necessary. */
67   void SingleMethodExecute() override;
68 
69   /** Set the SingleMethod to f() and the UserData field of the
70    * WorkUnitInfo that is passed to it will be data.
71    * This method must be of type itkThreadFunctionType and
72    * must take a single argument of type void. */
73   void SetSingleMethod(ThreadFunctionType, void *data) override;
74 
75   /** Parallelize an operation over an array. If filter argument is not nullptr,
76    * this function will update its progress as each index is completed. */
77   void
78   ParallelizeArray(
79     SizeValueType firstIndex,
80     SizeValueType lastIndexPlus1,
81     ArrayThreadingFunctorType aFunc,
82     ProcessObject* filter ) override;
83 
84   /** Break up region into smaller chunks, and call the function with chunks as parameters. */
85   void
86   ParallelizeImageRegion(
87     unsigned int dimension,
88     const IndexValueType index[],
89     const SizeValueType size[],
90     ThreadingFunctorType funcP,
91     ProcessObject* filter) override;
92 
93   /** Set the number of threads to use. PoolMultiThreader
94    * can only INCREASE its number of threads. */
95   void SetMaximumNumberOfThreads( ThreadIdType numberOfThreads ) override;
96 
97   struct ThreadPoolInfoStruct :WorkUnitInfo
98     {
99     std::future< ITK_THREAD_RETURN_TYPE > Future;
100     };
101 
102 protected:
103   PoolMultiThreader();
104   ~PoolMultiThreader() override;
105   void PrintSelf(std::ostream & os, Indent indent) const override;
106 
107 private:
108   // Thread pool instance and factory
109   ThreadPool::Pointer m_ThreadPool;
110 
111   /** An array of work unit information containing a work unit id
112    *  (0, 1, 2, .. ITK_MAX_THREADS-1), work unit count, and a pointer
113    *  to void so that user data can be passed to each thread. */
114   ThreadPoolInfoStruct m_ThreadInfoArray[ITK_MAX_THREADS];
115 
116   /** Friends of Multithreader.
117    * ProcessObject is a friend so that it can call PrintSelf() on its
118    * Multithreader. */
119   friend class ProcessObject;
120 };
121 
122 }  // end namespace itk
123 #endif
124