Skip to content

Commit

Permalink
STYLE: Declare "PerThreadVariables" data members as std::vector (4x)
Browse files Browse the repository at this point in the history
Instead of declaring these "PerThreadVariables" data members as raw pointers, of four `Metric` types. Removed the corresponding "PerThreadVariablesSize" data members. Defaulted ( `= default`) their destructors.Replaced `for (ThreadIdType i = 0; i < numberOfThreads; ++i)` loops by range-based for-loops.

Follow-up to pull request #644 commit 398cea9 "STYLE: Declared `m_ComputePerThreadVariables` as an `std::vector` (2 x)"
  • Loading branch information
N-Dekker committed May 17, 2022
1 parent 0248973 commit 237e16a
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 136 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

#include "itkAdvancedImageToImageMetric.h"
#include "itkKernelFunctionBase2.h"
#include <vector>


namespace itk
Expand Down Expand Up @@ -217,7 +218,7 @@ class ITK_TEMPLATE_EXPORT ParzenWindowHistogramImageToImageMetric
ParzenWindowHistogramImageToImageMetric();

/** The destructor. */
~ParzenWindowHistogramImageToImageMetric() override;
~ParzenWindowHistogramImageToImageMetric() override = default;

/** Print Self. */
void
Expand Down Expand Up @@ -501,9 +502,8 @@ class ITK_TEMPLATE_EXPORT ParzenWindowHistogramImageToImageMetric
itkAlignedTypedef(ITK_CACHE_LINE_ALIGNMENT,
PaddedParzenWindowHistogramGetValueAndDerivativePerThreadStruct,
AlignedParzenWindowHistogramGetValueAndDerivativePerThreadStruct);
mutable AlignedParzenWindowHistogramGetValueAndDerivativePerThreadStruct *
m_ParzenWindowHistogramGetValueAndDerivativePerThreadVariables;
mutable ThreadIdType m_ParzenWindowHistogramGetValueAndDerivativePerThreadVariablesSize;
mutable std::vector<AlignedParzenWindowHistogramGetValueAndDerivativePerThreadStruct>
m_ParzenWindowHistogramGetValueAndDerivativePerThreadVariables;

/** Variables that can/should be accessed by their Set/Get functions. */
unsigned long m_NumberOfFixedHistogramBins;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,24 +71,9 @@ ParzenWindowHistogramImageToImageMetric<TFixedImage, TMovingImage>::ParzenWindow
/** Initialize the m_ParzenWindowHistogramThreaderParameters */
this->m_ParzenWindowHistogramThreaderParameters.m_Metric = this;

// Multi-threading structs
this->m_ParzenWindowHistogramGetValueAndDerivativePerThreadVariables = nullptr;
this->m_ParzenWindowHistogramGetValueAndDerivativePerThreadVariablesSize = 0;

} // end Constructor


/**
* ******************* Destructor *******************
*/

template <class TFixedImage, class TMovingImage>
ParzenWindowHistogramImageToImageMetric<TFixedImage, TMovingImage>::~ParzenWindowHistogramImageToImageMetric()
{
delete[] this->m_ParzenWindowHistogramGetValueAndDerivativePerThreadVariables;
} // end Destructor


/**
* ********************* PrintSelf ******************************
*/
Expand Down Expand Up @@ -458,22 +443,15 @@ ParzenWindowHistogramImageToImageMetric<TFixedImage, TMovingImage>::InitializeTh
const ThreadIdType numberOfThreads = Self::GetNumberOfWorkUnits();

/** Only resize the array of structs when needed. */
if (this->m_ParzenWindowHistogramGetValueAndDerivativePerThreadVariablesSize != numberOfThreads)
{
delete[] this->m_ParzenWindowHistogramGetValueAndDerivativePerThreadVariables;
this->m_ParzenWindowHistogramGetValueAndDerivativePerThreadVariables =
new AlignedParzenWindowHistogramGetValueAndDerivativePerThreadStruct[numberOfThreads];
this->m_ParzenWindowHistogramGetValueAndDerivativePerThreadVariablesSize = numberOfThreads;
}
m_ParzenWindowHistogramGetValueAndDerivativePerThreadVariables.resize(numberOfThreads);

