ABACUS develop
Atomic-orbital Based Ab-initio Computation at UStc
Loading...
Searching...
No Matches
cuda.h
Go to the documentation of this file.
1#ifndef BASE_MACROS_CUDA_H_
2#define BASE_MACROS_CUDA_H_
3
4#include <cublas_v2.h>
5#include <cuda_runtime.h>
6#include <cusolverDn.h>
7#include <thrust/complex.h>
8#include <stdexcept>
9#include <string>
10
12
13#define THREADS_PER_BLOCK 256
14
15template <typename T>
17{
18 using type = T;
19};
20
21template <>
22struct GetTypeThrust<std::complex<float>>
23{
24 using type = thrust::complex<float>;
25};
26
27template <>
28struct GetTypeThrust<std::complex<double>>
29{
30 using type = thrust::complex<double>;
31};
32
34static inline cublasOperation_t GetCublasOperation(const char& trans, bool is_complex)
35{
36 switch (trans)
37 {
38 case 'N': case 'n': return CUBLAS_OP_N;
39 case 'T': case 't': return CUBLAS_OP_T;
40 case 'C': case 'c': return is_complex ? CUBLAS_OP_C : CUBLAS_OP_T;
41 default: throw std::invalid_argument(std::string("Invalid BLAS transpose flag: ") + trans);
42 }
43}
44
45static inline cublasOperation_t GetCublasOperation(const char& trans)
46{
47 return GetCublasOperation(trans, true);
48}
49
50template <typename T>
52{
53 static constexpr cudaDataType cuda_data_type = cudaDataType::CUDA_R_32F;
54};
55// Specializations of DataTypeToEnum for supported types.
56template <>
57struct GetTypeCuda<int>
58{
59 static constexpr cudaDataType cuda_data_type = cudaDataType::CUDA_R_32I;
60};
61template <>
62struct GetTypeCuda<float>
63{
64 static constexpr cudaDataType cuda_data_type = cudaDataType::CUDA_R_32F;
65};
66template <>
67struct GetTypeCuda<double>
68{
69 static constexpr cudaDataType cuda_data_type = cudaDataType::CUDA_R_64F;
70};
71#if CUDA_VERSION >= 11000
72template <>
73struct GetTypeCuda<int64_t>
74{
75 static constexpr cudaDataType cuda_data_type = cudaDataType::CUDA_R_64I;
76};
77#endif
78template <>
79struct GetTypeCuda<std::complex<float>>
80{
81 static constexpr cudaDataType cuda_data_type = cudaDataType::CUDA_C_32F;
82};
83template <>
84struct GetTypeCuda<std::complex<double>>
85{
86 static constexpr cudaDataType cuda_data_type = cudaDataType::CUDA_C_64F;
87};
88
89static inline cublasFillMode_t cublas_fill_mode(const char& uplo)
90{
91 if (uplo == 'U' || uplo == 'u')
92 return CUBLAS_FILL_MODE_UPPER;
93 else if (uplo == 'L' || uplo == 'l')
94 return CUBLAS_FILL_MODE_LOWER;
95 else
96 throw std::runtime_error(std::string("Invalid BLAS triangle: ") + uplo);
97}
98
99static inline cublasDiagType_t cublas_diag_type(const char& diag)
100{
101 if (diag == 'U' || diag == 'u')
102 return CUBLAS_DIAG_UNIT;
103 else if (diag == 'N' || diag == 'n')
104 return CUBLAS_DIAG_NON_UNIT;
105 else
106 throw std::runtime_error("cublas_diag_type: unknown diag");
107}
108
109static inline cusolverEigMode_t cublas_eig_mode(const char& jobz)
110{
111 if (jobz == 'N' || jobz == 'n')
112 return CUSOLVER_EIG_MODE_NOVECTOR;
113 else if (jobz == 'V' || jobz == 'v')
114 return CUSOLVER_EIG_MODE_VECTOR;
115 else
116 throw std::runtime_error("cublas_eig_mode: unknown diag");
117}
118
119static inline cusolverEigType_t cublas_eig_type(const int& itype)
120{
121 if (itype == 1)
122 return CUSOLVER_EIG_TYPE_1;
123 else if (itype == 2)
124 return CUSOLVER_EIG_TYPE_2;
125 else
126 throw std::runtime_error("cublas_eig_mode: unknown diag");
127}
128
140static inline cusolverEigRange_t cublas_eig_range(const char& range)
141{
142 if (range == 'A' || range == 'a')
143 return CUSOLVER_EIG_RANGE_ALL;
144 else if (range == 'V' || range == 'v')
145 return CUSOLVER_EIG_RANGE_V;
146 else if (range == 'I' || range == 'i')
147 return CUSOLVER_EIG_RANGE_I;
148 else
149 throw std::runtime_error("cublas_eig_range: unknown range '" + std::string(1, range) + "'");
150}
151
152#endif // BASE_MACROS_CUDA_H_
#define complex
Definition elpa_new.h:28
#define T
Definition exp.cpp:237
static constexpr cudaDataType cuda_data_type
Definition cuda.h:69
static constexpr cudaDataType cuda_data_type
Definition cuda.h:64
static constexpr cudaDataType cuda_data_type
Definition cuda.h:59
static constexpr cudaDataType cuda_data_type
Definition cuda.h:86
static constexpr cudaDataType cuda_data_type
Definition cuda.h:81
Definition cuda.h:52
static constexpr cudaDataType cuda_data_type
Definition cuda.h:53
thrust::complex< double > type
Definition cuda.h:30
thrust::complex< float > type
Definition cuda.h:24
Definition cuda.h:17
T type
Definition cuda.h:18