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;
29 template <int R, typename F>
30 class array_adapter;
32
33} // namespace nda
34
35namespace nda::mem {
36
41
51 enum class AddressSpace { None, Host, Device, Unified }; // Do not change order!
52
54 using AddressSpace::Device;
55
57 using AddressSpace::Host;
58
60 using AddressSpace::None;
61
63 using AddressSpace::Unified;
64
70 template <typename T>
71 static constexpr AddressSpace get_addr_space = mem::None;
72
73 // Specialization of nda::mem::get_addr_space for const types.
74 template <typename T>
76
77 // Specialization of nda::mem::get_addr_space for reference types.
78 template <typename T>
80
94 template <AddressSpace A1, AddressSpace A2 = None, AddressSpace... As>
95 constexpr AddressSpace combine = []() {
96 static_assert(!(A1 == Host && A2 == Device) && !(A1 == Device && A2 == Host),
97 "Error in nda::mem::combine: Cannot combine Host and Device address spaces");
98 if constexpr (sizeof...(As) > 0) { return combine<std::max(A1, A2), As...>; }
99 return std::max(A1, A2);
100 }();
101
110 template <Array A1, Array... As>
112
114 template <MemoryArray A>
115 static constexpr AddressSpace get_addr_space<A> = A::storage_t::address_space;
116
118 template <Handle H>
119 static constexpr AddressSpace get_addr_space<H> = H::address_space;
120
122 template <char OP, ArrayOrScalar L, ArrayOrScalar R>
124
126 template <typename F, Array... As>
128
130 template <char OP, Array A>
132
134 template <int R, typename F>
136
145 template <AddressSpace... AdrSpcs>
146 static const auto check_adr_sp_valid = []() {
147 static_assert(((AdrSpcs != None) & ...), "Error in nda::mem::check_adr_sp_valid: Cannot use None address space");
148 static_assert(nda::have_device or ((AdrSpcs == Host) & ...),
149 "Error in nda::mem::check_adr_sp_valid: Device address space requires compiling with GPU support.");
150 };
151
153 template <typename... Ts>
154 requires(sizeof...(Ts) > 0)
155 static constexpr bool on_host = ((get_addr_space<Ts> == mem::Host) and ...);
156
158 template <typename... Ts>
159 requires(sizeof...(Ts) > 0)
160 static constexpr bool on_device = ((get_addr_space<Ts> == mem::Device) and ...);
161
163 template <typename... Ts>
164 requires(sizeof...(Ts) > 0)
165 static constexpr bool on_unified = ((get_addr_space<Ts> == mem::Unified) and ...);
166
168 template <typename A0, typename... A>
169 static constexpr bool have_same_addr_space = ((get_addr_space<A0> == get_addr_space<A>) and ... and true);
170
172 template <typename... Ts>
173 static constexpr bool have_host_compatible_addr_space = ((on_host<Ts> or on_unified<Ts>) and ...);
174
176 template <typename... Ts>
177 static constexpr bool have_device_compatible_addr_space = ((on_device<Ts> or on_unified<Ts>) and ...);
178
180 template <typename... Ts>
182
183 // Test various combinations of address spaces.
184 static_assert(combine<None, None> == None);
185 static_assert(combine<Host, Host> == Host);
186 static_assert(combine<None, Host> == Host);
187 static_assert(combine<Host, None> == Host);
188
189 static_assert(combine<Device, Device> == Device);
190 static_assert(combine<None, Device> == Device);
191 static_assert(combine<Device, None> == Device);
192
193 static_assert(combine<Device, Unified> == Unified);
194 static_assert(combine<Unified, Device> == Unified);
195 static_assert(combine<Host, Unified> == Unified);
196 static_assert(combine<Unified, Host> == Unified);
197
199
200} // namespace nda::mem
@ Unified
Using declaration for the Unified address space (see nda::mem::AddressSpace).
@ None
Using declaration for the None address space (see nda::mem::AddressSpace).
@ Host
Using declaration for the Host address space (see nda::mem::AddressSpace).
@ Device
Using declaration for the Device address space (see nda::mem::AddressSpace).
Adapter that consists of a shape and a callable object, which takes R integers as arguments (just lik...
Check if a given type satisfies the array concept.
Definition concepts.hpp:205
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::Array 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.