# Is it possible to use GPU for image resample?

**URL:** https://discourse.itk.org/t/is-it-possible-to-use-gpu-for-image-resample/2775
**Category:** Uncategorized
**Created:** [March 2, 2020, 9:13am UTC](https://discourse.itk.org/t/is-it-possible-to-use-gpu-for-image-resample/2775 "2020-03-02T09:13:33Z")
**Posts on this page:** 14
**Page:** 1

<div class="post-metadata">

### Author: ![qiang-zhang-neu](https://discourse.itk.org/letter_avatar_proxy/v4/letter/q/e47774/32.png) [@qiang-zhang-neu](https://discourse.itk.org/u/qiang-zhang-neu)
#### Post date: [March 2, 2020, 9:13am UTC](https://discourse.itk.org/t/is-it-possible-to-use-gpu-for-image-resample/2775/1 "2020-03-02T09:13:33Z")

</div>

The image resample filter in ITK is slow, can we use GPU to accelerate it?

---

<div class="post-metadata">

### Author: ![dzenanz](https://discourse.itk.org/user_avatar/discourse.itk.org/dzenanz/32/1093_2.png) [@dzenanz](https://discourse.itk.org/u/dzenanz)
#### Post date: [March 2, 2020, 3:39pm UTC](https://discourse.itk.org/t/is-it-possible-to-use-gpu-for-image-resample/2775/2 "2020-03-02T15:39:10Z")

</div>

That is possible, but not implemented. Implementing it would be a lot of work.

---

<div class="post-metadata">

### Author: ![lassoan](https://discourse.itk.org/user_avatar/discourse.itk.org/lassoan/32/27_2.png) [@lassoan](https://discourse.itk.org/u/lassoan)
#### Post date: [March 2, 2020, 5:45pm UTC](https://discourse.itk.org/t/is-it-possible-to-use-gpu-for-image-resample/2775/3 "2020-03-02T17:45:52Z")

</div>

> [@dzenanz](#):
>
> Implementing it would be a lot of work

Why do you think it would be a lot of work? Would any design change be needed? Couldn’t it be another OpenCL-based imaging filter? Or are you concerned about the time to transfer the image between CPU/GPU would cancel out the performance gain?

---

<div class="post-metadata">

### Author: ![dzenanz](https://discourse.itk.org/user_avatar/discourse.itk.org/dzenanz/32/1093_2.png) [@dzenanz](https://discourse.itk.org/u/dzenanz)
#### Post date: [March 2, 2020, 6:47pm UTC](https://discourse.itk.org/t/is-it-possible-to-use-gpu-for-image-resample/2775/4 "2020-03-02T18:47:28Z")

</div>

Implementing all the interpolators to work efficiently on the GPU will be a lot of work. Even if we were to implement just linear and nearest neighbor, it is still not trivial.

---

<div class="post-metadata">

### Author: ![lassoan](https://discourse.itk.org/user_avatar/discourse.itk.org/lassoan/32/27_2.png) [@lassoan](https://discourse.itk.org/u/lassoan)
#### Post date: [March 2, 2020, 7:14pm UTC](https://discourse.itk.org/t/is-it-possible-to-use-gpu-for-image-resample/2775/5 "2020-03-02T19:14:15Z")

</div>

Is it complicated because the generic framework of interpolators would be hard to port to OpenCL? A simple image-to-image filter doing linear or NN interpolation on 3D volumes (including single-slice volumes) would cover almost all use cases.

---

<div class="post-metadata">

### Author: ![dzenanz](https://discourse.itk.org/user_avatar/discourse.itk.org/dzenanz/32/1093_2.png) [@dzenanz](https://discourse.itk.org/u/dzenanz)
#### Post date: [March 2, 2020, 7:19pm UTC](https://discourse.itk.org/t/is-it-possible-to-use-gpu-for-image-resample/2775/6 "2020-03-02T19:19:27Z")

</div>

> [@lassoan](#):
>
> Is it complicated because the generic framework of interpolators would be hard to port to OpenCL?

Yes, I think so.

> [@lassoan](#):
>
> A simple image-to-image filter doing linear or NN interpolation on 3D volumes (including single-slice volumes) would cover almost all use cases.

I agree. Still, it would probably take days to implement it for an experienced person.

---

<div class="post-metadata">

### Author: ![lassoan](https://discourse.itk.org/user_avatar/discourse.itk.org/lassoan/32/27_2.png) [@lassoan](https://discourse.itk.org/u/lassoan)
#### Post date: [March 2, 2020, 7:27pm UTC](https://discourse.itk.org/t/is-it-possible-to-use-gpu-for-image-resample/2775/7 "2020-03-02T19:27:36Z")

</div>

OK, thanks for the clarifications, we agree then.

---

<div class="post-metadata">

### Author: ![Gordian](https://discourse.itk.org/user_avatar/discourse.itk.org/gordian/32/335_2.png) [@Gordian](https://discourse.itk.org/u/Gordian)
#### Post date: [March 2, 2020, 8:51pm UTC](https://discourse.itk.org/t/is-it-possible-to-use-gpu-for-image-resample/2775/8 "2020-03-02T20:51:56Z")

</div>

What is reason for not using CUDA?  
The texture memory would provide a quite simple way to create an interpolator for linear and NN interpolation (at least with affine transformations).  
Because it is owned by NVIDIA and therefore not open-source and limited to their graphic cards?

---

<div class="post-metadata">

### Author: ![dzenanz](https://discourse.itk.org/user_avatar/discourse.itk.org/dzenanz/32/1093_2.png) [@dzenanz](https://discourse.itk.org/u/dzenanz)
#### Post date: [March 2, 2020, 8:56pm UTC](https://discourse.itk.org/t/is-it-possible-to-use-gpu-for-image-resample/2775/9 "2020-03-02T20:56:18Z")

</div>

They have kind-of opened it a few years ago. I don’t think there are any other implementations of CUDA but nVidia’s. GPU processing was added to ITK with version 4.0, around 2010. OpenCL was the open standard for that back then. So no CUDA.

---

<div class="post-metadata">

### Author: ![Gordian](https://discourse.itk.org/user_avatar/discourse.itk.org/gordian/32/335_2.png) [@Gordian](https://discourse.itk.org/u/Gordian)
#### Post date: [March 2, 2020, 8:59pm UTC](https://discourse.itk.org/t/is-it-possible-to-use-gpu-for-image-resample/2775/10 "2020-03-02T20:59:28Z")

</div>

I was asking because the RTK has some basis for a ITK Cuda implementation.  
Furthermore elastix has an [OpenCLResampler](http://elastix.isi.uu.nl/doxygen/classelastix_1_1OpenCLResampler.html) that could be a starting point for someone eager to have a GPU resample image filter. 🙂

---

<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: [March 5, 2020, 2:55am UTC](https://discourse.itk.org/t/is-it-possible-to-use-gpu-for-image-resample/2775/11 "2020-03-05T02:55:45Z")

</div>

[Elastix OpenCL resampling](https://www.insight-journal.org/browse/publication/884) code is being updated, integrated and wrapped for Python in the [ITKOpenCL](https://github.com/InsightSoftwareConsortium/ITKOpenCL) remote module (work in progress).

---

<div class="post-metadata">

### Author: ![agravgaard](https://discourse.itk.org/letter_avatar_proxy/v4/letter/a/db5fbb/32.png) [@agravgaard](https://discourse.itk.org/u/agravgaard)
#### Post date: [March 9, 2020, 10:01am UTC](https://discourse.itk.org/t/is-it-possible-to-use-gpu-for-image-resample/2775/12 "2020-03-09T10:01:12Z")

</div>

LLVM/Clang has a CUDA implementation ( [https://llvm.org/docs/CompileCudaWithLLVM.html](https://llvm.org/docs/CompileCudaWithLLVM.html) ), though I’ve not tried it myself for anything other than small code samples, yet, due to the lack of CMake support: [https://gitlab.kitware.com/cmake/cmake/issues/16586](https://gitlab.kitware.com/cmake/cmake/issues/16586) (which has finally, after 3 years, seen some work as of yesterday!)  
An alternative approach, which would be compatible with most hardware (not just nVidia GPUs), could be to use SYCL, Eigen already supports it as backend, so I guess that would be a more obvious choice.

However, as the image types\* in OpenCL supports nearest and linear interpolation ( [https://www.khronos.org/registry/OpenCL/specs/opencl-1.2.pdf#page=329](https://www.khronos.org/registry/OpenCL/specs/opencl-1.2.pdf#page=329) ), this would be the simplest, and probably most performant, solution. For any more advanced interpolation, like bspline, the way elastix does it (as @Gordian mentioned) looks like the right approach.

\*[Although the image types is not supported on all hardware, it’s supported on practically all GPUs (I’ve yet to encounter one which doesn’t), and, I think, most x86 CPU’s that support OpenCL 1.2 or newer. It’s not supported on Xeon Phi co-processors (but most OpenCL code doesn’t perform well on those anyway), and probably not supported on more exotic hardware either, like DSPs and FPGAs, but I haven’t programmed on these platforms.]

---

<div class="post-metadata">

### Author: ![Nirvedh\_Meshram](https://discourse.itk.org/user_avatar/discourse.itk.org/nirvedh_meshram/32/1551_2.png) [@Nirvedh\_Meshram](https://discourse.itk.org/u/Nirvedh_Meshram)
#### Post date: [June 14, 2020, 12:32am UTC](https://discourse.itk.org/t/is-it-possible-to-use-gpu-for-image-resample/2775/13 "2020-06-14T00:32:14Z")

</div>

> [@matt.mccormick](#):
>
> for

Hi Matt,  
Could this be provided as a simpleITK filter. Or it’s currently available only in c++?

---

<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: [June 15, 2020, 2:37pm UTC](https://discourse.itk.org/t/is-it-possible-to-use-gpu-for-image-resample/2775/14 "2020-06-15T14:37:15Z")

</div>

> [@Nirvedh\_Meshram](#):
>
> Hi Matt,  
> Could this be provided as a simpleITK filter. Or it’s currently available only in c++?

This is currently available in ITK Python binary packages on Linux, installed with

```auto
pip install itk-elastix-opencl

```
