Skip to content

COMP: Use std::atomic for the OpenCLVectorBase reference count - #1504

Merged
N-Dekker merged 1 commit into
SuperElastix:mainfrom
hjmjohnson:use-std-atomic-opencl-vector-refcount
Dec 3, 2026
Merged

N-Dekker merged 1 commit into
SuperElastix:mainfrom
hjmjohnson:use-std-atomic-opencl-vector-refcount

Conversation

@hjmjohnson

Copy link
Copy Markdown
Contributor

ELASTIX_USE_OPENCL=ON no longer compiles against ITK main. itkOpenCLVectorBase.cxx includes vcl_atomic_count.h, which ITK dropped when it switched to a VXL stripped down to the numerics code (InsightSoftwareConsortium/ITK#6421, merged June 2026). This PR replaces the counter with std::atomic<int>.
Merge danger: two-way door; blast radius: ELASTIX_USE_OPENCL=ON builds only (off by default).

This is independent of the ITK GPU-module removal discussed in #1503. The header is used by elastix's own OpenCL code, not by ITK's GPU modules.

Shape of the change
-// thread/SMP safe reference counter
-#include <vcl_atomic_count.h>
+#include <atomic>
 ...
-  vcl_atomic_count              referenceCount;
+  std::atomic<int>              referenceCount;

referenceCount is initialized to 1, incremented, decremented, and compared with <= 0. std::atomic<int> supports each of these with the same atomicity.

Evidence

Local, macOS arm64, conda clang, ELASTIX_USE_OPENCL=ON, against an ITK build without vcl_atomic_count.h (ITK main after #6421):

  • Without this change: itkOpenCLVectorBase.cxx:22: fatal error: 'vcl_atomic_count.h' file not found.
  • With this change, together with COMP: Stop requiring ITKGPUCommon for ELASTIX_USE_OPENCL (ITK 6) #1503 (needed because that ITK build has no ITKGPUCommon): 401 of 401 targets build and link with 0 errors.
  • I could not run the OpenCL runtime tests locally. They need an Intel, NVIDIA, or AMD OpenCL SDK, and OpenCLContext rejects Apple's platform with or without this change.
  • ITK 5.4 still ships vcl_atomic_count.h, but std::atomic works there as well, so builds against ITK 5.4.1 or later are unaffected.
  • No elastix or ITK issue or PR mentions vcl_atomic_count.
AI assistance

Prepared with Claude Code (Opus 5.5) under Hans Johnson's direction; reviewed by Hans Johnson.

ITK 6 no longer vendors vcl_atomic_count.h (VXL update of 2026-06-09),
so ELASTIX_USE_OPENCL fails to compile against ITK main. std::atomic<int>
provides the same initialization, increment, decrement, and comparison.
@N-Dekker

N-Dekker commented Dec 2, 2026

Copy link
Copy Markdown
Member

Looks fine to me, thanks Hans. I think I'll merge this tomorrow, but maybe my colleague Denis (@dpshamonin), who was responsible to elastix OpenCL support in the first place, still likes to have a final look... 😸

@N-Dekker

N-Dekker commented Dec 3, 2026

Copy link
Copy Markdown
Member

For the record, the old vcl_atomic_count use case was introduced in the elastix source file "itkOpenCLVectorBase.cxx" with commit 2ce0a0a by Denis Shamonin (@dpshamonin), Jan 22, 2015. At that time, elastix still did not require C++11 (it assumed C++03), so std::atomic was not yet available.

@N-Dekker
N-Dekker merged commit 2ece8c8 into SuperElastix:main Dec 3, 2026
5 checks passed
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.

2 participants