# Shadowed functions in GPUImage (or CudaImage)

**URL:** <https://discourse.itk.org/t/shadowed-functions-in-gpuimage-or-cudaimage/1614>\
**Category:** Uncategorized\
**Created:** [February 21, 2019, 4:20pm UTC](https://discourse.itk.org/t/shadowed-functions-in-gpuimage-or-cudaimage/1614 "2019-02-21T16:20:11Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![simon.rit](https://discourse.itk.org/letter_avatar_proxy/v4/letter/s/f08c70/32.png) [@simon.rit](https://discourse.itk.org/u/simon.rit)\
**Post date:** [February 21, 2019, 4:20pm UTC](https://discourse.itk.org/t/shadowed-functions-in-gpuimage-or-cudaimage/1614/1 "2019-02-21T16:20:11Z")

</div>

I am looking for explanations and advices, I hope you can help. I would like to be able to Graft an itk::Image to an itk::CudaImage (which is similar to itk::GPUImage). Recently, I have removed some code in itk::CudaImage (doing this) to be more consistent with itk::GPUImage, see [this commit](https://github.com/SimonRit/RTK/commit/f11369c03f8eac342415200a146496cee8a1fd1f) but, as you can see, the FirstCudaReconstruction.py example becomes more complex: it actually reproduces by hand the itk::Image::Graft function see [here](https://github.com/InsightSoftwareConsortium/ITK/blob/master/Modules/Core/Common/include/itkImage.hxx#L108) and [here](https://github.com/InsightSoftwareConsortium/ITK/blob/master/Modules/Core/Common/include/itkImageBase.hxx#L342). This function is inaccessible because [this line](https://github.com/InsightSoftwareConsortium/ITK/blob/master/Modules/Core/GPUCommon/include/itkGPUImage.h#L185) is in the protected section. So I first tried to simply move it to the public section but that does not work because SetPixelContainer is not virtual (and therefore [not overriden in itk::GPUImage](https://github.com/InsightSoftwareConsortium/ITK/blob/master/Modules/Core/Common/include/itkImage.h#L247)) so the wrong version is called by itk::Image::Graft.  
So my questions are:

- is this intentional that some functions are not virtual in itk::Image and shadowed by itk::GPUImage?
- what is the best solution here? To shadow Graft(itk::Image \*) in itk::GPUImage?

Thanks in advance!  
Simon

---

<div class="post-metadata">

**Author:** ![matt.mccormick](https://discourse.itk.org/user_avatar/discourse.itk.org/matt.mccormick/32/7_2.png) [@matt.mccormick](https://discourse.itk.org/u/matt.mccormick)\
**Post date:** [February 21, 2019, 10:05pm UTC](https://discourse.itk.org/t/shadowed-functions-in-gpuimage-or-cudaimage/1614/2 "2019-02-21T22:05:55Z")

</div>

Hi Simon,

Since PixelContainer is typed and typed to contain a pixel buffer stored in the CPU’s memory, we do not want / need `SetPixelContainer` to be `virtua`l. The `using` is misleading – there is a custom `Graft` implementation for `itk::GPUImage`:

> <https://github.com/InsightSoftwareConsortium/ITK/blob/73188f4e7b1bb558ed0f1871ac119c926950e49d/Modules/Core/GPUCommon/include/itkGPUImage.hxx#L173-L188>

and `itk::CudaImage` could be similar.

HTH,  
Matt

---

<div class="post-metadata">

**Author:** ![simon.rit](https://discourse.itk.org/letter_avatar_proxy/v4/letter/s/f08c70/32.png) [@simon.rit](https://discourse.itk.org/u/simon.rit)\
**Post date:** [February 21, 2019, 10:33pm UTC](https://discourse.itk.org/t/shadowed-functions-in-gpuimage-or-cudaimage/1614/3 "2019-02-21T22:33:47Z")

</div>

Thanks Matt. Yes, there is already a similar custom Graft for a CudaImage which might need to be corrected because I realize the SetImagePointer is missing (the TimeStamp is not used by the DataManager so I removed it):

> <https://github.com/SimonRit/RTK/blob/master/utilities/ITKCudaCommon/include/itkCudaImage.hxx#L192-L202>

  
But that means there is no way to Graft an itk::Image to an itk::GPUImage, right? Shouldn’t this be allowed? This is particularly useful in Python, because most filters do not accept a GPUImage. How do you handle conversion from CPU to GPU images in Python, e.g., to feed the CPU output of an itk::ImageFileReader to the GPU input of a GPUImageToImageFilter?

---

<div class="post-metadata">

**Author:** ![matt.mccormick](https://discourse.itk.org/user_avatar/discourse.itk.org/matt.mccormick/32/7_2.png) [@matt.mccormick](https://discourse.itk.org/u/matt.mccormick)\
**Post date:** [February 22, 2019, 8:08pm UTC](https://discourse.itk.org/t/shadowed-functions-in-gpuimage-or-cudaimage/1614/4 "2019-02-22T20:08:35Z")

</div>

In Python, I think it should look like this (though we have not wrapped itk.GPUImage):

```auto
PixelType = itk.ctype('float')
image = itk.imread('myfile.nrrd', PixelType)
GPUImageType = itk.GPUImage[PixelType, image.GetImageDimension()]
gpu_filter = itk.GPUImageToImageFilter[type(image), GPUImageType].New()
gpu_filter.SetInput(image)
gpu_filter.Update()
gpu_image = gpu_filter.GetOutput()
# Transfer from GPU memory to CPU memory if needed
gpu_image.UpdateBuffers()
# Since itk::GPUImage inherits from itk::Image, use it in parent filters

```

or, in ITK 5 syntax,

```auto
image = itk.imread('myfile.nrrd')
gpu_image = itk.gpu_image_to_image_filter(image)

```

So, the `itk::CudaImage` could be wrapped in a similar way…

---

<div class="post-metadata">

**Author:** ![simon.rit](https://discourse.itk.org/letter_avatar_proxy/v4/letter/s/f08c70/32.png) [@simon.rit](https://discourse.itk.org/u/simon.rit)\
**Post date:** [February 25, 2019, 1:18pm UTC](https://discourse.itk.org/t/shadowed-functions-in-gpuimage-or-cudaimage/1614/5 "2019-02-25T13:18:02Z")

</div>

Thanks again for suggesting a solution. I don’t see how this could work, GPUImageToImageFilter does not thing but call GPUGenerateData after allocating the outputs

> <https://github.com/InsightSoftwareConsortium/ITK/blob/master/Modules/Core/GPUCommon/include/itkGPUImageToImageFilter.hxx#L46-L61>

  
which does nothing  

> <https://github.com/InsightSoftwareConsortium/ITK/blob/master/Modules/Core/GPUCommon/include/itkGPUImageToImageFilter.h#L89-L90>

  
Similarly to the ImageToImageFilter, GPUImageToImageFilter is not meant to be used but to be inherited from. It’s not a converter from a CPU image to a GPU image. Or am I missing something?

---

<div class="post-metadata">

**Author:** ![matt.mccormick](https://discourse.itk.org/user_avatar/discourse.itk.org/matt.mccormick/32/7_2.png) [@matt.mccormick](https://discourse.itk.org/u/matt.mccormick)\
**Post date:** [February 25, 2019, 3:16pm UTC](https://discourse.itk.org/t/shadowed-functions-in-gpuimage-or-cudaimage/1614/6 "2019-02-25T15:16:32Z")

</div>

If the second template argument is an `itk::GPUImage`, it will produce a GPU image.

> <https://github.com/InsightSoftwareConsortium/ITK/blob/7470eab4c31c7d831d02112c6da00e88e3aadce4/Modules/Core/Common/include/itkImageSource.hxx#L68>

But `GPUGenerateData` / `GenerateData` would need to be defined to populate the pixel buffer.
