Repository navigation
Conversation
`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`.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
CombinationImageToImageMetric::Initialize()saves the number of work units of the combination metric, and assigns it to each sub-metric after theInitialize()of that sub-metric: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,ThreadedGetValueAndDerivativeaccesses the per-work-unit data out of bounds.This happens for a multi-metric registration with the
-threadsoption (ElastixRegistrationMethod::SetNumberOfThreads) when ITK has TBB as its default threader:-threads Nsets 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 defaultPoolMultiThreaderboth 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 withMultiMetricMultiResolutionRegistration,AdvancedMattesMutualInformation+TransformBendingEnergyPenalty,BSplineTransform,AdaptiveStochasticGradientDescentandSetNumberOfThreads(2)ends in a segmentation fault, inParzenWindowHistogramImageToImageMetric::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.InitializePreservesNumberOfWorkUnitsOfSubMetricin CommonGTest. It does not depend on the threader: it gives a sub-metric fewer, and more, work units than the combination metric, and checks thatInitialize()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:
SetNumberOfThreadschanges the global number of threads of ITK for the rest of the test executable, and it only fails with TBB.🤖 Generated with Claude Code