Repository navigation
Expand file tree
/
Copy pathpsi_init.cpp
More file actions
273 lines (253 loc) · 10.5 KB
/
Copy pathpsi_init.cpp
File metadata and controls
273 lines (253 loc) · 10.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
#include "psi_init.h"
#include "source_base/macros.h"
#include "source_base/memory.h"
#include "source_base/parallel_device.h"
#include "source_base/timer.h"
#include "source_base/tool_quit.h"
#include "source_hsolver/diago_iter_assist.h"
#include "source_io/module_parameter/parameter.h"
#include "source_psi/psi_initializer_atomic.h"
#include "source_psi/psi_initializer_atomic_random.h"
#include "source_psi/psi_initializer_file.h"
#include "source_psi/psi_initializer_nao.h"
#include "source_psi/psi_initializer_nao_random.h"
#include "source_psi/psi_initializer_random.h"
namespace psi
{
template <typename T, typename Device>
PSIInit<T, Device>::PSIInit(const std::string& init_wfc_in,
const std::string& ks_solver_in,
const std::string& basis_type_in,
const int& rank_in,
const UnitCell& ucell_in,
const Structure_Factor& sf_in,
const K_Vectors& kv_in,
const pseudopot_cell_vnl& nlpp_in,
const ModulePW::PW_Basis_K& pw_wfc_in)
: ucell(ucell_in), sf(sf_in), nlpp(nlpp_in), kv(kv_in), pw_wfc(pw_wfc_in), rank(rank_in)
{
this->init_wfc = init_wfc_in;
this->ks_solver = ks_solver_in;
this->basis_type = basis_type_in;
}
template <typename T, typename Device>
void PSIInit<T, Device>::prepare_init(const int& random_seed)
{
// under restriction of C++11, std::unique_ptr can not be allocate via std::make_unique
// use new instead, but will cause asymmetric allocation and deallocation, in literal aspect
ModuleBase::timer::tick("PSIInit", "prepare_init");
this->psi_initer.reset();
if (this->init_wfc == "random")
{
this->psi_initer = std::unique_ptr<psi_initializer<T>>(new psi_initializer_random<T>());
}
else if (this->init_wfc == "file")
{
this->psi_initer = std::unique_ptr<psi_initializer<T>>(new psi_initializer_file<T>());
}
else if ((this->init_wfc.substr(0, 6) == "atomic") && (this->ucell.natomwfc == 0))
{
this->psi_initer = std::unique_ptr<psi_initializer<T>>(new psi_initializer_random<T>());
}
else if (this->init_wfc == "atomic"
|| (this->init_wfc == "atomic+random" && this->ucell.natomwfc < PARAM.inp.nbands))
{
this->psi_initer = std::unique_ptr<psi_initializer<T>>(new psi_initializer_atomic<T>());
}
else if (this->init_wfc == "atomic+random")
{
this->psi_initer = std::unique_ptr<psi_initializer<T>>(new psi_initializer_atomic_random<T>());
}
else if (this->init_wfc == "nao")
{
this->psi_initer = std::unique_ptr<psi_initializer<T>>(new psi_initializer_nao<T>());
}
else if (this->init_wfc == "nao+random")
{
this->psi_initer = std::unique_ptr<psi_initializer<T>>(new psi_initializer_nao_random<T>());
}
else
{
ModuleBase::WARNING_QUIT("PSIInit::prepare_init", "for new psi initializer, init_wfc type not supported");
}
this->psi_initer->initialize(&sf, &pw_wfc, &ucell, &kv, random_seed, &nlpp, rank);
this->psi_initer->tabulate();
ModuleBase::timer::tick("PSIInit", "prepare_init");
}
template <typename T, typename Device>
void PSIInit<T, Device>::initialize_psi(Psi<std::complex<double>>* psi,
psi::Psi<T, Device>* kspw_psi,
hamilt::Hamilt<T, Device>* p_hamilt,
std::ofstream& ofs_running)
{
if (kspw_psi->get_nbands() == 0 || (!PARAM.globalv.ks_run))
{
return;
}
if (this->basis_type == "lcao_in_pw")
{
return;
}
ModuleBase::timer::tick("PSIInit", "initialize_psi");
const int nbands_start = this->psi_initer->nbands_start();
const int nbands_l = psi->get_nbands();
const int nbasis = psi->get_nbasis();
const bool not_equal = (nbands_start != nbands_l);
Psi<T>* psi_cpu = reinterpret_cast<psi::Psi<T>*>(psi);
Psi<T, Device>* psi_device = kspw_psi;
bool fill = PARAM.inp.ks_solver != "bpcg" || GlobalV::MY_BNDGROUP == 0;
if (fill)
{
if (not_equal)
{
psi_cpu = new Psi<T>(1, nbands_start, nbasis, nbasis, true);
psi_device = PARAM.inp.device == "gpu" ? new psi::Psi<T, Device>(psi_cpu[0])
: reinterpret_cast<psi::Psi<T, Device>*>(psi_cpu);
}
else if (PARAM.inp.precision == "single")
{
if (PARAM.inp.device == "cpu")
{
psi_cpu = reinterpret_cast<psi::Psi<T>*>(kspw_psi);
psi_device = kspw_psi;
}
else
{
psi_cpu = new Psi<T>(1, nbands_start, nbasis, nbasis, true);
psi_device = kspw_psi;
}
}
}
// loop over kpoints, make it possible to only allocate memory for psig at the only one kpt
// like (1, nbands, npwx), in which npwx is the maximal npw of all kpoints
for (int ik = 0; ik < this->pw_wfc.nks; ik++)
{
if(PARAM.inp.use_k_continuity && ik > 0) continue;
//! Fix the wavefunction to initialize at given kpoint
psi->fix_k(ik);
kspw_psi->fix_k(ik);
//! Update Hamiltonian from other kpoint to the given one
p_hamilt->updateHk(ik);
if (fill)
{
//! initialize psi_cpu
this->psi_initer->init_psig(psi_cpu->get_pointer(), ik);
if (psi_device->get_pointer() != psi_cpu->get_pointer())
{
syncmem_h2d_op()(psi_device->get_pointer(), psi_cpu->get_pointer(), nbands_start * nbasis);
}
if (this->ks_solver == "cg")
{
std::vector<typename GetTypeReal<T>::type> etatom(nbands_start, 0.0);
if (not_equal)
{
// for diagH_subspace_init, psi_device->get_pointer() and kspw_psi->get_pointer() should be
// different
hsolver::DiagoIterAssist<T, Device>::diagH_subspace_init(p_hamilt,
psi_device->get_pointer(),
nbands_start,
nbasis,
*(kspw_psi),
etatom.data());
}
else
{
// for diagH_subspace, psi_device->get_pointer() and kspw_psi->get_pointer() can be the same
hsolver::DiagoIterAssist<T, Device>::diagH_subspace(p_hamilt,
*psi_device,
*kspw_psi,
etatom.data(),
nbands_start);
}
}
else // dav, bpcg
{
if (psi_device->get_pointer() != kspw_psi->get_pointer())
{
syncmem_complex_op()(kspw_psi->get_pointer(), psi_device->get_pointer(), nbands_l * nbasis);
}
}
}
#ifdef __MPI
if (PARAM.inp.ks_solver == "bpcg" && PARAM.inp.bndpar > 1)
{
std::vector<int> sendcounts(PARAM.inp.bndpar);
std::vector<int> displs(PARAM.inp.bndpar);
MPI_Allgather(&nbands_l, 1, MPI_INT, sendcounts.data(), 1, MPI_INT, BP_WORLD);
displs[0] = 0;
sendcounts[0] *= nbasis;
for (int i = 1; i < PARAM.inp.bndpar; i++)
{
sendcounts[i] *= nbasis;
displs[i] = displs[i - 1] + sendcounts[i - 1];
}
if (GlobalV::MY_BNDGROUP == 0)
{
for (int ip = 1; ip < PARAM.inp.bndpar; ++ip)
{
Parallel_Common::send_data(psi_cpu->get_pointer() + displs[ip], sendcounts[ip], ip, 0, BP_WORLD);
}
}
else
{
MPI_Status status;
Parallel_Common::recv_dev<T, Device>(kspw_psi->get_pointer(), nbands_l * nbasis, 0, 0, BP_WORLD, &status);
}
}
#endif
} // end k-point loop
if (fill)
{
if (not_equal)
{
delete psi_cpu;
if (PARAM.inp.device == "gpu")
{
delete psi_device;
}
}
else if (PARAM.inp.precision == "single" && PARAM.inp.device == "gpu")
{
delete psi_cpu;
}
}
ModuleBase::timer::tick("PSIInit", "initialize_psi");
}
template <typename T, typename Device>
void PSIInit<T, Device>::initialize_lcao_in_pw(Psi<T>* psi_local, std::ofstream& ofs_running)
{
ofs_running << " START WAVEFUNCTION: LCAO_IN_PW, psi initialization skipped " << std::endl;
assert(this->psi_initer->method() == "nao");
for (int ik = 0; ik < this->pw_wfc.nks; ik++)
{
psi_local->fix_k(ik);
this->psi_initer->init_psig(psi_local->get_pointer(), ik);
}
}
void allocate_psi(Psi<std::complex<double>>*& psi,
const int& nks,
const std::vector<int>& ngk,
const int& nbands,
const int& npwx)
{
assert(npwx > 0);
assert(nks > 0);
ModuleBase::GlobalFunc::OUT(GlobalV::ofs_running, "npwx", npwx);
delete psi;
int nks2 = nks;
if (PARAM.inp.calculation == "nscf" && PARAM.inp.mem_saver == 1)
{
nks2 = 1;
}
psi = new psi::Psi<std::complex<double>>(nks2, nbands, npwx * PARAM.globalv.npol, ngk, true);
const size_t memory_cost = sizeof(std::complex<double>) * nks2 * nbands * (PARAM.globalv.npol * npwx);
std::cout << " MEMORY FOR PSI (MB) : " << static_cast<double>(memory_cost) / 1024.0 / 1024.0 << std::endl;
ModuleBase::Memory::record("Psi_PW", memory_cost);
}
template class PSIInit<std::complex<float>, base_device::DEVICE_CPU>;
template class PSIInit<std::complex<double>, base_device::DEVICE_CPU>;
#if ((defined __CUDA) || (defined __ROCM))
template class PSIInit<std::complex<float>, base_device::DEVICE_GPU>;
template class PSIInit<std::complex<double>, base_device::DEVICE_GPU>;
#endif
} // namespace psi