Skip to content

Commit

Permalink
STYLE: Use ITK 5.4 MultiThreaderBase::SetSingleMethodAndExecute
Browse files Browse the repository at this point in the history
Following ITK pull request InsightSoftwareConsortium/ITK#4370 commit InsightSoftwareConsortium/ITK@972b577 "ENH: Add `MultiThreaderBase::SetSingleMethodAndExecute` member function".
  • Loading branch information
N-Dekker committed Jun 10, 2024
1 parent ca36292 commit 2b764d7
Show file tree
Hide file tree
Showing 20 changed files with 52 additions and 86 deletions.
15 changes: 5 additions & 10 deletions Common/CostFunctions/itkAdvancedImageToImageMetric.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -686,11 +686,8 @@ template <class TFixedImage, class TMovingImage>
void
AdvancedImageToImageMetric<TFixedImage, TMovingImage>::LaunchGetValueThreaderCallback() const
{
/** Setup threader. */
Superclass::m_Threader->SetSingleMethod(this->GetValueThreaderCallback, &m_ThreaderMetricParameters);

/** Launch. */
Superclass::m_Threader->SingleMethodExecute();
/** Setup threader and launch. */
Superclass::m_Threader->SetSingleMethodAndExecute(this->GetValueThreaderCallback, &m_ThreaderMetricParameters);

} // end LaunchGetValueThreaderCallback()

