Repository navigation
Expand file tree
/
Copy pathevolve_psi.cpp
More file actions
350 lines (296 loc) · 13.5 KB
/
Copy pathevolve_psi.cpp
File metadata and controls
350 lines (296 loc) · 13.5 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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
#include "evolve_psi.h"
#include "band_energy.h"
#include "middle_hamilt.h"
#include "norm_psi.h"
#include "propagator.h"
#include "solve_propagation.h"
#include "source_base/module_container/ATen/kernels/blas.h" // cuBLAS handle
#include "source_base/module_container/ATen/kernels/lapack.h" // cuSOLVER handle
#include "source_esolver/esolver_ks_lcao_tddft.h" // use gatherMatrix
#include "source_io/module_parameter/parameter.h"
#include "source_lcao/hamilt_lcao.h"
#include "source_pw/module_pwdft/global.h"
#include "upsi.h"
#include <complex>
namespace module_rt
{
void evolve_psi(const int nband,
const int nlocal,
const Parallel_Orbitals* pv,
hamilt::Hamilt<std::complex<double>>* p_hamilt,
std::complex<double>* psi_k,
std::complex<double>* psi_k_laststep,
std::complex<double>* H_laststep,
std::complex<double>* S_laststep,
double* ekb,
int htype,
int propagator,
std::ofstream& ofs_running,
const int print_matrix)
{
ModuleBase::TITLE("Evolve_psi", "evolve_psi");
// ofs_running << " Evolving electronic wave functions begins" << std::endl;
time_t time_start = time(nullptr);
// ofs_running << " Start Time : " << ctime(&time_start);
#ifdef __MPI
hamilt::MatrixBlock<std::complex<double>> h_mat;
hamilt::MatrixBlock<std::complex<double>> s_mat;
p_hamilt->matrix(h_mat, s_mat);
std::complex<double>* Stmp = new std::complex<double>[pv->nloc];
ModuleBase::GlobalFunc::ZEROS(Stmp, pv->nloc);
BlasConnector::copy(pv->nloc, s_mat.p, 1, Stmp, 1);
std::complex<double>* Htmp = new std::complex<double>[pv->nloc];
ModuleBase::GlobalFunc::ZEROS(Htmp, pv->nloc);
BlasConnector::copy(pv->nloc, h_mat.p, 1, Htmp, 1);
std::complex<double>* Hold = new std::complex<double>[pv->nloc];
ModuleBase::GlobalFunc::ZEROS(Hold, pv->nloc);
BlasConnector::copy(pv->nloc, h_mat.p, 1, Hold, 1);
std::complex<double>* U_operator = new std::complex<double>[pv->nloc];
ModuleBase::GlobalFunc::ZEROS(U_operator, pv->nloc);
// (1)->>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
/// @brief compute H(t+dt/2)
/// @input H_laststep, Htmp, print_matrix
/// @output Htmp
if (htype == 1 && propagator != 2)
{
half_Hmatrix(pv, nband, nlocal, Htmp, Stmp, H_laststep, S_laststep, ofs_running, print_matrix);
}
// (2)->>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
if (propagator != 3)
{
/// @brief compute U_operator
/// @input Stmp, Htmp, print_matrix
/// @output U_operator
Propagator prop(propagator, pv, PARAM.inp.td_dt);
prop.compute_propagator(nlocal, Stmp, Htmp, H_laststep, U_operator, ofs_running, print_matrix);
}
// (3)->>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
if (propagator != 3)
{
/// @brief apply U_operator to the wave function of the previous step for new wave function
/// @input U_operator, psi_k_laststep, print_matrix
/// @output psi_k
upsi(pv, nband, nlocal, U_operator, psi_k_laststep, psi_k, ofs_running, print_matrix);
}
else
{
/// @brief solve the propagation equation
/// @input Stmp, Htmp, psi_k_laststep
/// @output psi_k
solve_propagation(pv, nband, nlocal, PARAM.inp.td_dt, Stmp, Htmp, psi_k_laststep, psi_k);
}
// (4)->>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
/// @brief normalize psi_k
/// @input Stmp, psi_not_norm, psi_k, print_matrix
/// @output psi_k
norm_psi(pv, nband, nlocal, Stmp, psi_k, ofs_running, print_matrix);
// (5)->>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
/// @brief compute ekb
/// @input Htmp, psi_k
/// @output ekb
compute_ekb(pv, nband, nlocal, Hold, psi_k, ekb, ofs_running);
delete[] Stmp;
delete[] Htmp;
delete[] Hold;
delete[] U_operator;
#endif
time_t time_end = time(nullptr);
ModuleBase::GlobalFunc::OUT_TIME("evolve(std::complex)", time_start, time_end);
// ofs_running << " Evolving electronic wave functions ends" << std::endl;
return;
}
template <typename Device>
void evolve_psi_tensor(const int nband,
const int nlocal,
const Parallel_Orbitals* pv,
hamilt::Hamilt<std::complex<double>>* p_hamilt,
ct::Tensor& psi_k,
ct::Tensor& psi_k_laststep,
ct::Tensor& H_laststep,
ct::Tensor& S_laststep,
ct::Tensor& ekb,
int htype,
int propagator,
std::ofstream& ofs_running,
const int print_matrix,
const bool use_lapack)
{
// ct_device_type = ct::DeviceType::CpuDevice or ct::DeviceType::GpuDevice
ct::DeviceType ct_device_type = ct::DeviceTypeToEnum<Device>::value;
// ct_Device = ct::DEVICE_CPU or ct::DEVICE_GPU
using ct_Device = typename ct::PsiToContainer<Device>::type;
// Memory operations
using syncmem_complex_h2d_op
= base_device::memory::synchronize_memory_op<std::complex<double>, Device, base_device::DEVICE_CPU>;
#if ((defined __CUDA) /* || (defined __ROCM) */)
// Initialize cuBLAS & cuSOLVER handle
ct::kernels::createGpuSolverHandle();
ct::kernels::createGpuBlasHandle();
#endif // __CUDA
// ofs_running << " evolve_psi_tensor::start " << std::endl;
ModuleBase::TITLE("Evolve_psi", "evolve_psi");
time_t time_start = time(nullptr);
// ofs_running << " Start Time : " << ctime(&time_start);
#ifdef __MPI
hamilt::MatrixBlock<std::complex<double>> h_mat, s_mat;
p_hamilt->matrix(h_mat, s_mat);
// Create Tensor objects for temporary data and sync from host to device
const int len_HS = use_lapack ? nlocal * nlocal : pv->nloc;
ct::Tensor Stmp(ct::DataType::DT_COMPLEX_DOUBLE, ct_device_type, ct::TensorShape({len_HS}));
ct::Tensor Htmp(ct::DataType::DT_COMPLEX_DOUBLE, ct_device_type, ct::TensorShape({len_HS}));
ct::Tensor Hold(ct::DataType::DT_COMPLEX_DOUBLE, ct_device_type, ct::TensorShape({len_HS}));
if (use_lapack)
{
// Need to gather H and S matrix to root process here
int myid = 0;
int num_procs = 1;
MPI_Comm_rank(MPI_COMM_WORLD, &myid);
MPI_Comm_size(MPI_COMM_WORLD, &num_procs);
ModuleESolver::Matrix_g<std::complex<double>> h_mat_g, s_mat_g; // Global matrix structure
// Collect H matrix
ModuleESolver::gatherMatrix(myid, 0, h_mat, h_mat_g);
syncmem_complex_h2d_op()(Htmp.data<std::complex<double>>(), h_mat_g.p.get(), len_HS);
syncmem_complex_h2d_op()(Hold.data<std::complex<double>>(), h_mat_g.p.get(), len_HS);
// Collect S matrix
ModuleESolver::gatherMatrix(myid, 0, s_mat, s_mat_g);
syncmem_complex_h2d_op()(Stmp.data<std::complex<double>>(), s_mat_g.p.get(), len_HS);
}
else
{
// Original code
syncmem_complex_h2d_op()(Stmp.data<std::complex<double>>(), s_mat.p, len_HS);
syncmem_complex_h2d_op()(Htmp.data<std::complex<double>>(), h_mat.p, len_HS);
syncmem_complex_h2d_op()(Hold.data<std::complex<double>>(), h_mat.p, len_HS);
}
ct::Tensor U_operator(ct::DataType::DT_COMPLEX_DOUBLE, ct_device_type, ct::TensorShape({len_HS}));
U_operator.zero();
int myid = 0;
int root_proc = 0;
MPI_Comm_rank(MPI_COMM_WORLD, &myid);
// (1)->>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
/// @brief compute H(t+dt/2)
/// @input H_laststep, Htmp, print_matrix
/// @output Htmp
if (htype == 1 && propagator != 2)
{
if (!use_lapack)
{
half_Hmatrix_tensor(pv, nband, nlocal, Htmp, Stmp, H_laststep, S_laststep, ofs_running, print_matrix);
}
else
{
if (myid == root_proc)
{
half_Hmatrix_tensor_lapack<Device>(pv,
nband,
nlocal,
Htmp,
Stmp,
H_laststep,
S_laststep,
ofs_running,
print_matrix);
}
}
}
// (2)->>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
/// @brief compute U_operator
/// @input Stmp, Htmp, print_matrix
/// @output U_operator
Propagator prop(propagator, pv, PARAM.mdp.md_dt);
prop.compute_propagator_tensor<Device>(nlocal,
Stmp,
Htmp,
H_laststep,
U_operator,
ofs_running,
print_matrix,
use_lapack);
// (3)->>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
/// @brief apply U_operator to the wave function of the previous step for new wave function
/// @input U_operator, psi_k_laststep, print_matrix
/// @output psi_k
if (!use_lapack)
{
upsi_tensor(pv, nband, nlocal, U_operator, psi_k_laststep, psi_k, ofs_running, print_matrix);
}
else
{
if (myid == root_proc)
{
upsi_tensor_lapack<Device>(pv, nband, nlocal, U_operator, psi_k_laststep, psi_k, ofs_running, print_matrix);
}
}
// (4)->>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
/// @brief normalize psi_k
/// @input Stmp, psi_not_norm, psi_k, print_matrix
/// @output psi_k
if (!use_lapack)
{
norm_psi_tensor(pv, nband, nlocal, Stmp, psi_k, ofs_running, print_matrix);
}
else
{
if (myid == root_proc)
{
norm_psi_tensor_lapack<Device>(pv, nband, nlocal, Stmp, psi_k, ofs_running, print_matrix);
}
}
// (5)->>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
/// @brief compute ekb
/// @input Htmp, psi_k
/// @output ekb
if (!use_lapack)
{
compute_ekb_tensor(pv, nband, nlocal, Hold, psi_k, ekb, ofs_running);
}
else
{
if (myid == root_proc)
{
compute_ekb_tensor_lapack<Device>(pv, nband, nlocal, Hold, psi_k, ekb, ofs_running);
}
}
#endif // __MPI
time_t time_end = time(nullptr);
ModuleBase::GlobalFunc::OUT_TIME("evolve(std::complex)", time_start, time_end);
// ofs_running << " evolve_psi_tensor::end " << std::endl;
#if ((defined __CUDA) /* || (defined __ROCM) */)
// Destroy cuBLAS & cuSOLVER handle
ct::kernels::destroyGpuSolverHandle();
ct::kernels::destroyGpuBlasHandle();
#endif // __CUDA
return;
}
// Explicit instantiation of template functions
template void evolve_psi_tensor<base_device::DEVICE_CPU>(const int nband,
const int nlocal,
const Parallel_Orbitals* pv,
hamilt::Hamilt<std::complex<double>>* p_hamilt,
ct::Tensor& psi_k,
ct::Tensor& psi_k_laststep,
ct::Tensor& H_laststep,
ct::Tensor& S_laststep,
ct::Tensor& ekb,
int htype,
int propagator,
std::ofstream& ofs_running,
const int print_matrix,
const bool use_lapack);
#if ((defined __CUDA) /* || (defined __ROCM) */)
template void evolve_psi_tensor<base_device::DEVICE_GPU>(const int nband,
const int nlocal,
const Parallel_Orbitals* pv,
hamilt::Hamilt<std::complex<double>>* p_hamilt,
ct::Tensor& psi_k,
ct::Tensor& psi_k_laststep,
ct::Tensor& H_laststep,
ct::Tensor& S_laststep,
ct::Tensor& ekb,
int htype,
int propagator,
std::ofstream& ofs_running,
const int print_matrix,
const bool use_lapack);
#endif // __CUDA
} // namespace module_rt