Line data Source code
1 : !--------------------------------------------------------------------------------------------------!
2 : ! CP2K: A general program to perform molecular dynamics simulations !
3 : ! Copyright 2000-2026 CP2K developers group <https://cp2k.org> !
4 : ! !
5 : ! SPDX-License-Identifier: GPL-2.0-or-later !
6 : !--------------------------------------------------------------------------------------------------!
7 :
8 : ! **************************************************************************************************
9 : !> \brief methods for arnoldi iteration
10 : !> \par History
11 : !> 2014.09 created [Florian Schiffmann]
12 : !> 2023.12 Removed support for single-precision [Ole Schuett]
13 : !> 2024.12 Removed support for complex input matrices [Ole Schuett]
14 : !> \author Florian Schiffmann
15 : ! **************************************************************************************************
16 : MODULE arnoldi_methods
17 : USE arnoldi_geev, ONLY: arnoldi_general_local_diag,&
18 : arnoldi_symm_local_diag,&
19 : arnoldi_tridiag_local_diag
20 : USE arnoldi_types, ONLY: arnoldi_control_type,&
21 : arnoldi_data_type,&
22 : arnoldi_env_type,&
23 : get_control,&
24 : get_data,&
25 : m_x_v_vectors_type
26 : USE arnoldi_vector, ONLY: dbcsr_matrix_colvec_multiply,&
27 : transfer_dbcsr_to_local_array,&
28 : transfer_local_array_to_dbcsr
29 : USE cp_dbcsr_api, ONLY: &
30 : dbcsr_add, dbcsr_copy, dbcsr_get_info, dbcsr_iterator_blocks_left, &
31 : dbcsr_iterator_next_block, dbcsr_iterator_start, dbcsr_iterator_stop, dbcsr_iterator_type, &
32 : dbcsr_p_type, dbcsr_scale, dbcsr_type
33 : USE kinds, ONLY: dp
34 : USE message_passing, ONLY: mp_comm_type
35 : #include "../base/base_uses.f90"
36 :
37 : IMPLICIT NONE
38 :
39 : PRIVATE
40 :
41 : CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'arnoldi_methods'
42 :
43 : PUBLIC :: arnoldi_init, build_subspace, compute_evals, arnoldi_iram, &
44 : gev_arnoldi_init, gev_build_subspace, gev_update_data
45 :
46 : CONTAINS
47 :
48 : ! **************************************************************************************************
49 : !> \brief Alogorithm for the implicit restarts in the arnoldi method
50 : !> this is an early implementation which scales subspace size^4
51 : !> by replacing the lapack calls with direct math the
52 : !> QR and gemms can be made linear and a N^2 sacling will be acchieved
53 : !> however this already sets the framework but should be used with care.
54 : !> Currently all based on lapack.
55 : !> \param arnoldi_env ...
56 : ! **************************************************************************************************
57 124 : SUBROUTINE arnoldi_iram(arnoldi_env)
58 : TYPE(arnoldi_env_type) :: arnoldi_env
59 :
60 : CHARACTER(LEN=*), PARAMETER :: routineN = 'arnoldi_iram'
61 :
62 124 : COMPLEX(dp), ALLOCATABLE, DIMENSION(:) :: tau, work, work_measure
63 124 : COMPLEX(dp), ALLOCATABLE, DIMENSION(:, :) :: Q, safe_mat, tmp_mat, tmp_mat1
64 : INTEGER :: handle, i, info, j, lwork, msize, nwant
65 : REAL(kind=dp) :: beta, sigma
66 124 : REAL(kind=dp), ALLOCATABLE, DIMENSION(:, :) :: Qdata
67 : TYPE(arnoldi_control_type), POINTER :: control
68 : TYPE(arnoldi_data_type), POINTER :: ar_data
69 :
70 124 : CALL timeset(routineN, handle)
71 :
72 : ! This is just a terribly inefficient implementation but I hope it is correct and might serve as a reference
73 124 : ar_data => get_data(arnoldi_env)
74 124 : control => get_control(arnoldi_env)
75 124 : msize = control%current_step
76 124 : nwant = control%nval_out
77 744 : ALLOCATE (tmp_mat(msize, msize)); ALLOCATE (safe_mat(msize, msize))
78 620 : ALLOCATE (Q(msize, msize)); ALLOCATE (tmp_mat1(msize, msize))
79 124 : ALLOCATE (work_measure(1))
80 744 : ALLOCATE (tau(msize)); ALLOCATE (Qdata(msize, msize))
81 : !make Q identity
82 124 : Q = CMPLX(0.0, 0.0, dp)
83 4924 : DO i = 1, msize
84 4924 : Q(i, i) = CMPLX(1.0, 0.0, dp)
85 : END DO
86 :
87 : ! Looks a bit odd, but safe_mat will contain the result in the end, while tmpmat gets violated by lapack
88 211772 : tmp_mat(:, :) = CMPLX(ar_data%Hessenberg(1:msize, 1:msize), 0.0, KIND=dp)
89 211772 : safe_mat(:, :) = tmp_mat(:, :)
90 :
91 4924 : DO i = 1, msize
92 : ! A bit a strange check but in the end we only want to shift the unwanted evals
93 202172 : IF (ANY(control%selected_ind == i)) CYCLE
94 : ! Here we shift the matrix by subtracting unwanted evals from the diagonal
95 201800 : DO j = 1, msize
96 201800 : tmp_mat(j, j) = tmp_mat(j, j) - ar_data%evals(i)
97 : END DO
98 : ! Now we repair the damage by QR factorizing
99 4552 : lwork = -1
100 4552 : CALL zgeqrf(msize, msize, tmp_mat, msize, tau, work_measure, lwork, info)
101 4552 : lwork = INT(work_measure(1))
102 4552 : IF (ALLOCATED(work)) THEN
103 4428 : IF (SIZE(work) < lwork) THEN
104 0 : DEALLOCATE (work)
105 : END IF
106 : END IF
107 4800 : IF (.NOT. ALLOCATED(work)) ALLOCATE (work(lwork))
108 4552 : CALL zgeqrf(msize, msize, tmp_mat, msize, tau, work, lwork, info)
109 : ! Ask Lapack to reconstruct Q from its own way of storing data (tmpmat will contain Q)
110 4552 : CALL zungqr(msize, msize, msize, tmp_mat, msize, tau, work, lwork, info)
111 : ! update Q=Q*Q_current
112 9815112 : tmp_mat1(:, :) = Q(:, :)
113 : CALL zgemm('N', 'N', msize, msize, msize, CMPLX(1.0, 0.0, dp), tmp_mat1, &
114 4552 : msize, tmp_mat, msize, CMPLX(0.0, 0.0, dp), Q, msize)
115 : ! Update H=(Q*)HQ
116 : CALL zgemm('C', 'N', msize, msize, msize, CMPLX(1.0, 0.0, dp), tmp_mat, &
117 4552 : msize, safe_mat, msize, CMPLX(0.0, 0.0, dp), tmp_mat1, msize)
118 : CALL zgemm('N', 'N', msize, msize, msize, CMPLX(1.0, 0.0, dp), tmp_mat1, &
119 4552 : msize, tmp_mat, msize, CMPLX(0.0, 0.0, dp), safe_mat, msize)
120 :
121 : ! this one is crucial for numerics not to accumulate noise in the subdiagonals
122 201800 : DO j = 1, msize
123 4717136 : safe_mat(j + 2:msize, j) = CMPLX(0.0, 0.0, dp)
124 : END DO
125 9815484 : tmp_mat(:, :) = safe_mat(:, :)
126 : END DO
127 :
128 : ! Now we can compute our restart quantities
129 216572 : ar_data%Hessenberg = 0.0_dp
130 211772 : ar_data%Hessenberg(1:msize, 1:msize) = REAL(safe_mat, KIND=dp)
131 211772 : Qdata(:, :) = REAL(Q(:, :), KIND=dp)
132 :
133 124 : beta = ar_data%Hessenberg(nwant + 1, nwant); sigma = Qdata(msize, nwant)
134 :
135 : !update the residuum and the basis vectors
136 124 : IF (control%local_comp) THEN
137 335559 : ar_data%f_vec = MATMUL(ar_data%local_history(:, 1:msize), Qdata(1:msize, nwant + 1))*beta + ar_data%f_vec(:)*sigma
138 997930 : ar_data%local_history(:, 1:nwant) = MATMUL(ar_data%local_history(:, 1:msize), Qdata(1:msize, 1:nwant))
139 : END IF
140 : ! Set the current step to nwant so the subspace build knows where to start
141 124 : control%current_step = nwant
142 :
143 124 : DEALLOCATE (tmp_mat, safe_mat, Q, Qdata, tmp_mat1, work, tau, work_measure)
144 124 : CALL timestop(handle)
145 :
146 124 : END SUBROUTINE arnoldi_iram
147 :
148 : ! **************************************************************************************************
149 : !> \brief Call the correct eigensolver, in the arnoldi method only the right
150 : !> eigenvectors are used. Lefts are created here but dumped immediately
151 : !> This is only the serial version
152 : !> \param arnoldi_env ...
153 : ! **************************************************************************************************
154 139182 : SUBROUTINE compute_evals(arnoldi_env)
155 : TYPE(arnoldi_env_type) :: arnoldi_env
156 :
157 : CHARACTER(LEN=*), PARAMETER :: routineN = 'compute_evals'
158 :
159 : COMPLEX(dp), ALLOCATABLE, DIMENSION(:, :) :: levec
160 : INTEGER :: handle, ndim
161 : TYPE(arnoldi_control_type), POINTER :: control
162 : TYPE(arnoldi_data_type), POINTER :: ar_data
163 :
164 139182 : CALL timeset(routineN, handle)
165 :
166 139182 : ar_data => get_data(arnoldi_env)
167 139182 : control => get_control(arnoldi_env)
168 139182 : ndim = control%current_step
169 556728 : ALLOCATE (levec(ndim, ndim))
170 :
171 : ! Needs antoher interface as the calls to real and complex geev differ (sucks!)
172 : ! only perform the diagonalization on processors which hold data
173 139182 : IF (control%generalized_ev) THEN
174 : CALL arnoldi_symm_local_diag('V', ar_data%Hessenberg(1:ndim, 1:ndim), ndim, &
175 5364 : ar_data%evals(1:ndim), ar_data%revec(1:ndim, 1:ndim))
176 : ELSE
177 133818 : IF (control%symmetric) THEN
178 : CALL arnoldi_tridiag_local_diag('N', 'V', ar_data%Hessenberg(1:ndim, 1:ndim), ndim, &
179 9078 : ar_data%evals(1:ndim), ar_data%revec(1:ndim, 1:ndim), levec)
180 : ELSE
181 : CALL arnoldi_general_local_diag('N', 'V', ar_data%Hessenberg(1:ndim, 1:ndim), ndim, &
182 124740 : ar_data%evals(1:ndim), ar_data%revec(1:ndim, 1:ndim), levec)
183 : END IF
184 : END IF
185 :
186 139182 : DEALLOCATE (levec)
187 139182 : CALL timestop(handle)
188 :
189 139182 : END SUBROUTINE compute_evals
190 :
191 : ! **************************************************************************************************
192 : !> \brief Interface for the initialization of the arnoldi subspace creation
193 : !> currently it can only setup a random vector but can be improved to
194 : !> various types of restarts easily
195 : !> \param matrix pointer to the matrices as described in main interface
196 : !> \param vectors work vectors for the matrix vector multiplications
197 : !> \param arnoldi_env all data concerning the subspace
198 : ! **************************************************************************************************
199 133694 : SUBROUTINE arnoldi_init(matrix, vectors, arnoldi_env)
200 : TYPE(dbcsr_p_type), DIMENSION(:) :: matrix
201 : TYPE(m_x_v_vectors_type) :: vectors
202 : TYPE(arnoldi_env_type) :: arnoldi_env
203 :
204 : CHARACTER(LEN=*), PARAMETER :: routineN = 'arnoldi_init'
205 :
206 : INTEGER :: col, col_size, handle, i, iseed(4), &
207 : ncol_local, nrow_local, row, row_size
208 : LOGICAL :: local_comp
209 : REAL(dp) :: rnorm
210 : REAL(kind=dp) :: norm
211 : REAL(kind=dp), ALLOCATABLE, DIMENSION(:) :: v_vec, w_vec
212 133694 : REAL(kind=dp), DIMENSION(:, :), POINTER :: data_block
213 : TYPE(arnoldi_control_type), POINTER :: control
214 : TYPE(arnoldi_data_type), POINTER :: ar_data
215 : TYPE(dbcsr_iterator_type) :: iter
216 : TYPE(mp_comm_type) :: pcol_group
217 :
218 133694 : CALL timeset(routineN, handle)
219 :
220 133694 : control => get_control(arnoldi_env)
221 133694 : pcol_group = control%pcol_group
222 133694 : local_comp = control%local_comp
223 :
224 133694 : ar_data => get_data(arnoldi_env)
225 :
226 : ! create a local data copy to store the vectors and make Gram Schmidt a bit simpler
227 133694 : CALL dbcsr_get_info(matrix=vectors%input_vec, nfullrows_local=nrow_local, nfullcols_local=ncol_local)
228 346230 : ALLOCATE (v_vec(nrow_local))
229 212536 : ALLOCATE (w_vec(nrow_local))
230 133694 : v_vec = 0.0_dp; w_vec = 0.0_dp
231 144279422 : ar_data%Hessenberg = 0.0_dp
232 :
233 133694 : IF (control%has_initial_vector) THEN
234 : ! after calling the set routine the initial vector is stored in f_vec
235 3032 : CALL transfer_local_array_to_dbcsr(vectors%input_vec, ar_data%f_vec, nrow_local, control%local_comp)
236 : ELSE
237 : ! Setup the initial normalized random vector (sufficient if it only happens on proc_col 0)
238 130662 : CALL dbcsr_iterator_start(iter, vectors%input_vec)
239 227128 : DO WHILE (dbcsr_iterator_blocks_left(iter))
240 96466 : CALL dbcsr_iterator_next_block(iter, row, col, data_block, row_size=row_size, col_size=col_size)
241 96466 : iseed(1) = 2; iseed(2) = MOD(row, 4095); iseed(3) = MOD(col, 4095); iseed(4) = 11
242 227128 : CALL dlarnv(2, iseed, row_size*col_size, data_block)
243 : END DO
244 130662 : CALL dbcsr_iterator_stop(iter)
245 : END IF
246 :
247 133694 : CALL transfer_dbcsr_to_local_array(vectors%input_vec, v_vec, nrow_local, control%local_comp)
248 :
249 : ! compute the vector norm of the random vectorm, get it real valued as well (rnorm)
250 133694 : CALL compute_norms(v_vec, norm, rnorm, control%pcol_group)
251 :
252 133694 : IF (rnorm == 0) rnorm = 1 ! catch case where this rank has no actual data
253 133694 : CALL dbcsr_scale(vectors%input_vec, REAL(1.0, dp)/rnorm)
254 :
255 : ! Everything prepared, initialize the Arnoldi iteration
256 133694 : CALL transfer_dbcsr_to_local_array(vectors%input_vec, v_vec, nrow_local, control%local_comp)
257 :
258 : ! This permits to compute the subspace of a matrix which is a product of multiple matrices
259 267388 : DO i = 1, SIZE(matrix)
260 : CALL dbcsr_matrix_colvec_multiply(matrix(i)%matrix, vectors%input_vec, vectors%result_vec, 1.0_dp, &
261 133694 : 0.0_dp, vectors%rep_row_vec, vectors%rep_col_vec)
262 267388 : CALL dbcsr_copy(vectors%input_vec, vectors%result_vec)
263 : END DO
264 :
265 133694 : CALL transfer_dbcsr_to_local_array(vectors%result_vec, w_vec, nrow_local, control%local_comp)
266 :
267 : ! Put the projection into the Hessenberg matrix, and make the vectors orthonormal
268 800659 : ar_data%Hessenberg(1, 1) = DOT_PRODUCT(v_vec, w_vec)
269 133694 : CALL pcol_group%sum(ar_data%Hessenberg(1, 1))
270 800659 : ar_data%f_vec = w_vec - v_vec*ar_data%Hessenberg(1, 1)
271 :
272 800659 : ar_data%local_history(:, 1) = v_vec(:)
273 :
274 : ! We did the first step in here so we should set the current step for the subspace generation accordingly
275 133694 : control%current_step = 1
276 :
277 133694 : DEALLOCATE (v_vec, w_vec)
278 133694 : CALL timestop(handle)
279 :
280 267388 : END SUBROUTINE arnoldi_init
281 :
282 : ! **************************************************************************************************
283 : !> \brief Computes the initial guess for the solution of the generalized eigenvalue
284 : !> using the arnoldi method
285 : !> \param matrix pointer to the matrices as described in main interface
286 : !> \param matrix_arnoldi ...
287 : !> \param vectors work vectors for the matrix vector multiplications
288 : !> \param arnoldi_env all data concerning the subspace
289 : ! **************************************************************************************************
290 4400 : SUBROUTINE gev_arnoldi_init(matrix, matrix_arnoldi, vectors, arnoldi_env)
291 : TYPE(dbcsr_p_type), DIMENSION(:) :: matrix, matrix_arnoldi
292 : TYPE(m_x_v_vectors_type) :: vectors
293 : TYPE(arnoldi_env_type) :: arnoldi_env
294 :
295 : CHARACTER(LEN=*), PARAMETER :: routineN = 'gev_arnoldi_init'
296 :
297 : INTEGER :: col, col_size, handle, iseed(4), &
298 : ncol_local, nrow_local, row, row_size
299 : LOGICAL :: local_comp
300 : REAL(dp) :: rnorm
301 : REAL(kind=dp) :: denom, norm
302 : REAL(kind=dp), ALLOCATABLE, DIMENSION(:) :: v_vec, w_vec
303 4400 : REAL(kind=dp), DIMENSION(:, :), POINTER :: data_block
304 : TYPE(arnoldi_control_type), POINTER :: control
305 : TYPE(arnoldi_data_type), POINTER :: ar_data
306 : TYPE(dbcsr_iterator_type) :: iter
307 : TYPE(mp_comm_type) :: pcol_group
308 :
309 4400 : CALL timeset(routineN, handle)
310 :
311 4400 : control => get_control(arnoldi_env)
312 4400 : pcol_group = control%pcol_group
313 4400 : local_comp = control%local_comp
314 :
315 4400 : ar_data => get_data(arnoldi_env)
316 :
317 : ! create a local data copy to store the vectors and make Gram Schmidt a bit simpler
318 4400 : CALL dbcsr_get_info(matrix=vectors%input_vec, nfullrows_local=nrow_local, nfullcols_local=ncol_local)
319 13170 : ALLOCATE (v_vec(nrow_local))
320 8770 : ALLOCATE (w_vec(nrow_local))
321 4400 : v_vec = 0.0_dp; w_vec = 0.0_dp
322 1940400 : ar_data%Hessenberg = 0.0_dp
323 :
324 4400 : IF (control%has_initial_vector) THEN
325 : ! after calling the set routine the initial vector is stored in f_vec
326 2296 : CALL transfer_local_array_to_dbcsr(vectors%input_vec, ar_data%f_vec, nrow_local, control%local_comp)
327 : ELSE
328 : ! Setup the initial normalized random vector (sufficient if it only happens on proc_col 0)
329 2104 : CALL dbcsr_iterator_start(iter, vectors%input_vec)
330 6062 : DO WHILE (dbcsr_iterator_blocks_left(iter))
331 3958 : CALL dbcsr_iterator_next_block(iter, row, col, data_block, row_size=row_size, col_size=col_size)
332 3958 : iseed(1) = 2; iseed(2) = MOD(row, 4095); iseed(3) = MOD(col, 4095); iseed(4) = 11
333 6062 : CALL dlarnv(2, iseed, row_size*col_size, data_block)
334 : END DO
335 2104 : CALL dbcsr_iterator_stop(iter)
336 : END IF
337 :
338 4400 : CALL transfer_dbcsr_to_local_array(vectors%input_vec, v_vec, nrow_local, control%local_comp)
339 :
340 : ! compute the vector norm of the reandom vectorm, get it real valued as well (rnorm)
341 4400 : CALL compute_norms(v_vec, norm, rnorm, control%pcol_group)
342 :
343 4400 : IF (rnorm == 0) rnorm = 1 ! catch case where this rank has no actual data
344 4400 : CALL dbcsr_scale(vectors%input_vec, REAL(1.0, dp)/rnorm)
345 :
346 : CALL dbcsr_matrix_colvec_multiply(matrix(1)%matrix, vectors%input_vec, vectors%result_vec, 1.0_dp, &
347 4400 : 0.0_dp, vectors%rep_row_vec, vectors%rep_col_vec)
348 :
349 4400 : CALL transfer_dbcsr_to_local_array(vectors%result_vec, w_vec, nrow_local, control%local_comp)
350 :
351 4400 : ar_data%rho_scale = 0.0_dp
352 64296 : ar_data%rho_scale = DOT_PRODUCT(v_vec, w_vec)
353 4400 : CALL pcol_group%sum(ar_data%rho_scale)
354 :
355 : CALL dbcsr_matrix_colvec_multiply(matrix(2)%matrix, vectors%input_vec, vectors%result_vec, 1.0_dp, &
356 4400 : 0.0_dp, vectors%rep_row_vec, vectors%rep_col_vec)
357 :
358 4400 : CALL transfer_dbcsr_to_local_array(vectors%result_vec, w_vec, nrow_local, control%local_comp)
359 :
360 : denom = 0.0_dp
361 64296 : denom = DOT_PRODUCT(v_vec, w_vec)
362 4400 : CALL pcol_group%sum(denom)
363 4400 : IF (control%myproc == 0) ar_data%rho_scale = ar_data%rho_scale/denom
364 4400 : CALL control%mp_group%bcast(ar_data%rho_scale, 0)
365 :
366 : ! if the maximum ev is requested we need to optimize with -A-rho*B
367 4400 : CALL dbcsr_copy(matrix_arnoldi(1)%matrix, matrix(1)%matrix)
368 4400 : CALL dbcsr_add(matrix_arnoldi(1)%matrix, matrix(2)%matrix, 1.0_dp, -ar_data%rho_scale)
369 :
370 64296 : ar_data%x_vec = v_vec
371 :
372 4400 : CALL timestop(handle)
373 :
374 13200 : END SUBROUTINE gev_arnoldi_init
375 :
376 : ! **************************************************************************************************
377 : !> \brief Here we create the Krylov subspace and fill the Hessenberg matrix
378 : !> convergence check is only performed on subspace convergence
379 : !> Gram Schidt is used to orthonogonalize.
380 : !> If this is numericall not sufficient a Daniel, Gragg, Kaufman and Steward
381 : !> correction is performed
382 : !> \param matrix ...
383 : !> \param vectors ...
384 : !> \param arnoldi_env ...
385 : ! **************************************************************************************************
386 133818 : SUBROUTINE build_subspace(matrix, vectors, arnoldi_env)
387 : TYPE(dbcsr_p_type), DIMENSION(:) :: matrix
388 : TYPE(m_x_v_vectors_type), TARGET :: vectors
389 : TYPE(arnoldi_env_type) :: arnoldi_env
390 :
391 : CHARACTER(LEN=*), PARAMETER :: routineN = 'build_subspace'
392 :
393 : INTEGER :: handle, i, j, ncol_local, nrow_local
394 : REAL(dp) :: rnorm
395 : REAL(kind=dp) :: norm
396 : REAL(kind=dp), ALLOCATABLE, DIMENSION(:) :: h_vec, s_vec, v_vec, w_vec
397 : TYPE(arnoldi_control_type), POINTER :: control
398 : TYPE(arnoldi_data_type), POINTER :: ar_data
399 : TYPE(dbcsr_type), POINTER :: input_vec, result_vec, swap_vec
400 :
401 133818 : CALL timeset(routineN, handle)
402 :
403 133818 : ar_data => get_data(arnoldi_env)
404 133818 : control => get_control(arnoldi_env)
405 133818 : control%converged = .FALSE.
406 :
407 : ! create the vectors required during the iterations
408 133818 : CALL dbcsr_get_info(matrix=vectors%input_vec, nfullrows_local=nrow_local, nfullcols_local=ncol_local)
409 425568 : ALLOCATE (v_vec(nrow_local)); ALLOCATE (w_vec(nrow_local))
410 133818 : v_vec = 0.0_dp; w_vec = 0.0_dp
411 535272 : ALLOCATE (s_vec(control%max_iter)); ALLOCATE (h_vec(control%max_iter))
412 :
413 491726 : DO j = control%current_step, control%max_iter - 1
414 :
415 : ! compute the vector norm of the residuum, get it real valued as well (rnorm)
416 488706 : CALL compute_norms(ar_data%f_vec, norm, rnorm, control%pcol_group)
417 :
418 : ! check convergence and inform everybody about it, a bit annoying to talk to everybody because of that
419 488706 : IF (control%myproc == 0) control%converged = rnorm < REAL(control%threshold, dp)
420 488706 : CALL control%mp_group%bcast(control%converged, 0)
421 488706 : IF (control%converged) EXIT
422 :
423 : ! transfer normalized residdum to history and its norm to the Hessenberg matrix
424 357908 : IF (rnorm == 0) rnorm = 1 ! catch case where this rank has no actual data
425 11093772 : v_vec(:) = ar_data%f_vec(:)/rnorm; ar_data%local_history(:, j + 1) = v_vec(:); ar_data%Hessenberg(j + 1, j) = norm
426 :
427 357908 : input_vec => vectors%input_vec
428 357908 : result_vec => vectors%result_vec
429 357908 : CALL transfer_local_array_to_dbcsr(input_vec, v_vec, nrow_local, control%local_comp)
430 :
431 : ! This permits to compute the subspace of a matrix which is a product of two matrices
432 715816 : DO i = 1, SIZE(matrix)
433 : CALL dbcsr_matrix_colvec_multiply(matrix(i)%matrix, input_vec, result_vec, 1.0_dp, &
434 357908 : 0.0_dp, vectors%rep_row_vec, vectors%rep_col_vec)
435 357908 : swap_vec => input_vec
436 357908 : input_vec => result_vec
437 715816 : result_vec => swap_vec
438 : END DO
439 :
440 357908 : CALL transfer_dbcsr_to_local_array(input_vec, w_vec, nrow_local, control%local_comp)
441 :
442 : ! Let's do the orthonormalization, to get the new f_vec. First try the Gram Schmidt scheme
443 : CALL Gram_Schmidt_ortho(h_vec, ar_data%f_vec, s_vec, w_vec, nrow_local, j + 1, &
444 357908 : ar_data%local_history, ar_data%local_history, control%local_comp, control%pcol_group)
445 :
446 : ! A bit more expensive but simply always top up with a DGKS correction, otherwise numerics
447 : ! can become a problem later on, there is probably a good check whether it's necessary, but we don't perform it
448 : CALL DGKS_ortho(h_vec, ar_data%f_vec, s_vec, nrow_local, j + 1, ar_data%local_history, &
449 357908 : ar_data%local_history, control%local_comp, control%pcol_group)
450 : ! Finally we can put the projections into our Hessenberg matrix
451 3405467 : ar_data%Hessenberg(1:j + 1, j + 1) = h_vec(1:j + 1)
452 491726 : control%current_step = j + 1
453 : END DO
454 :
455 : ! compute the vector norm of the final residuum and put it in to Hessenberg
456 133818 : CALL compute_norms(ar_data%f_vec, norm, rnorm, control%pcol_group)
457 133818 : ar_data%Hessenberg(control%current_step + 1, control%current_step) = norm
458 :
459 : ! broadcast the Hessenberg matrix so we don't need to care later on
460 288858170 : CALL control%mp_group%bcast(ar_data%Hessenberg, 0)
461 :
462 133818 : DEALLOCATE (v_vec, w_vec, h_vec, s_vec)
463 133818 : CALL timestop(handle)
464 :
465 267636 : END SUBROUTINE build_subspace
466 :
467 : ! **************************************************************************************************
468 : !> \brief builds the basis rothogonal wrt. the metric.
469 : !> The structure looks similar to normal arnoldi but norms, vectors and
470 : !> matrix_vector products are very differently defined. Therefore it is
471 : !> cleaner to put it in a separate subroutine to avoid confusion
472 : !> \param matrix ...
473 : !> \param vectors ...
474 : !> \param arnoldi_env ...
475 : ! **************************************************************************************************
476 5364 : SUBROUTINE gev_build_subspace(matrix, vectors, arnoldi_env)
477 : TYPE(dbcsr_p_type), DIMENSION(:) :: matrix
478 : TYPE(m_x_v_vectors_type) :: vectors
479 : TYPE(arnoldi_env_type) :: arnoldi_env
480 :
481 : CHARACTER(LEN=*), PARAMETER :: routineN = 'gev_build_subspace'
482 :
483 : INTEGER :: handle, j, ncol_local, nrow_local
484 : REAL(kind=dp) :: norm
485 : REAL(kind=dp), ALLOCATABLE, DIMENSION(:) :: h_vec, s_vec, v_vec, w_vec
486 5364 : REAL(kind=dp), ALLOCATABLE, DIMENSION(:, :) :: BZmat, CZmat, Zmat
487 : TYPE(arnoldi_control_type), POINTER :: control
488 : TYPE(arnoldi_data_type), POINTER :: ar_data
489 : TYPE(mp_comm_type) :: pcol_group
490 :
491 5364 : CALL timeset(routineN, handle)
492 :
493 5364 : ar_data => get_data(arnoldi_env)
494 5364 : control => get_control(arnoldi_env)
495 5364 : control%converged = .FALSE.
496 5364 : pcol_group = control%pcol_group
497 :
498 : ! create the vectors required during the iterations
499 5364 : CALL dbcsr_get_info(matrix=vectors%input_vec, nfullrows_local=nrow_local, nfullcols_local=ncol_local)
500 21394 : ALLOCATE (v_vec(nrow_local)); ALLOCATE (w_vec(nrow_local))
501 5364 : v_vec = 0.0_dp; w_vec = 0.0_dp
502 21456 : ALLOCATE (s_vec(control%max_iter)); ALLOCATE (h_vec(control%max_iter))
503 32122 : ALLOCATE (Zmat(nrow_local, control%max_iter)); ALLOCATE (CZmat(nrow_local, control%max_iter))
504 16061 : ALLOCATE (BZmat(nrow_local, control%max_iter))
505 :
506 5364 : CALL transfer_local_array_to_dbcsr(vectors%input_vec, ar_data%x_vec, nrow_local, control%local_comp)
507 : CALL dbcsr_matrix_colvec_multiply(matrix(2)%matrix, vectors%input_vec, vectors%result_vec, 1.0_dp, &
508 5364 : 0.0_dp, vectors%rep_row_vec, vectors%rep_col_vec)
509 5364 : CALL transfer_dbcsr_to_local_array(vectors%result_vec, BZmat(:, 1), nrow_local, control%local_comp)
510 :
511 : norm = 0.0_dp
512 87157 : norm = DOT_PRODUCT(ar_data%x_vec, BZmat(:, 1))
513 5364 : CALL pcol_group%sum(norm)
514 5364 : IF (control%local_comp) THEN
515 168919 : Zmat(:, 1) = ar_data%x_vec/SQRT(norm); BZmat(:, 1) = BZmat(:, 1)/SQRT(norm)
516 : END IF
517 :
518 84583 : DO j = 1, control%max_iter
519 84583 : control%current_step = j
520 84583 : CALL transfer_local_array_to_dbcsr(vectors%input_vec, Zmat(:, j), nrow_local, control%local_comp)
521 : CALL dbcsr_matrix_colvec_multiply(matrix(1)%matrix, vectors%input_vec, vectors%result_vec, 1.0_dp, &
522 84583 : 0.0_dp, vectors%rep_row_vec, vectors%rep_col_vec)
523 84583 : CALL transfer_dbcsr_to_local_array(vectors%result_vec, CZmat(:, j), nrow_local, control%local_comp)
524 1562863 : w_vec(:) = CZmat(:, j)
525 :
526 : ! Let's do the orthonormalization, to get the new f_vec. First try the Gram Schmidt scheme
527 : CALL Gram_Schmidt_ortho(h_vec, ar_data%f_vec, s_vec, w_vec, nrow_local, j, &
528 84583 : BZmat, Zmat, control%local_comp, control%pcol_group)
529 :
530 : ! A bit more expensive but simpliy always top up with a DGKS correction, otherwise numerics
531 : ! can becom a problem later on, there is probably a good check, but we don't perform it
532 : CALL DGKS_ortho(h_vec, ar_data%f_vec, s_vec, nrow_local, j, BZmat, &
533 84583 : Zmat, control%local_comp, control%pcol_group)
534 :
535 84583 : CALL transfer_local_array_to_dbcsr(vectors%input_vec, ar_data%f_vec, nrow_local, control%local_comp)
536 : CALL dbcsr_matrix_colvec_multiply(matrix(2)%matrix, vectors%input_vec, vectors%result_vec, 1.0_dp, &
537 84583 : 0.0_dp, vectors%rep_row_vec, vectors%rep_col_vec)
538 84583 : CALL transfer_dbcsr_to_local_array(vectors%result_vec, v_vec, nrow_local, control%local_comp)
539 : norm = 0.0_dp
540 1562863 : norm = DOT_PRODUCT(ar_data%f_vec, v_vec)
541 84583 : CALL pcol_group%sum(norm)
542 :
543 84583 : IF (control%myproc == 0) control%converged = REAL(norm, dp) < EPSILON(REAL(1.0, dp))
544 84583 : CALL control%mp_group%bcast(control%converged, 0)
545 84583 : IF (control%converged) EXIT
546 81952 : IF (j == control%max_iter - 1) EXIT
547 :
548 84583 : IF (control%local_comp) THEN
549 2871844 : Zmat(:, j + 1) = ar_data%f_vec/SQRT(norm); BZmat(:, j + 1) = v_vec(:)/SQRT(norm)
550 : END IF
551 : END DO
552 :
553 : ! getting a bit more complicated here as the final matrix is again a product which has to be computed with the
554 : ! distributed vectors, therefore a sum along the first proc_col is necessary. As we want that matrix everywhere,
555 : ! we set it to zero before and compute the distributed product only on the first col and then sum over the full grid
556 2365524 : ar_data%Hessenberg = 0.0_dp
557 5364 : IF (control%local_comp) THEN
558 : ar_data%Hessenberg(1:control%current_step, 1:control%current_step) = &
559 25506110 : MATMUL(TRANSPOSE(CZmat(:, 1:control%current_step)), Zmat(:, 1:control%current_step))
560 : END IF
561 4725684 : CALL control%mp_group%sum(ar_data%Hessenberg)
562 :
563 1748504 : ar_data%local_history = Zmat
564 : ! broadcast the Hessenberg matrix so we don't need to care later on
565 :
566 5364 : DEALLOCATE (v_vec); DEALLOCATE (w_vec); DEALLOCATE (s_vec); DEALLOCATE (h_vec); DEALLOCATE (CZmat)
567 5364 : DEALLOCATE (Zmat); DEALLOCATE (BZmat)
568 :
569 5364 : CALL timestop(handle)
570 :
571 10728 : END SUBROUTINE gev_build_subspace
572 :
573 : ! **************************************************************************************************
574 : !> \brief Updates all data after an inner loop of the generalized ev arnoldi.
575 : !> Updates rho and C=A-rho*B accordingly.
576 : !> As an update scheme is used for he ev, the output ev has to be replaced
577 : !> with the updated one.
578 : !> Furthermore a convergence check is performed. The mv product could
579 : !> be skiiped by making clever use of the precomputed data, However,
580 : !> it is most likely not worth the effort.
581 : !> \param matrix ...
582 : !> \param matrix_arnoldi ...
583 : !> \param vectors ...
584 : !> \param arnoldi_env ...
585 : ! **************************************************************************************************
586 5364 : SUBROUTINE gev_update_data(matrix, matrix_arnoldi, vectors, arnoldi_env)
587 : TYPE(dbcsr_p_type), DIMENSION(:) :: matrix, matrix_arnoldi
588 : TYPE(m_x_v_vectors_type) :: vectors
589 : TYPE(arnoldi_env_type) :: arnoldi_env
590 :
591 : CHARACTER(LEN=*), PARAMETER :: routineN = 'gev_update_data'
592 :
593 : COMPLEX(dp) :: val
594 : INTEGER :: handle, i, ind, ncol_local, nrow_local
595 : REAL(dp) :: rnorm
596 : REAL(kind=dp) :: norm
597 5364 : REAL(kind=dp), ALLOCATABLE, DIMENSION(:) :: v_vec
598 : TYPE(arnoldi_control_type), POINTER :: control
599 : TYPE(arnoldi_data_type), POINTER :: ar_data
600 :
601 5364 : CALL timeset(routineN, handle)
602 :
603 5364 : control => get_control(arnoldi_env)
604 :
605 5364 : ar_data => get_data(arnoldi_env)
606 :
607 : ! compute the new shift, hack around the problem templating the conversion
608 5364 : val = ar_data%evals(control%selected_ind(1))
609 5364 : ar_data%rho_scale = ar_data%rho_scale + REAL(val, dp)
610 : ! compute the new eigenvector / initial guess for the next arnoldi loop
611 87157 : ar_data%x_vec = 0.0_dp
612 89947 : DO i = 1, control%current_step
613 84583 : val = ar_data%revec(i, control%selected_ind(1))
614 1568227 : ar_data%x_vec(:) = ar_data%x_vec(:) + ar_data%local_history(:, i)*REAL(val, dp)
615 : END DO
616 : ! ar_data%x_vec(:)=MATMUL(ar_data%local_history(:,1:control%current_step),&
617 : ! ar_data%revec(1:control%current_step,control%selected_ind(1)))
618 :
619 : ! update the C-matrix (A-rho*B), if the maximum value is requested we have to use -A-rho*B
620 5364 : CALL dbcsr_copy(matrix_arnoldi(1)%matrix, matrix(1)%matrix)
621 5364 : CALL dbcsr_add(matrix_arnoldi(1)%matrix, matrix(2)%matrix, 1.0_dp, -ar_data%rho_scale)
622 :
623 : ! compute convergence and set the correct eigenvalue and eigenvector
624 5364 : CALL dbcsr_get_info(matrix=vectors%input_vec, nfullrows_local=nrow_local, nfullcols_local=ncol_local)
625 5364 : IF (ncol_local > 0) THEN
626 16061 : ALLOCATE (v_vec(nrow_local))
627 5364 : CALL compute_norms(ar_data%x_vec, norm, rnorm, control%pcol_group)
628 87157 : v_vec(:) = ar_data%x_vec(:)/rnorm
629 : END IF
630 5364 : CALL transfer_local_array_to_dbcsr(vectors%input_vec, v_vec, nrow_local, control%local_comp)
631 : CALL dbcsr_matrix_colvec_multiply(matrix_arnoldi(1)%matrix, vectors%input_vec, vectors%result_vec, 1.0_dp, &
632 5364 : 0.0_dp, vectors%rep_row_vec, vectors%rep_col_vec)
633 5364 : CALL transfer_dbcsr_to_local_array(vectors%result_vec, v_vec, nrow_local, control%local_comp)
634 5364 : IF (ncol_local > 0) THEN
635 5364 : CALL compute_norms(v_vec, norm, rnorm, control%pcol_group)
636 : ! check convergence
637 5364 : control%converged = rnorm < control%threshold
638 5364 : DEALLOCATE (v_vec)
639 : END IF
640 : ! and broadcast the real eigenvalue
641 5364 : CALL control%mp_group%bcast(control%converged, 0)
642 5364 : ind = control%selected_ind(1)
643 5364 : CALL control%mp_group%bcast(ar_data%rho_scale, 0)
644 :
645 : ! Again the maximum value request is done on -A therefore the eigenvalue needs the opposite sign
646 5364 : ar_data%evals(ind) = ar_data%rho_scale
647 :
648 5364 : CALL timestop(handle)
649 :
650 5364 : END SUBROUTINE gev_update_data
651 :
652 : ! **************************************************************************************************
653 : !> \brief Gram-Schmidt in matrix vector form
654 : !> \param h_vec ...
655 : !> \param f_vec ...
656 : !> \param s_vec ...
657 : !> \param w_vec ...
658 : !> \param nrow_local ...
659 : !> \param j ...
660 : !> \param local_history ...
661 : !> \param reorth_mat ...
662 : !> \param local_comp ...
663 : !> \param pcol_group ...
664 : ! **************************************************************************************************
665 442491 : SUBROUTINE Gram_Schmidt_ortho(h_vec, f_vec, s_vec, w_vec, nrow_local, &
666 442491 : j, local_history, reorth_mat, local_comp, pcol_group)
667 : REAL(kind=dp), DIMENSION(:) :: h_vec, f_vec, s_vec, w_vec
668 : INTEGER :: nrow_local, j
669 : REAL(kind=dp), DIMENSION(:, :) :: local_history, reorth_mat
670 : LOGICAL :: local_comp
671 : TYPE(mp_comm_type), INTENT(IN) :: pcol_group
672 :
673 : CHARACTER(LEN=*), PARAMETER :: routineN = 'Gram_Schmidt_ortho'
674 :
675 : INTEGER :: handle
676 :
677 442491 : CALL timeset(routineN, handle)
678 :
679 : ! Let's do the orthonormalization, first try the Gram Schmidt scheme
680 34769533 : h_vec = 0.0_dp; f_vec = 0.0_dp; s_vec = 0.0_dp
681 442491 : IF (local_comp) CALL dgemv('T', nrow_local, j, 1.0_dp, local_history, &
682 343455 : nrow_local, w_vec, 1, 0.0_dp, h_vec, 1)
683 8090479 : CALL pcol_group%sum(h_vec(1:j))
684 :
685 442491 : IF (local_comp) CALL dgemv('N', nrow_local, j, 1.0_dp, reorth_mat, &
686 343455 : nrow_local, h_vec, 1, 0.0_dp, f_vec, 1)
687 7288703 : f_vec(:) = w_vec(:) - f_vec(:)
688 :
689 442491 : CALL timestop(handle)
690 :
691 442491 : END SUBROUTINE Gram_Schmidt_ortho
692 :
693 : ! **************************************************************************************************
694 : !> \brief Compute the Daniel, Gragg, Kaufman and Steward correction
695 : !> \param h_vec ...
696 : !> \param f_vec ...
697 : !> \param s_vec ...
698 : !> \param nrow_local ...
699 : !> \param j ...
700 : !> \param local_history ...
701 : !> \param reorth_mat ...
702 : !> \param local_comp ...
703 : !> \param pcol_group ...
704 : ! **************************************************************************************************
705 442491 : SUBROUTINE DGKS_ortho(h_vec, f_vec, s_vec, nrow_local, j, &
706 884982 : local_history, reorth_mat, local_comp, pcol_group)
707 : REAL(kind=dp), DIMENSION(:) :: h_vec, f_vec, s_vec
708 : INTEGER :: nrow_local, j
709 : REAL(kind=dp), DIMENSION(:, :) :: local_history, reorth_mat
710 : LOGICAL :: local_comp
711 : TYPE(mp_comm_type), INTENT(IN) :: pcol_group
712 :
713 : CHARACTER(LEN=*), PARAMETER :: routineN = 'DGKS_ortho'
714 :
715 : INTEGER :: handle
716 :
717 442491 : CALL timeset(routineN, handle)
718 :
719 442491 : IF (local_comp) CALL dgemv('T', nrow_local, j, 1.0_dp, local_history, &
720 343455 : nrow_local, f_vec, 1, 0.0_dp, s_vec, 1)
721 8090479 : CALL pcol_group%sum(s_vec(1:j))
722 442491 : IF (local_comp) CALL dgemv('N', nrow_local, j, -1.0_dp, reorth_mat, &
723 343455 : nrow_local, s_vec, 1, 1.0_dp, f_vec, 1)
724 4266485 : h_vec(1:j) = h_vec(1:j) + s_vec(1:j)
725 :
726 442491 : CALL timestop(handle)
727 :
728 442491 : END SUBROUTINE DGKS_ortho
729 :
730 : ! **************************************************************************************************
731 : !> \brief Compute the norm of a vector distributed along proc_col
732 : !> as local arrays. Always return the real part next to the complex rep.
733 : !> \param vec ...
734 : !> \param norm ...
735 : !> \param rnorm ...
736 : !> \param pcol_group ...
737 : ! **************************************************************************************************
738 771346 : SUBROUTINE compute_norms(vec, norm, rnorm, pcol_group)
739 : REAL(kind=dp), DIMENSION(:) :: vec
740 : REAL(kind=dp) :: norm
741 : REAL(dp) :: rnorm
742 : TYPE(mp_comm_type), INTENT(IN) :: pcol_group
743 :
744 : ! the norm is computed along the processor column
745 8264068 : norm = DOT_PRODUCT(vec, vec)
746 771346 : CALL pcol_group%sum(norm)
747 771346 : rnorm = SQRT(REAL(norm, dp))
748 771346 : norm = rnorm
749 :
750 771346 : END SUBROUTINE compute_norms
751 :
752 124 : END MODULE arnoldi_methods
|