kokkos-fft icon indicating copy to clipboard operation
kokkos-fft copied to clipboard

Implement conversion helpers between `Kokkos::Array` and `std::array`

Open tpadioleau opened this issue 1 year ago • 8 comments

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.

tpadioleau avatar Oct 11 '24 12:10 tpadioleau

https://godbolt.org/z/3vfv34MK3

dalg24 avatar Oct 11 '24 13:10 dalg24

@dalg24 do you think this can go to Kokkos 4.5 ?

tpadioleau avatar Oct 11 '24 13:10 tpadioleau

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".

dalg24 avatar Oct 11 '24 13:10 dalg24

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.

tpadioleau avatar Oct 11 '24 14:10 tpadioleau

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

dalg24 avatar Oct 11 '24 16:10 dalg24

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 ?

tpadioleau avatar Oct 11 '24 16:10 tpadioleau

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 ;)

dalg24 avatar Oct 11 '24 17:10 dalg24

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.

tpadioleau avatar Oct 13 '24 07:10 tpadioleau

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 ?

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::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.

Right

dalg24 avatar Oct 14 '24 11:10 dalg24

Related discussion https://github.com/kokkos/kokkos/issues/7497

Shall we close ?

tpadioleau avatar Oct 30 '24 15:10 tpadioleau

I think so

yasahi-hpc avatar Oct 30 '24 17:10 yasahi-hpc