memory_type.hpp
1 
5 #pragma once
6 
7 #include <algorithm>
8 #include <array>
9 #include <iostream>
10 #include <ranges>
11 #include <span>
12 
13 namespace rapidsmpf {
14 
16 enum class MemoryType : int {
17  DEVICE = 0,
18  PINNED_HOST = 1,
19  HOST = 2
20 };
21 
23 constexpr std::array<MemoryType, 3> MEMORY_TYPES{
25 };
26 
28 constexpr std::array<char const*, MEMORY_TYPES.size()> MEMORY_TYPE_NAMES{
29  {"DEVICE", "PINNED_HOST", "HOST"}
30 };
31 
40 constexpr std::array<MemoryType, 2> SPILL_TARGET_MEMORY_TYPES{
42 };
43 
54 constexpr std::span<MemoryType const> leq_memory_types(MemoryType mem_type) noexcept {
55  return std::views::drop_while(MEMORY_TYPES, [&](MemoryType const& mt) {
56  return mt != mem_type;
57  });
58 }
59 
60 static_assert(std::ranges::equal(leq_memory_types(MemoryType::DEVICE), MEMORY_TYPES));
61 static_assert(std::ranges::equal(
62  leq_memory_types(MemoryType::HOST), std::ranges::single_view{MemoryType::HOST}
63 ));
64 // unknown memory type should return an empty view
65 static_assert(std::ranges::equal(
66  leq_memory_types(static_cast<MemoryType>(-1)), std::ranges::empty_view<MemoryType>{}
67 ));
68 
75 constexpr char const* to_string(MemoryType mem_type) {
76  return MEMORY_TYPE_NAMES[static_cast<std::size_t>(mem_type)];
77 }
78 
86 std::ostream& operator<<(std::ostream& os, MemoryType mem_type);
87 
102 std::istream& operator>>(std::istream& is, MemoryType& out);
103 
104 } // namespace rapidsmpf
RAPIDS Multi-Processor interfaces.
Definition: backend.hpp:13
constexpr std::array< char const *, MEMORY_TYPES.size()> MEMORY_TYPE_NAMES
Memory type names sorted to match MemoryType and MEMORY_TYPES.
Definition: memory_type.hpp:28
std::istream & operator>>(std::istream &is, MemoryType &out)
Overload to read a MemoryType value from an input stream.
constexpr std::span< MemoryType const > leq_memory_types(MemoryType mem_type) noexcept
Get the memory types with preference lower than or equal to mem_type.
Definition: memory_type.hpp:54
constexpr std::array< MemoryType, 3 > MEMORY_TYPES
All memory types sorted in decreasing order of preference.
Definition: memory_type.hpp:23
constexpr std::array< MemoryType, 2 > SPILL_TARGET_MEMORY_TYPES
Memory types that are valid spill destinations in decreasing order of preference.
Definition: memory_type.hpp:40
MemoryType
Enum representing the type of memory sorted in decreasing order of preference.
Definition: memory_type.hpp:16
@ PINNED_HOST
Pinned host memory.
@ HOST
Host memory.
@ DEVICE
Device memory.
std::ostream & operator<<(std::ostream &os, Communicator const &obj)
Overloads the stream insertion operator for the Communicator class.
constexpr char const * to_string(MemoryType mem_type)
Get the name of a MemoryType.
Definition: memory_type.hpp:75