Repository navigation
BUG: Call non-const GetBufferPointer() in non-const image iterators - #6955
hjmjohnson merged 1 commit into
Conversation
|
cc @mymonin |
[Medium impact] The change reaches writable iterators across several ITK image workflows. Complete the public reverse-iterator conversion fix before merging so GPU callers do not miss pixel edits. Findings
|
| // Base class calls the const GetBufferPointer(); call the non-const one for its side effects (e.g. CudaImage). | ||
| this->m_Buffer = ptr->GetBufferPointer(); |
There was a problem hiding this comment.
GPU misses reverse-iterator edits
The public ImageRegionReverseIterator(const ImageConstIterator<TImage> &) constructor still skips the non-const GetBufferPointer() call. When a caller converts a read-only iterator over a CudaImage with a clean GPU buffer, then writes through Set(), this path never marks the GPU buffer dirty. Later GPU work can therefore miss those writes.
Apply the same notification to that constructor and add a conversion case to the counting-image test.
aa46331 to
05979da
Compare
The const base class constructors only call the const overload, so image types relying on side effects of the non-const overload (e.g. CudaImage marking its GPU buffer dirty) were not notified of writes. Closes InsightSoftwareConsortium#6934.
05979da to
9a6c182
Compare
|
Looks good to me, assuming the proposed changes make the new tests pass. I was waiting for some acknowledgement of the issue I submitted but your PR makes everything clearer. |
9a6c182 to
5db0ef9
Compare
| {} | ||
| { | ||
| // Source iterator holds a const image; call the non-const GetBufferPointer() for its side effects (e.g. CudaImage). | ||
| (void)static_cast<void *>(const_cast<TImage *>(this->m_Image.GetPointer())->GetBufferPointer()); |
There was a problem hiding this comment.
This looks a bit suspicious, are you sure it's OK? I mean, casting away the const, and then (effectively) setting the dirty flag of the const image looks risky!
There was a problem hiding this comment.
Well, this is what is currently done: all images, whether const or not, are set dirty:
https://github.com/RTKConsortium/ITKCudaCommon/blob/main/include/itkCudaImage.hxx#L185-L191
This is done to circumvent the issue this PR aims at addressing. Since I'm trying to fix this issue :-), you could question why I did not try to fix this too in this PR? I was convinced by Claude when I suggested to make this constructor protected:
"Why it's public: it dates from the original reverse-iterator commit (38982a2, 2001). Its job is to turn any forward iterator into a reverse one. The parameter has to be ImageConstIterator because that's the only common base. ImageRegionIterator, the usual writable iterator, doesn't derive from ImageIterator; it derives from ImageRegionConstIterator. The repo uses it exactly this way: itkImageReverseIteratorTest.cxx:179 builds an ImageRegionReverseIterator from an ImageRegionIterator.
Why it's questionable: the same parameter type also accepts a real const iterator over a const image. You then get a writable iterator with no const_cast in sight. Every other const-to-writable conversion in ITK is protected "in order to enforce const correctness", including this class's other one: ImageRegionReverseIterator(const ImageRegionReverseConstIterator &).
Why not just make it protected: that breaks the existing test, and any outside code that converts a forward iterator to a reverse one. Doing it properly would need a deprecation and a replacement, such as overloads that take the writable forward iterator types. That's an API change, out of scope for this bug fix.
My recommendation: leave it public"
N-Dekker
left a comment
There was a problem hiding this comment.
Thanks Simon!
I don't really like the idea that GetBufferPointer() could be overridden to set the "dirty" flag of a derived image, and I don't really like const and non-const overloads to behave so differently, but I see that this PR should do the job, and make CudaImage users happy 🤷
Excellent unit tests 💯 by the way!
| {} | ||
| { | ||
| // Base class calls the const GetBufferPointer(); call the non-const one for its side effects (e.g. CudaImage). | ||
| (void)static_cast<void *>(ptr->GetBufferPointer()); |
There was a problem hiding this comment.
Oops:
itkImageRegionIteratorWithIndex.hxx:29:9: error: invalid 'static_cast' from type 'const itk::CovariantVector<
double, 2>
Does that mean that non-const ImageRegionIteratorWithIndex is actually used to iterate over const images? If so, it might be fixed by adding a non-const-check:
if ( ! std::is_const<TImage> )
{
(void)static_cast<void *>(ptr->GetBufferPointer());
}There was a problem hiding this comment.
But I would suggest users to use ImageRegionConstIteratorWithIndex instead, for const images 🤷
There was a problem hiding this comment.
Is there ever a valid reason to create a mutable iterator over a const image ? I would assume a const image only exposes a const pointer to its buffer, which would deliberately not be suitable to construct a writable iterator... but if so, I think the check should be if constexpr (! std::is_const_v<std::remove_reference_t<TImage>>)
There was a problem hiding this comment.
I'm not sure I'm following. Can you explain why std::remove_reference_t is required here please?
There was a problem hiding this comment.
My mistake, I saw the error containing a reference so I reflexively assumed it should be removed but since we pass a TImage* , there is no risk for TImage being deduced as a reference type.
There was a problem hiding this comment.
I am doubtful on the solution here. I have the impression my attempt to fix #6934 has uncovered another issue. I see two options:
- Rollback to
static_cast<void>(ptr->GetBufferPointer());
or add the suggested condition (this is equivalent in my opinion) and ignore the underlying issue.
- Leave it as is, fix
itkKappaStatisticImageToImageMetricTest.cxxby using anitk::ImageRegionConstIteratorWithIndex, but this will likely break some other codes which use non const iterators even if they just use const functionalities.
The latter point might be annoying so we might want to put this between some backward compatibilty guards.
What do you think @N-Dekker?
There was a problem hiding this comment.
Well... I did not realize that static_cast<void *>(ptr->GetBufferPointer()) would cause any compilation errors, although it is useful that it detected such unintended iterator uses. But then I don't really like to have those if-not-const-Image statements duplicated all over the place. A proper solution might be to add a helper function, like CallGetBufferPointerWhenImageIsNonConst(image). But then, I don't know where to place that helper function in the source tree.
So for pragmatism reasons, I'm now considering static_cast<void>(ptr->GetBufferPointer()) acceptable. It's a compromise anyway 🤷
Thanks!
There was a problem hiding this comment.
Thanks for your input, I think we agree :-). I reverted to 9a6c182 which did that.
5db0ef9 to
9a6c182
Compare
|
/azp run ITK.Linux.Python,ITK.macOS.Python |
|
/azp run ITK.Linux.Python |
|
/azp run ITK.macOS.Python |
814e4b3
into
InsightSoftwareConsortium:main
The const base class constructors only call the const overload, so image types relying on side effects of the non-const overload (e.g. CudaImage marking its GPU buffer dirty) were not notified of writes.
Closes #6934. Based on the discussions in RTKConsortium/ITKCudaCommon#107. I'm (of course) open to alternative suggestions for the implementation!
PR Checklist
Refer to the ITK Software Guide for
further development details if necessary.