Repository navigation
Expand file tree
/
Copy pathmiddle_hamilt.cpp
More file actions
289 lines (269 loc) · 11.9 KB
/
Copy pathmiddle_hamilt.cpp
File metadata and controls
289 lines (269 loc) · 11.9 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
#include "middle_hamilt.h"
#include "source_base/module_container/ATen/kernels/blas.h"
#include "source_base/module_device/memory_op.h" // memory operations
#include "source_base/module_external/scalapack_connector.h"
#include <complex>
#include <iostream>
namespace module_rt
{
#ifdef __MPI
void half_Hmatrix(const Parallel_Orbitals* pv,
const int nband,
const int nlocal,
std::complex<double>* Htmp,
std::complex<double>* Stmp,
const std::complex<double>* H_laststep,
const std::complex<double>* S_laststep,
std::ofstream& ofs_running,
const int print_matrix)
{
if (print_matrix)
{
ofs_running << std::setprecision(10);
ofs_running << std::endl;
ofs_running << " H(t+dt) :" << std::endl;
for (int i = 0; i < pv->nrow; i++)
{
const int in = i * pv->ncol;
for (int j = 0; j < pv->ncol; j++)
{
ofs_running << Htmp[in + j].real() << "+" << Htmp[in + j].imag() << "i ";
}
ofs_running << std::endl;
}
ofs_running << std::endl;
ofs_running << std::endl;
ofs_running << " H(t):" << std::endl;
for (int i = 0; i < pv->nrow; i++)
{
const int in = i * pv->ncol;
for (int j = 0; j < pv->ncol; j++)
{
ofs_running << H_laststep[in + j].real() << "+" << H_laststep[in + j].imag() << "i ";
}
ofs_running << std::endl;
}
ofs_running << std::endl;
}
std::complex<double> alpha = {0.5, 0.0};
std::complex<double> beta = {0.5, 0.0};
ScalapackConnector::geadd('N', nlocal, nlocal, alpha, H_laststep, 1, 1, pv->desc, beta, Htmp, 1, 1, pv->desc);
ScalapackConnector::geadd('N', nlocal, nlocal, alpha, S_laststep, 1, 1, pv->desc, beta, Stmp, 1, 1, pv->desc);
if (print_matrix)
{
ofs_running << std::endl;
ofs_running << " H (t+dt/2) :" << std::endl;
for (int i = 0; i < pv->nrow; i++)
{
const int in = i * pv->ncol;
for (int j = 0; j < pv->ncol; j++)
{
ofs_running << Htmp[in + j].real() << "+" << Htmp[in + j].imag() << "i ";
}
ofs_running << std::endl;
}
ofs_running << std::endl;
}
}
void half_Hmatrix_tensor(const Parallel_Orbitals* pv,
const int nband,
const int nlocal,
ct::Tensor& Htmp,
ct::Tensor& Stmp,
const ct::Tensor& H_laststep,
const ct::Tensor& S_laststep,
std::ofstream& ofs_running,
const int print_matrix)
{
if (print_matrix)
{
ofs_running << std::setprecision(10);
ofs_running << std::endl;
ofs_running << " H(t+dt) :" << std::endl;
for (int i = 0; i < pv->nrow; i++)
{
const int in = i * pv->ncol;
for (int j = 0; j < pv->ncol; j++)
{
ofs_running << Htmp.data<std::complex<double>>()[in + j].real() << "+"
<< Htmp.data<std::complex<double>>()[in + j].imag() << "i ";
}
ofs_running << std::endl;
}
ofs_running << std::endl;
ofs_running << std::endl;
ofs_running << " H(t):" << std::endl;
for (int i = 0; i < pv->nrow; i++)
{
const int in = i * pv->ncol;
for (int j = 0; j < pv->ncol; j++)
{
ofs_running << H_laststep.data<std::complex<double>>()[in + j].real() << "+"
<< H_laststep.data<std::complex<double>>()[in + j].imag() << "i ";
}
ofs_running << std::endl;
}
ofs_running << std::endl;
}
std::complex<double> alpha = {0.5, 0.0};
std::complex<double> beta = {0.5, 0.0};
// Perform the operation Htmp = alpha * H_laststep + beta * Htmp
ScalapackConnector::geadd('N',
nlocal,
nlocal,
alpha,
H_laststep.data<std::complex<double>>(),
1,
1,
pv->desc,
beta,
Htmp.data<std::complex<double>>(),
1,
1,
pv->desc);
// Perform the operation Stmp = alpha * S_laststep + beta * Stmp
ScalapackConnector::geadd('N',
nlocal,
nlocal,
alpha,
S_laststep.data<std::complex<double>>(),
1,
1,
pv->desc,
beta,
Stmp.data<std::complex<double>>(),
1,
1,
pv->desc);
if (print_matrix)
{
ofs_running << std::endl;
ofs_running << " H (t+dt/2) :" << std::endl;
for (int i = 0; i < pv->nrow; i++)
{
const int in = i * pv->ncol;
for (int j = 0; j < pv->ncol; j++)
{
ofs_running << Htmp.data<std::complex<double>>()[in + j].real() << "+"
<< Htmp.data<std::complex<double>>()[in + j].imag() << "i ";
}
ofs_running << std::endl;
}
ofs_running << std::endl;
}
}
template <typename Device>
void half_Hmatrix_tensor_lapack(const Parallel_Orbitals* pv,
const int nband,
const int nlocal,
ct::Tensor& Htmp,
ct::Tensor& Stmp,
const ct::Tensor& H_laststep,
const ct::Tensor& S_laststep,
std::ofstream& ofs_running,
const int print_matrix)
{
// 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;
if (print_matrix)
{
ct::Tensor Htmp_cpu = Htmp.to_device<ct::DEVICE_CPU>();
ct::Tensor H_laststep_cpu = H_laststep.to_device<ct::DEVICE_CPU>();
ofs_running << std::setprecision(10);
ofs_running << std::endl;
ofs_running << " H(t+dt) :" << std::endl;
for (int i = 0; i < nlocal; i++)
{
const int in = i * nlocal;
for (int j = 0; j < nlocal; j++)
{
ofs_running << Htmp_cpu.data<std::complex<double>>()[in + j].real() << "+"
<< Htmp_cpu.data<std::complex<double>>()[in + j].imag() << "i ";
}
ofs_running << std::endl;
}
ofs_running << std::endl;
ofs_running << std::endl;
ofs_running << " H(t):" << std::endl;
for (int i = 0; i < nlocal; i++)
{
const int in = i * nlocal;
for (int j = 0; j < nlocal; j++)
{
ofs_running << H_laststep_cpu.data<std::complex<double>>()[in + j].real() << "+"
<< H_laststep_cpu.data<std::complex<double>>()[in + j].imag() << "i ";
}
ofs_running << std::endl;
}
ofs_running << std::endl;
}
std::complex<double> one_half = {0.5, 0.0};
// Perform the operation Htmp = one_half * H_laststep + one_half * Htmp
// Scale Htmp by one_half
ct::kernels::blas_scal<std::complex<double>, ct_Device>()(nlocal * nlocal,
&one_half,
Htmp.data<std::complex<double>>(),
1);
// Htmp = one_half * H_laststep + Htmp
ct::kernels::blas_axpy<std::complex<double>, ct_Device>()(nlocal * nlocal,
&one_half,
H_laststep.data<std::complex<double>>(),
1,
Htmp.data<std::complex<double>>(),
1);
// Perform the operation Stmp = one_half * S_laststep + one_half * Stmp
// Scale Stmp by one_half
ct::kernels::blas_scal<std::complex<double>, ct_Device>()(nlocal * nlocal,
&one_half,
Stmp.data<std::complex<double>>(),
1);
// Stmp = one_half * S_laststep + Stmp
ct::kernels::blas_axpy<std::complex<double>, ct_Device>()(nlocal * nlocal,
&one_half,
S_laststep.data<std::complex<double>>(),
1,
Stmp.data<std::complex<double>>(),
1);
if (print_matrix)
{
ct::Tensor Htmp_cpu = Htmp.to_device<ct::DEVICE_CPU>();
ofs_running << std::endl;
ofs_running << " H (t+dt/2) :" << std::endl;
for (int i = 0; i < nlocal; i++)
{
const int in = i * nlocal;
for (int j = 0; j < nlocal; j++)
{
ofs_running << Htmp_cpu.data<std::complex<double>>()[in + j].real() << "+"
<< Htmp_cpu.data<std::complex<double>>()[in + j].imag() << "i ";
}
ofs_running << std::endl;
}
ofs_running << std::endl;
}
}
// Explicit instantiation of template functions
template void half_Hmatrix_tensor_lapack<base_device::DEVICE_CPU>(const Parallel_Orbitals* pv,
const int nband,
const int nlocal,
ct::Tensor& Htmp,
ct::Tensor& Stmp,
const ct::Tensor& H_laststep,
const ct::Tensor& S_laststep,
std::ofstream& ofs_running,
const int print_matrix);
#if ((defined __CUDA) /* || (defined __ROCM) */)
template void half_Hmatrix_tensor_lapack<base_device::DEVICE_GPU>(const Parallel_Orbitals* pv,
const int nband,
const int nlocal,
ct::Tensor& Htmp,
ct::Tensor& Stmp,
const ct::Tensor& H_laststep,
const ct::Tensor& S_laststep,
std::ofstream& ofs_running,
const int print_matrix);
#endif // __CUDA
#endif // __MPI
} // namespace module_rt