forked from deepmodeling/abacus-develop
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmath_kernel_op.cpp
More file actions
192 lines (179 loc) · 6.1 KB
/
math_kernel_op.cpp
File metadata and controls
192 lines (179 loc) · 6.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
#include "source_base/kernels/math_kernel_op.h"
#include "source_base/module_external/blas_connector.h"
#include <iomanip>
#include <iostream>
namespace ModuleBase
{
template <typename T>
struct gemv_op<T, base_device::DEVICE_CPU>
{
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)
{
BlasConnector::gemv(trans, m, n, *alpha, A, lda, X, incx, *beta, Y, incy);
}
};
template <typename T>
struct gemm_op<T, base_device::DEVICE_CPU>
{
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)
{
BlasConnector::gemm(transb, transa, n, m, k, *alpha, b, ldb, a, lda, *beta, c, ldc);
}
};
#ifdef __DSP
template <typename T>
struct gemv_op_mt<T, base_device::DEVICE_CPU>
{
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)
{
BlasConnector::gemv(trans, m, n, *alpha, A, lda, X, incx, *beta, Y, incy, base_device::AbacusDevice_t::DspDevice);
}
};
template <typename T>
struct gemm_op_mt<T, base_device::DEVICE_CPU>
{
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)
{
BlasConnector::gemm(transb, transa, n, m, k, *alpha, b, ldb, a, lda, *beta, c, ldc, base_device::AbacusDevice_t::DspDevice);
}
};
#endif
template <typename T>
struct matrixTranspose_op<T, base_device::DEVICE_CPU>
{
void operator()(const int& row,
const int& col,
const T* input_matrix,
T* output_matrix)
{
T* temp = nullptr;
base_device::memory::resize_memory_op<T, base_device::DEVICE_CPU>()(temp, row * col, "MTransOp");
#ifdef _OPENMP
#pragma omp parallel for collapse(2) schedule(static)
#endif
for (int j = 0; j < col; j++)
{
for (int i = 0; i < row; i++)
{
temp[j * row + i] = input_matrix[i * col + j];
}
}
#ifdef _OPENMP
#pragma omp parallel for schedule(static)
#endif
for (int i = 0; i < row * col; i++)
{
output_matrix[i] = temp[i];
}
base_device::memory::delete_memory_op<T, base_device::DEVICE_CPU>()(temp);
}
};
template <typename T>
struct matrixCopy<T, base_device::DEVICE_CPU>
{
void operator()(const int& n1, const int& n2, const T* A, const int& LDA, T* B, const int& LDB)
{
#ifdef _OPENMP
#pragma omp parallel for collapse(2) schedule(static)
#endif
for (int i = 0; i < n1; i++)
{
for (int j = 0; j < n2; j++)
{
B[i * LDB + j] = A[i * LDA + j];
}
}
}
};
template <typename T>
struct matrix_mul_vector_op<T, base_device::DEVICE_CPU> {
using Real = typename GetTypeReal<T>::type;
void operator()(const int& m, const int &n,
T *a,
const int &lda,
const Real *b,
const Real alpha,
T *c,
const int &ldc){
#ifdef _OPENMP
#pragma omp parallel for collapse(2) schedule(static)
#endif
for (int j = 0; j < n; j++){
for (int i = 0; i < m; i++){
c[j * ldc + i] = a[j * lda + i] * b[j] * alpha;
}
}
}
};
template struct gemv_op<std::complex<float>, base_device::DEVICE_CPU>;
template struct gemv_op<float, base_device::DEVICE_CPU>;
template struct gemm_op<std::complex<float>, base_device::DEVICE_CPU>;
template struct gemm_op<float, base_device::DEVICE_CPU>;
template struct matrixTranspose_op<std::complex<float>, base_device::DEVICE_CPU>;
template struct matrixCopy<std::complex<float>, base_device::DEVICE_CPU>;
template struct matrix_mul_vector_op<std::complex<float>, base_device::DEVICE_CPU>;
template struct gemv_op<std::complex<double>, base_device::DEVICE_CPU>;
template struct gemv_op<double, base_device::DEVICE_CPU>;
template struct gemm_op<std::complex<double>, base_device::DEVICE_CPU>;
template struct gemm_op<double, base_device::DEVICE_CPU>;
template struct matrixTranspose_op<std::complex<double>, base_device::DEVICE_CPU>;
template struct matrixCopy<double, base_device::DEVICE_CPU>;
template struct matrixCopy<std::complex<double>, base_device::DEVICE_CPU>;
template struct matrix_mul_vector_op<double, base_device::DEVICE_CPU>;
template struct matrix_mul_vector_op<std::complex<double>, base_device::DEVICE_CPU>;
#ifdef __LCAO
template struct matrixTranspose_op<double, base_device::DEVICE_CPU>;
#endif
#ifdef __DSP
template struct gemm_op_mt<float, base_device::DEVICE_CPU>;
template struct gemm_op_mt<double, base_device::DEVICE_CPU>;
template struct gemv_op_mt<std::complex<float>, base_device::DEVICE_CPU>;
template struct gemv_op_mt<std::complex<double>, base_device::DEVICE_CPU>;
template struct gemm_op_mt<std::complex<float>, base_device::DEVICE_CPU>;
template struct gemm_op_mt<std::complex<double>, base_device::DEVICE_CPU>;
#endif
} // namespace hsolver