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-2024 Simons Foundation
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0.txt
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14//
15// Authors: Thomas Hahn, Miguel Morales, Nils Wentzell
16
23#pragma once
24
25#include "../concepts.hpp"
26#include "../device.hpp"
27
28#include <algorithm>
29
30namespace nda {
31
33 // Forward declarations
34 template <char OP, ArrayOrScalar L, ArrayOrScalar R>
35 struct expr;
36 template <typename F, Array... As>
37 struct expr_call;
38 template <char OP, Array A>
39 struct expr_unary;
41
42} // namespace nda
43
44namespace nda::mem {
45
60 enum class AddressSpace { None, Host, Device, Unified }; // Do not change order!
61
63 using AddressSpace::Device;
64
66 using AddressSpace::Host;
67
69 using AddressSpace::None;
70
72 using AddressSpace::Unified;
73
79 template <typename T>
80 static constexpr AddressSpace get_addr_space = mem::None;
81
82 // Specialization of nda::mem::get_addr_space for const types.
83 template <typename T>
85
86 // Specialization of nda::mem::get_addr_space for reference types.
87 template <typename T>
88 static constexpr AddressSpace get_addr_space<T &> = get_addr_space<T>;
89
103 template <AddressSpace A1, AddressSpace A2 = None, AddressSpace... As>
104 constexpr AddressSpace combine = []() {
105 static_assert(!(A1 == Host && A2 == Device) && !(A1 == Device && A2 == Host),
106 "Error in nda::mem::combine: Cannot combine Host and Device address spaces");
107 if constexpr (sizeof...(As) > 0) { return combine<std::max(A1, A2), As...>; }
108 return std::max(A1, A2);
109 }();
110
119 template <MemoryArray A1, MemoryArray... As>
121
123 template <MemoryArray A>
124 static constexpr AddressSpace get_addr_space<A> = A::storage_t::address_space;
125
127 template <Handle H>
128 static constexpr AddressSpace get_addr_space<H> = H::address_space;
129
131 template <char OP, ArrayOrScalar L, ArrayOrScalar R>
133
135 template <typename F, Array... As>
137
139 template <char OP, Array A>
141
150 template <AddressSpace... AdrSpcs>
151 static const auto check_adr_sp_valid = []() {
152 static_assert(((AdrSpcs != None) & ...), "Error in nda::mem::check_adr_sp_valid: Cannot use None address space");
153 static_assert(nda::have_device or ((AdrSpcs == Host) & ...),
154 "Error in nda::mem::check_adr_sp_valid: Device address space requires compiling with GPU support.");
155 };
156
158 template <typename... Ts>
159 requires(sizeof...(Ts) > 0)
160 static constexpr bool on_host = ((get_addr_space<Ts> == mem::Host) and ...);
161
163 template <typename... Ts>
164 requires(sizeof...(Ts) > 0)
165 static constexpr bool on_device = ((get_addr_space<Ts> == mem::Device) and ...);
166
168 template <typename... Ts>
169 requires(sizeof...(Ts) > 0)
170 static constexpr bool on_unified = ((get_addr_space<Ts> == mem::Unified) and ...);
171
173 template <typename A0, typename... A>
174 static constexpr bool have_same_addr_space = ((get_addr_space<A0> == get_addr_space<A>)and... and true);
175
177 template <typename... Ts>
178 static constexpr bool have_host_compatible_addr_space = ((on_host<Ts> or on_unified<Ts>)and...);
179
181 template <typename... Ts>
183
185 template <typename... Ts>
187
188 // Test various combinations of address spaces.
189 static_assert(combine<None, None> == None);
190 static_assert(combine<Host, Host> == Host);
191 static_assert(combine<None, Host> == Host);
192 static_assert(combine<Host, None> == Host);
193
194 static_assert(combine<Device, Device> == Device);
195 static_assert(combine<None, Device> == Device);
196 static_assert(combine<Device, None> == Device);
197
198 static_assert(combine<Device, Unified> == Unified);
199 static_assert(combine<Unified, Device> == Unified);
200 static_assert(combine<Host, Unified> == Unified);
201 static_assert(combine<Unified, Host> == Unified);
202
205} // 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 AddressSpace get_addr_space< H >
Specialization of nda::mem::get_addr_space for nda::Handle types.
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 AddressSpace get_addr_space< A >
Specialization of nda::mem::get_addr_space for nda::Memory Array 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:143
A lazy function call expression on arrays/views.
Definition map.hpp:90