Skip to content

Commit

Permalink
BUG: ImageFullSampler do mask->UpdateSource() single-threaded
Browse files Browse the repository at this point in the history
It is inefficient to do the same `mask->UpdateSource()` call in multiple threads. Moreover, doing so might lead to race conditions.
  • Loading branch information
N-Dekker committed Feb 1, 2024
1 parent 7268f8c commit f5b9024
Showing 1 changed file with 9 additions and 7 deletions.
16 changes: 9 additions & 7 deletions Common/ImageSamplers/itkImageFullSampler.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,13 @@ template <class TInputImage>
void
ImageFullSampler<TInputImage>::GenerateData()
{
typename MaskType::ConstPointer mask = this->GetMask();

if (mask)
{
mask->UpdateSource();
}

/** If desired we exercise a multi-threaded version. */
if (Superclass::m_UseMultiThread)
{
Expand All @@ -42,9 +49,8 @@ ImageFullSampler<TInputImage>::GenerateData()
}

/** Get handles to the input image, output sample container, and the mask. */
const InputImageType & inputImage = elastix::Deref(this->GetInput());
ImageSampleContainerType & sampleContainer = elastix::Deref(this->GetOutput());
typename MaskType::ConstPointer mask = this->GetMask();
const InputImageType & inputImage = elastix::Deref(this->GetInput());
ImageSampleContainerType & sampleContainer = elastix::Deref(this->GetOutput());

// Take capacity from the output container, and clear it.
std::vector<ImageSampleType> sampleVector;
Expand Down Expand Up @@ -99,8 +105,6 @@ ImageFullSampler<TInputImage>::GenerateData()
} // end if no mask
else
{
mask->UpdateSource();

/** Loop over the image and check if the points falls within the mask. */
for (InputImageIterator iter(&inputImage, croppedInputImageRegion); !iter.IsAtEnd(); ++iter)
{
Expand Down Expand Up @@ -197,8 +201,6 @@ ImageFullSampler<TInputImage>::ThreadedGenerateData(const InputImageRegionType &
} // end if no mask
else
{
mask->UpdateSource();

/** Loop over the image and check if the points falls within the mask. */
for (InputImageIterator iter(&inputImage, inputRegionForThread); !iter.IsAtEnd(); ++iter)
{
Expand Down

0 comments on commit f5b9024

Please sign in to comment.