TRIQS/nda 1.3.0
Multi-dimensional array library for C++
Loading...
Searching...
No Matches
device.hpp
Go to the documentation of this file.
1// Copyright (c) 2023 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: Nils Wentzell
16
17/**
18 * @file
19 * @brief Provides GPU and non-GPU specific functionality.
20 */
21
22#pragma once
23
24#ifdef NDA_HAVE_CUDA
25#include "./exceptions.hpp"
26
27#include <cuda_runtime.h>
28#include <cublas_v2.h>
29
30#include <complex>
31#include <exception>
32#include <string>
33#endif // NDA_HAVE_CUDA
34
35namespace nda {
36
37 /**
38 * @addtogroup mem_utils
39 * @{
40 */
41
42 /**
43 * @brief Trigger a compilation error in case GPU specific functionality is used without configuring the project with
44 * GPU support.
45 */
46 template <bool flag = false>
48 static_assert(flag, "Using device functionality without gpu support! Configure project with -DCudaSupport=ON.");
49 }
50
51#ifdef NDA_HAVE_CUDA
52
53 /// Constexpr variable that is true if the project is configured with GPU support.
54 static constexpr bool have_device = true;
55
56 /// Constexpr variable that is true if the project is configured with CUDA support.
57 static constexpr bool have_cuda = true;
58
59 /**
60 * @brief Check if a CUDA function call was successful and throw an exception if not.
61 *
62 * @param success Return value of a CUDA function call.
63 * @param message An optional message to include in the exception.
64 */
65 inline void device_error_check(cudaError_t success, std::string message = "") {
66 if (success != cudaSuccess) {
67 NDA_RUNTIME_ERROR << "Cuda runtime error: " << std::to_string(success) << "\n"
68 << " message: " << message << "\n"
69 << " cudaGetErrorName: " << std::string(cudaGetErrorName(success)) << "\n"
70 << " cudaGetErrorString: " << std::string(cudaGetErrorString(success)) << "\n";
71 }
72 }
73
74 /**
75 * @brief Map between a single char and the corresponding `cublasOperation_t`.
76 *
77 * @details The mapping is as follows:
78 * - 'N' -> `CUBLAS_OP_N` (non-transpose operation)
79 * - 'T' -> `CUBLAS_OP_T` (transpose operation)
80 * - 'C' -> `CUBLAS_OP_C` (conjugate transpose operation)
81 * - everything else -> call `std::terminate()`.
82 *
83 * @param op Character to be mapped to a `cublasOperation_t`.
84 * @return The corresponding `cublasOperation_t`.
85 */
86 inline cublasOperation_t get_cublas_op(char op) {
87 switch (op) {
88 case 'N': return CUBLAS_OP_N;
89 case 'T': return CUBLAS_OP_T;
90 case 'C': return CUBLAS_OP_C;
91 default: std::terminate(); return {};
92 }
93 }
94
95 /**
96 * @brief Cast a `std::complex<double>` to a `cuDoubleComplex`.
97 *
98 * @param c `std::complex<double>` object.
99 * @return Equivalent `cuDoubleComplex` object.
100 */
101 inline auto cucplx(std::complex<double> c) { return cuDoubleComplex{c.real(), c.imag()}; }
102
103 /**
104 * @brief Reinterpret a pointer to a `std::complex<double>` as a pointer to a `cuDoubleComplex`.
105 *
106 * @param c Pointer to a `std::complex<double>`.
107 * @return Pointer to a `cuDoubleComplex` at the same address.
108 */
109 inline auto *cucplx(std::complex<double> *c) { return reinterpret_cast<cuDoubleComplex *>(c); } // NOLINT
110
111 /**
112 * @brief Reinterpret a pointer to a `const std::complex<double>` as a pointer to a `const cuDoubleComplex`.
113 *
114 * @param c Pointer to a `const std::complex<double>`.
115 * @return Pointer to a `const cuDoubleComplex` at the same address.
116 */
117 inline auto *cucplx(std::complex<double> const *c) { return reinterpret_cast<const cuDoubleComplex *>(c); } // NOLINT
118
119 /**
120 * @brief Reinterpret a pointer to a pointer to a `std::complex<double>` as a pointer to a pointer to a
121 * `cuDoubleComplex`.
122 *
123 * @param c Pointer to a pointer to a `std::complex<double>`.
124 * @return Pointer to a pointer to a `cuDoubleComplex` at the same address.
125 */
126 inline auto **cucplx(std::complex<double> **c) { return reinterpret_cast<cuDoubleComplex **>(c); } // NOLINT
127
128 /**
129 * @brief Reinterpret a pointer to a pointer to a `const std::complex<double>` as a pointer to a pointer to a
130 * `const cuDoubleComplex`.
131 *
132 * @param c Pointer to a pointer to a `const std::complex<double>`.
133 * @return Pointer to a pointer to a `const cuDoubleComplex` at the same address.
134 */
135 inline auto **cucplx(std::complex<double> const **c) { return reinterpret_cast<const cuDoubleComplex **>(c); } // NOLINT
136
137#else
138
139/// Trigger a compilation error every time the nda::device_error_check function is called.
140#define device_error_check(ARG1, ARG2) compile_error_no_gpu()
141
142 /// Constexpr variable that is true if the project is configured with GPU support.
143 static constexpr bool have_device = false;
144
145 /// Constexpr variable that is true if the project is configured with CUDA support.
146 static constexpr bool have_cuda = false;
147
148#endif // NDA_HAVE_CUDA
149
150 /** @} */
151
152} // namespace nda
Runtime error class used throughout the nda library.
const char * what() const noexcept override
Override the virtual function what from std::exception to retrieve the accumulated error message.
runtime_error() noexcept
Default constructor.
~runtime_error() noexcept override=default
Default destructor.
runtime_error(runtime_error const &err) noexcept
Copy constructor to copy the contents of the error message accumulator.
#define CUBLAS_CHECK(X,...)
#define NDA_RUNTIME_ERROR
double abs2(std::complex< double > z)
Get the squared absolute value of a std::complex<double>.
bool isnan(std::complex< double > const &z)
Check if a std::complex<double> is NaN.
double abs2(double x)
Get the squared absolute value of a double.
auto pow(A &&a, double p)
Lazy, coefficient-wise power function for nda::Array types.
auto real(T t)
Get the real part of a scalar.
auto conj(T t)
Get the complex conjugate of a scalar.
T pow(T x, int n)
Calculate the integer power of an integer.
decltype(auto) conj(A &&a)
Lazy, coefficient-wise complex conjugate function for nda::Array types.
mapped< F > map(F f)
Create a lazy function call expression on arrays/views.
Definition map.hpp:199
constexpr bool is_regular_v
Constexpr variable that is true if type A is a regular array, i.e. an nda::basic_array.
Definition traits.hpp:145
constexpr char get_algebra
Constexpr variable that specifies the algebra of a type.
Definition traits.hpp:126
constexpr bool is_matrix_or_view_v
Constexpr variable that is true if type A is a regular matrix or a view of a matrix.
Definition traits.hpp:167
constexpr bool have_same_value_type_v
Constexpr variable that is true if all types in As have the same value type as A0.
Definition traits.hpp:196
constexpr int get_rank
Constexpr variable that specifies the rank of an nda::Array or of a contiguous 1-dimensional range.
Definition traits.hpp:136
constexpr bool have_same_rank_v
Constexpr variable that is true if all types in As have the same rank as A0.
Definition traits.hpp:200
constexpr bool is_view_v
Constexpr variable that is true if type A is a view, i.e. an nda::basic_array_view.
Definition traits.hpp:154
constexpr bool is_regular_or_view_v
Constexpr variable that is true if type A is either a regular array or a view.
Definition traits.hpp:163
constexpr char get_algebra< expr_call< F, As... > >
Get the resulting algebra of a function call expression involving arrays/views.
Definition map.hpp:64
decltype(auto) get_first_element(A const &a)
Get the first element of an array/view or simply return the scalar if a scalar is given.
Definition traits.hpp:177
constexpr bool layout_property_compatible(layout_prop_e from, layout_prop_e to)
Checks if two layout properties are compatible with each other.
Definition traits.hpp:237
constexpr bool ellipsis_is_present
Constexpr variable that is true if the parameter pack Args contains an nda::ellipsis.
Definition range.hpp:69
constexpr bool has_contiguous(layout_prop_e lp)
Checks if a layout property has the contiguous property.
Definition traits.hpp:282
constexpr bool has_layout_smallest_stride_is_one
Constexpr variable that is true if type A has the smallest_stride_is_one nda::layout_prop_e guarantee...
Definition traits.hpp:338
constexpr bool has_strided_1d(layout_prop_e lp)
Checks if a layout property has the strided_1d property.
Definition traits.hpp:266
constexpr bool has_layout_strided_1d
Constexpr variable that is true if type A has the strided_1d nda::layout_prop_e guarantee.
Definition traits.hpp:334
constexpr layout_prop_e operator&(layout_prop_e lhs, layout_prop_e rhs)
Bitwise AND operator for two layout properties.
Definition traits.hpp:258
constexpr layout_info_t operator&(layout_info_t lhs, layout_info_t rhs)
Bitwise AND operator for layout infos.
Definition traits.hpp:312
constexpr layout_prop_e operator|(layout_prop_e lhs, layout_prop_e rhs)
Bitwise OR operator for two layout properties.
Definition traits.hpp:249
constexpr layout_info_t get_layout_info
Constexpr variable that specifies the nda::layout_info_t of type A.
Definition traits.hpp:321
constexpr bool has_smallest_stride_is_one(layout_prop_e lp)
Checks if a layout property has the smallest_stride_is_one property.
Definition traits.hpp:274
constexpr bool has_contiguous_layout
Constexpr variable that is true if type A has the contiguous nda::layout_prop_e guarantee.
Definition traits.hpp:330
constexpr bool is_range_or_ellipsis
Constexpr variable that is true if the type T is either an nda::range, an nda::range::all_t or an nda...
Definition range.hpp:76
layout_prop_e
Compile-time guarantees of the memory layout of an array/view.
Definition traits.hpp:222
int get_ld(A const &a)
Get the leading dimension in LAPACK jargon of an nda::MemoryMatrix.
Definition tools.hpp:109
static constexpr bool has_C_layout
Constexpr variable that is true if the given nda::Array type has a C memory layout.
Definition tools.hpp:76
static constexpr bool is_conj_array_expr
Constexpr variable that is true if the given type is a conjugate lazy expression.
Definition tools.hpp:52
int get_ncols(A const &a)
Get the number of columns in LAPACK jargon of an nda::MemoryMatrix.
Definition tools.hpp:121
static constexpr bool is_conj_array_expr< expr_call< conj_f, A > >
Specialization of nda::blas::is_conj_array_expr for the conjugate lazy expressions.
Definition tools.hpp:56
static constexpr bool has_F_layout
Constexpr variable that is true if the given nda::Array type has a Fortran memory layout.
Definition tools.hpp:66
const char get_op
Variable template that determines the BLAS matrix operation tag ('N','T','C') based on the given bool...
Definition tools.hpp:91
AddressSpace
Enum providing identifiers for the different memory address spaces.
static constexpr bool have_cuda
Constexpr variable that is true if the project is configured with CUDA support.
Definition device.hpp:146
static constexpr bool have_device
Constexpr variable that is true if the project is configured with GPU support.
Definition device.hpp:143
void compile_error_no_gpu()
Trigger a compilation error in case GPU specific functionality is used without configuring the projec...
Definition device.hpp:47
constexpr bool is_instantiation_of_v
Constexpr variable that is true if type T is an instantiation of TMPLT (see nda::is_instantiation_of)...
Definition traits.hpp:59
constexpr bool is_complex_v
Constexpr variable that is true if type T is a std::complex type.
Definition traits.hpp:75
constexpr bool is_blas_lapack_v
Alias for nda::is_double_or_complex_v.
Definition traits.hpp:102
static constexpr bool always_false
Constexpr variable that is always false regardless of the types in Ts (used to trigger static_assert)...
Definition traits.hpp:71
constexpr bool is_scalar_for_v
Constexpr variable used to check requirements when initializing an nda::basic_array or nda::basic_arr...
Definition traits.hpp:93
static constexpr bool is_any_of
Constexpr variable that is true if type T is contained in the parameter pack Ts.
Definition traits.hpp:63
constexpr bool is_double_or_complex_v
Constexpr variable that is true if type T is a std::complex type or a double type.
Definition traits.hpp:98
static constexpr bool always_true
Constexpr variable that is always true regardless of the types in Ts.
Definition traits.hpp:67
constexpr bool is_scalar_v
Constexpr variable that is true if type S is a scalar type, i.e. arithmetic or complex.
Definition traits.hpp:79
constexpr bool is_scalar_or_convertible_v
Constexpr variable that is true if type S is a scalar type (see nda::is_scalar_v) or if a std::comple...
Definition traits.hpp:86
#define EXPECTS(X)
Definition macros.hpp:59
#define AS_STRING(...)
Definition macros.hpp:31
#define AS_STRING2(...)
Definition macros.hpp:32
A small wrapper around a single long integer to be used as a linear index.
Definition traits.hpp:343
long value
Linear index.
Definition traits.hpp:345
Wrapper for nda::conj.
Mimics Python's ... syntax.
Definition range.hpp:49
A lazy function call expression on arrays/views.
Definition map.hpp:90
long size() const
Get the total size of the nda::Array objects.
Definition map.hpp:161
auto shape() const
Get the shape of the nda::Array objects.
Definition map.hpp:155
auto operator()(Args const &...args) const
Function call operator.
Definition map.hpp:129
std::tuple< const As... > a
Tuple containing the nda::Array arguments.
Definition map.hpp:95
F f
Callable object of the expression.
Definition map.hpp:92
auto operator[](Arg const &arg) const
Subscript operator.
Definition map.hpp:146
Check if type T is of type TMPLT<...>.
Definition traits.hpp:51
Stores information about the memory layout and the stride order of an array/view.
Definition traits.hpp:295
uint64_t stride_order
Stride order of the array/view.
Definition traits.hpp:297
layout_prop_e prop
Memory layout properties of the array/view.
Definition traits.hpp:300
Functor that is returned by the nda::map function.
Definition map.hpp:169
expr_call< F, A0, As... > operator()(A0 &&a0, As &&...as) const
Function call operator that returns a lazy function call expression.
Definition map.hpp:183
F f
Callable object.
Definition map.hpp:171
Memory block consisting of a pointer and its size.