/** Some initialization. */
for (ThreadIdType i = 0; i < numberOfThreads; ++i)
for (auto & perThreadVariable : m_ParzenWindowHistogramGetValueAndDerivativePerThreadVariables)
{
this->m_ParzenWindowHistogramGetValueAndDerivativePerThreadVariables[i].st_NumberOfPixelsCounted =
NumericTraits<SizeValueType>::Zero;
perThreadVariable.st_NumberOfPixelsCounted = NumericTraits<SizeValueType>::Zero;

// Initialize the joint pdf
JointPDFPointer & jointPDF = this->m_ParzenWindowHistogramGetValueAndDerivativePerThreadVariables[i].st_JointPDF;
JointPDFPointer & jointPDF = perThreadVariable.st_JointPDF;
if (jointPDF.IsNull())
{
jointPDF = JointPDFType::New();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#define itkAdvancedKappaStatisticImageToImageMetric_h

#include "itkAdvancedImageToImageMetric.h"
#include <vector>

namespace itk
{
Expand Down Expand Up @@ -168,7 +169,7 @@ class ITK_TEMPLATE_EXPORT AdvancedKappaStatisticImageToImageMetric

protected:
AdvancedKappaStatisticImageToImageMetric();
~AdvancedKappaStatisticImageToImageMetric() override;
~AdvancedKappaStatisticImageToImageMetric() override = default;

/** PrintSelf. */
void
Expand Down Expand Up @@ -259,8 +260,7 @@ class ITK_TEMPLATE_EXPORT AdvancedKappaStatisticImageToImageMetric
itkAlignedTypedef(ITK_CACHE_LINE_ALIGNMENT,
PaddedKappaGetValueAndDerivativePerThreadStruct,
AlignedKappaGetValueAndDerivativePerThreadStruct);
mutable AlignedKappaGetValueAndDerivativePerThreadStruct * m_KappaGetValueAndDerivativePerThreadVariables;
mutable ThreadIdType m_KappaGetValueAndDerivativePerThreadVariablesSize;
mutable std::vector<AlignedKappaGetValueAndDerivativePerThreadStruct> m_KappaGetValueAndDerivativePerThreadVariables;
};

} // end namespace itk
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,24 +41,9 @@ AdvancedKappaStatisticImageToImageMetric<TFixedImage, TMovingImage>::AdvancedKap
this->m_Epsilon = 1e-3;
this->m_Complement = true;

// Multi-threading structs
this->m_KappaGetValueAndDerivativePerThreadVariables = nullptr;
this->m_KappaGetValueAndDerivativePerThreadVariablesSize = 0;

} // end Constructor


/**
* ******************* Destructor *******************
*/

template <class TFixedImage, class TMovingImage>
AdvancedKappaStatisticImageToImageMetric<TFixedImage, TMovingImage>::~AdvancedKappaStatisticImageToImageMetric()
{
delete[] this->m_KappaGetValueAndDerivativePerThreadVariables;
} // end Destructor


/**
* ******************* InitializeThreadingParameters *******************
*/
Expand All @@ -77,26 +62,20 @@ AdvancedKappaStatisticImageToImageMetric<TFixedImage, TMovingImage>::InitializeT
const ThreadIdType numberOfThreads = Self::GetNumberOfWorkUnits();

/** Only resize the array of structs when needed. */
if (this->m_KappaGetValueAndDerivativePerThreadVariablesSize != numberOfThreads)
{
delete[] this->m_KappaGetValueAndDerivativePerThreadVariables;
this->m_KappaGetValueAndDerivativePerThreadVariables =
new AlignedKappaGetValueAndDerivativePerThreadStruct[numberOfThreads];
this->m_KappaGetValueAndDerivativePerThreadVariablesSize = numberOfThreads;
}
m_KappaGetValueAndDerivativePerThreadVariables.resize(numberOfThreads);

