|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#pragma once |
|
|
|
|
|
#include <cute/config.hpp> |
|
|
#include <cute/util/type_traits.hpp> |
|
|
#include <cute/container/tuple.hpp> |
|
|
#include <cute/numeric/integral_constant.hpp> |
|
|
#include <cute/numeric/integer_sequence.hpp> |
|
|
#include <cute/numeric/math.hpp> |
|
|
#include <cute/numeric/arithmetic_tuple.hpp> |
|
|
#include <cute/algorithm/functional.hpp> |
|
|
#include <cute/algorithm/tuple_algorithms.hpp> |
|
|
#include <cute/int_tuple.hpp> |
|
|
|
|
|
namespace cute |
|
|
{ |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
template <class Coord, class Shape, class Stride> |
|
|
CUTE_HOST_DEVICE constexpr |
|
|
auto |
|
|
crd2idx(Coord const& coord, |
|
|
Shape const& shape, |
|
|
Stride const& stride); |
|
|
|
|
|
namespace detail { |
|
|
|
|
|
template <class Coord, class Shape, class Stride, int... Is> |
|
|
CUTE_HOST_DEVICE constexpr |
|
|
auto |
|
|
crd2idx_ttt(Coord const& coord, |
|
|
Shape const& shape, |
|
|
Stride const& stride, seq<Is...>) |
|
|
{ |
|
|
return (... + crd2idx(get<Is>(coord), get<Is>(shape), get<Is>(stride))); |
|
|
} |
|
|
|
|
|
template <class CInt, class STuple, class DTuple, int I0, int... Is> |
|
|
CUTE_HOST_DEVICE constexpr |
|
|
auto |
|
|
crd2idx_itt(CInt const& coord, |
|
|
STuple const& shape, |
|
|
DTuple const& stride, seq<I0,Is...>) |
|
|
{ |
|
|
if constexpr (sizeof...(Is) == 0) { |
|
|
return crd2idx(coord, get<I0>(shape), get<I0>(stride)); |
|
|
} else if constexpr (is_constant<0, CInt>::value) { |
|
|
return crd2idx(_0{}, get<I0>(shape), get<I0>(stride)) |
|
|
+ (_0{} + ... + crd2idx(_0{}, get<Is>(shape), get<Is>(stride))); |
|
|
} else { |
|
|
auto [div, mod] = divmod(coord, product(get<I0>(shape))); |
|
|
return crd2idx(mod, get<I0>(shape), get<I0>(stride)) |
|
|
+ crd2idx_itt(div, shape, stride, seq<Is...>{}); |
|
|
} |
|
|
|
|
|
CUTE_GCC_UNREACHABLE; |
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
template <class Coord, class Shape, class Stride> |
|
|
CUTE_HOST_DEVICE constexpr |
|
|
auto |
|
|
crd2idx(Coord const& coord, |
|
|
Shape const& shape, |
|
|
Stride const& stride) |
|
|
{ |
|
|
if constexpr (is_tuple<Coord>::value) { |
|
|
if constexpr (is_tuple<Shape>::value) { |
|
|
static_assert(tuple_size<Coord>::value == tuple_size< Shape>::value, "Mismatched Ranks"); |
|
|
static_assert(tuple_size<Coord>::value == tuple_size<Stride>::value, "Mismatched Ranks"); |
|
|
return detail::crd2idx_ttt(coord, shape, stride, tuple_seq<Coord>{}); |
|
|
} else { |
|
|
static_assert(sizeof(Coord) == 0, "Invalid parameters"); |
|
|
} |
|
|
} else { |
|
|
if constexpr (is_tuple<Shape>::value) { |
|
|
static_assert(tuple_size<Shape>::value == tuple_size<Stride>::value, "Mismatched Ranks"); |
|
|
return detail::crd2idx_itt(coord, shape, stride, tuple_seq<Shape>{}); |
|
|
} else { |
|
|
return coord * stride; |
|
|
} |
|
|
} |
|
|
|
|
|
CUTE_GCC_UNREACHABLE; |
|
|
} |
|
|
|
|
|
namespace detail { |
|
|
|
|
|
template <class CTuple, class STuple, int I0, int... Is> |
|
|
CUTE_HOST_DEVICE constexpr |
|
|
auto |
|
|
crd2idx_horner(CTuple const& coord, |
|
|
STuple const& shape, seq<I0,Is...>) |
|
|
{ |
|
|
if constexpr (sizeof...(Is) == 0) { |
|
|
return get<I0>(coord); |
|
|
} else { |
|
|
return get<I0>(coord) + get<I0>(shape) * crd2idx_horner(coord, shape, seq<Is...>{}); |
|
|
} |
|
|
|
|
|
CUTE_GCC_UNREACHABLE; |
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
template <class Coord, class Shape> |
|
|
CUTE_HOST_DEVICE constexpr |
|
|
auto |
|
|
crd2idx(Coord const& coord, |
|
|
Shape const& shape) |
|
|
{ |
|
|
if constexpr (is_integral<Coord>::value) { |
|
|
return coord; |
|
|
} else if constexpr (is_integral<Shape>::value) { |
|
|
static_assert(dependent_false<Shape>, "Invalid parameters"); |
|
|
} else { |
|
|
static_assert(tuple_size<Coord>::value == tuple_size<Shape>::value, "Mismatched Ranks"); |
|
|
auto flat_coord = flatten(coord); |
|
|
auto flat_shape = flatten(product_like(shape, coord)); |
|
|
return detail::crd2idx_horner(flat_coord, flat_shape, tuple_seq<decltype(flat_shape)>{}); |
|
|
} |
|
|
|
|
|
CUTE_GCC_UNREACHABLE; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
template <class Index, class Shape, class Stride> |
|
|
CUTE_HOST_DEVICE constexpr |
|
|
auto |
|
|
idx2crd(Index const& idx, |
|
|
Shape const& shape, |
|
|
Stride const& stride) |
|
|
{ |
|
|
if constexpr (is_tuple<Index>::value) { |
|
|
if constexpr (is_tuple<Shape>::value) { |
|
|
static_assert(tuple_size<Index>::value == tuple_size< Shape>::value, "Mismatched Ranks"); |
|
|
static_assert(tuple_size<Index>::value == tuple_size<Stride>::value, "Mismatched Ranks"); |
|
|
return transform(idx, shape, stride, [](auto const& i, auto const& s, auto const& d){ return idx2crd(i,s,d); }); |
|
|
} else { |
|
|
static_assert(sizeof(Index) == 0, "Invalid parameters"); |
|
|
} |
|
|
} else { |
|
|
if constexpr (is_tuple<Shape>::value) { |
|
|
if constexpr (is_tuple<Stride>::value) { |
|
|
static_assert(tuple_size<Shape>::value == tuple_size<Stride>::value, "Mismatched Ranks"); |
|
|
return transform(shape, stride, [&](auto const& s, auto const& d){ return idx2crd(idx,s,d); }); |
|
|
} else { |
|
|
return transform(shape, compact_col_major(shape, stride), [&](auto const& s, auto const& d){ return idx2crd(idx,s,d); }); |
|
|
} |
|
|
} else { |
|
|
if constexpr (is_constant<1, Shape>::value) { |
|
|
|
|
|
return Int<0>{}; |
|
|
} else { |
|
|
return (idx / stride) % shape; |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
CUTE_GCC_UNREACHABLE; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
template <class Index, class Shape> |
|
|
CUTE_HOST_DEVICE constexpr |
|
|
auto |
|
|
idx2crd(Index const& idx, |
|
|
Shape const& shape) |
|
|
{ |
|
|
if constexpr (is_tuple<Index>::value) { |
|
|
if constexpr (is_tuple<Shape>::value) { |
|
|
static_assert(tuple_size<Index>::value == tuple_size<Shape>::value, "Mismatched Ranks"); |
|
|
return transform(idx, shape, [](auto const& i, auto const& s) { return idx2crd(i,s); }); |
|
|
} else { |
|
|
static_assert(sizeof(Index) == 0, "Invalid parameters"); |
|
|
} |
|
|
} else { |
|
|
if constexpr (is_tuple<Shape>::value) { |
|
|
return transform_leaf(as_arithmetic_tuple(crd2idx(idx, shape, make_basis_like(shape))), identity{}); |
|
|
} else { |
|
|
return idx; |
|
|
} |
|
|
} |
|
|
|
|
|
CUTE_GCC_UNREACHABLE; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
template <class Coord, class SShape, class DShape> |
|
|
CUTE_HOST_DEVICE constexpr |
|
|
auto |
|
|
crd2crd(Coord const& coord, |
|
|
SShape const& src_shape, |
|
|
DShape const& dst_shape) |
|
|
{ |
|
|
if constexpr (is_tuple<Coord>::value && is_tuple<SShape>::value && is_tuple<DShape>::value) { |
|
|
static_assert(tuple_size<Coord>::value == tuple_size<SShape>::value, "Mismatched Ranks"); |
|
|
static_assert(tuple_size<Coord>::value == tuple_size<DShape>::value, "Mismatched Ranks"); |
|
|
return transform(coord, src_shape, dst_shape, [](auto const& c, auto const& s, auto const& d) { return crd2crd(c,s,d); }); |
|
|
} else { |
|
|
|
|
|
return idx2crd(crd2idx(coord, src_shape), dst_shape); |
|
|
} |
|
|
|
|
|
CUTE_GCC_UNREACHABLE; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
struct LayoutLeft; |
|
|
using GenColMajor = LayoutLeft; |
|
|
|
|
|
struct LayoutRight; |
|
|
using GenRowMajor = LayoutRight; |
|
|
|
|
|
namespace detail { |
|
|
|
|
|
|
|
|
template <class Major> |
|
|
struct CompactLambda; |
|
|
|
|
|
|
|
|
|
|
|
template <class Major, class Shape, class Current> |
|
|
CUTE_HOST_DEVICE constexpr |
|
|
auto |
|
|
compact(Shape const& shape, |
|
|
Current const& current) |
|
|
{ |
|
|
if constexpr (is_tuple<Shape>::value) { |
|
|
using Lambda = CompactLambda<Major>; |
|
|
using Seq = typename Lambda::template seq<Shape>; |
|
|
return cute::detail::fold(shape, cute::make_tuple(cute::make_tuple(), current), Lambda{}, Seq{}); |
|
|
} else { |
|
|
if constexpr (is_constant<1, Shape>::value) { |
|
|
return cute::make_tuple(Int<0>{}, current); |
|
|
} else { |
|
|
return cute::make_tuple(current, current * shape); |
|
|
} |
|
|
} |
|
|
|
|
|
CUTE_GCC_UNREACHABLE; |
|
|
} |
|
|
|
|
|
|
|
|
template <> |
|
|
struct CompactLambda<LayoutLeft> |
|
|
{ |
|
|
template <class Init, class Shape> |
|
|
CUTE_HOST_DEVICE constexpr auto |
|
|
operator()(Init const& init, Shape const& si) { |
|
|
auto result = detail::compact<LayoutLeft>(si, get<1>(init)); |
|
|
return cute::make_tuple(append(get<0>(init), get<0>(result)), get<1>(result)); |
|
|
} |
|
|
|
|
|
template <class Shape> |
|
|
using seq = tuple_seq<Shape>; |
|
|
}; |
|
|
|
|
|
|
|
|
template <> |
|
|
struct CompactLambda<LayoutRight> |
|
|
{ |
|
|
template <class Init, class Shape> |
|
|
CUTE_HOST_DEVICE constexpr auto |
|
|
operator()(Init const& init, Shape const& si) { |
|
|
auto result = detail::compact<LayoutRight>(si, get<1>(init)); |
|
|
return cute::make_tuple(prepend(get<0>(init), get<0>(result)), get<1>(result)); |
|
|
} |
|
|
|
|
|
template <class Shape> |
|
|
using seq = tuple_rseq<Shape>; |
|
|
}; |
|
|
|
|
|
} |
|
|
|
|
|
template <class Major, class Shape, class Current = Int<1>, |
|
|
__CUTE_REQUIRES(is_tuple<Shape>::value || is_integral<Shape>::value)> |
|
|
CUTE_HOST_DEVICE constexpr |
|
|
auto |
|
|
compact_major(Shape const& shape, |
|
|
Current const& current = {}) |
|
|
{ |
|
|
if constexpr (is_tuple<Current>::value) { |
|
|
static_assert(is_tuple<Shape>::value, "Invalid parameters"); |
|
|
static_assert(tuple_size<Shape>::value == tuple_size<Current>::value, "Mismatched Ranks"); |
|
|
|
|
|
return transform(shape, current, [&](auto const& s, auto const& c){ return compact_major<Major>(s,c); }); |
|
|
} else { |
|
|
return get<0>(detail::compact<Major>(shape, current)); |
|
|
} |
|
|
|
|
|
CUTE_GCC_UNREACHABLE; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
struct LayoutLeft { |
|
|
template <class Shape> |
|
|
using Apply = decltype(compact_major<LayoutLeft>(declval<Shape>())); |
|
|
}; |
|
|
|
|
|
template <class Shape, class Current = Int<1>> |
|
|
CUTE_HOST_DEVICE constexpr |
|
|
auto |
|
|
compact_col_major(Shape const& shape, |
|
|
Current const& current = {}) |
|
|
{ |
|
|
return compact_major<LayoutLeft>(shape, current); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
struct LayoutRight { |
|
|
template <class Shape> |
|
|
using Apply = decltype(compact_major<LayoutRight>(declval<Shape>())); |
|
|
}; |
|
|
|
|
|
template <class Shape, class Current = Int<1>> |
|
|
CUTE_HOST_DEVICE constexpr |
|
|
auto |
|
|
compact_row_major(Shape const& shape, |
|
|
Current const& current = {}) |
|
|
{ |
|
|
return compact_major<LayoutRight>(shape, current); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace detail { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
template <class Shape, class Order, class RefShape, class RefOrder> |
|
|
CUTE_HOST_DEVICE constexpr |
|
|
auto |
|
|
compact_order(Shape const& shape, Order const& order, |
|
|
RefShape const& ref_shape, RefOrder const& ref_order) |
|
|
{ |
|
|
if constexpr (is_tuple<Order>::value) { |
|
|
static_assert(tuple_size<Shape>::value == tuple_size<Order>::value, "Need equal rank of shape and order"); |
|
|
return transform(shape, order, [&](auto const& s, auto const& o) { return compact_order(s, o, ref_shape, ref_order); }); |
|
|
} else { |
|
|
|
|
|
auto stride_start = product(transform(ref_shape, ref_order, |
|
|
[&](auto const& s, auto const& o) { |
|
|
return conditional_return(o < order, s, Int<1>{}); |
|
|
})); |
|
|
return compact_col_major(shape, stride_start); |
|
|
} |
|
|
|
|
|
CUTE_GCC_UNREACHABLE; |
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
template <class Shape, class Order> |
|
|
CUTE_HOST_DEVICE constexpr |
|
|
auto |
|
|
compact_order(Shape const& shape, Order const& order) |
|
|
{ |
|
|
auto ref_shape = flatten_to_tuple(product_like(shape, order)); |
|
|
|
|
|
auto flat_order = flatten_to_tuple(order); |
|
|
|
|
|
auto max_order = cute::fold(flat_order, Int<0>{}, [](auto v, auto order) { |
|
|
if constexpr (is_constant<true, decltype(v < order)>::value) { |
|
|
return order; |
|
|
} else { |
|
|
return v; |
|
|
} |
|
|
|
|
|
CUTE_GCC_UNREACHABLE; |
|
|
}); |
|
|
|
|
|
auto max_seq = make_range<max_order+1, max_order+1+rank(flat_order)>{}; |
|
|
auto ref_order = cute::transform(max_seq, flat_order, [](auto seq_v, auto order) { |
|
|
if constexpr (is_static<decltype(order)>::value) { |
|
|
return order; |
|
|
} else { |
|
|
return seq_v; |
|
|
} |
|
|
|
|
|
CUTE_GCC_UNREACHABLE; |
|
|
}); |
|
|
|
|
|
auto new_order = unflatten(ref_order, order); |
|
|
|
|
|
return detail::compact_order(shape, new_order, ref_shape, ref_order); |
|
|
} |
|
|
|
|
|
template <class Shape> |
|
|
CUTE_HOST_DEVICE constexpr |
|
|
auto |
|
|
compact_order(Shape const& shape, GenColMajor const& major) |
|
|
{ |
|
|
return compact_major<LayoutLeft>(shape); |
|
|
} |
|
|
|
|
|
template <class Shape> |
|
|
CUTE_HOST_DEVICE constexpr |
|
|
auto |
|
|
compact_order(Shape const& shape, GenRowMajor const& major) |
|
|
{ |
|
|
return compact_major<LayoutRight>(shape); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace detail { |
|
|
|
|
|
template <class Coord, class Shape, class Order> |
|
|
CUTE_HOST_DEVICE constexpr |
|
|
void |
|
|
increment(Coord& coord, Shape const& shape, Order const& order) |
|
|
{ |
|
|
++basis_get(get<0>(order), coord); |
|
|
cute::for_each(make_range<1, tuple_size<Order>::value>{}, [&](auto i){ |
|
|
if (basis_get(get<i-1>(order), coord) == basis_get(get<i-1>(order), shape)) { |
|
|
basis_get(get<i-1>(order), coord) = 0; |
|
|
++basis_get(get<i>(order), coord); |
|
|
} |
|
|
}); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
template <class Coord, class Shape> |
|
|
CUTE_HOST_DEVICE constexpr |
|
|
void |
|
|
increment(Coord& coord, Shape const& shape) |
|
|
{ |
|
|
increment(coord, shape, flatten_to_tuple(make_basis_like(shape))); |
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
struct ForwardCoordIteratorSentinel |
|
|
{}; |
|
|
|
|
|
|
|
|
|
|
|
template <class Coord, class Shape, class Order> |
|
|
struct ForwardCoordIterator |
|
|
{ |
|
|
static_assert(is_congruent<Coord, Shape>::value); |
|
|
|
|
|
CUTE_HOST_DEVICE constexpr |
|
|
Coord const& operator*() const { return coord; } |
|
|
CUTE_HOST_DEVICE constexpr |
|
|
ForwardCoordIterator& operator++() { detail::increment(coord, shape, Order{}); return *this; } |
|
|
|
|
|
CUTE_HOST_DEVICE constexpr |
|
|
bool operator==(ForwardCoordIteratorSentinel const&) const { return basis_get(back(Order{}), coord) == basis_get(back(Order{}), shape); } |
|
|
CUTE_HOST_DEVICE constexpr |
|
|
bool operator!=(ForwardCoordIteratorSentinel const&) const { return basis_get(back(Order{}), coord) != basis_get(back(Order{}), shape); } |
|
|
|
|
|
CUTE_HOST_DEVICE constexpr |
|
|
bool operator==(ForwardCoordIterator const& other) const { return coord == other.coord; } |
|
|
CUTE_HOST_DEVICE constexpr |
|
|
bool operator!=(ForwardCoordIterator const& other) const { return coord != other.coord; } |
|
|
|
|
|
Coord coord; |
|
|
Shape const& shape; |
|
|
}; |
|
|
|
|
|
|
|
|
template <class Order, class Shape, class Coord> |
|
|
CUTE_HOST_DEVICE constexpr |
|
|
auto |
|
|
make_coord_iterator(Coord const& coord, Shape const& shape) |
|
|
{ |
|
|
static_assert(is_congruent<Coord, Shape>::value); |
|
|
static_assert(is_congruent<Order, Coord>::value); |
|
|
static_assert(is_congruent<Order, Shape>::value); |
|
|
auto flat_order = flatten_to_tuple(Order{}); |
|
|
auto inv_order = transform(make_seq<rank(flat_order)>{}, [&](auto i){ return find(flat_order, i); }); |
|
|
auto basis_order = transform_leaf(inv_order, [&](auto i) { return get<i>(flatten_to_tuple(make_basis_like(shape))); }); |
|
|
return ForwardCoordIterator<Coord,Shape,decltype(basis_order)>{coord,shape}; |
|
|
} |
|
|
|
|
|
|
|
|
template <class Shape, class Coord> |
|
|
CUTE_HOST_DEVICE constexpr |
|
|
auto |
|
|
make_coord_iterator(Coord const& coord, Shape const& shape) |
|
|
{ |
|
|
static_assert(is_congruent<Coord, Shape>::value); |
|
|
auto basis_order = flatten_to_tuple(make_basis_like(shape)); |
|
|
return ForwardCoordIterator<Coord,Shape,decltype(basis_order)>{coord,shape}; |
|
|
} |
|
|
|
|
|
|
|
|
template <class Order, class Shape> |
|
|
CUTE_HOST_DEVICE constexpr |
|
|
auto |
|
|
make_coord_iterator(Shape const& shape) |
|
|
{ |
|
|
return make_coord_iterator<Order>(repeat_like(shape, int(0)), shape); |
|
|
} |
|
|
|
|
|
|
|
|
template <class Shape> |
|
|
CUTE_HOST_DEVICE constexpr |
|
|
auto |
|
|
make_coord_iterator(Shape const& shape) |
|
|
{ |
|
|
return make_coord_iterator(repeat_like(shape, int(0)), shape); |
|
|
} |
|
|
|
|
|
} |
|
|
|