array_multi_index.hpp Source File

array_multi_index.hpp Source File#

Composable Kernel: array_multi_index.hpp Source File
array_multi_index.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
2// Copyright (c) 2018-2023, Advanced Micro Devices, Inc. All rights reserved.
3
4#ifndef CK_ARRAY_MULTI_INDEX_HPP
5#define CK_ARRAY_MULTI_INDEX_HPP
6
7#include "common_header.hpp"
8
9namespace ck {
10
11template <index_t N>
13
14template <typename... Xs>
15__host__ __device__ constexpr auto make_multi_index(Xs&&... xs)
16{
17 return make_array<index_t>(index_t{xs}...);
18}
19
20template <index_t NSize>
21__host__ __device__ constexpr auto make_zero_multi_index()
22{
23 return unpack([](auto... xs) { return make_multi_index(xs...); },
25}
26
27template <typename T>
28__host__ __device__ constexpr auto to_multi_index(const T& x)
29{
30 return unpack([](auto... ys) { return make_multi_index(ys...); }, x);
31}
32
33template <index_t NSize, typename X>
34__host__ __device__ constexpr auto operator+=(MultiIndex<NSize>& y, const X& x)
35{
36 static_assert(X::Size() == NSize, "wrong! size not the same");
37 static_for<0, NSize, 1>{}([&](auto i) { y(i) += x[i]; });
38 return y;
39}
40
41template <index_t NSize, typename X>
42__host__ __device__ constexpr auto operator-=(MultiIndex<NSize>& y, const X& x)
43{
44 static_assert(X::Size() == NSize, "wrong! size not the same");
45 static_for<0, NSize, 1>{}([&](auto i) { y(i) -= x[i]; });
46 return y;
47}
48
49template <index_t NSize, typename T>
50__host__ __device__ constexpr auto operator+(const MultiIndex<NSize>& a, const T& b)
51{
52 using type = MultiIndex<NSize>;
53 static_assert(T::Size() == NSize, "wrong! size not the same");
54 type r;
55 static_for<0, NSize, 1>{}([&](auto i) { r(i) = a[i] + b[i]; });
56 return r;
57}
58
59template <index_t NSize, typename T>
60__host__ __device__ constexpr auto operator-(const MultiIndex<NSize>& a, const T& b)
61{
62 using type = MultiIndex<NSize>;
63 static_assert(T::Size() == NSize, "wrong! size not the same");
64 type r;
65 static_for<0, NSize, 1>{}([&](auto i) { r(i) = a[i] - b[i]; });
66 return r;
67}
68
69template <index_t NSize, typename T>
70__host__ __device__ constexpr auto operator*(const MultiIndex<NSize>& a, const T& b)
71{
72 using type = MultiIndex<NSize>;
73 static_assert(T::Size() == NSize, "wrong! size not the same");
74 type r;
75 static_for<0, NSize, 1>{}([&](auto i) { r(i) = a[i] * b[i]; });
76 return r;
77}
78
79} // namespace ck
80#endif
Definition ck.hpp:268
__host__ __device__ constexpr auto make_multi_index(Xs &&... xs)
Definition array_multi_index.hpp:15
int32_t index_t
Definition ck.hpp:299
__host__ __device__ constexpr auto operator+=(MultiIndex< NSize > &y, const X &x)
Definition array_multi_index.hpp:34
__host__ __device__ constexpr auto unpack(F &&f, X &&x)
Definition functional4.hpp:46
__host__ __device__ constexpr auto operator-=(MultiIndex< NSize > &y, const X &x)
Definition array_multi_index.hpp:42
__host__ __device__ constexpr auto operator-(const MultiIndex< NSize > &a, const T &b)
Definition array_multi_index.hpp:60
__host__ __device__ constexpr auto to_multi_index(const T &x)
Definition array_multi_index.hpp:28
__host__ __device__ constexpr auto operator+(const MultiIndex< NSize > &a, const T &b)
Definition array_multi_index.hpp:50
__host__ __device__ constexpr auto make_zero_multi_index()
Definition array_multi_index.hpp:21
__host__ __device__ constexpr auto make_array()
Definition utility/array.hpp:64
__host__ __device__ constexpr auto operator*(const MultiIndex< NSize > &a, const T &b)
Definition array_multi_index.hpp:70
Array< index_t, N > MultiIndex
Definition array_multi_index.hpp:12
const GenericPointer< typename T::ValueType > T2 T::AllocatorType & a
Definition pointer.h:1517
Definition utility/array.hpp:14
Definition functional2.hpp:33
typename sequence_gen< NSize, F >::type type
Definition utility/sequence.hpp:295