Expand Down Expand Up @@ -728,11 +725,9 @@ template <class TFixedImage, class TMovingImage>
void
AdvancedImageToImageMetric<TFixedImage, TMovingImage>::LaunchGetValueAndDerivativeThreaderCallback() const
{
/** Setup threader. */
Superclass::m_Threader->SetSingleMethod(this->GetValueAndDerivativeThreaderCallback, &m_ThreaderMetricParameters);

/** Launch. */
Superclass::m_Threader->SingleMethodExecute();
/** Setup threader and launch. */
Superclass::m_Threader->SetSingleMethodAndExecute(this->GetValueAndDerivativeThreaderCallback,
&m_ThreaderMetricParameters);

} // end LaunchGetValueAndDerivativeThreaderCallback()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1244,14 +1244,11 @@ template <class TFixedImage, class TMovingImage>
void
ParzenWindowHistogramImageToImageMetric<TFixedImage, TMovingImage>::LaunchComputePDFsThreaderCallback() const
{
/** Setup threader. */
this->m_Threader->SetSingleMethod(
/** Setup threader and launch. */
this->m_Threader->SetSingleMethodAndExecute(
this->ComputePDFsThreaderCallback,
const_cast<void *>(static_cast<const void *>(&this->m_ParzenWindowHistogramThreaderParameters)));

/** Launch. */
this->m_Threader->SingleMethodExecute();

} // end LaunchComputePDFsThreaderCallback()


Expand Down
12 changes: 6 additions & 6 deletions Common/ImageSamplers/itkImageFullSampler.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -118,16 +118,16 @@ ImageFullSampler<TInputImage>::MultiThreadedGenerateData(MultiThreaderBase &

if (mask)
{
multiThreader.SetSingleMethod(elastix::MaskHasSameImageDomain(*mask, inputImage)
? &Self::ThreaderCallback<elastix::MaskCondition::HasSameImageDomain>
: &Self::ThreaderCallback<elastix::MaskCondition::HasDifferentImageDomain>,
&userData);
multiThreader.SetSingleMethodAndExecute(
elastix::MaskHasSameImageDomain(*mask, inputImage)
? &Self::ThreaderCallback<elastix::MaskCondition::HasSameImageDomain>
: &Self::ThreaderCallback<elastix::MaskCondition::HasDifferentImageDomain>,
&userData);
}
else
{
multiThreader.SetSingleMethod(&Self::ThreaderCallback<elastix::MaskCondition::IsNull>, &userData);
multiThreader.SetSingleMethodAndExecute(&Self::ThreaderCallback<elastix::MaskCondition::IsNull>, &userData);
}
multiThreader.SingleMethodExecute();

if (mask)
{
Expand Down
11 changes: 5 additions & 6 deletions Common/ImageSamplers/itkImageGridSampler.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -228,16 +228,15 @@ ImageGridSampler<TInputImage>::MultiThreadedGenerateData(MultiThreaderBase &

if (mask)
{
multiThreader.SetSingleMethod(maskHasSameImageDomain
? &Self::ThreaderCallback<elastix::MaskCondition::HasSameImageDomain>
: &Self::ThreaderCallback<elastix::MaskCondition::HasDifferentImageDomain>,
&userData);
multiThreader.SetSingleMethodAndExecute(
maskHasSameImageDomain ? &Self::ThreaderCallback<elastix::MaskCondition::HasSameImageDomain>
: &Self::ThreaderCallback<elastix::MaskCondition::HasDifferentImageDomain>,
&userData);
}
else
{
multiThreader.SetSingleMethod(&Self::ThreaderCallback<elastix::MaskCondition::IsNull>, &userData);
multiThreader.SetSingleMethodAndExecute(&Self::ThreaderCallback<elastix::MaskCondition::IsNull>, &userData);
}
multiThreader.SingleMethodExecute();

if (mask)
{
Expand Down
5 changes: 2 additions & 3 deletions Common/ImageSamplers/itkImageRandomCoordinateSampler.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,8 @@ ImageRandomCoordinateSampler<TInputImage>::GenerateData()

UserData userData{ m_RandomCoordinates, inputImage, *interpolator, samples };

MultiThreaderBase & multiThreader = elastix::Deref(this->ProcessObject::GetMultiThreader());
multiThreader.SetSingleMethod(&Self::ThreaderCallback, &userData);
multiThreader.SingleMethodExecute();
elastix::Deref(this->ProcessObject::GetMultiThreader())
.SetSingleMethodAndExecute(&Self::ThreaderCallback, &userData);
return;
}

Expand Down
5 changes: 2 additions & 3 deletions Common/ImageSamplers/itkImageRandomSampler.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,8 @@ ImageRandomSampler<TInputImage>::GenerateData()
randomNumberList, inputImage, croppedInputImageRegion.GetIndex(), croppedInputImageRegion.GetSize(), samples
};

MultiThreaderBase & multiThreader = elastix::Deref(this->ProcessObject::GetMultiThreader());
multiThreader.SetSingleMethod(&Self::ThreaderCallback, &userData);
multiThreader.SingleMethodExecute();
elastix::Deref(this->ProcessObject::GetMultiThreader())
.SetSingleMethodAndExecute(&Self::ThreaderCallback, &userData);
return;
}

Expand Down
5 changes: 2 additions & 3 deletions Common/ImageSamplers/itkImageRandomSamplerSparseMask.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,8 @@ ImageRandomSamplerSparseMask<TInputImage>::GenerateData()

UserData userData{ allValidSamples.CastToSTLConstContainer(), m_RandomIndices, samples };

MultiThreaderBase & multiThreader = elastix::Deref(this->ProcessObject::GetMultiThreader());
multiThreader.SetSingleMethod(&Self::ThreaderCallback, &userData);
multiThreader.SingleMethodExecute();
elastix::Deref(this->ProcessObject::GetMultiThreader())
.SetSingleMethodAndExecute(&Self::ThreaderCallback, &userData);
return;
}

Expand Down
7 changes: 2 additions & 5 deletions Common/Transforms/itkAdvancedImageMomentsCalculator.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -218,11 +218,8 @@ template <typename TImage>
void
AdvancedImageMomentsCalculator<TImage>::LaunchComputeThreaderCallback() const
{
/** Setup threader. */
this->m_Threader->SetSingleMethod(this->ComputeThreaderCallback, &m_ThreaderParameters);

/** Launch. */
this->m_Threader->SingleMethodExecute();
/** Setup threader and launch. */
this->m_Threader->SetSingleMethodAndExecute(this->ComputeThreaderCallback, &m_ThreaderParameters);

} // end LaunchComputeThreaderCallback()

Expand Down
7 changes: 2 additions & 5 deletions Common/itkComputeDisplacementDistribution.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -284,11 +284,8 @@ template <class TFixedImage, class TTransform>
void
ComputeDisplacementDistribution<TFixedImage, TTransform>::LaunchComputeThreaderCallback() const
{
/** Setup threader. */
this->m_Threader->SetSingleMethod(this->ComputeThreaderCallback, &m_ThreaderParameters);

/** Launch. */
this->m_Threader->SingleMethodExecute();
/** Setup threader and launch. */
this->m_Threader->SetSingleMethodAndExecute(this->ComputeThreaderCallback, &m_ThreaderParameters);

} // end LaunchComputeThreaderCallback()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -619,8 +619,7 @@ AdvancedKappaStatisticImageToImageMetric<TFixedImage, TMovingImage>::AfterThread
userData.st_Coefficient2 = tmp2;
userData.st_DerivativePointer = derivative.begin();

this->m_Threader->SetSingleMethod(AccumulateDerivativesThreaderCallback, &userData);
this->m_Threader->SingleMethodExecute();
this->m_Threader->SetSingleMethodAndExecute(AccumulateDerivativesThreaderCallback, &userData);
}

} // end AfterThreadedGetValueAndDerivative()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -592,9 +592,8 @@ ParzenWindowMutualInformationImageToImageMetric<TFixedImage, TMovingImage>::Afte
Superclass::m_ThreaderMetricParameters.st_DerivativePointer = derivative.begin();
Superclass::m_ThreaderMetricParameters.st_NormalizationFactor = 1.0;

