Skip to content

Simplified CUDA Architecture Handling in CMake#27085

Closed
tony19053000 wants to merge 1 commit intoopencv:4.xfrom
tony19053000:fix-unused-cuda-var
Closed

Simplified CUDA Architecture Handling in CMake#27085
tony19053000 wants to merge 1 commit intoopencv:4.xfrom
tony19053000:fix-unused-cuda-var

Conversation

@tony19053000
Copy link
Copy Markdown

@tony19053000 tony19053000 commented Mar 17, 2025

Related: #27076

  • Removed the unused USER_DEFINED_CMAKE_CUDA_ARCHITECTURES variable.
  • Simplified the CMake logic for setting CUDA architectures.
  • Ensured CUDA is initialized properly without extra conditions.
  • Made the code cleaner and easier to understand while keeping the same functionality.

@asmorkalov
Copy link
Copy Markdown
Contributor

@cudawarped What do you think?

@cudawarped
Copy link
Copy Markdown
Contributor

@cudawarped What do you think?

This removes the fix added in #25941. Additionaly if we wanted to remove that fix there would be no reason to have the section after setting the language because it doesn't do anything @tony19053000?

if(NOT CMAKE_CUDA_ARCHITECTURES)
  set(CMAKE_CUDA_ARCHITECTURES "")
endif()

As per the original issue (#25920) and PR (#25941) when the minimum version of CMake is 3.18 CMAKE_CUDA_ARCHITECTURES is automatically initialized to the default architecture chosen by the compiler. This means without that fix (and with this PR) we would have to ignore CMAKE_CUDA_ARCHITECTURES or always use it (we couldn't use CUDA_ARCH_BIN/CUDA_ARCH_PTX).

The reason for this is that if CMAKE_CUDA_ARCHITECTURES is empty enabling CUDA as a first class language (enable_language(CUDA)) initializes CMAKE_CUDA_ARCHITECTURES so it contains a valid value. #25941 prevents that so we can fall back to OpenCV's internal logic which uses CUDA_ARCH_BIN/CUDA_ARCH_PTX. e.g.


Behaviour with #25941

CMAKE_CUDA_ARCHITECTURES not set

  • CMAKE_CUDA_ARCHITECTURES= (in)
  • enable_language(CUDA)
  • CMAKE_CUDA_ARCHITECTURES=52 (default architecture chosen by the compiler)
  • set(CMAKE_CUDA_ARCHITECTURES "")
  • CMAKE_CUDA_ARCHITECTURES= (out)

CMAKE_CUDA_ARCHITECTURES set

  • CMAKE_CUDA_ARCHITECTURES=75 (in)
  • USER_DEFINED_CMAKE_CUDA_ARCHITECTURES=TRUE
  • enable_language(CUDA)
  • CMAKE_CUDA_ARCHITECTURES=75 (out)

Behaviour with this PR (previous behaviour)

CMAKE_CUDA_ARCHITECTURES not set

  • CMAKE_CUDA_ARCHITECTURES= (in)
  • enable_language(CUDA)
  • CMAKE_CUDA_ARCHITECTURES=52 (default architecture chosen by the compiler)
  • CMAKE_CUDA_ARCHITECTURES=52 (out)

@patrikhuber
Copy link
Copy Markdown
Contributor

I think the current code essentially just simplifies to this, or does it not?

      if(NOT CMAKE_CUDA_ARCHITECTURES)
        set(CMAKE_CUDA_ARCHITECTURES "")
      endif()
      enable_language(CUDA)

Also as a related note. I just tried to build the latest opencv 4.x main (cloned 3 days ago), and from a completely empty build directory/cache, I've initialised CMake with -DCMAKE_CUDA_ARCHITECTURES:STRING="86;89;90;100;120". However, my CUDA_ARCH_BIN in the cmake cache was then still set to the "generic" default values of "5.0; ...". So it seems initialising the CUDA architecture with CMAKE_CUDA_ARCHITECTURES (which is the standard) is currently not working?

@cudawarped
Copy link
Copy Markdown
Contributor

I think the current code essentially just simplifies to this, or does it not?

      if(NOT CMAKE_CUDA_ARCHITECTURES)
        set(CMAKE_CUDA_ARCHITECTURES "")
      endif()
      enable_language(CUDA)

That results in the following error for me

CMake Error at C:/Program Files/CMake/share/cmake-3.29/Modules/Internal/CMakeCUDAArchitecturesValidate.cmake:7 (message):
CMAKE_CUDA_ARCHITECTURES must be non-empty if set.
Call Stack (most recent call first):
C:/Program Files/CMake/share/cmake-3.29/Modules/CMakeDetermineCUDACompiler.cmake:112 (cmake_cuda_architectures_validate)
CMakeLists.txt:693 (enable_language)

Try configuring with -DENABLE_CUDA_FIRST_CLASS_LANGUAGE=ON -DCMAKE_CUDA_ARCHITECTURES=86 -DMIN_VER_CMAKE=3.18 with and without your suggestions.

Also as a related note. I just tried to build the latest opencv 4.x main (cloned 3 days ago), and from a completely empty build directory/cache, I've initialised CMake with -DCMAKE_CUDA_ARCHITECTURES:STRING="86;89;90;100;120". However, my CUDA_ARCH_BIN in the cmake cache was then still set to the "generic" default values of "5.0; ...". So it seems initialising the CUDA architecture with CMAKE_CUDA_ARCHITECTURES (which is the standard) is currently not working?

I just tried and its working for me have you set CUDA a first class language (-DENABLE_CUDA_FIRST_CLASS_LANGUAGE=ON)?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants