alpaka-group / alpaka

Abstraction Library for Parallel Kernel Acceleration :llama:
https://alpaka.readthedocs.io
Mozilla Public License 2.0
356 stars 74 forks source link

Add compiler check for the supported C++ standard in CMake #2414

Open SimeonEhrig opened 3 weeks ago

SimeonEhrig commented 3 weeks ago

In a offline discussion with @franzpoeschel he told me, that there are some confusion because alpaka does not compile with C++17 anymore. The reason is because the dev branch only supports C++20 and newer but the latest release (1.2) supports C++17.

Therefore an improvement would be to check the supported C++ standard of the compiler in CMake and display a message that the compiler does not support C++20 and maybe the message also to upgrade the compiler or use alpaka 1.2.

franzpoeschel commented 3 weeks ago

Even when the compiler supports C++20, problems will occur when including Alpaka into a code that itself is still C++17. So, this check should be more specifically about the C++ configuration of downstream codes that include Alpaka. I expect that this will be a common pitfall during the time shortly after Alpaka switches to C++20.

fwyzard commented 3 weeks ago

Isn't it enough to check

#if __cplusplus < 202002L
#error Alpaka supports only C++ 20 and later standards
#endif

in one of the main alpaka headers ?

SimeonEhrig commented 3 weeks ago

Isn't it enough to check

#if __cplusplus < 202002L
#error Alpaka supports only C++ 20 and later standards
#endif

in one of the main alpaka headers ?

Yes, and we should implement this for the approach if you don't use CMake. For CMake it would be a fall back, because in the configure phase of CMake, there is no code compiled. Therefore it will not fail at configure time. Therefore we have to solutions:

  1. Check if CMake has a feature to check the support C++ Standard
  2. Define a custom command, which is executed at configure time check if the example code can be compiled. If not, the C++ standard is not supported.
SimeonEhrig commented 3 weeks ago

Looks like mdspan has our solution :-) If I compile with mdspan, I get following message if I run CMake configure:

[cmake] -- Detected support for C++23 standard

And I found the following CMake code in mdspan:

if("cxx_std_23" IN_LIST CMAKE_CXX_COMPILE_FEATURES)
    set(CMAKE_CXX_STANDARD 23)
    message(STATUS "Detected support for C++23 standard")
  elseif("cxx_std_20" IN_LIST CMAKE_CXX_COMPILE_FEATURES)
    set(CMAKE_CXX_STANDARD 20)
    message(STATUS "Detected support for C++20 standard")
  elseif("cxx_std_17" IN_LIST CMAKE_CXX_COMPILE_FEATURES)
    set(CMAKE_CXX_STANDARD 17)
    message(STATUS "Detected support for C++17 standard")
  elseif("cxx_std_14" IN_LIST CMAKE_CXX_COMPILE_FEATURES)
    set(CMAKE_CXX_STANDARD 14)
    message(STATUS "Detected support for C++14 standard")
  else()
    message(FATAL_ERROR "Cannot detect CXX_STANDARD of C++14 or newer.")
  endif()