Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Native std::mdspan support #2319

Open
SimeonEhrig opened this issue Jul 22, 2024 · 2 comments
Open

Native std::mdspan support #2319

SimeonEhrig opened this issue Jul 22, 2024 · 2 comments

Comments

@SimeonEhrig
Copy link
Member

During PR #2317 (comment) there was the question if we already support the native implementation of C++23 std::mdspan. At the moment we do not support it. Instead we use the reference implementation from the kokkos team. This implementation has the two major advantages that it supports C++17 and is working with all our back-ends.

@fwyzard Suggest that we should use the native implementation of the standard library if the available. Therefore we have to solve two different problems.

  1. C++ 20 and older only supports only the access operator[] with one argument. Instead the operator() is used in the kokkos implementation. If C++23 is used, also the operator[] is available. The standard library implementation only supports the operator[]. Ether we write our codes with an preprocessor if-else or we use a wrapper function to provide the operator() function for the standard library implementation.
  2. Supporting the different back-ends. At the moment only the llvm C++ standard library (libc++) and the Microsoft compiler supports std::mdspan. Therefore the following back-ends works or not.

Working

  • Clang + libc++ for CPU backends
  • HIP + libc++

Not working

  • GCC (in theory it supports the libc++ but we do not support this)
  • NVCC (does not support the libc++ in general)
  • Clang as CUDA compiler (does not support the libc++ in general)

Unknown/no tested

  • MSVC (not tested but should work)
  • Apple Clang (not tested but should work)
  • OneAPI/Intel ICPX + libc++ (can work, because the compiler is clang based)

https://en.cppreference.com/w/cpp/compiler_support#cpp23

So, we need to implement a mechanism, which checks if the standard library is available and working with the active backend. Otherwise we need to use the kokkos implementation.

@fwyzard
Copy link
Contributor

fwyzard commented Jul 22, 2024

So, we need to implement a mechanism, which checks if the standard library is available and working with the active backend.

We could use

#if __cpp_lib_mdspan >= 202207L

// use the c++23 mdspan
#    include <mdspan>
using mdspan = std::mdspan;

#else

// use the standalone version
#    include <experimental/mdspan>
using mdspan = std::experimental::mdspan;

#endif

For the accessors, we could use

#if __cplusplus >= 202302L
// use the [x,y] accessor
#else
// use the (x,y) accessor
#endif

to decide what to use.

@SimeonEhrig
Copy link
Member Author

For the accessors, we could use

#if __cplusplus >= 202302L
// use the [x,y] accessor
#else
// use the (x,y) accessor
#endif

to decide what to use.

This is, how it would look in practice: https://github.com/SimeonEhrig/MyCpp/blob/28d20632657697533184f95c883739718f8397ad/features/23/mdspan/hello_mspan/main.cpp#L44

Actual I would prefer the wrapper. But in this case, we need to inherit from std::mdspan to implement the operator(). Therefore we can also not use the stl implementation directly.

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