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 DBCSR operations in CP2K
10 : !> \author Urban Borstnik
11 : !> \date 2009-05-12
12 : !> \version 0.8
13 : !>
14 : !> <b>Modification history:</b>
15 : !> - Created 2009-05-12
16 : !> - Generalized sm_fm_mulitply for matrices w/ different row/col block size (A. Bussy, 11.2018)
17 : ! **************************************************************************************************
18 : MODULE cp_dbcsr_operations
19 : USE cp_blacs_env, ONLY: cp_blacs_env_type
20 : USE cp_dbcsr_api, ONLY: &
21 : dbcsr_add, dbcsr_complete_redistribute, dbcsr_convert_sizes_to_offsets, dbcsr_copy, &
22 : dbcsr_create, dbcsr_deallocate_matrix, dbcsr_desymmetrize, dbcsr_distribution_get, &
23 : dbcsr_distribution_new, dbcsr_distribution_release, dbcsr_distribution_type, &
24 : dbcsr_get_info, dbcsr_get_matrix_type, dbcsr_iterator_blocks_left, &
25 : dbcsr_iterator_next_block, dbcsr_iterator_readonly_start, dbcsr_iterator_start, &
26 : dbcsr_iterator_stop, dbcsr_iterator_type, dbcsr_multiply, dbcsr_p_type, dbcsr_release, &
27 : dbcsr_scale, dbcsr_type, dbcsr_type_antisymmetric, dbcsr_type_no_symmetry, &
28 : dbcsr_type_symmetric, dbcsr_valid_index, dbcsr_verify_matrix
29 : USE cp_dbcsr_contrib, ONLY: dbcsr_frobenius_norm,&
30 : dbcsr_reserve_all_blocks
31 : USE cp_fm_basic_linalg, ONLY: cp_fm_gemm
32 : USE cp_fm_struct, ONLY: cp_fm_struct_create,&
33 : cp_fm_struct_release,&
34 : cp_fm_struct_type
35 : USE cp_fm_types, ONLY: cp_fm_create,&
36 : cp_fm_get_info,&
37 : cp_fm_release,&
38 : cp_fm_to_fm,&
39 : cp_fm_type
40 : USE distribution_2d_types, ONLY: distribution_2d_get,&
41 : distribution_2d_type
42 : USE kinds, ONLY: default_string_length,&
43 : dp,&
44 : int_8
45 : USE mathlib, ONLY: gcd,&
46 : lcm
47 : USE message_passing, ONLY: mp_para_env_type
48 :
49 : !$ USE OMP_LIB, ONLY: omp_get_max_threads, omp_get_thread_num, omp_get_num_threads
50 : #include "base/base_uses.f90"
51 :
52 : IMPLICIT NONE
53 : PRIVATE
54 :
55 : CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'cp_dbcsr_operations'
56 : LOGICAL, PARAMETER :: debug_mod = .FALSE.
57 :
58 : INTEGER, SAVE, PUBLIC :: max_elements_per_block = 32
59 :
60 : ! A plan caches S/A routing for a source block layout and FM distribution.
61 : ! Use check_plan for sources whose layout can change. The owner must release
62 : ! the plan before releasing the FM structure. All owned storage is allocatable;
63 : ! no matrix data pointers or MPI requests are retained. Calls must be serialized.
64 : TYPE dbcsr_fm_thread_plan_type
65 : INTEGER, ALLOCATABLE :: blocks(:, :), slots(:)
66 : END TYPE dbcsr_fm_thread_plan_type
67 :
68 : TYPE, PUBLIC :: dbcsr_fm_copy_plan_type
69 : PRIVATE
70 : TYPE(cp_fm_struct_type), POINTER :: matrix_struct => NULL()
71 : CHARACTER :: matrix_type = " "
72 : TYPE(dbcsr_fm_thread_plan_type), ALLOCATABLE :: thread(:)
73 : INTEGER, ALLOCATABLE :: send_count(:), send_disp(:), recv_count(:), recv_disp(:), recv_index(:, :)
74 : REAL(KIND=dp), ALLOCATABLE :: send_values(:), recv_values(:)
75 : END TYPE dbcsr_fm_copy_plan_type
76 :
77 : PUBLIC :: copy_dbcsr_to_local, copy_local_to_dbcsr
78 : PUBLIC :: dbcsr_multiply_local, dbcsr_sum_many
79 :
80 : ! CP2K API emulation
81 : PUBLIC :: copy_fm_to_dbcsr, copy_dbcsr_to_fm, &
82 : cp_dbcsr_sm_fm_multiply, cp_dbcsr_plus_fm_fm_t, &
83 : copy_dbcsr_to_fm_bc, copy_fm_to_dbcsr_bc, cp_fm_to_dbcsr_row_template, &
84 : cp_dbcsr_m_by_n_from_template, cp_dbcsr_m_by_n_from_row_template, &
85 : dbcsr_create_dist_r_unrot
86 :
87 : ! distribution_2d_type compatibility
88 : PUBLIC :: cp_dbcsr_dist2d_to_dist
89 :
90 : PUBLIC :: dbcsr_copy_columns_hack
91 :
92 : ! matrix set
93 : PUBLIC :: dbcsr_allocate_matrix_set
94 : PUBLIC :: dbcsr_deallocate_matrix_set
95 :
96 : INTERFACE dbcsr_allocate_matrix_set
97 : MODULE PROCEDURE allocate_dbcsr_matrix_set_1d
98 : MODULE PROCEDURE allocate_dbcsr_matrix_set_2d
99 : MODULE PROCEDURE allocate_dbcsr_matrix_set_3d
100 : MODULE PROCEDURE allocate_dbcsr_matrix_set_4d
101 : MODULE PROCEDURE allocate_dbcsr_matrix_set_5d
102 : END INTERFACE
103 :
104 : INTERFACE dbcsr_deallocate_matrix_set
105 : MODULE PROCEDURE deallocate_dbcsr_matrix_set_1d
106 : MODULE PROCEDURE deallocate_dbcsr_matrix_set_2d
107 : MODULE PROCEDURE deallocate_dbcsr_matrix_set_3d
108 : MODULE PROCEDURE deallocate_dbcsr_matrix_set_4d
109 : MODULE PROCEDURE deallocate_dbcsr_matrix_set_5d
110 : END INTERFACE
111 :
112 : CONTAINS
113 :
114 : ! **************************************************************************************************
115 : !> \brief Sum local, nonsymmetric matrices in history order, using bounded per-worker storage.
116 : !> Pack and store outside OpenMP, including missing or transposed input blocks.
117 : !> \param matrix_c independent output matrices
118 : !> \param matrix_a input matrices, indexed by output and history entry; must not alias outputs
119 : !> \param alpha coefficients of the history entries
120 : ! **************************************************************************************************
121 770 : SUBROUTINE dbcsr_sum_many(matrix_c, matrix_a, alpha)
122 : TYPE(dbcsr_p_type), INTENT(IN) :: matrix_c(:), matrix_a(:, :)
123 : REAL(dp), INTENT(IN) :: alpha(:)
124 :
125 : TYPE local_sum_type
126 : REAL(dp), ALLOCATABLE :: value(:, :), buffer(:, :)
127 : END TYPE local_sum_type
128 :
129 : INTEGER :: first, i, j, k, n, nbatch, nworkers
130 770 : TYPE(local_sum_type), ALLOCATABLE :: work(:)
131 :
132 770 : CPASSERT(SIZE(matrix_c) == SIZE(matrix_a, 1))
133 770 : CPASSERT(SIZE(alpha) == SIZE(matrix_a, 2))
134 770 : IF (SIZE(matrix_c) == 0) RETURN
135 770 : nworkers = 1
136 770 : !$ nworkers = MIN(SIZE(matrix_c), omp_get_max_threads())
137 3080 : ALLOCATE (work(nworkers))
138 4558 : DO first = 1, SIZE(matrix_c), nworkers
139 3788 : nbatch = MIN(nworkers, SIZE(matrix_c) - first + 1)
140 7576 : DO j = 1, nbatch
141 3788 : CALL dbcsr_get_info(matrix_c(first + j - 1)%matrix, nfullrows_total=n, nfullcols_total=k)
142 3788 : IF (ALLOCATED(work(j)%value)) THEN
143 3018 : IF (SIZE(work(j)%value, 1) /= n .OR. SIZE(work(j)%value, 2) /= k) THEN
144 0 : DEALLOCATE (work(j)%value, work(j)%buffer)
145 : END IF
146 : END IF
147 7638 : IF (.NOT. ALLOCATED(work(j)%value)) ALLOCATE (work(j)%value(n, k), work(j)%buffer(n, k))
148 689876 : work(j)%value(:, :) = 0.0_dp
149 : END DO
150 37004 : DO i = 1, SIZE(alpha)
151 66432 : DO j = 1, nbatch
152 66432 : CALL copy_dbcsr_to_local(matrix_a(first + j - 1, i)%matrix, work(j)%buffer)
153 : END DO
154 37004 : !$OMP PARALLEL DO DEFAULT(NONE) SCHEDULE(STATIC) NUM_THREADS(nbatch) IF(nbatch > 1) SHARED(work,alpha,i,nbatch)
155 : DO j = 1, nbatch
156 : work(j)%value(:, :) = work(j)%value(:, :) + alpha(i)*work(j)%buffer(:, :)
157 : END DO
158 : !$OMP END PARALLEL DO
159 : END DO
160 8346 : DO j = 1, nbatch
161 7576 : CALL copy_local_to_dbcsr(matrix_c(first + j - 1)%matrix, work(j)%value)
162 : END DO
163 : END DO
164 1540 : END SUBROUTINE dbcsr_sum_many
165 :
166 : ! **************************************************************************************************
167 : !> \brief Load a single-rank, nonsymmetric matrix without redistribution.
168 : !> \param matrix DBCSR matrix
169 : !> \param dense full local matrix
170 : ! **************************************************************************************************
171 100768 : SUBROUTINE copy_dbcsr_to_local(matrix, dense)
172 : TYPE(dbcsr_type), INTENT(IN) :: matrix
173 : REAL(dp), INTENT(OUT) :: dense(:, :)
174 :
175 : INTEGER :: col, nc, nr, row
176 : LOGICAL :: transposed
177 100768 : REAL(dp), POINTER :: block(:, :)
178 : TYPE(dbcsr_iterator_type) :: iter
179 :
180 100768 : CPASSERT(dbcsr_get_matrix_type(matrix) == dbcsr_type_no_symmetry)
181 21123500 : dense(:, :) = 0.0_dp
182 : !$OMP PARALLEL DEFAULT(NONE) SHARED(matrix, dense) &
183 100768 : !$OMP PRIVATE(iter, block, transposed, nr, nc, row, col)
184 : CALL dbcsr_iterator_readonly_start(iter, matrix)
185 : DO WHILE (dbcsr_iterator_blocks_left(iter))
186 : CALL dbcsr_iterator_next_block(iter, block=block, transposed=transposed, &
187 : row_size=nr, col_size=nc, row_offset=row, col_offset=col)
188 : IF (transposed) THEN
189 : dense(row:row + nr - 1, col:col + nc - 1) = TRANSPOSE(block)
190 : ELSE
191 : dense(row:row + nr - 1, col:col + nc - 1) = block
192 : END IF
193 : END DO
194 : CALL dbcsr_iterator_stop(iter)
195 : !$OMP END PARALLEL
196 100768 : END SUBROUTINE copy_dbcsr_to_local
197 :
198 : ! **************************************************************************************************
199 : !> \brief Store a single-rank, nonsymmetric matrix without redistribution.
200 : !> \param matrix DBCSR matrix
201 : !> \param dense full local matrix
202 : ! **************************************************************************************************
203 54026 : SUBROUTINE copy_local_to_dbcsr(matrix, dense)
204 : TYPE(dbcsr_type), INTENT(INOUT) :: matrix
205 : REAL(dp), INTENT(IN) :: dense(:, :)
206 :
207 : INTEGER :: col, nc, nr, row
208 : LOGICAL :: transposed
209 54026 : REAL(dp), POINTER :: block(:, :)
210 : TYPE(dbcsr_iterator_type) :: iter
211 :
212 54026 : CPASSERT(dbcsr_get_matrix_type(matrix) == dbcsr_type_no_symmetry)
213 54026 : CALL dbcsr_reserve_all_blocks(matrix)
214 : !$OMP PARALLEL DEFAULT(NONE) SHARED(matrix, dense) &
215 54026 : !$OMP PRIVATE(iter, block, transposed, nr, nc, row, col)
216 : CALL dbcsr_iterator_start(iter, matrix)
217 : DO WHILE (dbcsr_iterator_blocks_left(iter))
218 : CALL dbcsr_iterator_next_block(iter, block=block, transposed=transposed, &
219 : row_size=nr, col_size=nc, row_offset=row, col_offset=col)
220 : IF (transposed) THEN
221 : block(:, :) = TRANSPOSE(dense(row:row + nr - 1, col:col + nc - 1))
222 : ELSE
223 : block(:, :) = dense(row:row + nr - 1, col:col + nc - 1)
224 : END IF
225 : END DO
226 : CALL dbcsr_iterator_stop(iter)
227 : !$OMP END PARALLEL
228 54026 : END SUBROUTINE copy_local_to_dbcsr
229 :
230 : ! **************************************************************************************************
231 : !> \brief Copy a BLACS matrix to a dbcsr matrix.
232 : !>
233 : !> real_matrix=beta*real_matrix+alpha*fm
234 : !> beta defaults to 0, alpha to 1
235 : !> \param[in] fm full matrix
236 : !> \param[out] matrix DBCSR matrix
237 : !> \param[in] keep_sparsity (optional) retains the sparsity of the input
238 : !> matrix
239 : !> \date 2009-10-13
240 : !> \par History
241 : !> 2009-10-13 rewritten based on copy_dbcsr_to_fm
242 : !> \author Urban Borstnik
243 : !> \version 2.0
244 : ! **************************************************************************************************
245 1327253 : SUBROUTINE copy_fm_to_dbcsr(fm, matrix, keep_sparsity)
246 : TYPE(cp_fm_type), INTENT(IN) :: fm
247 : TYPE(dbcsr_type), INTENT(INOUT) :: matrix
248 : LOGICAL, INTENT(IN), OPTIONAL :: keep_sparsity
249 :
250 : CHARACTER(LEN=*), PARAMETER :: routineN = 'copy_fm_to_dbcsr'
251 :
252 : INTEGER :: handle
253 : LOGICAL :: my_keep_sparsity
254 : TYPE(dbcsr_type) :: bc_mat, redist_mat
255 :
256 1327253 : CALL timeset(routineN, handle)
257 :
258 1327253 : my_keep_sparsity = .FALSE.
259 1327253 : IF (PRESENT(keep_sparsity)) my_keep_sparsity = keep_sparsity
260 :
261 1327253 : CALL copy_fm_to_dbcsr_bc(fm, bc_mat)
262 :
263 1327253 : IF (my_keep_sparsity) THEN
264 42576 : CALL dbcsr_create(redist_mat, template=matrix)
265 42576 : CALL dbcsr_complete_redistribute(bc_mat, redist_mat)
266 42576 : CALL dbcsr_copy(matrix, redist_mat, keep_sparsity=.TRUE.)
267 42576 : CALL dbcsr_release(redist_mat)
268 : ELSE
269 1284677 : CALL dbcsr_complete_redistribute(bc_mat, matrix)
270 : END IF
271 :
272 1327253 : CALL dbcsr_release(bc_mat)
273 :
274 1327253 : CALL timestop(handle)
275 1327253 : END SUBROUTINE copy_fm_to_dbcsr
276 :
277 : ! **************************************************************************************************
278 : !> \brief Copy a BLACS matrix to a dbcsr matrix with a special block-cyclic distribution,
279 : !> which requires no complete redistribution.
280 : !> \param fm ...
281 : !> \param bc_mat ...
282 : ! **************************************************************************************************
283 1341441 : SUBROUTINE copy_fm_to_dbcsr_bc(fm, bc_mat)
284 : TYPE(cp_fm_type), INTENT(IN) :: fm
285 : TYPE(dbcsr_type), INTENT(INOUT) :: bc_mat
286 :
287 : CHARACTER(LEN=*), PARAMETER :: routineN = 'copy_fm_to_dbcsr_bc'
288 :
289 : INTEGER :: col, handle, ncol_block, ncol_global, &
290 : nrow_block, nrow_global, row
291 1341441 : INTEGER, ALLOCATABLE, DIMENSION(:) :: first_col, first_row, last_col, last_row
292 1341441 : INTEGER, CONTIGUOUS, DIMENSION(:), POINTER :: col_blk_size, row_blk_size
293 1341441 : INTEGER, DIMENSION(:, :), POINTER :: pgrid
294 1341441 : REAL(KIND=dp), DIMENSION(:, :), POINTER :: dbcsr_block, fm_block
295 : TYPE(dbcsr_distribution_type) :: bc_dist
296 : TYPE(dbcsr_iterator_type) :: iter
297 :
298 1341441 : CALL timeset(routineN, handle)
299 :
300 : ! Create processor grid
301 1341441 : pgrid => fm%matrix_struct%context%blacs2mpi
302 :
303 : ! Create a block-cyclic distribution compatible with the FM matrix.
304 1341441 : nrow_block = fm%matrix_struct%nrow_block
305 1341441 : ncol_block = fm%matrix_struct%ncol_block
306 1341441 : nrow_global = fm%matrix_struct%nrow_global
307 1341441 : ncol_global = fm%matrix_struct%ncol_global
308 1341441 : NULLIFY (col_blk_size, row_blk_size)
309 : CALL dbcsr_create_dist_block_cyclic(bc_dist, &
310 : nrows=nrow_global, ncolumns=ncol_global, & ! Actual full matrix size
311 : nrow_block=nrow_block, ncol_block=ncol_block, & ! BLACS parameters
312 : group_handle=fm%matrix_struct%para_env%get_handle(), pgrid=pgrid, &
313 1341441 : row_blk_sizes=row_blk_size, col_blk_sizes=col_blk_size) ! block-cyclic row/col sizes
314 :
315 : ! Create the block-cyclic DBCSR matrix
316 : CALL dbcsr_create(bc_mat, "Block-cyclic ", bc_dist, &
317 1341441 : dbcsr_type_no_symmetry, row_blk_size, col_blk_size, reuse_arrays=.TRUE.)
318 1341441 : CALL dbcsr_distribution_release(bc_dist)
319 :
320 : ! allocate all blocks
321 1341441 : CALL dbcsr_reserve_all_blocks(bc_mat)
322 :
323 1341441 : CALL calculate_fm_block_ranges(bc_mat, first_row, last_row, first_col, last_col)
324 :
325 : ! Copy the FM data to the block-cyclic DBCSR matrix. This step
326 : ! could be skipped with appropriate DBCSR index manipulation.
327 1341441 : fm_block => fm%local_data
328 : !$OMP PARALLEL DEFAULT(NONE) PRIVATE(iter, row, col, dbcsr_block) &
329 1341441 : !$OMP SHARED(bc_mat, last_row, first_row, last_col, first_col, fm_block)
330 : CALL dbcsr_iterator_start(iter, bc_mat)
331 : DO WHILE (dbcsr_iterator_blocks_left(iter))
332 : CALL dbcsr_iterator_next_block(iter, row, col, dbcsr_block)
333 : dbcsr_block(:, :) = fm_block(first_row(row):last_row(row), first_col(col):last_col(col))
334 : END DO
335 : CALL dbcsr_iterator_stop(iter)
336 : !$OMP END PARALLEL
337 :
338 1341441 : CALL timestop(handle)
339 2682882 : END SUBROUTINE copy_fm_to_dbcsr_bc
340 :
341 : ! **************************************************************************************************
342 : !> \brief Copy a DBCSR matrix to a BLACS matrix
343 : !> \param[in] matrix DBCSR matrix
344 : !> \param[out] fm full matrix
345 : !> \param plan optional S/A routing plan; discard before releasing the FM structure
346 : !> \param check_plan check for layout changes and collectively rebuild the plan;
347 : !> defaults to false for callers that guarantee fixed layouts and threads
348 : ! **************************************************************************************************
349 1275055 : SUBROUTINE copy_dbcsr_to_fm(matrix, fm, plan, check_plan)
350 : TYPE(dbcsr_type), INTENT(IN) :: matrix
351 : TYPE(cp_fm_type), INTENT(INOUT) :: fm
352 : TYPE(dbcsr_fm_copy_plan_type), INTENT(INOUT), &
353 : OPTIONAL :: plan
354 : LOGICAL, INTENT(IN), OPTIONAL :: check_plan
355 :
356 : CHARACTER(LEN=*), PARAMETER :: routineN = 'copy_dbcsr_to_fm'
357 :
358 : CHARACTER(len=default_string_length) :: name
359 : INTEGER :: group_handle, handle, ncol_block, &
360 : nfullcols_total, nfullrows_total, &
361 : nrow_block
362 1275055 : INTEGER, CONTIGUOUS, DIMENSION(:), POINTER :: col_blk_size, row_blk_size
363 1275055 : INTEGER, DIMENSION(:, :), POINTER :: pgrid
364 : LOGICAL :: validate_plan
365 : TYPE(dbcsr_distribution_type) :: bc_dist, dist
366 1275055 : TYPE(dbcsr_fm_copy_plan_type) :: local_plan
367 : TYPE(dbcsr_type) :: bc_mat, matrix_nosym
368 :
369 1275055 : CALL timeset(routineN, handle)
370 1275055 : validate_plan = .FALSE.
371 1275055 : IF (PRESENT(check_plan)) validate_plan = check_plan
372 :
373 : ! check compatibility
374 : CALL dbcsr_get_info(matrix, &
375 : name=name, &
376 : distribution=dist, &
377 : nfullrows_total=nfullrows_total, &
378 1275055 : nfullcols_total=nfullcols_total)
379 :
380 1275055 : CPASSERT(fm%matrix_struct%nrow_global == nfullrows_total)
381 1275055 : CPASSERT(fm%matrix_struct%ncol_global == nfullcols_total)
382 :
383 423345 : SELECT CASE (dbcsr_get_matrix_type(matrix))
384 : CASE (dbcsr_type_symmetric, dbcsr_type_antisymmetric)
385 423345 : IF (PRESENT(plan)) THEN
386 245900 : CALL copy_dbcsr_to_fm_sym(matrix, fm, plan, validate_plan)
387 : ELSE
388 177445 : CALL copy_dbcsr_to_fm_sym(matrix, fm, local_plan, .FALSE.)
389 : END IF
390 423345 : CALL timestop(handle)
391 1698400 : RETURN
392 : END SELECT
393 :
394 : ! Other matrix types use the uncached redistribution path.
395 851710 : IF (PRESENT(plan)) plan = dbcsr_fm_copy_plan_type()
396 :
397 : ! info about the full matrix
398 851710 : nrow_block = fm%matrix_struct%nrow_block
399 851710 : ncol_block = fm%matrix_struct%ncol_block
400 :
401 : ! Convert DBCSR to a block-cyclic
402 851710 : NULLIFY (col_blk_size, row_blk_size)
403 851710 : CALL dbcsr_distribution_get(dist, group=group_handle, pgrid=pgrid)
404 : CALL dbcsr_create_dist_block_cyclic(bc_dist, &
405 : nrows=nfullrows_total, ncolumns=nfullcols_total, &
406 : nrow_block=nrow_block, ncol_block=ncol_block, &
407 : group_handle=group_handle, pgrid=pgrid, &
408 851710 : row_blk_sizes=row_blk_size, col_blk_sizes=col_blk_size)
409 :
410 : CALL dbcsr_create(bc_mat, "Block-cyclic"//name, bc_dist, &
411 851710 : dbcsr_type_no_symmetry, row_blk_size, col_blk_size, reuse_arrays=.TRUE.)
412 851710 : CALL dbcsr_distribution_release(bc_dist)
413 :
414 851710 : IF (dbcsr_get_matrix_type(matrix) == dbcsr_type_no_symmetry) THEN
415 851710 : CALL dbcsr_complete_redistribute(matrix, bc_mat)
416 : ELSE
417 0 : CALL dbcsr_create(matrix_nosym, template=matrix, matrix_type=dbcsr_type_no_symmetry)
418 0 : CALL dbcsr_desymmetrize(matrix, matrix_nosym)
419 0 : CALL dbcsr_complete_redistribute(matrix_nosym, bc_mat)
420 851710 : CALL dbcsr_release(matrix_nosym)
421 : END IF
422 :
423 851710 : CALL copy_dbcsr_to_fm_bc(bc_mat, fm)
424 :
425 851710 : CALL dbcsr_release(bc_mat)
426 :
427 851710 : CALL timestop(handle)
428 2727555 : END SUBROUTINE copy_dbcsr_to_fm
429 :
430 : ! **************************************************************************************************
431 : !> \brief Send S/A values using a routing plan, checking the block sequence while packing.
432 : !> Mutable sources require a collective check before using the cached routing.
433 : !> \param matrix symmetric or antisymmetric DBCSR workspace
434 : !> \param fm full matrix on the source communicator
435 : !> \param plan reusable routing and buffers; no outstanding communication on return
436 : !> \param check_plan rebuild collectively if the source layout, FM structure or threads change
437 : ! **************************************************************************************************
438 423345 : SUBROUTINE copy_dbcsr_to_fm_sym(matrix, fm, plan, check_plan)
439 : TYPE(dbcsr_type), INTENT(IN) :: matrix
440 : TYPE(cp_fm_type), INTENT(INOUT) :: fm
441 : TYPE(dbcsr_fm_copy_plan_type), INTENT(INOUT) :: plan
442 : LOGICAL, INTENT(IN) :: check_plan
443 :
444 : INTEGER :: attempt, col, col_offset, i, iblock, j, &
445 : k, mirror, ncol, nrecv, nrow, &
446 : nthreads, pos, rebuild, row, &
447 : row_offset, thread
448 : LOGICAL :: block_matches, layout_matches, transposed
449 : REAL(KIND=dp) :: symmetry_sign, value
450 423345 : REAL(KIND=dp), DIMENSION(:, :), POINTER :: block
451 : TYPE(dbcsr_iterator_type) :: iter
452 : TYPE(mp_para_env_type), POINTER :: para_env
453 :
454 200275 : IF (.NOT. ALLOCATED(plan%thread)) CALL dbcsr_to_fm_plan_create(matrix, fm, plan)
455 423345 : para_env => fm%matrix_struct%context%para_env
456 423345 : symmetry_sign = 1.0_dp
457 423345 : IF (dbcsr_get_matrix_type(matrix) == dbcsr_type_antisymmetric) symmetry_sign = -1.0_dp
458 :
459 423529 : DO attempt = 1, 2
460 : layout_matches = ASSOCIATED(plan%matrix_struct, fm%matrix_struct) .AND. &
461 423529 : plan%matrix_type == dbcsr_get_matrix_type(matrix)
462 : rebuild = 0
463 : !$OMP PARALLEL DEFAULT(NONE) SHARED(matrix, plan, symmetry_sign, layout_matches) &
464 : !$OMP PRIVATE(iter, row, col, block, transposed, row_offset, col_offset, nrow, ncol, &
465 423529 : !$OMP thread, nthreads, iblock, pos, mirror, i, j, k, value, block_matches) REDUCTION(MAX:rebuild)
466 : thread = 0
467 : nthreads = 1
468 : !$ thread = omp_get_thread_num()
469 : !$ nthreads = omp_get_num_threads()
470 : IF (.NOT. layout_matches .OR. nthreads /= SIZE(plan%thread)) THEN
471 : rebuild = 1
472 : ELSE
473 : iblock = 0
474 : pos = 0
475 : CALL dbcsr_iterator_readonly_start(iter, matrix)
476 : DO WHILE (dbcsr_iterator_blocks_left(iter))
477 : CALL dbcsr_iterator_next_block(iter, row, col, block, transposed=transposed, &
478 : row_size=nrow, col_size=ncol, row_offset=row_offset, col_offset=col_offset)
479 : iblock = iblock + 1
480 : block_matches = iblock <= SIZE(plan%thread(thread)%blocks, 2)
481 : IF (block_matches) block_matches = ALL(plan%thread(thread)%blocks(:, iblock) == &
482 : [row, col, nrow, ncol, row_offset, col_offset])
483 : IF (.NOT. block_matches) THEN
484 : rebuild = 1
485 : EXIT
486 : END IF
487 : DO j = 1, ncol
488 : DO i = 1, nrow
489 : IF (transposed) THEN
490 : value = symmetry_sign*block(j, i)
491 : ELSE
492 : value = block(i, j)
493 : END IF
494 : DO mirror = 1, 2
495 : IF (mirror == 2 .AND. row == col) CYCLE
496 : pos = pos + 1
497 : k = plan%thread(thread)%slots(pos)
498 : plan%send_values(k) = value
499 : value = symmetry_sign*value
500 : END DO
501 : END DO
502 : END DO
503 : END DO
504 : CALL dbcsr_iterator_stop(iter)
505 : IF (iblock /= SIZE(plan%thread(thread)%blocks, 2) .OR. &
506 : pos /= SIZE(plan%thread(thread)%slots)) rebuild = 1
507 : END IF
508 : !$OMP END PARALLEL
509 423529 : IF (check_plan) CALL para_env%max(rebuild)
510 423529 : IF (rebuild == 0) EXIT
511 184 : CPASSERT(check_plan .AND. attempt == 1)
512 423529 : CALL dbcsr_to_fm_plan_create(matrix, fm, plan)
513 : END DO
514 :
515 : ! Counts and destination indices were exchanged once when the plan was built.
516 : CALL para_env%alltoall(plan%send_values, plan%send_count, plan%send_disp, &
517 423345 : plan%recv_values, plan%recv_count, plan%recv_disp)
518 1267378 : nrecv = SUM(plan%recv_count)
519 208910625 : fm%local_data = 0.0_dp
520 423345 : !$OMP PARALLEL DO DEFAULT(NONE) SHARED(fm, plan, nrecv) PRIVATE(k)
521 : DO k = 1, nrecv
522 : fm%local_data(plan%recv_index(1, k), plan%recv_index(2, k)) = plan%recv_values(k)
523 : END DO
524 : !$OMP END PARALLEL DO
525 :
526 423345 : END SUBROUTINE copy_dbcsr_to_fm_sym
527 :
528 : ! **************************************************************************************************
529 : !> \brief Cache source-to-packet slots and destination FM indices, not source pointers.
530 : !> Counting and indexing use the same static DBCSR thread assignment. The
531 : !> plan is valid only while that assignment and both matrix layouts are fixed.
532 : !> \param matrix symmetric or antisymmetric DBCSR workspace
533 : !> \param fm destination full matrix
534 : !> \param plan new routing plan
535 : ! **************************************************************************************************
536 200643 : SUBROUTINE dbcsr_to_fm_plan_create(matrix, fm, plan)
537 : TYPE(dbcsr_type), INTENT(IN) :: matrix
538 : TYPE(cp_fm_type), INTENT(IN) :: fm
539 : TYPE(dbcsr_fm_copy_plan_type), INTENT(OUT) :: plan
540 :
541 : CHARACTER(LEN=*), PARAMETER :: routineN = 'dbcsr_to_fm_plan_create'
542 :
543 : INTEGER :: col, col_offset, dest, handle, i, &
544 : iblock, j, k, mirror, ncol, nproc, &
545 : nrow, nthreads, pass, pos, row, &
546 : row_offset, thread, x, y
547 : INTEGER(KIND=int_8) :: count, offset
548 200459 : INTEGER(KIND=int_8), ALLOCATABLE, DIMENSION(:) :: local_pos
549 200459 : INTEGER(KIND=int_8), ALLOCATABLE, DIMENSION(:, :) :: thread_pos
550 200459 : INTEGER, ALLOCATABLE, DIMENSION(:) :: nblocks
551 200459 : INTEGER, ALLOCATABLE, DIMENSION(:, :) :: send_index
552 : LOGICAL :: transposed
553 : TYPE(cp_fm_struct_type), POINTER :: fms
554 : TYPE(dbcsr_iterator_type) :: iter
555 : TYPE(mp_para_env_type), POINTER :: para_env
556 :
557 200459 : CALL timeset(routineN, handle)
558 200459 : fms => fm%matrix_struct
559 200459 : plan%matrix_struct => fms
560 200459 : plan%matrix_type = dbcsr_get_matrix_type(matrix)
561 200459 : para_env => fms%context%para_env
562 200459 : nproc = para_env%num_pe
563 200459 : nthreads = 1
564 200459 : !$ nthreads = omp_get_max_threads()
565 1202754 : ALLOCATE (thread_pos(0:nproc - 1, 0:nthreads - 1), nblocks(0:nthreads - 1))
566 0 : ALLOCATE (plan%send_count(0:nproc - 1), plan%send_disp(0:nproc - 1), &
567 1202754 : plan%recv_count(0:nproc - 1), plan%recv_disp(0:nproc - 1))
568 200459 : thread_pos(:, :) = 0
569 200459 : nblocks(:) = 0
570 :
571 : !$OMP PARALLEL DEFAULT(NONE) &
572 : !$OMP SHARED(matrix, fms, nproc, nthreads, &
573 : !$OMP thread_pos, nblocks, send_index, plan) &
574 : !$OMP PRIVATE(iter, row, col, row_offset, col_offset, nrow, ncol, &
575 200459 : !$OMP thread, iblock, pos, pass, mirror, i, j, x, y, dest, k, offset, count, local_pos, transposed)
576 : thread = 0
577 : !$ thread = omp_get_thread_num()
578 : ALLOCATE (local_pos(0:nproc - 1))
579 : local_pos(:) = 0
580 : DO pass = 1, 2
581 : iblock = 0
582 : pos = 0
583 : IF (pass == 2) THEN
584 : !$OMP SINGLE
585 : CPASSERT(SUM(thread_pos) <= INT(HUGE(0), int_8)/2)
586 : plan%send_count(:) = INT(SUM(thread_pos, DIM=2))
587 : !$ nthreads = omp_get_num_threads()
588 : ALLOCATE (plan%thread(0:nthreads - 1))
589 : DO k = 0, nthreads - 1
590 : ALLOCATE (plan%thread(k)%blocks(6, nblocks(k)), &
591 : plan%thread(k)%slots(INT(SUM(thread_pos(:, k)))))
592 : END DO
593 : offset = 0
594 : DO dest = 0, nproc - 1
595 : plan%send_disp(dest) = INT(offset)
596 : DO k = 0, nthreads - 1
597 : count = thread_pos(dest, k)
598 : thread_pos(dest, k) = offset
599 : offset = offset + count
600 : END DO
601 : END DO
602 : ALLOCATE (send_index(2, MAX(1, SUM(plan%send_count))), &
603 : plan%send_values(MAX(1, SUM(plan%send_count))))
604 : send_index(:, :) = 0
605 : plan%send_values(:) = 0.0_dp
606 : !$OMP END SINGLE
607 : local_pos(:) = thread_pos(:, thread)
608 : END IF
609 : CALL dbcsr_iterator_readonly_start(iter, matrix)
610 : DO WHILE (dbcsr_iterator_blocks_left(iter))
611 : CALL dbcsr_iterator_next_block(iter, row, col, row_size=nrow, col_size=ncol, &
612 : row_offset=row_offset, col_offset=col_offset, transposed=transposed)
613 : iblock = iblock + 1
614 : IF (pass == 2) THEN
615 : plan%thread(thread)%blocks(:, iblock) = [row, col, nrow, ncol, row_offset, col_offset]
616 : END IF
617 : DO j = 1, ncol
618 : DO i = 1, nrow
619 : DO mirror = 1, 2
620 : IF (mirror == 2 .AND. row == col) CYCLE
621 : x = row_offset + i - 1
622 : y = col_offset + j - 1
623 : IF (mirror == 2) THEN
624 : x = col_offset + j - 1
625 : y = row_offset + i - 1
626 : END IF
627 : dest = fms%context%blacs2mpi(fms%g2p_row(x), fms%g2p_col(y))
628 : local_pos(dest) = local_pos(dest) + 1
629 : IF (pass == 1) CYCLE
630 : k = INT(local_pos(dest))
631 : send_index(:, k) = [fms%g2l_row(x), fms%g2l_col(y)]
632 : pos = pos + 1
633 : plan%thread(thread)%slots(pos) = k
634 : END DO
635 : END DO
636 : END DO
637 : END DO
638 : CALL dbcsr_iterator_stop(iter)
639 : IF (pass == 1) THEN
640 : thread_pos(:, thread) = local_pos(:)
641 : nblocks(thread) = iblock
642 : END IF
643 : !$OMP BARRIER
644 : END DO
645 : DEALLOCATE (local_pos)
646 : !$OMP END PARALLEL
647 200459 : DEALLOCATE (thread_pos, nblocks)
648 :
649 200459 : CALL para_env%alltoall(plan%send_count, plan%recv_count, 1)
650 600412 : CPASSERT(SUM(INT(plan%recv_count, int_8)) <= INT(HUGE(0), int_8)/2)
651 200459 : plan%recv_disp(0) = 0
652 399953 : DO dest = 1, nproc - 1
653 399953 : plan%recv_disp(dest) = plan%recv_disp(dest - 1) + plan%recv_count(dest - 1)
654 : END DO
655 0 : ALLOCATE (plan%recv_index(2, MAX(1, SUM(plan%recv_count))), &
656 1802201 : plan%recv_values(MAX(1, SUM(plan%recv_count))))
657 210246071 : plan%recv_index(:, :) = 0
658 70215663 : plan%recv_values(:) = 0.0_dp
659 : CALL para_env%alltoall(send_index, 2*plan%send_count, 2*plan%send_disp, &
660 1800271 : plan%recv_index, 2*plan%recv_count, 2*plan%recv_disp)
661 200459 : CALL timestop(handle)
662 :
663 601377 : END SUBROUTINE dbcsr_to_fm_plan_create
664 :
665 : ! **************************************************************************************************
666 : !> \brief Copy a DBCSR_BLACS matrix to a BLACS matrix
667 : !> \param bc_mat DBCSR matrix
668 : !> \param[out] fm full matrix
669 : ! **************************************************************************************************
670 851710 : SUBROUTINE copy_dbcsr_to_fm_bc(bc_mat, fm)
671 : TYPE(dbcsr_type), INTENT(IN) :: bc_mat
672 : TYPE(cp_fm_type), INTENT(INOUT) :: fm
673 :
674 : CHARACTER(LEN=*), PARAMETER :: routineN = 'copy_dbcsr_to_fm_bc'
675 :
676 : INTEGER :: col, handle, row
677 851710 : INTEGER, ALLOCATABLE, DIMENSION(:) :: first_col, first_row, last_col, last_row
678 851710 : REAL(KIND=dp), DIMENSION(:, :), POINTER :: dbcsr_block, fm_block
679 : TYPE(dbcsr_iterator_type) :: iter
680 :
681 851710 : CALL timeset(routineN, handle)
682 :
683 851710 : CALL calculate_fm_block_ranges(bc_mat, first_row, last_row, first_col, last_col)
684 :
685 : ! Now copy data to the FM matrix
686 851710 : fm_block => fm%local_data
687 196021515 : fm_block = REAL(0.0, KIND=dp)
688 : !$OMP PARALLEL DEFAULT(NONE) PRIVATE(iter, row, col, dbcsr_block) &
689 851710 : !$OMP SHARED(bc_mat, last_row, first_row, last_col, first_col, fm_block)
690 : CALL dbcsr_iterator_readonly_start(iter, bc_mat)
691 : DO WHILE (dbcsr_iterator_blocks_left(iter))
692 : CALL dbcsr_iterator_next_block(iter, row, col, dbcsr_block)
693 : fm_block(first_row(row):last_row(row), first_col(col):last_col(col)) = dbcsr_block(:, :)
694 : END DO
695 : CALL dbcsr_iterator_stop(iter)
696 : !$OMP END PARALLEL
697 :
698 851710 : CALL timestop(handle)
699 1703420 : END SUBROUTINE copy_dbcsr_to_fm_bc
700 :
701 : ! **************************************************************************************************
702 : !> \brief Helper routine used to copy blocks from DBCSR into FM matrices and vice versa
703 : !> \param bc_mat ...
704 : !> \param first_row ...
705 : !> \param last_row ...
706 : !> \param first_col ...
707 : !> \param last_col ...
708 : !> \author Ole Schuett
709 : ! **************************************************************************************************
710 2193151 : SUBROUTINE calculate_fm_block_ranges(bc_mat, first_row, last_row, first_col, last_col)
711 : TYPE(dbcsr_type), INTENT(IN) :: bc_mat
712 : INTEGER, ALLOCATABLE, DIMENSION(:), INTENT(OUT) :: first_row, last_row, first_col, last_col
713 :
714 : INTEGER :: col, nblkcols_local, nblkcols_total, &
715 : nblkrows_local, nblkrows_total, row
716 : INTEGER, ALLOCATABLE, DIMENSION(:) :: local_col_sizes, local_row_sizes
717 2193151 : INTEGER, DIMENSION(:), POINTER :: col_blk_size, local_cols, local_rows, &
718 2193151 : row_blk_size
719 :
720 : CALL dbcsr_get_info(bc_mat, &
721 : nblkrows_total=nblkrows_total, &
722 : nblkcols_total=nblkcols_total, &
723 : nblkrows_local=nblkrows_local, &
724 : nblkcols_local=nblkcols_local, &
725 : local_rows=local_rows, &
726 : local_cols=local_cols, &
727 : row_blk_size=row_blk_size, &
728 2193151 : col_blk_size=col_blk_size)
729 :
730 : ! calculate first_row and last_row
731 6578205 : ALLOCATE (local_row_sizes(nblkrows_total))
732 2193151 : local_row_sizes(:) = 0
733 2193151 : IF (nblkrows_local >= 1) THEN
734 5437072 : DO row = 1, nblkrows_local
735 5437072 : local_row_sizes(local_rows(row)) = row_blk_size(local_rows(row))
736 : END DO
737 : END IF
738 6576957 : ALLOCATE (first_row(nblkrows_total), last_row(nblkrows_total))
739 2193151 : CALL dbcsr_convert_sizes_to_offsets(local_row_sizes, first_row, last_row)
740 2193151 : DEALLOCATE (local_row_sizes)
741 :
742 : ! calculate first_col and last_col
743 6575825 : ALLOCATE (local_col_sizes(nblkcols_total))
744 2193151 : local_col_sizes(:) = 0
745 2193151 : IF (nblkcols_local >= 1) THEN
746 6259742 : DO col = 1, nblkcols_local
747 6259742 : local_col_sizes(local_cols(col)) = col_blk_size(local_cols(col))
748 : END DO
749 : END IF
750 6572197 : ALLOCATE (first_col(nblkcols_total), last_col(nblkcols_total))
751 2193151 : CALL dbcsr_convert_sizes_to_offsets(local_col_sizes, first_col, last_col)
752 2193151 : DEALLOCATE (local_col_sizes)
753 :
754 2193151 : END SUBROUTINE calculate_fm_block_ranges
755 :
756 : ! **************************************************************************************************
757 : !> \brief hack for dbcsr_copy_columns
758 : !> \param matrix_b ...
759 : !> \param matrix_a ...
760 : !> \param ncol ...
761 : !> \param source_start ...
762 : !> \param target_start ...
763 : !> \param para_env ...
764 : !> \param blacs_env ...
765 : !> \author vw
766 : ! **************************************************************************************************
767 10472 : SUBROUTINE dbcsr_copy_columns_hack(matrix_b, matrix_a, &
768 : ncol, source_start, target_start, para_env, blacs_env)
769 :
770 : TYPE(dbcsr_type), INTENT(INOUT) :: matrix_b
771 : TYPE(dbcsr_type), INTENT(IN) :: matrix_a
772 : INTEGER, INTENT(IN) :: ncol, source_start, target_start
773 : TYPE(mp_para_env_type), POINTER :: para_env
774 : TYPE(cp_blacs_env_type), POINTER :: blacs_env
775 :
776 : INTEGER :: nfullcols_total, nfullrows_total
777 : TYPE(cp_fm_struct_type), POINTER :: fm_struct
778 : TYPE(cp_fm_type) :: fm_matrix_a, fm_matrix_b
779 :
780 2618 : NULLIFY (fm_struct)
781 2618 : CALL dbcsr_get_info(matrix_a, nfullrows_total=nfullrows_total, nfullcols_total=nfullcols_total)
782 : CALL cp_fm_struct_create(fm_struct, context=blacs_env, nrow_global=nfullrows_total, &
783 2618 : ncol_global=nfullcols_total, para_env=para_env)
784 2618 : CALL cp_fm_create(fm_matrix_a, fm_struct, name="fm_matrix_a")
785 2618 : CALL cp_fm_struct_release(fm_struct)
786 :
787 2618 : CALL dbcsr_get_info(matrix_b, nfullrows_total=nfullrows_total, nfullcols_total=nfullcols_total)
788 : CALL cp_fm_struct_create(fm_struct, context=blacs_env, nrow_global=nfullrows_total, &
789 2618 : ncol_global=nfullcols_total, para_env=para_env)
790 2618 : CALL cp_fm_create(fm_matrix_b, fm_struct, name="fm_matrix_b")
791 2618 : CALL cp_fm_struct_release(fm_struct)
792 :
793 2618 : CALL copy_dbcsr_to_fm(matrix_a, fm_matrix_a)
794 2618 : CALL copy_dbcsr_to_fm(matrix_b, fm_matrix_b)
795 :
796 2618 : CALL cp_fm_to_fm(fm_matrix_a, fm_matrix_b, ncol, source_start, target_start)
797 :
798 2618 : CALL copy_fm_to_dbcsr(fm_matrix_b, matrix_b)
799 :
800 2618 : CALL cp_fm_release(fm_matrix_a)
801 2618 : CALL cp_fm_release(fm_matrix_b)
802 :
803 2618 : END SUBROUTINE dbcsr_copy_columns_hack
804 :
805 : ! **************************************************************************************************
806 : !> \brief Creates a DBCSR distribution from a distribution_2d
807 : !> \param[in] dist2d distribution_2d
808 : !> \param[out] dist DBCSR distribution
809 : !> \par History
810 : !> move form dbcsr_operation 01.2010
811 : ! **************************************************************************************************
812 12886 : SUBROUTINE cp_dbcsr_dist2d_to_dist(dist2d, dist)
813 : TYPE(distribution_2d_type), INTENT(IN), TARGET :: dist2d
814 : TYPE(dbcsr_distribution_type), INTENT(OUT) :: dist
815 :
816 12886 : INTEGER, CONTIGUOUS, DIMENSION(:), POINTER :: col_dist, row_dist
817 12886 : INTEGER, CONTIGUOUS, DIMENSION(:, :), POINTER :: col_dist_data, pgrid, row_dist_data
818 : TYPE(cp_blacs_env_type), POINTER :: blacs_env
819 : TYPE(distribution_2d_type), POINTER :: dist2d_p
820 : TYPE(mp_para_env_type), POINTER :: para_env
821 :
822 12886 : dist2d_p => dist2d
823 : CALL distribution_2d_get(dist2d_p, &
824 : row_distribution=row_dist_data, &
825 : col_distribution=col_dist_data, &
826 12886 : blacs_env=blacs_env)
827 12886 : CALL blacs_env%get(para_env=para_env, blacs2mpi=pgrid)
828 :
829 : ! map to 1D arrays
830 12886 : row_dist => row_dist_data(:, 1)
831 12886 : col_dist => col_dist_data(:, 1)
832 : !row_cluster => row_dist_data(:, 2)
833 : !col_cluster => col_dist_data(:, 2)
834 :
835 : CALL dbcsr_distribution_new(dist, &
836 : group=para_env%get_handle(), pgrid=pgrid, &
837 : row_dist=row_dist, &
838 12886 : col_dist=col_dist)
839 :
840 12886 : END SUBROUTINE cp_dbcsr_dist2d_to_dist
841 :
842 : ! **************************************************************************************************
843 : !> \brief multiply a dbcsr with a replicated array
844 : !> c = alpha_scalar * A (dbscr) * b + c
845 : !> \param[in] matrix_a DBSCR matrxx
846 : !> \param[in] vec_b vectors b
847 : !> \param[inout] vec_c vectors c
848 : !> \param[in] ncol nbr of columns
849 : !> \param[in] alpha alpha
850 : !>
851 : ! **************************************************************************************************
852 0 : SUBROUTINE dbcsr_multiply_local(matrix_a, vec_b, vec_c, ncol, alpha)
853 : TYPE(dbcsr_type), INTENT(IN) :: matrix_a
854 : REAL(dp), DIMENSION(:, :), INTENT(IN) :: vec_b
855 : REAL(dp), DIMENSION(:, :), INTENT(INOUT) :: vec_c
856 : INTEGER, INTENT(in), OPTIONAL :: ncol
857 : REAL(dp), INTENT(IN), OPTIONAL :: alpha
858 :
859 : CHARACTER(LEN=*), PARAMETER :: routineN = 'dbcsr_multiply_local'
860 :
861 : INTEGER :: col, coloff, my_ncol, nrows, row, &
862 : rowoff, timing_handle
863 : LOGICAL :: has_symm
864 : REAL(dp) :: my_alpha, my_alpha2
865 0 : REAL(dp), ALLOCATABLE, DIMENSION(:, :) :: vec_c_priv
866 0 : REAL(dp), DIMENSION(:, :), POINTER :: data_d
867 : TYPE(dbcsr_iterator_type) :: iter
868 :
869 0 : CALL timeset(routineN, timing_handle)
870 :
871 0 : my_alpha = 1.0_dp
872 0 : IF (PRESENT(alpha)) my_alpha = alpha
873 :
874 0 : my_ncol = SIZE(vec_b, 2)
875 0 : IF (PRESENT(ncol)) my_ncol = ncol
876 0 : nrows = SIZE(vec_c, 1)
877 :
878 0 : my_alpha2 = 0.0_dp
879 0 : IF (dbcsr_get_matrix_type(matrix_a) == dbcsr_type_symmetric) my_alpha2 = my_alpha
880 0 : IF (dbcsr_get_matrix_type(matrix_a) == dbcsr_type_antisymmetric) my_alpha2 = -my_alpha
881 :
882 : has_symm = (dbcsr_get_matrix_type(matrix_a) == dbcsr_type_symmetric .OR. &
883 0 : dbcsr_get_matrix_type(matrix_a) == dbcsr_type_antisymmetric)
884 :
885 : !$OMP PARALLEL DEFAULT(NONE) &
886 : !$OMP SHARED(matrix_a, vec_b, vec_c, my_alpha2, my_alpha, my_ncol, nrows, has_symm) &
887 0 : !$OMP PRIVATE(iter, row, col, data_d, rowoff, coloff, vec_c_priv)
888 : ALLOCATE (vec_c_priv(nrows, my_ncol))
889 : vec_c_priv(:, :) = 0.0_dp
890 : CALL dbcsr_iterator_readonly_start(iter, matrix_a)
891 : DO WHILE (dbcsr_iterator_blocks_left(iter))
892 : CALL dbcsr_iterator_next_block(iter, row, col, data_d, row_offset=rowoff, col_offset=coloff)
893 : IF (my_ncol /= 1) THEN
894 : CALL dgemm('N', 'N', &
895 : SIZE(data_d, 1), my_ncol, SIZE(data_d, 2), &
896 : my_alpha, data_d(1, 1), SIZE(data_d, 1), &
897 : vec_b(coloff, 1), SIZE(vec_b, 1), &
898 : 1.0_dp, vec_c_priv(rowoff, 1), nrows)
899 : ELSE
900 : CALL dgemv('N', SIZE(data_d, 1), SIZE(data_d, 2), &
901 : my_alpha, data_d(1, 1), SIZE(data_d, 1), &
902 : vec_b(coloff, 1), 1, &
903 : 1.0_dp, vec_c_priv(rowoff, 1), 1)
904 : END IF
905 : IF (has_symm .AND. row /= col) THEN
906 : IF (my_ncol /= 1) THEN
907 : CALL dgemm('T', 'N', &
908 : SIZE(data_d, 2), my_ncol, SIZE(data_d, 1), &
909 : my_alpha2, data_d(1, 1), SIZE(data_d, 1), &
910 : vec_b(rowoff, 1), SIZE(vec_b, 1), &
911 : 1.0_dp, vec_c_priv(coloff, 1), nrows)
912 : ELSE
913 : CALL dgemv('T', SIZE(data_d, 1), SIZE(data_d, 2), &
914 : my_alpha2, data_d(1, 1), SIZE(data_d, 1), &
915 : vec_b(rowoff, 1), 1, &
916 : 1.0_dp, vec_c_priv(coloff, 1), 1)
917 : END IF
918 : END IF
919 : END DO
920 : CALL dbcsr_iterator_stop(iter)
921 : !$OMP CRITICAL(dbcsr_multiply_local_crit)
922 : vec_c(:, 1:my_ncol) = vec_c(:, 1:my_ncol) + vec_c_priv(:, 1:my_ncol)
923 : !$OMP END CRITICAL(dbcsr_multiply_local_crit)
924 : DEALLOCATE (vec_c_priv)
925 : !$OMP END PARALLEL
926 :
927 0 : CALL timestop(timing_handle)
928 0 : END SUBROUTINE dbcsr_multiply_local
929 :
930 : ! **************************************************************************************************
931 : !> \brief multiply a dbcsr with a fm matrix
932 : !>
933 : !> For backwards compatibility with BLAS XGEMM, this routine supports
934 : !> the multiplication of matrices with incompatible dimensions.
935 : !>
936 : !> \param[in] matrix DBCSR matrix
937 : !> \param fm_in full matrix
938 : !> \param fm_out full matrix
939 : !> \param[in] ncol nbr of columns
940 : !> \param[in] alpha alpha
941 : !> \param[in] beta beta
942 : !>
943 : ! **************************************************************************************************
944 3198090 : SUBROUTINE cp_dbcsr_sm_fm_multiply(matrix, fm_in, fm_out, ncol, alpha, beta)
945 : TYPE(dbcsr_type), INTENT(IN) :: matrix
946 : TYPE(cp_fm_type), INTENT(IN) :: fm_in
947 : TYPE(cp_fm_type), INTENT(INOUT) :: fm_out
948 : INTEGER, INTENT(IN) :: ncol
949 : REAL(dp), INTENT(IN), OPTIONAL :: alpha, beta
950 :
951 : CHARACTER(LEN=*), PARAMETER :: routineN = 'cp_dbcsr_sm_fm_multiply'
952 :
953 : INTEGER :: a_ncol, a_nrow, b_ncol, b_nrow, c_ncol, &
954 : c_nrow, icol_local, k_in, k_out, &
955 : ncol_local, timing_handle, &
956 : timing_handle_mult
957 533015 : INTEGER, CONTIGUOUS, DIMENSION(:), POINTER :: col_blk_size, col_blk_size_right_in, &
958 533015 : col_blk_size_right_out, col_dist, &
959 533015 : row_blk_size, row_dist
960 533015 : INTEGER, DIMENSION(:), POINTER :: col_indices
961 : TYPE(dbcsr_type) :: in, out
962 : TYPE(dbcsr_distribution_type) :: dist, dist_right_in, product_dist
963 : REAL(dp) :: my_alpha, my_beta
964 :
965 533015 : CALL timeset(routineN, timing_handle)
966 :
967 533015 : my_alpha = 1.0_dp
968 533015 : my_beta = 0.0_dp
969 533015 : IF (PRESENT(alpha)) my_alpha = alpha
970 533015 : IF (PRESENT(beta)) my_beta = beta
971 :
972 : ! TODO
973 533015 : CALL cp_fm_get_info(fm_in, ncol_global=b_ncol, nrow_global=b_nrow)
974 533015 : CALL cp_fm_get_info(fm_out, ncol_global=c_ncol, nrow_global=c_nrow)
975 533015 : CALL dbcsr_get_info(matrix, nfullrows_total=a_nrow, nfullcols_total=a_ncol)
976 : !WRITE(*,*) "cp_dbcsr_sm_fm_multiply: A ", a_nrow, "x", a_ncol
977 : !WRITE(*,*) "cp_dbcsr_sm_fm_multiply: B ", b_nrow, "x", b_ncol
978 : !WRITE(*,*) "cp_dbcsr_sm_fm_multiply: C ", c_nrow, "x", c_ncol
979 :
980 533015 : CALL cp_fm_get_info(fm_out, ncol_global=k_out)
981 :
982 533015 : CALL cp_fm_get_info(fm_in, ncol_global=k_in)
983 : !write(*,*)routineN//" -----------------------------------"
984 : !IF (k_in /= k_out) &
985 : ! WRITE(*,'(3(A,I5,1X),2(A,F5.2,1X))')&
986 : ! routineN//" ncol", ncol,'k_in',k_in,'k_out',k_out,&
987 : ! 'alpha',my_alpha,'beta',my_beta
988 :
989 533015 : IF (ncol > 0 .AND. k_out > 0 .AND. k_in > 0) THEN
990 531735 : CALL dbcsr_get_info(matrix, row_blk_size=row_blk_size, col_blk_size=col_blk_size, distribution=dist)
991 531735 : CALL dbcsr_create_dist_r_unrot(dist_right_in, dist, k_in, col_blk_size_right_in)
992 :
993 : CALL dbcsr_create(in, "D", dist_right_in, dbcsr_type_no_symmetry, &
994 531735 : col_blk_size, col_blk_size_right_in)
995 :
996 531735 : CALL dbcsr_distribution_get(dist, row_dist=row_dist)
997 531735 : CALL dbcsr_distribution_get(dist_right_in, col_dist=col_dist)
998 : CALL dbcsr_distribution_new(product_dist, template=dist, &
999 531735 : row_dist=row_dist, col_dist=col_dist)
1000 1595205 : ALLOCATE (col_blk_size_right_out(SIZE(col_blk_size_right_in)))
1001 2164176 : col_blk_size_right_out = col_blk_size_right_in
1002 531735 : CALL match_col_sizes(col_blk_size_right_out, col_blk_size_right_in, k_out)
1003 :
1004 : !if (k_in .ne. k_out) then
1005 : ! write(*,*)routineN//" in cs", col_blk_size_right_in
1006 : ! write(*,*)routineN//" out cs", col_blk_size_right_out
1007 : !endif
1008 :
1009 : CALL dbcsr_create(out, "D", product_dist, dbcsr_type_no_symmetry, &
1010 531735 : row_blk_size, col_blk_size_right_out)
1011 :
1012 531735 : CALL copy_fm_to_dbcsr(fm_in, in)
1013 531735 : IF (ncol /= k_out .OR. my_beta /= 0.0_dp) THEN
1014 119736 : IF (my_beta == 0.0_dp) THEN
1015 6106 : CALL cp_fm_get_info(fm_out, ncol_local=ncol_local, col_indices=col_indices)
1016 30972 : DO icol_local = 1, ncol_local
1017 174345 : IF (col_indices(icol_local) <= ncol) fm_out%local_data(:, icol_local) = 0.0_dp
1018 : END DO
1019 : END IF
1020 119736 : CALL copy_fm_to_dbcsr(fm_out, out)
1021 : END IF
1022 :
1023 531735 : CALL timeset(routineN//'_core', timing_handle_mult)
1024 : CALL dbcsr_multiply("N", "N", my_alpha, matrix, in, my_beta, out, &
1025 531735 : last_column=ncol)
1026 531735 : CALL timestop(timing_handle_mult)
1027 :
1028 531735 : CALL copy_dbcsr_to_fm(out, fm_out)
1029 :
1030 531735 : CALL dbcsr_release(in)
1031 531735 : CALL dbcsr_release(out)
1032 531735 : DEALLOCATE (col_blk_size_right_in, col_blk_size_right_out)
1033 531735 : CALL dbcsr_distribution_release(dist_right_in)
1034 2126940 : CALL dbcsr_distribution_release(product_dist)
1035 :
1036 : END IF
1037 :
1038 533015 : CALL timestop(timing_handle)
1039 :
1040 533015 : END SUBROUTINE cp_dbcsr_sm_fm_multiply
1041 :
1042 : ! **************************************************************************************************
1043 : !> \brief ...
1044 : !> \param sizes1 ...
1045 : !> \param sizes2 ...
1046 : !> \param full_num ...
1047 : ! **************************************************************************************************
1048 531735 : SUBROUTINE match_col_sizes(sizes1, sizes2, full_num)
1049 : INTEGER, DIMENSION(:), INTENT(INOUT) :: sizes1
1050 : INTEGER, DIMENSION(:), INTENT(IN) :: sizes2
1051 : INTEGER, INTENT(IN) :: full_num
1052 :
1053 : INTEGER :: left, n1, n2, p, rm, used
1054 :
1055 531735 : n1 = SIZE(sizes1)
1056 531735 : n2 = SIZE(sizes2)
1057 531735 : IF (n1 /= n2) THEN
1058 0 : CPABORT("distributions must be equal!")
1059 : END IF
1060 1082088 : sizes1(1:n1) = sizes2(1:n1)
1061 1082088 : used = SUM(sizes1(1:n1))
1062 : ! If sizes1 does not cover everything, then we increase the
1063 : ! size of the last block; otherwise we reduce the blocks
1064 : ! (from the end) until it is small enough.
1065 531735 : IF (used < full_num) THEN
1066 0 : sizes1(n1) = sizes1(n1) + full_num - used
1067 : ELSE
1068 531735 : left = used - full_num
1069 531735 : p = n1
1070 531735 : DO WHILE (left > 0 .AND. p > 0)
1071 0 : rm = MIN(left, sizes1(p))
1072 0 : sizes1(p) = sizes1(p) - rm
1073 0 : left = left - rm
1074 0 : p = p - 1
1075 : END DO
1076 : END IF
1077 531735 : END SUBROUTINE match_col_sizes
1078 :
1079 : ! **************************************************************************************************
1080 : !> \brief performs the multiplication sparse_matrix+dense_mat*dens_mat^T
1081 : !> if matrix_g is not explicitly given, matrix_v^T will be used
1082 : !> this can be important to save the necessary redistribute for a
1083 : !> different matrix_g and increase performance.
1084 : !> \param sparse_matrix ...
1085 : !> \param matrix_v ...
1086 : !> \param matrix_g ...
1087 : !> \param ncol ...
1088 : !> \param alpha ...
1089 : !> \param keep_sparsity Determines if the sparsity of sparse_matrix is retained
1090 : !> by default it is TRUE
1091 : !> \param symmetry_mode There are the following modes
1092 : !> 1: sparse_matrix += 0.5*alpha*(v*g^T+g^T*v) (symmetric update)
1093 : !> -1: sparse_matrix += 0.5*alpha*(v*g^T-g^T*v) (skewsymmetric update)
1094 : !> else: sparse_matrix += alpha*v*g^T (no symmetry, default)
1095 : !> saves some redistribution steps
1096 : ! **************************************************************************************************
1097 268618 : SUBROUTINE cp_dbcsr_plus_fm_fm_t(sparse_matrix, matrix_v, matrix_g, ncol, alpha, keep_sparsity, symmetry_mode)
1098 : TYPE(dbcsr_type), INTENT(INOUT) :: sparse_matrix
1099 : TYPE(cp_fm_type), INTENT(IN) :: matrix_v
1100 : TYPE(cp_fm_type), INTENT(IN), OPTIONAL :: matrix_g
1101 : INTEGER, INTENT(IN) :: ncol
1102 : REAL(KIND=dp), INTENT(IN), OPTIONAL :: alpha
1103 : LOGICAL, INTENT(IN), OPTIONAL :: keep_sparsity
1104 : INTEGER, INTENT(IN), OPTIONAL :: symmetry_mode
1105 :
1106 : CHARACTER(LEN=*), PARAMETER :: routineN = 'cp_dbcsr_plus_fm_fm_t'
1107 :
1108 : INTEGER :: k, my_symmetry_mode, nao, npcols, &
1109 : timing_handle
1110 268618 : INTEGER, CONTIGUOUS, DIMENSION(:), POINTER :: col_blk_size_left, col_dist_left, &
1111 268618 : row_blk_size, row_dist
1112 : LOGICAL :: check_product, my_keep_sparsity
1113 : REAL(KIND=dp) :: my_alpha, norm
1114 : TYPE(cp_fm_struct_type), POINTER :: fm_struct_tmp
1115 : TYPE(cp_fm_type) :: fm_matrix
1116 : TYPE(dbcsr_distribution_type) :: dist_left, sparse_dist
1117 : TYPE(dbcsr_type) :: mat_g, mat_v, sparse_matrix2, &
1118 : sparse_matrix3
1119 :
1120 268618 : check_product = .FALSE.
1121 :
1122 268618 : CALL timeset(routineN, timing_handle)
1123 :
1124 268618 : my_keep_sparsity = .TRUE.
1125 268618 : IF (PRESENT(keep_sparsity)) my_keep_sparsity = keep_sparsity
1126 :
1127 268618 : my_symmetry_mode = 0
1128 268618 : IF (PRESENT(symmetry_mode)) my_symmetry_mode = symmetry_mode
1129 :
1130 268618 : NULLIFY (col_dist_left)
1131 :
1132 268618 : IF (ncol > 0) THEN
1133 266630 : IF (.NOT. dbcsr_valid_index(sparse_matrix)) THEN
1134 0 : CPABORT("sparse_matrix must pre-exist")
1135 : END IF
1136 : !
1137 : ! Setup matrix_v
1138 266630 : CALL cp_fm_get_info(matrix_v, ncol_global=k)
1139 : !WRITE(*,*)routineN//'truncated mult k, ncol',k,ncol,' PRESENT (matrix_g)',PRESENT (matrix_g)
1140 266630 : CALL dbcsr_get_info(sparse_matrix, distribution=sparse_dist)
1141 266630 : CALL dbcsr_distribution_get(sparse_dist, npcols=npcols, row_dist=row_dist)
1142 266630 : CALL create_bl_distribution(col_dist_left, col_blk_size_left, k, npcols)
1143 : CALL dbcsr_distribution_new(dist_left, template=sparse_dist, &
1144 266630 : row_dist=row_dist, col_dist=col_dist_left)
1145 266630 : DEALLOCATE (col_dist_left)
1146 266630 : CALL dbcsr_get_info(sparse_matrix, row_blk_size=row_blk_size)
1147 : CALL dbcsr_create(mat_v, "DBCSR matrix_v", dist_left, dbcsr_type_no_symmetry, &
1148 266630 : row_blk_size, col_blk_size_left)
1149 266630 : CALL copy_fm_to_dbcsr(matrix_v, mat_v)
1150 266630 : CALL dbcsr_verify_matrix(mat_v)
1151 : !
1152 : ! Setup matrix_g
1153 266630 : IF (PRESENT(matrix_g)) THEN
1154 : CALL dbcsr_create(mat_g, "DBCSR matrix_g", dist_left, dbcsr_type_no_symmetry, &
1155 129303 : row_blk_size, col_blk_size_left)
1156 129303 : CALL copy_fm_to_dbcsr(matrix_g, mat_g)
1157 : END IF
1158 : !
1159 266630 : DEALLOCATE (col_blk_size_left)
1160 266630 : CALL dbcsr_distribution_release(dist_left)
1161 : !
1162 : !
1163 : IF (check_product) THEN
1164 : CALL cp_fm_get_info(matrix_v, nrow_global=nao)
1165 : CALL cp_fm_struct_create(fm_struct_tmp, context=matrix_v%matrix_struct%context, nrow_global=nao, &
1166 : ncol_global=nao, para_env=matrix_v%matrix_struct%para_env)
1167 : CALL cp_fm_create(fm_matrix, fm_struct_tmp, name="fm matrix")
1168 : CALL cp_fm_struct_release(fm_struct_tmp)
1169 : CALL copy_dbcsr_to_fm(sparse_matrix, fm_matrix)
1170 : CALL dbcsr_copy(sparse_matrix3, sparse_matrix)
1171 : END IF
1172 : !
1173 266630 : my_alpha = 1.0_dp
1174 266630 : IF (PRESENT(alpha)) my_alpha = alpha
1175 266630 : IF (PRESENT(matrix_g)) THEN
1176 129303 : IF (my_symmetry_mode == 1) THEN
1177 : ! Symmetric mode
1178 : CALL dbcsr_multiply("N", "T", 0.5_dp*my_alpha, mat_v, mat_g, &
1179 : 1.0_dp, sparse_matrix, &
1180 : retain_sparsity=my_keep_sparsity, &
1181 43966 : last_k=ncol)
1182 : CALL dbcsr_multiply("N", "T", 0.5_dp*my_alpha, mat_g, mat_v, &
1183 : 1.0_dp, sparse_matrix, &
1184 : retain_sparsity=my_keep_sparsity, &
1185 43966 : last_k=ncol)
1186 85337 : ELSE IF (my_symmetry_mode == -1) THEN
1187 : ! Skewsymmetric mode
1188 : CALL dbcsr_multiply("N", "T", 0.5_dp*my_alpha, mat_v, mat_g, &
1189 : 1.0_dp, sparse_matrix, &
1190 : retain_sparsity=my_keep_sparsity, &
1191 3016 : last_k=ncol)
1192 : CALL dbcsr_multiply("N", "T", -0.5_dp*my_alpha, mat_g, mat_v, &
1193 : 1.0_dp, sparse_matrix, &
1194 : retain_sparsity=my_keep_sparsity, &
1195 3016 : last_k=ncol)
1196 : ELSE
1197 : ! Normal mode
1198 : CALL dbcsr_multiply("N", "T", my_alpha, mat_v, mat_g, &
1199 : 1.0_dp, sparse_matrix, &
1200 : retain_sparsity=my_keep_sparsity, &
1201 82321 : last_k=ncol)
1202 : END IF
1203 : ELSE
1204 : CALL dbcsr_multiply("N", "T", my_alpha, mat_v, mat_v, &
1205 : 1.0_dp, sparse_matrix, &
1206 : retain_sparsity=my_keep_sparsity, &
1207 137327 : last_k=ncol)
1208 : END IF
1209 :
1210 : IF (check_product) THEN
1211 : IF (PRESENT(matrix_g)) THEN
1212 : IF (my_symmetry_mode == 1) THEN
1213 : CALL cp_fm_gemm("N", "T", nao, nao, ncol, 0.5_dp*my_alpha, matrix_v, matrix_g, &
1214 : 1.0_dp, fm_matrix)
1215 : CALL cp_fm_gemm("N", "T", nao, nao, ncol, 0.5_dp*my_alpha, matrix_g, matrix_v, &
1216 : 1.0_dp, fm_matrix)
1217 : ELSE IF (my_symmetry_mode == -1) THEN
1218 : CALL cp_fm_gemm("N", "T", nao, nao, ncol, 0.5_dp*my_alpha, matrix_v, matrix_g, &
1219 : 1.0_dp, fm_matrix)
1220 : CALL cp_fm_gemm("N", "T", nao, nao, ncol, -0.5_dp*my_alpha, matrix_g, matrix_v, &
1221 : 1.0_dp, fm_matrix)
1222 : ELSE
1223 : CALL cp_fm_gemm("N", "T", nao, nao, ncol, my_alpha, matrix_v, matrix_g, &
1224 : 1.0_dp, fm_matrix)
1225 : END IF
1226 : ELSE
1227 : CALL cp_fm_gemm("N", "T", nao, nao, ncol, my_alpha, matrix_v, matrix_v, &
1228 : 1.0_dp, fm_matrix)
1229 : END IF
1230 :
1231 : CALL dbcsr_copy(sparse_matrix2, sparse_matrix)
1232 : CALL dbcsr_scale(sparse_matrix2, alpha_scalar=0.0_dp)
1233 : CALL copy_fm_to_dbcsr(fm_matrix, sparse_matrix2, keep_sparsity=my_keep_sparsity)
1234 : CALL dbcsr_add(sparse_matrix2, sparse_matrix, alpha_scalar=1.0_dp, &
1235 : beta_scalar=-1.0_dp)
1236 : norm = dbcsr_frobenius_norm(sparse_matrix2)
1237 : WRITE (*, *) 'nao=', nao, ' k=', k, ' ncol=', ncol, ' my_alpha=', my_alpha
1238 : WRITE (*, *) 'PRESENT (matrix_g)', PRESENT(matrix_g)
1239 : WRITE (*, *) 'matrix_type=', dbcsr_get_matrix_type(sparse_matrix)
1240 : WRITE (*, *) 'norm(sm+alpha*v*g^t - fm+alpha*v*g^t)/n=', norm/REAL(nao, dp)
1241 : CALL dbcsr_release(sparse_matrix2)
1242 : CALL dbcsr_release(sparse_matrix3)
1243 : CALL cp_fm_release(fm_matrix)
1244 : END IF
1245 266630 : CALL dbcsr_release(mat_v)
1246 266630 : IF (PRESENT(matrix_g)) CALL dbcsr_release(mat_g)
1247 : END IF
1248 268618 : CALL timestop(timing_handle)
1249 :
1250 268618 : END SUBROUTINE cp_dbcsr_plus_fm_fm_t
1251 :
1252 : ! **************************************************************************************************
1253 : !> \brief Utility function to copy a specially shaped fm to dbcsr_matrix
1254 : !> The result matrix will be the matrix in dbcsr format
1255 : !> with the row blocks sizes according to the block_sizes of the template
1256 : !> and the col blocks sizes evenly blocked with the internal dbcsr conversion
1257 : !> size (32 is the current default)
1258 : !> \param matrix ...
1259 : !> \param fm_in ...
1260 : !> \param template ...
1261 : ! **************************************************************************************************
1262 17277 : SUBROUTINE cp_fm_to_dbcsr_row_template(matrix, fm_in, template)
1263 : TYPE(dbcsr_type), INTENT(INOUT) :: matrix
1264 : TYPE(cp_fm_type), INTENT(IN) :: fm_in
1265 : TYPE(dbcsr_type), INTENT(IN) :: template
1266 :
1267 : INTEGER :: k_in
1268 5759 : INTEGER, CONTIGUOUS, DIMENSION(:), POINTER :: col_blk_size_right_in, row_blk_size
1269 : TYPE(dbcsr_distribution_type) :: dist_right_in, tmpl_dist
1270 :
1271 5759 : CALL cp_fm_get_info(fm_in, ncol_global=k_in)
1272 :
1273 5759 : CALL dbcsr_get_info(template, distribution=tmpl_dist)
1274 5759 : CALL dbcsr_create_dist_r_unrot(dist_right_in, tmpl_dist, k_in, col_blk_size_right_in)
1275 5759 : CALL dbcsr_get_info(template, row_blk_size=row_blk_size)
1276 : CALL dbcsr_create(matrix, "D", dist_right_in, dbcsr_type_no_symmetry, &
1277 5759 : row_blk_size, col_blk_size_right_in)
1278 :
1279 5759 : CALL copy_fm_to_dbcsr(fm_in, matrix)
1280 5759 : DEALLOCATE (col_blk_size_right_in)
1281 5759 : CALL dbcsr_distribution_release(dist_right_in)
1282 :
1283 5759 : END SUBROUTINE cp_fm_to_dbcsr_row_template
1284 :
1285 : ! **************************************************************************************************
1286 : !> \brief Utility function to create an arbitrary shaped dbcsr matrix
1287 : !> with the same processor grid as the template matrix
1288 : !> both row sizes and col sizes are evenly blocked with the internal
1289 : !> dbcsr_conversion size (32 is the current default)
1290 : !> \param matrix dbcsr matrix to be created
1291 : !> \param template template dbcsr matrix giving its mp_env
1292 : !> \param m global row size of output matrix
1293 : !> \param n global col size of output matrix
1294 : !> \param sym ...
1295 : ! **************************************************************************************************
1296 356392 : SUBROUTINE cp_dbcsr_m_by_n_from_template(matrix, template, m, n, sym)
1297 : TYPE(dbcsr_type), INTENT(INOUT) :: matrix, template
1298 : INTEGER, INTENT(IN) :: m, n
1299 : CHARACTER, INTENT(IN), OPTIONAL :: sym
1300 :
1301 : CHARACTER :: mysym
1302 : INTEGER :: npcols, nprows
1303 178196 : INTEGER, CONTIGUOUS, DIMENSION(:), POINTER :: col_blk_size, col_dist, row_blk_size, &
1304 178196 : row_dist
1305 : TYPE(dbcsr_distribution_type) :: dist_m_n, tmpl_dist
1306 :
1307 178196 : CALL dbcsr_get_info(template, matrix_type=mysym, distribution=tmpl_dist)
1308 :
1309 178196 : IF (PRESENT(sym)) mysym = sym
1310 :
1311 178196 : NULLIFY (row_dist, col_dist)
1312 178196 : NULLIFY (row_blk_size, col_blk_size)
1313 : !NULLIFY (row_cluster, col_cluster)
1314 :
1315 178196 : CALL dbcsr_distribution_get(tmpl_dist, nprows=nprows, npcols=npcols)
1316 178196 : CALL create_bl_distribution(row_dist, row_blk_size, m, nprows)
1317 178196 : CALL create_bl_distribution(col_dist, col_blk_size, n, npcols)
1318 : CALL dbcsr_distribution_new(dist_m_n, template=tmpl_dist, &
1319 : row_dist=row_dist, col_dist=col_dist, &
1320 : !row_cluster=row_cluster, col_cluster=col_cluster, &
1321 178196 : reuse_arrays=.TRUE.)
1322 :
1323 : CALL dbcsr_create(matrix, "m_n_template", dist_m_n, mysym, &
1324 178196 : row_blk_size, col_blk_size, reuse_arrays=.TRUE.)
1325 178196 : CALL dbcsr_distribution_release(dist_m_n)
1326 :
1327 178196 : END SUBROUTINE cp_dbcsr_m_by_n_from_template
1328 :
1329 : ! **************************************************************************************************
1330 : !> \brief Utility function to create dbcsr matrix, m x n matrix (n arbitrary)
1331 : !> with the same processor grid and row distribution as the template matrix
1332 : !> col sizes are evenly blocked with the internal
1333 : !> dbcsr_conversion size (32 is the current default)
1334 : !> \param matrix dbcsr matrix to be created
1335 : !> \param template template dbcsr matrix giving its mp_env
1336 : !> \param n global col size of output matrix
1337 : !> \param sym ...
1338 : ! **************************************************************************************************
1339 708000 : SUBROUTINE cp_dbcsr_m_by_n_from_row_template(matrix, template, n, sym)
1340 : TYPE(dbcsr_type), INTENT(INOUT) :: matrix, template
1341 : INTEGER :: n
1342 : CHARACTER, OPTIONAL :: sym
1343 :
1344 : CHARACTER :: mysym
1345 : INTEGER :: npcols
1346 177000 : INTEGER, CONTIGUOUS, DIMENSION(:), POINTER :: col_blk_size, col_dist, row_blk_size, &
1347 177000 : row_dist
1348 : TYPE(dbcsr_distribution_type) :: dist_m_n, tmpl_dist
1349 :
1350 354000 : mysym = dbcsr_get_matrix_type(template)
1351 177000 : IF (PRESENT(sym)) mysym = sym
1352 :
1353 177000 : CALL dbcsr_get_info(template, distribution=tmpl_dist)
1354 : CALL dbcsr_distribution_get(tmpl_dist, &
1355 : npcols=npcols, &
1356 177000 : row_dist=row_dist)
1357 :
1358 177000 : NULLIFY (col_dist, col_blk_size)
1359 177000 : CALL create_bl_distribution(col_dist, col_blk_size, n, npcols)
1360 : CALL dbcsr_distribution_new(dist_m_n, template=tmpl_dist, &
1361 177000 : row_dist=row_dist, col_dist=col_dist)
1362 :
1363 177000 : CALL dbcsr_get_info(template, row_blk_size=row_blk_size)
1364 177000 : CALL dbcsr_create(matrix, "m_n_template", dist_m_n, mysym, row_blk_size, col_blk_size)
1365 :
1366 177000 : DEALLOCATE (col_dist, col_blk_size)
1367 177000 : CALL dbcsr_distribution_release(dist_m_n)
1368 :
1369 177000 : END SUBROUTINE cp_dbcsr_m_by_n_from_row_template
1370 :
1371 : ! **************************************************************************************************
1372 : !> \brief Distributes elements into blocks and into bins
1373 : !>
1374 : !> \param[out] block_distribution block distribution to bins
1375 : !> \param[out] block_size sizes of blocks
1376 : !> \param[in] nelements number of elements to bin
1377 : !> \param[in] nbins number of bins
1378 : !> \par Term clarification
1379 : !> An example: blocks are atom blocks and bins are process rows/columns.
1380 : ! **************************************************************************************************
1381 1337516 : SUBROUTINE create_bl_distribution(block_distribution, &
1382 : block_size, nelements, nbins)
1383 : INTEGER, DIMENSION(:), INTENT(OUT), POINTER :: block_distribution, block_size
1384 : INTEGER, INTENT(IN) :: nelements, nbins
1385 :
1386 : CHARACTER(len=*), PARAMETER :: routineN = 'create_bl_distribution', &
1387 : routineP = moduleN//':'//routineN
1388 :
1389 : INTEGER :: bin, blk_layer, element_stack, els, &
1390 : estimated_blocks, max_blocks_per_bin, &
1391 : nblks, nblocks, stat
1392 1337516 : INTEGER, DIMENSION(:), POINTER :: blk_dist, blk_sizes
1393 :
1394 : ! ---------------------------------------------------------------------------
1395 :
1396 1337516 : NULLIFY (block_distribution)
1397 1337516 : NULLIFY (block_size)
1398 : ! Define the sizes on which we build the distribution.
1399 1337516 : IF (nelements > 0) THEN
1400 :
1401 1321994 : nblocks = CEILING(REAL(nelements, KIND=dp)/REAL(max_elements_per_block, KIND=dp))
1402 1321994 : max_blocks_per_bin = CEILING(REAL(nblocks, KIND=dp)/REAL(nbins, KIND=dp))
1403 :
1404 : IF (debug_mod) THEN
1405 : WRITE (*, '(1X,A,1X,A,I7,A,I7,A)') routineP, "For", nelements, &
1406 : " elements and", nbins, " bins"
1407 : WRITE (*, '(1X,A,1X,A,I7,A)') routineP, "There are", &
1408 : max_elements_per_block, " max elements per block"
1409 : WRITE (*, '(1X,A,1X,A,I7,A)') routineP, "There are", &
1410 : nblocks, " blocks"
1411 : WRITE (*, '(1X,A,1X,A,I7,A)') routineP, "There are", &
1412 : max_blocks_per_bin, " max blocks/bin"
1413 : END IF
1414 :
1415 1321994 : estimated_blocks = max_blocks_per_bin*nbins
1416 3965982 : ALLOCATE (blk_dist(estimated_blocks), stat=stat)
1417 1321994 : IF (stat /= 0) THEN
1418 0 : CPABORT("blk_dist")
1419 : END IF
1420 2643988 : ALLOCATE (blk_sizes(estimated_blocks), stat=stat)
1421 : IF (stat /= 0) THEN
1422 0 : CPABORT("blk_sizes")
1423 : END IF
1424 1321994 : element_stack = 0
1425 1321994 : nblks = 0
1426 2722532 : DO blk_layer = 1, max_blocks_per_bin
1427 4271242 : DO bin = 0, nbins - 1
1428 1548710 : els = MIN(max_elements_per_block, nelements - element_stack)
1429 2949248 : IF (els > 0) THEN
1430 1411462 : element_stack = element_stack + els
1431 1411462 : nblks = nblks + 1
1432 1411462 : blk_dist(nblks) = bin
1433 1411462 : blk_sizes(nblks) = els
1434 : IF (debug_mod) WRITE (*, '(1X,A,I5,A,I5,A,I5)') routineP//" Assigning", &
1435 : els, " elements as block", nblks, " to bin", bin
1436 : END IF
1437 : END DO
1438 : END DO
1439 : ! Create the output arrays.
1440 1321994 : IF (nblks == estimated_blocks) THEN
1441 1184746 : block_distribution => blk_dist
1442 1184746 : block_size => blk_sizes
1443 : ELSE
1444 411744 : ALLOCATE (block_distribution(nblks), stat=stat)
1445 : IF (stat /= 0) THEN
1446 0 : CPABORT("blk_dist")
1447 : END IF
1448 552544 : block_distribution(:) = blk_dist(1:nblks)
1449 137248 : DEALLOCATE (blk_dist)
1450 274496 : ALLOCATE (block_size(nblks), stat=stat)
1451 : IF (stat /= 0) THEN
1452 0 : CPABORT("blk_sizes")
1453 : END IF
1454 552544 : block_size(:) = blk_sizes(1:nblks)
1455 137248 : DEALLOCATE (blk_sizes)
1456 : END IF
1457 : ELSE
1458 15522 : ALLOCATE (block_distribution(0), stat=stat)
1459 : IF (stat /= 0) THEN
1460 0 : CPABORT("blk_dist")
1461 : END IF
1462 15522 : ALLOCATE (block_size(0), stat=stat)
1463 : IF (stat /= 0) THEN
1464 0 : CPABORT("blk_sizes")
1465 : END IF
1466 : END IF
1467 : 1579 FORMAT(I5, 1X, I5, 1X, I5, 1X, I5, 1X, I5, 1X, I5, 1X, I5, 1X, I5, 1X, I5, 1X, I5)
1468 : IF (debug_mod) THEN
1469 : WRITE (*, '(1X,A,A)') routineP//" Distribution"
1470 : WRITE (*, 1579) block_distribution(:)
1471 : WRITE (*, '(1X,A,A)') routineP//" Sizes"
1472 : WRITE (*, 1579) block_size(:)
1473 : END IF
1474 1337516 : END SUBROUTINE create_bl_distribution
1475 :
1476 : ! **************************************************************************************************
1477 : !> \brief Creates a new distribution for the right matrix in a matrix
1478 : !> multiplication with unrotated grid.
1479 : !> \param[out] dist_right new distribution for the right matrix
1480 : !> \param[in] dist_left the distribution of the left matrix
1481 : !> \param[in] ncolumns number of columns in right matrix
1482 : !> \param[out] right_col_blk_sizes sizes of blocks in the created column
1483 : !> \par The new row distribution for the right matrix is the same as the row
1484 : !> distribution of the left matrix, while the column distribution is
1485 : !> created so that it is appropriate to the parallel environment.
1486 : ! **************************************************************************************************
1487 537494 : SUBROUTINE dbcsr_create_dist_r_unrot(dist_right, dist_left, ncolumns, &
1488 : right_col_blk_sizes)
1489 : TYPE(dbcsr_distribution_type), INTENT(OUT) :: dist_right
1490 : TYPE(dbcsr_distribution_type), INTENT(IN) :: dist_left
1491 : INTEGER, INTENT(IN) :: ncolumns
1492 : INTEGER, DIMENSION(:), INTENT(OUT), POINTER :: right_col_blk_sizes
1493 :
1494 : INTEGER :: multiplicity, ncols, nimages, npcols, &
1495 : nprows
1496 : INTEGER, ALLOCATABLE, DIMENSION(:) :: tmp_images
1497 537494 : INTEGER, CONTIGUOUS, DIMENSION(:), POINTER :: right_col_dist, right_row_dist
1498 537494 : INTEGER, DIMENSION(:), POINTER :: old_col_dist
1499 :
1500 : CALL dbcsr_distribution_get(dist_left, &
1501 : ncols=ncols, &
1502 : col_dist=old_col_dist, &
1503 : nprows=nprows, &
1504 537494 : npcols=npcols)
1505 :
1506 : ! Create the column distribution
1507 537494 : CALL create_bl_distribution(right_col_dist, right_col_blk_sizes, ncolumns, npcols)
1508 : ! Create an even row distribution.
1509 2149976 : ALLOCATE (right_row_dist(ncols), tmp_images(ncols))
1510 537494 : nimages = lcm(nprows, npcols)/nprows
1511 537494 : multiplicity = nprows/gcd(nprows, npcols)
1512 537494 : CALL rebin_distribution(right_row_dist, tmp_images, old_col_dist, nprows, multiplicity, nimages)
1513 :
1514 : CALL dbcsr_distribution_new(dist_right, &
1515 : template=dist_left, &
1516 : row_dist=right_row_dist, &
1517 : col_dist=right_col_dist, &
1518 : !row_cluster=dummy,&
1519 : !col_cluster=dummy,&
1520 537494 : reuse_arrays=.TRUE.)
1521 537494 : DEALLOCATE (tmp_images)
1522 537494 : END SUBROUTINE dbcsr_create_dist_r_unrot
1523 :
1524 : ! **************************************************************************************************
1525 : !> \brief Makes new distribution with decimation and multiplicity
1526 : !> \param[out] new_bins new real distribution
1527 : !> \param[out] images new image distribution
1528 : !> \param[in] source_bins Basis for the new distribution and images
1529 : !> \param[in] nbins number of bins in the new real distribution
1530 : !> \param[in] multiplicity multiplicity
1531 : !> \param[in] nimages number of images in the new distribution
1532 : !> \par Definition of multiplicity and nimages
1533 : !> Multiplicity and decimation (number of images) are used to
1534 : !> match process grid coordinates on non-square process
1535 : !> grids. Given source_nbins and target_nbins, their relation is
1536 : !> source_nbins * target_multiplicity
1537 : !> = target_nbins * target_nimages.
1538 : !> It is best when both multiplicity and nimages are small. To
1539 : !> get these two factors, then, one can use the following formulas:
1540 : !> nimages = lcm(source_nbins, target_nbins) / target_nbins
1541 : !> multiplicity = target_nbins / gcd(source_nbins, target_nbins)
1542 : !> from the target's point of view (nimages = target_nimages).
1543 : !> \par Mapping
1544 : !> The new distribution comprises of real bins and images within
1545 : !> bins. These can be view as target_nbins*nimages virtual
1546 : !> columns. These same virtual columns are also
1547 : !> source_nbins*multiplicity in number. Therefore these virtual
1548 : !> columns are mapped from source_nbins*multiplicity onto
1549 : !> target_bins*nimages (each target bin has nimages images):
1550 : !> Source 4: |1 2 3|4 5 6|7 8 9|A B C| (4*3)
1551 : !> Target 6: |1 2|3 4|5 6|7 8|9 A|B C| (6*2)
1552 : !> multiplicity=3, nimages=2, 12 virtual columns (1-C).
1553 : !> Source bin elements are evenly mapped into one of multiplicity
1554 : !> virtual columns. Other (non-even, block-size aware) mappings
1555 : !> could be better.
1556 : ! **************************************************************************************************
1557 537494 : SUBROUTINE rebin_distribution(new_bins, images, source_bins, &
1558 : nbins, multiplicity, nimages)
1559 : INTEGER, DIMENSION(:), INTENT(OUT) :: new_bins, images
1560 : INTEGER, DIMENSION(:), INTENT(IN) :: source_bins
1561 : INTEGER, INTENT(IN) :: nbins, multiplicity, nimages
1562 :
1563 : INTEGER :: bin, i, old_nbins, virtual_bin
1564 537494 : INTEGER, ALLOCATABLE, DIMENSION(:) :: bin_multiplier
1565 :
1566 : ! ---------------------------------------------------------------------------
1567 :
1568 537494 : IF (MOD(nbins*nimages, multiplicity) /= 0) THEN
1569 0 : CPWARN("mulitplicity is not divisor of new process grid coordinate")
1570 : END IF
1571 537494 : old_nbins = (nbins*nimages)/multiplicity
1572 1612482 : ALLOCATE (bin_multiplier(0:old_nbins - 1))
1573 537494 : bin_multiplier(:) = 0
1574 2544835 : DO i = 1, SIZE(new_bins)
1575 2007341 : IF (i <= SIZE(source_bins)) THEN
1576 2007341 : bin = source_bins(i)
1577 : ELSE
1578 : ! Fill remainder with a cyclic distribution
1579 0 : bin = MOD(i, old_nbins)
1580 : END IF
1581 2007341 : virtual_bin = bin*multiplicity + bin_multiplier(bin)
1582 2007341 : new_bins(i) = virtual_bin/nimages
1583 2007341 : images(i) = 1 + MOD(virtual_bin, nimages)
1584 2007341 : bin_multiplier(bin) = bin_multiplier(bin) + 1
1585 2544835 : IF (bin_multiplier(bin) >= multiplicity) THEN
1586 858167 : bin_multiplier(bin) = 0
1587 : END IF
1588 : END DO
1589 537494 : END SUBROUTINE rebin_distribution
1590 :
1591 : ! **************************************************************************************************
1592 : !> \brief Creates a block-cyclic compatible distribution
1593 : !>
1594 : !> All blocks in a dimension, except for possibly the last
1595 : !> block, have the same size.
1596 : !> \param[out] dist the elemental distribution
1597 : !> \param[in] nrows number of full rows
1598 : !> \param[in] ncolumns number of full columns
1599 : !> \param[in] nrow_block size of row blocks
1600 : !> \param[in] ncol_block size of column blocks
1601 : !> \param group_handle ...
1602 : !> \param pgrid ...
1603 : !> \param[out] row_blk_sizes row block sizes
1604 : !> \param[out] col_blk_sizes column block sizes
1605 : ! **************************************************************************************************
1606 2193151 : SUBROUTINE dbcsr_create_dist_block_cyclic(dist, nrows, ncolumns, &
1607 : nrow_block, ncol_block, group_handle, pgrid, row_blk_sizes, col_blk_sizes)
1608 : TYPE(dbcsr_distribution_type), INTENT(OUT) :: dist
1609 : INTEGER, INTENT(IN) :: nrows, ncolumns, nrow_block, ncol_block, &
1610 : group_handle
1611 : INTEGER, DIMENSION(:, :), POINTER :: pgrid
1612 : INTEGER, DIMENSION(:), INTENT(OUT), POINTER :: row_blk_sizes, col_blk_sizes
1613 :
1614 : CHARACTER(len=*), PARAMETER :: routineN = 'dbcsr_create_dist_block_cyclic'
1615 :
1616 : INTEGER :: nblkcols, nblkrows, npcols, nprows, &
1617 : pdim, sz
1618 2193151 : INTEGER, CONTIGUOUS, DIMENSION(:), POINTER :: cd_data, rd_data
1619 :
1620 : ! Row sizes
1621 2193151 : IF (nrow_block == 0) THEN
1622 : nblkrows = 0
1623 : sz = 0
1624 : ELSE
1625 2193151 : nblkrows = nrows/nrow_block
1626 2193151 : sz = MOD(nrows, nrow_block)
1627 : END IF
1628 2193151 : IF (sz > 0) nblkrows = nblkrows + 1
1629 8770108 : ALLOCATE (row_blk_sizes(nblkrows), rd_data(nblkrows))
1630 8603624 : row_blk_sizes = nrow_block
1631 2193151 : IF (sz /= 0) row_blk_sizes(nblkrows) = sz
1632 :
1633 : ! Column sizes
1634 2193151 : IF (ncol_block == 0) THEN
1635 : nblkcols = 0
1636 : sz = 0
1637 : ELSE
1638 2193151 : nblkcols = ncolumns/ncol_block
1639 2193151 : sz = MOD(ncolumns, ncol_block)
1640 : END IF
1641 2193151 : IF (sz > 0) nblkcols = nblkcols + 1
1642 8765348 : ALLOCATE (col_blk_sizes(nblkcols), cd_data(nblkcols))
1643 6263370 : col_blk_sizes = ncol_block
1644 2193151 : IF (sz /= 0) col_blk_sizes(nblkcols) = sz
1645 : !
1646 : IF (debug_mod) THEN
1647 : WRITE (*, *) routineN//" nrows,nrow_block,nblkrows=", &
1648 : nrows, nrow_block, nblkrows
1649 : WRITE (*, *) routineN//" ncols,ncol_block,nblkcols=", &
1650 : ncolumns, ncol_block, nblkcols
1651 : END IF
1652 : ! Calculate process row distribution
1653 2193151 : nprows = SIZE(pgrid, 1)
1654 6493464 : DO pdim = 0, MIN(nprows - 1, nblkrows - 1)
1655 12903937 : rd_data(1 + pdim:nblkrows:nprows) = pdim
1656 : END DO
1657 : ! Calculate process column distribution
1658 2193151 : npcols = SIZE(pgrid, 2)
1659 4382674 : DO pdim = 0, MIN(npcols - 1, nblkcols - 1)
1660 8452893 : cd_data(1 + pdim:nblkcols:npcols) = pdim
1661 : END DO
1662 : !
1663 : IF (debug_mod) THEN
1664 : WRITE (*, *) routineN//" row_dist", &
1665 : rd_data
1666 : WRITE (*, *) routineN//" col_dist", &
1667 : cd_data
1668 : END IF
1669 : !
1670 : CALL dbcsr_distribution_new(dist, &
1671 : group=group_handle, pgrid=pgrid, &
1672 : row_dist=rd_data, &
1673 : col_dist=cd_data, &
1674 2193151 : reuse_arrays=.TRUE.)
1675 :
1676 2193151 : END SUBROUTINE dbcsr_create_dist_block_cyclic
1677 :
1678 : ! **************************************************************************************************
1679 : !> \brief Allocate and initialize a real matrix 1-dimensional set.
1680 : !> \param[in,out] matrix_set Set containing the DBCSR matrices
1681 : !> \param[in] nmatrix Size of set
1682 : !> \par History
1683 : !> 2009-08-17 Adapted from sparse_matrix_type for DBCSR
1684 : ! **************************************************************************************************
1685 188467 : SUBROUTINE allocate_dbcsr_matrix_set_1d(matrix_set, nmatrix)
1686 : TYPE(dbcsr_p_type), DIMENSION(:), POINTER :: matrix_set
1687 : INTEGER, INTENT(IN) :: nmatrix
1688 :
1689 : INTEGER :: imatrix
1690 :
1691 188467 : IF (ASSOCIATED(matrix_set)) CALL dbcsr_deallocate_matrix_set(matrix_set)
1692 1335688 : ALLOCATE (matrix_set(nmatrix))
1693 958754 : DO imatrix = 1, nmatrix
1694 958754 : NULLIFY (matrix_set(imatrix)%matrix)
1695 : END DO
1696 188467 : END SUBROUTINE allocate_dbcsr_matrix_set_1d
1697 :
1698 : ! **************************************************************************************************
1699 : !> \brief Allocate and initialize a real matrix 2-dimensional set.
1700 : !> \param[in,out] matrix_set Set containing the DBCSR matrix pointer type
1701 : !> \param[in] nmatrix Size of set
1702 : !> \param mmatrix ...
1703 : !> \par History
1704 : !> 2009-08-17 Adapted from sparse_matrix_type for DBCSR
1705 : ! **************************************************************************************************
1706 220248 : SUBROUTINE allocate_dbcsr_matrix_set_2d(matrix_set, nmatrix, mmatrix)
1707 : TYPE(dbcsr_p_type), DIMENSION(:, :), POINTER :: matrix_set
1708 : INTEGER, INTENT(IN) :: nmatrix, mmatrix
1709 :
1710 : INTEGER :: imatrix, jmatrix
1711 :
1712 220248 : IF (ASSOCIATED(matrix_set)) CALL dbcsr_deallocate_matrix_set(matrix_set)
1713 5853790 : ALLOCATE (matrix_set(nmatrix, mmatrix))
1714 2554288 : DO jmatrix = 1, mmatrix
1715 5193046 : DO imatrix = 1, nmatrix
1716 4972798 : NULLIFY (matrix_set(imatrix, jmatrix)%matrix)
1717 : END DO
1718 : END DO
1719 220248 : END SUBROUTINE allocate_dbcsr_matrix_set_2d
1720 :
1721 : ! **************************************************************************************************
1722 : !> \brief Allocate and initialize a real matrix 3-dimensional set.
1723 : !> \param[in,out] matrix_set Set containing the DBCSR matrix pointer type
1724 : !> \param[in] nmatrix Size of set
1725 : !> \param mmatrix ...
1726 : !> \param pmatrix ...
1727 : !> \par History
1728 : !> 2009-08-17 Adapted from sparse_matrix_type for DBCSR
1729 : ! **************************************************************************************************
1730 328 : SUBROUTINE allocate_dbcsr_matrix_set_3d(matrix_set, nmatrix, mmatrix, pmatrix)
1731 : TYPE(dbcsr_p_type), DIMENSION(:, :, :), POINTER :: matrix_set
1732 : INTEGER, INTENT(IN) :: nmatrix, mmatrix, pmatrix
1733 :
1734 : INTEGER :: imatrix, jmatrix, kmatrix
1735 :
1736 328 : IF (ASSOCIATED(matrix_set)) CALL dbcsr_deallocate_matrix_set(matrix_set)
1737 7662 : ALLOCATE (matrix_set(nmatrix, mmatrix, pmatrix))
1738 668 : DO kmatrix = 1, pmatrix
1739 2562 : DO jmatrix = 1, mmatrix
1740 6022 : DO imatrix = 1, nmatrix
1741 5682 : NULLIFY (matrix_set(imatrix, jmatrix, kmatrix)%matrix)
1742 : END DO
1743 : END DO
1744 : END DO
1745 328 : END SUBROUTINE allocate_dbcsr_matrix_set_3d
1746 :
1747 : ! **************************************************************************************************
1748 : !> \brief Allocate and initialize a real matrix 4-dimensional set.
1749 : !> \param[in,out] matrix_set Set containing the DBCSR matrix pointer type
1750 : !> \param[in] nmatrix Size of set
1751 : !> \param mmatrix ...
1752 : !> \param pmatrix ...
1753 : !> \param qmatrix ...
1754 : !> \par History
1755 : !> 2009-08-17 Adapted from sparse_matrix_type for DBCSR
1756 : ! **************************************************************************************************
1757 0 : SUBROUTINE allocate_dbcsr_matrix_set_4d(matrix_set, nmatrix, mmatrix, pmatrix, qmatrix)
1758 : TYPE(dbcsr_p_type), DIMENSION(:, :, :, :), POINTER :: matrix_set
1759 : INTEGER, INTENT(IN) :: nmatrix, mmatrix, pmatrix, qmatrix
1760 :
1761 : INTEGER :: imatrix, jmatrix, kmatrix, lmatrix
1762 :
1763 0 : IF (ASSOCIATED(matrix_set)) CALL dbcsr_deallocate_matrix_set(matrix_set)
1764 0 : ALLOCATE (matrix_set(nmatrix, mmatrix, pmatrix, qmatrix))
1765 0 : DO lmatrix = 1, qmatrix
1766 0 : DO kmatrix = 1, pmatrix
1767 0 : DO jmatrix = 1, mmatrix
1768 0 : DO imatrix = 1, nmatrix
1769 0 : NULLIFY (matrix_set(imatrix, jmatrix, kmatrix, lmatrix)%matrix)
1770 : END DO
1771 : END DO
1772 : END DO
1773 : END DO
1774 0 : END SUBROUTINE allocate_dbcsr_matrix_set_4d
1775 :
1776 : ! **************************************************************************************************
1777 : !> \brief Allocate and initialize a real matrix 5-dimensional set.
1778 : !> \param[in,out] matrix_set Set containing the DBCSR matrix pointer type
1779 : !> \param[in] nmatrix Size of set
1780 : !> \param mmatrix ...
1781 : !> \param pmatrix ...
1782 : !> \param qmatrix ...
1783 : !> \param smatrix ...
1784 : !> \par History
1785 : !> 2009-08-17 Adapted from sparse_matrix_type for DBCSR
1786 : ! **************************************************************************************************
1787 0 : SUBROUTINE allocate_dbcsr_matrix_set_5d(matrix_set, nmatrix, mmatrix, pmatrix, qmatrix, smatrix)
1788 : TYPE(dbcsr_p_type), DIMENSION(:, :, :, :, :), &
1789 : POINTER :: matrix_set
1790 : INTEGER, INTENT(IN) :: nmatrix, mmatrix, pmatrix, qmatrix, &
1791 : smatrix
1792 :
1793 : INTEGER :: hmatrix, imatrix, jmatrix, kmatrix, &
1794 : lmatrix
1795 :
1796 0 : IF (ASSOCIATED(matrix_set)) CALL dbcsr_deallocate_matrix_set(matrix_set)
1797 0 : ALLOCATE (matrix_set(nmatrix, mmatrix, pmatrix, qmatrix, smatrix))
1798 0 : DO hmatrix = 1, smatrix
1799 0 : DO lmatrix = 1, qmatrix
1800 0 : DO kmatrix = 1, pmatrix
1801 0 : DO jmatrix = 1, mmatrix
1802 0 : DO imatrix = 1, nmatrix
1803 0 : NULLIFY (matrix_set(imatrix, jmatrix, kmatrix, lmatrix, hmatrix)%matrix)
1804 : END DO
1805 : END DO
1806 : END DO
1807 : END DO
1808 : END DO
1809 0 : END SUBROUTINE allocate_dbcsr_matrix_set_5d
1810 :
1811 : ! **************************************************************************************************
1812 : !> \brief Deallocate a real matrix set and release all of the member matrices.
1813 : !> \param[in,out] matrix_set Set containing the DBCSR matrix pointer type
1814 : !> \par History
1815 : !> 2009-08-17 Adapted from sparse_matrix_type for DBCSR
1816 : ! **************************************************************************************************
1817 188544 : SUBROUTINE deallocate_dbcsr_matrix_set_1d(matrix_set)
1818 :
1819 : TYPE(dbcsr_p_type), DIMENSION(:), POINTER :: matrix_set
1820 :
1821 : INTEGER :: imatrix
1822 :
1823 188544 : IF (ASSOCIATED(matrix_set)) THEN
1824 1329148 : DO imatrix = LBOUND(matrix_set, 1), UBOUND(matrix_set, 1)
1825 957652 : CALL dbcsr_deallocate_matrix(matrix_set(imatrix)%matrix)
1826 : END DO
1827 185748 : DEALLOCATE (matrix_set)
1828 : END IF
1829 :
1830 188544 : END SUBROUTINE deallocate_dbcsr_matrix_set_1d
1831 :
1832 : ! **************************************************************************************************
1833 : !> \brief Deallocate a real matrix set and release all of the member matrices.
1834 : !> \param[in,out] matrix_set Set containing the DBCSR matrix pointer type
1835 : !> \par History
1836 : !> 2009-08-17 Adapted from sparse_matrix_type for DBCSR
1837 : ! **************************************************************************************************
1838 225295 : SUBROUTINE deallocate_dbcsr_matrix_set_2d(matrix_set)
1839 :
1840 : TYPE(dbcsr_p_type), DIMENSION(:, :), POINTER :: matrix_set
1841 :
1842 : INTEGER :: imatrix, jmatrix
1843 :
1844 225295 : IF (ASSOCIATED(matrix_set)) THEN
1845 3011740 : DO jmatrix = LBOUND(matrix_set, 2), UBOUND(matrix_set, 2)
1846 9912405 : DO imatrix = LBOUND(matrix_set, 1), UBOUND(matrix_set, 1)
1847 5000280 : CALL dbcsr_deallocate_matrix(matrix_set(imatrix, jmatrix)%matrix)
1848 : END DO
1849 : END DO
1850 222271 : DEALLOCATE (matrix_set)
1851 : END IF
1852 225295 : END SUBROUTINE deallocate_dbcsr_matrix_set_2d
1853 :
1854 : ! **************************************************************************************************
1855 : !> \brief Deallocate a real matrix set and release all of the member matrices.
1856 : !> \param[in,out] matrix_set Set containing the DBCSR matrix pointer type
1857 : !> \par History
1858 : !> 2009-08-17 Adapted from sparse_matrix_type for DBCSR
1859 : ! **************************************************************************************************
1860 328 : SUBROUTINE deallocate_dbcsr_matrix_set_3d(matrix_set)
1861 :
1862 : TYPE(dbcsr_p_type), DIMENSION(:, :, :), POINTER :: matrix_set
1863 :
1864 : INTEGER :: imatrix, jmatrix, kmatrix
1865 :
1866 328 : IF (ASSOCIATED(matrix_set)) THEN
1867 1324 : DO kmatrix = LBOUND(matrix_set, 3), UBOUND(matrix_set, 3)
1868 3242 : DO jmatrix = LBOUND(matrix_set, 2), UBOUND(matrix_set, 2)
1869 9810 : DO imatrix = LBOUND(matrix_set, 1), UBOUND(matrix_set, 1)
1870 5682 : CALL dbcsr_deallocate_matrix(matrix_set(imatrix, jmatrix, kmatrix)%matrix)
1871 : END DO
1872 : END DO
1873 : END DO
1874 328 : DEALLOCATE (matrix_set)
1875 : END IF
1876 328 : END SUBROUTINE deallocate_dbcsr_matrix_set_3d
1877 :
1878 : ! **************************************************************************************************
1879 : !> \brief Deallocate a real matrix set and release all of the member matrices.
1880 : !> \param[in,out] matrix_set Set containing the DBCSR matrix pointer type
1881 : !> \par History
1882 : !> 2009-08-17 Adapted from sparse_matrix_type for DBCSR
1883 : ! **************************************************************************************************
1884 0 : SUBROUTINE deallocate_dbcsr_matrix_set_4d(matrix_set)
1885 :
1886 : TYPE(dbcsr_p_type), DIMENSION(:, :, :, :), POINTER :: matrix_set
1887 :
1888 : INTEGER :: imatrix, jmatrix, kmatrix, lmatrix
1889 :
1890 0 : IF (ASSOCIATED(matrix_set)) THEN
1891 0 : DO lmatrix = LBOUND(matrix_set, 4), UBOUND(matrix_set, 4)
1892 0 : DO kmatrix = LBOUND(matrix_set, 3), UBOUND(matrix_set, 3)
1893 0 : DO jmatrix = LBOUND(matrix_set, 2), UBOUND(matrix_set, 2)
1894 0 : DO imatrix = LBOUND(matrix_set, 1), UBOUND(matrix_set, 1)
1895 0 : CALL dbcsr_deallocate_matrix(matrix_set(imatrix, jmatrix, kmatrix, lmatrix)%matrix)
1896 : END DO
1897 : END DO
1898 : END DO
1899 : END DO
1900 0 : DEALLOCATE (matrix_set)
1901 : END IF
1902 0 : END SUBROUTINE deallocate_dbcsr_matrix_set_4d
1903 :
1904 : ! **************************************************************************************************
1905 : !> \brief Deallocate a real matrix set and release all of the member matrices.
1906 : !> \param[in,out] matrix_set Set containing the DBCSR matrix pointer type
1907 : !> \par History
1908 : !> 2009-08-17 Adapted from sparse_matrix_type for DBCSR
1909 : ! **************************************************************************************************
1910 0 : SUBROUTINE deallocate_dbcsr_matrix_set_5d(matrix_set)
1911 :
1912 : TYPE(dbcsr_p_type), DIMENSION(:, :, :, :, :), &
1913 : POINTER :: matrix_set
1914 :
1915 : INTEGER :: hmatrix, imatrix, jmatrix, kmatrix, &
1916 : lmatrix
1917 :
1918 0 : IF (ASSOCIATED(matrix_set)) THEN
1919 0 : DO hmatrix = LBOUND(matrix_set, 5), UBOUND(matrix_set, 5)
1920 0 : DO lmatrix = LBOUND(matrix_set, 4), UBOUND(matrix_set, 4)
1921 0 : DO kmatrix = LBOUND(matrix_set, 3), UBOUND(matrix_set, 3)
1922 0 : DO jmatrix = LBOUND(matrix_set, 2), UBOUND(matrix_set, 2)
1923 0 : DO imatrix = LBOUND(matrix_set, 1), UBOUND(matrix_set, 1)
1924 0 : CALL dbcsr_deallocate_matrix(matrix_set(imatrix, jmatrix, kmatrix, lmatrix, hmatrix)%matrix)
1925 : END DO
1926 : END DO
1927 : END DO
1928 : END DO
1929 : END DO
1930 0 : DEALLOCATE (matrix_set)
1931 : END IF
1932 0 : END SUBROUTINE deallocate_dbcsr_matrix_set_5d
1933 :
1934 0 : END MODULE cp_dbcsr_operations
|