Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved style of m_ComputePerThreadVariable data members #132

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
19 changes: 12 additions & 7 deletions Common/Transforms/itkAdvancedImageMomentsCalculator.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@

#include "itkMultiThreader.h"

#include <vector>

namespace itk
{
/** \class AdvancedImageMomentsCalculator
Expand Down Expand Up @@ -243,7 +245,6 @@ class AdvancedImageMomentsCalculator : public Object
/** Typedefs for multi-threading. */
typedef itk::MultiThreader ThreaderType;
typedef ThreaderType::ThreadInfoStruct ThreadInfoType;
ThreaderType::Pointer m_Threader;

/** Launch MultiThread Compute. */
void LaunchComputeThreaderCallback(void) const;
Expand All @@ -262,8 +263,6 @@ class AdvancedImageMomentsCalculator : public Object
{
Self * st_Self;
};
mutable MultiThreaderParameterType m_ThreaderParameters;

struct ComputePerThreadStruct
{
/** Used for accumulating variables. */
Expand All @@ -278,14 +277,20 @@ class AdvancedImageMomentsCalculator : public Object
PaddedComputePerThreadStruct);
itkAlignedTypedef(ITK_CACHE_LINE_ALIGNMENT, PaddedComputePerThreadStruct,
AlignedComputePerThreadStruct);
mutable AlignedComputePerThreadStruct * m_ComputePerThreadVariables;
mutable ThreadIdType m_ComputePerThreadVariablesSize;
bool m_UseMultiThread;
SizeValueType m_NumberOfPixelsCounted;

/** The type of region used for multithreading */
typedef typename ImageType::RegionType ThreadRegionType;

private:

ThreaderType::Pointer m_Threader;

MultiThreaderParameterType m_ThreaderParameters;

std::vector<AlignedComputePerThreadStruct> m_ComputePerThreadVariables;
bool m_UseMultiThread;
SizeValueType m_NumberOfPixelsCounted;

SizeValueType m_NumberOfSamplesForCenteredTransformInitialization;
InputPixelType m_LowerThresholdForCenterGravity;
bool m_CenterOfGravityUsesLowerThreshold;
Expand Down
24 changes: 3 additions & 21 deletions Common/Transforms/itkAdvancedImageMomentsCalculator.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,6 @@ AdvancedImageMomentsCalculator< TImage >::AdvancedImageMomentsCalculator(void)
this->m_ThreaderParameters.st_Self = this;

// Multi-threading structs
this->m_ComputePerThreadVariables = NULL;
this->m_ComputePerThreadVariablesSize = 0;
this->m_CenterOfGravityUsesLowerThreshold = false;
this->m_NumberOfSamplesForCenteredTransformInitialization = 10000;
this->m_LowerThresholdForCenterGravity = 500;
Expand All @@ -91,7 +89,6 @@ template< typename TImage >
AdvancedImageMomentsCalculator< TImage >::
~AdvancedImageMomentsCalculator()
{
delete[] this->m_ComputePerThreadVariables;
}

/**
Expand All @@ -114,24 +111,9 @@ AdvancedImageMomentsCalculator< TImage >
*/
const ThreadIdType numberOfThreads = this->m_Threader->GetNumberOfThreads();

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

/** Some initialization. */
for (ThreadIdType i = 0; i < numberOfThreads; ++i)
{
this->m_ComputePerThreadVariables[i].st_M0 = NumericTraits< ScalarType >::Zero;
this->m_ComputePerThreadVariables[i].st_M1 = NumericTraits< typename VectorType::ValueType >::Zero;
this->m_ComputePerThreadVariables[i].st_M2.Fill(NumericTraits< typename MatrixType::ValueType >::ZeroValue());
this->m_ComputePerThreadVariables[i].st_Cg = NumericTraits< typename VectorType::ValueType >::Zero;
this->m_ComputePerThreadVariables[i].st_Cm.Fill(NumericTraits< typename MatrixType::ValueType >::ZeroValue());
this->m_ComputePerThreadVariables[i].st_NumberOfPixelsCounted = NumericTraits< SizeValueType >::Zero;
}
/** Resize (if necessary) and assign structs of zero-initialized values */
this->m_ComputePerThreadVariables.resize(numberOfThreads);
this->m_ComputePerThreadVariables.assign(numberOfThreads, AlignedComputePerThreadStruct());

} // end InitializeThreadingParameters()

Expand Down
11 changes: 8 additions & 3 deletions Common/itkComputeDisplacementDistribution.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
#include "itkImageFullSampler.h"
#include "itkMultiThreader.h"

#include <vector>

namespace itk
{
/**\class ComputeDisplacementDistribution
Expand Down Expand Up @@ -198,7 +200,6 @@ class ComputeDisplacementDistribution :
{
Self * st_Self;
};
mutable MultiThreaderParameterType m_ThreaderParameters;

struct ComputePerThreadStruct
{
Expand All @@ -212,8 +213,12 @@ class ComputeDisplacementDistribution :
PaddedComputePerThreadStruct );
itkAlignedTypedef( ITK_CACHE_LINE_ALIGNMENT, PaddedComputePerThreadStruct,
AlignedComputePerThreadStruct );
mutable AlignedComputePerThreadStruct * m_ComputePerThreadVariables;
mutable ThreadIdType m_ComputePerThreadVariablesSize;

private:

MultiThreaderParameterType m_ThreaderParameters;

std::vector<AlignedComputePerThreadStruct> m_ComputePerThreadVariables;

SizeValueType m_NumberOfPixelsCounted;
bool m_UseMultiThread;
Expand Down
24 changes: 3 additions & 21 deletions Common/itkComputeDisplacementDistribution.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,6 @@ ComputeDisplacementDistribution< TFixedImage, TTransform >
/** Initialize the m_ThreaderParameters. */
this->m_ThreaderParameters.st_Self = this;

// Multi-threading structs
this->m_ComputePerThreadVariables = NULL;
this->m_ComputePerThreadVariablesSize = 0;

} // end Constructor


Expand All @@ -78,7 +74,6 @@ template< class TFixedImage, class TTransform >
ComputeDisplacementDistribution< TFixedImage, TTransform >
::~ComputeDisplacementDistribution()
{
delete[] this->m_ComputePerThreadVariables;
} // end Destructor


Expand All @@ -102,22 +97,9 @@ ComputeDisplacementDistribution< TFixedImage, TTransform >
*/
const ThreadIdType numberOfThreads = this->m_Threader->GetNumberOfThreads();

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

/** Some initialization. */
for( ThreadIdType i = 0; i < numberOfThreads; ++i )
{
this->m_ComputePerThreadVariables[ i ].st_MaxJJ = NumericTraits< double >::Zero;
this->m_ComputePerThreadVariables[ i ].st_Displacement = NumericTraits< double >::Zero;
this->m_ComputePerThreadVariables[ i ].st_DisplacementSquared = NumericTraits< double >::Zero;
this->m_ComputePerThreadVariables[ i ].st_NumberOfPixelsCounted = NumericTraits< SizeValueType >::Zero;
}
/** Resize (if necessary) and assign structs of zero-initialized values */
this->m_ComputePerThreadVariables.resize(numberOfThreads);
this->m_ComputePerThreadVariables.assign(numberOfThreads, AlignedComputePerThreadStruct());

} // end InitializeThreadingParameters()

Expand Down