this->m_Threader->SetSingleMethod(this->AccumulateDerivativesThreaderCallback,
&(Superclass::m_ThreaderMetricParameters));
this->m_Threader->SingleMethodExecute();
this->m_Threader->SetSingleMethodAndExecute(this->AccumulateDerivativesThreaderCallback,
&(Superclass::m_ThreaderMetricParameters));
}

} // end AfterThreadedComputeDerivativeLowMemory()
Expand Down Expand Up @@ -632,14 +631,11 @@ void
ParzenWindowMutualInformationImageToImageMetric<TFixedImage,
TMovingImage>::LaunchComputeDerivativeLowMemoryThreaderCallback() const
{
/** Setup threader. */
this->m_Threader->SetSingleMethod(
/** Setup threader and launch. */
this->m_Threader->SetSingleMethodAndExecute(
this->ComputeDerivativeLowMemoryThreaderCallback,
const_cast<void *>(static_cast<const void *>(&this->m_ParzenWindowMutualInformationThreaderParameters)));

/** Launch. */
this->m_Threader->SingleMethodExecute();

} // end LaunchComputeDerivativeLowMemoryThreaderCallback()


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -686,9 +686,8 @@ AdvancedMeanSquaresImageToImageMetric<TFixedImage, TMovingImage>::AfterThreadedG
Superclass::m_ThreaderMetricParameters.st_DerivativePointer = derivative.begin();
Superclass::m_ThreaderMetricParameters.st_NormalizationFactor = 1.0 / normal_sum;

this->m_Threader->SetSingleMethod(this->AccumulateDerivativesThreaderCallback,
&(Superclass::m_ThreaderMetricParameters));
this->m_Threader->SingleMethodExecute();
this->m_Threader->SetSingleMethodAndExecute(this->AccumulateDerivativesThreaderCallback,
&(Superclass::m_ThreaderMetricParameters));
}
#ifdef ELASTIX_USE_OPENMP
// compute multi-threadedly with openmp
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -679,8 +679,7 @@ AdvancedNormalizedCorrelationImageToImageMetric<TFixedImage, TMovingImage>::Afte
userData.st_InvertedDenominator = 1.0 / denom;
userData.st_DerivativePointer = derivative.begin();

this->m_Threader->SetSingleMethod(AccumulateDerivativesThreaderCallback, &userData);
this->m_Threader->SingleMethodExecute();
this->m_Threader->SetSingleMethodAndExecute(AccumulateDerivativesThreaderCallback, &userData);
}
#ifdef ELASTIX_USE_OPENMP
// compute multi-threadedly with openmp
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -587,9 +587,8 @@ TransformBendingEnergyPenaltyTerm<TFixedImage, TScalarType>::AfterThreadedGetVal
Superclass::m_ThreaderMetricParameters.st_NormalizationFactor =
static_cast<DerivativeValueType>(this->m_NumberOfPixelsCounted);

this->m_Threader->SetSingleMethod(this->AccumulateDerivativesThreaderCallback,
&(Superclass::m_ThreaderMetricParameters));
this->m_Threader->SingleMethodExecute();
this->m_Threader->SetSingleMethodAndExecute(this->AccumulateDerivativesThreaderCallback,
&(Superclass::m_ThreaderMetricParameters));
}
#ifdef ELASTIX_USE_OPENMP
// compute multi-threadedly with openmp
Expand Down
18 changes: 7 additions & 11 deletions Components/Metrics/PCAMetric/itkPCAMetric_F_multithreaded.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -884,11 +884,9 @@ PCAMetric<TFixedImage, TMovingImage>::LaunchGetSamplesThreaderCallback() const
// \todo: is a global threader better performance-wise? check
auto local_threader = ThreaderType::New();
local_threader->SetNumberOfWorkUnits(Self::GetNumberOfWorkUnits());
local_threader->SetSingleMethod(this->GetSamplesThreaderCallback,
const_cast<void *>(static_cast<const void *>(&this->m_PCAMetricThreaderParameters)));

