array.hpp Source File

array.hpp Source File#

Composable Kernel: array.hpp Source File
utility/array.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
2// Copyright (c) 2018-2025, Advanced Micro Devices, Inc. All rights reserved.
3
4#ifndef CK_ARRAY_HPP
5#define CK_ARRAY_HPP
6
7#include "functional2.hpp"
8#include "sequence.hpp"
9
10namespace ck {
11
12template <typename TData, index_t NSize>
13struct Array
14{
15 using type = Array;
16 using data_type = TData;
17
18 TData mData[NSize];
19
20 __host__ __device__ static constexpr index_t Size() { return NSize; }
21
22 __host__ __device__ constexpr const TData& At(index_t i) const { return mData[i]; }
23
24 __host__ __device__ constexpr TData& At(index_t i) { return mData[i]; }
25
26 __host__ __device__ constexpr const TData& operator[](index_t i) const { return At(i); }
27
28 __host__ __device__ constexpr TData& operator()(index_t i) { return At(i); }
29
30 template <typename T>
31 __host__ __device__ constexpr auto operator=(const T& a)
32 {
33 static_assert(T::Size() == Size(), "wrong! size not the same");
34
35 static_for<0, Size(), 1>{}([&](auto i) { operator()(i) = a[i]; });
36
37 return *this;
38 }
39 __host__ __device__ constexpr const TData* begin() const { return &mData[0]; }
40 __host__ __device__ constexpr const TData* end() const { return &mData[NSize]; }
41 __host__ __device__ constexpr TData* begin() { return &mData[0]; }
42 __host__ __device__ constexpr TData* end() { return &mData[NSize]; }
43};
44
45// empty Array
46template <typename TData>
47struct Array<TData, 0>
48{
49 using type = Array;
50 using data_type = TData;
51
52 __host__ __device__ static constexpr index_t Size() { return 0; }
53};
54
55template <typename X, typename... Xs>
56__host__ __device__ constexpr auto make_array(X&& x, Xs&&... xs)
57{
58 using data_type = remove_cvref_t<X>;
59 return Array<data_type, sizeof...(Xs) + 1>{ck::forward<X>(x), ck::forward<Xs>(xs)...};
60}
61
62// make empty array
63template <typename X>
64__host__ __device__ constexpr auto make_array()
65{
66 return Array<X, 0>{};
67}
68
69} // namespace ck
70#endif
Definition ck.hpp:268
int32_t index_t
Definition ck.hpp:299
remove_cv_t< remove_reference_t< T > > remove_cvref_t
Definition type.hpp:297
__host__ __device__ constexpr auto make_array()
Definition utility/array.hpp:64
const GenericPointer< typename T::ValueType > T2 T::AllocatorType & a
Definition pointer.h:1517
Array type
Definition utility/array.hpp:49
TData data_type
Definition utility/array.hpp:50
__host__ static __device__ constexpr index_t Size()
Definition utility/array.hpp:52
Definition utility/array.hpp:14
__host__ __device__ constexpr auto operator=(const T &a)
Definition utility/array.hpp:31
__host__ __device__ constexpr const TData & At(index_t i) const
Definition utility/array.hpp:22
__host__ static __device__ constexpr index_t Size()
Definition utility/array.hpp:20
__host__ __device__ constexpr TData & At(index_t i)
Definition utility/array.hpp:24
__host__ __device__ constexpr TData & operator()(index_t i)
Definition utility/array.hpp:28
__host__ __device__ constexpr const TData & operator[](index_t i) const
Definition utility/array.hpp:26
Array type
Definition utility/array.hpp:15
index_t mData[NSize]
Definition utility/array.hpp:18
__host__ __device__ constexpr const TData * end() const
Definition utility/array.hpp:40
__host__ __device__ constexpr const TData * begin() const
Definition utility/array.hpp:39
__host__ __device__ constexpr TData * begin()
Definition utility/array.hpp:41
__host__ __device__ constexpr TData * end()
Definition utility/array.hpp:42
TData data_type
Definition utility/array.hpp:16
Definition functional2.hpp:33