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
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
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
54 static constexpr bool have_device = true;
55
57 static constexpr bool have_cuda = true;
58
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
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
101 inline auto cucplx(std::complex<double> c) { return cuDoubleComplex{c.real(), c.imag()}; }
102
109 inline auto *cucplx(std::complex<double> *c) { return reinterpret_cast<cuDoubleComplex *>(c); } // NOLINT
110
117 inline auto *cucplx(std::complex<double> const *c) { return reinterpret_cast<const cuDoubleComplex *>(c); } // NOLINT
118
126 inline auto **cucplx(std::complex<double> **c) { return reinterpret_cast<cuDoubleComplex **>(c); } // NOLINT
127
135 inline auto **cucplx(std::complex<double> const **c) { return reinterpret_cast<const cuDoubleComplex **>(c); } // NOLINT
136
137#else
138
140#define device_error_check(ARG1, ARG2) compile_error_no_gpu()
141
143 static constexpr bool have_device = false;
144
146 static constexpr bool have_cuda = false;
147
148#endif // NDA_HAVE_CUDA
149
152} // 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:146
#define device_error_check(ARG1, ARG2)
Trigger a compilation error every time the nda::device_error_check function is called.
Definition device.hpp:140
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
std::string to_string(std::array< T, R > const &a)
Get a string representation of a std::array.
Definition array.hpp:65