Skip to content

Commit

Permalink
ENH: Add backwards compatibility for Get/SetNumberOfThreads
Browse files Browse the repository at this point in the history
SetNumberOfThreads() should be called on
processObject->GetMultiThreader()->SetMaximumNumberOfThreads().
But processObject->SetNumberOfWorkUnits() is usually desired now.

Change-Id: I20d95b343765dbdb4472d07a3eb5b7f2458800b5
  • Loading branch information
dzenanz committed Jul 17, 2018
1 parent 011266e commit 38f516d
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 5 deletions.
4 changes: 3 additions & 1 deletion Documentation/ITK5MigrationGuide.md
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,9 @@ this->AfterThreadedGenerateData();

Get/Set GlobalMaximumNumberOfThreads and GlobalDefaultNumberOfThreads
now reside in `MultiThreaderBase`. With a warning, they are still
available in `PlatformMultiThreader`.
available in `PlatformMultiThreader`. The common case of
`innerFilter->SetNumberOfThreads(1);` should be replaced by
`innerFilter->SetNumberOfWorkUnits(1);`.

To transition to the new threading model, it is usually enough to rename
`ThreadedGenerateData` into `DynamicThreadedGenerateData`, remove the
Expand Down
12 changes: 12 additions & 0 deletions Modules/Core/Common/include/itkProcessObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,18 @@ class ITKCommon_EXPORT ProcessObject:public Object
itkSetClampMacro(NumberOfWorkUnits, ThreadIdType, 1, ITK_MAX_THREADS);
itkGetConstReferenceMacro(NumberOfWorkUnits, ThreadIdType);

#if !defined( ITK_LEGACY_REMOVE ) || defined( ITKV4_COMPATIBILITY )
itkLegacyMacro(void SetNumberOfThreads(ThreadIdType count))
{
this->SetNumberOfWorkUnits(count);
}

itkLegacyMacro(ThreadIdType GetNumberOfThreads() const)
{
return this->GetNumberOfWorkUnits();
}
#endif // !ITK_LEGACY_REMOVE

/** Return the multithreader used by this class. */
MultiThreaderType * GetMultiThreader() const
{ return m_MultiThreader; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,12 +182,26 @@ class ITK_TEMPLATE_EXPORT ObjectToObjectOptimizerBaseTemplate : public Object
itkGetConstReferenceMacro(DoEstimateScales, bool);
itkBooleanMacro(DoEstimateScales);

/** Set the number of threads to use when threading.
* The default is the global default number of threads
* returned from itkMultiThreader. */
/** Set the number of work units to use when threading.
* The default is the global default number of work units
* decided in the constructor of the MultiThreader. */
virtual void SetNumberOfWorkUnits( ThreadIdType number );

/** Get the number of threads set to be used. */
#if !defined( ITK_LEGACY_REMOVE )
/** Set the number of work units to use when threading.
*
* NOTE: deprecated. Use SetNumberOfWorkUnits() */
itkLegacyMacro( virtual void SetNumberOfThreads( ThreadIdType number ) )
{
return this->SetNumberOfWorkUnits( number );
}
itkLegacyMacro( virtual const ThreadIdType& GetNumberOfThreads() const )
{
return this->m_NumberOfWorkUnits;
}
#endif // !ITK_LEGACY_REMOVE

/** Get the number of work units set to be used. */
itkGetConstReferenceMacro( NumberOfWorkUnits, ThreadIdType );

/** Return current number of iterations. */
Expand Down
28 changes: 28 additions & 0 deletions Modules/Registration/Metricsv4/include/itkImageToImageMetricv4.h
Original file line number Diff line number Diff line change
Expand Up @@ -457,6 +457,34 @@ class ITK_TEMPLATE_EXPORT ImageToImageMetricv4
virtual void SetMaximumNumberOfWorkUnits( const ThreadIdType workUnits );
virtual ThreadIdType GetMaximumNumberOfWorkUnits() const;

#if !defined ( ITK_LEGACY_REMOVE )
/** Get number of threads to used in the the most recent
* evaluation. Only valid after GetValueAndDerivative() or
* GetValue() has been called.
*
* NOTE: deprecated. Use GetNumberOfWorkUnitsUsed() */
itkLegacyMacro( virtual ThreadIdType GetNumberOfThreadsUsed() const )
{
return this->GetNumberOfWorkUnitsUsed();
}

/** Set number of threads to use. This the maximum number of threads to use
* when multithreaded. The actual number of threads used (may be less than
* this value) can be obtained with \c GetNumberOfWorkUnitsUsed.
*
* NOTE: deprecated. Use SetMaximumNumberOfWorkUnits() and
* GetMaximumNumberOfWorkUnits() */
itkLegacyMacro( virtual void SetMaximumNumberOfThreads( const ThreadIdType count ) )
{
this->SetMaximumNumberOfWorkUnits(count);
}
itkLegacyMacro( virtual ThreadIdType GetMaximumNumberOfThreads() const )
{
return this->GetMaximumNumberOfWorkUnits();
}
#endif // !ITK_LEGACY_REMOVE


/**
* Finalize the per-thread components for computing
* metric. Some threads can accumulate their data
Expand Down

0 comments on commit 38f516d

Please sign in to comment.