/** Some initialization. */
const SizeValueType zero1 = NumericTraits<SizeValueType>::Zero;
const DerivativeValueType zero2 = NumericTraits<DerivativeValueType>::Zero;
for (ThreadIdType i = 0; i < numberOfThreads; ++i)
for (auto & perThreadVariable : m_KappaGetValueAndDerivativePerThreadVariables)
{
this->m_KappaGetValueAndDerivativePerThreadVariables[i].st_NumberOfPixelsCounted = zero1;
this->m_KappaGetValueAndDerivativePerThreadVariables[i].st_AreaSum = zero1;
this->m_KappaGetValueAndDerivativePerThreadVariables[i].st_AreaIntersection = zero1;
this->m_KappaGetValueAndDerivativePerThreadVariables[i].st_DerivativeSum1.SetSize(this->GetNumberOfParameters());
this->m_KappaGetValueAndDerivativePerThreadVariables[i].st_DerivativeSum2.SetSize(this->GetNumberOfParameters());
this->m_KappaGetValueAndDerivativePerThreadVariables[i].st_DerivativeSum1.Fill(zero2);
this->m_KappaGetValueAndDerivativePerThreadVariables[i].st_DerivativeSum2.Fill(zero2);
perThreadVariable.st_NumberOfPixelsCounted = zero1;
perThreadVariable.st_AreaSum = zero1;
perThreadVariable.st_AreaIntersection = zero1;
perThreadVariable.st_DerivativeSum1.SetSize(this->GetNumberOfParameters());
perThreadVariable.st_DerivativeSum2.SetSize(this->GetNumberOfParameters());
perThreadVariable.st_DerivativeSum1.Fill(zero2);
perThreadVariable.st_DerivativeSum2.Fill(zero2);
}

} // end InitializeThreadingParameters()
Expand Down Expand Up @@ -698,14 +677,14 @@ AdvancedKappaStatisticImageToImageMetric<TFixedImage, TMovingImage>::AccumulateD
for (unsigned int j = jmin; j < jmax; ++j)
{
sum1 = sum2 = zero;
for (ThreadIdType i = 0; i < nrOfThreads; ++i)
for (auto & perThreadVariable : temp->st_Metric->m_KappaGetValueAndDerivativePerThreadVariables)
{
sum1 += temp->st_Metric->m_KappaGetValueAndDerivativePerThreadVariables[i].st_DerivativeSum1[j];
sum2 += temp->st_Metric->m_KappaGetValueAndDerivativePerThreadVariables[i].st_DerivativeSum2[j];
sum1 += perThreadVariable.st_DerivativeSum1[j];
sum2 += perThreadVariable.st_DerivativeSum2[j];

/** Reset these variables for the next iteration. */
temp->st_Metric->m_KappaGetValueAndDerivativePerThreadVariables[i].st_DerivativeSum1[j] = zero;
temp->st_Metric->m_KappaGetValueAndDerivativePerThreadVariables[i].st_DerivativeSum2[j] = zero;
perThreadVariable.st_DerivativeSum1[j] = zero;
perThreadVariable.st_DerivativeSum2[j] = zero;
}
temp->st_DerivativePointer[j] = temp->st_Coefficient1 * sum1 - temp->st_Coefficient2 * sum2;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#define itkAdvancedNormalizedCorrelationImageToImageMetric_h

#include "itkAdvancedImageToImageMetric.h"
#include <vector>

namespace itk
{
Expand Down Expand Up @@ -184,7 +185,7 @@ class ITK_TEMPLATE_EXPORT AdvancedNormalizedCorrelationImageToImageMetric

protected:
AdvancedNormalizedCorrelationImageToImageMetric();
~AdvancedNormalizedCorrelationImageToImageMetric() override;
~AdvancedNormalizedCorrelationImageToImageMetric() override = default;

void
PrintSelf(std::ostream & os, Indent indent) const override;
Expand Down Expand Up @@ -275,8 +276,8 @@ class ITK_TEMPLATE_EXPORT AdvancedNormalizedCorrelationImageToImageMetric
itkAlignedTypedef(ITK_CACHE_LINE_ALIGNMENT,
PaddedCorrelationGetValueAndDerivativePerThreadStruct,
AlignedCorrelationGetValueAndDerivativePerThreadStruct);
mutable AlignedCorrelationGetValueAndDerivativePerThreadStruct * m_CorrelationGetValueAndDerivativePerThreadVariables;
mutable ThreadIdType m_CorrelationGetValueAndDerivativePerThreadVariablesSize;
mutable std::vector<AlignedCorrelationGetValueAndDerivativePerThreadStruct>
m_CorrelationGetValueAndDerivativePerThreadVariables;
};

} // end namespace itk
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,25 +41,9 @@ AdvancedNormalizedCorrelationImageToImageMetric<TFixedImage,
this->SetUseFixedImageLimiter(false);
this->SetUseMovingImageLimiter(false);

