Skip to content

Commit

Permalink
ENH: Add NumberOfThreads property to itk::TransformixFilter
Browse files Browse the repository at this point in the history
Addressed issue InsightSoftwareConsortium/ITKElastix#163 "No option to set number of threads for Transformix", submitted by Sebastian van der Voort.
  • Loading branch information
N-Dekker committed Aug 26, 2022
1 parent 6bcce44 commit c3dd02c
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 0 deletions.
42 changes: 42 additions & 0 deletions Core/Main/GTesting/itkTransformixFilterGTest.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ using ParameterValuesType = itk::ParameterFileParser::ParameterValuesType;

// Using-declarations:
using elx::CoreMainGTestUtilities::CheckNew;
using elx::CoreMainGTestUtilities::CreateImage;
using elx::CoreMainGTestUtilities::CreateImageFilledWithSequenceOfNaturalNumbers;
using elx::CoreMainGTestUtilities::Deref;
using elx::CoreMainGTestUtilities::DerefSmartPointer;
Expand Down Expand Up @@ -445,6 +446,47 @@ GTEST_TEST(itkTransformixFilter, IsDefaultInitialized)
EXPECT_FALSE(transformixFilter.GetLogToConsole());
EXPECT_FALSE(transformixFilter.GetLogToFile());
EXPECT_EQ(transformixFilter.GetOutputMesh(), nullptr);
EXPECT_EQ(transformixFilter.GetNumberOfThreads(), 0);
}


GTEST_TEST(itkTransformixFilter, NumberOfThreadsEqualsGlobalMaximumNumberOfThreads)
{
constexpr auto ImageDimension = 2U;
using PixelType = float;
using ImageType = itk::Image<PixelType, ImageDimension>;
using TransformixFilterType = itk::TransformixFilter<ImageType>;

elx::DefaultConstruct<TransformixFilterType> transformixFilter;

constexpr auto imageSize = ImageType::SizeType::Filled(1);
const auto image = CreateImage<PixelType>(imageSize);

transformixFilter.SetMovingImage(image);
transformixFilter.SetTransformParameterObject(
CreateParameterObject({ // Parameters in alphabetic order:
{ "Direction", CreateDefaultDirectionParameterValues<ImageDimension>() },
{ "Index", ParameterValuesType(ImageDimension, "0") },
{ "NumberOfParameters", { std::to_string(ImageDimension) } },
{ "Origin", ParameterValuesType(ImageDimension, "0") },
{ "ResampleInterpolator", { "FinalLinearInterpolator" } },
{ "Size", ConvertToParameterValues(imageSize) },
{ "Transform", ParameterValuesType{ "TranslationTransform" } },
{ "TransformParameters", ParameterValuesType(ImageDimension, "0") },
{ "Spacing", ParameterValuesType(ImageDimension, "1") } }));

const auto initialGlobalMaximumNumberOfThreads = itk::MultiThreaderBase::GetGlobalMaximumNumberOfThreads();
ASSERT_GE(initialGlobalMaximumNumberOfThreads, 1);
ASSERT_LE(initialGlobalMaximumNumberOfThreads, itk::ITK_MAX_THREADS);

for (const int numberOfThreads :
{ 1, 2, int{ itk::ITK_MAX_THREADS }, static_cast<int>(initialGlobalMaximumNumberOfThreads) })
{
transformixFilter.SetNumberOfThreads(numberOfThreads);
ASSERT_EQ(transformixFilter.GetNumberOfThreads(), numberOfThreads);
transformixFilter.Update();
EXPECT_EQ(itk::MultiThreaderBase::GetGlobalMaximumNumberOfThreads(), numberOfThreads);
}
}


Expand Down
5 changes: 5 additions & 0 deletions Core/Main/itkTransformixFilter.h
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,9 @@ class ITK_TEMPLATE_EXPORT TransformixFilter : public ImageSource<TMovingImage>
m_EnableOutput = false;
}

itkSetMacro(NumberOfThreads, int);
itkGetConstMacro(NumberOfThreads, int);

/** Sets an (optional) input mesh. An Update() will transform its points, and store them in the output mesh. */
void
SetInputMesh(typename MeshType::ConstPointer mesh)
Expand Down Expand Up @@ -264,6 +267,8 @@ class ITK_TEMPLATE_EXPORT TransformixFilter : public ImageSource<TMovingImage>
bool m_LogToConsole{ false };
bool m_LogToFile{ false };

int m_NumberOfThreads{ 0 };

typename MeshType::ConstPointer m_InputMesh{ nullptr };
typename MeshType::Pointer m_OutputMesh{ nullptr };
};
Expand Down
5 changes: 5 additions & 0 deletions Core/Main/itkTransformixFilter.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,11 @@ TransformixFilter<TMovingImage>::GenerateData()
}
}

if (m_NumberOfThreads > 0)
{
argumentMap.insert(ArgumentMapEntryType("-threads", std::to_string(m_NumberOfThreads)));
}

// Setup xout
const auto manager = m_EnableOutput
? std::make_unique<const elx::xoutManager>(logFileName, m_LogToFile, m_LogToConsole)
Expand Down

0 comments on commit c3dd02c

Please sign in to comment.