Repository navigation
Expand file tree
/
Copy pathpw_transform.cpp
More file actions
320 lines (298 loc) · 11.4 KB
/
Copy pathpw_transform.cpp
File metadata and controls
320 lines (298 loc) · 11.4 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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
#include "source_base/global_function.h"
#include "source_base/timer.h"
#include "source_basis/module_pw/kernels/pw_op.h"
#include "module_fft/fft_bundle.h"
#include "pw_basis.h"
#include "pw_gatherscatter.h"
#include <cassert>
#include <complex>
namespace ModulePW
{
// const base_device::DEVICE_CPU* PW_Basis::get_default_device_ctx() {
// static const base_device::DEVICE_CPU* default_device_cpu;
// return default_device_cpu;
// }
/**
* @brief transform real space to reciprocal space
* @details c(g)=\int dr*f(r)*exp(-ig*r)
* Here we calculate c(g)
* @param in: (nplane,ny,nx), std::complex<double> data
* @param out: (nz, ns), std::complex<double> data
*/
template <typename FPTYPE>
void PW_Basis::real2recip(const std::complex<FPTYPE>* in,
std::complex<FPTYPE>* out,
const bool add,
const FPTYPE factor) const
{
ModuleBase::timer::tick(this->classname, "real2recip");
assert(this->gamma_only == false);
#ifdef _OPENMP
#pragma omp parallel for schedule(static, 4096 / sizeof(FPTYPE))
#endif
for (int ir = 0; ir < this->nrxx; ++ir)
{
this->fft_bundle.get_auxr_data<FPTYPE>()[ir] = in[ir];
}
this->fft_bundle.fftxyfor(fft_bundle.get_auxr_data<FPTYPE>(), fft_bundle.get_auxr_data<FPTYPE>());
this->gatherp_scatters(this->fft_bundle.get_auxr_data<FPTYPE>(), this->fft_bundle.get_auxg_data<FPTYPE>());
this->fft_bundle.fftzfor(fft_bundle.get_auxg_data<FPTYPE>(), fft_bundle.get_auxg_data<FPTYPE>());
if (add)
{
FPTYPE tmpfac = factor / FPTYPE(this->nxyz);
#ifdef _OPENMP
#pragma omp parallel for schedule(static, 4096 / sizeof(FPTYPE))
#endif
for (int ig = 0; ig < this->npw; ++ig)
{
out[ig] += tmpfac * this->fft_bundle.get_auxg_data<FPTYPE>()[this->ig2isz[ig]];
}
}
else
{
FPTYPE tmpfac = 1.0 / FPTYPE(this->nxyz);
#ifdef _OPENMP
#pragma omp parallel for schedule(static, 4096 / sizeof(FPTYPE))
#endif
for (int ig = 0; ig < this->npw; ++ig)
{
out[ig] = tmpfac * this->fft_bundle.get_auxg_data<FPTYPE>()[this->ig2isz[ig]];
}
}
ModuleBase::timer::tick(this->classname, "real2recip");
}
/**
* @brief transform real space to reciprocal space
* @details c(g)=\int dr*f(r)*exp(-ig*r)
* Here we calculate c(g)
* @param in: (nplane,ny,nx), double data
* @param out: (nz, ns), std::complex<double> data
*/
template <typename FPTYPE>
void PW_Basis::real2recip(const FPTYPE* in, std::complex<FPTYPE>* out, const bool add, const FPTYPE factor) const
{
ModuleBase::timer::tick(this->classname, "real2recip");
if (this->gamma_only)
{
const int npy = this->ny * this->nplane;
#ifdef _OPENMP
#pragma omp parallel for collapse(2) schedule(static, 4096 / sizeof(FPTYPE))
#endif
for (int ix = 0; ix < this->nx; ++ix)
{
for (int ipy = 0; ipy < npy; ++ipy)
{
this->fft_bundle.get_rspace_data<FPTYPE>()[ix * npy + ipy] = in[ix * npy + ipy];
}
}
this->fft_bundle.fftxyr2c(fft_bundle.get_rspace_data<FPTYPE>(), fft_bundle.get_auxr_data<FPTYPE>());
}
else
{
#ifdef _OPENMP
#pragma omp parallel for schedule(static, 4096 / sizeof(FPTYPE))
#endif
for (int ir = 0; ir < this->nrxx; ++ir)
{
this->fft_bundle.get_auxr_data<FPTYPE>()[ir] = std::complex<FPTYPE>(in[ir], 0);
}
this->fft_bundle.fftxyfor(fft_bundle.get_auxr_data<FPTYPE>(), fft_bundle.get_auxr_data<FPTYPE>());
}
this->gatherp_scatters(this->fft_bundle.get_auxr_data<FPTYPE>(), this->fft_bundle.get_auxg_data<FPTYPE>());
this->fft_bundle.fftzfor(fft_bundle.get_auxg_data<FPTYPE>(), fft_bundle.get_auxg_data<FPTYPE>());
if (add)
{
FPTYPE tmpfac = factor / FPTYPE(this->nxyz);
#ifdef _OPENMP
#pragma omp parallel for schedule(static, 4096 / sizeof(FPTYPE))
#endif
for (int ig = 0; ig < this->npw; ++ig)
{
out[ig] += tmpfac * this->fft_bundle.get_auxg_data<FPTYPE>()[this->ig2isz[ig]];
}
}
else
{
FPTYPE tmpfac = 1.0 / FPTYPE(this->nxyz);
#ifdef _OPENMP
#pragma omp parallel for schedule(static, 4096 / sizeof(FPTYPE))
#endif
for (int ig = 0; ig < this->npw; ++ig)
{
out[ig] = tmpfac * this->fft_bundle.get_auxg_data<FPTYPE>()[this->ig2isz[ig]];
}
}
ModuleBase::timer::tick(this->classname, "real2recip");
}
/**
* @brief transform reciprocal space to real space
* @details f(r)=1/V * \sum_{g} c(g)*exp(ig*r)
* Here we calculate f(r)
* @param in: (nz,ns), std::complex<double>
* @param out: (nplane, ny, nx), std::complex<double>
*/
template <typename FPTYPE>
void PW_Basis::recip2real(const std::complex<FPTYPE>* in,
std::complex<FPTYPE>* out,
const bool add,
const FPTYPE factor) const
{
ModuleBase::timer::tick(this->classname, "recip2real");
assert(this->gamma_only == false);
#ifdef _OPENMP
#pragma omp parallel for schedule(static, 4096 / sizeof(FPTYPE))
#endif
for (int i = 0; i < this->nst * this->nz; ++i)
{
fft_bundle.get_auxg_data<FPTYPE>()[i] = std::complex<FPTYPE>(0, 0);
}
#ifdef _OPENMP
#pragma omp parallel for schedule(static, 4096 / sizeof(FPTYPE))
#endif
for (int ig = 0; ig < this->npw; ++ig)
{
this->fft_bundle.get_auxg_data<FPTYPE>()[this->ig2isz[ig]] = in[ig];
}
this->fft_bundle.fftzbac(fft_bundle.get_auxg_data<FPTYPE>(), fft_bundle.get_auxg_data<FPTYPE>());
this->gathers_scatterp(this->fft_bundle.get_auxg_data<FPTYPE>(), this->fft_bundle.get_auxr_data<FPTYPE>());
this->fft_bundle.fftxybac(fft_bundle.get_auxr_data<FPTYPE>(), fft_bundle.get_auxr_data<FPTYPE>());
if (add)
{
#ifdef _OPENMP
#pragma omp parallel for schedule(static, 4096 / sizeof(FPTYPE))
#endif
for (int ir = 0; ir < this->nrxx; ++ir)
{
out[ir] += factor * this->fft_bundle.get_auxr_data<FPTYPE>()[ir];
}
}
else
{
#ifdef _OPENMP
#pragma omp parallel for schedule(static, 4096 / sizeof(FPTYPE))
#endif
for (int ir = 0; ir < this->nrxx; ++ir)
{
out[ir] = this->fft_bundle.get_auxr_data<FPTYPE>()[ir];
}
}
ModuleBase::timer::tick(this->classname, "recip2real");
}
/**
* @brief transform reciprocal space to real space
* @details f(r)=1/V * \sum_{g} c(g)*exp(ig*r)
* Here we calculate f(r)
* @param in: (nz,ns), std::complex<double>
* @param out: (nplane, ny, nx), double
*/
template <typename FPTYPE>
void PW_Basis::recip2real(const std::complex<FPTYPE>* in, FPTYPE* out, const bool add, const FPTYPE factor) const
{
ModuleBase::timer::tick(this->classname, "recip2real");
#ifdef _OPENMP
#pragma omp parallel for schedule(static, 4096 / sizeof(FPTYPE))
#endif
for (int i = 0; i < this->nst * this->nz; ++i)
{
fft_bundle.get_auxg_data<FPTYPE>()[i] = std::complex<FPTYPE>(0, 0);
}
#ifdef _OPENMP
#pragma omp parallel for schedule(static, 4096 / sizeof(FPTYPE))
#endif
for (int ig = 0; ig < this->npw; ++ig)
{
this->fft_bundle.get_auxg_data<FPTYPE>()[this->ig2isz[ig]] = in[ig];
}
this->fft_bundle.fftzbac(fft_bundle.get_auxg_data<FPTYPE>(), fft_bundle.get_auxg_data<FPTYPE>());
this->gathers_scatterp(this->fft_bundle.get_auxg_data<FPTYPE>(), this->fft_bundle.get_auxr_data<FPTYPE>());
if (this->gamma_only)
{
this->fft_bundle.fftxyc2r(fft_bundle.get_auxr_data<FPTYPE>(), fft_bundle.get_rspace_data<FPTYPE>());
// r2c in place
const int npy = this->ny * this->nplane;
if (add)
{
#ifdef _OPENMP
#pragma omp parallel for collapse(2) schedule(static, 4096 / sizeof(FPTYPE))
#endif
for (int ix = 0; ix < this->nx; ++ix)
{
for (int ipy = 0; ipy < npy; ++ipy)
{
out[ix * npy + ipy] += factor * this->fft_bundle.get_rspace_data<FPTYPE>()[ix * npy + ipy];
}
}
}
else
{
#ifdef _OPENMP
#pragma omp parallel for collapse(2) schedule(static, 4096 / sizeof(FPTYPE))
#endif
for (int ix = 0; ix < this->nx; ++ix)
{
for (int ipy = 0; ipy < npy; ++ipy)
{
out[ix * npy + ipy] = this->fft_bundle.get_rspace_data<FPTYPE>()[ix * npy + ipy];
}
}
}
}
else
{
this->fft_bundle.fftxybac(fft_bundle.get_auxr_data<FPTYPE>(), fft_bundle.get_auxr_data<FPTYPE>());
if (add)
{
#ifdef _OPENMP
#pragma omp parallel for schedule(static, 4096 / sizeof(FPTYPE))
#endif
for (int ir = 0; ir < this->nrxx; ++ir)
{
out[ir] += factor * this->fft_bundle.get_auxr_data<FPTYPE>()[ir].real();
}
}
else
{
#ifdef _OPENMP
#pragma omp parallel for schedule(static, 4096 / sizeof(FPTYPE))
#endif
for (int ir = 0; ir < this->nrxx; ++ir)
{
out[ir] = this->fft_bundle.get_auxr_data<FPTYPE>()[ir].real();
}
}
}
ModuleBase::timer::tick(this->classname, "recip2real");
}
template void PW_Basis::real2recip<float>(const float* in,
std::complex<float>* out,
const bool add,
const float factor) const; // in:(nplane,nx*ny) ; out(nz, ns)
template void PW_Basis::real2recip<float>(const std::complex<float>* in,
std::complex<float>* out,
const bool add,
const float factor) const; // in:(nplane,nx*ny) ; out(nz, ns)
template void PW_Basis::recip2real<float>(const std::complex<float>* in,
float* out,
const bool add,
const float factor) const; // in:(nz, ns) ; out(nplane,nx*ny)
template void PW_Basis::recip2real<float>(const std::complex<float>* in,
std::complex<float>* out,
const bool add,
const float factor) const;
template void PW_Basis::real2recip<double>(const double* in,
std::complex<double>* out,
const bool add,
const double factor) const; // in:(nplane,nx*ny) ; out(nz, ns)
template void PW_Basis::real2recip<double>(const std::complex<double>* in,
std::complex<double>* out,
const bool add,
const double factor) const; // in:(nplane,nx*ny) ; out(nz, ns)
template void PW_Basis::recip2real<double>(const std::complex<double>* in,
double* out,
const bool add,
const double factor) const; // in:(nz, ns) ; out(nplane,nx*ny)
template void PW_Basis::recip2real<double>(const std::complex<double>* in,
std::complex<double>* out,
const bool add,
const double factor) const;
} // namespace ModulePW