Skip to content

Fix empty patch match results on CUDA compute >= 100 (Blackwell GPUs) - #4161

Merged
ahojnnes merged 4 commits into
mainfrom
user/jsch/fix-non-trivially-copyable-cuda-arg
Mar 5, 2026
Merged

ahojnnes merged 4 commits into
mainfrom
user/jsch/fix-non-trivially-copyable-cuda-arg

Conversation

@ahojnnes

Copy link
Copy Markdown
Contributor
  • GpuMat<T> contains std::shared_ptr, making it non-trivially copyable. Passing it by value to CUDA kernels is undefined behavior per the CUDA spec. On Blackwell GPUs (compute >= 100), this causes the compiler to misoptimize the depth_ loop in SetSlice/GetSlice, producing all-zero normal maps and 0 fused points.
  • Introduce GpuMatView<T>, a trivially-copyable POD struct with only the fields needed on the device (ptr, pitch, width, height, depth) and all device-side accessors (Get, Set, GetSlice, SetSlice, GetRef).
  • All CUDA kernels now accept GpuMatView<T> instead of GpuMat<T>. Launch sites use ->View() instead of *ptr.

Fixes #3514

  - `GpuMat<T>` contains `std::shared_ptr`, making it non-trivially copyable.
    Passing it by value to CUDA kernels is undefined behavior per the
    [CUDA spec](https://docs.nvidia.com/cuda/cuda-programming-guide/05-appendices/cpp-language-support.html).
    On Blackwell GPUs (compute >= 100), this causes the compiler to misoptimize
    the `depth_` loop in `SetSlice`/`GetSlice`, producing all-zero normal maps
    and 0 fused points.
  - Introduce `GpuMatView<T>`, a trivially-copyable POD struct with only the
    fields needed on the device (`ptr`, `pitch`, `width`, `height`, `depth`)
    and all device-side accessors (`Get`, `Set`, `GetSlice`, `SetSlice`, `GetRef`).
  - All CUDA kernels now accept `GpuMatView<T>` instead of `GpuMat<T>`.
    Launch sites use `->View()` instead of `*ptr`.

  Fixes #3514
@gemini-code-assist

Copy link
Copy Markdown
Contributor

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request resolves a critical issue where GpuMat<T> objects, containing std::shared_ptr, were being passed by value to CUDA kernels. This practice constitutes undefined behavior according to the CUDA specification and led to incorrect results, specifically all-zero normal maps and zero fused points, on newer Blackwell GPUs. The solution involves introducing a lightweight, trivially-copyable GpuMatView<T> struct that provides a safe representation of GPU memory for device-side functions, thereby adhering to CUDA best practices and restoring correct functionality.

Highlights

  • CUDA Undefined Behavior Fix: Addressed undefined behavior in CUDA kernels on Blackwell GPUs (compute >= 100) caused by passing non-trivially copyable GpuMat<T> by value, which resulted in incorrect computation (all-zero normal maps and zero fused points).
  • Introduction of GpuMatView: Introduced GpuMatView<T>, a new trivially-copyable POD (Plain Old Data) struct, to safely represent GPU device memory for kernel arguments, adhering to CUDA specification requirements.
  • Kernel Argument Refactoring: Refactored all relevant CUDA kernels to accept GpuMatView<T> instead of GpuMat<T> for device-side operations, ensuring safe and correct data access.
  • Host-Side Integration: Modified host-side code to utilize the new GpuMat::View() method when launching kernels, facilitating the proper passing of GpuMatView objects.
Changelog
  • src/colmap/mvs/gpu_mat.h
    • Introduced GpuMatView<T> struct with device-side accessors (Get, Set, GetSlice, SetSlice, GetRef).
    • Moved __device__ methods from GpuMat<T> to GpuMatView<T>.
    • Added View() method to GpuMat<T> to create a GpuMatView<T>.
    • Removed __host__ __device__ qualifiers from GpuMat<T>'s GetPtr, GetPitch, GetWidth, GetHeight, GetDepth methods, making them host-only.
    • Updated kernel function signatures (FillWithScalarKernel, FillWithVectorKernel, FillWithRandomNumbersKernel) to accept GpuMatView<T>.
    • Modified GpuMat<T>'s FillWithScalar, FillWithVector, and FillWithRandomNumbers methods to pass GpuMatView to kernels.
  • src/colmap/mvs/gpu_mat_prng.cu
    • Updated InitRandomStateKernel to accept GpuMatView<curandState>.
    • Modified GpuMatPRNG constructor to pass View() to InitRandomStateKernel.
  • src/colmap/mvs/gpu_mat_ref_image.cu
    • Updated FilterKernel to accept GpuMatView for image, sum, and squared sum images.
    • Modified GpuMatRefImage::Filter to pass View() for GpuMat arguments to FilterKernel.
  • src/colmap/mvs/patch_match_cuda.cu
    • Updated InitNormalMap and RotateNormalMap kernels to accept GpuMatView.
    • Updated ComputeInitialCost and SweepFromTopToBottom kernels to accept GpuMatView for all GpuMat parameters.
    • Modified PatchMatchCuda::RunWithWindowSizeAndStep to pass View() to ComputeInitialCost and SweepFromTopToBottom.
    • Modified PatchMatchCuda::InitWorkspaceMemory and PatchMatchCuda::Rotate to pass View() to InitNormalMap and RotateNormalMap respectively.
Activity
  • No specific activity (comments, reviews, progress) has been recorded for this pull request yet.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution. ↩

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request effectively addresses a critical issue with CUDA compatibility on newer GPU architectures by introducing GpuMatView<T>. This new struct serves as a trivially-copyable view of GpuMat<T>, resolving the undefined behavior of passing non-trivially copyable objects to CUDA kernels. The implementation is clean, and the changes are consistently applied across all relevant kernels and launch sites. The approach is sound and aligns with best practices for CUDA programming. I have one minor suggestion to further improve code maintainability.

Comment thread src/colmap/mvs/gpu_mat.h Resolved
@ahojnnes
ahojnnes merged commit 4d4c100 into main Mar 5, 2026
14 checks passed
@ahojnnes
ahojnnes deleted the user/jsch/fix-non-trivially-copyable-cuda-arg branch March 5, 2026 11:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Empty patch match results on CUDA compute >= 100

2 participants