3

I am trying to enable the new C++20 support in CUDA 12 in Visual Studio 2022 using CMake 3.25. If I set these variables

set(CMAKE_CUDA_STANDARD 20)  # works with 17, but not 20
set(CMAKE_CUDA_STANDARD_REQUIRED ON)

then when my project is declared

project(Foo LANGUAGES CXX CUDA)

It gives an error: Target "Foo" requires the language dialect "CUDA20" . But the current compiler "NVIDIA" does not support this, or CMake does not know the flags to enable it.

I have spent a day trying setting other variables, passing command-line arguments to nvcc.exe, etc. but nothing has worked. The CUDA 12 documentation has also not helped me. I made absolutely sure that there is only one nvcc.exe on my entire system, and it's found in C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v12.0\bin.

Any ideas how to get C++20 working with CUDA 12?

Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982
Dale Barnard
  • 779
  • 1
  • 7
  • 16
  • 2
    Are you saying that even manually specifying `-std=c++20` does not work? As there was no new CMake version since CUDA 12 dropped, problems with CMake are not that surprising to me. You might just have to wait for better CUDA 12 support in CMake. Could it be that `nvcc` finds the wrong host compiler (i.e. one that doesn't have C++20 support)? – paleonix Jan 04 '23 at 20:01
  • 1
    "or CMake does not know the flags to enable it." – Robert Crovella Jan 04 '23 at 20:05
  • Passing --std=c++20 seems to be what happens at compile time, but the error I describe is when running configure on the cmake before I can attempt to compile it. As I mentioned, there is only one instance of nvcc.exe on my computer. From the CMake docs: "CUDA C++20. While CMake 3.12 and later recognize 20 as a valid value, CMake 3.18 was the first version to include support for any compiler." I am using 3.25. – Dale Barnard Jan 04 '23 at 21:17
  • 1
    "I have spent a day [...] passing command-line arguments to nvcc.exe" sounded like you gave up on CMake and tried compiling manually. While CMake knows 20 to be a valid option for C++, it might not know that it is a valid option for CUDA. I am asking for the host compiler(s), i.e. `msvc`. `nvcc` is a compiler wrapper and needs a C++20 host compiler for its own C++20 support. If it finds an older version of `msvc` it will not work with C++20. – paleonix Jan 04 '23 at 21:28
  • 2
    Just compile a very basic C++20 sample manually with `nvcc -std=c++20` and see if it works. – paleonix Jan 04 '23 at 21:30
  • 2
    I wouldn't be surprised if CMake is broken in this respect. If your question is how to do this with CMake, it might be that CMake development work is needed. If your question, as stated, is "Any ideas how to get C++20 working with CUDA 12?" (no mention of CMake) then I think you have your answer already. Compile with `nvcc`/Visual Studio, and add c++20 to the command line, perhaps similar to the method [here](https://stackoverflow.com/questions/63036624/how-to-enable-c17-code-generation-in-vs2019-cuda-project/63057409#63057409). – Robert Crovella Jan 04 '23 at 21:31
  • I think that passing the -std=c++20 works, but not through CMake. For now, I left it as set(CMAKE_CUDA_STANDARD 17) rather than 20. I tried to sneak the -std=c++20 in despite saying CUDA 17, but alas, the build fails due to other parameters that are different between 17 and 20. I guess I'm in a wait-for-CMake-to-catch-up mode. I will update my post when I have a solution. – Dale Barnard Jan 05 '23 at 16:20
  • I am also working on this problem, I'm hoping CMake 3.25.2 is coming out soon, it seems to have some planned fixes for this. Have you seen this conversation on the CMake repo? https://gitlab.kitware.com/cmake/cmake/-/issues/23079 – jperl Jan 07 '23 at 03:37

1 Answers1

6

UPDATE: CMake 3.25.2 is live! According to the changelog:

CUDA language level 20 (corresponding to C++20) is now supported with NVCC 12.0 and above.

This is now a supported configuration. Just update to CMake 3.25.2, and you should be good to go.

CMakeLists.txt

cmake_minimum_required (VERSION 3.25.2)

project (MyAwesomeCUDA12WithCpp20Project LANGUAGES CUDA CXX)

set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CUDA_STANDARD 20)
set(CMAKE_CUDA_STANDARD_REQUIRED ON)

...

Prior to this release, the only available solution was to build CMake from source. If something like this happens in the future (cutting edge SDKs not working together), try searching for recent discussions on Kitware's GitLab. In this case, the answer was hinted at in this discussion: https://gitlab.kitware.com/cmake/cmake/-/issues/23079.

!8008 (merged) added C++20 support for NVCC

From there, it can be seen the PR was merged into both master and release, which means you can build CMake from source following the instructions at https://cmake.org/install/. Or, you can build CMake using CMake as follows:

git clone https://gitlab.kitware.com/cmake/cmake
cd cmake
git checkout release
cmake . -B out
cmake --build out --config Release -j
jperl
  • 1,066
  • 7
  • 14