ABACUS develop
Atomic-orbital Based Ab-initio Computation at UStc
Loading...
Searching...
No Matches
math_kernel_op.h
Go to the documentation of this file.
1// TODO: This is a temperary location for these functions.
2// And will be moved to a global module(module base) later.
3#ifndef MODULE_HSOLVER_MATH_KERNEL_H
4#define MODULE_HSOLVER_MATH_KERNEL_H
5
7
9
12
13#if defined(__CUDA) || defined(__UT_USE_CUDA)
14#include <cuda_runtime.h>
15
16#include "cublas_v2.h"
17#endif //__CUDA || __UT_USE_CUDA
18
19namespace ModuleBase {
20
21//---------------------------------------------------------------------------------
22//-----------------------------0. Tool Functions-----------------------------------
23//---------------------------------------------------------------------------------
24inline std::complex<double> set_real_tocomplex(const std::complex<double> &x) {
25 return {x.real(), 0.0};
26}
27
28inline std::complex<float> set_real_tocomplex(const std::complex<float> &x) {
29 return {x.real(), 0.0};
30}
31
32inline double set_real_tocomplex(const double &x) { return x; }
33
34inline float set_real_tocomplex(const float &x) { return x; }
35
36inline std::complex<double> get_conj(const std::complex<double> &x) {
37 return {x.real(), -x.imag()};
38}
39
40inline std::complex<float> get_conj(const std::complex<float> &x) {
41 return {x.real(), -x.imag()};
42}
43
44inline double get_conj(const double &x) { return x; }
45
46inline float get_conj(const float &x) { return x; }
47
48
49//---------------------------------------------------------------------------------
50//-----------------------------1. Vector Operations--------------------------------
51//---------------------------------------------------------------------------------
52template <typename FPTYPE, typename Device> struct scal_op {
63 void operator()(const int &N,
64 const std::complex<FPTYPE> *alpha, std::complex<FPTYPE> *X,
65 const int &incx);
66};
67
68template <typename T, typename Device> struct vector_mul_real_op {
69 using Real = typename GetTypeReal<T>::type;
81 void operator()(const int dim, T* result, const T* vector, const Real constant);
82};
83
84// vector operator: result[i] = vector1[i](complex) * vector2[i](not complex)
85template <typename T, typename Device> struct vector_mul_vector_op {
86 using Real = typename GetTypeReal<T>::type;
97 void operator()(const int& dim, T* result, const T* vector1, const Real* vector2, const bool& add = false);
98};
99
100// vector operator: result[i] = vector[i] / constant
101template <typename T, typename Device> struct vector_div_constant_op {
102 using Real = typename GetTypeReal<T>::type;
112 void operator()(const int& dim, T* result, const T* vector, const Real constant);
113};
114
115// vector operator: result[i] = vector1[i](complex) / vector2[i](not complex)
116template <typename T, typename Device> struct vector_div_vector_op {
117 using Real = typename GetTypeReal<T>::type;
127 void operator()(const int &dim, T *result, const T *vector1,
128 const Real *vector2);
129};
130
131// compute Y = alpha * X + Y
132template <typename T, typename Device> struct axpy_op {
145 void operator()(const int &N, const T *alpha, const T *X,
146 const int &incX, T *Y, const int &incY);
147};
148
149// vector operator: result[i] = vector1[i] * constant1 + vector2[i] * constant2
150template <typename T, typename Device>
152 using Real = typename GetTypeReal<T>::type;
164 void operator()(const int &dim, T *result, const T *vector1,
165 const Real constant1, const T *vector2, const Real constant2);
166};
167
168template <typename T, typename Device> struct dot_real_op {
169 using Real = typename GetTypeReal<T>::type;
182 Real operator()(const int &dim, const T *psi_L,
183 const T *psi_R, const bool reduce = true);
184};
185
186
187//---------------------------------------------------------------------------------
188//-----------------------------2. Matrix Operations--------------------------------
189//---------------------------------------------------------------------------------
190
191// compute y = alpha * op(A) * x + beta * y
192template <typename T, typename Device> struct gemv_op {
210 void operator()(const char &trans, const int &m,
211 const int &n, const T *alpha, const T *A, const int &lda,
212 const T *X, const int &incx, const T *beta, T *Y,
213 const int &incy);
214};
215
216// compute C = alpha * op(A) * op(B) + beta * C
217template <typename T, typename Device> struct gemm_op {
237 void operator()(const char &transa, const char &transb,
238 const int &m, const int &n, const int &k, const T *alpha,
239 const T *a, const int &lda, const T *b, const int &ldb,
240 const T *beta, T *c, const int &ldc);
241};
242
243#ifdef __DSP
244// compute C = alpha * op(A) * op(B) + beta * C on DSP Hardware
245template <typename T, typename Device> struct gemm_op_mt {
265 void operator()(const char &transa, const char &transb,
266 const int &m, const int &n, const int &k, const T *alpha,
267 const T *a, const int &lda, const T *b, const int &ldb,
268 const T *beta, T *c, const int &ldc);
269};
270#endif
271
272template <typename T, typename Device> struct matrixTranspose_op {
282 void operator()(const int &row, const int &col,
283 const T *input_matrix, T *output_matrix);
284};
285
286template <typename T, typename Device> struct matrixCopy {
298 void operator()(const int& n1, const int& n2, const T* A, const int& LDA, T* B, const int& LDB);
299};
300
301template <typename T, typename Device>
303 using Real = typename GetTypeReal<T>::type;
304
305 void operator()(const Device *d, const int &nbase, const int &nbase_x, const int &notconv,
306 T *result, const T *vectors, const Real *eigenvalues);
307};
308
309template <typename T, typename Device>
311 using Real = typename GetTypeReal<T>::type;
312 void operator()(const Device* d,
313 const int& dim,
314 T* psi_iter,
315 const int& nbase,
316 const int& notconv,
317 const Real* precondition,
318 const Real* eigenvalues);
319};
320
321template <typename T, typename Device>
323 using Real = typename GetTypeReal<T>::type;
324 void operator()(const Device* d,
325 const int& dim,
326 T* psi_iter,
327 const int& nbase,
328 const int& notconv,
329 Real* psi_norm = nullptr);
330};
331
332template <typename T>
333struct normalize_op<T, base_device::DEVICE_GPU> {
334 using Real = typename GetTypeReal<T>::type;
335 void operator()(const base_device::DEVICE_GPU* d,
336 const int& dim,
337 T* psi_iter,
338 const int& nbase,
339 const int& notconv,
340 Real* psi_norm);
341};
342
343#if __CUDA || __UT_USE_CUDA || __ROCM || __UT_USE_ROCM
344// Partially specialize functor for base_device::GpuDevice.
345template <typename T> struct dot_real_op<T, base_device::DEVICE_GPU> {
346 using Real = typename GetTypeReal<T>::type;
347 Real operator()(const int &dim,
348 const T *psi_L, const T *psi_R, const bool reduce = true);
349};
350
351// vector operator: result[i] = vector[i] / constant
352template <typename T>
353struct vector_mul_real_op<T, base_device::DEVICE_GPU>
354{
355 using Real = typename GetTypeReal<T>::type;
356 void operator()(const int dim, T* result, const T* vector, const Real constant);
357};
358
359// vector operator: result[i] = vector1[i](complex) * vector2[i](not complex)
360template <typename T> struct vector_mul_vector_op<T, base_device::DEVICE_GPU> {
361 using Real = typename GetTypeReal<T>::type;
362 void operator()(const int& dim, T* result, const T* vector1, const Real* vector2, const bool& add = false);
363};
364
365// vector operator: result[i] = vector[i] / constant
366template <typename T> struct vector_div_constant_op<T, base_device::DEVICE_GPU> {
367 using Real = typename GetTypeReal<T>::type;
368 void operator()(const int& dim, T* result, const T* vector, const Real constant);
369};
370
371// vector operator: result[i] = vector1[i](complex) / vector2[i](not complex)
372template <typename T> struct vector_div_vector_op<T, base_device::DEVICE_GPU> {
373 using Real = typename GetTypeReal<T>::type;
374 void operator()(const int &dim, T *result,
375 const T *vector1, const Real *vector2);
376};
377
378// vector operator: result[i] = vector1[i] * constant1 + vector2[i] * constant2
379template <typename T>
380struct vector_add_vector_op<T, base_device::DEVICE_GPU> {
381 using Real = typename GetTypeReal<T>::type;
382 void operator()(const int &dim, T *result,
383 const T *vector1, const Real constant1, const T *vector2,
384 const Real constant2);
385};
386
387template <typename T> struct matrixCopy<T, base_device::DEVICE_GPU> {
388 void operator()(const int& n1,
389 const int& n2,
390 const T* A, // input
391 const int& LDA,
392 T* B, // output
393 const int& LDB);
394};
395
396void createGpuBlasHandle();
397void destoryBLAShandle();
398
399// vector operator: result[i] = -lambda[i] * vector[i]
400template <typename T> struct apply_eigenvalues_op<T, base_device::DEVICE_GPU> {
401 using Real = typename GetTypeReal<T>::type;
402
403 void operator()(const base_device::DEVICE_GPU *d, const int &nbase, const int &nbase_x, const int &notconv,
404 T *result, const T *vectors, const Real *eigenvalues);
405};
406
407template <typename T>
408struct precondition_op<T, base_device::DEVICE_GPU> {
409 using Real = typename GetTypeReal<T>::type;
410 void operator()(const base_device::DEVICE_GPU* d,
411 const int& dim,
412 T* psi_iter,
413 const int& nbase,
414 const int& notconv,
415 const Real* precondition,
416 const Real* eigenvalues);
417};
418
419#endif // __CUDA || __UT_USE_CUDA || __ROCM || __UT_USE_ROCM
420} // namespace hsolver
421
422#endif // MODULE_HSOLVER_MATH_KERNEL_H
#define N
Definition exp.cpp:24
#define T
Definition exp.cpp:237
Definition array_pool.h:6
std::complex< double > set_real_tocomplex(const std::complex< double > &x)
Definition math_kernel_op.h:24
std::complex< double > get_conj(const std::complex< double > &x)
Definition math_kernel_op.h:36
Definition device.cpp:21
T type
Definition macros.h:8
Definition math_kernel_op.h:302
void operator()(const Device *d, const int &nbase, const int &nbase_x, const int &notconv, T *result, const T *vectors, const Real *eigenvalues)
typename GetTypeReal< T >::type Real
Definition math_kernel_op.h:303
Definition math_kernel_op.h:132
void operator()(const int &N, const T *alpha, const T *X, const int &incX, T *Y, const int &incY)
Y = alpha * X + Y.
Definition math_kernel_op.h:168
typename GetTypeReal< T >::type Real
Definition math_kernel_op.h:169
Real operator()(const int &dim, const T *psi_L, const T *psi_R, const bool reduce=true)
dot_real_op computes the dot product of the given complex arrays(treated as float arrays)....
Definition math_kernel_op.h:217
void operator()(const char &transa, const char &transb, const int &m, const int &n, const int &k, const T *alpha, const T *a, const int &lda, const T *b, const int &ldb, const T *beta, T *c, const int &ldc)
C = alpha * op(A) * op(B) + beta * C.
Definition math_kernel_op.h:192
void operator()(const char &trans, const int &m, const int &n, const T *alpha, const T *A, const int &lda, const T *X, const int &incx, const T *beta, T *Y, const int &incy)
y = alpha * op(A) * x + beta * y
Definition math_kernel_op.h:286
void operator()(const int &n1, const int &n2, const T *A, const int &LDA, T *B, const int &LDB)
copy matrix A to B, they can have different leading dimensions
Definition math_kernel_op.h:272
void operator()(const int &row, const int &col, const T *input_matrix, T *output_matrix)
transpose the input matrix
void operator()(const base_device::DEVICE_GPU *d, const int &dim, T *psi_iter, const int &nbase, const int &notconv, Real *psi_norm)
typename GetTypeReal< T >::type Real
Definition math_kernel_op.h:334
Definition math_kernel_op.h:322
typename GetTypeReal< T >::type Real
Definition math_kernel_op.h:323
void operator()(const Device *d, const int &dim, T *psi_iter, const int &nbase, const int &notconv, Real *psi_norm=nullptr)
Definition math_kernel_op.h:310
typename GetTypeReal< T >::type Real
Definition math_kernel_op.h:311
void operator()(const Device *d, const int &dim, T *psi_iter, const int &nbase, const int &notconv, const Real *precondition, const Real *eigenvalues)
Definition math_kernel_op.h:52
void operator()(const int &N, const std::complex< FPTYPE > *alpha, std::complex< FPTYPE > *X, const int &incx)
x = alpha * x, where alpha and x are complex numbers
Definition math_kernel_op.h:151
void operator()(const int &dim, T *result, const T *vector1, const Real constant1, const T *vector2, const Real constant2)
result[i] = vector1[i] * constant1 + vector2[i] * constant2
typename GetTypeReal< T >::type Real
Definition math_kernel_op.h:152
Definition math_kernel_op.h:101
typename GetTypeReal< T >::type Real
Definition math_kernel_op.h:102
void operator()(const int &dim, T *result, const T *vector, const Real constant)
result[i] = vector[i] / constant
Definition math_kernel_op.h:116
void operator()(const int &dim, T *result, const T *vector1, const Real *vector2)
result[i] = vector1[i](complex) / vector2[i](not complex)
typename GetTypeReal< T >::type Real
Definition math_kernel_op.h:117
Definition math_kernel_op.h:68
void operator()(const int dim, T *result, const T *vector, const Real constant)
result[i] = vector[i] * constant, where vector is complex number and constant is real number。 It is d...
typename GetTypeReal< T >::type Real
Definition math_kernel_op.h:69
Definition math_kernel_op.h:85
typename GetTypeReal< T >::type Real
Definition math_kernel_op.h:86
void operator()(const int &dim, T *result, const T *vector1, const Real *vector2, const bool &add=false)
result[i] = vector1[i](complex) * vector2[i](not complex)