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--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
10
11#pragma once
12
13#ifdef NDA_HAVE_CUDA
14#include "./exceptions.hpp"
15
16#include <cuda_runtime.h>
17#include <cublas_v2.h>
18
19#include <complex>
20#include <exception>
21#include <string>
22#endif // NDA_HAVE_CUDA
23
24namespace nda {
25
30
35 template <bool flag = false>
37 static_assert(flag, "Using device functionality without gpu support! Configure project with -DCudaSupport=ON.");
38 }
39
40#ifdef NDA_HAVE_CUDA
41
43 static constexpr bool have_device = true;
44
46 static constexpr bool have_cuda = true;
47
54 inline void device_error_check(cudaError_t success, std::string message = "") {
55 if (success != cudaSuccess) {
56 NDA_RUNTIME_ERROR << "Cuda runtime error: " << std::to_string(success) << "\n"
57 << " message: " << message << "\n"
58 << " cudaGetErrorName: " << std::string(cudaGetErrorName(success)) << "\n"
59 << " cudaGetErrorString: " << std::string(cudaGetErrorString(success)) << "\n";
60 }
61 }
62
75 inline cublasOperation_t get_cublas_op(char op) {
76 switch (op) {
77 case 'N': return CUBLAS_OP_N;
78 case 'T': return CUBLAS_OP_T;
79 case 'C': return CUBLAS_OP_C;
80 default: std::terminate(); return {};
81 }
82 }
83
90 inline auto cucplx(std::complex<double> c) { return cuDoubleComplex{c.real(), c.imag()}; }
91
98 inline auto *cucplx(std::complex<double> *c) { return reinterpret_cast<cuDoubleComplex *>(c); } // NOLINT
99
106 inline auto *cucplx(std::complex<double> const *c) { return reinterpret_cast<const cuDoubleComplex *>(c); } // NOLINT
107
115 inline auto **cucplx(std::complex<double> **c) { return reinterpret_cast<cuDoubleComplex **>(c); } // NOLINT
116
124 inline auto **cucplx(std::complex<double> const **c) { return reinterpret_cast<const cuDoubleComplex **>(c); } // NOLINT
125
126#else
127
129#define device_error_check(ARG1, ARG2) compile_error_no_gpu()
130
132 static constexpr bool have_device = false;
133
135 static constexpr bool have_cuda = false;
136
137#endif // NDA_HAVE_CUDA
138
140
141} // namespace nda
Provides a custom runtime error class and macros to assert conditions and throw exceptions.
static constexpr bool have_cuda
Constexpr variable that is true if the project is configured with CUDA support.
Definition device.hpp:135
#define device_error_check(ARG1, ARG2)
Trigger a compilation error every time the nda::device_error_check function is called.
Definition device.hpp:129
static constexpr bool have_device
Constexpr variable that is true if the project is configured with GPU support.
Definition device.hpp:132
void compile_error_no_gpu()
Trigger a compilation error in case GPU specific functionality is used without configuring the projec...
Definition device.hpp:36
std::string to_string(std::array< T, R > const &a)
Get a string representation of a std::array.
Definition array.hpp:52