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

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

Composable Kernel: /home/docs/checkouts/readthedocs.org/user_builds/advanced-micro-devices-composable-kernel/checkouts/3643/include/ck/utility/array.hpp Source File
array.hpp
Go to the documentation of this file.
1 // Copyright (c) Advanced Micro Devices, Inc., or its affiliates.
2 // SPDX-License-Identifier: MIT
3 
4 #ifndef CK_ARRAY_HPP
5 #define CK_ARRAY_HPP
6 
7 #include "functional2.hpp"
8 #include "sequence.hpp"
9 #include <type_traits>
10 #include <cassert>
11 
12 namespace ck {
13 
14 template <typename TData, index_t NSize>
15 struct Array
16 {
17  using type = Array;
18  using data_type = TData;
19 
20  TData mData[NSize];
21 
22  __host__ __device__ static constexpr index_t Size() { return NSize; }
23 
24  __host__ __device__ constexpr const TData& At(index_t i) const { return mData[i]; }
25 
26  __host__ __device__ constexpr TData& At(index_t i) { return mData[i]; }
27 
28  __host__ __device__ constexpr const TData& operator[](index_t i) const { return At(i); }
29 
30  __host__ __device__ constexpr TData& operator()(index_t i) { return At(i); }
31 
32  template <typename... Args>
33  __host__ constexpr auto Emplace(index_t i, Args&&... args)
34  -> std::enable_if_t<std::is_nothrow_constructible_v<TData, Args&&...>>
35  {
36  assert(i >= 0 && i < NSize);
37  mData[i].~TData();
38  new(mData + i) TData(ck::forward<Args>(args)...);
39  }
40 
41  template <typename T>
42  __host__ __device__ constexpr auto operator=(const T& a)
43  {
44  static_assert(T::Size() == Size(), "wrong! size not the same");
45 
46  static_for<0, Size(), 1>{}([&](auto i) { operator()(i) = a[i]; });
47 
48  return *this;
49  }
50  __host__ __device__ constexpr const TData* begin() const { return &mData[0]; }
51  __host__ __device__ constexpr const TData* end() const { return &mData[NSize]; }
52  __host__ __device__ constexpr TData* begin() { return &mData[0]; }
53  __host__ __device__ constexpr TData* end() { return &mData[NSize]; }
54 };
55 
56 // empty Array
57 template <typename TData>
58 struct Array<TData, 0>
59 {
60  using type = Array;
61  using data_type = TData;
62 
63  __host__ __device__ static constexpr index_t Size() { return 0; }
64 };
65 
66 template <typename X, typename... Xs>
67 __host__ __device__ constexpr auto make_array(X&& x, Xs&&... xs)
68 {
69  using data_type = remove_cvref_t<X>;
70  return Array<data_type, sizeof...(Xs) + 1>{ck::forward<X>(x), ck::forward<Xs>(xs)...};
71 }
72 
73 // make empty array
74 template <typename X>
75 __host__ __device__ constexpr auto make_array()
76 {
77  return Array<X, 0>{};
78 }
79 
80 } // namespace ck
81 #endif
Definition: ck.hpp:270
__host__ constexpr __device__ auto make_array(X &&x, Xs &&... xs)
Definition: array.hpp:67
remove_cv_t< remove_reference_t< T > > remove_cvref_t
Definition: type.hpp:297
int32_t index_t
Definition: ck.hpp:301
typename std::enable_if< B, T >::type enable_if_t
Definition: enable_if.hpp:27
const GenericPointer< typename T::ValueType > T2 T::AllocatorType & a
Definition: pointer.h:1517
TData data_type
Definition: array.hpp:61
__host__ static constexpr __device__ index_t Size()
Definition: array.hpp:63
Definition: array.hpp:16
__host__ constexpr __device__ TData * begin()
Definition: array.hpp:52
__host__ constexpr __device__ TData * end()
Definition: array.hpp:53
__host__ constexpr __device__ const TData & At(index_t i) const
Definition: array.hpp:24
__host__ constexpr __device__ TData & operator()(index_t i)
Definition: array.hpp:30
__host__ constexpr __device__ const TData * begin() const
Definition: array.hpp:50
TData mData[NSize]
Definition: array.hpp:20
__host__ constexpr __device__ TData & At(index_t i)
Definition: array.hpp:26
__host__ constexpr __device__ auto operator=(const T &a)
Definition: array.hpp:42
constexpr __host__ auto Emplace(index_t i, Args &&... args) -> std::enable_if_t< std::is_nothrow_constructible_v< TData, Args &&... >>
Definition: array.hpp:33
__host__ constexpr __device__ const TData * end() const
Definition: array.hpp:51
__host__ constexpr __device__ const TData & operator[](index_t i) const
Definition: array.hpp:28
TData data_type
Definition: array.hpp:18
__host__ static constexpr __device__ index_t Size()
Definition: array.hpp:22
Definition: functional2.hpp:33