Distributed Ranges
Loading...
Searching...
No Matches
device_span.hpp
1// SPDX-FileCopyrightText: Intel Corporation
2//
3// SPDX-License-Identifier: BSD-3-Clause
4
5#pragma once
6
7#include <dr/concepts/concepts.hpp>
8#include <dr/sp/span.hpp>
9#include <span>
10
11namespace dr::sp {
12
13// A `device_span` is simply a normal `std::span` that's
14// been decorated with an extra `rank()` function, showing
15// which rank its memory is located on.
16// (Thus fulfilling the `remote_range` concept.)
17/*
18template <class T,
19 std::size_t Extent = std::dynamic_extent>
20class device_span : public std::span<T, Extent> {
21public:
22 constexpr device_span() noexcept {}
23
24 template< class It >
25 explicit(Extent != std::dynamic_extent)
26 constexpr device_span(It first, std::size_t count, std::size_t rank)
27 : rank_(rank), std::span<T, Extent>(first, count) {}
28
29 template< class It, class End >
30 explicit(Extent != std::dynamic_extent)
31 constexpr device_span(It first, End last, std::size_t rank)
32 : rank_(rank), std::span<T, Extent>(first, last) {}
33
34 constexpr std::size_t rank() const noexcept {
35 return rank_;
36 }
37
38private:
39 std::size_t rank_;
40};
41*/
42
43template <typename T, typename Iter = T *>
44class device_span : public dr::sp::span<T, Iter> {
45public:
46 constexpr device_span() noexcept {}
47
48 using value_type = T;
49 using size_type = std::size_t;
50 using difference_type = std::size_t;
51 using reference = std::iter_reference_t<Iter>;
52
53 template <rng::random_access_range R>
54 requires(dr::remote_range<R>)
55 device_span(R &&r)
56 : dr::sp::span<T, Iter>(rng::begin(r), rng::size(r)),
57 rank_(dr::ranges::rank(r)) {}
58
59 template <rng::random_access_range R>
60 device_span(R &&r, std::size_t rank)
61 : dr::sp::span<T, Iter>(rng::begin(r), rng::size(r)), rank_(rank) {}
62
63 template <class It>
64 constexpr device_span(It first, std::size_t count, std::size_t rank)
65 : dr::sp::span<T, Iter>(first, count), rank_(rank) {}
66
67 template <class It, class End>
68 constexpr device_span(It first, End last, std::size_t rank)
69 : dr::sp::span<T, Iter>(first, last), rank_(rank) {}
70
71 constexpr std::size_t rank() const noexcept { return rank_; }
72
73 device_span first(std::size_t n) const {
74 return device_span(this->begin(), this->begin() + n, rank_);
75 }
76
77 device_span last(std::size_t n) const {
78 return device_span(this->end() - n, this->end(), rank_);
79 }
80
81 device_span subspan(std::size_t offset, std::size_t count) const {
82 return device_span(this->begin() + offset, this->begin() + offset + count,
83 rank_);
84 }
85
86private:
87 std::size_t rank_;
88};
89
90template <rng::random_access_range R>
91device_span(R &&) -> device_span<rng::range_value_t<R>, rng::iterator_t<R>>;
92
93template <rng::random_access_range R>
94device_span(R &&, std::size_t)
95 -> device_span<rng::range_value_t<R>, rng::iterator_t<R>>;
96
97} // namespace dr::sp
Definition: device_span.hpp:44
Definition: span.hpp:14
Definition: concepts.hpp:16