ITK  6.0.0
Insight Toolkit
Classes | Public Types | Public Member Functions | Static Public Member Functions | Protected Member Functions | Static Protected Member Functions | Protected Attributes | Private Member Functions | Static Private Member Functions | Private Attributes | Static Private Attributes | Friends | List of all members
itk::MultiThreaderBase Class Referenceabstract

#include <itkMultiThreaderBase.h>

Detailed Description

A class for performing multithreaded execution.

Multithreaders are a class hierarchy that provides support for multithreaded execution by abstracting away platform-specific details. This class can be used to execute a single method on multiple threads or to parallelize an operation over a given image region or array.

Definition at line 107 of file itkMultiThreaderBase.h.

+ Inheritance diagram for itk::MultiThreaderBase:
+ Collaboration diagram for itk::MultiThreaderBase:

Classes

struct  ArrayCallback
 
struct  RegionAndCallback
 
struct  WorkUnitInfo
 

Public Types

using ArrayThreadingFunctorType = std::function< void(SizeValueType)>
 
using ConstPointer = SmartPointer< const Self >
 
using Pointer = SmartPointer< Self >
 
using Self = MultiThreaderBase
 
using Superclass = Object
 
using ThreaderEnum = MultiThreaderBaseEnums::Threader
 
using ThreadingFunctorType = std::function< void(const IndexValueType index[], const SizeValueType size[])>
 
- Public Types inherited from itk::Object
using ConstPointer = SmartPointer< const Self >
 
using Pointer = SmartPointer< Self >
 
using Self = Object
 
using Superclass = LightObject
 
- Public Types inherited from itk::LightObject
using ConstPointer = SmartPointer< const Self >
 
using Pointer = SmartPointer< Self >
 
using Self = LightObject
 

Public Member Functions

const char * GetNameOfClass () const override
 
virtual bool GetUpdateProgress () const
 
virtual void ParallelizeArray (SizeValueType firstIndex, SizeValueType lastIndexPlus1, ArrayThreadingFunctorType aFunc, ProcessObject *filter)
 
virtual void ParallelizeImageRegion (unsigned int dimension, const IndexValueType index[], const SizeValueType size[], ThreadingFunctorType funcP, ProcessObject *filter)
 
template<unsigned int VDimension, typename TFunction >
ITK_TEMPLATE_EXPORT void ParallelizeImageRegionRestrictDirection (unsigned int restrictedDirection, const ImageRegion< VDimension > &requestedRegion, TFunction funcP, ProcessObject *filter)
 
virtual void SetSingleMethod (ThreadFunctionType, void *data)=0
 
void SetSingleMethodAndExecute (ThreadFunctionType func, void *data)
 
virtual void SetUpdateProgress (bool updates)
 
virtual void SingleMethodExecute ()=0
 
virtual void SetMaximumNumberOfThreads (ThreadIdType numberOfThreads)
 
virtual ThreadIdType GetMaximumNumberOfThreads () const
 
virtual void SetNumberOfWorkUnits (ThreadIdType numberOfWorkUnits)
 
