# The time of converting cpu image to gpu image is too long

**URL:** <https://discourse.itk.org/t/the-time-of-converting-cpu-image-to-gpu-image-is-too-long/2836>\
**Category:** Uncategorized\
**Created:** [March 17, 2020, 3:13am UTC](https://discourse.itk.org/t/the-time-of-converting-cpu-image-to-gpu-image-is-too-long/2836 "2020-03-17T03:13:32Z")\
**Posts on this page:** 8\
**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 17, 2020, 3:13am UTC](https://discourse.itk.org/t/the-time-of-converting-cpu-image-to-gpu-image-is-too-long/2836/1 "2020-03-17T03:13:32Z")

</div>

In my project, I need to convert the itk::Image to itk::GPUImage. However, I find the time is too long (~ 3s) in release version.

The code is:

```auto
		start1 = clock();
		CPUTOGPUImageFilterPointer cpuToGpuImageFilter = CPUTOGPUImageFilterType::New();
		cpuToGpuImageFilter->SetInput(m_cpuMovingImage);
		cpuToGpuImageFilter->UpdateLargestPossibleRegion();
		m_gpuMovingImage = cpuToGpuImageFilter->GetOutput();
		end1 = clock();
		std::cout << "cpu to gpu image time:" << (double)(end1 - start1) / CLOCKS_PER_SEC << std::endl;

```

And the information in console is:

```auto
cpu to gpu image time:3.441

```

Why it would cost so much time? Or is there any thing wrong with my code?

---

<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 17, 2020, 3:02pm UTC](https://discourse.itk.org/t/the-time-of-converting-cpu-image-to-gpu-image-is-too-long/2836/2 "2020-03-17T15:02:38Z")

</div>

CPU to GPU time is costly, but that is more than expected.

It is worth investigating your hardware and the CPU-GPU bus speed.

---

<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:** [March 17, 2020, 8:41pm UTC](https://discourse.itk.org/t/the-time-of-converting-cpu-image-to-gpu-image-is-too-long/2836/3 "2020-03-17T20:41:18Z")

</div>

I’m interested in your `CPUTOGPUImageFilterType`. Can you give me a pointer to the corresponding filter? I can’t find in ITK. For Cuda, we do the conversion manually

> <https://github.com/SimonRit/RTK/blob/master/examples/FirstReconstruction/FirstCudaReconstruction.py#L67-L70>

  
which costs almost nothing but the image is not on the GPU yet. I think it’s a better idea to put these operations in a filter and I’d like to know how it’s done in the one you used.

---

<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 18, 2020, 10:01am UTC](https://discourse.itk.org/t/the-time-of-converting-cpu-image-to-gpu-image-is-too-long/2836/4 "2020-03-18T10:01:58Z")

</div>

Actually, it is a cast filter:

```auto
	using CPUTOGPUImageFilterType = itk::CastImageFilter<CPUImageType, GPUImageType>;
	using CPUTOGPUImageFilterPointer = typename CPUTOGPUImageFilterType::Pointer;

```

---

<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 18, 2020, 10:04am UTC](https://discourse.itk.org/t/the-time-of-converting-cpu-image-to-gpu-image-is-too-long/2836/5 "2020-03-18T10:04:44Z")

</div>

However, I convert a array vector to GPU image, and it only cost about 0.05s. If I convert the CPU image to array vector, and then convert the array vector to GPU image, I think the cost time is smaller than 0.1s (I don’t test it).

I just think it is very weird. Why convert CPU image to GPU image would cost so much time? What operator does it perform?

---

<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:** [March 18, 2020, 10:29am UTC](https://discourse.itk.org/t/the-time-of-converting-cpu-image-to-gpu-image-is-too-long/2836/6 "2020-03-18T10:29:01Z")

</div>

What are the CPUImageType and the GPUImageType? I don’t think there is anything specific to gpu in the cast image filter so I think it’s not related to the GPUImage type. The solution I suggested above is much faster since there is no data copy whereas the cast does a data copy.

---

<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 18, 2020, 12:31pm UTC](https://discourse.itk.org/t/the-time-of-converting-cpu-image-to-gpu-image-is-too-long/2836/7 "2020-03-18T12:31:35Z")

</div>

> [@qiang-zhang-neu](#):
>
> What operator does it perform?

You can step through the code to find out.

---

<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 20, 2020, 1:18am UTC](https://discourse.itk.org/t/the-time-of-converting-cpu-image-to-gpu-image-is-too-long/2836/8 "2020-03-20T01:18:26Z")

</div>

```auto
using CPUImageType = itk::Image<PixelType, Dimension>;
using GPUImageType = itk::GPUImage<PixelType, Dimension>;

```

Thanks, I have tested the SetPIxelContainer method, and it really make the program faster than caster filter.
