ITKCudaCommon is an ITK module for processing images with Cuda, including the itk::CudaImage class and base classes for processing objects of this type, itk::CudaImageToImageFilter and itk::CudaInPlaceImageFilter. ITKCudaCommon was originally based on the GPUCommon module for OpenCL.
The installation is described in INSTALLATION.md.
CudaCommon's documentation is limited to a few Markdown files (README.md, cuda_array_interface.md, INSTALLATION.md, etc.) integrated into RTK's documentation.
An itk::CudaImage is an itk::Image (by inheritance) with a new member, m_DataManager of type itk::CudaDataManager. The role of this member is to manage a GPU memory buffer with the same size as the itk::Image::m_Buffer. Data is transferred from one buffer to the other (m_GPUBuffer and m_CPUBuffer) only when necessary, i.e., when accessing one of the two buffers and the other has been accessed more recently and therefore marked as "dirty" (using the members m_IsGPUBufferDirty and m_IsCPUBufferDirty).
The Python CudaImage wrapping exposes a __cuda_array_interface__ for zero-copy views to other packages such as PyTorch or CuPy. See the CUDA array interface documentation for more information.
When no device is explicitly selected (and no environment variable is set), the GPU used by all freshly created itk::CudaImage (or itk::CudaDataManager) objects is selected automatically as the device with the maximum FLOPS. In a multi-GPU system you may want to choose a specific device instead.
Call this from C++ before creating any CudaImage:
// Use GPU number 2 for all subsequent images.
itk::SetDefaultCudaDevice(2);
// Automatically select the (first) device with the max FLOPS.
itk::SetDefaultCudaDevice(-1);The equivalent is available from Python:
import itk
# Use GPU number 2 for all subsequently created CudaImages.
itk.set_default_cuda_device(2)
# Automatically select the (first) device with the max FLOPS.
itk.set_default_cuda_device(-1)An explicitly set value takes precedence over the ITK_CUDA_DEFAULT_DEVICE environment variable which itself takes precedence over the automated value.
Passing a device index outside the valid range throws an itk::ExceptionObject. The environment variable must be a non-negative integer, otherwise an exception is thrown.
An itk::CudaImageToImageFilter and its in place child, itk::CudaInPlaceImageFilter, are ITK filters which can be integrated in an ITK pipeline to process an itk::CudaImage. They are CUDA equivalent to the CPU classes itk::ImageToImageFilter and itk::InPlaceImageFilter in ITK's Common module. See the ITK software guide for more information. Their GPUGenerateData member function must process the output CudaImage objects.
CudaCommon includes CUDA implementations of common ITK unary and binary image operators.
These operators were added to perform pixel-wise calculations directly on the GPU within an ITK pipeline. They allow pipelines using itk::CudaImage to keep image data on the GPU between operations, avoiding transfers to and from CPU memory just to apply these calculations.