Implement conversion helpers between `Kokkos::Array` and `std::array`
We have many of these conversions: https://github.com/kokkos/kokkos-fft/blob/4fe0f2e5e2247904bcf35fcd2e5edbc2a081bd4a/common/src/KokkosFFT_transpose.hpp#L156 https://github.com/kokkos/kokkos-fft/blob/4fe0f2e5e2247904bcf35fcd2e5edbc2a081bd4a/common/src/KokkosFFT_transpose.hpp#L190 https://github.com/kokkos/kokkos-fft/blob/4fe0f2e5e2247904bcf35fcd2e5edbc2a081bd4a/common/src/KokkosFFT_transpose.hpp#L227 ...
It would be helpful to implement once and for all a helper to go back and forth between Kokkos::Array and std::array. It could even be an upstream proposal for Kokkos core.
https://godbolt.org/z/3vfv34MK3
@dalg24 do you think this can go to Kokkos 4.5 ?
I am torn on this. I don't mind too much the to_array free functions but I am afraid others will soon come and ask for regular conversion operations and I am strongly against these.
We already have some of that mess in Kokkos::complex in the name of "compatibility".
I would be happy to have these free functions, even for other projects. That could also be seen as helper tools in porting existing C++ codes to GPU.
About conversion operations we could argue that we only want to support at most the API of std::array.
Kokkos::to_array(std::array) -> Kokkos::Array looks like the obvious choice but what did you have in mind for the conversion Kokkos:: to std::array?
Kokkos::to_std_array(Kokkos::Array) -> std::array?
Apparently Kokkos::pair has a to_std_pair() member function
https://github.com/kokkos/kokkos/blob/abcb3dce75effd5cd7a2d73e9e51a9202573acab/core/src/Kokkos_Pair.hpp#L144-L146
Yes seems fine
namespace Kokkos
{
// 1.
template <class T, std::size_t N>
using array = Kokkos::Array<T, N>;
// 2.
template <class T, std::size_t N>
array<T, N> to_array(std::array<T, N> const&/&& x)
// 3.
template <class T, std::size_t N>
array<T, N> to_array(T (const&/&& x) [N])
// 4.
// maybe a member function to be coherent with the existing `to_std_pair`
template <class T, std::size_t N>
std::array<T, N> to_std_array(array<T, N> const&/&& x)
// 5.
template <class T1, class T2>
std::pair<T1, T2> to_std_pair(pair<T1, T2> const&/&& x)
}
What do you think of that ?
Alias: I am not loving the uppercase A either but I wonder if that would not just add more to confusion. Remember that there is no CTAD on aliases in C++17.
to_array free function overloads: conversion from C arrays is already there. https://github.com/kokkos/kokkos/blob/abcb3dce75effd5cd7a2d73e9e51a9202573acab/core/src/Kokkos_Array.hpp#L388-L396 I would want to think more about the const-qualifier and I would say no to the member function.
to_std_pair: I would say no unless someone actually ask for it or if you volunteer to document both pair and array ;)
Alias: I am not loving the uppercase A either but I wonder if that would not just add more to confusion. Remember that there is no CTAD on aliases in C++17.
What do you think of deprecating Kokko::Array in favor of Kokkos::array ? This alias could be used in 4.5 and make Kokkos::Array the alias in 5.0 ?
to_array free function overloads: conversion from C arrays is already there. https://github.com/kokkos/kokkos/blob/abcb3dce75effd5cd7a2d73e9e51a9202573acab/core/src/Kokkos_Array.hpp#L388-L396 I would want to think more about the const-qualifier and I would say no to the member function.
Are you wondering about the const qualifier for the C-array or std::array ? The current implementation seems to handle fine the const qualifier for C-array so proposition 3. is no more necessary.
to_std_pair: I would say no unless someone actually ask for it or if you volunteer to document both pair and array ;)
The idea is to be as close as possible to std::array and std::pair so that the documentation would be "go see cppref, Kokkos only make them GPU compatible". Of course we would need to document functions that allow conversions between standard and Kokkos implementations.
What do you think of deprecating
Kokko::Arrayin favor ofKokkos::array? This alias could be used in 4.5 and makeKokkos::Arraythe alias in 5.0 ?
Not sure I understand. You want template <...> using array = Array<...>; in 4.5 and in 5.0 we swap them template <...> using Array [[deprecated]] = array<...>;?
Are you wondering about the const qualifier for the C-array or
std::array? The current implementation seems to handle fine the const qualifier for C-array so proposition 3. is no more necessary.
You had a const-qualifier in your to_array(std::array const&) -> Kokkos::Array, I suspect we really want it non-const.
The idea is to be as close as possible to
std::arrayandstd::pairso that the documentation would be "go see cppref, Kokkos only make them GPU compatible". Of course we would need to document functions that allow conversions between standard and Kokkos implementations.
Right
Related discussion https://github.com/kokkos/kokkos/issues/7497
Shall we close ?
I think so