<div dir="ltr">Hey Maik,<div><br></div><div>here is some example code for a forwardprojection on GPU from a numpy array volume.</div><div><br></div><div><a href="https://gist.github.com/clemisch/9ec752430b2eb65d9840d69fc8c058bb">https://gist.github.com/clemisch/9ec752430b2eb65d9840d69fc8c058bb</a><br></div><div><br></div><div>You may write a projector class around that to save the proj_spacing, img_spacing, ... parameters and projector objects. Maybe even re-use the GPU images. I constructed this snipped from my framework but did not test it by itself; there might be bugs. Let me know if that helps you.</div><div><br></div><div><br></div><div>Best</div><div>Clemens</div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">Am Di., 16. Juli 2019 um 08:18 Uhr schrieb imt <<a href="mailto:stille@imt.uni-luebeck.de">stille@imt.uni-luebeck.de</a>>:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">



<div>
<div name="messageBodySection">
<div dir="auto">Hej Clemens,
<div dir="auto"><br></div>
<div dir="auto">I was following your conversation with Simon. I am quite interested in the problem. Do you have some simple example source code of your solution? That would be really helpful.</div>
</div>
</div>
<div name="messageSignatureSection"><br>
<div dir="auto">Thank you in advance
<div dir="auto">
<div dir="auto">Maik</div>
</div>
</div>
</div>
<div name="messageReplySection">On 9 Jul 2019, 18:24 +0200, C S <<a href="mailto:clem.schmid@gmail.com" target="_blank">clem.schmid@gmail.com</a>>, wrote:<br>
<blockquote type="cite" class="gmail-m_-6554688072325906707spark_quote" style="margin:5px;padding-left:10px;border-left:thin solid rgb(26,188,156)">
<div dir="ltr">Hi Simon,
<div><br></div>
<div>I think I understood the issue.</div>
<div><br></div>
<div><b>Problem:</b> The CPUBufferPointer of the CudaImage gets altered after calling .Update() on the backprojection/forwardprojection filter. Because of that, the existing numpy array (I created the CPU and GPU images from) does not get changed from the filter.</div>
<div><b>Solution:</b> Obtain the CudaDataManager of the CudaImage <i>before</i> applying the back-/forwardprojection filter, apply the filter and .Update(), set cpu pointer and update buffer via </div>
<div><br></div>
<div><font face="courier new, monospace">manager.SetCPUBufferPointer(cpu_img.GetBufferPointer())<br>
manager.UpdateCPUBuffer()</font><br></div>
<div><font face="courier new, monospace"><br></font></div>
<div><font face="arial, sans-serif">The data from the cuda_img is correctly written in-place into the numpy array I constructed the cpu_img from</font><font face="arial, sans-serif">.</font></div>
<div><font face="arial, sans-serif"><br></font></div>
<div><font face="arial, sans-serif">I don't know the underlying problem and cause why the CPUBuffer gets mangled, but this method achieves what I need. Thank you very much for your advice! </font></div>
<div><font face="arial, sans-serif"><br></font></div>
<div><font face="arial, sans-serif"><br></font></div>
<div><font face="arial, sans-serif">Best</font></div>
<div><font face="arial, sans-serif">Clemens</font></div>
</div>
<br>
<div class="gmail_quote">
<div dir="ltr" class="gmail_attr">Am Mo., 8. Juli 2019 um 18:07 Uhr schrieb C S <<a href="mailto:clem.schmid@gmail.com" target="_blank">clem.schmid@gmail.com</a>>:<br></div>
<blockquote class="gmail_quote gmail-m_-6554688072325906707spark_quote" style="margin:5px;padding-left:10px;border-left:thin solid rgb(230,126,34)">
<div dir="ltr">
<div>Hi Simon, </div>
<div><br></div>
<div class="gmail_quote">
<blockquote class="gmail_quote gmail-m_-6554688072325906707spark_quote" style="margin:5px;padding-left:10px;border-left:thin solid rgb(52,152,219)">
<div dir="ltr">
<div class="gmail_quote">
<div>I'm not sure I understand but have you tried grafting the Image or CudaImage to an existing itk::Image (the Graft function)?<br></div>
<div></div>
</div>
</div>
</blockquote>
<div>I tried that but when I call <font face="courier new, monospace">itk.GetArrayFromImage(cuda_img)</font> on the grafted image (<font face="courier new, monospace">cpu_img.Graft(cuda_img)</font>) I get the error  <font face="courier new, monospace">```ValueError: PyMemoryView_FromBuffer(): info->buf must not be NULL```</font> <font face="arial, sans-serif">from within ITK (or its Python bindings).</font></div>
<div> </div>
<blockquote class="gmail_quote gmail-m_-6554688072325906707spark_quote" style="margin:5px;padding-left:10px;border-left:thin solid rgb(52,152,219)">
<div dir="ltr">
<div class="gmail_quote">
<div></div>
<div>Again, I'm not sure I understand but you should be able to graft a CudaImage to another CudaImage.</div>
</div>
</div>
</blockquote>
<div> If anything I'd like to graft an Image into a CudaImage. When I try something like <span style="font-family:"courier new",monospace">`</span><font face="courier new, monospace">cuda_img.Graft(cpu_img)`</font> I get a TypeError. If this and the Graft'ing above would work (including the array view), that would be exactly my initial wish. </div>
<div> </div>
<blockquote class="gmail_quote gmail-m_-6554688072325906707spark_quote" style="margin:5px;padding-left:10px;border-left:thin solid rgb(52,152,219)">
<div dir="ltr">
<div class="gmail_quote">
<div>You can always ask explicit transfers by calling the functions of the data manager (accessible via <span class="gmail-m_-6554688072325906707gmail-m_-5438176733264102517gmail-m_7394961722479210295gmail-pl-en">CudaImage::GetCudaDataManager</span>())</div>
<div></div>
</div>
</div>
</blockquote>
<div>I assume you mean <font face="courier new, monospace">manager.UpdateCPUBuffer()</font>? When I run that, the CPU image I used to create the GPU image (by <a href="https://github.com/SimonRit/RTK/blob/master/examples/FirstReconstruction/FirstCudaReconstruction.py#L64-L70" target="_blank">this</a>) is not updated. </div>
<div> </div>
<div>My scenario is this: I give a numpy array as a volume to be forward projected. I get a ImageView from that array, set origin and spacing of that image and transfer to GPU via <a href="https://github.com/SimonRit/RTK/blob/master/examples/FirstReconstruction/FirstCudaReconstruction.py#L64-L70" target="_blank">your method</a>. For the output projections, I use an ImageView from a numpy.zeros array with according shape, spacing and origin and transfer that to GPU the same way. I then use the CudaForwardProjection filter. Now I'd like to have the projection data on CPU. Unfortunately, none of the suggested methods worked for me other than using an <font face="courier new, monospace">itk.ImageDuplicator</font> <font face="arial, sans-serif">on the CudaImage :(</font></div>
<div><br></div>
<div>Sorry for the lenghty mail. </div>
<div><br></div>
<div>Best</div>
<div>Clemens</div>
<div><br></div>
<blockquote class="gmail_quote gmail-m_-6554688072325906707spark_quote" style="margin:5px;padding-left:10px;border-left:thin solid rgb(52,152,219)">
<div dir="ltr">
<div class="gmail_quote">
<div> </div>
</div>
</div>
</blockquote>
<blockquote class="gmail_quote gmail-m_-6554688072325906707spark_quote" style="margin:5px;padding-left:10px;border-left:thin solid rgb(52,152,219)">
<div dir="ltr">
<div class="gmail_quote">
<blockquote class="gmail_quote gmail-m_-6554688072325906707spark_quote" style="margin:5px;padding-left:10px;border-left:thin solid rgb(211,84,0)">
<div dir="ltr">
<div><br></div>
<div><br></div>
<div>Best</div>
<div>Clemens</div>
</div>
<br>
<div class="gmail_quote">
<div dir="ltr" class="gmail_attr">Am Mo., 8. Juli 2019 um 16:20 Uhr schrieb Simon Rit <<a href="mailto:simon.rit@creatis.insa-lyon.fr" target="_blank">simon.rit@creatis.insa-lyon.fr</a>>:<br></div>
<blockquote class="gmail_quote gmail-m_-6554688072325906707spark_quote" style="margin:5px;padding-left:10px;border-left:thin solid rgb(52,73,94)">
<div dir="ltr">
<div>Hi,</div>
<div>Conversion from Image to CudaImage is not optimal. The way I'm doing it now is shown in an example in <a href="https://github.com/SimonRit/RTK/blob/master/examples/FirstReconstruction/FirstCudaReconstruction.py#L64-L70" target="_blank">these few lines</a>. I am aware of the problem and discussed it on the <a href="https://discourse.itk.org/t/shadowed-functions-in-gpuimage-or-cudaimage/1614" target="_blank">ITK forum</a> but I don't have a better solution yet.</div>
<div>I'm not sure what you mean by explicitely transferring data from/to GPU but I guess you can always work with itk::Image and do your own CUDA computations in the GenerateData of the ImageFilter if you don't like the CudaImage mechanism.</div>
<div>I hope this helps,</div>
<div>Simon<br></div>
</div>
<br>
<div class="gmail_quote">
<div dir="ltr" class="gmail_attr">On Mon, Jul 8, 2019 at 10:06 PM C S <<a href="mailto:clem.schmid@gmail.com" target="_blank">clem.schmid@gmail.com</a>> wrote:<br></div>
<blockquote class="gmail_quote gmail-m_-6554688072325906707spark_quote" style="margin:5px;padding-left:10px;border-left:thin solid rgb(46,204,113)">
<div dir="ltr">Dear RTK users,
<div><br></div>
<div>I'm looking for a way to use exisiting ITK Images (either on GPU or in RAM) when transfering data from/to GPU. That is, not only re-using the Image object, but writing into the memory where its buffer is. </div>
<div><br></div>
<div>Why: As I'm using the Python bindings, I guess this ties in with ITK wrapping the CudaImage type. In <a href="https://github.com/SimonRit/RTK/blob/master/utilities/ITKCudaCommon/include/itkCudaImage.h#L32" target="_blank">https://github.com/SimonRit/RTK/blob/master/utilities/ITKCudaCommon/include/itkCudaImage.h#L32</a> I read that the memory management is done implicitly and the CudaImage can be used with CPU filters. However when using the bindings, only rtk.BackProjectionImageFilter can be used with CudaImages. The other filters complain about not being wrapped for that type. </div>
<div><br></div>
<div>That is why I want to explicitely transfer the data from/to GPU, but preferably using the exisiting Images and buffers. I can't rely on RTK managing GPU memory implicitly.</div>
<div><br></div>
<div><br></div>
<div>Thank you very much for your help!</div>
<div>Clemens</div>
<div><br></div>
<div><br></div>
</div>
_______________________________________________<br>
Rtk-users mailing list<br>
<a href="mailto:Rtk-users@public.kitware.com" target="_blank">Rtk-users@public.kitware.com</a><br>
<a href="https://public.kitware.com/mailman/listinfo/rtk-users" rel="noreferrer" target="_blank">https://public.kitware.com/mailman/listinfo/rtk-users</a><br></blockquote>
</div>
</blockquote>
</div>
</blockquote>
</div>
</div>
</blockquote>
</div>
</div>
</blockquote>
</div>
_______________________________________________<br>
Rtk-users mailing list<br>
<a href="mailto:Rtk-users@public.kitware.com" target="_blank">Rtk-users@public.kitware.com</a><br>
<a href="https://public.kitware.com/mailman/listinfo/rtk-users" target="_blank">https://public.kitware.com/mailman/listinfo/rtk-users</a><br></blockquote>
</div>
</div>

</blockquote></div>