span.hpp Source File

span.hpp Source File#

Composable Kernel: span.hpp Source File
tile/core/container/span.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#pragma once
5
7#include <cstddef>
8#include <array>
9#include <type_traits>
10
11namespace ck_tile {
12
13// implement the c++20 std::span, lightweight, non-owning reference to a sequence
14// weather it is dynamic or static range. Or can be seen as a view of a contiguous sequence
15// TODO: do we need in device consider this is pointer?
16template <typename T>
17class span
18{
19 public:
20 using element_type = T;
21 using value_type = std::remove_cv_t<element_type>;
22 using size_type = std::size_t;
23 using difference_type = std::ptrdiff_t;
30
31 CK_TILE_HOST_DEVICE constexpr span() : span(nullptr, size_type{0}) {}
32
33 CK_TILE_HOST_DEVICE constexpr span(pointer first, size_type count) : ptr_(first), size_(count)
34 {
35 }
36
37 CK_TILE_HOST_DEVICE constexpr span(pointer first, pointer last) : span(first, last - first) {}
38
39 template <std::size_t N>
40 CK_TILE_HOST_DEVICE constexpr span(element_type (&arr)[N]) noexcept : span(arr, N)
41 {
42 }
43
44 template <std::size_t N>
45 CK_TILE_HOST_DEVICE constexpr span(std::array<value_type, N>& arr) noexcept
46 : span(arr.data(), N)
47 {
48 }
49
50 template <typename Container>
51 CK_TILE_HOST_DEVICE constexpr span(const Container& container)
52 : span(container.data(), container.size())
53 {
54 }
55
56 CK_TILE_HOST_DEVICE constexpr iterator begin() const noexcept { return ptr_; }
57 CK_TILE_HOST_DEVICE constexpr const_iterator cbegin() const noexcept { return begin(); }
58
59 CK_TILE_HOST_DEVICE constexpr iterator end() const noexcept { return begin() + size(); }
60 CK_TILE_HOST_DEVICE constexpr const_iterator cend() const noexcept { return end(); }
61
62 CK_TILE_HOST_DEVICE constexpr reference front() const { return *begin(); }
63 CK_TILE_HOST_DEVICE constexpr reference back() const { return *(--end()); }
64
66 {
67 return *(begin() + idx);
68 }
69 CK_TILE_HOST_DEVICE constexpr pointer data() const noexcept { return ptr_; }
70
71 CK_TILE_HOST_DEVICE constexpr size_type size() const noexcept { return size_; }
72
73 private:
74 pointer ptr_;
75 size_type size_;
76};
77
78} // namespace ck_tile
CK_TILE_HOST_DEVICE constexpr reference back() const
Definition tile/core/container/span.hpp:63
const element_type * const_pointer
Definition tile/core/container/span.hpp:25
CK_TILE_HOST_DEVICE constexpr reference operator[](size_type idx) const
Definition tile/core/container/span.hpp:65
std::ptrdiff_t difference_type
Definition tile/core/container/span.hpp:23
CK_TILE_HOST_DEVICE constexpr reference front() const
Definition tile/core/container/span.hpp:62
const element_type & const_reference
Definition tile/core/container/span.hpp:27
std::size_t size_type
Definition tile/core/container/span.hpp:22
CK_TILE_HOST_DEVICE constexpr const_iterator cend() const noexcept
Definition tile/core/container/span.hpp:60
CK_TILE_HOST_DEVICE constexpr size_type size() const noexcept
Definition tile/core/container/span.hpp:71
CK_TILE_HOST_DEVICE constexpr span()
Definition tile/core/container/span.hpp:31
std::remove_cv_t< element_type > value_type
Definition tile/core/container/span.hpp:21
CK_TILE_HOST_DEVICE constexpr span(pointer first, pointer last)
Definition tile/core/container/span.hpp:37
pointer iterator
Definition tile/core/container/span.hpp:28
CK_TILE_HOST_DEVICE constexpr const_iterator cbegin() const noexcept
Definition tile/core/container/span.hpp:57
element_type * pointer
Definition tile/core/container/span.hpp:24
CK_TILE_HOST_DEVICE constexpr iterator begin() const noexcept
Definition tile/core/container/span.hpp:56
element_type & reference
Definition tile/core/container/span.hpp:26
T element_type
Definition tile/core/container/span.hpp:20
CK_TILE_HOST_DEVICE constexpr span(element_type(&arr)[N]) noexcept
Definition tile/core/container/span.hpp:40
pointer const_iterator
Definition tile/core/container/span.hpp:29
CK_TILE_HOST_DEVICE constexpr iterator end() const noexcept
Definition tile/core/container/span.hpp:59
CK_TILE_HOST_DEVICE constexpr span(std::array< value_type, N > &arr) noexcept
Definition tile/core/container/span.hpp:45
CK_TILE_HOST_DEVICE constexpr pointer data() const noexcept
Definition tile/core/container/span.hpp:69
CK_TILE_HOST_DEVICE constexpr span(pointer first, size_type count)
Definition tile/core/container/span.hpp:33
CK_TILE_HOST_DEVICE constexpr span(const Container &container)
Definition tile/core/container/span.hpp:51
#define CK_TILE_HOST_DEVICE
Definition config.hpp:42
Definition tile/core/algorithm/cluster_descriptor.hpp:13
const GenericPointer< typename T::ValueType > & pointer
Definition pointer.h:1514