// Multi-threading structs
this->m_CorrelationGetValueAndDerivativePerThreadVariables = nullptr;
this->m_CorrelationGetValueAndDerivativePerThreadVariablesSize = 0;

} // end Constructor


/**
* ******************* Destructor *******************
*/

template <class TFixedImage, class TMovingImage>
AdvancedNormalizedCorrelationImageToImageMetric<TFixedImage,
TMovingImage>::~AdvancedNormalizedCorrelationImageToImageMetric()
{
delete[] this->m_CorrelationGetValueAndDerivativePerThreadVariables;
} // end Destructor


/**
* ******************* InitializeThreadingParameters *******************
*/
Expand All @@ -78,34 +62,26 @@ AdvancedNormalizedCorrelationImageToImageMetric<TFixedImage, TMovingImage>::Init
*/

/** Only resize the array of structs when needed. */
if (this->m_CorrelationGetValueAndDerivativePerThreadVariablesSize != numberOfThreads)
{
delete[] this->m_CorrelationGetValueAndDerivativePerThreadVariables;
this->m_CorrelationGetValueAndDerivativePerThreadVariables =
new AlignedCorrelationGetValueAndDerivativePerThreadStruct[numberOfThreads];
this->m_CorrelationGetValueAndDerivativePerThreadVariablesSize = numberOfThreads;
}
m_CorrelationGetValueAndDerivativePerThreadVariables.resize(numberOfThreads);

/** Some initialization. */
const AccumulateType zero1 = NumericTraits<AccumulateType>::Zero;
const DerivativeValueType zero2 = NumericTraits<DerivativeValueType>::Zero;
const auto numberOfParameters = this->GetNumberOfParameters();
for (ThreadIdType i = 0; i < numberOfThreads; ++i)
for (auto & perThreadVariable : m_CorrelationGetValueAndDerivativePerThreadVariables)
{
this->m_CorrelationGetValueAndDerivativePerThreadVariables[i].st_NumberOfPixelsCounted =
NumericTraits<SizeValueType>::Zero;
this->m_CorrelationGetValueAndDerivativePerThreadVariables[i].st_Sff = zero1;
this->m_CorrelationGetValueAndDerivativePerThreadVariables[i].st_Smm = zero1;
this->m_CorrelationGetValueAndDerivativePerThreadVariables[i].st_Sfm = zero1;
this->m_CorrelationGetValueAndDerivativePerThreadVariables[i].st_Sf = zero1;
this->m_CorrelationGetValueAndDerivativePerThreadVariables[i].st_Sm = zero1;
this->m_CorrelationGetValueAndDerivativePerThreadVariables[i].st_DerivativeF.SetSize(numberOfParameters);
this->m_CorrelationGetValueAndDerivativePerThreadVariables[i].st_DerivativeM.SetSize(numberOfParameters);
this->m_CorrelationGetValueAndDerivativePerThreadVariables[i].st_Differential.SetSize(
this->GetNumberOfParameters());
this->m_CorrelationGetValueAndDerivativePerThreadVariables[i].st_DerivativeF.Fill(zero2);
this->m_CorrelationGetValueAndDerivativePerThreadVariables[i].st_DerivativeM.Fill(zero2);
this->m_CorrelationGetValueAndDerivativePerThreadVariables[i].st_Differential.Fill(zero2);
perThreadVariable.st_NumberOfPixelsCounted = NumericTraits<SizeValueType>::Zero;
perThreadVariable.st_Sff = zero1;
perThreadVariable.st_Smm = zero1;
perThreadVariable.st_Sfm = zero1;
perThreadVariable.st_Sf = zero1;
perThreadVariable.st_Sm = zero1;
perThreadVariable.st_DerivativeF.SetSize(numberOfParameters);
perThreadVariable.st_DerivativeM.SetSize(numberOfParameters);
perThreadVariable.st_Differential.SetSize(this->GetNumberOfParameters());
perThreadVariable.st_DerivativeF.Fill(zero2);
perThreadVariable.st_DerivativeM.Fill(zero2);
perThreadVariable.st_Differential.Fill(zero2);
}

} // end InitializeThreadingParameters()
Expand Down
6 changes: 3 additions & 3 deletions Components/Metrics/PCAMetric/itkPCAMetric_F_multithreaded.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include "itkImageRandomCoordinateSampler.h"
#include "itkNearestNeighborInterpolateImageFunction.h"
#include "itkExtractImageFilter.h"
#include <vector>

