TRIQS/nda 1.3.0
Multi-dimensional array library for C++
Loading...
Searching...
No Matches
address_space.hpp
Go to the documentation of this file.
1// Copyright (c) 2022--present, The Simons Foundation
2// This file is part of TRIQS/nda and is licensed under the Apache License, Version 2.0.
3// SPDX-License-Identifier: Apache-2.0
4// See LICENSE in the root of this distribution for details.
5
11
12#pragma once
13
14#include "../concepts.hpp"
15#include "../device.hpp"
16
17#include <algorithm>
18
19namespace nda {
20
22 // Forward declarations
23 template <char OP, ArrayOrScalar L, ArrayOrScalar R>
24 struct expr;
25 template <typename F, Array... As>
26 struct expr_call;
27 template <char OP, Array A>
28 struct expr_unary;
30
31} // namespace nda
32
33namespace nda::mem {
34
39
49 enum class AddressSpace { None, Host, Device, Unified }; // Do not change order!
50
52 using AddressSpace::Device;
53
55 using AddressSpace::Host;
56
58 using AddressSpace::None;
59
61 using AddressSpace::Unified;
62
68 template <typename T>
69 static constexpr AddressSpace get_addr_space = mem::None;
70
71 // Specialization of nda::mem::get_addr_space for const types.
72 template <typename T>
74
75 // Specialization of nda::mem::get_addr_space for reference types.
76 template <typename T>
78
92 template <AddressSpace A1, AddressSpace A2 = None, AddressSpace... As>
93 constexpr AddressSpace combine = []() {
94 static_assert(!(A1 == Host && A2 == Device) && !(A1 == Device && A2 == Host),
95 "Error in nda::mem::combine: Cannot combine Host and Device address spaces");
96 if constexpr (sizeof...(As) > 0) { return combine<std::max(A1, A2), As...>; }
97 return std::max(A1, A2);
98 }();
99
108 template <MemoryArray A1, MemoryArray... As>
110
112 template <MemoryArray A>
113 static constexpr AddressSpace get_addr_space<A> = A::storage_t::address_space;
114
116 template <Handle H>
117 static constexpr AddressSpace get_addr_space<H> = H::address_space;
118
120 template <char OP, ArrayOrScalar L, ArrayOrScalar R>
122
124 template <typename F, Array... As>
126
128 template <char OP, Array A>
130
139 template <AddressSpace... AdrSpcs>
140 static const auto check_adr_sp_valid = []() {
141 static_assert(((AdrSpcs != None) & ...), "Error in nda::mem::check_adr_sp_valid: Cannot use None address space");
142 static_assert(nda::have_device or ((AdrSpcs == Host) & ...),
143 "Error in nda::mem::check_adr_sp_valid: Device address space requires compiling with GPU support.");
144 };
145
147 template <typename... Ts>
148 requires(sizeof...(Ts) > 0)
149 static constexpr bool on_host = ((get_addr_space<Ts> == mem::Host) and ...);
150
152 template <typename... Ts>
153 requires(sizeof...(Ts) > 0)
154 static constexpr bool on_device = ((get_addr_space<Ts> == mem::Device) and ...);
155
157 template <typename... Ts>
158 requires(sizeof...(Ts) > 0)
159 static constexpr bool on_unified = ((get_addr_space<Ts> == mem::Unified) and ...);
160
162 template <typename A0, typename... A>
163 static constexpr bool have_same_addr_space = ((get_addr_space<A0> == get_addr_space<A>)and... and true);
164
166 template <typename... Ts>
167 static constexpr bool have_host_compatible_addr_space = ((on_host<Ts> or on_unified<Ts>)and...);
168
170 template <typename... Ts>
172
174 template <typename... Ts>
176
177 // Test various combinations of address spaces.
178 static_assert(combine<None, None> == None);
179 static_assert(combine<Host, Host> == Host);
180 static_assert(combine<None, Host> == Host);
181 static_assert(combine<Host, None> == Host);
182
183 static_assert(combine<Device, Device> == Device);
184 static_assert(combine<None, Device> == Device);
185 static_assert(combine<Device, None> == Device);
186
187 static_assert(combine<Device, Unified> == Unified);
188 static_assert(combine<Unified, Device> == Unified);
189 static_assert(combine<Host, Unified> == Unified);
190 static_assert(combine<Unified, Host> == Unified);
191
193
194} // namespace nda::mem
Check if a given type satisfies the array concept.
Definition concepts.hpp:230
Check if a given type satisfies the memory array concept.
Definition concepts.hpp:248
Provides concepts for the nda library.
Provides GPU and non-GPU specific functionality.
AddressSpace
Enum providing identifiers for the different memory address spaces.
static constexpr bool have_host_compatible_addr_space
Constexpr variable that is true if all given types have an address space compatible with Host.
static constexpr bool have_compatible_addr_space
Constexpr variable that is true if all given types have compatible address spaces.
constexpr AddressSpace combine
Promotion rules for nda::mem::AddressSpace values.
static constexpr bool have_device_compatible_addr_space
Constexpr variable that is true if all given types have an address space compatible with Device.
static constexpr bool on_device
Constexpr variable that is true if all given types have a Device address space.
static constexpr bool on_unified
Constexpr variable that is true if all given types have a Unified address space.
static constexpr AddressSpace get_addr_space
Variable template providing the address space for different types.
static constexpr bool on_host
Constexpr variable that is true if all given types have a Host address space.
constexpr AddressSpace common_addr_space
Get common address space for a number of given nda::MemoryArray types.
static const auto check_adr_sp_valid
Check validity of a set of nda::mem::AddressSpace values.
static constexpr bool have_same_addr_space
Constexpr variable that is true if all given types have the same address space.
static constexpr bool have_device
Constexpr variable that is true if the project is configured with GPU support.
Definition device.hpp:132
A lazy function call expression on arrays/views.
Definition map.hpp:79
Lazy unary expression for nda::Array types.
Lazy binary expression for nda::ArrayOrScalar types.