CUDA¶
If you have an Nvidia GPU, you can accelerate some CPU-intensive tasks with CUDA.
Note
Using CUDA requires an Nvidia GPU with a Compute Capability of at least 5.0. To check whether your GPU satisfies this requirement, you can use this overview on the Nvidia website.
You can globally enable CUDA support with the /CUDA/Enabled node. The following commands can be accelerated with CUDA:
Note
Executing the ComputeDisparityMap command automatically rectifies the camera images. If you need a disparity map, you should therefore never explicitly execute the RectifyImages command, but directly call ComputeDisparityMap. This will yield a better performance, because it allows the CUDA implementation to interleave the rectification with the stereo matching and avoid unnecessary copy operations to the GPU.
Note
GPU support in the PartFinder command is in alpha stage. Currently, only the LineMod hypothesis search can be performed on the GPU. However, the implementation of this search is different from that of the CPU, so the results of the PartFinder can be significantly different between CPU and GPU.
To switch the search from CPU to GPU, CUDA should be explicitly enabled when executing the command, see the CUDA/Enabled parameter.
Multiple GPUs¶
You can see a list of all available GPUs in the Devices node. By default all commands will use the first GPU in that list. The Nvidia driver will also order the devices such that the most powerful one becomes the first entry. In most cases you therefore won’t need to change anything.
If you want to use a different GPU for computations you can globally select it with the Device node. Additionally, you can override the device to be used in the parameters of all commands that support CUDA (see the parameter descriptions of the commands for more information). This way, you can also distribute the computations for different cameras to multiple CUDA devices.
Direct Access to Disparity Maps on the GPU¶
If you want to further process the 3D data in CUDA, you can copy disparity maps from the NxLib’s internal CUDA buffer to your own CUDA buffer using the CopyGpuBuffer command. This saves the time to reupload the disparity map to the GPU.
This is only possible when the disparity map was computed using CUDA and is accessed via the binary node in the command result (not the one in the camera node). The buffer is kept alive and reserves a static buffer as long as the command node exists.
std::string cameraSerial = "123456";
// Compute the disparity map using CUDA. Keep this object alive until you copied the data.
NxLibCommand computeDisparityMap(cmdComputeDisparityMap);
computeDisparityMap.parameters()[itmCameras] = cameraSerial;
computeDisparityMap.execute();
NxLibItem disparityMap = computeDisparityMap.result()[cameraSerial][itmDisparityMap];
// Query the format of the disparity map from the NxLib.
int width, height, channels, bytesPerElement;
disparityMap.getBinaryDataInfo(&width, &height, &channels, &bytesPerElement, nullptr, nullptr);
int widthInBytes = width * channels * bytesPerElement;
// Allocate a CUDA buffer with the same format. Note that the buffer should be freed later.
void* buffer = nullptr;
size_t pitch = 0;
cudaMallocPitch(&buffer, &pitch, widthInBytes, height);
// Execute CopyGpuBuffer to copy the disparity map data to the new CUDA buffer.
NxLibCommand copyGpuBuffer(cmdCopyGpuBuffer);
copyGpuBuffer.parameters()[itmNode] = disparityMap.path;
copyGpuBuffer.parameters()[itmData] = std::format("{:p}", buffer);
copyGpuBuffer.parameters()[itmSize][0] = widthInBytes;
copyGpuBuffer.parameters()[itmSize][1] = height;
copyGpuBuffer.parameters()[itmPitch] = static_cast<int>(pitch);
copyGpuBuffer.execute();
Use the reprojection matrix to reproject the disparity data to 3D in your CUDA kernels.
CopyGpuBuffer can also copy to a Vulkan buffer, see the Data and (Type) parameters.
Hints and Limitations¶
By default, the NxLib keeps memory allocated on the GPU in between commands that use CUDA. This increases performance. You can limit or disable this behavior with the StaticBuffers setting.
The CUDA implementation of Semi-Global-Matching needs the number of disparities to be a multiple of 32. For compatibility with the CPU implementation, you can still choose it in multiples of 16, but the computation will automatically use the next larger multiple of 32.