/** Launch. */
local_threader->SingleMethodExecute();
local_threader->SetSingleMethodAndExecute(
this->GetSamplesThreaderCallback,
const_cast<void *>(static_cast<const void *>(&this->m_PCAMetricThreaderParameters)));

} // end LaunchGetSamplesThreaderCallback()

Expand Down Expand Up @@ -1084,15 +1082,13 @@ template <class TFixedImage, class TMovingImage>
void
PCAMetric<TFixedImage, TMovingImage>::LaunchComputeDerivativeThreaderCallback() const
{
/** Setup local threader. */
/** Setup local threader and launch. */
// \todo: is a global threader better performance-wise? check
auto local_threader = ThreaderType::New();
local_threader->SetNumberOfWorkUnits(Self::GetNumberOfWorkUnits());
local_threader->SetSingleMethod(this->ComputeDerivativeThreaderCallback,
const_cast<void *>(static_cast<const void *>(&this->m_PCAMetricThreaderParameters)));

/** Launch. */
local_threader->SingleMethodExecute();
local_threader->SetSingleMethodAndExecute(
this->ComputeDerivativeThreaderCallback,
const_cast<void *>(static_cast<const void *>(&this->m_PCAMetricThreaderParameters)));

} // end LaunchComputeDerivativeThreaderCallback()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -651,9 +651,8 @@ SumSquaredTissueVolumeDifferenceImageToImageMetric<TFixedImage, TMovingImage>::A
Superclass::m_ThreaderMetricParameters.st_NormalizationFactor =
static_cast<DerivativeValueType>(this->m_NumberOfPixelsCounted);

this->m_Threader->SetSingleMethod(this->AccumulateDerivativesThreaderCallback,
&(Superclass::m_ThreaderMetricParameters));
this->m_Threader->SingleMethodExecute();
this->m_Threader->SetSingleMethodAndExecute(this->AccumulateDerivativesThreaderCallback,
&(Superclass::m_ThreaderMetricParameters));
}

#ifdef ELASTIX_USE_OPENMP
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -291,8 +291,7 @@ StochasticVarianceReducedGradientDescentOptimizer::AdvanceOneStep()
/** Call multi-threaded AdvanceOneStep(). */
auto local_threader = ThreaderType::New();
local_threader->SetNumberOfWorkUnits(this->m_Threader->GetNumberOfWorkUnits());
local_threader->SetSingleMethod(AdvanceOneStepThreaderCallback, &temp);
local_threader->SingleMethodExecute();
local_threader->SetSingleMethodAndExecute(AdvanceOneStepThreaderCallback, &temp);
}

this->InvokeEvent(IterationEvent());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -290,8 +290,7 @@ StochasticGradientDescentOptimizer::AdvanceOneStep()
/** Call multi-threaded AdvanceOneStep(). */
auto local_threader = ThreaderType::New();
local_threader->SetNumberOfWorkUnits(this->m_Threader->GetNumberOfWorkUnits());
local_threader->SetSingleMethod(AdvanceOneStepThreaderCallback, &temp);
local_threader->SingleMethodExecute();
local_threader->SetSingleMethodAndExecute(AdvanceOneStepThreaderCallback, &temp);
}

this->InvokeEvent(IterationEvent());
Expand Down
4 changes: 2 additions & 2 deletions Testing/itkAccumulateDerivativesParallellizationTest.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,8 @@ class MetricTEMP : public itk::Object
this->m_ThreaderMetricParameters.st_DerivativePointer = derivative.begin();
this->m_ThreaderMetricParameters.st_NormalizationFactor = 1.0 / normal_sum;

this->m_Threader->SetSingleMethod(this->AccumulateDerivativesThreaderCallback, &this->m_ThreaderMetricParameters);
this->m_Threader->SingleMethodExecute();
this->m_Threader->SetSingleMethodAndExecute(this->AccumulateDerivativesThreaderCallback,
&this->m_ThreaderMetricParameters);
}
#ifdef ELASTIX_USE_OPENMP
// compute multi-threadedly with openmp
Expand Down
3 changes: 1 addition & 2 deletions Testing/itkAdvanceOneStepParallellizationTest.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -162,8 +162,7 @@ class OptimizerTEMP : public itk::Object
userData.t_Optimizer = this;

/** Call multi-threaded AdvanceOneStep(). */
this->m_Threader->SetSingleMethod(AdvanceOneStepThreaderCallback, &userData);
this->m_Threader->SingleMethodExecute();
this->m_Threader->SetSingleMethodAndExecute(AdvanceOneStepThreaderCallback, &userData);
}
} // end

Expand Down

0 comments on commit 2b764d7

Please sign in to comment.