ABACUS develop
Atomic-orbital Based Ab-initio Computation at UStc
Loading...
Searching...
No Matches
hipsolver.h
Go to the documentation of this file.
1#ifndef BASE_THIRD_PARTY_HIPSOLVER_H_
2#define BASE_THIRD_PARTY_HIPSOLVER_H_
3
4#include <base/macros/rocm.h>
5
6#include <hip/hip_runtime.h>
7#include <hipblas/hipblas.h>
8#include <hipsolver/hipsolver.h>
9
10namespace container {
11namespace hipSolverConnector {
12
13template <typename T>
14static inline
15void trtri (hipsolverHandle_t& hipsolver_handle, const char& uplo, const char& diag, const int& n, T* A, const int& lda)
16{
17 size_t d_lwork = 0, h_lwork = 0;
18 hipsolverErrcheck(hipsolverDnXtrtri_bufferSize(hipsolver_handle, hipsolver_fill_mode(uplo), hipblas_diag_type(diag), n, GetTypeRocm<T>::cuda_data_type, A, lda, &d_lwork, &h_lwork));
19 void* d_work = nullptr, *h_work = nullptr;
20 hipErrcheck(hipMalloc((void**)&d_work, d_lwork));
21 if (h_lwork) {
22 h_work = malloc(h_lwork);
23 if (h_work == nullptr) {
24 throw std::bad_alloc();
25 }
26 }
27 int h_info = 0;
28 int* d_info = nullptr;
29 hipErrcheck(hipMalloc((void**)&d_info, sizeof(int)));
30 // Perform Cholesky decomposition
31 hipsolverErrcheck(hipsolverDnXtrtri(hipsolver_handle, hipsolver_fill_mode(uplo), hipblas_diag_type(diag), n, GetTypeRocm<T>::cuda_data_type, A, n, d_work, d_lwork, h_work, h_lwork, d_info));
32 hipErrcheck(hipMemcpy(&h_info, d_info, sizeof(int), hipMemcpyDeviceToHost));
33 if (h_info != 0) {
34 throw std::runtime_error("trtri: failed to invert matrix");
35 }
36 free(h_work);
37 hipErrcheck(hipFree(d_work));
38 hipErrcheck(hipFree(d_info));
39}
40
41static inline
42void potri (hipsolverHandle_t& hipsolver_handle, const char& uplo, const char& diag, const int& n, float * A, const int& lda)
43{
44 int lwork;
45 hipsolverErrcheck(hipsolverDnSpotri_bufferSize(hipsolver_handle, hipsolver_fill_mode(uplo), n, A, n, &lwork));
46 float* work;
47 hipErrcheck(hipMalloc((void**)&work, lwork * sizeof(float)));
48 // Perform Cholesky decomposition
49 hipsolverErrcheck(hipsolverDnSpotri(hipsolver_handle, hipsolver_fill_mode(uplo), n, A, n, work, lwork, nullptr));
50 hipErrcheck(hipFree(work));
51}
52static inline
53void potri (hipsolverHandle_t& hipsolver_handle, const char& uplo, const char& diag, const int& n, double * A, const int& lda)
54{
55 int lwork;
56 hipsolverErrcheck(hipsolverDnDpotri_bufferSize(hipsolver_handle, hipsolver_fill_mode(uplo), n, A, n, &lwork));
57 double* work;
58 hipErrcheck(hipMalloc((void**)&work, lwork * sizeof(double)));
59 // Perform Cholesky decomposition
60 hipsolverErrcheck(hipsolverDnDpotri(hipsolver_handle, hipsolver_fill_mode(uplo), n, A, n, work, lwork, nullptr));
61 hipErrcheck(hipFree(work));
62}
63static inline
64void potri (hipsolverHandle_t& hipsolver_handle, const char& uplo, const char& diag, const int& n, std::complex<float> * A, const int& lda)
65{
66 int lwork;
67 hipsolverErrcheck(hipsolverDnCpotri_bufferSize(hipsolver_handle, hipsolver_fill_mode(uplo), n, reinterpret_cast<hipFloatComplex *>(A), n, &lwork));
68 hipFloatComplex* work;
69 hipErrcheck(hipMalloc((void**)&work, lwork * sizeof(hipFloatComplex)));
70 // Perform Cholesky decomposition
71 hipsolverErrcheck(hipsolverDnCpotri(hipsolver_handle, hipsolver_fill_mode(uplo), n, reinterpret_cast<hipFloatComplex *>(A), n, work, lwork, nullptr));
72 hipErrcheck(hipFree(work));
73}
74static inline
75void potri (hipsolverHandle_t& hipsolver_handle, const char& uplo, const char& diag, const int& n, std::complex<double> * A, const int& lda)
76{
77 int lwork;
78 hipsolverErrcheck(hipsolverDnZpotri_bufferSize(hipsolver_handle, hipsolver_fill_mode(uplo), n, reinterpret_cast<hipDoubleComplex *>(A), n, &lwork));
79 hipDoubleComplex* work;
80 hipErrcheck(hipMalloc((void**)&work, lwork * sizeof(hipDoubleComplex)));
81 // Perform Cholesky decomposition
82 hipsolverErrcheck(hipsolverDnZpotri(hipsolver_handle, hipsolver_fill_mode(uplo), n, reinterpret_cast<hipDoubleComplex *>(A), n, work, lwork, nullptr));
83 hipErrcheck(hipFree(work));
84}
85
86
87static inline
88void potrf (hipsolverHandle_t& hipsolver_handle, const char& uplo, const int& n, float * A, const int& lda)
89{
90 int lwork;
91 hipsolverErrcheck(hipsolverDnSpotrf_bufferSize(hipsolver_handle, hipsolver_fill_mode(uplo), n, A, n, &lwork));
92 float* work;
93 hipErrcheck(hipMalloc((void**)&work, lwork * sizeof(float)));
94 // Perform Cholesky decomposition
95 hipsolverErrcheck(hipsolverDnSpotrf(hipsolver_handle, hipsolver_fill_mode(uplo), n, A, n, work, lwork, nullptr));
96 hipErrcheck(hipFree(work));
97}
98static inline
99void potrf (hipsolverHandle_t& hipsolver_handle, const char& uplo, const int& n, double * A, const int& lda)
100{
101 int lwork;
102 hipsolverErrcheck(hipsolverDnDpotrf_bufferSize(hipsolver_handle, hipsolver_fill_mode(uplo), n, A, n, &lwork));
103 double* work;
104 hipErrcheck(hipMalloc((void**)&work, lwork * sizeof(double)));
105 // Perform Cholesky decomposition
106 hipsolverErrcheck(hipsolverDnDpotrf(hipsolver_handle, hipsolver_fill_mode(uplo), n, A, n, work, lwork, nullptr));
107 hipErrcheck(hipFree(work));
108}
109static inline
110void potrf (hipsolverHandle_t& hipsolver_handle, const char& uplo, const int& n, std::complex<float> * A, const int& lda)
111{
112 int lwork;
113 hipsolverErrcheck(hipsolverDnCpotrf_bufferSize(hipsolver_handle, hipsolver_fill_mode(uplo), n, reinterpret_cast<hipFloatComplex*>(A), n, &lwork));
114 hipFloatComplex* work;
115 hipErrcheck(hipMalloc((void**)&work, lwork * sizeof(hipFloatComplex)));
116 // Perform Cholesky decomposition
117 hipsolverErrcheck(hipsolverDnCpotrf(hipsolver_handle, hipsolver_fill_mode(uplo), n, reinterpret_cast<hipFloatComplex*>(A), n, work, lwork, nullptr));
118 hipErrcheck(hipFree(work));
119}
120static inline
121void potrf (hipsolverHandle_t& hipsolver_handle, const char& uplo, const int& n, std::complex<double> * A, const int& lda)
122{
123 int lwork;
124 hipsolverErrcheck(hipsolverDnZpotrf_bufferSize(hipsolver_handle, hipsolver_fill_mode(uplo), n, reinterpret_cast<hipDoubleComplex*>(A), n, &lwork));
125 hipDoubleComplex* work;
126 hipErrcheck(hipMalloc((void**)&work, lwork * sizeof(hipDoubleComplex)));
127 // Perform Cholesky decomposition
128 hipsolverErrcheck(hipsolverDnZpotrf(hipsolver_handle, hipsolver_fill_mode(uplo), n, reinterpret_cast<hipDoubleComplex*>(A), n, work, lwork, nullptr));
129 hipErrcheck(hipFree(work));
130}
131
132
133static inline
134void dnevd (hipsolverHandle_t& hipsolver_handle, const char& jobz, const char& uplo, const int& n, float* A, const int& lda, float * W)
135{
136 // prepare some values for hipsolverDnZhegvd_bufferSize
137 int lwork = 0;
138 int h_info = 0;
139 int* d_info = nullptr;
140 float* d_work = nullptr;
141 hipErrcheck(hipMalloc((void**)&d_info, sizeof(int)));
142
143 // calculate the sizes needed for pre-allocated buffer.
144 hipsolverErrcheck(hipsolverDnSsyevd_bufferSize(hipsolver_handle, hipblas_eig_mode(jobz), hipsolver_fill_mode(uplo),
145 n, A, lda, W, &lwork));
146 // allocate memery
147 hipErrcheck(hipMalloc((void**)&d_work, sizeof(float) * lwork));
148 // compute eigenvalues and eigenvectors.
149 hipsolverErrcheck(hipsolverDnSsyevd(hipsolver_handle, hipblas_eig_mode(jobz), hipsolver_fill_mode(uplo),
150 n, A, lda, W, d_work, lwork, d_info));
151
152 hipErrcheck(hipMemcpy(&h_info, d_info, sizeof(int), hipMemcpyDeviceToHost));
153 if (h_info != 0) {
154 throw std::runtime_error("dnevd: failed to invert matrix");
155 }
156 hipErrcheck(hipFree(d_info));
157 hipErrcheck(hipFree(d_work));
158}
159static inline
160void dnevd (hipsolverHandle_t& hipsolver_handle, const char& jobz, const char& uplo, const int& n, double* A, const int& lda, double * W)
161{
162 // prepare some values for hipsolverDnZhegvd_bufferSize
163 int lwork = 0;
164 int h_info = 0;
165 int* d_info = nullptr;
166 double* d_work = nullptr;
167 hipErrcheck(hipMalloc((void**)&d_info, sizeof(int)));
168
169 // calculate the sizes needed for pre-allocated buffer.
170 hipsolverErrcheck(hipsolverDnDsyevd_bufferSize(hipsolver_handle, hipblas_eig_mode(jobz), hipsolver_fill_mode(uplo),
171 n, A, lda, W, &lwork));
172 // allocate memery
173 hipErrcheck(hipMalloc((void**)&d_work, sizeof(double) * lwork));
174 // compute eigenvalues and eigenvectors.
175 hipsolverErrcheck(hipsolverDnDsyevd(hipsolver_handle, hipblas_eig_mode(jobz), hipsolver_fill_mode(uplo),
176 n, A, lda, W, d_work, lwork, d_info));
177
178 hipErrcheck(hipMemcpy(&h_info, d_info, sizeof(int), hipMemcpyDeviceToHost));
179 if (h_info != 0) {
180 throw std::runtime_error("dnevd: failed to invert matrix");
181 }
182 hipErrcheck(hipFree(d_info));
183 hipErrcheck(hipFree(d_work));
184}
185static inline
186void dnevd (hipsolverHandle_t& hipsolver_handle, const char& jobz, const char& uplo, const int& n, std::complex<float>* A, const int& lda, float * W)
187{
188 // prepare some values for hipsolverDnZhegvd_bufferSize
189 int lwork = 0;
190 int h_info = 0;
191 int* d_info = nullptr;
192 hipFloatComplex* d_work = nullptr;
193 hipErrcheck(hipMalloc((void**)&d_info, sizeof(int)));
194
195 // calculate the sizes needed for pre-allocated buffer.
196 hipsolverErrcheck(hipsolverDnCheevd_bufferSize(hipsolver_handle, hipblas_eig_mode(jobz), hipsolver_fill_mode(uplo),
197 n, reinterpret_cast<hipFloatComplex*>(A), lda, W, &lwork));
198 // allocate memery
199 hipErrcheck(hipMalloc((void**)&d_work, sizeof(hipFloatComplex) * lwork));
200 // compute eigenvalues and eigenvectors.
201 hipsolverErrcheck(hipsolverDnCheevd(hipsolver_handle, hipblas_eig_mode(jobz), hipsolver_fill_mode(uplo),
202 n, reinterpret_cast<hipFloatComplex*>(A), lda, W, d_work, lwork, d_info));
203
204 hipErrcheck(hipMemcpy(&h_info, d_info, sizeof(int), hipMemcpyDeviceToHost));
205 if (h_info != 0) {
206 throw std::runtime_error("dnevd: failed to invert matrix");
207 }
208 hipErrcheck(hipFree(d_info));
209 hipErrcheck(hipFree(d_work));
210}
211static inline
212void dnevd (hipsolverHandle_t& hipsolver_handle, const char& jobz, const char& uplo, const int& n, std::complex<double>* A, const int& lda, double* W)
213{
214 // prepare some values for hipsolverDnZhegvd_bufferSize
215 int lwork = 0;
216 int h_info = 0;
217 int* d_info = nullptr;
218 hipDoubleComplex* d_work = nullptr;
219 hipErrcheck(hipMalloc((void**)&d_info, sizeof(int)));
220
221 // calculate the sizes needed for pre-allocated buffer.
222 hipsolverErrcheck(hipsolverDnZheevd_bufferSize(hipsolver_handle, hipblas_eig_mode(jobz), hipsolver_fill_mode(uplo),
223 n, reinterpret_cast<hipDoubleComplex*>(A), lda, W, &lwork));
224 // allocate memery
225 hipErrcheck(hipMalloc((void**)&d_work, sizeof(hipDoubleComplex) * lwork));
226 // compute eigenvalues and eigenvectors.
227 hipsolverErrcheck(hipsolverDnZheevd(hipsolver_handle, hipblas_eig_mode(jobz), hipsolver_fill_mode(uplo),
228 n, reinterpret_cast<hipDoubleComplex*>(A), lda, W, d_work, lwork, d_info));
229
230 hipErrcheck(hipMemcpy(&h_info, d_info, sizeof(int), hipMemcpyDeviceToHost));
231 if (h_info != 0) {
232 throw std::runtime_error("dnevd: failed to invert matrix");
233 }
234 hipErrcheck(hipFree(d_info));
235 hipErrcheck(hipFree(d_work));
236}
237
238static inline
239void dngvd (hipsolverHandle_t& hipsolver_handle, const int& itype, const char& jobz, const char& uplo, const int& n, float* A, const int& lda, float* B, const int& ldb, float * W)
240{
241 // prepare some values for hipsolverDnZhegvd_bufferSize
242 int lwork = 0;
243 int h_info = 0;
244 int* d_info = nullptr;
245 float* d_work = nullptr;
246 hipErrcheck(hipMalloc((void**)&d_info, sizeof(int)));
247
248 // calculate the sizes needed for pre-allocated buffer.
249 hipsolverErrcheck(hipsolverDnSsygvd_bufferSize(hipsolver_handle, hipblas_eig_type(itype), hipblas_eig_mode(jobz), hipsolver_fill_mode(uplo),
250 n, A, lda, B, ldb, W, &lwork));
251 // allocate memery
252 hipErrcheck(hipMalloc((void**)&d_work, sizeof(float) * lwork));
253 // compute eigenvalues and eigenvectors.
254 hipsolverErrcheck(hipsolverDnSsygvd(hipsolver_handle, hipblas_eig_type(itype), hipblas_eig_mode(jobz), hipsolver_fill_mode(uplo),
255 n, A, lda, B, ldb, W, d_work, lwork, d_info));
256
257 hipErrcheck(hipMemcpy(&h_info, d_info, sizeof(int), hipMemcpyDeviceToHost));
258 if (h_info != 0) {
259 throw std::runtime_error("dnevd: failed to invert matrix");
260 }
261 hipErrcheck(hipFree(d_info));
262 hipErrcheck(hipFree(d_work));
263}
264static inline
265void dngvd (hipsolverHandle_t& hipsolver_handle, const int& itype, const char& jobz, const char& uplo, const int& n, double* A, const int& lda, double* B, const int& ldb, double * W)
266{
267 // prepare some values for hipsolverDnZhegvd_bufferSize
268 int lwork = 0;
269 int h_info = 0;
270 int* d_info = nullptr;
271 double* d_work = nullptr;
272 hipErrcheck(hipMalloc((void**)&d_info, sizeof(int)));
273
274 // calculate the sizes needed for pre-allocated buffer.
275 hipsolverErrcheck(hipsolverDnDsygvd_bufferSize(hipsolver_handle, hipblas_eig_type(itype), hipblas_eig_mode(jobz), hipsolver_fill_mode(uplo),
276 n, A, lda, B, ldb, W, &lwork));
277 // allocate memery
278 hipErrcheck(hipMalloc((void**)&d_work, sizeof(double) * lwork));
279 // compute eigenvalues and eigenvectors.
280 hipsolverErrcheck(hipsolverDnDsygvd(hipsolver_handle, hipblas_eig_type(itype), hipblas_eig_mode(jobz), hipsolver_fill_mode(uplo),
281 n, A, lda, B, ldb, W, d_work, lwork, d_info));
282
283 hipErrcheck(hipMemcpy(&h_info, d_info, sizeof(int), hipMemcpyDeviceToHost));
284 if (h_info != 0) {
285 throw std::runtime_error("dnevd: failed to invert matrix");
286 }
287 hipErrcheck(hipFree(d_info));
288 hipErrcheck(hipFree(d_work));
289}
290static inline
291void dngvd (hipsolverHandle_t& hipsolver_handle, const int& itype, const char& jobz, const char& uplo, const int& n, std::complex<float>* A, const int& lda, std::complex<float>* B, const int& ldb, float* W)
292{
293 // prepare some values for hipsolverDnZhegvd_bufferSize
294 int lwork = 0;
295 int h_info = 0;
296 int* d_info = nullptr;
297 hipFloatComplex* d_work = nullptr;
298 hipErrcheck(hipMalloc((void**)&d_info, sizeof(int)));
299
300 // calculate the sizes needed for pre-allocated buffer.
301 hipsolverErrcheck(hipsolverDnChegvd_bufferSize(hipsolver_handle, hipblas_eig_type(itype), hipblas_eig_mode(jobz), hipsolver_fill_mode(uplo),
302 n, reinterpret_cast<hipFloatComplex*>(A), lda, reinterpret_cast<hipFloatComplex*>(B), ldb, W, &lwork));
303 // allocate memery
304 hipErrcheck(hipMalloc((void**)&d_work, sizeof(hipFloatComplex) * lwork));
305 // compute eigenvalues and eigenvectors.
306 hipsolverErrcheck(hipsolverDnChegvd(hipsolver_handle, hipblas_eig_type(itype), hipblas_eig_mode(jobz), hipsolver_fill_mode(uplo),
307 n, reinterpret_cast<hipFloatComplex*>(A), lda, reinterpret_cast<hipFloatComplex*>(B), ldb, W, d_work, lwork, d_info));
308
309 hipErrcheck(hipMemcpy(&h_info, d_info, sizeof(int), hipMemcpyDeviceToHost));
310 if (h_info != 0) {
311 throw std::runtime_error("dnevd: failed to invert matrix");
312 }
313 hipErrcheck(hipFree(d_info));
314 hipErrcheck(hipFree(d_work));
315}
316static inline
317void dngvd (hipsolverHandle_t& hipsolver_handle, const int& itype, const char& jobz, const char& uplo, const int& n, std::complex<double>* A, const int& lda, std::complex<double>* B, const int& ldb, double* W)
318{
319 // prepare some values for hipsolverDnZhegvd_bufferSize
320 int lwork = 0;
321 int h_info = 0;
322 int* d_info = nullptr;
323 hipDoubleComplex* d_work = nullptr;
324 hipErrcheck(hipMalloc((void**)&d_info, sizeof(int)));
325
326 // calculate the sizes needed for pre-allocated buffer.
327 hipsolverErrcheck(hipsolverDnZhegvd_bufferSize(hipsolver_handle, hipblas_eig_type(itype), hipblas_eig_mode(jobz), hipsolver_fill_mode(uplo),
328 n, reinterpret_cast<hipDoubleComplex*>(A), lda, reinterpret_cast<hipDoubleComplex*>(B), ldb, W, &lwork));
329 // allocate memery
330 hipErrcheck(hipMalloc((void**)&d_work, sizeof(hipDoubleComplex) * lwork));
331 // compute eigenvalues and eigenvectors.
332 hipsolverErrcheck(hipsolverDnZhegvd(hipsolver_handle, hipblas_eig_type(itype), hipblas_eig_mode(jobz), hipsolver_fill_mode(uplo),
333 n, reinterpret_cast<hipDoubleComplex*>(A), lda, reinterpret_cast<hipDoubleComplex*>(B), ldb, W, d_work, lwork, d_info));
334
335 hipErrcheck(hipMemcpy(&h_info, d_info, sizeof(int), hipMemcpyDeviceToHost));
336 if (h_info != 0) {
337 throw std::runtime_error("dnevd: failed to invert matrix");
338 }
339 hipErrcheck(hipFree(d_info));
340 hipErrcheck(hipFree(d_work));
341}
342
343} // namespace hipSolverConnector
344} // namespace container
345
346#endif // BASE_THIRD_PARTY_HIPSOLVER_H_
#define T
Definition exp.cpp:237
Definition tensor.cpp:8
#define hipsolverErrcheck(res)
Definition rocm.h:222
#define hipErrcheck(res)
Definition rocm.h:233
Definition rocm.h:60