map.hpp Source File

map.hpp Source File#

Composable Kernel: map.hpp Source File
map.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
2// Copyright (c) 2018-2024, Advanced Micro Devices, Inc. All rights reserved.
3
4#pragma once
5
10
11namespace ck_tile {
12
13// naive map
14template <typename key, typename data, index_t max_size = 128>
15struct map
16{
19
22
23 struct iterator
24 {
27
29 : impl_{impl}, pos_{pos}
30 {
31 }
32
34 {
35 pos_++;
36 return *this;
37 }
38
39 CK_TILE_HOST_DEVICE constexpr bool operator!=(const iterator& other) const
40 {
41 return other.pos_ != pos_;
42 }
43
44 CK_TILE_HOST_DEVICE constexpr pair_type& operator*() { return impl_.at(pos_); }
45 };
46
48 {
51
53 : impl_{impl}, pos_{pos}
54 {
55 }
56
58 {
59 pos_++;
60
61 return *this;
62 }
63
64 CK_TILE_HOST_DEVICE constexpr bool operator!=(const const_iterator& other) const
65 {
66 return other.pos_ != pos_;
67 }
68
69 CK_TILE_HOST_DEVICE constexpr const pair_type& operator*() const { return impl_.at(pos_); }
70 };
71
72 CK_TILE_HOST_DEVICE constexpr map() : impl_{}, size_{0} {}
73
74 CK_TILE_HOST_DEVICE constexpr index_t size() const { return size_; }
75
77
78 CK_TILE_HOST_DEVICE constexpr index_t find_position(const key& k) const
79 {
80 for(index_t i = 0; i < size(); i++)
81 {
82 if(impl_[i].template at<0>() == k)
83 {
84 return i;
85 }
86 }
87
88 return size_;
89 }
90
91 CK_TILE_HOST_DEVICE constexpr const_iterator find(const key& k) const
92 {
94 }
95
96 CK_TILE_HOST_DEVICE constexpr iterator find(const key& k)
97 {
98 return iterator{impl_, find_position(k)};
99 }
100
101 CK_TILE_HOST_DEVICE constexpr const data& operator[](const key& k) const
102 {
103 const auto it = find(k);
104
105 // FIXME
106 // assert(it.pos_ < size());
107
108 return impl_[it.pos_].template at<1>();
109 }
110
111 CK_TILE_HOST_DEVICE constexpr data& operator()(const key& k)
112 {
113 auto it = find(k);
114
115 // if entry not found
116 if(it.pos_ == size())
117 {
118 impl_(it.pos_).template at<0>() = k;
119 size_++;
120 }
121
122 // FIXME
123 // assert(size_ <= max_size);
124
125 return impl_(it.pos_).template at<1>();
126 }
127
128 // WARNING: needed by compiler for C++ range-based for loop only, don't use this function!
130
131 // WARNING: needed by compiler for C++ range-based for loop only, don't use this function!
133 {
134 return const_iterator{impl_, size_};
135 }
136
137 // WARNING: needed by compiler for C++ range-based for loop only, don't use this function!
138 CK_TILE_HOST_DEVICE constexpr iterator begin() { return iterator{impl_, 0}; }
139
140 // WARNING: needed by compiler for C++ range-based for loop only, don't use this function!
142};
143
144template <typename key, typename data, index_t max_size>
145CK_TILE_HOST_DEVICE static void print(const map<key, data, max_size>& m)
146{
147 printf("map{size_: %d, impl_: [", m.size_);
148 for(const auto& [k, d] : m)
149 {
150 printf("{key: ");
151 print(k);
152 printf(", data: ");
153 print(d);
154 printf("}, ");
155 }
156 printf("]}");
157}
158
159} // namespace ck_tile
#define CK_TILE_HOST_DEVICE
Definition config.hpp:42
Definition tile/core/arch/amd_buffer_addressing.hpp:110
Definition tile/core/algorithm/cluster_descriptor.hpp:13
int32_t index_t
Definition integer.hpp:9
A fixed-size array container similar to std::array with additional utilities.
Definition tile/core/container/array.hpp:43
Definition map.hpp:48
const impl_type & impl_
Definition map.hpp:49
CK_TILE_HOST_DEVICE constexpr const pair_type & operator*() const
Definition map.hpp:69
CK_TILE_HOST_DEVICE constexpr bool operator!=(const const_iterator &other) const
Definition map.hpp:64
CK_TILE_HOST_DEVICE constexpr const_iterator & operator++()
Definition map.hpp:57
CK_TILE_HOST_DEVICE constexpr const_iterator(const impl_type &impl, index_t pos)
Definition map.hpp:52
index_t pos_
Definition map.hpp:50
Definition map.hpp:24
impl_type & impl_
Definition map.hpp:25
CK_TILE_HOST_DEVICE constexpr pair_type & operator*()
Definition map.hpp:44
CK_TILE_HOST_DEVICE constexpr iterator & operator++()
Definition map.hpp:33
CK_TILE_HOST_DEVICE constexpr iterator(impl_type &impl, index_t pos)
Definition map.hpp:28
index_t pos_
Definition map.hpp:26
CK_TILE_HOST_DEVICE constexpr bool operator!=(const iterator &other) const
Definition map.hpp:39
CK_TILE_HOST_DEVICE constexpr index_t size() const
Definition map.hpp:74
CK_TILE_HOST_DEVICE constexpr const_iterator begin() const
Definition map.hpp:129
CK_TILE_HOST_DEVICE constexpr iterator begin()
Definition map.hpp:138
array< pair_type, max_size > impl_type
Definition map.hpp:18
impl_type impl_
Definition map.hpp:20
CK_TILE_HOST_DEVICE constexpr index_t find_position(const key &k) const
Definition map.hpp:78
CK_TILE_HOST_DEVICE constexpr const data & operator[](const key &k) const
Definition map.hpp:101
tuple< key, data > pair_type
Definition map.hpp:17
CK_TILE_HOST_DEVICE constexpr const_iterator end() const
Definition map.hpp:132
CK_TILE_HOST_DEVICE constexpr iterator end()
Definition map.hpp:141
CK_TILE_HOST_DEVICE constexpr iterator find(const key &k)
Definition map.hpp:96
CK_TILE_HOST_DEVICE void clear()
Definition map.hpp:76
CK_TILE_HOST_DEVICE constexpr map()
Definition map.hpp:72
CK_TILE_HOST_DEVICE constexpr const_iterator find(const key &k) const
Definition map.hpp:91
CK_TILE_HOST_DEVICE constexpr data & operator()(const key &k)
Definition map.hpp:111
index_t size_
Definition map.hpp:21
Definition tile/core/container/tuple.hpp:192