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
8
11
12#if defined(__CUDA) || defined(__UT_USE_CUDA)
13#include <cuda_runtime.h>
14
15#include "cublas_v2.h"
16#endif //__CUDA || __UT_USE_CUDA
17
18namespace ModuleBase {
19
25void gram_herk_cpu(int ld, int dim, int bands, const std::complex<double>* x,
26 std::complex<double>* output);
27
28//---------------------------------------------------------------------------------
29//-----------------------------0. Tool Functions-----------------------------------
30//---------------------------------------------------------------------------------
31inline std::complex<double> set_real_tocomplex(const std::complex<double> &x) {
32 return {x.real(), 0.0};
33}
34
35inline std::complex<float> set_real_tocomplex(const std::complex<float> &x) {
36 return {x.real(), 0.0};
37}
38
39inline double set_real_tocomplex(const double &x) { return x; }
40
41inline float set_real_tocomplex(const float &x) { return x; }
42
43inline std::complex<double> get_conj(const std::complex<double> &x) {
44 return {x.real(), -x.imag()};
45}
46
47inline std::complex<float> get_conj(const std::complex<float> &x) {
48 return {x.real(), -x.imag()};
49}
50
51inline double get_conj(const double &x) { return x; }
52
53inline float get_conj(const float &x) { return x; }
54
55
56//---------------------------------------------------------------------------------
57//-----------------------------1. Vector Operations--------------------------------
58//---------------------------------------------------------------------------------
59template <typename FPTYPE, typename Device> struct scal_op {
70 void operator()(const int &N,
71 const std::complex<FPTYPE> *alpha, std::complex<FPTYPE> *X,
72 const int &incx);
73};
74
75template <typename T, typename Device> struct vector_mul_real_op {
76 using Real = typename GetTypeReal<T>::type;
88 void operator()(const int dim, T* result, const T* vector, const Real constant);
89};
90
91// vector operator: result[i] = vector1[i] * vector2[i]
92template <typename T, typename Device, typename Operand> struct vector_mul_vector_op {
103 void operator()(const int& dim, T* result, const T* vector1, const Operand* vector2, const bool& add);
104};
105
106template <typename T, typename Operand>
107struct vector_mul_vector_op<T, base_device::DEVICE_CPU, Operand> {
108 void operator()(const int &dim, T *result, const T *vector1,
109 const Operand *vector2, const bool &add) {
110 if (dim <= 0) {
111 return;
112 }
113 if (add) {
114#ifdef _OPENMP
115#pragma omp parallel for schedule(static)
116#endif
117 for (int i = 0; i < dim; i++) {
118 result[i] += vector1[i] * vector2[i];
119 }
120 } else {
121#ifdef _OPENMP
122#pragma omp parallel for schedule(static)
123#endif
124 for (int i = 0; i < dim; i++) {
125 result[i] = vector1[i] * vector2[i];
126 }
127 }
128 }
129};
130
131// vector operator: result[i] = vector[i] / constant
132template <typename T, typename Device> struct vector_div_constant_op {
133 using Real = typename GetTypeReal<T>::type;
143 void operator()(const int& dim, T* result, const T* vector, const Real constant);
144};
145
146// vector operator: result[i] = vector1[i] / vector2[i]
147template <typename T, typename Device, typename Operand> struct vector_div_vector_op {
157 void operator()(const int &dim, T *result, const T *vector1,
158 const Operand *vector2);
159};
160
161template <typename T, typename Operand>
162struct vector_div_vector_op<T, base_device::DEVICE_CPU, Operand> {
163 void operator()(const int &dim, T *result, const T *vector1,
164 const Operand *vector2) {
165 if (dim <= 0) {
166 return;
167 }
168#ifdef _OPENMP
169#pragma omp parallel for schedule(static)
170#endif
171 for (int i = 0; i < dim; i++) {
172 result[i] = vector1[i] / vector2[i];
173 }
174 }
175};
176
177// compute Y = alpha * X + Y
178template <typename T, typename Device> struct axpy_op {
191 void operator()(const int &N, const T *alpha, const T *X,
192 const int &incX, T *Y, const int &incY);
193};
194
195// vector operator: result[i] = vector1[i] * constant1 + vector2[i] * constant2
196template <typename T, typename Device, typename Scalar>
209 void operator()(const int &dim, T *result, const T *vector1,
210 const Scalar constant1, const T *vector2, const Scalar constant2);
211};
212
213template <typename T, typename Scalar>
214struct vector_add_vector_op<T, base_device::DEVICE_CPU, Scalar> {
215 void operator()(const int &dim, T *result, const T *vector1,
216 const Scalar constant1, const T *vector2,
217 const Scalar constant2) {
218 if (dim <= 0) {
219 return;
220 }
221#ifdef _OPENMP
222#pragma omp parallel for schedule(static)
223#endif
224 for (int i = 0; i < dim; i++) {
225 result[i] = vector1[i] * constant1 + vector2[i] * constant2;
226 }
227 }
228};
229
230template <typename T, typename Device> struct dot_real_op {
231 using Real = typename GetTypeReal<T>::type;
244 Real operator()(const int &dim, const T *psi_L,
245 const T *psi_R, const bool reduce = true);
246};
247
248
249//---------------------------------------------------------------------------------
250//-----------------------------2. Matrix Operations--------------------------------
251//---------------------------------------------------------------------------------
252
253// compute y = alpha * op(A) * x + beta * y
254template <typename T, typename Device> struct gemv_op {
272 void operator()(const char &trans, const int &m,
273 const int &n, const T *alpha, const T *A, const int &lda,
274 const T *X, const int &incx, const T *beta, T *Y,
275 const int &incy);
276};
277
278template <typename T, typename Device> struct trmm_op
279{
292 void operator()(const char& side, const char& uplo, const char& trans, const char& diag,
293 const int& m, const int& n, const T* alpha, const T* a, const int& lda,
294 T* b, const int& ldb);
295};
296
297// compute C = alpha * op(A) * op(B) + beta * C
298template <typename T, typename Device> struct gemm_op {
318 void operator()(const char &transa, const char &transb,
319 const int &m, const int &n, const int &k, const T *alpha,
320 const T *a, const int &lda, const T *b, const int &ldb,
321 const T *beta, T *c, const int &ldc);
322};
323
324#ifdef __DSP
325// compute Y = alpha * op(A) * X + beta * Y on DSP Hardware
326template <typename T, typename Device> struct gemv_op_mt {
344 void operator()(const char &trans, const int &m,
345 const int &n, const T *alpha, const T *A, const int &lda,
346 const T *X, const int &incx, const T *beta, T *Y,
347 const int &incy);
348};
349
350// compute C = alpha * op(A) * op(B) + beta * C on DSP Hardware
351template <typename T, typename Device> struct gemm_op_mt {
371 void operator()(const char &transa, const char &transb,
372 const int &m, const int &n, const int &k, const T *alpha,
373 const T *a, const int &lda, const T *b, const int &ldb,
374 const T *beta, T *c, const int &ldc);
375};
376#endif
377
378template <typename T, typename Device> struct matrixTranspose_op {
388 void operator()(const int &row, const int &col,
389 const T *input_matrix, T *output_matrix);
390};
391
392template <typename T, typename Device> struct matrixCopy {
404 void operator()(const int& n1, const int& n2, const T* A, const int& LDA, T* B, const int& LDB);
405};
406
407template <typename T, typename Device>
409 using Real = typename GetTypeReal<T>::type;
423 void operator()(const int &m, const int &n,
424 T *a,
425 const int &lda,
426 const Real *b,
427 const Real alpha,
428 T *c,
429 const int &ldc);
430};
431
432template <typename T, typename Device>
434 using Real = typename GetTypeReal<T>::type;
435
436 void operator()(const Device *d, const int &nbase, const int &nbase_x, const int &notconv,
437 T *result, const T *vectors, const Real *eigenvalues);
438};
439
440template <typename T, typename Device>
442 using Real = typename GetTypeReal<T>::type;
443 void operator()(const Device* d,
444 const int& dim,
445 T* psi_iter,
446 const int& nbase,
447 const int& notconv,
448 const Real* precondition,
449 const Real* eigenvalues);
450};
451
452template <typename T, typename Device>
454 using Real = typename GetTypeReal<T>::type;
455 void operator()(const Device* d,
456 const int& dim,
457 T* psi_iter,
458 const int& nbase,
459 const int& notconv,
460 Real* psi_norm = nullptr);
461};
462
463template <typename T>
464struct normalize_op<T, base_device::DEVICE_GPU> {
465 using Real = typename GetTypeReal<T>::type;
466 void operator()(const base_device::DEVICE_GPU* d,
467 const int& dim,
468 T* psi_iter,
469 const int& nbase,
470 const int& notconv,
471 Real* psi_norm);
472};
473
474#if __CUDA || __UT_USE_CUDA || __ROCM || __UT_USE_ROCM
475// Partially specialize functor for base_device::GpuDevice.
476template <typename T> struct dot_real_op<T, base_device::DEVICE_GPU> {
477 using Real = typename GetTypeReal<T>::type;
478 Real operator()(const int &dim,
479 const T *psi_L, const T *psi_R, const bool reduce = true);
480};
481
482// vector operator: result[i] = vector[i] / constant
483template <typename T>
484struct vector_mul_real_op<T, base_device::DEVICE_GPU>
485{
486 using Real = typename GetTypeReal<T>::type;
487 void operator()(const int dim, T* result, const T* vector, const Real constant);
488};
489
490// vector operator: result[i] = vector1[i] * vector2[i]
491template <typename T, typename Operand>
492struct vector_mul_vector_op<T, base_device::DEVICE_GPU, Operand> {
493 void operator()(const int& dim, T* result, const T* vector1, const Operand* vector2, const bool& add);
494};
495
496// vector operator: result[i] = vector[i] / constant
497template <typename T> struct vector_div_constant_op<T, base_device::DEVICE_GPU> {
498 using Real = typename GetTypeReal<T>::type;
499 void operator()(const int& dim, T* result, const T* vector, const Real constant);
500};
501
502// vector operator: result[i] = vector1[i] / vector2[i]
503template <typename T, typename Operand>
504struct vector_div_vector_op<T, base_device::DEVICE_GPU, Operand> {
505 void operator()(const int &dim, T *result,
506 const T *vector1, const Operand *vector2);
507};
508
509// vector operator: result[i] = vector1[i] * constant1 + vector2[i] * constant2
510template <typename T, typename Scalar>
511struct vector_add_vector_op<T, base_device::DEVICE_GPU, Scalar> {
512 void operator()(const int &dim, T *result,
513 const T *vector1, const Scalar constant1, const T *vector2,
514 const Scalar constant2);
515};
516
517template <typename T> struct matrixCopy<T, base_device::DEVICE_GPU> {
518 void operator()(const int& n1,
519 const int& n2,
520 const T* A, // input
521 const int& LDA,
522 T* B, // output
523 const int& LDB);
524};
525
526template <typename T> struct matrix_mul_vector_op<T, base_device::DEVICE_GPU> {
527 using Real = typename GetTypeReal<T>::type;
528 void operator()(const int &m, const int &n,
529 T *a,
530 const int &lda,
531 const Real *b,
532 const Real alpha,
533 T *c,
534 const int &ldc);
535};
536
537void createGpuBlasHandle();
538void destoryBLAShandle();
539
540// vector operator: result[i] = -lambda[i] * vector[i]
541template <typename T> struct apply_eigenvalues_op<T, base_device::DEVICE_GPU> {
542 using Real = typename GetTypeReal<T>::type;
543
544 void operator()(const base_device::DEVICE_GPU *d, const int &nbase, const int &nbase_x, const int &notconv,
545 T *result, const T *vectors, const Real *eigenvalues);
546};
547
548template <typename T>
549struct precondition_op<T, base_device::DEVICE_GPU> {
550 using Real = typename GetTypeReal<T>::type;
551 void operator()(const base_device::DEVICE_GPU* d,
552 const int& dim,
553 T* psi_iter,
554 const int& nbase,
555 const int& notconv,
556 const Real* precondition,
557 const Real* eigenvalues);
558};
559
560#endif // __CUDA || __UT_USE_CUDA || __ROCM || __UT_USE_ROCM
561} // namespace hsolver
562
563#endif // MODULE_HSOLVER_MATH_KERNEL_H
#define N
Definition exp.cpp:24
#define T
Definition exp.cpp:237
Definition clebsch_gordan_coeff.cpp:8
void gram_herk_cpu(int ld, int dim, int bands, const std::complex< double > *x, std::complex< double > *output)
Definition math_kernel_op.cpp:37
std::complex< double > set_real_tocomplex(const std::complex< double > &x)
Definition math_kernel_op.h:31
std::complex< double > get_conj(const std::complex< double > &x)
Definition math_kernel_op.h:43
Definition device.cpp:21
T type
Definition macros.h:8
Definition math_kernel_op.h:433
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:434
Definition math_kernel_op.h:178
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:230
typename GetTypeReal< T >::type Real
Definition math_kernel_op.h:231
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:298
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:254
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:392
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:378
void operator()(const int &row, const int &col, const T *input_matrix, T *output_matrix)
transpose the input matrix
Definition math_kernel_op.h:408
void operator()(const int &m, const int &n, T *a, const int &lda, const Real *b, const Real alpha, T *c, const int &ldc)
a * b * beta by each column
typename GetTypeReal< T >::type Real
Definition math_kernel_op.h:409
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:465
Definition math_kernel_op.h:453
typename GetTypeReal< T >::type Real
Definition math_kernel_op.h:454
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:441
typename GetTypeReal< T >::type Real
Definition math_kernel_op.h:442
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:59
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:279
void operator()(const char &side, const char &uplo, const char &trans, const char &diag, const int &m, const int &n, const T *alpha, const T *a, const int &lda, T *b, const int &ldb)
In-place column-major B = alpha*op(A)*B or B = alpha*B*op(A).
void operator()(const int &dim, T *result, const T *vector1, const Scalar constant1, const T *vector2, const Scalar constant2)
Definition math_kernel_op.h:215
Definition math_kernel_op.h:197
void operator()(const int &dim, T *result, const T *vector1, const Scalar constant1, const T *vector2, const Scalar constant2)
result[i] = vector1[i] * constant1 + vector2[i] * constant2
Definition math_kernel_op.h:132
typename GetTypeReal< T >::type Real
Definition math_kernel_op.h:133
void operator()(const int &dim, T *result, const T *vector, const Real constant)
result[i] = vector[i] / constant
void operator()(const int &dim, T *result, const T *vector1, const Operand *vector2)
Definition math_kernel_op.h:163
Definition math_kernel_op.h:147
void operator()(const int &dim, T *result, const T *vector1, const Operand *vector2)
result[i] = vector1[i] / vector2[i]
Definition math_kernel_op.h:75
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:76
void operator()(const int &dim, T *result, const T *vector1, const Operand *vector2, const bool &add)
Definition math_kernel_op.h:108
Definition math_kernel_op.h:92
void operator()(const int &dim, T *result, const T *vector1, const Operand *vector2, const bool &add)
result[i] = vector1[i] * vector2[i]
std::string output
Definition test_io_sys_var.cpp:60