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 operations for skinny matrices/vectors expressed in dbcsr form
10 : !> \par History
11 : !> 2014.10 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_vector
17 : USE cp_dbcsr_api, ONLY: &
18 : dbcsr_copy, dbcsr_create, dbcsr_distribution_get, dbcsr_distribution_new, &
19 : dbcsr_distribution_release, dbcsr_distribution_type, dbcsr_get_block_p, dbcsr_get_info, &
20 : dbcsr_iterator_blocks_left, dbcsr_iterator_next_block, dbcsr_iterator_start, &
21 : dbcsr_iterator_stop, dbcsr_iterator_type, dbcsr_release, dbcsr_set, dbcsr_type, &
22 : dbcsr_type_antisymmetric, dbcsr_type_no_symmetry, dbcsr_type_symmetric
23 : USE cp_dbcsr_contrib, ONLY: dbcsr_reserve_all_blocks
24 : USE kinds, ONLY: dp,&
25 : real_8
26 : USE message_passing, ONLY: mp_comm_type
27 : #include "../base/base_uses.f90"
28 :
29 : !$ USE OMP_LIB, ONLY: omp_get_max_threads, omp_get_thread_num, omp_get_num_threads
30 :
31 : IMPLICIT NONE
32 :
33 : PRIVATE
34 :
35 : CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'arnoldi_vector'
36 :
37 : ! **************************************************************************************************
38 : !> \brief Types needed for the hashtable.
39 : ! **************************************************************************************************
40 : TYPE ele_type
41 : INTEGER :: c = 0
42 : INTEGER :: p = 0
43 : END TYPE ele_type
44 :
45 : TYPE hash_table_type
46 : TYPE(ele_type), DIMENSION(:), POINTER :: table => NULL()
47 : INTEGER :: nele = 0
48 : INTEGER :: nmax = 0
49 : INTEGER :: prime = 0
50 : END TYPE hash_table_type
51 :
52 : ! **************************************************************************************************
53 : !> \brief Types needed for fast access to distributed dbcsr vectors.
54 : ! **************************************************************************************************
55 : TYPE block_ptr
56 : REAL(real_8), DIMENSION(:, :), POINTER :: ptr => NULL()
57 : INTEGER :: assigned_thread = -1
58 : END TYPE block_ptr
59 :
60 : TYPE fast_vec_access_type
61 : TYPE(hash_table_type) :: hash_table = hash_table_type()
62 : TYPE(block_ptr), DIMENSION(:), ALLOCATABLE :: blk_map
63 : END TYPE fast_vec_access_type
64 :
65 : PUBLIC :: dbcsr_matrix_colvec_multiply, &
66 : create_col_vec_from_matrix, &
67 : create_row_vec_from_matrix, &
68 : create_replicated_col_vec_from_matrix, &
69 : create_replicated_row_vec_from_matrix, &
70 : transfer_dbcsr_to_local_array, &
71 : transfer_local_array_to_dbcsr
72 :
73 : CONTAINS
74 :
75 : ! **************************************************************************************************
76 : !> \brief creates a dbcsr col vector like object which lives on proc_col 0
77 : !> and has the same row dist as the template matrix
78 : !> the returned matrix is fully allocated and all blocks are set to 0
79 : !> this is not a sparse object (and must never be)
80 : !> \param dbcsr_vec the vector object to create must be allocated but not initialized
81 : !> \param matrix a dbcsr matrix used as template
82 : !> \param ncol number of vectors in the dbcsr_typeect (1 for vector, n for skinny matrix)
83 : ! **************************************************************************************************
84 281245 : SUBROUTINE create_col_vec_from_matrix(dbcsr_vec, matrix, ncol)
85 : TYPE(dbcsr_type) :: dbcsr_vec, matrix
86 : INTEGER :: ncol
87 :
88 : CHARACTER(LEN=*), PARAMETER :: routineN = 'create_col_vec_from_matrix'
89 :
90 : INTEGER :: handle, npcols
91 281245 : INTEGER, CONTIGUOUS, DIMENSION(:), POINTER :: col_blk_size, col_dist, row_blk_size, &
92 281245 : row_dist
93 : TYPE(dbcsr_distribution_type) :: dist, dist_col_vec
94 :
95 281245 : CALL timeset(routineN, handle)
96 :
97 281245 : CALL dbcsr_get_info(matrix, row_blk_size=row_blk_size, distribution=dist)
98 281245 : CALL dbcsr_distribution_get(dist, npcols=npcols, row_dist=row_dist)
99 :
100 281245 : ALLOCATE (col_dist(1), col_blk_size(1))
101 281245 : col_dist(1) = 0
102 281245 : col_blk_size(1) = ncol
103 281245 : CALL dbcsr_distribution_new(dist_col_vec, template=dist, row_dist=row_dist, col_dist=col_dist)
104 :
105 : CALL dbcsr_create(dbcsr_vec, "D", dist_col_vec, matrix_type=dbcsr_type_no_symmetry, &
106 281245 : row_blk_size=row_blk_size, col_blk_size=col_blk_size)
107 281245 : CALL dbcsr_reserve_all_blocks(dbcsr_vec)
108 :
109 281245 : CALL dbcsr_distribution_release(dist_col_vec)
110 281245 : DEALLOCATE (col_dist, col_blk_size)
111 281245 : CALL timestop(handle)
112 :
113 562490 : END SUBROUTINE create_col_vec_from_matrix
114 :
115 : ! **************************************************************************************************
116 : !> \brief creates a dbcsr row vector like object which lives on proc_row 0
117 : !> and has the same row dist as the template matrix
118 : !> the returned matrix is fully allocated and all blocks are set to 0
119 : !> this is not a sparse object (and must never be)
120 : !> \param dbcsr_vec ...
121 : !> \param matrix a dbcsr matrix used as template
122 : !> \param nrow number of vectors in the dbcsr_typeect (1 for vector, n for skinny matrix)
123 : ! **************************************************************************************************
124 0 : SUBROUTINE create_row_vec_from_matrix(dbcsr_vec, matrix, nrow)
125 : TYPE(dbcsr_type) :: dbcsr_vec, matrix
126 : INTEGER :: nrow
127 :
128 : CHARACTER(LEN=*), PARAMETER :: routineN = 'create_row_vec_from_matrix'
129 :
130 : INTEGER :: handle, nprows
131 0 : INTEGER, CONTIGUOUS, DIMENSION(:), POINTER :: col_blk_size, col_dist, row_blk_size, &
132 0 : row_dist
133 : TYPE(dbcsr_distribution_type) :: dist, dist_row_vec
134 :
135 0 : CALL timeset(routineN, handle)
136 :
137 0 : CALL dbcsr_get_info(matrix, col_blk_size=col_blk_size, distribution=dist)
138 0 : CALL dbcsr_distribution_get(dist, nprows=nprows, col_dist=col_dist)
139 :
140 0 : ALLOCATE (row_dist(1), row_blk_size(1))
141 0 : row_dist(1) = 0
142 0 : row_blk_size(1) = nrow
143 0 : CALL dbcsr_distribution_new(dist_row_vec, template=dist, row_dist=row_dist, col_dist=col_dist)
144 :
145 : CALL dbcsr_create(dbcsr_vec, "D", dist_row_vec, matrix_type=dbcsr_type_no_symmetry, &
146 0 : row_blk_size=row_blk_size, col_blk_size=col_blk_size)
147 0 : CALL dbcsr_reserve_all_blocks(dbcsr_vec)
148 :
149 0 : CALL dbcsr_distribution_release(dist_row_vec)
150 0 : DEALLOCATE (row_dist, row_blk_size)
151 0 : CALL timestop(handle)
152 :
153 0 : END SUBROUTINE create_row_vec_from_matrix
154 :
155 : ! **************************************************************************************************
156 : !> \brief creates a col vector like object whose blocks can be replicated
157 : !> along the processor row and has the same row dist as the template matrix
158 : !> the returned matrix is fully allocated and all blocks are set to 0
159 : !> this is not a sparse object (and must never be)
160 : !> \param dbcsr_vec the vector object to create must be allocated but not initialized
161 : !> \param matrix a dbcsr matrix used as template
162 : !> \param ncol number of vectors in the dbcsr_typeect (1 for vector, n for skinny matrix)
163 : ! **************************************************************************************************
164 138051 : SUBROUTINE create_replicated_col_vec_from_matrix(dbcsr_vec, matrix, ncol)
165 : TYPE(dbcsr_type) :: dbcsr_vec, matrix
166 : INTEGER :: ncol
167 :
168 : CHARACTER(LEN=*), PARAMETER :: routineN = 'create_replicated_col_vec_from_matrix'
169 :
170 : INTEGER :: handle, i, npcols
171 138051 : INTEGER, CONTIGUOUS, DIMENSION(:), POINTER :: col_blk_size, col_dist, row_blk_size, &
172 138051 : row_dist
173 : TYPE(dbcsr_distribution_type) :: dist, dist_col_vec
174 :
175 138051 : CALL timeset(routineN, handle)
176 :
177 138051 : CALL dbcsr_get_info(matrix, row_blk_size=row_blk_size, distribution=dist)
178 138051 : CALL dbcsr_distribution_get(dist, npcols=npcols, row_dist=row_dist)
179 :
180 552204 : ALLOCATE (col_dist(npcols), col_blk_size(npcols))
181 276102 : col_blk_size(:) = ncol
182 276102 : DO i = 0, npcols - 1
183 276102 : col_dist(i + 1) = i
184 : END DO
185 138051 : CALL dbcsr_distribution_new(dist_col_vec, template=dist, row_dist=row_dist, col_dist=col_dist)
186 :
187 : CALL dbcsr_create(dbcsr_vec, "D", dist_col_vec, matrix_type=dbcsr_type_no_symmetry, &
188 138051 : row_blk_size=row_blk_size, col_blk_size=col_blk_size)
189 138051 : CALL dbcsr_reserve_all_blocks(dbcsr_vec)
190 :
191 138051 : CALL dbcsr_distribution_release(dist_col_vec)
192 138051 : DEALLOCATE (col_dist, col_blk_size)
193 138051 : CALL timestop(handle)
194 :
195 276102 : END SUBROUTINE create_replicated_col_vec_from_matrix
196 :
197 : ! **************************************************************************************************
198 : !> \brief creates a row vector like object whose blocks can be replicated
199 : !> along the processor col and has the same col dist as the template matrix
200 : !> the returned matrix is fully allocated and all blocks are set to 0
201 : !> this is not a sparse object (and must never be)
202 : !> \param dbcsr_vec the vector object to create must be allocated but not initialized
203 : !> \param matrix a dbcsr matrix used as template
204 : !> \param nrow number of vectors in the dbcsr_typeect (1 for vector, n for skinny matrix)
205 : ! **************************************************************************************************
206 138051 : SUBROUTINE create_replicated_row_vec_from_matrix(dbcsr_vec, matrix, nrow)
207 : TYPE(dbcsr_type) :: dbcsr_vec, matrix
208 : INTEGER :: nrow
209 :
210 : CHARACTER(LEN=*), PARAMETER :: routineN = 'create_replicated_row_vec_from_matrix'
211 :
212 : INTEGER :: handle, i, nprows
213 138051 : INTEGER, CONTIGUOUS, DIMENSION(:), POINTER :: col_blk_size, col_dist, row_blk_size, &
214 138051 : row_dist
215 : TYPE(dbcsr_distribution_type) :: dist, dist_row_vec
216 :
217 138051 : CALL timeset(routineN, handle)
218 :
219 138051 : CALL dbcsr_get_info(matrix, distribution=dist, col_blk_size=col_blk_size)
220 138051 : CALL dbcsr_distribution_get(dist, nprows=nprows, col_dist=col_dist)
221 :
222 552204 : ALLOCATE (row_dist(nprows), row_blk_size(nprows))
223 411762 : row_blk_size(:) = nrow
224 411762 : DO i = 0, nprows - 1
225 411762 : row_dist(i + 1) = i
226 : END DO
227 138051 : CALL dbcsr_distribution_new(dist_row_vec, template=dist, row_dist=row_dist, col_dist=col_dist)
228 :
229 : CALL dbcsr_create(dbcsr_vec, "D", dist_row_vec, dbcsr_type_no_symmetry, &
230 138051 : row_blk_size=row_blk_size, col_blk_size=col_blk_size)
231 138051 : CALL dbcsr_reserve_all_blocks(dbcsr_vec)
232 :
233 138051 : CALL dbcsr_distribution_release(dist_row_vec)
234 138051 : DEALLOCATE (row_dist, row_blk_Size)
235 138051 : CALL timestop(handle)
236 :
237 276102 : END SUBROUTINE create_replicated_row_vec_from_matrix
238 :
239 : ! **************************************************************************************************
240 : !> \brief given a column vector, prepare the fast_vec_access container
241 : !> \param vec ...
242 : !> \param fast_vec_access ...
243 : ! **************************************************************************************************
244 986977 : SUBROUTINE create_fast_col_vec_access(vec, fast_vec_access)
245 : TYPE(dbcsr_type) :: vec
246 : TYPE(fast_vec_access_type) :: fast_vec_access
247 :
248 : CHARACTER(LEN=*), PARAMETER :: routineN = 'create_fast_col_vec_access'
249 :
250 : INTEGER :: col, handle, iblock, nblk_local, &
251 : nthreads, row
252 986977 : REAL(kind=real_8), DIMENSION(:, :), POINTER :: vec_bl
253 : TYPE(dbcsr_iterator_type) :: iter
254 :
255 986977 : CALL timeset(routineN, handle)
256 :
257 : ! figure out the number of threads
258 986977 : nthreads = 1
259 986977 : !$OMP PARALLEL DEFAULT(NONE) SHARED(nthreads)
260 : !$OMP MASTER
261 : !$ nthreads = OMP_GET_NUM_THREADS()
262 : !$OMP END MASTER
263 : !$OMP END PARALLEL
264 :
265 986977 : CALL dbcsr_get_info(matrix=vec, nblkrows_local=nblk_local)
266 : ! 4 times makes sure the table is big enough to limit collisions.
267 986977 : CALL hash_table_create(fast_vec_access%hash_table, 4*nblk_local)
268 : ! include zero for effective dealing with values not in the hash table (will return 0)
269 6444953 : ALLOCATE (fast_vec_access%blk_map(0:nblk_local))
270 :
271 986977 : CALL dbcsr_get_info(matrix=vec, nblkcols_local=col)
272 986977 : IF (col > 1) CPABORT("BUG")
273 :
274 : ! go through the blocks of the vector
275 986977 : iblock = 0
276 986977 : CALL dbcsr_iterator_start(iter, vec)
277 3484022 : DO WHILE (dbcsr_iterator_blocks_left(iter))
278 2497045 : CALL dbcsr_iterator_next_block(iter, row, col, vec_bl)
279 2497045 : iblock = iblock + 1
280 2497045 : CALL hash_table_add(fast_vec_access%hash_table, row, iblock)
281 2497045 : fast_vec_access%blk_map(iblock)%ptr => vec_bl
282 2497045 : fast_vec_access%blk_map(iblock)%assigned_thread = MOD(iblock, nthreads)
283 : END DO
284 986977 : CALL dbcsr_iterator_stop(iter)
285 986977 : CALL timestop(handle)
286 :
287 2960931 : END SUBROUTINE create_fast_col_vec_access
288 :
289 : ! **************************************************************************************************
290 : !> \brief given a row vector, prepare the fast_vec_access_container
291 : !> \param vec ...
292 : !> \param fast_vec_access ...
293 : ! **************************************************************************************************
294 986977 : SUBROUTINE create_fast_row_vec_access(vec, fast_vec_access)
295 : TYPE(dbcsr_type) :: vec
296 : TYPE(fast_vec_access_type) :: fast_vec_access
297 :
298 : CHARACTER(LEN=*), PARAMETER :: routineN = 'create_fast_row_vec_access'
299 :
300 : INTEGER :: col, handle, iblock, nblk_local, &
301 : nthreads, row
302 986977 : REAL(kind=real_8), DIMENSION(:, :), POINTER :: vec_bl
303 : TYPE(dbcsr_iterator_type) :: iter
304 :
305 986977 : CALL timeset(routineN, handle)
306 :
307 : ! figure out the number of threads
308 986977 : nthreads = 1
309 986977 : !$OMP PARALLEL DEFAULT(NONE) SHARED(nthreads)
310 : !$OMP MASTER
311 : !$ nthreads = OMP_GET_NUM_THREADS()
312 : !$OMP END MASTER
313 : !$OMP END PARALLEL
314 :
315 986977 : CALL dbcsr_get_info(matrix=vec, nblkcols_local=nblk_local)
316 : ! 4 times makes sure the table is big enough to limit collisions.
317 986977 : CALL hash_table_create(fast_vec_access%hash_table, 4*nblk_local)
318 : ! include zero for effective dealing with values not in the hash table (will return 0)
319 8916725 : ALLOCATE (fast_vec_access%blk_map(0:nblk_local))
320 :
321 : ! sanity check
322 986977 : CALL dbcsr_get_info(matrix=vec, nblkrows_local=row)
323 986977 : IF (row > 1) CPABORT("BUG")
324 :
325 : ! go through the blocks of the vector
326 986977 : iblock = 0
327 986977 : CALL dbcsr_iterator_start(iter, vec)
328 5955794 : DO WHILE (dbcsr_iterator_blocks_left(iter))
329 4968817 : CALL dbcsr_iterator_next_block(iter, row, col, vec_bl)
330 4968817 : iblock = iblock + 1
331 4968817 : CALL hash_table_add(fast_vec_access%hash_table, col, iblock)
332 4968817 : fast_vec_access%blk_map(iblock)%ptr => vec_bl
333 4968817 : fast_vec_access%blk_map(iblock)%assigned_thread = MOD(iblock, nthreads)
334 : END DO
335 986977 : CALL dbcsr_iterator_stop(iter)
336 :
337 986977 : CALL timestop(handle)
338 :
339 2960931 : END SUBROUTINE create_fast_row_vec_access
340 :
341 : ! **************************************************************************************************
342 : !> \brief release all memory associated with the fast_vec_access type
343 : !> \param fast_vec_access ...
344 : ! **************************************************************************************************
345 1973954 : SUBROUTINE release_fast_vec_access(fast_vec_access)
346 : TYPE(fast_vec_access_type) :: fast_vec_access
347 :
348 : CHARACTER(LEN=*), PARAMETER :: routineN = 'release_fast_vec_access'
349 :
350 : INTEGER :: handle
351 :
352 1973954 : CALL timeset(routineN, handle)
353 :
354 1973954 : CALL hash_table_release(fast_vec_access%hash_table)
355 1973954 : DEALLOCATE (fast_vec_access%blk_map)
356 :
357 1973954 : CALL timestop(handle)
358 :
359 1973954 : END SUBROUTINE release_fast_vec_access
360 :
361 : ! --------------------------------------------------------------------------------------------------
362 : ! Beginning of hashtable.
363 : ! this file can be 'INCLUDE'ed verbatim in various place, where it needs to be
364 : ! part of the module to guarantee inlining
365 : ! hashes (c,p) pairs, where p is assumed to be >0
366 : ! on return (0 is used as a flag for not present)
367 : !
368 : !
369 : ! **************************************************************************************************
370 : !> \brief finds a prime equal or larger than i, needed at creation
371 : !> \param i ...
372 : !> \return ...
373 : ! **************************************************************************************************
374 1973954 : FUNCTION matching_prime(i) RESULT(res)
375 : INTEGER, INTENT(IN) :: i
376 : INTEGER :: res
377 :
378 : INTEGER :: j
379 :
380 1973954 : res = i
381 1973954 : j = 0
382 5843910 : DO WHILE (j < res)
383 51348309 : DO j = 2, res - 1
384 49374355 : IF (MOD(res, j) == 0) THEN
385 1896002 : res = res + 1
386 1896002 : EXIT
387 : END IF
388 : END DO
389 : END DO
390 1973954 : END FUNCTION matching_prime
391 :
392 : ! **************************************************************************************************
393 : !> \brief create a hash_table of given initial size.
394 : !> the hash table will expand as needed (but this requires rehashing)
395 : !> \param hash_table ...
396 : !> \param table_size ...
397 : ! **************************************************************************************************
398 1973954 : SUBROUTINE hash_table_create(hash_table, table_size)
399 : TYPE(hash_table_type) :: hash_table
400 : INTEGER, INTENT(IN) :: table_size
401 :
402 : INTEGER :: j
403 :
404 : ! guarantee a minimal hash table size (8), so that expansion works
405 :
406 1973954 : j = 3
407 3794028 : DO WHILE (2**j - 1 < table_size)
408 1820074 : j = j + 1
409 : END DO
410 1973954 : hash_table%nmax = 2**j - 1
411 1973954 : hash_table%prime = matching_prime(hash_table%nmax)
412 1973954 : hash_table%nele = 0
413 54147678 : ALLOCATE (hash_table%table(0:hash_table%nmax))
414 1973954 : END SUBROUTINE hash_table_create
415 :
416 : ! **************************************************************************************************
417 : !> \brief ...
418 : !> \param hash_table ...
419 : ! **************************************************************************************************
420 1973954 : SUBROUTINE hash_table_release(hash_table)
421 : TYPE(hash_table_type) :: hash_table
422 :
423 1973954 : hash_table%nmax = 0
424 1973954 : hash_table%nele = 0
425 1973954 : DEALLOCATE (hash_table%table)
426 :
427 1973954 : END SUBROUTINE hash_table_release
428 :
429 : ! **************************************************************************************************
430 : !> \brief add a pair (c,p) to the hash table
431 : !> \param hash_table ...
432 : !> \param c this value is being hashed
433 : !> \param p this is being stored
434 : ! **************************************************************************************************
435 7465862 : RECURSIVE SUBROUTINE hash_table_add(hash_table, c, p)
436 : TYPE(hash_table_type), INTENT(INOUT) :: hash_table
437 : INTEGER, INTENT(IN) :: c, p
438 :
439 : REAL(KIND=real_8), PARAMETER :: hash_table_expand = 1.5_real_8, &
440 : inv_hash_table_fill = 2.5_real_8
441 :
442 : INTEGER :: i, j
443 7465862 : TYPE(ele_type), ALLOCATABLE, DIMENSION(:) :: tmp_hash
444 :
445 : ! if too small, make a copy and rehash in a larger table
446 :
447 7465862 : IF (hash_table%nele*inv_hash_table_fill > hash_table%nmax) THEN
448 0 : ALLOCATE (tmp_hash(LBOUND(hash_table%table, 1):UBOUND(hash_table%table, 1)))
449 0 : tmp_hash(:) = hash_table%table
450 0 : CALL hash_table_release(hash_table)
451 0 : CALL hash_table_create(hash_table, INT((UBOUND(tmp_hash, 1) + 8)*hash_table_expand))
452 0 : DO i = LBOUND(tmp_hash, 1), UBOUND(tmp_hash, 1)
453 0 : IF (tmp_hash(i)%c /= 0) THEN
454 0 : CALL hash_table_add(hash_table, tmp_hash(i)%c, tmp_hash(i)%p)
455 : END IF
456 : END DO
457 0 : DEALLOCATE (tmp_hash)
458 : END IF
459 :
460 7465862 : hash_table%nele = hash_table%nele + 1
461 7465862 : i = IAND(c*hash_table%prime, hash_table%nmax)
462 :
463 7465862 : DO j = i, hash_table%nmax
464 7465862 : IF (hash_table%table(j)%c == 0 .OR. hash_table%table(j)%c == c) THEN
465 7465862 : hash_table%table(j)%c = c
466 7465862 : hash_table%table(j)%p = p
467 7465862 : RETURN
468 : END IF
469 : END DO
470 0 : DO j = 0, i - 1
471 0 : IF (hash_table%table(j)%c == 0 .OR. hash_table%table(j)%c == c) THEN
472 0 : hash_table%table(j)%c = c
473 0 : hash_table%table(j)%p = p
474 0 : RETURN
475 : END IF
476 : END DO
477 :
478 : END SUBROUTINE hash_table_add
479 :
480 : ! **************************************************************************************************
481 : !> \brief ...
482 : !> \param hash_table ...
483 : !> \param c ...
484 : !> \return ...
485 : ! **************************************************************************************************
486 126603197 : PURE FUNCTION hash_table_get(hash_table, c) RESULT(p)
487 : TYPE(hash_table_type), INTENT(IN) :: hash_table
488 : INTEGER, INTENT(IN) :: c
489 : INTEGER :: p
490 :
491 : INTEGER :: i, j
492 :
493 126603197 : i = IAND(c*hash_table%prime, hash_table%nmax)
494 :
495 : ! catch the likely case first
496 126603197 : IF (hash_table%table(i)%c == c) THEN
497 122405227 : p = hash_table%table(i)%p
498 122405227 : RETURN
499 : END IF
500 :
501 4197970 : DO j = i, hash_table%nmax
502 4197970 : IF (hash_table%table(j)%c == 0 .OR. hash_table%table(j)%c == c) THEN
503 4197970 : p = hash_table%table(j)%p
504 4197970 : RETURN
505 : END IF
506 : END DO
507 0 : DO j = 0, i - 1
508 0 : IF (hash_table%table(j)%c == 0 .OR. hash_table%table(j)%c == c) THEN
509 0 : p = hash_table%table(j)%p
510 0 : RETURN
511 : END IF
512 : END DO
513 :
514 : ! we should never reach this point.
515 126603197 : p = HUGE(p)
516 :
517 : END FUNCTION hash_table_get
518 :
519 : ! End of hashtable
520 : ! --------------------------------------------------------------------------------------------------
521 :
522 : ! **************************************************************************************************
523 : !> \brief the real driver routine for the multiply, not all symmetries implemented yet
524 : !> \param matrix ...
525 : !> \param vec_in ...
526 : !> \param vec_out ...
527 : !> \param alpha ...
528 : !> \param beta ...
529 : !> \param work_row ...
530 : !> \param work_col ...
531 : ! **************************************************************************************************
532 693468 : SUBROUTINE dbcsr_matrix_colvec_multiply(matrix, vec_in, vec_out, alpha, beta, work_row, work_col)
533 : TYPE(dbcsr_type) :: matrix, vec_in, vec_out
534 : REAL(kind=real_8) :: alpha, beta
535 : TYPE(dbcsr_type) :: work_row, work_col
536 :
537 : CHARACTER :: matrix_type
538 :
539 693468 : CALL dbcsr_get_info(matrix, matrix_type=matrix_type)
540 :
541 399959 : SELECT CASE (matrix_type)
542 : CASE (dbcsr_type_no_symmetry)
543 399959 : CALL dbcsr_matrix_vector_mult(matrix, vec_in, vec_out, alpha, beta, work_row, work_col)
544 : CASE (dbcsr_type_symmetric)
545 293509 : CALL dbcsr_sym_matrix_vector_mult(matrix, vec_in, vec_out, alpha, beta, work_row, work_col)
546 : CASE (dbcsr_type_antisymmetric)
547 : ! Not yet implemented, should mainly be some prefactor magic, but who knows how antisymmetric matrices are stored???
548 0 : CPABORT("NYI, antisymmetric matrix not permitted")
549 : CASE DEFAULT
550 693468 : CPABORT("Unknown matrix type, ...")
551 : END SELECT
552 :
553 693468 : END SUBROUTINE dbcsr_matrix_colvec_multiply
554 :
555 : ! **************************************************************************************************
556 : !> \brief low level routines for matrix vector multiplies
557 : !> \param matrix ...
558 : !> \param vec_in ...
559 : !> \param vec_out ...
560 : !> \param alpha ...
561 : !> \param beta ...
562 : !> \param work_row ...
563 : !> \param work_col ...
564 : ! **************************************************************************************************
565 399959 : SUBROUTINE dbcsr_matrix_vector_mult(matrix, vec_in, vec_out, alpha, beta, work_row, work_col)
566 : TYPE(dbcsr_type) :: matrix, vec_in, vec_out
567 : REAL(kind=real_8) :: alpha, beta
568 : TYPE(dbcsr_type) :: work_row, work_col
569 :
570 : CHARACTER(LEN=*), PARAMETER :: routineN = 'dbcsr_matrix_vector_mult'
571 :
572 : INTEGER :: col, handle, handle1, ithread, k, m, &
573 : mypcol, myprow, n, pcol, prow, &
574 : prow_handle, row
575 399959 : REAL(kind=real_8), DIMENSION(:, :), POINTER :: data_d, vec_res
576 : TYPE(dbcsr_distribution_type) :: dist
577 : TYPE(dbcsr_iterator_type) :: iter
578 399959 : TYPE(fast_vec_access_type) :: fast_vec_col, fast_vec_row
579 : TYPE(mp_comm_type) :: prow_group
580 :
581 399959 : CALL timeset(routineN, handle)
582 399959 : ithread = 0
583 :
584 : ! Collect some data about the parallel environment. We will use them later to move the vector around
585 399959 : CALL dbcsr_get_info(matrix, distribution=dist)
586 399959 : CALL dbcsr_distribution_get(dist, prow_group=prow_handle, myprow=myprow, mypcol=mypcol)
587 :
588 399959 : CALL prow_group%set_handle(prow_handle)
589 :
590 399959 : CALL create_fast_row_vec_access(work_row, fast_vec_row)
591 399959 : CALL create_fast_col_vec_access(work_col, fast_vec_col)
592 :
593 : ! Transfer the correct parts of the input vector to the correct locations so we can do a local multiply
594 399959 : CALL dbcsr_col_vec_to_rep_row(vec_in, work_col, work_row, fast_vec_col)
595 :
596 : ! Set the work vector for the results to 0
597 399959 : CALL dbcsr_set(work_col, 0.0_real_8)
598 :
599 : ! Perform the local multiply. Here we exploit, that we have the blocks replicated on the mpi processes
600 : ! It is important to note, that the input and result vector are distributed differently (row wise, col wise respectively)
601 399959 : CALL timeset(routineN//"_local_mm", handle1)
602 :
603 : !$OMP PARALLEL DEFAULT(NONE) PRIVATE(row,col,iter,data_d,ithread,pcol,prow,m,n,k) &
604 399959 : !$OMP SHARED(matrix,fast_vec_col,fast_vec_row)
605 : !$ ithread = omp_get_thread_num()
606 : CALL dbcsr_iterator_start(iter, matrix, shared=.FALSE.)
607 : DO WHILE (dbcsr_iterator_blocks_left(iter))
608 : CALL dbcsr_iterator_next_block(iter, row, col, data_d)
609 : prow = hash_table_get(fast_vec_col%hash_table, row)
610 : IF (fast_vec_col%blk_map(prow)%assigned_thread /= ithread) CYCLE
611 : m = SIZE(fast_vec_col%blk_map(prow)%ptr, 1)
612 : n = SIZE(fast_vec_col%blk_map(prow)%ptr, 2)
613 : k = SIZE(data_d, 2)
614 : IF ((m == 0) .OR. (n == 0) .OR. (k == 0)) CYCLE
615 : CPASSERT(n == 1)
616 : pcol = hash_table_get(fast_vec_row%hash_table, col)
617 : CALL dgemm('N', 'T', m, n, k, &
618 : 1.0_dp, data_d, m, &
619 : fast_vec_row%blk_map(pcol)%ptr, n, &
620 : 1.0_dp, &
621 : fast_vec_col%blk_map(prow)%ptr, m)
622 : END DO
623 : CALL dbcsr_iterator_stop(iter)
624 : !$OMP END PARALLEL
625 :
626 399959 : CALL timestop(handle1)
627 :
628 : ! sum all the data onto the first processor col where the original vector is stored
629 399959 : CALL sum_replicated_vector_blocks(work_col, prow_group)
630 :
631 : ! Local copy on the first mpi col (as this is the localtion of the vec_res blocks) of the result vector
632 : ! from the replicated to the original vector. Let's play it safe and use the iterator
633 399959 : CALL dbcsr_iterator_start(iter, vec_out)
634 770040 : DO WHILE (dbcsr_iterator_blocks_left(iter))
635 370081 : CALL dbcsr_iterator_next_block(iter, row, col, vec_res)
636 370081 : prow = hash_table_get(fast_vec_col%hash_table, row)
637 770040 : IF (ASSOCIATED(fast_vec_col%blk_map(prow)%ptr)) THEN
638 4944690 : vec_res(:, :) = beta*vec_res(:, :) + alpha*fast_vec_col%blk_map(prow)%ptr(:, :)
639 : ELSE
640 0 : vec_res(:, :) = beta*vec_res(:, :)
641 : END IF
642 : END DO
643 399959 : CALL dbcsr_iterator_stop(iter)
644 :
645 399959 : CALL release_fast_vec_access(fast_vec_row)
646 399959 : CALL release_fast_vec_access(fast_vec_col)
647 :
648 399959 : CALL timestop(handle)
649 :
650 799918 : END SUBROUTINE dbcsr_matrix_vector_mult
651 :
652 : ! **************************************************************************************************
653 : !> \brief ...
654 : !> \param matrix ...
655 : !> \param vec_in ...
656 : !> \param vec_out ...
657 : !> \param alpha ...
658 : !> \param beta ...
659 : !> \param work_row ...
660 : !> \param work_col ...
661 : !> \param skip_diag ...
662 : ! **************************************************************************************************
663 0 : SUBROUTINE dbcsr_matrixT_vector_mult(matrix, vec_in, vec_out, alpha, beta, work_row, work_col, skip_diag)
664 : TYPE(dbcsr_type) :: matrix, vec_in, vec_out
665 : REAL(kind=real_8) :: alpha, beta
666 : TYPE(dbcsr_type) :: work_row, work_col
667 : LOGICAL :: skip_diag
668 :
669 : CHARACTER(LEN=*), PARAMETER :: routineN = 'dbcsr_matrixT_vector_mult'
670 :
671 : INTEGER :: col, col_size, handle, handle1, ithread, &
672 : mypcol, myprow, ncols, pcol, &
673 : pcol_handle, prow, prow_handle, row, &
674 : row_size
675 0 : REAL(kind=real_8), DIMENSION(:, :), POINTER :: data_d, vec_bl, vec_res
676 : TYPE(dbcsr_distribution_type) :: dist
677 : TYPE(dbcsr_iterator_type) :: iter
678 0 : TYPE(fast_vec_access_type) :: fast_vec_col, fast_vec_row
679 : TYPE(mp_comm_type) :: pcol_group, prow_group
680 :
681 0 : CALL timeset(routineN, handle)
682 0 : ithread = 0
683 :
684 : ! Collect some data about the parallel environment. We will use them later to move the vector around
685 0 : CALL dbcsr_get_info(matrix, distribution=dist)
686 0 : CALL dbcsr_distribution_get(dist, prow_group=prow_handle, pcol_group=pcol_handle, myprow=myprow, mypcol=mypcol)
687 :
688 0 : CALL prow_group%set_handle(prow_handle)
689 0 : CALL pcol_group%set_handle(pcol_handle)
690 :
691 0 : CALL create_fast_row_vec_access(work_row, fast_vec_row)
692 0 : CALL create_fast_col_vec_access(work_col, fast_vec_col)
693 :
694 : ! Set the work vector for the results to 0
695 0 : CALL dbcsr_set(work_row, 0.0_real_8)
696 :
697 : ! Transfer the correct parts of the input vector to the replicated vector on proc_col 0
698 0 : CALL dbcsr_iterator_start(iter, vec_in)
699 0 : DO WHILE (dbcsr_iterator_blocks_left(iter))
700 0 : CALL dbcsr_iterator_next_block(iter, row, col, vec_bl, row_size=row_size, col_size=col_size)
701 0 : prow = hash_table_get(fast_vec_col%hash_table, row)
702 0 : fast_vec_col%blk_map(prow)%ptr(1:row_size, 1:col_size) = vec_bl(1:row_size, 1:col_size)
703 : END DO
704 0 : CALL dbcsr_iterator_stop(iter)
705 : ! Replicate the data on all processore in the row
706 0 : CALL bcast_replicated_vector_blocks(work_col, prow_group, 0)
707 :
708 : ! Perform the local multiply. Here it is obvious why the vectors are replicated on the mpi rows and cols
709 0 : CALL timeset(routineN//"local_mm", handle1)
710 0 : CALL dbcsr_get_info(matrix=work_col, nfullcols_local=ncols)
711 : !$OMP PARALLEL DEFAULT(NONE) PRIVATE(row,col,iter,data_d,row_size,col_size,ithread,prow,pcol) &
712 0 : !$OMP SHARED(matrix,fast_vec_row,fast_vec_col,skip_diag,ncols)
713 : !$ ithread = omp_get_thread_num()
714 : CALL dbcsr_iterator_start(iter, matrix, shared=.FALSE.)
715 : DO WHILE (dbcsr_iterator_blocks_left(iter))
716 : CALL dbcsr_iterator_next_block(iter, row, col, data_d, row_size=row_size, col_size=col_size)
717 : IF (skip_diag .AND. col == row) CYCLE
718 : prow = hash_table_get(fast_vec_col%hash_table, row)
719 : pcol = hash_table_get(fast_vec_row%hash_table, col)
720 : IF (ASSOCIATED(fast_vec_row%blk_map(pcol)%ptr) .AND. &
721 : ASSOCIATED(fast_vec_col%blk_map(prow)%ptr)) THEN
722 : IF (fast_vec_row%blk_map(pcol)%assigned_thread /= ithread) CYCLE
723 : fast_vec_row%blk_map(pcol)%ptr = fast_vec_row%blk_map(pcol)%ptr + &
724 : MATMUL(TRANSPOSE(fast_vec_col%blk_map(prow)%ptr), data_d)
725 : ELSE
726 : prow = hash_table_get(fast_vec_row%hash_table, row)
727 : pcol = hash_table_get(fast_vec_col%hash_table, col)
728 : IF (fast_vec_row%blk_map(prow)%assigned_thread /= ithread) CYCLE
729 : fast_vec_row%blk_map(prow)%ptr = fast_vec_row%blk_map(prow)%ptr + &
730 : MATMUL(TRANSPOSE(fast_vec_col%blk_map(pcol)%ptr), TRANSPOSE(data_d))
731 : END IF
732 : END DO
733 : CALL dbcsr_iterator_stop(iter)
734 : !$OMP END PARALLEL
735 :
736 0 : CALL timestop(handle1)
737 :
738 : ! sum all the data within a processor column to obtain the replicated result
739 : ! we use the replicated vector but the final answer is only summed to proc_col 0 for efficiency
740 0 : CALL sum_replicated_vector_blocks(work_row, pcol_group)
741 :
742 : ! Convert the result to a column wise distribution
743 0 : CALL dbcsr_rep_row_to_rep_col_vec(work_col, work_row, fast_vec_row)
744 :
745 : ! Create_the final vector by summing it to the result vector which lives on proc_col 0
746 0 : CALL dbcsr_iterator_start(iter, vec_out)
747 0 : DO WHILE (dbcsr_iterator_blocks_left(iter))
748 0 : CALL dbcsr_iterator_next_block(iter, row, col, vec_res, row_size=row_size)
749 0 : prow = hash_table_get(fast_vec_col%hash_table, row)
750 0 : IF (ASSOCIATED(fast_vec_col%blk_map(prow)%ptr)) THEN
751 0 : vec_res(:, :) = beta*vec_res(:, :) + alpha*fast_vec_col%blk_map(prow)%ptr(:, :)
752 : ELSE
753 0 : vec_res(:, :) = beta*vec_res(:, :)
754 : END IF
755 : END DO
756 0 : CALL dbcsr_iterator_stop(iter)
757 :
758 0 : CALL timestop(handle)
759 :
760 0 : END SUBROUTINE dbcsr_matrixT_vector_mult
761 :
762 : ! **************************************************************************************************
763 : !> \brief ...
764 : !> \param vec_in ...
765 : !> \param rep_col_vec ...
766 : !> \param rep_row_vec ...
767 : !> \param fast_vec_col ...
768 : ! **************************************************************************************************
769 4160808 : SUBROUTINE dbcsr_col_vec_to_rep_row(vec_in, rep_col_vec, rep_row_vec, fast_vec_col)
770 : TYPE(dbcsr_type) :: vec_in, rep_col_vec, rep_row_vec
771 : TYPE(fast_vec_access_type), INTENT(IN) :: fast_vec_col
772 :
773 : CHARACTER(LEN=*), PARAMETER :: routineN = 'dbcsr_col_vec_to_rep_row'
774 :
775 : INTEGER :: col, handle, mypcol, myprow, ncols, &
776 : nrows, pcol_handle, prow_handle, row
777 693468 : INTEGER, DIMENSION(:), POINTER :: local_cols, row_dist
778 693468 : REAL(kind=real_8), DIMENSION(:, :), POINTER :: vec_row
779 : TYPE(dbcsr_distribution_type) :: dist_in, dist_rep_col
780 : TYPE(dbcsr_iterator_type) :: iter
781 : TYPE(mp_comm_type) :: pcol_group, prow_group
782 :
783 693468 : CALL timeset(routineN, handle)
784 :
785 : ! get information about the parallel environment
786 693468 : CALL dbcsr_get_info(vec_in, distribution=dist_in)
787 : CALL dbcsr_distribution_get(dist_in, &
788 : prow_group=prow_handle, &
789 : pcol_group=pcol_handle, &
790 : myprow=myprow, &
791 693468 : mypcol=mypcol)
792 :
793 693468 : CALL prow_group%set_handle(prow_handle)
794 693468 : CALL pcol_group%set_handle(pcol_handle)
795 :
796 : ! Get the vector which tells us which blocks are local to which processor row in the col vec
797 693468 : CALL dbcsr_get_info(rep_col_vec, distribution=dist_rep_col)
798 693468 : CALL dbcsr_distribution_get(dist_rep_col, row_dist=row_dist)
799 :
800 : ! Copy the local vector to the replicated on the first processor column (this is where vec_in lives)
801 693468 : IF (mypcol == 0) THEN
802 693468 : CALL dbcsr_iterator_start(iter, vec_in)
803 2127031 : DO WHILE (dbcsr_iterator_blocks_left(iter))
804 1433563 : CALL dbcsr_iterator_next_block(iter, row, col, vec_row)
805 13688479 : fast_vec_col%blk_map(hash_table_get(fast_vec_col%hash_table, row))%ptr(:, :) = vec_row(:, :)
806 : END DO
807 693468 : CALL dbcsr_iterator_stop(iter)
808 : END IF
809 : ! Replicate the data along the row
810 693468 : CALL bcast_replicated_vector_blocks(rep_col_vec, prow_group, 0)
811 :
812 : ! Here it gets a bit tricky as we are dealing with two different parallel layouts:
813 : ! The rep_col_vec contains all blocks local to the row distribution of the vector.
814 : ! The rep_row_vec only needs the fraction which is local to the col distribution.
815 : ! However in most cases this won't the complete set of block which can be obtained from col_vector p_row i
816 : ! Anyway, as the blocks don't repeat in the col_vec, a different fraction of the row vec will be available
817 : ! on every replica in the processor column, by summing along the column we end up with the complete vector everywhere
818 : ! Hope this clarifies the idea
819 693468 : CALL dbcsr_set(rep_row_vec, 0.0_real_8)
820 693468 : CALL dbcsr_get_info(matrix=rep_row_vec, nfullrows_local=nrows, local_cols=local_cols, nfullcols_local=ncols)
821 693468 : CALL dbcsr_iterator_start(iter, rep_row_vec)
822 3546166 : DO WHILE (dbcsr_iterator_blocks_left(iter))
823 2852698 : CALL dbcsr_iterator_next_block(iter, row, col, vec_row)
824 3546166 : IF (row_dist(col) == myprow) THEN
825 21689333 : vec_row = TRANSPOSE(fast_vec_col%blk_map(hash_table_get(fast_vec_col%hash_table, col))%ptr)
826 : END IF
827 : END DO
828 693468 : CALL dbcsr_iterator_stop(iter)
829 693468 : CALL sum_replicated_vector_blocks(rep_row_vec, pcol_group)
830 :
831 693468 : CALL timestop(handle)
832 :
833 693468 : END SUBROUTINE dbcsr_col_vec_to_rep_row
834 :
835 : ! **************************************************************************************************
836 : !> \brief ...
837 : !> \param rep_col_vec ...
838 : !> \param rep_row_vec ...
839 : !> \param fast_vec_row ...
840 : !> \param fast_vec_col_add ...
841 : ! **************************************************************************************************
842 1467545 : SUBROUTINE dbcsr_rep_row_to_rep_col_vec(rep_col_vec, rep_row_vec, fast_vec_row, fast_vec_col_add)
843 : TYPE(dbcsr_type) :: rep_col_vec, rep_row_vec
844 : TYPE(fast_vec_access_type) :: fast_vec_row
845 : TYPE(fast_vec_access_type), OPTIONAL :: fast_vec_col_add
846 :
847 : CHARACTER(LEN=*), PARAMETER :: routineN = 'dbcsr_rep_row_to_rep_col_vec'
848 :
849 : INTEGER :: col, handle, mypcol, myprow, &
850 : prow_handle, row
851 293509 : INTEGER, DIMENSION(:), POINTER :: col_dist
852 293509 : REAL(kind=real_8), DIMENSION(:, :), POINTER :: vec_col
853 : TYPE(dbcsr_distribution_type) :: dist_rep_col, dist_rep_row
854 : TYPE(dbcsr_iterator_type) :: iter
855 : TYPE(mp_comm_type) :: prow_group
856 :
857 293509 : CALL timeset(routineN, handle)
858 :
859 : ! get information about the parallel environment
860 293509 : CALL dbcsr_get_info(matrix=rep_col_vec, distribution=dist_rep_col)
861 : CALL dbcsr_distribution_get(dist_rep_col, &
862 : prow_group=prow_handle, &
863 : myprow=myprow, &
864 293509 : mypcol=mypcol)
865 :
866 293509 : CALL prow_group%set_handle(prow_handle)
867 :
868 : ! Get the vector which tells us which blocks are local to which processor col in the row vec
869 293509 : CALL dbcsr_get_info(matrix=rep_row_vec, distribution=dist_rep_row)
870 293509 : CALL dbcsr_distribution_get(dist_rep_row, col_dist=col_dist)
871 :
872 : ! The same trick as described above with opposite direction
873 293509 : CALL dbcsr_set(rep_col_vec, 0.0_real_8)
874 293509 : CALL dbcsr_iterator_start(iter, rep_col_vec)
875 1356991 : DO WHILE (dbcsr_iterator_blocks_left(iter))
876 1063482 : CALL dbcsr_iterator_next_block(iter, row, col, vec_col)
877 1063482 : IF (col_dist(row) == mypcol) THEN
878 8050321 : vec_col = TRANSPOSE(fast_vec_row%blk_map(hash_table_get(fast_vec_row%hash_table, row))%ptr)
879 : END IF
880 : ! this one is special and allows to add the elements of a not yet summed replicated
881 : ! column vector as it appears in M*V(row_rep) as result. Save an parallel summation in the symmetric case
882 1356991 : IF (PRESENT(fast_vec_col_add)) THEN
883 8050321 : vec_col = vec_col + fast_vec_col_add%blk_map(hash_table_get(fast_vec_col_add%hash_table, row))%ptr(:, :)
884 : END IF
885 : END DO
886 293509 : CALL dbcsr_iterator_stop(iter)
887 293509 : CALL sum_replicated_vector_blocks(rep_col_vec, prow_group)
888 :
889 293509 : CALL timestop(handle)
890 :
891 293509 : END SUBROUTINE dbcsr_rep_row_to_rep_col_vec
892 :
893 : ! **************************************************************************************************
894 : !> \brief ...
895 : !> \param matrix ...
896 : !> \param vec_in ...
897 : !> \param vec_out ...
898 : !> \param alpha ...
899 : !> \param beta ...
900 : !> \param work_row ...
901 : !> \param work_col ...
902 : ! **************************************************************************************************
903 293509 : SUBROUTINE dbcsr_sym_matrix_vector_mult(matrix, vec_in, vec_out, alpha, beta, work_row, work_col)
904 : TYPE(dbcsr_type) :: matrix, vec_in, vec_out
905 : REAL(kind=real_8) :: alpha, beta
906 : TYPE(dbcsr_type) :: work_row, work_col
907 :
908 : CHARACTER(LEN=*), PARAMETER :: routineN = 'dbcsr_sym_matrix_vector_mult'
909 :
910 : INTEGER :: col, handle, handle1, ithread, mypcol, &
911 : myprow, pcol, pcol_handle, prow, row, &
912 : rpcol, rprow, vec_dim
913 293509 : REAL(kind=real_8), DIMENSION(:, :), POINTER :: data_d, vec_res
914 : TYPE(dbcsr_distribution_type) :: dist
915 : TYPE(dbcsr_iterator_type) :: iter
916 : TYPE(dbcsr_type) :: result_col, result_row
917 293509 : TYPE(fast_vec_access_type) :: fast_vec_col, fast_vec_row, &
918 293509 : res_fast_vec_col, res_fast_vec_row
919 : TYPE(mp_comm_type) :: pcol_group
920 :
921 293509 : CALL timeset(routineN, handle)
922 293509 : ithread = 0
923 : ! We need some work matrices as we try to exploit operations on the replicated vectors which are duplicated otherwise
924 293509 : CALL dbcsr_get_info(matrix=vec_in, nfullcols_total=vec_dim)
925 : ! This is a performance hack as the new creation of a replicated vector is a fair bit more expensive
926 293509 : CALL dbcsr_set(work_col, 0.0_real_8)
927 293509 : CALL dbcsr_copy(result_col, work_col)
928 293509 : CALL dbcsr_set(work_row, 0.0_real_8)
929 293509 : CALL dbcsr_copy(result_row, work_row)
930 :
931 : ! Collect some data about the parallel environment. We will use them later to move the vector around
932 293509 : CALL dbcsr_get_info(matrix=matrix, distribution=dist)
933 293509 : CALL dbcsr_distribution_get(dist, pcol_group=pcol_handle, myprow=myprow, mypcol=mypcol)
934 :
935 293509 : CALL pcol_group%set_handle(pcol_handle)
936 :
937 293509 : CALL create_fast_row_vec_access(work_row, fast_vec_row)
938 293509 : CALL create_fast_col_vec_access(work_col, fast_vec_col)
939 293509 : CALL create_fast_row_vec_access(result_row, res_fast_vec_row)
940 293509 : CALL create_fast_col_vec_access(result_col, res_fast_vec_col)
941 :
942 : ! Transfer the correct parts of the input vector to the correct locations so we can do a local multiply
943 293509 : CALL dbcsr_col_vec_to_rep_row(vec_in, work_col, work_row, fast_vec_col)
944 :
945 : ! Probably I should rename the routine above as it delivers both the replicated row and column vector
946 :
947 : ! Perform the local multiply. Here we exploit, that we have the blocks replicated on the mpi processes
948 : ! It is important to note, that the input and result vector are distributed differently (row wise, col wise respectively)
949 293509 : CALL timeset(routineN//"_local_mm", handle1)
950 :
951 : !------ perform the multiplication, we have to take car to take the correct blocks ----------
952 :
953 : !$OMP PARALLEL DEFAULT(NONE) PRIVATE(row,col,iter,data_d,ithread,pcol,prow,rpcol,rprow) &
954 293509 : !$OMP SHARED(matrix,fast_vec_row,res_fast_vec_col,res_fast_vec_row,fast_vec_col)
955 : !$ ithread = omp_get_thread_num()
956 : CALL dbcsr_iterator_start(iter, matrix, shared=.FALSE.)
957 : DO WHILE (dbcsr_iterator_blocks_left(iter))
958 : CALL dbcsr_iterator_next_block(iter, row, col, data_d)
959 : pcol = hash_table_get(fast_vec_row%hash_table, col)
960 : rprow = hash_table_get(res_fast_vec_col%hash_table, row)
961 : IF (ASSOCIATED(fast_vec_row%blk_map(pcol)%ptr) .AND. &
962 : ASSOCIATED(res_fast_vec_col%blk_map(rprow)%ptr)) THEN
963 : IF (res_fast_vec_col%blk_map(rprow)%assigned_thread == ithread) THEN
964 : res_fast_vec_col%blk_map(rprow)%ptr = res_fast_vec_col%blk_map(rprow)%ptr + &
965 : MATMUL(data_d, TRANSPOSE(fast_vec_row%blk_map(pcol)%ptr))
966 : END IF
967 : prow = hash_table_get(fast_vec_col%hash_table, row)
968 : rpcol = hash_table_get(res_fast_vec_row%hash_table, col)
969 : IF (res_fast_vec_row%blk_map(rpcol)%assigned_thread == ithread .AND. row /= col) THEN
970 : res_fast_vec_row%blk_map(rpcol)%ptr = res_fast_vec_row%blk_map(rpcol)%ptr + &
971 : MATMUL(TRANSPOSE(fast_vec_col%blk_map(prow)%ptr), data_d)
972 : END IF
973 : ELSE
974 : rpcol = hash_table_get(res_fast_vec_col%hash_table, col)
975 : prow = hash_table_get(fast_vec_row%hash_table, row)
976 : IF (res_fast_vec_col%blk_map(rpcol)%assigned_thread == ithread) THEN
977 : res_fast_vec_col%blk_map(rpcol)%ptr = res_fast_vec_col%blk_map(rpcol)%ptr + &
978 : TRANSPOSE(MATMUL(fast_vec_row%blk_map(prow)%ptr, data_d))
979 : END IF
980 : rprow = hash_table_get(res_fast_vec_row%hash_table, row)
981 : pcol = hash_table_get(fast_vec_col%hash_table, col)
982 : IF (res_fast_vec_row%blk_map(rprow)%assigned_thread == ithread .AND. row /= col) THEN
983 : res_fast_vec_row%blk_map(rprow)%ptr = res_fast_vec_row%blk_map(rprow)%ptr + &
984 : TRANSPOSE(MATMUL(data_d, fast_vec_col%blk_map(pcol)%ptr))
985 : END IF
986 : END IF
987 : END DO
988 : CALL dbcsr_iterator_stop(iter)
989 : !$OMP END PARALLEL
990 :
991 293509 : CALL timestop(handle1)
992 :
993 : ! sum all the data within a processor column to obtain the replicated result from lower
994 293509 : CALL sum_replicated_vector_blocks(result_row, pcol_group)
995 :
996 : ! Convert the results to a column wise distribution, this is a bit involved as the result_row is fully replicated
997 : ! While the result_col still has the partial results in parallel. The routine below takes care of that and saves a
998 : ! parallel summation. Of the res_row vectors are created only taking the appropriate element (0 otherwise) while the res_col
999 : ! parallel bits are locally added. The sum magically creates the correct vector
1000 293509 : CALL dbcsr_rep_row_to_rep_col_vec(work_col, result_row, res_fast_vec_row, res_fast_vec_col)
1001 :
1002 : ! Create_the final vector by summing it to the result vector which lives on proc_col 0 lower
1003 293509 : CALL dbcsr_iterator_start(iter, vec_out)
1004 1356991 : DO WHILE (dbcsr_iterator_blocks_left(iter))
1005 1063482 : CALL dbcsr_iterator_next_block(iter, row, col, vec_res)
1006 1063482 : prow = hash_table_get(fast_vec_col%hash_table, row)
1007 1356991 : IF (ASSOCIATED(fast_vec_col%blk_map(prow)%ptr)) THEN
1008 8050321 : vec_res(:, :) = beta*vec_res(:, :) + alpha*(fast_vec_col%blk_map(prow)%ptr(:, :))
1009 : ELSE
1010 0 : vec_res(:, :) = beta*vec_res(:, :)
1011 : END IF
1012 : END DO
1013 293509 : CALL dbcsr_iterator_stop(iter)
1014 :
1015 293509 : CALL release_fast_vec_access(fast_vec_row)
1016 293509 : CALL release_fast_vec_access(fast_vec_col)
1017 293509 : CALL release_fast_vec_access(res_fast_vec_row)
1018 293509 : CALL release_fast_vec_access(res_fast_vec_col)
1019 :
1020 293509 : CALL dbcsr_release(result_row); CALL dbcsr_release(result_col)
1021 :
1022 293509 : CALL timestop(handle)
1023 :
1024 587018 : END SUBROUTINE dbcsr_sym_matrix_vector_mult
1025 :
1026 : ! **************************************************************************************************
1027 : !> \brief Helper routine to transfer all local data of a dense dbcsr vector to a 1D array
1028 : !> \param vec ...
1029 : !> \param array ...
1030 : !> \param n ...
1031 : !> \param is_local ...
1032 : ! **************************************************************************************************
1033 1927803 : SUBROUTINE transfer_dbcsr_to_local_array(vec, array, n, is_local)
1034 : TYPE(dbcsr_type) :: vec
1035 : REAL(kind=dp), DIMENSION(:) :: array
1036 : INTEGER, INTENT(IN) :: n
1037 : LOGICAL, INTENT(IN) :: is_local
1038 :
1039 : INTEGER :: i, j, m, offset
1040 1927803 : INTEGER, DIMENSION(:), POINTER :: local_cols, local_rows
1041 : LOGICAL :: found
1042 1927803 : REAL(kind=dp), DIMENSION(:, :), POINTER :: blk
1043 :
1044 1927803 : IF (.NOT. is_local .OR. n <= 0) RETURN
1045 1664081 : CALL dbcsr_get_info(vec, local_rows=local_rows, local_cols=local_cols)
1046 1664081 : offset = 0
1047 4255471 : DO i = 1, SIZE(local_rows)
1048 10817661 : DO j = 1, SIZE(local_cols)
1049 6562190 : CALL dbcsr_get_block_p(vec, local_rows(i), local_cols(j), blk, found)
1050 6562190 : IF (.NOT. found) CYCLE
1051 19686570 : m = SIZE(blk)
1052 15715770 : IF (m > 0) THEN
1053 6562190 : CPASSERT(offset + m <= n)
1054 13124380 : array(offset + 1:offset + m) = RESHAPE(blk, [m])
1055 6562190 : offset = offset + m
1056 : END IF
1057 : END DO
1058 : END DO
1059 1664081 : CPASSERT(offset == n)
1060 1927803 : END SUBROUTINE transfer_dbcsr_to_local_array
1061 :
1062 : ! **************************************************************************************************
1063 : !> \brief Helper routine to transfer data back from a 1D array to a dense dbcsr vector
1064 : !> \param vec ...
1065 : !> \param array ...
1066 : !> \param n ...
1067 : !> \param is_local ...
1068 : ! **************************************************************************************************
1069 1601406 : SUBROUTINE transfer_local_array_to_dbcsr(vec, array, n, is_local)
1070 : TYPE(dbcsr_type) :: vec
1071 : REAL(kind=dp), DIMENSION(:), INTENT(IN) :: array
1072 : INTEGER, INTENT(IN) :: n
1073 : LOGICAL, INTENT(IN) :: is_local
1074 :
1075 : INTEGER :: i, j, m, offset
1076 1601406 : INTEGER, DIMENSION(:), POINTER :: local_cols, local_rows
1077 : LOGICAL :: found
1078 1601406 : REAL(kind=dp), DIMENSION(:, :), POINTER :: blk
1079 :
1080 1601406 : IF (.NOT. is_local .OR. n <= 0) RETURN
1081 1501122 : CALL dbcsr_get_info(vec, local_rows=local_rows, local_cols=local_cols)
1082 1501122 : offset = 0
1083 3869674 : DO i = 1, SIZE(local_rows)
1084 10209026 : DO j = 1, SIZE(local_cols)
1085 6339352 : CALL dbcsr_get_block_p(vec, local_rows(i), local_cols(j), blk, found)
1086 6339352 : IF (.NOT. found) CYCLE
1087 19018056 : m = SIZE(blk)
1088 15047256 : IF (m > 0) THEN
1089 6339352 : CPASSERT(offset + m <= n)
1090 19018056 : blk(:, :) = RESHAPE(array(offset + 1:offset + m), [SIZE(blk, 1), SIZE(blk, 2)])
1091 6339352 : offset = offset + m
1092 : END IF
1093 : END DO
1094 : END DO
1095 1501122 : CPASSERT(offset == n)
1096 1601406 : END SUBROUTINE transfer_local_array_to_dbcsr
1097 :
1098 : ! **************************************************************************************************
1099 : !> \brief Sum a replicated dense vector across an MPI subgroup in deterministic block order
1100 : !> \param vec ...
1101 : !> \param group ...
1102 : ! **************************************************************************************************
1103 1680445 : SUBROUTINE sum_replicated_vector_blocks(vec, group)
1104 : TYPE(dbcsr_type) :: vec
1105 : TYPE(mp_comm_type), INTENT(IN) :: group
1106 :
1107 : INTEGER :: n, ncols, nrows
1108 1680445 : REAL(kind=dp), ALLOCATABLE, DIMENSION(:) :: buf
1109 :
1110 1680445 : IF (group%num_pe <= 1) RETURN
1111 975744 : CALL dbcsr_get_info(matrix=vec, nfullrows_local=nrows, nfullcols_local=ncols)
1112 975744 : n = nrows*ncols
1113 975744 : IF (n <= 0) RETURN
1114 2918232 : ALLOCATE (buf(n))
1115 972744 : CALL transfer_dbcsr_to_local_array(vec, buf, n, .TRUE.)
1116 972744 : CALL group%sum(buf)
1117 972744 : CALL transfer_local_array_to_dbcsr(vec, buf, n, .TRUE.)
1118 972744 : DEALLOCATE (buf)
1119 975744 : END SUBROUTINE sum_replicated_vector_blocks
1120 :
1121 : ! **************************************************************************************************
1122 : !> \brief Broadcast a replicated dense vector across an MPI subgroup in deterministic block order
1123 : !> \param vec ...
1124 : !> \param group ...
1125 : !> \param source ...
1126 : ! **************************************************************************************************
1127 693468 : SUBROUTINE bcast_replicated_vector_blocks(vec, group, source)
1128 : TYPE(dbcsr_type) :: vec
1129 : TYPE(mp_comm_type), INTENT(IN) :: group
1130 : INTEGER, INTENT(IN) :: source
1131 :
1132 : INTEGER :: n, ncols, nrows
1133 693468 : REAL(kind=dp), ALLOCATABLE, DIMENSION(:) :: buf
1134 :
1135 693468 : IF (group%num_pe <= 1) RETURN
1136 0 : CALL dbcsr_get_info(matrix=vec, nfullrows_local=nrows, nfullcols_local=ncols)
1137 0 : n = nrows*ncols
1138 0 : IF (n <= 0) RETURN
1139 0 : ALLOCATE (buf(n))
1140 0 : IF (group%mepos == source) THEN
1141 0 : CALL transfer_dbcsr_to_local_array(vec, buf, n, .TRUE.)
1142 : END IF
1143 0 : CALL group%bcast(buf, source)
1144 0 : IF (group%mepos /= source) THEN
1145 0 : CALL transfer_local_array_to_dbcsr(vec, buf, n, .TRUE.)
1146 : END IF
1147 0 : DEALLOCATE (buf)
1148 0 : END SUBROUTINE bcast_replicated_vector_blocks
1149 :
1150 0 : END MODULE arnoldi_vector
|