namespace itk
{
Expand Down Expand Up @@ -135,7 +136,7 @@ class ITK_TEMPLATE_EXPORT PCAMetric : public AdvancedImageToImageMetric<TFixedIm

protected:
PCAMetric();
~PCAMetric() override;
~PCAMetric() override = default;
void
PrintSelf(std::ostream & os, Indent indent) const override;

Expand Down Expand Up @@ -221,8 +222,7 @@ class ITK_TEMPLATE_EXPORT PCAMetric : public AdvancedImageToImageMetric<TFixedIm
PaddedPCAMetricGetSamplesPerThreadStruct,
AlignedPCAMetricGetSamplesPerThreadStruct);

mutable AlignedPCAMetricGetSamplesPerThreadStruct * m_PCAMetricGetSamplesPerThreadVariables;
mutable ThreadIdType m_PCAMetricGetSamplesPerThreadVariablesSize;
mutable std::vector<AlignedPCAMetricGetSamplesPerThreadStruct> m_PCAMetricGetSamplesPerThreadVariables;

unsigned int m_G;
unsigned int m_LastDimIndex;
Expand Down
28 changes: 4 additions & 24 deletions Components/Metrics/PCAMetric/itkPCAMetric_F_multithreaded.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -46,26 +46,11 @@ PCAMetric<TFixedImage, TMovingImage>::PCAMetric()
this->SetUseFixedImageLimiter(false);
this->SetUseMovingImageLimiter(false);

// Multi-threading structs
this->m_PCAMetricGetSamplesPerThreadVariables = nullptr;
this->m_PCAMetricGetSamplesPerThreadVariablesSize = 0;

/** Initialize the m_ParzenWindowHistogramThreaderParameters. */
this->m_PCAMetricThreaderParameters.m_Metric = this;
} // end constructor


/**
* ******************* Destructor *******************
*/

template <class TFixedImage, class TMovingImage>
PCAMetric<TFixedImage, TMovingImage>::~PCAMetric()
{
delete[] this->m_PCAMetricGetSamplesPerThreadVariables;
} // end Destructor


/**
* ******************* Initialize *******************
*/
Expand Down Expand Up @@ -120,18 +105,13 @@ PCAMetric<TFixedImage, TMovingImage>::InitializeThreadingParameters() const
*/

/** Only resize the array of structs when needed. */
if (this->m_PCAMetricGetSamplesPerThreadVariablesSize != numberOfThreads)
{
delete[] this->m_PCAMetricGetSamplesPerThreadVariables;
this->m_PCAMetricGetSamplesPerThreadVariables = new AlignedPCAMetricGetSamplesPerThreadStruct[numberOfThreads];
this->m_PCAMetricGetSamplesPerThreadVariablesSize = numberOfThreads;
}
m_PCAMetricGetSamplesPerThreadVariables.resize(numberOfThreads);

/** Some initialization. */
for (ThreadIdType i = 0; i < numberOfThreads; ++i)
for (auto & perThreadVariable : m_PCAMetricGetSamplesPerThreadVariables)
{
this->m_PCAMetricGetSamplesPerThreadVariables[i].st_NumberOfPixelsCounted = NumericTraits<SizeValueType>::Zero;
this->m_PCAMetricGetSamplesPerThreadVariables[i].st_Derivative.SetSize(this->GetNumberOfParameters());
perThreadVariable.st_NumberOfPixelsCounted = NumericTraits<SizeValueType>::Zero;
perThreadVariable.st_Derivative.SetSize(this->GetNumberOfParameters());
}

this->m_PixelStartIndex.resize(numberOfThreads);
Expand Down

0 comments on commit 237e16a

Please sign in to comment.