Repository navigation
STIR and optimising GPU support (i.e. parallelproj and NiftyPET) #1239
Replies: 6 comments 7 replies
|
@casperdcl, just replacing the |
|
Note that being able to expose the GPU arrays directly to Python would presumably speed-up |
|
@gschramm working on v2.0.0 release of https://github.com/KUL-recon-lab/libparallelproj:
|
|
A lot of progress was made during and after the SyneRBI AI-RBI hackathon. See SyneRBI/SIRF-Contribs#31 (comment) |
|
There seem to be a few different approaches to get STIR images use cuda managed pointers. One of which is to add an "allocator" argument to However, STIR/src/include/stir/DiscretisedDensity.inl Lines 40 to 53 in 4368758 where as opposed to base_type(range_v)we would use something like (not 100% sure! @casperdcl will correct me) base_type(range_v, std::allocate_shared<T>(cuvec::CuAlloc(), range_v.size_all())The disadvantage of The approach of changing only the |
|
Valerio and I discussed ways to hide complicated cuda fall-backs by introducing some additional functions. After reading a bit more, I came up with the untested example template < class T>
void
mycopy(T& to, const T& from)
{
const auto begin_to = to.begin();
const auto end_to = to.end();
const auto begin_from = from.begin();
if constexpr (std::is_trivially_copyable_v<decltype(*begin_to)>)
{
if (on_gpu)
{
cudaMemcpy(begin_to, begin_from, (end_to - begin_to) * sizeof(*begin_to));
}
else
{
std::memcpy(begin_to, begin_from, (end_to - begin_to) * sizeof(*begin_to));
}
}
else
{
std::copy(begin_to, end_to, begin_from);
}
}Some notes:
|
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
STIR currently supports CUDA-enabled projectors from NiftyPET (for mMR only due to hard-wiring) and Parallelproj, but performance is suboptimal. We're thinking on how to speed this up. As I'm more familiar with parallelproj, we can start there. That STIR code was based on @rijobro's work on NiftyPET anyway.
The current STIR strategy is to call the GPU code for all projection data (and then sort things out on subsets afterwards. This is sub-optimal as well, but let's concentrate on how to speed the projection up first.)
Example is in the forward projection. Current strategy:
std::vector(to get continuous memory)STIR/src/recon_buildblock/Parallelproj_projector/ForwardProjectorByBinParallelproj.cxx
Lines 148 to 149 in 4408419
std::vectordata to all GPUsSTIR/src/recon_buildblock/Parallelproj_projector/ForwardProjectorByBinParallelproj.cxx
Lines 169 to 170 in 4408419
ProjDataInMemory, i.e. CPU). Note thatparallelprojwill do the copy of projection data from GPU to hostSTIR/src/recon_buildblock/Parallelproj_projector/ForwardProjectorByBinParallelproj.cxx
Lines 181 to 189 in 4408419
ProjDataInMemorytoProjDataobject that is actually given by the user (which could be in memory, but could also sit on disk, asset_viewgrametc are overloaded accordingly)Some reasons for this are:
stir::Arrayis currently not contiguously stored. This is being addressed in update Array hierarchy and allocate nD arrays in a contiguous block by default #1236, which means that step 1 could be avoided.parallelprojto do this in chunks.Similar steps (in reverse) happen in the back-projection
Some points from a discussion held with @casperdcl @markus-jehl @evgueni-ovtchinnikov and others on 31Aug2023:
cudaMallocManagedin a few places might avoid the explicit transfer (of images only?) between CPU and GPU (CUDA will take care of it). One option could be to usecudaMallocManagedfor allArrays. This could be extended to numerical operations by either using libraries, or by using#ifdefs. Example of this is in https://github.com/AMYPAD/NumCu/blob/main/numcu/src/elemwise.cuSome anticipated difficulties:
cudaMallocManagedwill probably fail when asking for more memory than available on the GPU. I don't know if this is a problem when allocating multiple blocks (i.e. do they all need to fit in GPU together?)ProjDataInMemory, but this needs a refactor of theForwardProjectorByBinclass.Comments/suggestions/PRs welcome!
All reactions