virtual ThreadIdType GetNumberOfWorkUnits () const
 
 itkLegacyMacro (static void SetGlobalDefaultUseThreadPool(const bool GlobalDefaultUseThreadPool);) itkLegacyMacro(static bool GetGlobalDefaultUseThreadPool()
 
template<unsigned int VDimension, typename TFunction >
ITK_TEMPLATE_EXPORT void ParallelizeImageRegion (const ImageRegion< VDimension > &requestedRegion, TFunction funcP, ProcessObject *filter)
 
- Public Member Functions inherited from itk::Object
unsigned long AddObserver (const EventObject &event, Command *cmd) const
 
unsigned long AddObserver (const EventObject &event, std::function< void(const EventObject &)> function) const
 
LightObject::Pointer CreateAnother () const override
 
virtual void DebugOff () const
 
virtual void DebugOn () const
 
CommandGetCommand (unsigned long tag)
 
bool GetDebug () const
 
MetaDataDictionaryGetMetaDataDictionary ()
 
const MetaDataDictionaryGetMetaDataDictionary () const
 
virtual ModifiedTimeType GetMTime () const
 
const char * GetNameOfClass () const override
 
virtual const TimeStampGetTimeStamp () const
 
bool HasObserver (const EventObject &event) const
 
void InvokeEvent (const EventObject &)
 
void InvokeEvent (const EventObject &) const
 
virtual void Modified () const
 
void Register () const override
 
void RemoveAllObservers ()
 
void RemoveObserver (unsigned long tag) const
 
void SetDebug (bool debugFlag) const
 
void SetReferenceCount (int) override
 
void UnRegister () const noexcept override
 
void SetMetaDataDictionary (const MetaDataDictionary &rhs)
 
void SetMetaDataDictionary (MetaDataDictionary &&rrhs)
 
virtual void SetObjectName (std::string _arg)
 
virtual const std::string & GetObjectName () const
 
- Public Member Functions inherited from itk::LightObject
Pointer Clone () const
 
virtual void Delete ()
 
virtual int GetReferenceCount () const
 
void Print (std::ostream &os, Indent indent=0) const
 

Static Public Member Functions

static Pointer New ()
 
static ThreaderEnum ThreaderTypeFromString (std::string threaderString)
 
static void SetGlobalMaximumNumberOfThreads (ThreadIdType val)
 
static ThreadIdType GetGlobalMaximumNumberOfThreads ()
 
static std::string ThreaderTypeToString (ThreaderEnum threader)
 
static void SetGlobalDefaultThreader (ThreaderEnum threaderType)
 
static ThreaderEnum GetGlobalDefaultThreader ()
 
static void SetGlobalDefaultNumberOfThreads (ThreadIdType val)
 
static ThreadIdType GetGlobalDefaultNumberOfThreads ()
 
- Static Public Member Functions inherited from itk::Object
static bool GetGlobalWarningDisplay ()
 
static void GlobalWarningDisplayOff ()
 
static void GlobalWarningDisplayOn ()
 
static Pointer New ()
 
static void SetGlobalWarningDisplay (bool val)
 
- Static Public Member Functions inherited from itk::LightObject
static void BreakOnError ()
 
static Pointer New ()
 

Protected Member Functions

 MultiThreaderBase ()
 
void PrintSelf (std::ostream &os, Indent indent) const override
 
 ~MultiThreaderBase () override
 
- Protected Member Functions inherited from itk::Object
 Object ()
 
bool PrintObservers (std::ostream &os, Indent indent) const
 
void PrintSelf (std::ostream &os, Indent indent) const override
 
virtual void SetTimeStamp (const TimeStamp &timeStamp)
 
 ~Object () override
 
- Protected Member Functions inherited from itk::LightObject
virtual LightObject::Pointer InternalClone () const
 
 LightObject ()
 
virtual void PrintHeader (std::ostream &os, Indent indent) const
 
virtual void PrintTrailer (std::ostream &os, Indent indent) const
 
virtual ~LightObject ()
 

Static Protected Member Functions

static ITK_THREAD_RETURN_FUNCTION_CALL_CONVENTION ParallelizeArrayHelper (void *arg)
 
static ITK_THREAD_RETURN_FUNCTION_CALL_CONVENTION ParallelizeImageRegionHelper (void *arg)
 
static ITK_THREAD_RETURN_FUNCTION_CALL_CONVENTION SingleMethodProxy (void *arg)
 

Protected Attributes

ThreadIdType m_MaximumNumberOfThreads {}
 
ThreadIdType m_NumberOfWorkUnits {}
 
void * m_SingleData { nullptr }
 
ThreadFunctionType m_SingleMethod { nullptr }
 
- Protected Attributes inherited from itk::LightObject
std::atomic< int > m_ReferenceCount {}
 

Private Member Functions

 itkGetGlobalDeclarationMacro (MultiThreaderBaseGlobals, PimplGlobals)
 

Static Private Member Functions

static ThreadIdType GetGlobalDefaultNumberOfThreadsByPlatform ()
 
static ThreaderEnum GetGlobalDefaultThreaderPrivate ()
 
static void SetGlobalDefaultThreaderPrivate (ThreaderEnum threaderType)
 

Private Attributes

std::atomic< bool > m_UpdateProgress { true }
 

Static Private Attributes

static MultiThreaderBaseGlobals * m_PimplGlobals
 

Friends

class ProcessObject
 

Member Typedef Documentation

◆ ArrayThreadingFunctorType

Definition at line 334 of file itkMultiThreaderBase.h.

◆ ConstPointer

Definition at line 116 of file itkMultiThreaderBase.h.

◆ Pointer

Definition at line 115 of file itkMultiThreaderBase.h.

◆ Self

Standard class type aliases.

Definition at line 113 of file itkMultiThreaderBase.h.

◆ Superclass

Definition at line 114 of file itkMultiThreaderBase.h.

◆ ThreaderEnum

Definition at line 165 of file itkMultiThreaderBase.h.

◆ ThreadingFunctorType

using itk::MultiThreaderBase::ThreadingFunctorType = std::function<void(const IndexValueType index[], const SizeValueType size[])>

Definition at line 333 of file itkMultiThreaderBase.h.

Constructor & Destructor Documentation

◆ MultiThreaderBase()

itk::MultiThreaderBase::MultiThreaderBase ( )
protected

◆ ~MultiThreaderBase()

itk::MultiThreaderBase::~MultiThreaderBase ( )
overrideprotected

Member Function Documentation

◆ GetGlobalDefaultNumberOfThreads()

static ThreadIdType itk::MultiThreaderBase::GetGlobalDefaultNumberOfThreads ( )
static

Set/Get the value which is used to initialize the NumberOfThreads in the constructor. It will be clamped to the range [1, m_GlobalMaximumNumberOfThreads ]. Therefore the caller of this method should check that the requested number of threads was accepted.

◆ GetGlobalDefaultNumberOfThreadsByPlatform()

static ThreadIdType itk::MultiThreaderBase::GetGlobalDefaultNumberOfThreadsByPlatform ( )
staticprivate

Platform specific number of threads

◆ GetGlobalDefaultThreader()

static ThreaderEnum itk::MultiThreaderBase::GetGlobalDefaultThreader ( )
static

Set/Get whether the default multi-threader implementation.

The default multi-threader type is picked up from ITK_GLOBAL_DEFAULT_THREADER
environment variable. Example ITK_GLOBAL_DEFAULT_THREADER=TBB
A deprecated ITK_USE_THREADPOOL environment variable is also examined,
but it can only choose Pool or Platform multi-threader.
Platform multi-threader should be avoided,
as it introduces the biggest overhead.

If the SetGlobalDefaultThreaderType API is ever used by the developer,
the developer's choice is respected over the environment variables. 

◆ GetGlobalDefaultThreaderPrivate()

static ThreaderEnum itk::MultiThreaderBase::GetGlobalDefaultThreaderPrivate ( )
staticprivate

◆ GetGlobalMaximumNumberOfThreads()

static ThreadIdType itk::MultiThreaderBase::GetGlobalMaximumNumberOfThreads ( )
static

Set/Get the maximum number of threads to use when multithreading. It will be clamped to the range [ 1, ITK_MAX_THREADS ] because several arrays are already statically allocated using the ITK_MAX_THREADS number. Therefore the caller of this method should check that the requested number of threads was accepted.

◆ GetMaximumNumberOfThreads()

virtual ThreadIdType itk::MultiThreaderBase::GetMaximumNumberOfThreads ( ) const
virtual

Get/Set the number of threads to use. It will be clamped to the range [ 1, m_GlobalMaximumNumberOfThreads ], so the caller of this method should check that the requested number of threads was accepted.

◆ GetNameOfClass()

const char* itk::MultiThreaderBase::GetNameOfClass ( ) const
overridevirtual

◆ GetNumberOfWorkUnits()

virtual ThreadIdType itk::MultiThreaderBase::GetNumberOfWorkUnits ( ) const
virtual

Get/Set the number of work units to create. It might be clamped to the range [ 1, SomeMaximumNumber ], so the caller of this method should check that the requested number of work units was accepted.

◆ GetUpdateProgress()

virtual bool itk::MultiThreaderBase::GetUpdateProgress ( ) const
virtual

◆ itkGetGlobalDeclarationMacro()

itk::MultiThreaderBase::itkGetGlobalDeclarationMacro ( MultiThreaderBaseGlobals  ,
PimplGlobals   
)
private

Only used to synchronize the global variable across static libraries.

◆ itkLegacyMacro()

itk::MultiThreaderBase::itkLegacyMacro ( static void SetGlobalDefaultUseThreadPool(const bool GlobalDefaultUseThreadPool);  )

Set/Get whether to use the to use the thread pool implementation or the spawning implementation of starting threads.

Deprecated: use Get/Set GlobalDefaultThreader.

◆ New()

static Pointer itk::MultiThreaderBase::New ( )
static

◆ ParallelizeArray()

virtual void itk::MultiThreaderBase::ParallelizeArray ( SizeValueType  firstIndex,
SizeValueType  lastIndexPlus1,
ArrayThreadingFunctorType  aFunc,
ProcessObject filter 
)
virtual

Parallelize an operation over an array. If filter argument is not nullptr, this function will update its progress as each index is completed.

This implementation simply delegates parallelization to the old interface SetSingleMethod+SingleMethodExecute. This method is meant to be overloaded!

Reimplemented in itk::PoolMultiThreader, and itk::TBBMultiThreader.

◆ ParallelizeArrayHelper()

static ITK_THREAD_RETURN_FUNCTION_CALL_CONVENTION itk::MultiThreaderBase::ParallelizeArrayHelper ( void *  arg)
staticprotected

◆ ParallelizeImageRegion() [1/2]

template<unsigned int VDimension, typename TFunction >
ITK_TEMPLATE_EXPORT void itk::MultiThreaderBase::ParallelizeImageRegion ( const ImageRegion< VDimension > &  requestedRegion,
TFunction  funcP,
ProcessObject filter 
)
inline

Break up region into smaller chunks, and call the user-specified function or function object funcP for each chunk, having the region of the chunk as argument. The type of such a chuck region is ImageRegion<VDimension>. Each such funcP(region) call must be thread-safe. If filter argument is not nullptr, this function will update its progress as each work unit is completed. Delegates work to non-templated version.

Definition at line 354 of file itkMultiThreaderBase.h.

References itk::ImageRegion< VImageDimension >::GetIndex(), itk::ImageRegion< VImageDimension >::GetSize(), itk::Size< VDimension >::m_InternalArray, itk::Index< VDimension >::m_InternalArray, itk::ImageRegion< VImageDimension >::SetIndex(), and itk::ImageRegion< VImageDimension >::SetSize().

◆ ParallelizeImageRegion() [2/2]

virtual void itk::MultiThreaderBase::ParallelizeImageRegion ( unsigned int  dimension,
const IndexValueType  index[],
const SizeValueType  size[],
ThreadingFunctorType  funcP,
ProcessObject filter 
)
virtual

Break up region into smaller chunks, and call the function with chunks as parameters. This overload does the actual work and should be implemented by derived classes.

Reimplemented in itk::PoolMultiThreader, and itk::TBBMultiThreader.

◆ ParallelizeImageRegionHelper()

static ITK_THREAD_RETURN_FUNCTION_CALL_CONVENTION itk::MultiThreaderBase::ParallelizeImageRegionHelper ( void *  arg)
staticprotected

◆ ParallelizeImageRegionRestrictDirection()

template<unsigned int VDimension, typename TFunction >
ITK_TEMPLATE_EXPORT void itk::MultiThreaderBase::ParallelizeImageRegionRestrictDirection ( unsigned int  restrictedDirection,
const ImageRegion< VDimension > &  requestedRegion,
TFunction  funcP,
ProcessObject filter 
)
inline

Similar to ParallelizeImageRegion, but do not split the region along one of the directions. If VDimension is 1, restrictedDirection is ignored and no parallelization occurs.

Definition at line 378 of file itkMultiThreaderBase.h.

References itk::ImageRegion< VImageDimension >::GetIndex(), itk::ImageRegion< VImageDimension >::GetNumberOfPixels(), and itk::ImageRegion< VImageDimension >::GetSize().

◆ PrintSelf()

void itk::MultiThreaderBase::PrintSelf ( std::ostream &  os,
Indent  indent 
) const
overrideprotectedvirtual

Methods invoked by Print() to print information about the object including superclasses. Typically not called by the user (use Print() instead) but used in the hierarchical print process to combine the output of several classes.

Reimplemented from itk::LightObject.

Reimplemented in itk::PoolMultiThreader, and itk::TBBMultiThreader.

◆ SetGlobalDefaultNumberOfThreads()

static void itk::MultiThreaderBase::SetGlobalDefaultNumberOfThreads ( ThreadIdType  val)
static

Set/Get the value which is used to initialize the NumberOfThreads in the constructor. It will be clamped to the range [1, m_GlobalMaximumNumberOfThreads ]. Therefore the caller of this method should check that the requested number of threads was accepted.

Examples
SphinxExamples/src/Core/Common/SetDefaultNumberOfThreads/Code.cxx.

◆ SetGlobalDefaultThreader()

static void itk::MultiThreaderBase::SetGlobalDefaultThreader ( ThreaderEnum  threaderType)
static

Set/Get whether the default multi-threader implementation.

The default multi-threader type is picked up from ITK_GLOBAL_DEFAULT_THREADER
environment variable. Example ITK_GLOBAL_DEFAULT_THREADER=TBB
A deprecated ITK_USE_THREADPOOL environment variable is also examined,
but it can only choose Pool or Platform multi-threader.
Platform multi-threader should be avoided,
as it introduces the biggest overhead.

If the SetGlobalDefaultThreaderType API is ever used by the developer,
the developer's choice is respected over the environment variables. 

◆ SetGlobalDefaultThreaderPrivate()

static void itk::MultiThreaderBase::SetGlobalDefaultThreaderPrivate ( ThreaderEnum  threaderType)
staticprivate

◆ SetGlobalMaximumNumberOfThreads()

static void itk::MultiThreaderBase::SetGlobalMaximumNumberOfThreads ( ThreadIdType  val)
static

Set/Get the maximum number of threads to use when multithreading. It will be clamped to the range [ 1, ITK_MAX_THREADS ] because several arrays are already statically allocated using the ITK_MAX_THREADS number. Therefore the caller of this method should check that the requested number of threads was accepted.

◆ SetMaximumNumberOfThreads()

virtual void itk::MultiThreaderBase::SetMaximumNumberOfThreads ( ThreadIdType  numberOfThreads)
virtual

Get/Set the number of threads to use. It will be clamped to the range [ 1, m_GlobalMaximumNumberOfThreads ], so the caller of this method should check that the requested number of threads was accepted.

Reimplemented in itk::PoolMultiThreader.

◆ SetNumberOfWorkUnits()

virtual void itk::MultiThreaderBase::SetNumberOfWorkUnits ( ThreadIdType  numberOfWorkUnits)
virtual

Get/Set the number of work units to create. It might be clamped to the range [ 1, SomeMaximumNumber ], so the caller of this method should check that the requested number of work units was accepted.

Reimplemented in itk::TBBMultiThreader.

◆ SetSingleMethod()

virtual void itk::MultiThreaderBase::SetSingleMethod ( ThreadFunctionType  ,
void *  data 
)
pure virtual

Set the SingleMethod to f() and the UserData field of the WorkUnitInfo that is passed to it will be data. This method must be of type itkThreadFunctionType and must take a single argument of type void *.

Implemented in itk::PoolMultiThreader, and itk::TBBMultiThreader.

◆ SetSingleMethodAndExecute()

void itk::MultiThreaderBase::SetSingleMethodAndExecute ( ThreadFunctionType  func,
void *  data 
)

Set the method and the user data, by SetSingleMethod, and executes the method, by SingleMethodExecute.

◆ SetUpdateProgress()

virtual void itk::MultiThreaderBase::SetUpdateProgress ( bool  updates)
virtual

◆ SingleMethodExecute()

virtual void itk::MultiThreaderBase::SingleMethodExecute ( )
pure virtual

Execute the SingleMethod (as define by SetSingleMethod) using m_NumberOfWorkUnits threads. As a side effect the m_NumberOfWorkUnits will be checked against the current m_GlobalMaximumNumberOfThreads and clamped if necessary.

Implemented in itk::PoolMultiThreader, and itk::TBBMultiThreader.

◆ SingleMethodProxy()

static ITK_THREAD_RETURN_FUNCTION_CALL_CONVENTION itk::MultiThreaderBase::SingleMethodProxy ( void *  arg)
staticprotected

Static function used as a "proxy callback" by multi-threaders. The threading library will call this routine for each thread, which will delegate the control to the prescribed SingleMethod. This routine acts as an intermediary between the multi-threaders and the user supplied callback (SingleMethod) in order to catch any exceptions thrown by the threads.

◆ ThreaderTypeFromString()

static ThreaderEnum itk::MultiThreaderBase::ThreaderTypeFromString ( std::string  threaderString)
static

Convert a threader name into its enum type.

◆ ThreaderTypeToString()

static std::string itk::MultiThreaderBase::ThreaderTypeToString ( ThreaderEnum  threader)
inlinestatic

Convert a threader enum type into a string for displaying or logging.

Definition at line 182 of file itkMultiThreaderBase.h.

Friends And Related Function Documentation

◆ ProcessObject

friend class ProcessObject
friend

Friends of Multithreader. ProcessObject is a friend so that it can call PrintSelf() on its Multithreader.

Friends of DataObject

Definition at line 509 of file itkMultiThreaderBase.h.

Member Data Documentation

◆ m_MaximumNumberOfThreads

ThreadIdType itk::MultiThreaderBase::m_MaximumNumberOfThreads {}
protected

The number of threads to use. The m_MaximumNumberOfThreads must always be less than or equal to the m_GlobalMaximumNumberOfThreads before it is used during the execution of a threaded method. Its value is initialized to m_GlobalDefaultNumberOfThreads at construction time. Its value is clamped to the current m_GlobalMaximumNumberOfThreads in the SingleMethodExecute() method.

Definition at line 477 of file itkMultiThreaderBase.h.

◆ m_NumberOfWorkUnits

ThreadIdType itk::MultiThreaderBase::m_NumberOfWorkUnits {}
protected

The number of work units to create.

Definition at line 467 of file itkMultiThreaderBase.h.

◆ m_PimplGlobals

MultiThreaderBaseGlobals* itk::MultiThreaderBase::m_PimplGlobals
staticprivate

Definition at line 505 of file itkMultiThreaderBase.h.

◆ m_SingleData

void* itk::MultiThreaderBase::m_SingleData { nullptr }
protected

The data to be passed as argument.

Definition at line 492 of file itkMultiThreaderBase.h.

◆ m_SingleMethod

ThreadFunctionType itk::MultiThreaderBase::m_SingleMethod { nullptr }
protected

The method to invoke.

Definition at line 489 of file itkMultiThreaderBase.h.

◆ m_UpdateProgress

std::atomic<bool> itk::MultiThreaderBase::m_UpdateProgress { true }
private

Definition at line 503 of file itkMultiThreaderBase.h.


The documentation for this class was generated from the following file: