Skip to content

Commit

Permalink
BUG: fixed bad assumptions about GetGlobalDefaultNumberOfThreads() in…
Browse files Browse the repository at this point in the history
… tests

GetGlobalDefaultNumberOfThreads() 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 committed Jan 28, 2025
1 parent fde014a commit a603670
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 9 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
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 a603670

Please sign in to comment.