forked from deepmodeling/abacus-develop
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmath_kernel_op_vec.cpp
More file actions
200 lines (178 loc) · 5.97 KB
/
Copy pathmath_kernel_op_vec.cpp
File metadata and controls
200 lines (178 loc) · 5.97 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
193
194
195
196
197
198
199
200
#include "source_base/kernels/math_kernel_op.h"
#include "source_base/module_external/blas_connector.h"
#include "source_base/parallel_reduce.h"
namespace ModuleBase
{
template <typename FPTYPE>
struct scal_op<FPTYPE, base_device::DEVICE_CPU>
{
void operator()(const int& N,
const std::complex<FPTYPE>* alpha,
std::complex<FPTYPE>* X,
const int& incx)
{
BlasConnector::scal(N, *alpha, X, incx);
}
};
template <typename T>
struct vector_mul_real_op<T, base_device::DEVICE_CPU>
{
using Real = typename GetTypeReal<T>::type;
void operator()(const int dim, T* result, const T* vector, const Real constant)
{
#ifdef _OPENMP
#pragma omp parallel for schedule(static)
#endif
for (int i = 0; i < dim; i++)
{
result[i] = vector[i] * constant;
}
}
};
template <typename T>
struct vector_mul_vector_op<T, base_device::DEVICE_CPU>
{
using Real = typename GetTypeReal<T>::type;
void operator()(const int& dim, T* result, const T* vector1, const Real* vector2, const bool& add)
{
if (add)
{
#ifdef _OPENMP
#pragma omp parallel for schedule(static)
#endif
for (int i = 0; i < dim; i++)
{
result[i] += vector1[i] * vector2[i];
}
}
else
{
#ifdef _OPENMP
#pragma omp parallel for schedule(static)
#endif
for (int i = 0; i < dim; i++)
{
result[i] = vector1[i] * vector2[i];
}
}
}
};
template <typename T>
struct vector_div_constant_op<T, base_device::DEVICE_CPU>
{
using Real = typename GetTypeReal<T>::type;
void operator()(const int& dim, T* result, const T* vector, const Real constant)
{
#ifdef _OPENMP
#pragma omp parallel for schedule(static)
#endif
for (int i = 0; i < dim; i++)
{
result[i] = vector[i] / constant;
}
}
};
template <typename T>
struct vector_div_vector_op<T, base_device::DEVICE_CPU>
{
using Real = typename GetTypeReal<T>::type;
void operator()(const int& dim, T* result, const T* vector1, const Real* vector2)
{
#ifdef _OPENMP
#pragma omp parallel for schedule(static)
#endif
for (int i = 0; i < dim; i++)
{
result[i] = vector1[i] / vector2[i];
}
}
};
template <typename T>
struct axpy_op<T, base_device::DEVICE_CPU>
{
void operator()(const int& dim,
const T* alpha,
const T* X,
const int& incX,
T* Y,
const int& incY)
{
BlasConnector::axpy(dim, *alpha, X, incX, Y, incY);
}
};
template <typename T>
struct vector_add_vector_op<T, base_device::DEVICE_CPU>
{
using Real = typename GetTypeReal<T>::type;
void operator()(const int& dim,
T* result,
const T* vector1,
const Real constant1,
const T* vector2,
const Real constant2)
{
#ifdef _OPENMP
#pragma omp parallel for schedule(static)
#endif
for (int i = 0; i < dim; i++)
{
result[i] = vector1[i] * constant1 + vector2[i] * constant2;
}
}
};
template <typename FPTYPE>
struct dot_real_op<FPTYPE, base_device::DEVICE_CPU>
{
FPTYPE operator()(const int& dim, const FPTYPE* psi_L, const FPTYPE* psi_R, const bool reduce)
{
FPTYPE result = BlasConnector::dot(dim, psi_L, 1, psi_R, 1);
if (reduce)
{
Parallel_Reduce::reduce_pool(result);
}
return result;
}
};
template <typename FPTYPE>
struct dot_real_op<std::complex<FPTYPE>, base_device::DEVICE_CPU>
{
FPTYPE operator()(const int& dim,
const std::complex<FPTYPE>* psi_L,
const std::complex<FPTYPE>* psi_R,
const bool reduce)
{
// Note that ddot_(2*dim,a,1,b,1) = REAL( zdotc_(dim,a,1,b,1) )
const FPTYPE* pL = reinterpret_cast<const FPTYPE*>(psi_L);
const FPTYPE* pR = reinterpret_cast<const FPTYPE*>(psi_R);
FPTYPE result = BlasConnector::dot(2 * dim, pL, 1, pR, 1);
if (reduce)
{
Parallel_Reduce::reduce_pool(result);
}
return result;
}
};
template struct scal_op<float, base_device::DEVICE_CPU>;
template struct scal_op<double, base_device::DEVICE_CPU>;
template struct vector_mul_real_op<std::complex<float>, base_device::DEVICE_CPU>;
template struct vector_mul_real_op<double, base_device::DEVICE_CPU>;
template struct vector_mul_real_op<std::complex<double>, base_device::DEVICE_CPU>;
template struct vector_mul_vector_op<std::complex<float>, base_device::DEVICE_CPU>;
template struct vector_mul_vector_op<double, base_device::DEVICE_CPU>;
template struct vector_mul_vector_op<std::complex<double>, base_device::DEVICE_CPU>;
template struct vector_div_constant_op<std::complex<float>, base_device::DEVICE_CPU>;
template struct vector_div_constant_op<double, base_device::DEVICE_CPU>;
template struct vector_div_constant_op<std::complex<double>, base_device::DEVICE_CPU>;
template struct vector_div_vector_op<std::complex<float>, base_device::DEVICE_CPU>;
template struct vector_div_vector_op<double, base_device::DEVICE_CPU>;
template struct vector_div_vector_op<std::complex<double>, base_device::DEVICE_CPU>;
template struct axpy_op<std::complex<float>, base_device::DEVICE_CPU>;
template struct axpy_op<std::complex<double>, base_device::DEVICE_CPU>;
template struct axpy_op<double, base_device::DEVICE_CPU>;
template struct vector_add_vector_op<std::complex<float>, base_device::DEVICE_CPU>;
template struct vector_add_vector_op<double, base_device::DEVICE_CPU>;
template struct vector_add_vector_op<std::complex<double>, base_device::DEVICE_CPU>;
template struct dot_real_op<std::complex<float>, base_device::DEVICE_CPU>;
template struct dot_real_op<std::complex<double>, base_device::DEVICE_CPU>;
template struct dot_real_op<double, base_device::DEVICE_CPU>;
} // namespace ModuleBase