Skip to content

Commit

Permalink
BUG: fixed bad assumptions about ITK_DEFAULT_MAX_THREADS in tests
Browse files Browse the repository at this point in the history
GetGlobalDefaultNumberOfThreads() and GetNumberOfWorkUnits() in fact can return less that you ask it to, because it's also clamped to the compile-time choice of ITK_DEFAULT_MAX_THREADS.
  • Loading branch information
seanm authored and dzenanz committed Jan 30, 2025
1 parent b87a8b0 commit 7414f89
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -228,8 +228,8 @@ itkDataObjectAndProcessObjectTest(int, char *[])
ITK_TEST_SET_GET_VALUE(true, process->GetReleaseDataBeforeUpdateFlag());

ITK_TEST_EXPECT_TRUE(itk::MultiThreaderBase::GetGlobalDefaultNumberOfThreads() <= process->GetNumberOfWorkUnits());
process->SetNumberOfWorkUnits(11);
ITK_TEST_SET_GET_VALUE(11, process->GetNumberOfWorkUnits());
process->SetNumberOfWorkUnits(ITK_MAX_THREADS - 1);
ITK_TEST_SET_GET_VALUE(ITK_MAX_THREADS - 1, process->GetNumberOfWorkUnits());
process->SetNumberOfWorkUnits(0);
ITK_TEST_SET_GET_VALUE(1, process->GetNumberOfWorkUnits());
process->SetNumberOfWorkUnits(itk::NumericTraits<itk::ThreadIdType>::max());
Expand Down
6 changes: 4 additions & 2 deletions Modules/Core/Common/test/itkMultiThreaderExceptionsTest.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
*
*=========================================================================*/

#include <algorithm>

#include "itkImageSource.h"
#include "itkTestingMacros.h"

Expand Down Expand Up @@ -56,7 +58,7 @@ class ITK_TEMPLATE_EXPORT DummyImageSource : public ImageSource<TOutputImage>
TOutputImage * output = nullptr;
output = this->GetOutput(0);
typename TOutputImage::RegionType largestPossibleRegion;
largestPossibleRegion.SetSize({ { 4 } });
largestPossibleRegion.SetSize({ { std::min(4, ITK_DEFAULT_MAX_THREADS) } });
output->SetLargestPossibleRegion(largestPossibleRegion);
}

Expand Down Expand Up @@ -96,7 +98,7 @@ itkMultiThreaderExceptionsTest(int, char *[])
const typename itk::DummyImageSource<OutputImageType>::Pointer dummySrc =
itk::DummyImageSource<OutputImageType>::New();
dummySrc->SetNumberOfWorkUnits(4);
for (itk::IndexValueType i = 0; i < 4; ++i)
for (itk::IndexValueType i = 0; i < dummySrc->GetNumberOfWorkUnits(); ++i)
{
dummySrc->SetExceptionIndex(i);
ITK_TRY_EXPECT_EXCEPTION(dummySrc->Update());
Expand Down
8 changes: 5 additions & 3 deletions Modules/Core/Common/test/itkMultiThreadingEnvironmentTest.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,12 @@ itkMultiThreadingEnvironmentTest(int argc, char * argv[])
{
return EXIT_FAILURE;
}
if (itk::MultiThreaderBase::GetGlobalDefaultNumberOfThreads() != requiredValue)
const auto actualValue = itk::MultiThreaderBase::GetGlobalDefaultNumberOfThreads();
if ((actualValue != requiredValue) &&
(actualValue != ITK_MAX_THREADS))
{
std::cout << "ERROR: Wrong number of maximum number of threads set from environment. " << requiredValue
<< " != " << itk::MultiThreaderBase::GetGlobalDefaultNumberOfThreads() << std::endl;
std::cout << "ERROR: Wrong number of maximum number of threads set from environment. " << actualValue
<< " != " << requiredValue << " or " << ITK_MAX_THREADS << std::endl;
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -269,22 +269,23 @@ itkThreadedIteratorRangePartitionerTest3(int, char *[])
using DomainContainerType = IteratorRangeDomainThreaderAssociate::DomainContainerType;
auto container = DomainContainerType::New();

for (unsigned int i = 0; i < ITK_DEFAULT_MAX_THREADS + 10; ++i)
// ITK_DEFAULT_MAX_THREADS is at least 1, so we have at least 100 elements in container.
for (unsigned int i = 0; i < ITK_DEFAULT_MAX_THREADS + 99; ++i)
{
container->SetElement(static_cast<int>(i * 2), 2 * i + 1);
}
DomainType fullDomain(container->Begin(), container->End());

/* Test with single thread */
setStartEnd(0, 103, container, fullDomain);
setStartEnd(0, 100, container, fullDomain);
itk::ThreadIdType numberOfThreads = 1;
if (ThreadedIteratorRangePartitionerRunTest(enclosingClass, numberOfThreads, fullDomain) != EXIT_SUCCESS)
{
return EXIT_FAILURE;
}

/* Test with range that doesn't start at 0 */
setStartEnd(2, 105, container, fullDomain);
setStartEnd(5, 100, container, fullDomain);
numberOfThreads = 1;
if (ThreadedIteratorRangePartitionerRunTest(enclosingClass, numberOfThreads, fullDomain) != EXIT_SUCCESS)
{
Expand All @@ -295,7 +296,7 @@ itkThreadedIteratorRangePartitionerTest3(int, char *[])
if (domainThreader->GetMultiThreader()->GetGlobalMaximumNumberOfThreads() > 1)
{
/* Test with default number of threads. */
setStartEnd(6, 109, container, fullDomain);
setStartEnd(6, 89, container, fullDomain);
numberOfThreads = domainThreader->GetMultiThreader()->GetGlobalDefaultNumberOfThreads();
if (ThreadedIteratorRangePartitionerRunTest(enclosingClass, numberOfThreads, fullDomain) != EXIT_SUCCESS)
{
Expand Down

0 comments on commit 7414f89

Please sign in to comment.