alpaka-group / alpaka

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

Native `std::mdspan` support #2319

Open SimeonEhrig opened 1 month ago

SimeonEhrig commented 1 month ago

During PR https://github.com/alpaka-group/alpaka/pull/2317#discussion_r1683056959 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

Not working

Unknown/no tested

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 commented 1 month ago

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 commented 1 month ago

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.