Skip to content

BUG: Preserve work units of sub-metrics in CombinationImageToImageMetric - #1505

Open
Voggo wants to merge 1 commit into
SuperElastix:mainfrom
Voggo:CombinationImageToImageMetric-Initialize-work-units
Open

Voggo wants to merge 1 commit into
SuperElastix:mainfrom
Voggo:CombinationImageToImageMetric-Initialize-work-units

Conversation

@Voggo

@Voggo Voggo commented Dec 2, 2026

Copy link
Copy Markdown
Contributor

Problem

CombinationImageToImageMetric::Initialize() saves the number of work units of the combination metric, and assigns it to each sub-metric after the Initialize() of that sub-metric:

unsigned nrOfThreadsPerMetric = this->GetNumberOfWorkUnits();
testPtr1->Initialize();
testPtr1->SetNumberOfWorkUnits(nrOfThreadsPerMetric);

A sub-metric allocates its per-work-unit data during its own Initialize() (InitializeThreadingParameters()), for its own number of work units. When that number differs from the number of the combination metric, the threader of the sub-metric afterwards runs a different number of work units than it has data for. When the sub-metric has fewer, ThreadedGetValueAndDerivative accesses the per-work-unit data out of bounds.

This happens for a multi-metric registration with the -threads option (ElastixRegistrationMethod::SetNumberOfThreads) when ITK has TBB as its default threader: -threads N sets each sub-metric to N work units, while the combination metric has the default of the TBB threader, 16 work units per thread. With the default PoolMultiThreader both numbers are N, so the bug does not show.

Reproduced on main (b399e1b), Linux, GCC 13.3, ITK 5.4.3 built with TBB: a registration with MultiMetricMultiResolutionRegistration, AdvancedMattesMutualInformation + TransformBendingEnergyPenalty, BSplineTransform, AdaptiveStochasticGradientDescent and SetNumberOfThreads(2) ends in a segmentation fault, in ParzenWindowHistogramImageToImageMetric::ThreadedComputePDFs.

Fix

Save and restore the number of work units of the sub-metric itself. (As far as I can see, Initialize() of a sub-metric no longer changes its number of work units, so removing the save and restore entirely would work as well. I kept them to keep the change minimal; I am happy to remove them instead.)

Test

New CombinationImageToImageMetric.InitializePreservesNumberOfWorkUnitsOfSubMetric in CommonGTest. It does not depend on the threader: it gives a sub-metric fewer, and more, work units than the combination metric, and checks that Initialize() preserves them and that the combination metric yields the value and derivative of the sub-metric by itself. Without the fix, the test fails (and then crashes).

I did not add the registration that crashed as a test: SetNumberOfThreads changes the global number of threads of ITK for the rest of the test executable, and it only fails with TBB.

🤖 Generated with Claude Code

`CombinationImageToImageMetric::Initialize()` saved the number of work units
of the combination metric itself,
and assigned that number to each sub-metric after the `Initialize()` of the sub-metric.
When the number of work units of a sub-metric was different from the number of the combination metric,
the per-work-unit data of the sub-metric (allocated during its `Initialize()`)
no longer matched the number of work units its threader would use.
When the sub-metric then had fewer work units than the combination metric,
`ThreadedGetValueAndDerivative` accessed its per-work-unit data out of bounds.

This occurred for a multi-metric registration with the "-threads" option
(`ElastixRegistrationMethod::SetNumberOfThreads`), using an ITK that has TBB as its default threader:
"-threads N" sets the number of work units of each sub-metric to N,
while the combination metric then has 16 x N work units by default.
It caused a segmentation fault in `ParzenWindowHistogramImageToImageMetric::ThreadedComputePDFs`.

Fixed by saving and restoring the number of work units of the sub-metric itself.
Added a GoogleTest unit test, `CombinationImageToImageMetric.InitializePreservesNumberOfWorkUnitsOfSubMetric`.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant