InsightSoftwareConsortium / ITK

Insight Toolkit (ITK) -- Official Repository. ITK builds on a proven, spatially-oriented architecture for processing, segmentation, and registration of scientific images in two, three, or more dimensions.
https://itk.org
Apache License 2.0
1.41k stars 663 forks source link

Race condition in ObjectMorphology filters #4588

Open makarovNick opened 5 months ago

makarovNick commented 5 months ago

GlobalDefaultNumberOfThreads affects result Applying ErodeObjectMorphologyImageFilter on binary image produces different result with GlobalDefaultNumberOfThreads > 1

To Reproduce Steps to reproduce the behavior:

  1. Operating system, version, and architecture

    • OS: [Ubuntu 18.4] Architecture: x86_64 CPU(s): 16
  2. Programming language: Python 3.9.14]

  3. Version of SimpleITK: tested on 2.1.0, 2.2.0, 2.3.1

  4. How was SimpleITK installed?

    • binary distribution [e.g. python -m pip install SimpleITK]
  5. A minimal working example which causes the error. data - shape.zip

    
    import SimpleITK as sitk
    img = sitk.ReadImage("shape.nrrd")

sitk.ProcessObject.SetGlobalDefaultNumberOfThreads(1) eroded_img = sitk.ErodeObjectMorphology(img) result_volume = sitk.GetArrayFromImage(eroded_img).sum() print("Expecting volume: ", result_volume)

sitk.ProcessObject.SetGlobalDefaultNumberOfThreads(16) for _ in range(10): eroded_img = sitk.ErodeObjectMorphology(img) volume = sitk.GetArrayFromImage(eroded_img).sum() if result_volume != volume: print("Wrong result! Got", volume)

Got:

Expecting volume: 6039 Wrong result! Got 6213 Wrong result! Got 6109 Wrong result! Got 6046 Wrong result! Got 6119 Wrong result! Got 6212 Wrong result! Got 6042 Wrong result! Got 6183 Wrong result! Got 6212 Wrong result! Got 6044



**Expected behavior**
Expecting eroded image to be the same

Came here from SimpleITK [issue](https://github.com/SimpleITK/SimpleITK/issues/2095) as @blowekamp advised 
github-actions[bot] commented 5 months ago

Thank you for contributing an issue! πŸ™

Welcome to the ITK community! πŸ€—πŸ‘‹β˜€οΈ

We are glad you are here and appreciate your contribution. Please keep in mind our community participation guidelines. πŸ“œ Also, please check existing open issues and consider discussion on the ITK Discourse. πŸ“–

This is an automatic message. Allow for time for the ITK community to be able to read the issue and comment on it.

blowekamp commented 5 months ago

edited Not clear to me what the race conditions are causing the varying results after my initial investigation.