/home/docs/checkouts/readthedocs.org/user_builds/advanced-micro-devices-composable-kernel/checkouts/develop/include/ck/utility/array.hpp Source File

/home/docs/checkouts/readthedocs.org/user_builds/advanced-micro-devices-composable-kernel/checkouts/develop/include/ck/utility/array.hpp Source File#

Composable Kernel: /home/docs/checkouts/readthedocs.org/user_builds/advanced-micro-devices-composable-kernel/checkouts/develop/include/ck/utility/array.hpp Source File
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 
10 namespace ck {
11 
12 template <typename TData, index_t NSize>
13 struct 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
46 template <typename TData>
47 struct 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 
55 template <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
63 template <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:267
__host__ constexpr __device__ auto make_array(X &&x, Xs &&... xs)
Definition: array.hpp:56
remove_cv_t< remove_reference_t< T > > remove_cvref_t
Definition: type.hpp:297
int32_t index_t
Definition: ck.hpp:298
const GenericPointer< typename T::ValueType > T2 T::AllocatorType & a
Definition: pointer.h:1249
TData data_type
Definition: array.hpp:50
__host__ static constexpr __device__ index_t Size()
Definition: array.hpp:52
Definition: array.hpp:14
__host__ constexpr __device__ TData * begin()
Definition: array.hpp:41
__host__ constexpr __device__ TData * end()
Definition: array.hpp:42
__host__ constexpr __device__ const TData & At(index_t i) const
Definition: array.hpp:22
__host__ constexpr __device__ TData & operator()(index_t i)
Definition: array.hpp:28
__host__ constexpr __device__ const TData * begin() const
Definition: array.hpp:39
TData mData[NSize]
Definition: array.hpp:18
__host__ constexpr __device__ TData & At(index_t i)
Definition: array.hpp:24
__host__ constexpr __device__ auto operator=(const T &a)
Definition: array.hpp:31
__host__ constexpr __device__ const TData * end() const
Definition: array.hpp:40
__host__ constexpr __device__ const TData & operator[](index_t i) const
Definition: array.hpp:26
TData data_type
Definition: array.hpp:16
__host__ static constexpr __device__ index_t Size()
Definition: array.hpp:20
Definition: functional2.hpp:33