-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathchase_fortran_serial_solve.f90
More file actions
357 lines (332 loc) · 10.8 KB
/
Copy pathchase_fortran_serial_solve.f90
File metadata and controls
357 lines (332 loc) · 10.8 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
351
352
353
354
355
356
357
! This file is a part of ChASE.
! Copyright (c) 2015-2026, Simulation and Data Laboratory Quantum Materials,
! Forschungszentrum Juelich GmbH, Germany. All rights reserved.
! License is 3-clause BSD:
! https://github.com/ChASE-library/ChASE
!
! Fortran interface test aligned with the C++ serial integration tests:
! single-rank pure Clement matrix, s/d/c/z chase init/solve/finalize.
! Sequence of solves with approximate mode after the first (no matrix perturbation).
! After the final solve: compute residuals ||H*v_i - lambda_i*v_i|| from stored H, v, lambda;
! require residuals < tol and residuals /= 0 (CPU/GPU agnostic).
program main
use mpi
use iso_fortran_env, only: real32, real64
use chase_diag
implicit none
integer :: ierr, rank
integer :: n, nev, nex, ldh, idx_max, idx
integer :: init
character :: mode, opt, qr
! Solver settings: single precision uses looser tolerance.
integer, parameter :: deg_s = 16
integer, parameter :: deg_d = 20
real(real32), parameter :: chase_tol_s = 1.0e-4_real32
real(real64), parameter :: chase_tol_d = 1.0e-8_real64
! Residual check tolerances (match getResidualTolerance: single 1e-3, double 1e-8)
real(real32), parameter :: resid_tol_s = 1.0e-3_real32
real(real64), parameter :: resid_tol_d = 1.0e-8_real64
! Keep this as a stable Fortran interface smoke test. The heavier Clement
! cases are covered by the C++ integration tests and examples.
n = 256
ldh = n
nev = 24
nex = 16
idx_max = 2
mode = 'R'
opt = 'S'
qr = 'C'
call mpi_init(ierr)
call mpi_comm_rank(MPI_COMM_WORLD, rank, ierr)
if (rank /= 0) then
call mpi_finalize(ierr)
stop 0
end if
! Run tests for all four precisions (s, d, c, z)
call test_schase()
call test_dchase()
call test_cchase()
call test_zchase()
call mpi_finalize(ierr)
stop 0
contains
subroutine test_schase()
real(real32), allocatable :: h(:,:), v(:,:), resid(:)
real(real32), allocatable :: ritzv(:)
allocate(h(ldh, n), v(n, nev+nex), ritzv(nev+nex), resid(nev))
call build_clement_real_s(h, ldh, n)
v = 0.0_real32
ritzv = 0.0_real32
call schase_init(n, nev, nex, h, ldh, v, ritzv, init)
mode = 'R'
do idx = 1, idx_max
call schase(deg_s, chase_tol_s, mode, opt, qr)
if (idx < idx_max) mode = 'A'
end do
call check_ritz_finite_s(ritzv, nev)
call compute_residuals_s(n, ldh, h, nev, v, n, ritzv, resid)
call check_residuals_s(nev, resid, resid_tol_s)
call schase_finalize(init)
deallocate(h, v, ritzv, resid)
end subroutine test_schase
subroutine test_dchase()
real(real64), allocatable :: h(:,:), v(:,:), resid(:)
real(real64), allocatable :: ritzv(:)
allocate(h(ldh, n), v(n, nev+nex), ritzv(nev+nex), resid(nev))
call build_clement_real_d(h, ldh, n)
v = 0.0_real64
ritzv = 0.0_real64
call dchase_init(n, nev, nex, h, ldh, v, ritzv, init)
mode = 'R'
do idx = 1, idx_max
call dchase(deg_d, chase_tol_d, mode, opt, qr)
if (idx < idx_max) mode = 'A'
end do
call check_ritz_finite_d(ritzv, nev)
call compute_residuals_d(n, ldh, h, nev, v, n, ritzv, resid)
call check_residuals_d(nev, resid, resid_tol_d)
call dchase_finalize(init)
deallocate(h, v, ritzv, resid)
end subroutine test_dchase
subroutine test_cchase()
complex(real32), allocatable :: h(:,:), v(:,:)
real(real32), allocatable :: ritzv(:), resid(:)
allocate(h(ldh, n), v(n, nev+nex), ritzv(nev+nex), resid(nev))
call build_clement_cmplx_s(h, ldh, n)
v = (0.0_real32, 0.0_real32)
ritzv = 0.0_real32
call cchase_init(n, nev, nex, h, ldh, v, ritzv, init)
mode = 'R'
do idx = 1, idx_max
call cchase(deg_s, chase_tol_s, mode, opt, qr)
if (idx < idx_max) mode = 'A'
end do
call check_ritz_finite_s(ritzv, nev)
call compute_residuals_c(n, ldh, h, nev, v, n, ritzv, resid)
call check_residuals_s(nev, resid, resid_tol_s)
call cchase_finalize(init)
deallocate(h, v, ritzv, resid)
end subroutine test_cchase
subroutine test_zchase()
complex(real64), allocatable :: h(:,:), v(:,:)
real(real64), allocatable :: ritzv(:), resid(:)
allocate(h(ldh, n), v(n, nev+nex), ritzv(nev+nex), resid(nev))
call build_clement_cmplx_d(h, ldh, n)
v = (0.0_real64, 0.0_real64)
ritzv = 0.0_real64
call zchase_init(n, nev, nex, h, ldh, v, ritzv, init)
mode = 'R'
do idx = 1, idx_max
call zchase(deg_d, chase_tol_d, mode, opt, qr)
if (idx < idx_max) mode = 'A'
end do
call check_ritz_finite_d(ritzv, nev)
call compute_residuals_z(n, ldh, h, nev, v, n, ritzv, resid)
call check_residuals_d(nev, resid, resid_tol_d)
call zchase_finalize(init)
deallocate(h, v, ritzv, resid)
end subroutine test_zchase
subroutine build_clement_real_s(h, ldh, n)
integer, intent(in) :: ldh, n
real(real32), intent(inout) :: h(ldh, n)
integer :: i
h = 0.0_real32
do i = 1, n
h(i, i) = 0.0_real32
if (i < n) then
h(i+1, i) = real(sqrt(real(i*(n+1-i), real64)), real32)
h(i, i+1) = h(i+1, i)
end if
end do
end subroutine build_clement_real_s
subroutine build_clement_real_d(h, ldh, n)
integer, intent(in) :: ldh, n
real(real64), intent(inout) :: h(ldh, n)
integer :: i
h = 0.0_real64
do i = 1, n
h(i, i) = 0.0_real64
if (i < n) then
h(i+1, i) = sqrt(real(i*(n+1-i), real64))
h(i, i+1) = h(i+1, i)
end if
end do
end subroutine build_clement_real_d
subroutine build_clement_cmplx_s(h, ldh, n)
integer, intent(in) :: ldh, n
complex(real32), intent(inout) :: h(ldh, n)
integer :: i
real(real64) :: tmp
h = (0.0_real32, 0.0_real32)
do i = 1, n
h(i, i) = (0.0_real32, 0.0_real32)
if (i < n) then
tmp = real(i*(n+1-i), real64)
h(i+1, i) = cmplx(sqrt(tmp), 0.0_real64, real32)
h(i, i+1) = h(i+1, i)
end if
end do
end subroutine build_clement_cmplx_s
subroutine build_clement_cmplx_d(h, ldh, n)
integer, intent(in) :: ldh, n
complex(real64), intent(inout) :: h(ldh, n)
integer :: i
real(real64) :: tmp
h = (0.0_real64, 0.0_real64)
do i = 1, n
h(i, i) = (0.0_real64, 0.0_real64)
if (i < n) then
tmp = real(i*(n+1-i), real64)
h(i+1, i) = cmplx(sqrt(tmp), 0.0_real64, real64)
h(i, i+1) = h(i+1, i)
end if
end do
end subroutine build_clement_cmplx_d
! Compute residuals resid(i) = || H*v_i - lambda_i*v_i ||_2 (same as C++ residuals())
subroutine compute_residuals_s(n, ldh, h, nev, v, ldv, ritzv, resid)
integer, intent(in) :: n, ldh, nev, ldv
real(real32), intent(in) :: h(ldh, n), v(ldv, nev), ritzv(nev)
real(real32), intent(out) :: resid(nev)
real(real32), allocatable :: w(:)
integer :: i, k, j
allocate(w(n))
do i = 1, nev
w = 0.0_real32
do j = 1, n
do k = 1, n
w(k) = w(k) + h(k, j) * v(j, i)
end do
end do
do k = 1, n
w(k) = w(k) - ritzv(i) * v(k, i)
end do
resid(i) = 0.0_real32
do k = 1, n
resid(i) = resid(i) + w(k) * w(k)
end do
resid(i) = sqrt(resid(i))
end do
deallocate(w)
end subroutine compute_residuals_s
subroutine compute_residuals_d(n, ldh, h, nev, v, ldv, ritzv, resid)
integer, intent(in) :: n, ldh, nev, ldv
real(real64), intent(in) :: h(ldh, n), v(ldv, nev), ritzv(nev)
real(real64), intent(out) :: resid(nev)
real(real64), allocatable :: w(:)
integer :: i, k, j
allocate(w(n))
do i = 1, nev
w = 0.0_real64
do j = 1, n
do k = 1, n
w(k) = w(k) + h(k, j) * v(j, i)
end do
end do
do k = 1, n
w(k) = w(k) - ritzv(i) * v(k, i)
end do
resid(i) = 0.0_real64
do k = 1, n
resid(i) = resid(i) + w(k) * w(k)
end do
resid(i) = sqrt(resid(i))
end do
deallocate(w)
end subroutine compute_residuals_d
subroutine compute_residuals_c(n, ldh, h, nev, v, ldv, ritzv, resid)
integer, intent(in) :: n, ldh, nev, ldv
complex(real32), intent(in) :: h(ldh, n), v(ldv, nev)
real(real32), intent(in) :: ritzv(nev)
real(real32), intent(out) :: resid(nev)
complex(real32), allocatable :: w(:)
integer :: i, k, j
allocate(w(n))
do i = 1, nev
w = (0.0_real32, 0.0_real32)
do j = 1, n
do k = 1, n
w(k) = w(k) + h(k, j) * v(j, i)
end do
end do
do k = 1, n
w(k) = w(k) - ritzv(i) * v(k, i)
end do
resid(i) = 0.0_real32
do k = 1, n
resid(i) = resid(i) + real(conjg(w(k))*w(k), real32)
end do
resid(i) = sqrt(resid(i))
end do
deallocate(w)
end subroutine compute_residuals_c
subroutine compute_residuals_z(n, ldh, h, nev, v, ldv, ritzv, resid)
integer, intent(in) :: n, ldh, nev, ldv
complex(real64), intent(in) :: h(ldh, n), v(ldv, nev)
real(real64), intent(in) :: ritzv(nev)
real(real64), intent(out) :: resid(nev)
complex(real64), allocatable :: w(:)
integer :: i, k, j
allocate(w(n))
do i = 1, nev
w = (0.0_real64, 0.0_real64)
do j = 1, n
do k = 1, n
w(k) = w(k) + h(k, j) * v(j, i)
end do
end do
do k = 1, n
w(k) = w(k) - ritzv(i) * v(k, i)
end do
resid(i) = 0.0_real64
do k = 1, n
resid(i) = resid(i) + real(conjg(w(k))*w(k), real64)
end do
resid(i) = sqrt(resid(i))
end do
deallocate(w)
end subroutine compute_residuals_z
subroutine check_residuals_s(nev, resid, resid_tol)
integer, intent(in) :: nev
real(real32), intent(in) :: resid(nev), resid_tol
integer :: i
do i = 1, nev
if (resid(i) >= resid_tol) stop 1
if (resid(i) == 0.0_real32) stop 1
end do
end subroutine check_residuals_s
subroutine check_residuals_d(nev, resid, resid_tol)
integer, intent(in) :: nev
real(real64), intent(in) :: resid(nev), resid_tol
integer :: i
do i = 1, nev
if (resid(i) >= resid_tol) stop 1
if (resid(i) == 0.0_real64) stop 1
end do
end subroutine check_residuals_d
subroutine check_ritz_finite_s(ritzv, nev)
integer, intent(in) :: nev
real(real32), intent(in) :: ritzv(*)
integer :: i
do i = 1, min(5, nev)
if (.not. (abs(ritzv(i)) >= 0.0_real32 .and. abs(ritzv(i)) <= huge(1.0_real32))) then
stop 1
end if
if (ritzv(i) /= ritzv(i)) then
stop 1
end if
end do
end subroutine check_ritz_finite_s
subroutine check_ritz_finite_d(ritzv, nev)
integer, intent(in) :: nev
real(real64), intent(in) :: ritzv(*)
integer :: i
do i = 1, min(5, nev)
if (.not. (abs(ritzv(i)) >= 0.0_real64 .and. abs(ritzv(i)) <= huge(1.0_real64))) then
stop 1
end if
if (ritzv(i) /= ritzv(i)) then
stop 1
end if
end do
end subroutine check_ritz_finite_d
end program main