(git:4f857eb)
Loading...
Searching...
No Matches
dbt_methods.F
Go to the documentation of this file.
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 DBT tensor framework for block-sparse tensor contraction.
10!> Representation of n-rank tensors as DBT tall-and-skinny matrices.
11!> Support for arbitrary redistribution between different representations.
12!> Support for arbitrary tensor contractions
13!> \todo implement checks and error messages
14!> \author Patrick Seewald
15! **************************************************************************************************
17
18
19 USE cp_dbcsr_api, ONLY: &
23 USE dbt_allocate_wrap, ONLY: &
25 USE dbt_array_list_methods, ONLY: &
28 USE dbm_api, ONLY: &
30 USE dbt_tas_types, ONLY: &
32 USE dbt_tas_base, ONLY: &
34 USE dbt_tas_mm, ONLY: &
37 USE dbt_block, ONLY: &
41 USE dbt_index, ONLY: &
44 USE dbt_types, ONLY: &
53 USE kinds, ONLY: &
55 USE message_passing, ONLY: &
57 USE util, ONLY: &
58 sort
59 USE dbt_reshape_ops, ONLY: &
61 USE dbt_tas_split, ONLY: &
64 USE dbt_split, ONLY: &
66 USE dbt_io, ONLY: &
69
70#include "../base/base_uses.f90"
71
72 IMPLICIT NONE
73 PRIVATE
74 CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'dbt_methods'
75
76 PUBLIC :: &
78 dbt_copy, &
97
98 ! Per contraction (tensor names): remapped and cropped tensors, i.e., data copied
99 ! before the multiplication (local to the rank)
100 TYPE contract_stats_type
101 CHARACTER(LEN=3*default_string_length + 6) :: names = ""
102 INTEGER(KIND=int_8) :: ncalls = 0, nempty = 0, flop = 0
103 INTEGER(KIND=int_8), DIMENSION(3) :: nremap = 0, nzremap = 0
104 INTEGER(KIND=int_8), DIMENSION(2) :: ncrop = 0, nzcrop = 0, ncrop_remap = 0
105 END TYPE contract_stats_type
106
107 TYPE(contract_stats_type), DIMENSION(:), ALLOCATABLE, SAVE :: contract_stats
108 INTEGER, SAVE :: ncontract_stats = 0
109
110CONTAINS
111
112! **************************************************************************************************
113!> \brief Initialize the DBT library (after dbm_library_init).
114!> \author Hans Pabst
115! **************************************************************************************************
116 SUBROUTINE dbt_library_init()
117 END SUBROUTINE dbt_library_init
118
119! **************************************************************************************************
120!> \brief Finalize the DBT library (before dbm_library_finalize), which releases its buffers.
121!> \author Hans Pabst
122! **************************************************************************************************
124 CALL dbt_clear_mempools()
125 IF (ALLOCATED(contract_stats)) DEALLOCATE (contract_stats)
126 ncontract_stats = 0
127 END SUBROUTINE dbt_library_finalize
128
129! **************************************************************************************************
130!> \brief Release the buffers that persist across calls (as dbcsr_clear_mempools), e.g., once a
131!> phase with large tensor operations ends. They are allocated again when needed.
132!> \author Hans Pabst
133! **************************************************************************************************
136 END SUBROUTINE dbt_clear_mempools
137
138! **************************************************************************************************
139!> \brief Print per-contraction statistics (rank-local table, totals over all ranks).
140!> \param mpi_comm communicator of all ranks calling this routine
141!> \param output_unit unit to write to (rank-local table is written if positive)
142!> \author Hans Pabst
143! **************************************************************************************************
144 SUBROUTINE dbt_library_print_stats(mpi_comm, output_unit)
145 TYPE(mp_comm_type), INTENT(IN) :: mpi_comm
146 INTEGER, INTENT(IN) :: output_unit
147
148 INTEGER :: i, j, k
149 INTEGER(KIND=int_8) :: ncalls, nzcopied
150 INTEGER, DIMENSION(:), ALLOCATABLE :: order
151 REAL(kind=dp), DIMENSION(:), ALLOCATABLE :: copied
152
153 ncalls = 0
154 nzcopied = 0
155 DO i = 1, ncontract_stats
156 ncalls = ncalls + contract_stats(i)%ncalls
157 nzcopied = nzcopied + sum(contract_stats(i)%nzremap) + sum(contract_stats(i)%nzcrop)
158 END DO
159 CALL mpi_comm%sum(ncalls)
160 CALL mpi_comm%sum(nzcopied)
161
162 IF (output_unit > 0 .AND. 0 < ncalls) THEN
163 ALLOCATE (order(ncontract_stats), copied(ncontract_stats))
164 DO i = 1, ncontract_stats
165 order(i) = i
166 copied(i) = -real(sum(contract_stats(i)%nzremap) + sum(contract_stats(i)%nzcrop), dp)
167 END DO
168 CALL sort(copied, ncontract_stats, order) ! ascending: most copied data first
169 WRITE (output_unit, '(/,T2,A)') repeat("-", 79)
170 WRITE (output_unit, '(T2,A,T70,A)') "DBT CONTRACTION STATISTICS (rank-local)", "copied [MB]"
171 WRITE (output_unit, '(T2,A,T47,A,T55,A,T62,A,T68,A,T75,A)') &
172 "A x B = C", "calls", "remap", "crop", "both", "total"
173 DO k = 1, ncontract_stats
174 i = order(k)
175 associate(s => contract_stats(i))
176 WRITE (output_unit, '(T2,A,T44,I8,T52,3I3,T61,2I3,T67,2I3,T73,F8.1)') &
177 s%names(1:min(len_trim(s%names), 41)), s%ncalls, &
178 (min(s%nremap(j), 99_int_8), j=1, 3), (min(s%ncrop(j), 99_int_8), j=1, 2), &
179 (min(s%ncrop_remap(j), 99_int_8), j=1, 2), &
180 8.0_dp*real(sum(s%nzremap) + sum(s%nzcrop), dp)/1.0e6_dp
181 END associate
182 END DO
183 WRITE (output_unit, '(T2,A,T44,I8,T73,F8.1)') "all ranks", ncalls, 8.0_dp*real(nzcopied, dp)/1.0e6_dp
184 WRITE (output_unit, '(T2,A)') repeat("-", 79)
185 END IF
186 END SUBROUTINE dbt_library_print_stats
187
188! **************************************************************************************************
189!> \brief Index of the statistics entry for the given tensor names (created if missing).
190!> \author Hans Pabst
191! **************************************************************************************************
192 FUNCTION contract_stats_index(name_1, name_2, name_3) RESULT(idx)
193 CHARACTER(LEN=*), INTENT(IN) :: name_1, name_2, name_3
194 INTEGER :: idx
195
196 CHARACTER(LEN=3*default_string_length + 6) :: names
197 INTEGER :: i
198 TYPE(contract_stats_type), DIMENSION(:), ALLOCATABLE :: grown
199
200 names = trim(name_1)//" x "//trim(name_2)//" = "//trim(name_3)
201 idx = 0
202 DO i = 1, ncontract_stats
203 IF (contract_stats(i)%names == names) idx = i
204 IF (0 < idx) EXIT
205 END DO
206 IF (0 == idx) THEN
207 IF (.NOT. ALLOCATED(contract_stats)) ALLOCATE (contract_stats(16))
208 IF (SIZE(contract_stats) <= ncontract_stats) THEN
209 ALLOCATE (grown(2*SIZE(contract_stats)))
210 grown(1:ncontract_stats) = contract_stats(1:ncontract_stats)
211 CALL move_alloc(grown, contract_stats)
212 END IF
213 ncontract_stats = ncontract_stats + 1
214 idx = ncontract_stats
215 contract_stats(idx)%names = names
216 END IF
217 END FUNCTION contract_stats_index
218
219! **************************************************************************************************
220!> \brief Copy tensor data.
221!> Redistributes tensor data according to distributions of target and source tensor.
222!> Permutes tensor index according to `order` argument (if present).
223!> Source and target tensor formats are arbitrary as long as the following requirements are met:
224!> * source and target tensors have the same rank and the same sizes in each dimension in terms
225!> of tensor elements (block sizes don't need to be the same).
226!> If `order` argument is present, sizes must match after index permutation.
227!> OR
228!> * target tensor is not yet created, in this case an exact copy of source tensor is returned.
229!> \param tensor_in Source
230!> \param tensor_out Target
231!> \param order Permutation of target tensor index.
232!> Exact same convention as order argument of RESHAPE intrinsic.
233!> \param bounds crop tensor data: start and end index for each tensor dimension
234!> \author Patrick Seewald
235! **************************************************************************************************
236 SUBROUTINE dbt_copy(tensor_in, tensor_out, order, summation, bounds, move_data, unit_nr)
237 TYPE(dbt_type), INTENT(INOUT), TARGET :: tensor_in, tensor_out
238 INTEGER, DIMENSION(ndims_tensor(tensor_in)), &
239 INTENT(IN), OPTIONAL :: order
240 LOGICAL, INTENT(IN), OPTIONAL :: summation, move_data
241 INTEGER, DIMENSION(2, ndims_tensor(tensor_in)), &
242 INTENT(IN), OPTIONAL :: bounds
243 INTEGER, INTENT(IN), OPTIONAL :: unit_nr
244 INTEGER :: handle
245
246 CALL timeset("dbt_total", handle)
247
248 ! make sure that it is safe to use dbt_copy during a batched contraction
249 CALL dbt_tas_batched_mm_complete(tensor_in%matrix_rep, warn=.true.)
250 CALL dbt_tas_batched_mm_complete(tensor_out%matrix_rep, warn=.true.)
251
252 CALL dbt_copy_expert(tensor_in, tensor_out, order, summation, bounds, move_data, unit_nr)
253 CALL timestop(handle)
254 END SUBROUTINE dbt_copy
255
256! **************************************************************************************************
257!> \brief expert routine for copying a tensor. For internal use only.
258!> \author Patrick Seewald
259! **************************************************************************************************
260 SUBROUTINE dbt_copy_expert(tensor_in, tensor_out, order, summation, bounds, move_data, unit_nr)
261 TYPE(dbt_type), INTENT(INOUT), TARGET :: tensor_in, tensor_out
262 INTEGER, DIMENSION(ndims_tensor(tensor_in)), &
263 INTENT(IN), OPTIONAL :: order
264 LOGICAL, INTENT(IN), OPTIONAL :: summation, move_data
265 INTEGER, DIMENSION(2, ndims_tensor(tensor_in)), &
266 INTENT(IN), OPTIONAL :: bounds
267 INTEGER, INTENT(IN), OPTIONAL :: unit_nr
268
269 TYPE(dbt_type), POINTER :: in_tmp_1, in_tmp_2, &
270 in_tmp_3, out_tmp_1
271 INTEGER :: handle, unit_nr_prv
272 INTEGER, DIMENSION(:), ALLOCATABLE :: map1_in_1, map1_in_2, map2_in_1, map2_in_2
273
274 CHARACTER(LEN=*), PARAMETER :: routinen = 'dbt_copy'
275 LOGICAL :: dist_compatible_tas, dist_compatible_tensor, &
276 summation_prv, new_in_1, new_in_2, &
277 new_in_3, new_out_1, block_compatible, &
278 move_prv, crop_reshape
279 TYPE(array_list) :: blk_sizes_in
280
281 CALL timeset(routinen, handle)
282
283 cpassert(tensor_out%valid)
284
285 unit_nr_prv = prep_output_unit(unit_nr)
286
287 IF (PRESENT(move_data)) THEN
288 move_prv = move_data
289 ELSE
290 move_prv = .false.
291 END IF
292
293 dist_compatible_tas = .false.
294 dist_compatible_tensor = .false.
295 block_compatible = .false.
296 new_in_1 = .false.
297 new_in_2 = .false.
298 new_in_3 = .false.
299 new_out_1 = .false.
300
301 IF (PRESENT(summation)) THEN
302 summation_prv = summation
303 ELSE
304 summation_prv = .false.
305 END IF
306
307 ! crop while reshaping (single copy) if that is the copy anyway
308 crop_reshape = .false.
309 IF (PRESENT(bounds) .AND. .NOT. PRESENT(order)) crop_reshape = copy_reshapes(tensor_in, tensor_out)
310 IF (PRESENT(bounds) .AND. .NOT. crop_reshape) THEN
311 ALLOCATE (in_tmp_1)
312 CALL dbt_crop(tensor_in, in_tmp_1, bounds=bounds, move_data=move_prv)
313 new_in_1 = .true.
314 move_prv = .true.
315 ELSE
316 in_tmp_1 => tensor_in
317 END IF
318
319 IF (PRESENT(order)) THEN
320 CALL reorder_arrays(in_tmp_1%blk_sizes, blk_sizes_in, order=order)
321 block_compatible = check_equal(blk_sizes_in, tensor_out%blk_sizes)
322 ELSE
323 block_compatible = check_equal(in_tmp_1%blk_sizes, tensor_out%blk_sizes)
324 END IF
325
326 IF (.NOT. block_compatible) THEN
327 ALLOCATE (in_tmp_2, out_tmp_1)
328 CALL dbt_make_compatible_blocks(in_tmp_1, tensor_out, in_tmp_2, out_tmp_1, order=order, &
329 nodata2=.NOT. summation_prv, move_data=move_prv)
330 new_in_2 = .true.; new_out_1 = .true.
331 move_prv = .true.
332 ELSE
333 in_tmp_2 => in_tmp_1
334 out_tmp_1 => tensor_out
335 END IF
336
337 IF (PRESENT(order)) THEN
338 ALLOCATE (in_tmp_3)
339 CALL dbt_permute_index(in_tmp_2, in_tmp_3, order)
340 new_in_3 = .true.
341 ELSE
342 in_tmp_3 => in_tmp_2
343 END IF
344
345 ALLOCATE (map1_in_1(ndims_matrix_row(in_tmp_3)))
346 ALLOCATE (map1_in_2(ndims_matrix_column(in_tmp_3)))
347 CALL dbt_get_mapping_info(in_tmp_3%nd_index, map1_2d=map1_in_1, map2_2d=map1_in_2)
348
349 ALLOCATE (map2_in_1(ndims_matrix_row(out_tmp_1)))
350 ALLOCATE (map2_in_2(ndims_matrix_column(out_tmp_1)))
351 CALL dbt_get_mapping_info(out_tmp_1%nd_index, map1_2d=map2_in_1, map2_2d=map2_in_2)
352
353 IF (.NOT. PRESENT(order)) THEN
354 IF (array_eq_i(map1_in_1, map2_in_1) .AND. array_eq_i(map1_in_2, map2_in_2)) THEN
355 dist_compatible_tas = check_equal(in_tmp_3%nd_dist, out_tmp_1%nd_dist)
356 ELSE IF (array_eq_i([map1_in_1, map1_in_2], [map2_in_1, map2_in_2])) THEN
357 dist_compatible_tensor = check_equal(in_tmp_3%nd_dist, out_tmp_1%nd_dist)
358 END IF
359 END IF
360
361 IF (dist_compatible_tas) THEN
362 CALL dbt_tas_copy(out_tmp_1%matrix_rep, in_tmp_3%matrix_rep, summation)
363 IF (move_prv) CALL dbt_clear(in_tmp_3)
364 ELSE IF (dist_compatible_tensor) THEN
365 CALL dbt_copy_nocomm(in_tmp_3, out_tmp_1, summation)
366 IF (move_prv) CALL dbt_clear(in_tmp_3)
367 ELSE IF (crop_reshape) THEN
368 CALL dbt_reshape(in_tmp_3, out_tmp_1, summation, move_data=move_prv, bounds=bounds)
369 ELSE
370 CALL dbt_reshape(in_tmp_3, out_tmp_1, summation, move_data=move_prv)
371 END IF
372
373 IF (new_in_1) THEN
374 CALL dbt_destroy(in_tmp_1)
375 DEALLOCATE (in_tmp_1)
376 END IF
377
378 IF (new_in_2) THEN
379 CALL dbt_destroy(in_tmp_2)
380 DEALLOCATE (in_tmp_2)
381 END IF
382
383 IF (new_in_3) THEN
384 CALL dbt_destroy(in_tmp_3)
385 DEALLOCATE (in_tmp_3)
386 END IF
387
388 IF (new_out_1) THEN
389 IF (unit_nr_prv /= 0) THEN
390 CALL dbt_write_tensor_dist(out_tmp_1, unit_nr)
391 END IF
392 CALL dbt_split_copyback(out_tmp_1, tensor_out, summation)
393 CALL dbt_destroy(out_tmp_1)
394 DEALLOCATE (out_tmp_1)
395 END IF
396
397 CALL timestop(handle)
398
399 END SUBROUTINE dbt_copy_expert
400
401! **************************************************************************************************
402!> \brief Whether dbt_copy_expert (without order) copies by dbt_reshape: compatible blocks, but
403!> neither the matrix layout nor the distribution allow a copy without communication.
404!> \author Hans Pabst
405! **************************************************************************************************
406 FUNCTION copy_reshapes(tensor_in, tensor_out)
407 TYPE(dbt_type), INTENT(INOUT) :: tensor_in, tensor_out
408 LOGICAL :: copy_reshapes
409
410 INTEGER, DIMENSION(ndims_matrix_row(tensor_in)) :: map1_in_1
411 INTEGER, DIMENSION(ndims_matrix_column(tensor_in)) :: map1_in_2
412 INTEGER, DIMENSION(ndims_matrix_row(tensor_out)) :: map2_in_1
413 INTEGER, DIMENSION(ndims_matrix_column(tensor_out)) :: map2_in_2
414
415 copy_reshapes = check_equal(tensor_in%blk_sizes, tensor_out%blk_sizes)
416 IF (copy_reshapes) THEN
417 CALL dbt_get_mapping_info(tensor_in%nd_index, map1_2d=map1_in_1, map2_2d=map1_in_2)
418 CALL dbt_get_mapping_info(tensor_out%nd_index, map1_2d=map2_in_1, map2_2d=map2_in_2)
419 IF (array_eq_i([map1_in_1, map1_in_2], [map2_in_1, map2_in_2])) THEN
420 copy_reshapes = .NOT. check_equal(tensor_in%nd_dist, tensor_out%nd_dist)
421 END IF
422 END IF
423 END FUNCTION copy_reshapes
424
425! **************************************************************************************************
426!> \brief copy without communication, requires that both tensors have same process grid and distribution
427!> \param summation Whether to sum matrices b = a + b
428!> \author Patrick Seewald
429! **************************************************************************************************
430 SUBROUTINE dbt_copy_nocomm(tensor_in, tensor_out, summation)
431 TYPE(dbt_type), INTENT(INOUT) :: tensor_in
432 TYPE(dbt_type), INTENT(INOUT) :: tensor_out
433 LOGICAL, INTENT(IN), OPTIONAL :: summation
434 TYPE(dbt_iterator_type) :: iter
435 INTEGER, DIMENSION(ndims_tensor(tensor_in)) :: ind_nd
436 TYPE(block_nd) :: blk_data
437 LOGICAL :: found
438
439 CHARACTER(LEN=*), PARAMETER :: routinen = 'dbt_copy_nocomm'
440 INTEGER :: handle
441
442 CALL timeset(routinen, handle)
443 cpassert(tensor_out%valid)
444
445 IF (PRESENT(summation)) THEN
446 IF (.NOT. summation) CALL dbt_clear(tensor_out)
447 ELSE
448 CALL dbt_clear(tensor_out)
449 END IF
450
451 CALL dbt_reserve_blocks(tensor_in, tensor_out)
452
453!$OMP PARALLEL DEFAULT(NONE) SHARED(tensor_in,tensor_out,summation) &
454!$OMP PRIVATE(iter,ind_nd,blk_data,found)
455 CALL dbt_iterator_start(iter, tensor_in)
456 DO WHILE (dbt_iterator_blocks_left(iter))
457 CALL dbt_iterator_next_block(iter, ind_nd)
458 CALL dbt_get_block(tensor_in, ind_nd, blk_data, found)
459 cpassert(found)
460 CALL dbt_put_block(tensor_out, ind_nd, blk_data, summation=summation)
461 CALL destroy_block(blk_data)
462 END DO
463 CALL dbt_iterator_stop(iter)
464!$OMP END PARALLEL
465
466 CALL timestop(handle)
467 END SUBROUTINE dbt_copy_nocomm
468
469! **************************************************************************************************
470!> \brief copy matrix to tensor.
471!> \param summation tensor_out = tensor_out + matrix_in
472!> \author Patrick Seewald
473! **************************************************************************************************
474 SUBROUTINE dbt_copy_matrix_to_tensor(matrix_in, tensor_out, summation)
475 TYPE(dbcsr_type), TARGET, INTENT(IN) :: matrix_in
476 TYPE(dbt_type), INTENT(INOUT) :: tensor_out
477 LOGICAL, INTENT(IN), OPTIONAL :: summation
478 TYPE(dbcsr_type), POINTER :: matrix_in_desym
479
480 INTEGER, DIMENSION(2) :: ind_2d
481 REAL(kind=dp), ALLOCATABLE, DIMENSION(:, :) :: block_arr
482 REAL(kind=dp), DIMENSION(:, :), POINTER :: block
483 TYPE(dbcsr_iterator_type) :: iter
484
485 INTEGER :: handle
486 CHARACTER(LEN=*), PARAMETER :: routinen = 'dbt_copy_matrix_to_tensor'
487
488 CALL timeset(routinen, handle)
489 cpassert(tensor_out%valid)
490
491 NULLIFY (block)
492
493 IF (dbcsr_has_symmetry(matrix_in)) THEN
494 ALLOCATE (matrix_in_desym)
495 CALL dbcsr_desymmetrize(matrix_in, matrix_in_desym)
496 ELSE
497 matrix_in_desym => matrix_in
498 END IF
499
500 IF (PRESENT(summation)) THEN
501 IF (.NOT. summation) CALL dbt_clear(tensor_out)
502 ELSE
503 CALL dbt_clear(tensor_out)
504 END IF
505
506 CALL dbt_reserve_blocks(matrix_in_desym, tensor_out)
507
508!$OMP PARALLEL DEFAULT(NONE) SHARED(matrix_in_desym,tensor_out,summation) &
509!$OMP PRIVATE(iter,ind_2d,block,block_arr)
510 CALL dbcsr_iterator_start(iter, matrix_in_desym)
511 DO WHILE (dbcsr_iterator_blocks_left(iter))
512 CALL dbcsr_iterator_next_block(iter, ind_2d(1), ind_2d(2), block)
513 CALL allocate_any(block_arr, source=block)
514 CALL dbt_put_block(tensor_out, ind_2d, shape(block_arr), block_arr, summation=summation)
515 DEALLOCATE (block_arr)
516 END DO
517 CALL dbcsr_iterator_stop(iter)
518!$OMP END PARALLEL
519
520 IF (dbcsr_has_symmetry(matrix_in)) THEN
521 CALL dbcsr_release(matrix_in_desym)
522 DEALLOCATE (matrix_in_desym)
523 END IF
524
525 CALL timestop(handle)
526
527 END SUBROUTINE dbt_copy_matrix_to_tensor
528
529! **************************************************************************************************
530!> \brief copy tensor to matrix
531!> \param summation matrix_out = matrix_out + tensor_in
532!> \author Patrick Seewald
533! **************************************************************************************************
534 SUBROUTINE dbt_copy_tensor_to_matrix(tensor_in, matrix_out, summation)
535 TYPE(dbt_type), INTENT(INOUT) :: tensor_in
536 TYPE(dbcsr_type), INTENT(INOUT) :: matrix_out
537 LOGICAL, INTENT(IN), OPTIONAL :: summation
538 TYPE(dbt_iterator_type) :: iter
539 INTEGER :: handle
540 INTEGER, DIMENSION(2) :: ind_2d
541 REAL(kind=dp), DIMENSION(:, :), ALLOCATABLE :: block
542 CHARACTER(LEN=*), PARAMETER :: routinen = 'dbt_copy_tensor_to_matrix'
543 LOGICAL :: found
544
545 CALL timeset(routinen, handle)
546
547 IF (PRESENT(summation)) THEN
548 IF (.NOT. summation) CALL dbcsr_clear(matrix_out)
549 ELSE
550 CALL dbcsr_clear(matrix_out)
551 END IF
552
553 CALL dbt_reserve_blocks(tensor_in, matrix_out)
554
555!$OMP PARALLEL DEFAULT(NONE) SHARED(tensor_in,matrix_out,summation) &
556!$OMP PRIVATE(iter,ind_2d,block,found)
557 CALL dbt_iterator_start(iter, tensor_in)
558 DO WHILE (dbt_iterator_blocks_left(iter))
559 CALL dbt_iterator_next_block(iter, ind_2d)
560 IF (dbcsr_has_symmetry(matrix_out) .AND. checker_tr(ind_2d(1), ind_2d(2))) cycle
561
562 CALL dbt_get_block(tensor_in, ind_2d, block, found)
563 cpassert(found)
564
565 IF (dbcsr_has_symmetry(matrix_out) .AND. ind_2d(1) > ind_2d(2)) THEN
566 CALL dbcsr_put_block(matrix_out, ind_2d(2), ind_2d(1), transpose(block), summation=summation)
567 ELSE
568 CALL dbcsr_put_block(matrix_out, ind_2d(1), ind_2d(2), block, summation=summation)
569 END IF
570 DEALLOCATE (block)
571 END DO
572 CALL dbt_iterator_stop(iter)
573!$OMP END PARALLEL
574
575 CALL timestop(handle)
576
577 END SUBROUTINE dbt_copy_tensor_to_matrix
578
579! **************************************************************************************************
580!> \brief Contract tensors by multiplying matrix representations.
581!> tensor_3(map_1, map_2) := alpha * tensor_1(notcontract_1, contract_1)
582!> * tensor_2(contract_2, notcontract_2)
583!> + beta * tensor_3(map_1, map_2)
584!>
585!> \note
586!> note 1: block sizes of the corresponding indices need to be the same in all tensors.
587!>
588!> note 2: for best performance the tensors should have been created in matrix layouts
589!> compatible with the contraction, e.g. tensor_1 should have been created with either
590!> map1_2d == contract_1 and map2_2d == notcontract_1 or map1_2d == notcontract_1 and
591!> map2_2d == contract_1 (the same with tensor_2 and contract_2 / notcontract_2 and with
592!> tensor_3 and map_1 / map_2).
593!> Furthermore the two largest tensors involved in the contraction should map both to either
594!> tall or short matrices: the largest matrix dimension should be "on the same side"
595!> and should have identical distribution (which is always the case if the distributions were
596!> obtained with dbt_default_distvec).
597!>
598!> note 3: if the same tensor occurs in multiple contractions, a different tensor object should
599!> be created for each contraction and the data should be copied between the tensors by use of
600!> dbt_copy. If the same tensor object is used in multiple contractions,
601!> matrix layouts are not compatible for all contractions (see note 2).
602!>
603!> note 4: automatic optimizations are enabled by using the feature of batched contraction, see
604!> dbt_batched_contract_init, dbt_batched_contract_finalize.
605!> The arguments bounds_1, bounds_2, bounds_3 give the index ranges of the batches.
606!>
607!> \param tensor_1 first tensor (in)
608!> \param tensor_2 second tensor (in)
609!> \param contract_1 indices of tensor_1 to contract
610!> \param contract_2 indices of tensor_2 to contract (1:1 with contract_1)
611!> \param map_1 which indices of tensor_3 map to non-contracted indices of tensor_1 (1:1 with notcontract_1)
612!> \param map_2 which indices of tensor_3 map to non-contracted indices of tensor_2 (1:1 with notcontract_2)
613!> \param notcontract_1 indices of tensor_1 not to contract
614!> \param notcontract_2 indices of tensor_2 not to contract
615!> \param tensor_3 contracted tensor (out)
616!> \param bounds_1 bounds corresponding to contract_1 AKA contract_2:
617!> start and end index of an index range over which to contract.
618!> For use in batched contraction.
619!> \param bounds_2 bounds corresponding to notcontract_1: start and end index of an index range.
620!> For use in batched contraction.
621!> \param bounds_3 bounds corresponding to notcontract_2: start and end index of an index range.
622!> For use in batched contraction.
623!> \param optimize_dist Whether distribution should be optimized internally. In the current
624!> implementation this guarantees optimal parameters only for dense matrices.
625!> \param pgrid_opt_1 Optionally return optimal process grid for tensor_1.
626!> This can be used to choose optimal process grids for subsequent tensor
627!> contractions with tensors of similar shape and sparsity. Under some conditions,
628!> pgrid_opt_1 can not be returned, in this case the pointer is not associated.
629!> \param pgrid_opt_2 Optionally return optimal process grid for tensor_2.
630!> \param pgrid_opt_3 Optionally return optimal process grid for tensor_3.
631!> \param filter_eps As in DBM mm
632!> \param flop As in DBM mm
633!> \param move_data memory optimization: transfer data such that tensor_1 and tensor_2 are empty on return
634!> \param retain_sparsity enforce the sparsity pattern of the existing tensor_3; default is no
635!> \param unit_nr output unit for logging
636!> set it to -1 on ranks that should not write (and any valid unit number on
637!> ranks that should write output) if 0 on ALL ranks, no output is written
638!> \param log_verbose verbose logging (for testing only)
639!> \author Patrick Seewald
640! **************************************************************************************************
641 SUBROUTINE dbt_contract(alpha, tensor_1, tensor_2, beta, tensor_3, &
642 contract_1, notcontract_1, &
643 contract_2, notcontract_2, &
644 map_1, map_2, &
645 bounds_1, bounds_2, bounds_3, &
646 optimize_dist, pgrid_opt_1, pgrid_opt_2, pgrid_opt_3, &
647 filter_eps, flop, move_data, retain_sparsity, unit_nr, log_verbose)
648 REAL(dp), INTENT(IN) :: alpha
649 TYPE(dbt_type), INTENT(INOUT), TARGET :: tensor_1
650 TYPE(dbt_type), INTENT(INOUT), TARGET :: tensor_2
651 REAL(dp), INTENT(IN) :: beta
652 INTEGER, DIMENSION(:), INTENT(IN) :: contract_1
653 INTEGER, DIMENSION(:), INTENT(IN) :: contract_2
654 INTEGER, DIMENSION(:), INTENT(IN) :: map_1
655 INTEGER, DIMENSION(:), INTENT(IN) :: map_2
656 INTEGER, DIMENSION(:), INTENT(IN) :: notcontract_1
657 INTEGER, DIMENSION(:), INTENT(IN) :: notcontract_2
658 TYPE(dbt_type), INTENT(INOUT), TARGET :: tensor_3
659 INTEGER, DIMENSION(2, SIZE(contract_1)), &
660 INTENT(IN), OPTIONAL :: bounds_1
661 INTEGER, DIMENSION(2, SIZE(notcontract_1)), &
662 INTENT(IN), OPTIONAL :: bounds_2
663 INTEGER, DIMENSION(2, SIZE(notcontract_2)), &
664 INTENT(IN), OPTIONAL :: bounds_3
665 LOGICAL, INTENT(IN), OPTIONAL :: optimize_dist
666 TYPE(dbt_pgrid_type), INTENT(OUT), &
667 POINTER, OPTIONAL :: pgrid_opt_1
668 TYPE(dbt_pgrid_type), INTENT(OUT), &
669 POINTER, OPTIONAL :: pgrid_opt_2
670 TYPE(dbt_pgrid_type), INTENT(OUT), &
671 POINTER, OPTIONAL :: pgrid_opt_3
672 REAL(kind=dp), INTENT(IN), OPTIONAL :: filter_eps
673 INTEGER(KIND=int_8), INTENT(OUT), OPTIONAL :: flop
674 LOGICAL, INTENT(IN), OPTIONAL :: move_data
675 LOGICAL, INTENT(IN), OPTIONAL :: retain_sparsity
676 INTEGER, OPTIONAL, INTENT(IN) :: unit_nr
677 LOGICAL, INTENT(IN), OPTIONAL :: log_verbose
678
679 INTEGER :: handle
680
681 CALL timeset("dbt_total", handle)
682 CALL dbt_contract_expert(alpha, tensor_1, tensor_2, beta, tensor_3, &
683 contract_1, notcontract_1, &
684 contract_2, notcontract_2, &
685 map_1, map_2, &
686 bounds_1=bounds_1, &
687 bounds_2=bounds_2, &
688 bounds_3=bounds_3, &
689 optimize_dist=optimize_dist, &
690 pgrid_opt_1=pgrid_opt_1, &
691 pgrid_opt_2=pgrid_opt_2, &
692 pgrid_opt_3=pgrid_opt_3, &
693 filter_eps=filter_eps, &
694 flop=flop, &
695 move_data=move_data, &
696 retain_sparsity=retain_sparsity, &
697 unit_nr=unit_nr, &
698 log_verbose=log_verbose)
699 CALL timestop(handle)
700
701 END SUBROUTINE dbt_contract
702
703! **************************************************************************************************
704!> \brief expert routine for tensor contraction. For internal use only.
705!> \param nblks_local number of local blocks on this MPI rank
706!> \author Patrick Seewald
707! **************************************************************************************************
708 SUBROUTINE dbt_contract_expert(alpha, tensor_1, tensor_2, beta, tensor_3, &
709 contract_1, notcontract_1, &
710 contract_2, notcontract_2, &
711 map_1, map_2, &
712 bounds_1, bounds_2, bounds_3, &
713 optimize_dist, pgrid_opt_1, pgrid_opt_2, pgrid_opt_3, &
714 filter_eps, flop, move_data, retain_sparsity, &
715 nblks_local, unit_nr, log_verbose)
716 REAL(dp), INTENT(IN) :: alpha
717 TYPE(dbt_type), INTENT(INOUT), TARGET :: tensor_1
718 TYPE(dbt_type), INTENT(INOUT), TARGET :: tensor_2
719 REAL(dp), INTENT(IN) :: beta
720 INTEGER, DIMENSION(:), INTENT(IN) :: contract_1
721 INTEGER, DIMENSION(:), INTENT(IN) :: contract_2
722 INTEGER, DIMENSION(:), INTENT(IN) :: map_1
723 INTEGER, DIMENSION(:), INTENT(IN) :: map_2
724 INTEGER, DIMENSION(:), INTENT(IN) :: notcontract_1
725 INTEGER, DIMENSION(:), INTENT(IN) :: notcontract_2
726 TYPE(dbt_type), INTENT(INOUT), TARGET :: tensor_3
727 INTEGER, DIMENSION(2, SIZE(contract_1)), &
728 INTENT(IN), OPTIONAL :: bounds_1
729 INTEGER, DIMENSION(2, SIZE(notcontract_1)), &
730 INTENT(IN), OPTIONAL :: bounds_2
731 INTEGER, DIMENSION(2, SIZE(notcontract_2)), &
732 INTENT(IN), OPTIONAL :: bounds_3
733 LOGICAL, INTENT(IN), OPTIONAL :: optimize_dist
734 TYPE(dbt_pgrid_type), INTENT(OUT), &
735 POINTER, OPTIONAL :: pgrid_opt_1
736 TYPE(dbt_pgrid_type), INTENT(OUT), &
737 POINTER, OPTIONAL :: pgrid_opt_2
738 TYPE(dbt_pgrid_type), INTENT(OUT), &
739 POINTER, OPTIONAL :: pgrid_opt_3
740 REAL(kind=dp), INTENT(IN), OPTIONAL :: filter_eps
741 INTEGER(KIND=int_8), INTENT(OUT), OPTIONAL :: flop
742 LOGICAL, INTENT(IN), OPTIONAL :: move_data
743 LOGICAL, INTENT(IN), OPTIONAL :: retain_sparsity
744 INTEGER, INTENT(OUT), OPTIONAL :: nblks_local
745 INTEGER, OPTIONAL, INTENT(IN) :: unit_nr
746 LOGICAL, INTENT(IN), OPTIONAL :: log_verbose
747
748 TYPE(dbt_type), POINTER :: tensor_contr_1, tensor_contr_2, tensor_contr_3
749 TYPE(dbt_type), TARGET :: tensor_algn_1, tensor_algn_2, tensor_algn_3
750 TYPE(dbt_type), POINTER :: tensor_crop_1, tensor_crop_2
751 TYPE(dbt_type), POINTER :: tensor_small, tensor_large
752
753 LOGICAL :: assert_stmt, tensors_remapped
754 INTEGER :: max_mm_dim, max_tensor, &
755 unit_nr_prv, ref_tensor, handle
756 TYPE(mp_cart_type) :: mp_comm_opt
757 INTEGER, DIMENSION(SIZE(contract_1)) :: contract_1_mod
758 INTEGER, DIMENSION(SIZE(notcontract_1)) :: notcontract_1_mod
759 INTEGER, DIMENSION(SIZE(contract_2)) :: contract_2_mod
760 INTEGER, DIMENSION(SIZE(notcontract_2)) :: notcontract_2_mod
761 INTEGER, DIMENSION(SIZE(map_1)) :: map_1_mod
762 INTEGER, DIMENSION(SIZE(map_2)) :: map_2_mod
763 LOGICAL :: trans_1, trans_2, trans_3
764 LOGICAL :: new_1, new_2, new_3, move_data_1, move_data_2
765 INTEGER :: ndims1, ndims2, ndims3
766 INTEGER, DIMENSION(2) :: occ_1, occ_2 ! blocks in bounds, sticking out
767 INTEGER, DIMENSION(:), ALLOCATABLE :: dims1, dims2, dims3
768
769 CHARACTER(LEN=*), PARAMETER :: routinen = 'dbt_contract'
770 CHARACTER(LEN=1), DIMENSION(:), ALLOCATABLE :: indchar1, indchar2, indchar3, indchar1_mod, &
771 indchar2_mod, indchar3_mod
772 CHARACTER(LEN=1), DIMENSION(15), SAVE :: alph = &
773 ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o']
774 INTEGER, DIMENSION(2, ndims_tensor(tensor_1)) :: bounds_t1
775 INTEGER, DIMENSION(2, ndims_tensor(tensor_2)) :: bounds_t2
776 LOGICAL :: do_crop_1, do_crop_2, do_write_3, nodata_3, do_batched, pgrid_changed, &
777 pgrid_changed_any, do_change_pgrid(2)
778 TYPE(dbt_tas_split_info) :: split_opt, split, split_opt_avg
779 INTEGER, DIMENSION(2) :: pdims_2d_opt, pdims_sub, pdims_sub_opt
780 REAL(dp) :: pdim_ratio, pdim_ratio_opt
781 INTEGER :: istat
782 INTEGER(KIND=int_8) :: flop_prv
783 INTEGER, DIMENSION(ndims_tensor(tensor_1)) :: order_1
784 INTEGER, DIMENSION(ndims_tensor(tensor_2)) :: order_2
785 INTEGER, DIMENSION(:, :), ALLOCATABLE :: bounds_a1, bounds_a2 ! unallocated means absent
786 LOGICAL :: cropped_1, cropped_2
787
788 NULLIFY (tensor_contr_1, tensor_contr_2, tensor_contr_3, tensor_crop_1, tensor_crop_2, &
789 tensor_small)
790
791 CALL timeset(routinen, handle)
792
793 cpassert(tensor_1%valid)
794 cpassert(tensor_2%valid)
795 cpassert(tensor_3%valid)
796
797 assert_stmt = SIZE(contract_1) == SIZE(contract_2)
798 cpassert(assert_stmt)
799
800 assert_stmt = SIZE(map_1) == SIZE(notcontract_1)
801 cpassert(assert_stmt)
802
803 assert_stmt = SIZE(map_2) == SIZE(notcontract_2)
804 cpassert(assert_stmt)
805
806 assert_stmt = SIZE(notcontract_1) + SIZE(contract_1) == ndims_tensor(tensor_1)
807 cpassert(assert_stmt)
808
809 assert_stmt = SIZE(notcontract_2) + SIZE(contract_2) == ndims_tensor(tensor_2)
810 cpassert(assert_stmt)
811
812 assert_stmt = SIZE(map_1) + SIZE(map_2) == ndims_tensor(tensor_3)
813 cpassert(assert_stmt)
814
815 unit_nr_prv = prep_output_unit(unit_nr)
816
817 IF (PRESENT(flop)) flop = 0
818 IF (PRESENT(nblks_local)) nblks_local = 0
819
820 IF (PRESENT(move_data)) THEN
821 move_data_1 = move_data
822 move_data_2 = move_data
823 ELSE
824 move_data_1 = .false.
825 move_data_2 = .false.
826 END IF
827
828 nodata_3 = .true.
829 IF (PRESENT(retain_sparsity)) THEN
830 IF (retain_sparsity) nodata_3 = .false.
831 END IF
832
833 CALL dbt_map_bounds_to_tensors(tensor_1, tensor_2, &
834 contract_1, notcontract_1, &
835 contract_2, notcontract_2, &
836 bounds_t1, bounds_t2, &
837 bounds_1=bounds_1, bounds_2=bounds_2, bounds_3=bounds_3, &
838 do_crop_1=do_crop_1, do_crop_2=do_crop_2)
839
840 ! cropping is deferred: a tensor that is remapped is cropped while copied (single copy)
841 tensor_crop_1 => tensor_1
842 tensor_crop_2 => tensor_2
843 cropped_1 = .false.
844 cropped_2 = .false.
845
846 istat = contract_stats_index(tensor_1%name, tensor_2%name, tensor_3%name)
847 associate(s => contract_stats(istat))
848 s%ncalls = s%ncalls + 1
849 END associate
850
851 ! shortcut for empty tensors
852 ! this is needed to avoid unnecessary work in case user contracts different portions of a
853 ! tensor consecutively to save memory
854 associate(mp_comm => tensor_crop_1%pgrid%mp_comm_2d)
855 IF (do_crop_1) THEN
856 occ_1 = num_blocks_in_bounds(tensor_1, bounds_t1)
857 ELSE
858 occ_1 = [dbt_get_num_blocks(tensor_1), 0]
859 END IF
860 CALL mp_comm%max(occ_1)
861 IF (do_crop_2) THEN
862 occ_2 = num_blocks_in_bounds(tensor_2, bounds_t2)
863 ELSE
864 occ_2 = [dbt_get_num_blocks(tensor_2), 0]
865 END IF
866 CALL mp_comm%max(occ_2)
867 END associate
868
869 IF (occ_1(1) == 0 .OR. occ_2(1) == 0) THEN
870 contract_stats(istat)%nempty = contract_stats(istat)%nempty + 1
871 CALL dbt_scale(tensor_3, beta)
872 ! as if cropped with move_data (tensor data was transferred into the crop)
873 IF (do_crop_1 .AND. move_data_1) CALL dbt_clear(tensor_1)
874 IF (do_crop_2 .AND. move_data_2) CALL dbt_clear(tensor_2)
875
876 CALL timestop(handle)
877 RETURN
878 END IF
879
880 ! no block sticks out on any rank: a crop would copy the tensor as is
881 do_crop_1 = do_crop_1 .AND. occ_1(2) > 0
882 do_crop_2 = do_crop_2 .AND. occ_2(2) > 0
883 associate(s => contract_stats(istat))
884 IF (do_crop_1) s%ncrop(1) = s%ncrop(1) + 1
885 IF (do_crop_2) s%ncrop(2) = s%ncrop(2) + 1
886 END associate
887
888 IF (unit_nr_prv /= 0) THEN
889 IF (unit_nr_prv > 0) THEN
890 WRITE (unit_nr_prv, '(A)') repeat("-", 80)
891 WRITE (unit_nr_prv, '(A,1X,A,1X,A,1X,A,1X,A,1X,A)') "DBT TENSOR CONTRACTION:", &
892 trim(tensor_crop_1%name), 'x', trim(tensor_crop_2%name), '=', trim(tensor_3%name)
893 WRITE (unit_nr_prv, '(A)') repeat("-", 80)
894 END IF
895 CALL dbt_write_tensor_info(tensor_crop_1, unit_nr_prv, full_info=log_verbose)
896 CALL dbt_write_tensor_dist(tensor_crop_1, unit_nr_prv)
897 CALL dbt_write_tensor_info(tensor_crop_2, unit_nr_prv, full_info=log_verbose)
898 CALL dbt_write_tensor_dist(tensor_crop_2, unit_nr_prv)
899 END IF
900
901 ! align tensor index with data, tensor data is not modified
902 ndims1 = ndims_tensor(tensor_crop_1)
903 ndims2 = ndims_tensor(tensor_crop_2)
904 ndims3 = ndims_tensor(tensor_3)
905 ALLOCATE (indchar1(ndims1), indchar1_mod(ndims1))
906 ALLOCATE (indchar2(ndims2), indchar2_mod(ndims2))
907 ALLOCATE (indchar3(ndims3), indchar3_mod(ndims3))
908
909 ! labeling tensor index with letters
910
911 indchar1([notcontract_1, contract_1]) = alph(1:ndims1) ! arb. choice
912 indchar2(notcontract_2) = alph(ndims1 + 1:ndims1 + SIZE(notcontract_2)) ! arb. choice
913 indchar2(contract_2) = indchar1(contract_1)
914 indchar3(map_1) = indchar1(notcontract_1)
915 indchar3(map_2) = indchar2(notcontract_2)
916
917 IF (unit_nr_prv /= 0) CALL dbt_print_contraction_index(tensor_crop_1, indchar1, &
918 tensor_crop_2, indchar2, &
919 tensor_3, indchar3, unit_nr_prv)
920 IF (unit_nr_prv > 0) THEN
921 WRITE (unit_nr_prv, '(T2,A)') "aligning tensor index with data"
922 END IF
923
924 CALL align_tensor(tensor_crop_1, contract_1, notcontract_1, &
925 tensor_algn_1, contract_1_mod, notcontract_1_mod, indchar1, indchar1_mod, order_1)
926
927 CALL align_tensor(tensor_crop_2, contract_2, notcontract_2, &
928 tensor_algn_2, contract_2_mod, notcontract_2_mod, indchar2, indchar2_mod, order_2)
929
930 ! bounds in the aligned index order (index i of tensor_X is index order_X(i) of tensor_algn_X)
931 IF (do_crop_1) THEN
932 ALLOCATE (bounds_a1(2, ndims_tensor(tensor_1)))
933 bounds_a1(:, order_1) = bounds_t1(:, :)
934 END IF
935 IF (do_crop_2) THEN
936 ALLOCATE (bounds_a2(2, ndims_tensor(tensor_2)))
937 bounds_a2(:, order_2) = bounds_t2(:, :)
938 END IF
939
940 CALL align_tensor(tensor_3, map_1, map_2, &
941 tensor_algn_3, map_1_mod, map_2_mod, indchar3, indchar3_mod)
942
943 IF (unit_nr_prv /= 0) CALL dbt_print_contraction_index(tensor_algn_1, indchar1_mod, &
944 tensor_algn_2, indchar2_mod, &
945 tensor_algn_3, indchar3_mod, unit_nr_prv)
946
947 ALLOCATE (dims1(ndims1))
948 ALLOCATE (dims2(ndims2))
949 ALLOCATE (dims3(ndims3))
950
951 ! ideally we should consider block sizes and occupancy to measure tensor sizes but current solution should work for most
952 ! cases and is more elegant. Note that we can not easily consider occupancy since it is unknown for result tensor
953 CALL blk_dims_tensor(tensor_crop_1, dims1)
954 CALL blk_dims_tensor(tensor_crop_2, dims2)
955 CALL blk_dims_tensor(tensor_3, dims3)
956
957 max_mm_dim = maxloc([product(int(dims1(notcontract_1), int_8)), &
958 product(int(dims1(contract_1), int_8)), &
959 product(int(dims2(notcontract_2), int_8))], dim=1)
960 max_tensor = maxloc([product(int(dims1, int_8)), product(int(dims2, int_8)), product(int(dims3, int_8))], dim=1)
961 SELECT CASE (max_mm_dim)
962 CASE (1)
963 IF (unit_nr_prv > 0) THEN
964 WRITE (unit_nr_prv, '(T2,A)') "large tensors: 1, 3; small tensor: 2"
965 WRITE (unit_nr_prv, '(T2,A)') "sorting contraction indices"
966 END IF
967 CALL index_linked_sort(contract_1_mod, contract_2_mod)
968 CALL index_linked_sort(map_2_mod, notcontract_2_mod)
969 SELECT CASE (max_tensor)
970 CASE (1)
971 CALL index_linked_sort(notcontract_1_mod, map_1_mod)
972 CASE (3)
973 CALL index_linked_sort(map_1_mod, notcontract_1_mod)
974 CASE DEFAULT
975 cpabort("should not happen")
976 END SELECT
977
978 CALL reshape_mm_compatible(tensor_algn_1, tensor_algn_3, tensor_contr_1, tensor_contr_3, &
979 contract_1_mod, notcontract_1_mod, map_2_mod, map_1_mod, &
980 trans_1, trans_3, new_1, new_3, ref_tensor, nodata2=nodata_3, optimize_dist=optimize_dist, &
981 move_data_1=move_data_1, unit_nr=unit_nr_prv, &
982 bounds1=bounds_a1, crop1=cropped_1)
983
984 CALL reshape_mm_small(tensor_algn_2, contract_2_mod, notcontract_2_mod, tensor_contr_2, trans_2, &
985 new_2, move_data=move_data_2, unit_nr=unit_nr_prv, &
986 bounds=bounds_a2, crop=cropped_2)
987
988 SELECT CASE (ref_tensor)
989 CASE (1)
990 tensor_large => tensor_contr_1
991 CASE (2)
992 tensor_large => tensor_contr_3
993 END SELECT
994 tensor_small => tensor_contr_2
995
996 CASE (2)
997 IF (unit_nr_prv > 0) THEN
998 WRITE (unit_nr_prv, '(T2,A)') "large tensors: 1, 2; small tensor: 3"
999 WRITE (unit_nr_prv, '(T2,A)') "sorting contraction indices"
1000 END IF
1001
1002 CALL index_linked_sort(notcontract_1_mod, map_1_mod)
1003 CALL index_linked_sort(notcontract_2_mod, map_2_mod)
1004 SELECT CASE (max_tensor)
1005 CASE (1)
1006 CALL index_linked_sort(contract_1_mod, contract_2_mod)
1007 CASE (2)
1008 CALL index_linked_sort(contract_2_mod, contract_1_mod)
1009 CASE DEFAULT
1010 cpabort("should not happen")
1011 END SELECT
1012
1013 CALL reshape_mm_compatible(tensor_algn_1, tensor_algn_2, tensor_contr_1, tensor_contr_2, &
1014 notcontract_1_mod, contract_1_mod, notcontract_2_mod, contract_2_mod, &
1015 trans_1, trans_2, new_1, new_2, ref_tensor, optimize_dist=optimize_dist, &
1016 move_data_1=move_data_1, move_data_2=move_data_2, unit_nr=unit_nr_prv, &
1017 bounds1=bounds_a1, bounds2=bounds_a2, crop1=cropped_1, crop2=cropped_2)
1018 trans_1 = .NOT. trans_1
1019
1020 CALL reshape_mm_small(tensor_algn_3, map_1_mod, map_2_mod, tensor_contr_3, trans_3, &
1021 new_3, nodata=nodata_3, unit_nr=unit_nr_prv)
1022
1023 SELECT CASE (ref_tensor)
1024 CASE (1)
1025 tensor_large => tensor_contr_1
1026 CASE (2)
1027 tensor_large => tensor_contr_2
1028 END SELECT
1029 tensor_small => tensor_contr_3
1030
1031 CASE (3)
1032 IF (unit_nr_prv > 0) THEN
1033 WRITE (unit_nr_prv, '(T2,A)') "large tensors: 2, 3; small tensor: 1"
1034 WRITE (unit_nr_prv, '(T2,A)') "sorting contraction indices"
1035 END IF
1036 CALL index_linked_sort(map_1_mod, notcontract_1_mod)
1037 CALL index_linked_sort(contract_2_mod, contract_1_mod)
1038 SELECT CASE (max_tensor)
1039 CASE (2)
1040 CALL index_linked_sort(notcontract_2_mod, map_2_mod)
1041 CASE (3)
1042 CALL index_linked_sort(map_2_mod, notcontract_2_mod)
1043 CASE DEFAULT
1044 cpabort("should not happen")
1045 END SELECT
1046
1047 CALL reshape_mm_compatible(tensor_algn_2, tensor_algn_3, tensor_contr_2, tensor_contr_3, &
1048 contract_2_mod, notcontract_2_mod, map_1_mod, map_2_mod, &
1049 trans_2, trans_3, new_2, new_3, ref_tensor, nodata2=nodata_3, optimize_dist=optimize_dist, &
1050 move_data_1=move_data_2, unit_nr=unit_nr_prv, &
1051 bounds1=bounds_a2, crop1=cropped_2)
1052
1053 trans_2 = .NOT. trans_2
1054 trans_3 = .NOT. trans_3
1055
1056 CALL reshape_mm_small(tensor_algn_1, notcontract_1_mod, contract_1_mod, tensor_contr_1, &
1057 trans_1, new_1, move_data=move_data_1, unit_nr=unit_nr_prv, &
1058 bounds=bounds_a1, crop=cropped_1)
1059
1060 SELECT CASE (ref_tensor)
1061 CASE (1)
1062 tensor_large => tensor_contr_2
1063 CASE (2)
1064 tensor_large => tensor_contr_3
1065 END SELECT
1066 tensor_small => tensor_contr_1
1067
1068 END SELECT
1069
1070 IF (unit_nr_prv /= 0) CALL dbt_print_contraction_index(tensor_contr_1, indchar1_mod, &
1071 tensor_contr_2, indchar2_mod, &
1072 tensor_contr_3, indchar3_mod, unit_nr_prv)
1073 IF (unit_nr_prv /= 0) THEN
1074 IF (new_1) CALL dbt_write_tensor_info(tensor_contr_1, unit_nr_prv, full_info=log_verbose)
1075 IF (new_1) CALL dbt_write_tensor_dist(tensor_contr_1, unit_nr_prv)
1076 IF (new_2) CALL dbt_write_tensor_info(tensor_contr_2, unit_nr_prv, full_info=log_verbose)
1077 IF (new_2) CALL dbt_write_tensor_dist(tensor_contr_2, unit_nr_prv)
1078 END IF
1079
1080 associate(s => contract_stats(istat))
1081 IF (do_crop_1) s%nzcrop(1) = s%nzcrop(1) + dbt_get_nze(tensor_contr_1)
1082 IF (do_crop_2) s%nzcrop(2) = s%nzcrop(2) + dbt_get_nze(tensor_contr_2)
1083 IF (new_1) THEN
1084 s%nremap(1) = s%nremap(1) + 1
1085 s%nzremap(1) = s%nzremap(1) + dbt_get_nze(tensor_contr_1)
1086 IF (do_crop_1) s%ncrop_remap(1) = s%ncrop_remap(1) + 1
1087 END IF
1088 IF (new_2) THEN
1089 s%nremap(2) = s%nremap(2) + 1
1090 s%nzremap(2) = s%nzremap(2) + dbt_get_nze(tensor_contr_2)
1091 IF (do_crop_2) s%ncrop_remap(2) = s%ncrop_remap(2) + 1
1092 END IF
1093 END associate
1094
1095 CALL dbt_tas_multiply(trans_1, trans_2, trans_3, alpha, &
1096 tensor_contr_1%matrix_rep, tensor_contr_2%matrix_rep, &
1097 beta, &
1098 tensor_contr_3%matrix_rep, filter_eps=filter_eps, flop=flop_prv, &
1099 unit_nr=unit_nr_prv, log_verbose=log_verbose, &
1100 split_opt=split_opt, &
1101 move_data_a=move_data_1, move_data_b=move_data_2, retain_sparsity=retain_sparsity)
1102 IF (PRESENT(flop)) flop = flop_prv
1103 associate(s => contract_stats(istat))
1104 s%flop = s%flop + flop_prv
1105 IF (new_3) THEN ! result is copied back into tensor_3
1106 s%nremap(3) = s%nremap(3) + 1
1107 s%nzremap(3) = s%nzremap(3) + dbt_get_nze(tensor_contr_3)
1108 END IF
1109 END associate
1110
1111 IF (PRESENT(pgrid_opt_1)) THEN
1112 IF (.NOT. new_1) THEN
1113 ALLOCATE (pgrid_opt_1)
1114 pgrid_opt_1 = opt_pgrid(tensor_1, split_opt)
1115 END IF
1116 END IF
1117
1118 IF (PRESENT(pgrid_opt_2)) THEN
1119 IF (.NOT. new_2) THEN
1120 ALLOCATE (pgrid_opt_2)
1121 pgrid_opt_2 = opt_pgrid(tensor_2, split_opt)
1122 END IF
1123 END IF
1124
1125 IF (PRESENT(pgrid_opt_3)) THEN
1126 IF (.NOT. new_3) THEN
1127 ALLOCATE (pgrid_opt_3)
1128 pgrid_opt_3 = opt_pgrid(tensor_3, split_opt)
1129 END IF
1130 END IF
1131
1132 do_batched = tensor_small%matrix_rep%do_batched > 0
1133
1134 tensors_remapped = .false.
1135 IF (new_1 .OR. new_2 .OR. new_3) tensors_remapped = .true.
1136
1137 IF (tensors_remapped .AND. do_batched) THEN
1138 CALL cp_warn(__location__, &
1139 "Internal process grid optimization disabled because tensors are not in contraction-compatible format")
1140 END IF
1141
1142 ! optimize process grid during batched contraction
1143 do_change_pgrid(:) = .false.
1144 IF ((.NOT. tensors_remapped) .AND. do_batched) THEN
1145 associate(storage => tensor_small%contraction_storage)
1146 cpassert(storage%static)
1147 split = dbt_tas_info(tensor_large%matrix_rep)
1148 do_change_pgrid(:) = &
1149 update_contraction_storage(storage, split_opt, split)
1150
1151 IF (any(do_change_pgrid)) THEN
1152 mp_comm_opt = dbt_tas_mp_comm(tensor_small%pgrid%mp_comm_2d, split_opt%split_rowcol, nint(storage%nsplit_avg))
1153 CALL dbt_tas_create_split(split_opt_avg, mp_comm_opt, split_opt%split_rowcol, &
1154 nint(storage%nsplit_avg), own_comm=.true.)
1155 pdims_2d_opt = split_opt_avg%mp_comm%num_pe_cart
1156 END IF
1157
1158 END associate
1159
1160 IF (do_change_pgrid(1) .AND. .NOT. do_change_pgrid(2)) THEN
1161 ! check if new grid has better subgrid, if not there is no need to change process grid
1162 pdims_sub_opt = split_opt_avg%mp_comm_group%num_pe_cart
1163 pdims_sub = split%mp_comm_group%num_pe_cart
1164
1165 pdim_ratio = maxval(real(pdims_sub, dp))/minval(pdims_sub)
1166 pdim_ratio_opt = maxval(real(pdims_sub_opt, dp))/minval(pdims_sub_opt)
1167 IF (pdim_ratio/pdim_ratio_opt <= default_pdims_accept_ratio**2) THEN
1168 do_change_pgrid(1) = .false.
1169 CALL dbt_tas_release_info(split_opt_avg)
1170 END IF
1171 END IF
1172 END IF
1173
1174 IF (unit_nr_prv /= 0) THEN
1175 do_write_3 = .true.
1176 IF (tensor_contr_3%matrix_rep%do_batched > 0) THEN
1177 IF (tensor_contr_3%matrix_rep%mm_storage%batched_out) do_write_3 = .false.
1178 END IF
1179 IF (do_write_3) THEN
1180 CALL dbt_write_tensor_info(tensor_contr_3, unit_nr_prv, full_info=log_verbose)
1181 CALL dbt_write_tensor_dist(tensor_contr_3, unit_nr_prv)
1182 END IF
1183 END IF
1184
1185 IF (new_3) THEN
1186 ! need redistribute if we created new tensor for tensor 3
1187 CALL dbt_scale(tensor_algn_3, beta)
1188 CALL dbt_copy_expert(tensor_contr_3, tensor_algn_3, summation=.true., move_data=.true.)
1189 IF (PRESENT(filter_eps)) CALL dbt_filter(tensor_algn_3, filter_eps)
1190 ! tensor_3 automatically has correct data because tensor_algn_3 contains a matrix
1191 ! pointer to data of tensor_3
1192 END IF
1193
1194 ! transfer contraction storage
1195 CALL dbt_copy_contraction_storage(tensor_contr_1, tensor_1)
1196 CALL dbt_copy_contraction_storage(tensor_contr_2, tensor_2)
1197 CALL dbt_copy_contraction_storage(tensor_contr_3, tensor_3)
1198
1199 IF (unit_nr_prv /= 0) THEN
1200 IF (new_3 .AND. do_write_3) CALL dbt_write_tensor_info(tensor_3, unit_nr_prv, full_info=log_verbose)
1201 IF (new_3 .AND. do_write_3) CALL dbt_write_tensor_dist(tensor_3, unit_nr_prv)
1202 END IF
1203
1204 CALL dbt_destroy(tensor_algn_1)
1205 CALL dbt_destroy(tensor_algn_2)
1206 CALL dbt_destroy(tensor_algn_3)
1207
1208 IF (new_1 .OR. cropped_1) THEN
1209 CALL dbt_destroy(tensor_contr_1)
1210 DEALLOCATE (tensor_contr_1)
1211 END IF
1212 IF (new_2 .OR. cropped_2) THEN
1213 CALL dbt_destroy(tensor_contr_2)
1214 DEALLOCATE (tensor_contr_2)
1215 END IF
1216 IF (new_3) THEN
1217 CALL dbt_destroy(tensor_contr_3)
1218 DEALLOCATE (tensor_contr_3)
1219 END IF
1220
1221 IF (PRESENT(move_data)) THEN
1222 IF (move_data) THEN
1223 CALL dbt_clear(tensor_1)
1224 CALL dbt_clear(tensor_2)
1225 END IF
1226 END IF
1227
1228 IF (unit_nr_prv > 0) THEN
1229 WRITE (unit_nr_prv, '(A)') repeat("-", 80)
1230 WRITE (unit_nr_prv, '(A)') "TENSOR CONTRACTION DONE"
1231 WRITE (unit_nr_prv, '(A)') repeat("-", 80)
1232 END IF
1233
1234 IF (any(do_change_pgrid)) THEN
1235 pgrid_changed_any = .false.
1236 SELECT CASE (max_mm_dim)
1237 CASE (1)
1238 IF (ALLOCATED(tensor_1%contraction_storage) .AND. ALLOCATED(tensor_3%contraction_storage)) THEN
1239 CALL dbt_change_pgrid_2d(tensor_1, tensor_1%pgrid%mp_comm_2d, pdims=pdims_2d_opt, &
1240 nsplit=split_opt_avg%ngroup, dimsplit=split_opt_avg%split_rowcol, &
1241 pgrid_changed=pgrid_changed, &
1242 unit_nr=unit_nr_prv)
1243 IF (pgrid_changed) pgrid_changed_any = .true.
1244 CALL dbt_change_pgrid_2d(tensor_3, tensor_3%pgrid%mp_comm_2d, pdims=pdims_2d_opt, &
1245 nsplit=split_opt_avg%ngroup, dimsplit=split_opt_avg%split_rowcol, &
1246 pgrid_changed=pgrid_changed, &
1247 unit_nr=unit_nr_prv)
1248 IF (pgrid_changed) pgrid_changed_any = .true.
1249 END IF
1250 IF (pgrid_changed_any) THEN
1251 IF (tensor_2%matrix_rep%do_batched == 3) THEN
1252 ! set flag that process grid has been optimized to make sure that no grid optimizations are done
1253 ! in TAS multiply algorithm
1254 CALL dbt_tas_batched_mm_complete(tensor_2%matrix_rep)
1255 END IF
1256 END IF
1257 CASE (2)
1258 IF (ALLOCATED(tensor_1%contraction_storage) .AND. ALLOCATED(tensor_2%contraction_storage)) THEN
1259 CALL dbt_change_pgrid_2d(tensor_1, tensor_1%pgrid%mp_comm_2d, pdims=pdims_2d_opt, &
1260 nsplit=split_opt_avg%ngroup, dimsplit=split_opt_avg%split_rowcol, &
1261 pgrid_changed=pgrid_changed, &
1262 unit_nr=unit_nr_prv)
1263 IF (pgrid_changed) pgrid_changed_any = .true.
1264 CALL dbt_change_pgrid_2d(tensor_2, tensor_2%pgrid%mp_comm_2d, pdims=pdims_2d_opt, &
1265 nsplit=split_opt_avg%ngroup, dimsplit=split_opt_avg%split_rowcol, &
1266 pgrid_changed=pgrid_changed, &
1267 unit_nr=unit_nr_prv)
1268 IF (pgrid_changed) pgrid_changed_any = .true.
1269 END IF
1270 IF (pgrid_changed_any) THEN
1271 IF (tensor_3%matrix_rep%do_batched == 3) THEN
1272 CALL dbt_tas_batched_mm_complete(tensor_3%matrix_rep)
1273 END IF
1274 END IF
1275 CASE (3)
1276 IF (ALLOCATED(tensor_2%contraction_storage) .AND. ALLOCATED(tensor_3%contraction_storage)) THEN
1277 CALL dbt_change_pgrid_2d(tensor_2, tensor_2%pgrid%mp_comm_2d, pdims=pdims_2d_opt, &
1278 nsplit=split_opt_avg%ngroup, dimsplit=split_opt_avg%split_rowcol, &
1279 pgrid_changed=pgrid_changed, &
1280 unit_nr=unit_nr_prv)
1281 IF (pgrid_changed) pgrid_changed_any = .true.
1282 CALL dbt_change_pgrid_2d(tensor_3, tensor_3%pgrid%mp_comm_2d, pdims=pdims_2d_opt, &
1283 nsplit=split_opt_avg%ngroup, dimsplit=split_opt_avg%split_rowcol, &
1284 pgrid_changed=pgrid_changed, &
1285 unit_nr=unit_nr_prv)
1286 IF (pgrid_changed) pgrid_changed_any = .true.
1287 END IF
1288 IF (pgrid_changed_any) THEN
1289 IF (tensor_1%matrix_rep%do_batched == 3) THEN
1290 CALL dbt_tas_batched_mm_complete(tensor_1%matrix_rep)
1291 END IF
1292 END IF
1293 END SELECT
1294 CALL dbt_tas_release_info(split_opt_avg)
1295 END IF
1296
1297 IF ((.NOT. tensors_remapped) .AND. do_batched) THEN
1298 ! freeze TAS process grids if tensor grids were optimized
1299 CALL dbt_tas_set_batched_state(tensor_1%matrix_rep, opt_grid=.true.)
1300 CALL dbt_tas_set_batched_state(tensor_2%matrix_rep, opt_grid=.true.)
1301 CALL dbt_tas_set_batched_state(tensor_3%matrix_rep, opt_grid=.true.)
1302 END IF
1303
1304 CALL dbt_tas_release_info(split_opt)
1305
1306 CALL timestop(handle)
1307
1308 END SUBROUTINE dbt_contract_expert
1309
1310! **************************************************************************************************
1311!> \brief align tensor index with data
1312!> \author Patrick Seewald
1313! **************************************************************************************************
1314 SUBROUTINE align_tensor(tensor_in, contract_in, notcontract_in, &
1315 tensor_out, contract_out, notcontract_out, indp_in, indp_out, order)
1316 TYPE(dbt_type), INTENT(INOUT) :: tensor_in
1317 INTEGER, DIMENSION(:), INTENT(IN) :: contract_in, notcontract_in
1318 TYPE(dbt_type), INTENT(OUT) :: tensor_out
1319 INTEGER, DIMENSION(SIZE(contract_in)), &
1320 INTENT(OUT) :: contract_out
1321 INTEGER, DIMENSION(SIZE(notcontract_in)), &
1322 INTENT(OUT) :: notcontract_out
1323 CHARACTER(LEN=1), DIMENSION(ndims_tensor(tensor_in)), INTENT(IN) :: indp_in
1324 CHARACTER(LEN=1), DIMENSION(ndims_tensor(tensor_in)), INTENT(OUT) :: indp_out
1325 INTEGER, DIMENSION(ndims_tensor(tensor_in)), INTENT(OUT), OPTIONAL :: order
1326 INTEGER, DIMENSION(ndims_tensor(tensor_in)) :: align
1327
1328 CALL dbt_align_index(tensor_in, tensor_out, order=align)
1329 IF (PRESENT(order)) order = align
1330 contract_out = align(contract_in)
1331 notcontract_out = align(notcontract_in)
1332 indp_out(align) = indp_in
1333
1334 END SUBROUTINE align_tensor
1335
1336! **************************************************************************************************
1337!> \brief Prepare tensor for contraction: redistribute to a 2d format which can be contracted by
1338!> matrix multiplication. This routine reshapes the two largest of the three tensors.
1339!> Redistribution is avoided if tensors already in a consistent layout.
1340!> \param ind1_free indices of tensor 1 that are "free" (not linked to any index of tensor 2)
1341!> \param ind1_linked indices of tensor 1 that are linked to indices of tensor 2
1342!> 1:1 correspondence with ind1_linked
1343!> \param trans1 transpose flag of matrix rep. tensor 1
1344!> \param trans2 transpose flag of matrix rep. tensor 2
1345!> \param new1 whether a new tensor 1 was created
1346!> \param new2 whether a new tensor 2 was created
1347!> \param nodata1 don't copy data of tensor 1
1348!> \param nodata2 don't copy data of tensor 2
1349!> \param move_data_1 memory optimization: transfer data s.t. tensor1 may be empty on return
1350!> \param move_data_2 memory optimization: transfer data s.t. tensor2 may be empty on return
1351!> \param optimize_dist experimental: optimize distribution
1352!> \param unit_nr output unit
1353!> \author Patrick Seewald
1354! **************************************************************************************************
1355 SUBROUTINE reshape_mm_compatible(tensor1, tensor2, tensor1_out, tensor2_out, ind1_free, ind1_linked, &
1356 ind2_free, ind2_linked, trans1, trans2, new1, new2, ref_tensor, &
1357 nodata1, nodata2, move_data_1, &
1358 move_data_2, optimize_dist, unit_nr, bounds1, bounds2, crop1, crop2)
1359 TYPE(dbt_type), TARGET, INTENT(INOUT) :: tensor1
1360 TYPE(dbt_type), TARGET, INTENT(INOUT) :: tensor2
1361 TYPE(dbt_type), POINTER, INTENT(OUT) :: tensor1_out, tensor2_out
1362 INTEGER, DIMENSION(:), INTENT(IN) :: ind1_free, ind2_free
1363 INTEGER, DIMENSION(:), INTENT(IN) :: ind1_linked, ind2_linked
1364 LOGICAL, INTENT(OUT) :: trans1, trans2
1365 LOGICAL, INTENT(OUT) :: new1, new2
1366 INTEGER, INTENT(OUT) :: ref_tensor
1367 LOGICAL, INTENT(IN), OPTIONAL :: nodata1, nodata2
1368 LOGICAL, INTENT(INOUT), OPTIONAL :: move_data_1, move_data_2
1369 LOGICAL, INTENT(IN), OPTIONAL :: optimize_dist
1370 INTEGER, INTENT(IN), OPTIONAL :: unit_nr
1371 INTEGER, DIMENSION(2, ndims_tensor(tensor1)), INTENT(IN), OPTIONAL :: bounds1
1372 INTEGER, DIMENSION(2, ndims_tensor(tensor2)), INTENT(IN), OPTIONAL :: bounds2
1373 LOGICAL, INTENT(OUT), OPTIONAL :: crop1, crop2
1374 LOGICAL :: crop1_prv, crop2_prv
1375 INTEGER :: compat1, compat1_old, compat2, compat2_old, &
1376 unit_nr_prv
1377 TYPE(mp_cart_type) :: comm_2d
1378 TYPE(array_list) :: dist_list
1379 INTEGER, DIMENSION(:), ALLOCATABLE :: mp_dims
1380 TYPE(dbt_distribution_type) :: dist_in
1381 INTEGER(KIND=int_8) :: nblkrows, nblkcols
1382 LOGICAL :: optimize_dist_prv
1383 INTEGER, DIMENSION(ndims_tensor(tensor1)) :: dims1
1384 INTEGER, DIMENSION(ndims_tensor(tensor2)) :: dims2
1385
1386 NULLIFY (tensor1_out, tensor2_out)
1387
1388 unit_nr_prv = prep_output_unit(unit_nr)
1389
1390 CALL blk_dims_tensor(tensor1, dims1)
1391 CALL blk_dims_tensor(tensor2, dims2)
1392
1393 IF (product(int(dims1, int_8)) >= product(int(dims2, int_8))) THEN
1394 ref_tensor = 1
1395 ELSE
1396 ref_tensor = 2
1397 END IF
1398
1399 IF (PRESENT(optimize_dist)) THEN
1400 optimize_dist_prv = optimize_dist
1401 ELSE
1402 optimize_dist_prv = .false.
1403 END IF
1404
1405 compat1 = compat_map(tensor1%nd_index, ind1_linked)
1406 compat2 = compat_map(tensor2%nd_index, ind2_linked)
1407 compat1_old = compat1
1408 compat2_old = compat2
1409
1410 IF (unit_nr_prv > 0) THEN
1411 WRITE (unit_nr_prv, '(T2,A,1X,A,A,1X)', advance='no') "compatibility of", trim(tensor1%name), ":"
1412 SELECT CASE (compat1)
1413 CASE (0)
1414 WRITE (unit_nr_prv, '(A)') "Not compatible"
1415 CASE (1)
1416 WRITE (unit_nr_prv, '(A)') "Normal"
1417 CASE (2)
1418 WRITE (unit_nr_prv, '(A)') "Transposed"
1419 END SELECT
1420 WRITE (unit_nr_prv, '(T2,A,1X,A,A,1X)', advance='no') "compatibility of", trim(tensor2%name), ":"
1421 SELECT CASE (compat2)
1422 CASE (0)
1423 WRITE (unit_nr_prv, '(A)') "Not compatible"
1424 CASE (1)
1425 WRITE (unit_nr_prv, '(A)') "Normal"
1426 CASE (2)
1427 WRITE (unit_nr_prv, '(A)') "Transposed"
1428 END SELECT
1429 END IF
1430
1431 new1 = .false.
1432 new2 = .false.
1433 crop1_prv = .false.
1434 crop2_prv = .false.
1435
1436 IF (compat1 == 0 .OR. optimize_dist_prv) THEN
1437 new1 = .true.
1438 END IF
1439
1440 IF (compat2 == 0 .OR. optimize_dist_prv) THEN
1441 new2 = .true.
1442 END IF
1443
1444 IF (ref_tensor == 1) THEN ! tensor 1 is reference and tensor 2 is reshaped compatible with tensor 1
1445 IF (compat1 == 0 .OR. optimize_dist_prv) THEN ! tensor 1 is not contraction compatible --> reshape
1446 IF (unit_nr_prv > 0) WRITE (unit_nr_prv, '(T2,A,1X,A)') "Redistribution of", trim(tensor1%name)
1447 nblkrows = product(int(dims1(ind1_linked), kind=int_8))
1448 nblkcols = product(int(dims1(ind1_free), kind=int_8))
1449 comm_2d = dbt_tas_mp_comm(tensor1%pgrid%mp_comm_2d, nblkrows, nblkcols)
1450 ALLOCATE (tensor1_out)
1451 CALL dbt_remap(tensor1, ind1_linked, ind1_free, tensor1_out, comm_2d=comm_2d, &
1452 nodata=nodata1, move_data=move_data_1, &
1453 bounds=bounds1)
1454 CALL comm_2d%free()
1455 compat1 = 1
1456 ELSE
1457 IF (unit_nr_prv > 0) WRITE (unit_nr_prv, '(T2,A,1X,A)') "No redistribution of", trim(tensor1%name)
1458 CALL crop_or_point(tensor1, tensor1_out, crop1_prv, bounds1, move_data_1)
1459 END IF
1460 IF (compat2 == 0 .OR. optimize_dist_prv) THEN ! tensor 2 is not contraction compatible --> reshape
1461 IF (unit_nr_prv > 0) WRITE (unit_nr_prv, '(T2,A,1X,A,1X,A,1X,A)') "Redistribution of", &
1462 trim(tensor2%name), "compatible with", trim(tensor1%name)
1463 dist_in = dbt_distribution(tensor1_out)
1464 dist_list = array_sublist(dist_in%nd_dist, ind1_linked)
1465 IF (compat1 == 1) THEN ! linked index is first 2d dimension
1466 ! get distribution of linked index, tensor 2 must adopt this distribution
1467 ! get grid dimensions of linked index
1468 ALLOCATE (mp_dims(ndims_mapping_row(dist_in%pgrid%nd_index_grid)))
1469 CALL dbt_get_mapping_info(dist_in%pgrid%nd_index_grid, dims1_2d=mp_dims)
1470 ALLOCATE (tensor2_out)
1471 CALL dbt_remap(tensor2, ind2_linked, ind2_free, tensor2_out, comm_2d=dist_in%pgrid%mp_comm_2d, &
1472 dist1=dist_list, mp_dims_1=mp_dims, nodata=nodata2, move_data=move_data_2, &
1473 bounds=bounds2)
1474 ELSE IF (compat1 == 2) THEN ! linked index is second 2d dimension
1475 ! get distribution of linked index, tensor 2 must adopt this distribution
1476 ! get grid dimensions of linked index
1477 ALLOCATE (mp_dims(ndims_mapping_column(dist_in%pgrid%nd_index_grid)))
1478 CALL dbt_get_mapping_info(dist_in%pgrid%nd_index_grid, dims2_2d=mp_dims)
1479 ALLOCATE (tensor2_out)
1480 CALL dbt_remap(tensor2, ind2_free, ind2_linked, tensor2_out, comm_2d=dist_in%pgrid%mp_comm_2d, &
1481 dist2=dist_list, mp_dims_2=mp_dims, nodata=nodata2, move_data=move_data_2, &
1482 bounds=bounds2)
1483 ELSE
1484 cpabort("should not happen")
1485 END IF
1486 compat2 = compat1
1487 ELSE
1488 IF (unit_nr_prv > 0) WRITE (unit_nr_prv, '(T2,A,1X,A)') "No redistribution of", trim(tensor2%name)
1489 CALL crop_or_point(tensor2, tensor2_out, crop2_prv, bounds2, move_data_2)
1490 END IF
1491 ELSE ! tensor 2 is reference and tensor 1 is reshaped compatible with tensor 2
1492 IF (compat2 == 0 .OR. optimize_dist_prv) THEN ! tensor 2 is not contraction compatible --> reshape
1493 IF (unit_nr_prv > 0) WRITE (unit_nr_prv, '(T2,A,1X,A)') "Redistribution of", trim(tensor2%name)
1494 nblkrows = product(int(dims2(ind2_linked), kind=int_8))
1495 nblkcols = product(int(dims2(ind2_free), kind=int_8))
1496 comm_2d = dbt_tas_mp_comm(tensor2%pgrid%mp_comm_2d, nblkrows, nblkcols)
1497 ALLOCATE (tensor2_out)
1498 CALL dbt_remap(tensor2, ind2_linked, ind2_free, tensor2_out, nodata=nodata2, move_data=move_data_2, &
1499 bounds=bounds2)
1500 CALL comm_2d%free()
1501 compat2 = 1
1502 ELSE
1503 IF (unit_nr_prv > 0) WRITE (unit_nr_prv, '(T2,A,1X,A)') "No redistribution of", trim(tensor2%name)
1504 CALL crop_or_point(tensor2, tensor2_out, crop2_prv, bounds2, move_data_2)
1505 END IF
1506 IF (compat1 == 0 .OR. optimize_dist_prv) THEN ! tensor 1 is not contraction compatible --> reshape
1507 IF (unit_nr_prv > 0) WRITE (unit_nr_prv, '(T2,A,1X,A,1X,A,1X,A)') "Redistribution of", trim(tensor1%name), &
1508 "compatible with", trim(tensor2%name)
1509 dist_in = dbt_distribution(tensor2_out)
1510 dist_list = array_sublist(dist_in%nd_dist, ind2_linked)
1511 IF (compat2 == 1) THEN
1512 ALLOCATE (mp_dims(ndims_mapping_row(dist_in%pgrid%nd_index_grid)))
1513 CALL dbt_get_mapping_info(dist_in%pgrid%nd_index_grid, dims1_2d=mp_dims)
1514 ALLOCATE (tensor1_out)
1515 CALL dbt_remap(tensor1, ind1_linked, ind1_free, tensor1_out, comm_2d=dist_in%pgrid%mp_comm_2d, &
1516 dist1=dist_list, mp_dims_1=mp_dims, nodata=nodata1, move_data=move_data_1, &
1517 bounds=bounds1)
1518 ELSE IF (compat2 == 2) THEN
1519 ALLOCATE (mp_dims(ndims_mapping_column(dist_in%pgrid%nd_index_grid)))
1520 CALL dbt_get_mapping_info(dist_in%pgrid%nd_index_grid, dims2_2d=mp_dims)
1521 ALLOCATE (tensor1_out)
1522 CALL dbt_remap(tensor1, ind1_free, ind1_linked, tensor1_out, comm_2d=dist_in%pgrid%mp_comm_2d, &
1523 dist2=dist_list, mp_dims_2=mp_dims, nodata=nodata1, move_data=move_data_1, &
1524 bounds=bounds1)
1525 ELSE
1526 cpabort("should not happen")
1527 END IF
1528 compat1 = compat2
1529 ELSE
1530 IF (unit_nr_prv > 0) WRITE (unit_nr_prv, '(T2,A,1X,A)') "No redistribution of", trim(tensor1%name)
1531 CALL crop_or_point(tensor1, tensor1_out, crop1_prv, bounds1, move_data_1)
1532 END IF
1533 END IF
1534
1535 SELECT CASE (compat1)
1536 CASE (1)
1537 trans1 = .false.
1538 CASE (2)
1539 trans1 = .true.
1540 CASE DEFAULT
1541 cpabort("should not happen")
1542 END SELECT
1543
1544 SELECT CASE (compat2)
1545 CASE (1)
1546 trans2 = .false.
1547 CASE (2)
1548 trans2 = .true.
1549 CASE DEFAULT
1550 cpabort("should not happen")
1551 END SELECT
1552
1553 IF (unit_nr_prv > 0) THEN
1554 IF (compat1 /= compat1_old) THEN
1555 WRITE (unit_nr_prv, '(T2,A,1X,A,A,1X)', advance='no') "compatibility of", trim(tensor1_out%name), ":"
1556 SELECT CASE (compat1)
1557 CASE (0)
1558 WRITE (unit_nr_prv, '(A)') "Not compatible"
1559 CASE (1)
1560 WRITE (unit_nr_prv, '(A)') "Normal"
1561 CASE (2)
1562 WRITE (unit_nr_prv, '(A)') "Transposed"
1563 END SELECT
1564 END IF
1565 IF (compat2 /= compat2_old) THEN
1566 WRITE (unit_nr_prv, '(T2,A,1X,A,A,1X)', advance='no') "compatibility of", trim(tensor2_out%name), ":"
1567 SELECT CASE (compat2)
1568 CASE (0)
1569 WRITE (unit_nr_prv, '(A)') "Not compatible"
1570 CASE (1)
1571 WRITE (unit_nr_prv, '(A)') "Normal"
1572 CASE (2)
1573 WRITE (unit_nr_prv, '(A)') "Transposed"
1574 END SELECT
1575 END IF
1576 END IF
1577
1578 IF (new1 .AND. PRESENT(move_data_1)) move_data_1 = .true.
1579 IF (new2 .AND. PRESENT(move_data_2)) move_data_2 = .true.
1580
1581 IF (PRESENT(crop1)) crop1 = crop1_prv
1582 IF (PRESENT(crop2)) crop2 = crop2_prv
1583
1584 END SUBROUTINE reshape_mm_compatible
1585
1586! **************************************************************************************************
1587!> \brief Prepare tensor for contraction: redistribute to a 2d format which can be contracted by
1588!> matrix multiplication. This routine reshapes the smallest of the three tensors.
1589!> \param ind1 index that should be mapped to first matrix dimension
1590!> \param ind2 index that should be mapped to second matrix dimension
1591!> \param trans transpose flag of matrix rep.
1592!> \param new whether a new tensor was created for tensor_out
1593!> \param nodata don't copy tensor data
1594!> \param move_data memory optimization: transfer data s.t. tensor_in may be empty on return
1595!> \param unit_nr output unit
1596!> \author Patrick Seewald
1597! **************************************************************************************************
1598 SUBROUTINE reshape_mm_small(tensor_in, ind1, ind2, tensor_out, trans, new, nodata, move_data, unit_nr, &
1599 bounds, crop)
1600 TYPE(dbt_type), TARGET, INTENT(INOUT) :: tensor_in
1601 INTEGER, DIMENSION(:), INTENT(IN) :: ind1, ind2
1602 TYPE(dbt_type), POINTER, INTENT(OUT) :: tensor_out
1603 LOGICAL, INTENT(OUT) :: trans
1604 LOGICAL, INTENT(OUT) :: new
1605 LOGICAL, INTENT(IN), OPTIONAL :: nodata
1606 LOGICAL, INTENT(INOUT), OPTIONAL :: move_data
1607 INTEGER, INTENT(IN), OPTIONAL :: unit_nr
1608 INTEGER, DIMENSION(2, ndims_tensor(tensor_in)), INTENT(IN), OPTIONAL :: bounds
1609 LOGICAL, INTENT(OUT), OPTIONAL :: crop
1610 LOGICAL :: crop_prv
1611 INTEGER :: compat1, compat2, compat1_old, compat2_old, unit_nr_prv
1612 LOGICAL :: nodata_prv
1613
1614 NULLIFY (tensor_out)
1615 IF (PRESENT(nodata)) THEN
1616 nodata_prv = nodata
1617 ELSE
1618 nodata_prv = .false.
1619 END IF
1620
1621 unit_nr_prv = prep_output_unit(unit_nr)
1622
1623 new = .false.
1624 crop_prv = .false.
1625 compat1 = compat_map(tensor_in%nd_index, ind1)
1626 compat2 = compat_map(tensor_in%nd_index, ind2)
1627 compat1_old = compat1; compat2_old = compat2
1628 IF (unit_nr_prv > 0) THEN
1629 WRITE (unit_nr_prv, '(T2,A,1X,A,A,1X)', advance='no') "compatibility of", trim(tensor_in%name), ":"
1630 IF (compat1 == 1 .AND. compat2 == 2) THEN
1631 WRITE (unit_nr_prv, '(A)') "Normal"
1632 ELSE IF (compat1 == 2 .AND. compat2 == 1) THEN
1633 WRITE (unit_nr_prv, '(A)') "Transposed"
1634 ELSE
1635 WRITE (unit_nr_prv, '(A)') "Not compatible"
1636 END IF
1637 END IF
1638 IF (compat1 == 0 .or. compat2 == 0) THEN ! index mapping not compatible with contract index
1639
1640 IF (unit_nr_prv > 0) WRITE (unit_nr_prv, '(T2,A,1X,A)') "Redistribution of", trim(tensor_in%name)
1641
1642 ALLOCATE (tensor_out)
1643 CALL dbt_remap(tensor_in, ind1, ind2, tensor_out, nodata=nodata, move_data=move_data, bounds=bounds)
1644 CALL dbt_copy_contraction_storage(tensor_in, tensor_out)
1645 compat1 = 1
1646 compat2 = 2
1647 new = .true.
1648 ELSE
1649 IF (unit_nr_prv > 0) WRITE (unit_nr_prv, '(T2,A,1X,A)') "No redistribution of", trim(tensor_in%name)
1650 CALL crop_or_point(tensor_in, tensor_out, crop_prv, bounds, move_data)
1651 END IF
1652
1653 IF (compat1 == 1 .AND. compat2 == 2) THEN
1654 trans = .false.
1655 ELSE IF (compat1 == 2 .AND. compat2 == 1) THEN
1656 trans = .true.
1657 ELSE
1658 cpabort("this should not happen")
1659 END IF
1660
1661 IF (unit_nr_prv > 0) THEN
1662 IF (compat1_old /= compat1 .OR. compat2_old /= compat2) THEN
1663 WRITE (unit_nr_prv, '(T2,A,1X,A,A,1X)', advance='no') "compatibility of", trim(tensor_out%name), ":"
1664 IF (compat1 == 1 .AND. compat2 == 2) THEN
1665 WRITE (unit_nr_prv, '(A)') "Normal"
1666 ELSE IF (compat1 == 2 .AND. compat2 == 1) THEN
1667 WRITE (unit_nr_prv, '(A)') "Transposed"
1668 ELSE
1669 WRITE (unit_nr_prv, '(A)') "Not compatible"
1670 END IF
1671 END IF
1672 END IF
1673
1674 IF (PRESENT(crop)) crop = crop_prv
1675
1676 END SUBROUTINE reshape_mm_small
1677
1678! **************************************************************************************************
1679!> \brief Crop tensor_in into a new tensor_out if bounds are present, otherwise point to tensor_in.
1680!> \param cropped whether tensor_out is a new tensor
1681!> \param move_data memory optimization: transfer data s.t. tensor_in may be empty on return
1682!> \author Hans Pabst
1683! **************************************************************************************************
1684 SUBROUTINE crop_or_point(tensor_in, tensor_out, cropped, bounds, move_data)
1685 TYPE(dbt_type), TARGET, INTENT(INOUT) :: tensor_in
1686 TYPE(dbt_type), POINTER, INTENT(OUT) :: tensor_out
1687 LOGICAL, INTENT(OUT) :: cropped
1688 INTEGER, DIMENSION(2, ndims_tensor(tensor_in)), INTENT(IN), OPTIONAL :: bounds
1689 LOGICAL, INTENT(INOUT), OPTIONAL :: move_data
1690
1691 cropped = PRESENT(bounds)
1692 IF (cropped) THEN
1693 ALLOCATE (tensor_out)
1694 CALL dbt_crop(tensor_in, tensor_out, bounds, move_data=move_data)
1695 IF (PRESENT(move_data)) move_data = .true. ! tensor_out is temporary
1696 ELSE
1697 tensor_out => tensor_in
1698 END IF
1699 END SUBROUTINE crop_or_point
1700
1701! **************************************************************************************************
1702!> \brief Number of local blocks overlapping the bounds (blocks dbt_crop would keep), and number
1703!> of local blocks not entirely within the bounds (none: dbt_crop would copy as is).
1704!> \author Hans Pabst
1705! **************************************************************************************************
1706 FUNCTION num_blocks_in_bounds(tensor, bounds) RESULT(nblocks)
1707 TYPE(dbt_type), INTENT(INOUT) :: tensor
1708 INTEGER, DIMENSION(2, ndims_tensor(tensor)), INTENT(IN) :: bounds
1709 INTEGER, DIMENSION(2) :: nblocks
1710
1711 TYPE(dbt_iterator_type) :: iter
1712 INTEGER, DIMENSION(ndims_tensor(tensor)) :: blk_index, blk_offset, blk_size
1713 INTEGER :: nin, nout
1714
1715 nin = 0
1716 nout = 0
1717!$OMP PARALLEL DEFAULT(NONE) SHARED(tensor,bounds) PRIVATE(iter,blk_index,blk_size,blk_offset) &
1718!$OMP REDUCTION(+:nin,nout)
1719 CALL dbt_iterator_start(iter, tensor)
1720 DO WHILE (dbt_iterator_blocks_left(iter))
1721 CALL dbt_iterator_next_block(iter, blk_index, blk_size=blk_size, blk_offset=blk_offset)
1722 IF (all(blk_offset <= bounds(2, :) .AND. bounds(1, :) <= blk_offset + blk_size - 1)) THEN
1723 nin = nin + 1
1724 END IF
1725 IF (any(blk_offset < bounds(1, :) .OR. bounds(2, :) < blk_offset + blk_size - 1)) THEN
1726 nout = nout + 1
1727 END IF
1728 END DO
1729 CALL dbt_iterator_stop(iter)
1730!$OMP END PARALLEL
1731 nblocks = [nin, nout]
1732 END FUNCTION num_blocks_in_bounds
1733
1734! **************************************************************************************************
1735!> \brief update contraction storage that keeps track of process grids during a batched contraction
1736!> and decide if tensor process grid needs to be optimized
1737!> \param split_opt optimized TAS process grid
1738!> \param split current TAS process grid
1739!> \author Patrick Seewald
1740! **************************************************************************************************
1741 FUNCTION update_contraction_storage(storage, split_opt, split) RESULT(do_change_pgrid)
1742 TYPE(dbt_contraction_storage), INTENT(INOUT) :: storage
1743 TYPE(dbt_tas_split_info), INTENT(IN) :: split_opt
1744 TYPE(dbt_tas_split_info), INTENT(IN) :: split
1745 INTEGER, DIMENSION(2) :: pdims, pdims_sub
1746 LOGICAL, DIMENSION(2) :: do_change_pgrid
1747 REAL(kind=dp) :: change_criterion, pdims_ratio
1748 INTEGER :: nsplit_opt, nsplit
1749
1750 cpassert(ALLOCATED(split_opt%ngroup_opt))
1751 nsplit_opt = split_opt%ngroup_opt
1752 nsplit = split%ngroup
1753
1754 pdims = split%mp_comm%num_pe_cart
1755
1756 storage%ibatch = storage%ibatch + 1
1757
1758 storage%nsplit_avg = (storage%nsplit_avg*real(storage%ibatch - 1, dp) + real(nsplit_opt, dp)) &
1759 /real(storage%ibatch, dp)
1760
1761 SELECT CASE (split_opt%split_rowcol)
1762 CASE (rowsplit)
1763 pdims_ratio = real(pdims(1), dp)/pdims(2)
1764 CASE (colsplit)
1765 pdims_ratio = real(pdims(2), dp)/pdims(1)
1766 END SELECT
1767
1768 do_change_pgrid(:) = .false.
1769
1770 ! check for process grid dimensions
1771 pdims_sub = split%mp_comm_group%num_pe_cart
1772 change_criterion = maxval(real(pdims_sub, dp))/minval(pdims_sub)
1773 IF (change_criterion > default_pdims_accept_ratio**2) do_change_pgrid(1) = .true.
1774
1775 ! check for split factor
1776 change_criterion = max(real(nsplit, dp)/storage%nsplit_avg, real(storage%nsplit_avg, dp)/nsplit)
1777 IF (change_criterion > default_nsplit_accept_ratio) do_change_pgrid(2) = .true.
1778
1779 END FUNCTION update_contraction_storage
1780
1781! **************************************************************************************************
1782!> \brief Check if 2d index is compatible with tensor index
1783!> \author Patrick Seewald
1784! **************************************************************************************************
1785 FUNCTION compat_map(nd_index, compat_ind)
1786 TYPE(nd_to_2d_mapping), INTENT(IN) :: nd_index
1787 INTEGER, DIMENSION(:), INTENT(IN) :: compat_ind
1788 INTEGER, DIMENSION(ndims_mapping_row(nd_index)) :: map1
1789 INTEGER, DIMENSION(ndims_mapping_column(nd_index)) :: map2
1790 INTEGER :: compat_map
1791
1792 CALL dbt_get_mapping_info(nd_index, map1_2d=map1, map2_2d=map2)
1793
1794 compat_map = 0
1795 IF (array_eq_i(map1, compat_ind)) THEN
1796 compat_map = 1
1797 ELSE IF (array_eq_i(map2, compat_ind)) THEN
1798 compat_map = 2
1799 END IF
1800
1801 END FUNCTION compat_map
1802
1803! **************************************************************************************************
1804!> \brief
1805!> \author Patrick Seewald
1806! **************************************************************************************************
1807 SUBROUTINE index_linked_sort(ind_ref, ind_dep)
1808 INTEGER, DIMENSION(:), INTENT(INOUT) :: ind_ref, ind_dep
1809 INTEGER, DIMENSION(SIZE(ind_ref)) :: sort_indices
1810
1811 CALL sort(ind_ref, SIZE(ind_ref), sort_indices)
1812 ind_dep(:) = ind_dep(sort_indices)
1813
1814 END SUBROUTINE index_linked_sort
1815
1816! **************************************************************************************************
1817!> \brief
1818!> \author Patrick Seewald
1819! **************************************************************************************************
1820 FUNCTION opt_pgrid(tensor, tas_split_info)
1821 TYPE(dbt_type), INTENT(IN) :: tensor
1822 TYPE(dbt_tas_split_info), INTENT(IN) :: tas_split_info
1823 INTEGER, DIMENSION(ndims_matrix_row(tensor)) :: map1
1824 INTEGER, DIMENSION(ndims_matrix_column(tensor)) :: map2
1825 TYPE(dbt_pgrid_type) :: opt_pgrid
1826 INTEGER, DIMENSION(ndims_tensor(tensor)) :: dims
1827
1828 CALL dbt_get_mapping_info(tensor%pgrid%nd_index_grid, map1_2d=map1, map2_2d=map2)
1829 CALL blk_dims_tensor(tensor, dims)
1830 opt_pgrid = dbt_nd_mp_comm(tas_split_info%mp_comm, map1, map2, tdims=dims)
1831
1832 ALLOCATE (opt_pgrid%tas_split_info, source=tas_split_info)
1833 CALL dbt_tas_info_hold(opt_pgrid%tas_split_info)
1834 END FUNCTION opt_pgrid
1835
1836! **************************************************************************************************
1837!> \brief Copy tensor to tensor with modified index mapping
1838!> \param map1_2d new index mapping
1839!> \param map2_2d new index mapping
1840!> \author Patrick Seewald
1841! **************************************************************************************************
1842 SUBROUTINE dbt_remap(tensor_in, map1_2d, map2_2d, tensor_out, comm_2d, dist1, dist2, &
1843 mp_dims_1, mp_dims_2, name, nodata, move_data, bounds)
1844 TYPE(dbt_type), INTENT(INOUT) :: tensor_in
1845 INTEGER, DIMENSION(2, ndims_tensor(tensor_in)), INTENT(IN), OPTIONAL :: bounds
1846 INTEGER, DIMENSION(:), INTENT(IN) :: map1_2d, map2_2d
1847 TYPE(dbt_type), INTENT(OUT) :: tensor_out
1848 CHARACTER(len=*), INTENT(IN), OPTIONAL :: name
1849 LOGICAL, INTENT(IN), OPTIONAL :: nodata, move_data
1850 CLASS(mp_comm_type), INTENT(IN), OPTIONAL :: comm_2d
1851 TYPE(array_list), INTENT(IN), OPTIONAL :: dist1, dist2
1852 INTEGER, DIMENSION(SIZE(map1_2d)), OPTIONAL :: mp_dims_1
1853 INTEGER, DIMENSION(SIZE(map2_2d)), OPTIONAL :: mp_dims_2
1854 CHARACTER(len=default_string_length) :: name_tmp
1855 INTEGER, DIMENSION(:), ALLOCATABLE :: blk_sizes_1, blk_sizes_2, blk_sizes_3, blk_sizes_4, &
1856 nd_dist_1, nd_dist_2, nd_dist_3, nd_dist_4
1857 TYPE(dbt_distribution_type) :: dist
1858 TYPE(mp_cart_type) :: comm_2d_prv
1859 INTEGER :: handle, i
1860 INTEGER, DIMENSION(ndims_tensor(tensor_in)) :: pdims, myploc
1861 CHARACTER(LEN=*), PARAMETER :: routinen = 'dbt_remap'
1862 LOGICAL :: nodata_prv
1863 TYPE(dbt_pgrid_type) :: comm_nd
1864
1865 CALL timeset(routinen, handle)
1866
1867 IF (PRESENT(name)) THEN
1868 name_tmp = name
1869 ELSE
1870 name_tmp = tensor_in%name
1871 END IF
1872 IF (PRESENT(dist1)) THEN
1873 cpassert(PRESENT(mp_dims_1))
1874 END IF
1875
1876 IF (PRESENT(dist2)) THEN
1877 cpassert(PRESENT(mp_dims_2))
1878 END IF
1879
1880 IF (PRESENT(comm_2d)) THEN
1881 comm_2d_prv = comm_2d
1882 ELSE
1883 comm_2d_prv = tensor_in%pgrid%mp_comm_2d
1884 END IF
1885
1886 comm_nd = dbt_nd_mp_comm(comm_2d_prv, map1_2d, map2_2d, dims1_nd=mp_dims_1, dims2_nd=mp_dims_2)
1887 CALL mp_environ_pgrid(comm_nd, pdims, myploc)
1888
1889 IF (ndims_tensor(tensor_in) == 2) THEN
1890 CALL get_arrays(tensor_in%blk_sizes, blk_sizes_1, blk_sizes_2)
1891 END IF
1892 IF (ndims_tensor(tensor_in) == 3) THEN
1893 CALL get_arrays(tensor_in%blk_sizes, blk_sizes_1, blk_sizes_2, blk_sizes_3)
1894 END IF
1895 IF (ndims_tensor(tensor_in) == 4) THEN
1896 CALL get_arrays(tensor_in%blk_sizes, blk_sizes_1, blk_sizes_2, blk_sizes_3, blk_sizes_4)
1897 END IF
1898
1899 IF (ndims_tensor(tensor_in) == 2) THEN
1900 IF (PRESENT(dist1)) THEN
1901 IF (any(map1_2d == 1)) THEN
1902 i = minloc(map1_2d, dim=1, mask=map1_2d == 1) ! i is location of idim in map1_2d
1903 CALL get_ith_array(dist1, i, nd_dist_1)
1904 END IF
1905 END IF
1906
1907 IF (PRESENT(dist2)) THEN
1908 IF (any(map2_2d == 1)) THEN
1909 i = minloc(map2_2d, dim=1, mask=map2_2d == 1) ! i is location of idim in map2_2d
1910 CALL get_ith_array(dist2, i, nd_dist_1)
1911 END IF
1912 END IF
1913
1914 IF (.NOT. ALLOCATED(nd_dist_1)) THEN
1915 ALLOCATE (nd_dist_1(SIZE(blk_sizes_1)))
1916 CALL dbt_default_distvec(SIZE(blk_sizes_1), pdims(1), blk_sizes_1, nd_dist_1)
1917 END IF
1918 IF (PRESENT(dist1)) THEN
1919 IF (any(map1_2d == 2)) THEN
1920 i = minloc(map1_2d, dim=1, mask=map1_2d == 2) ! i is location of idim in map1_2d
1921 CALL get_ith_array(dist1, i, nd_dist_2)
1922 END IF
1923 END IF
1924
1925 IF (PRESENT(dist2)) THEN
1926 IF (any(map2_2d == 2)) THEN
1927 i = minloc(map2_2d, dim=1, mask=map2_2d == 2) ! i is location of idim in map2_2d
1928 CALL get_ith_array(dist2, i, nd_dist_2)
1929 END IF
1930 END IF
1931
1932 IF (.NOT. ALLOCATED(nd_dist_2)) THEN
1933 ALLOCATE (nd_dist_2(SIZE(blk_sizes_2)))
1934 CALL dbt_default_distvec(SIZE(blk_sizes_2), pdims(2), blk_sizes_2, nd_dist_2)
1935 END IF
1936 CALL dbt_distribution_new_expert(dist, comm_nd, map1_2d, map2_2d, &
1937 nd_dist_1, nd_dist_2, own_comm=.true.)
1938 END IF
1939 IF (ndims_tensor(tensor_in) == 3) THEN
1940 IF (PRESENT(dist1)) THEN
1941 IF (any(map1_2d == 1)) THEN
1942 i = minloc(map1_2d, dim=1, mask=map1_2d == 1) ! i is location of idim in map1_2d
1943 CALL get_ith_array(dist1, i, nd_dist_1)
1944 END IF
1945 END IF
1946
1947 IF (PRESENT(dist2)) THEN
1948 IF (any(map2_2d == 1)) THEN
1949 i = minloc(map2_2d, dim=1, mask=map2_2d == 1) ! i is location of idim in map2_2d
1950 CALL get_ith_array(dist2, i, nd_dist_1)
1951 END IF
1952 END IF
1953
1954 IF (.NOT. ALLOCATED(nd_dist_1)) THEN
1955 ALLOCATE (nd_dist_1(SIZE(blk_sizes_1)))
1956 CALL dbt_default_distvec(SIZE(blk_sizes_1), pdims(1), blk_sizes_1, nd_dist_1)
1957 END IF
1958 IF (PRESENT(dist1)) THEN
1959 IF (any(map1_2d == 2)) THEN
1960 i = minloc(map1_2d, dim=1, mask=map1_2d == 2) ! i is location of idim in map1_2d
1961 CALL get_ith_array(dist1, i, nd_dist_2)
1962 END IF
1963 END IF
1964
1965 IF (PRESENT(dist2)) THEN
1966 IF (any(map2_2d == 2)) THEN
1967 i = minloc(map2_2d, dim=1, mask=map2_2d == 2) ! i is location of idim in map2_2d
1968 CALL get_ith_array(dist2, i, nd_dist_2)
1969 END IF
1970 END IF
1971
1972 IF (.NOT. ALLOCATED(nd_dist_2)) THEN
1973 ALLOCATE (nd_dist_2(SIZE(blk_sizes_2)))
1974 CALL dbt_default_distvec(SIZE(blk_sizes_2), pdims(2), blk_sizes_2, nd_dist_2)
1975 END IF
1976 IF (PRESENT(dist1)) THEN
1977 IF (any(map1_2d == 3)) THEN
1978 i = minloc(map1_2d, dim=1, mask=map1_2d == 3) ! i is location of idim in map1_2d
1979 CALL get_ith_array(dist1, i, nd_dist_3)
1980 END IF
1981 END IF
1982
1983 IF (PRESENT(dist2)) THEN
1984 IF (any(map2_2d == 3)) THEN
1985 i = minloc(map2_2d, dim=1, mask=map2_2d == 3) ! i is location of idim in map2_2d
1986 CALL get_ith_array(dist2, i, nd_dist_3)
1987 END IF
1988 END IF
1989
1990 IF (.NOT. ALLOCATED(nd_dist_3)) THEN
1991 ALLOCATE (nd_dist_3(SIZE(blk_sizes_3)))
1992 CALL dbt_default_distvec(SIZE(blk_sizes_3), pdims(3), blk_sizes_3, nd_dist_3)
1993 END IF
1994 CALL dbt_distribution_new_expert(dist, comm_nd, map1_2d, map2_2d, &
1995 nd_dist_1, nd_dist_2, nd_dist_3, own_comm=.true.)
1996 END IF
1997 IF (ndims_tensor(tensor_in) == 4) THEN
1998 IF (PRESENT(dist1)) THEN
1999 IF (any(map1_2d == 1)) THEN
2000 i = minloc(map1_2d, dim=1, mask=map1_2d == 1) ! i is location of idim in map1_2d
2001 CALL get_ith_array(dist1, i, nd_dist_1)
2002 END IF
2003 END IF
2004
2005 IF (PRESENT(dist2)) THEN
2006 IF (any(map2_2d == 1)) THEN
2007 i = minloc(map2_2d, dim=1, mask=map2_2d == 1) ! i is location of idim in map2_2d
2008 CALL get_ith_array(dist2, i, nd_dist_1)
2009 END IF
2010 END IF
2011
2012 IF (.NOT. ALLOCATED(nd_dist_1)) THEN
2013 ALLOCATE (nd_dist_1(SIZE(blk_sizes_1)))
2014 CALL dbt_default_distvec(SIZE(blk_sizes_1), pdims(1), blk_sizes_1, nd_dist_1)
2015 END IF
2016 IF (PRESENT(dist1)) THEN
2017 IF (any(map1_2d == 2)) THEN
2018 i = minloc(map1_2d, dim=1, mask=map1_2d == 2) ! i is location of idim in map1_2d
2019 CALL get_ith_array(dist1, i, nd_dist_2)
2020 END IF
2021 END IF
2022
2023 IF (PRESENT(dist2)) THEN
2024 IF (any(map2_2d == 2)) THEN
2025 i = minloc(map2_2d, dim=1, mask=map2_2d == 2) ! i is location of idim in map2_2d
2026 CALL get_ith_array(dist2, i, nd_dist_2)
2027 END IF
2028 END IF
2029
2030 IF (.NOT. ALLOCATED(nd_dist_2)) THEN
2031 ALLOCATE (nd_dist_2(SIZE(blk_sizes_2)))
2032 CALL dbt_default_distvec(SIZE(blk_sizes_2), pdims(2), blk_sizes_2, nd_dist_2)
2033 END IF
2034 IF (PRESENT(dist1)) THEN
2035 IF (any(map1_2d == 3)) THEN
2036 i = minloc(map1_2d, dim=1, mask=map1_2d == 3) ! i is location of idim in map1_2d
2037 CALL get_ith_array(dist1, i, nd_dist_3)
2038 END IF
2039 END IF
2040
2041 IF (PRESENT(dist2)) THEN
2042 IF (any(map2_2d == 3)) THEN
2043 i = minloc(map2_2d, dim=1, mask=map2_2d == 3) ! i is location of idim in map2_2d
2044 CALL get_ith_array(dist2, i, nd_dist_3)
2045 END IF
2046 END IF
2047
2048 IF (.NOT. ALLOCATED(nd_dist_3)) THEN
2049 ALLOCATE (nd_dist_3(SIZE(blk_sizes_3)))
2050 CALL dbt_default_distvec(SIZE(blk_sizes_3), pdims(3), blk_sizes_3, nd_dist_3)
2051 END IF
2052 IF (PRESENT(dist1)) THEN
2053 IF (any(map1_2d == 4)) THEN
2054 i = minloc(map1_2d, dim=1, mask=map1_2d == 4) ! i is location of idim in map1_2d
2055 CALL get_ith_array(dist1, i, nd_dist_4)
2056 END IF
2057 END IF
2058
2059 IF (PRESENT(dist2)) THEN
2060 IF (any(map2_2d == 4)) THEN
2061 i = minloc(map2_2d, dim=1, mask=map2_2d == 4) ! i is location of idim in map2_2d
2062 CALL get_ith_array(dist2, i, nd_dist_4)
2063 END IF
2064 END IF
2065
2066 IF (.NOT. ALLOCATED(nd_dist_4)) THEN
2067 ALLOCATE (nd_dist_4(SIZE(blk_sizes_4)))
2068 CALL dbt_default_distvec(SIZE(blk_sizes_4), pdims(4), blk_sizes_4, nd_dist_4)
2069 END IF
2070 CALL dbt_distribution_new_expert(dist, comm_nd, map1_2d, map2_2d, &
2071 nd_dist_1, nd_dist_2, nd_dist_3, nd_dist_4, own_comm=.true.)
2072 END IF
2073
2074 IF (ndims_tensor(tensor_in) == 2) THEN
2075 CALL dbt_create(tensor_out, name_tmp, dist, map1_2d, map2_2d, &
2076 blk_sizes_1, blk_sizes_2)
2077 END IF
2078 IF (ndims_tensor(tensor_in) == 3) THEN
2079 CALL dbt_create(tensor_out, name_tmp, dist, map1_2d, map2_2d, &
2080 blk_sizes_1, blk_sizes_2, blk_sizes_3)
2081 END IF
2082 IF (ndims_tensor(tensor_in) == 4) THEN
2083 CALL dbt_create(tensor_out, name_tmp, dist, map1_2d, map2_2d, &
2084 blk_sizes_1, blk_sizes_2, blk_sizes_3, blk_sizes_4)
2085 END IF
2086
2087 IF (PRESENT(nodata)) THEN
2088 nodata_prv = nodata
2089 ELSE
2090 nodata_prv = .false.
2091 END IF
2092
2093 IF (.NOT. nodata_prv) CALL dbt_copy_expert(tensor_in, tensor_out, move_data=move_data, bounds=bounds)
2094 CALL dbt_distribution_destroy(dist)
2095
2096 CALL timestop(handle)
2097 END SUBROUTINE dbt_remap
2098
2099! **************************************************************************************************
2100!> \brief Align index with data
2101!> \param order permutation resulting from alignment
2102!> \author Patrick Seewald
2103! **************************************************************************************************
2104 SUBROUTINE dbt_align_index(tensor_in, tensor_out, order)
2105 TYPE(dbt_type), INTENT(INOUT) :: tensor_in
2106 TYPE(dbt_type), INTENT(OUT) :: tensor_out
2107 INTEGER, DIMENSION(ndims_matrix_row(tensor_in)) :: map1_2d
2108 INTEGER, DIMENSION(ndims_matrix_column(tensor_in)) :: map2_2d
2109 INTEGER, DIMENSION(ndims_tensor(tensor_in)), &
2110 INTENT(OUT), OPTIONAL :: order
2111 INTEGER, DIMENSION(ndims_tensor(tensor_in)) :: order_prv
2112 CHARACTER(LEN=*), PARAMETER :: routinen = 'dbt_align_index'
2113 INTEGER :: handle
2114
2115 CALL timeset(routinen, handle)
2116
2117 CALL dbt_get_mapping_info(tensor_in%nd_index_blk, map1_2d=map1_2d, map2_2d=map2_2d)
2118 order_prv = dbt_inverse_order([map1_2d, map2_2d])
2119 CALL dbt_permute_index(tensor_in, tensor_out, order=order_prv)
2120
2121 IF (PRESENT(order)) order = order_prv
2122
2123 CALL timestop(handle)
2124 END SUBROUTINE dbt_align_index
2125
2126! **************************************************************************************************
2127!> \brief Create new tensor by reordering index, data is copied exactly (shallow copy)
2128!> \author Patrick Seewald
2129! **************************************************************************************************
2130 SUBROUTINE dbt_permute_index(tensor_in, tensor_out, order)
2131 TYPE(dbt_type), INTENT(INOUT) :: tensor_in
2132 TYPE(dbt_type), INTENT(OUT) :: tensor_out
2133 INTEGER, DIMENSION(ndims_tensor(tensor_in)), &
2134 INTENT(IN) :: order
2135
2136 TYPE(nd_to_2d_mapping) :: nd_index_blk_rs, nd_index_rs
2137 CHARACTER(LEN=*), PARAMETER :: routinen = 'dbt_permute_index'
2138 INTEGER :: handle
2139 INTEGER :: ndims
2140
2141 CALL timeset(routinen, handle)
2142
2143 ndims = ndims_tensor(tensor_in)
2144
2145 CALL permute_index(tensor_in%nd_index, nd_index_rs, order)
2146 CALL permute_index(tensor_in%nd_index_blk, nd_index_blk_rs, order)
2147 CALL permute_index(tensor_in%pgrid%nd_index_grid, tensor_out%pgrid%nd_index_grid, order)
2148
2149 tensor_out%matrix_rep => tensor_in%matrix_rep
2150 tensor_out%owns_matrix = .false.
2151
2152 tensor_out%nd_index = nd_index_rs
2153 tensor_out%nd_index_blk = nd_index_blk_rs
2154 tensor_out%pgrid%mp_comm_2d = tensor_in%pgrid%mp_comm_2d
2155 IF (ALLOCATED(tensor_in%pgrid%tas_split_info)) THEN
2156 ALLOCATE (tensor_out%pgrid%tas_split_info, source=tensor_in%pgrid%tas_split_info)
2157 END IF
2158 tensor_out%refcount => tensor_in%refcount
2159 CALL dbt_hold(tensor_out)
2160
2161 CALL reorder_arrays(tensor_in%blk_sizes, tensor_out%blk_sizes, order)
2162 CALL reorder_arrays(tensor_in%blk_offsets, tensor_out%blk_offsets, order)
2163 CALL reorder_arrays(tensor_in%nd_dist, tensor_out%nd_dist, order)
2164 CALL reorder_arrays(tensor_in%blks_local, tensor_out%blks_local, order)
2165 ALLOCATE (tensor_out%nblks_local(ndims))
2166 ALLOCATE (tensor_out%nfull_local(ndims))
2167 tensor_out%nblks_local(order) = tensor_in%nblks_local(:)
2168 tensor_out%nfull_local(order) = tensor_in%nfull_local(:)
2169 tensor_out%name = tensor_in%name
2170 tensor_out%valid = .true.
2171
2172 IF (ALLOCATED(tensor_in%contraction_storage)) THEN
2173 ALLOCATE (tensor_out%contraction_storage, source=tensor_in%contraction_storage)
2174 CALL destroy_array_list(tensor_out%contraction_storage%batch_ranges)
2175 CALL reorder_arrays(tensor_in%contraction_storage%batch_ranges, tensor_out%contraction_storage%batch_ranges, order)
2176 END IF
2177
2178 CALL timestop(handle)
2179 END SUBROUTINE dbt_permute_index
2180
2181! **************************************************************************************************
2182!> \brief Map contraction bounds to bounds referring to tensor indices
2183!> see dbt_contract for docu of dummy arguments
2184!> \param bounds_t1 bounds mapped to tensor_1
2185!> \param bounds_t2 bounds mapped to tensor_2
2186!> \param do_crop_1 whether tensor 1 should be cropped
2187!> \param do_crop_2 whether tensor 2 should be cropped
2188!> \author Patrick Seewald
2189! **************************************************************************************************
2190 SUBROUTINE dbt_map_bounds_to_tensors(tensor_1, tensor_2, &
2191 contract_1, notcontract_1, &
2192 contract_2, notcontract_2, &
2193 bounds_t1, bounds_t2, &
2194 bounds_1, bounds_2, bounds_3, &
2195 do_crop_1, do_crop_2)
2196
2197 TYPE(dbt_type), INTENT(IN) :: tensor_1, tensor_2
2198 INTEGER, DIMENSION(:), INTENT(IN) :: contract_1, contract_2, &
2199 notcontract_1, notcontract_2
2200 INTEGER, DIMENSION(2, ndims_tensor(tensor_1)), &
2201 INTENT(OUT) :: bounds_t1
2202 INTEGER, DIMENSION(2, ndims_tensor(tensor_2)), &
2203 INTENT(OUT) :: bounds_t2
2204 INTEGER, DIMENSION(2, SIZE(contract_1)), &
2205 INTENT(IN), OPTIONAL :: bounds_1
2206 INTEGER, DIMENSION(2, SIZE(notcontract_1)), &
2207 INTENT(IN), OPTIONAL :: bounds_2
2208 INTEGER, DIMENSION(2, SIZE(notcontract_2)), &
2209 INTENT(IN), OPTIONAL :: bounds_3
2210 LOGICAL, INTENT(OUT), OPTIONAL :: do_crop_1, do_crop_2
2211 LOGICAL, DIMENSION(2) :: do_crop
2212
2213 do_crop = .false.
2214
2215 bounds_t1(1, :) = 1
2216 CALL dbt_get_info(tensor_1, nfull_total=bounds_t1(2, :))
2217
2218 bounds_t2(1, :) = 1
2219 CALL dbt_get_info(tensor_2, nfull_total=bounds_t2(2, :))
2220
2221 IF (PRESENT(bounds_1)) THEN
2222 bounds_t1(:, contract_1) = bounds_1
2223 do_crop(1) = .true.
2224 bounds_t2(:, contract_2) = bounds_1
2225 do_crop(2) = .true.
2226 END IF
2227
2228 IF (PRESENT(bounds_2)) THEN
2229 bounds_t1(:, notcontract_1) = bounds_2
2230 do_crop(1) = .true.
2231 END IF
2232
2233 IF (PRESENT(bounds_3)) THEN
2234 bounds_t2(:, notcontract_2) = bounds_3
2235 do_crop(2) = .true.
2236 END IF
2237
2238 IF (PRESENT(do_crop_1)) do_crop_1 = do_crop(1)
2239 IF (PRESENT(do_crop_2)) do_crop_2 = do_crop(2)
2240
2241 END SUBROUTINE dbt_map_bounds_to_tensors
2242
2243! **************************************************************************************************
2244!> \brief print tensor contraction indices in a human readable way
2245!> \param indchar1 characters printed for index of tensor 1
2246!> \param indchar2 characters printed for index of tensor 2
2247!> \param indchar3 characters printed for index of tensor 3
2248!> \param unit_nr output unit
2249!> \author Patrick Seewald
2250! **************************************************************************************************
2251 SUBROUTINE dbt_print_contraction_index(tensor_1, indchar1, tensor_2, indchar2, tensor_3, indchar3, unit_nr)
2252 TYPE(dbt_type), INTENT(IN) :: tensor_1, tensor_2, tensor_3
2253 CHARACTER(LEN=1), DIMENSION(ndims_tensor(tensor_1)), INTENT(IN) :: indchar1
2254 CHARACTER(LEN=1), DIMENSION(ndims_tensor(tensor_2)), INTENT(IN) :: indchar2
2255 CHARACTER(LEN=1), DIMENSION(ndims_tensor(tensor_3)), INTENT(IN) :: indchar3
2256 INTEGER, INTENT(IN) :: unit_nr
2257 INTEGER, DIMENSION(ndims_matrix_row(tensor_1)) :: map11
2258 INTEGER, DIMENSION(ndims_matrix_column(tensor_1)) :: map12
2259 INTEGER, DIMENSION(ndims_matrix_row(tensor_2)) :: map21
2260 INTEGER, DIMENSION(ndims_matrix_column(tensor_2)) :: map22
2261 INTEGER, DIMENSION(ndims_matrix_row(tensor_3)) :: map31
2262 INTEGER, DIMENSION(ndims_matrix_column(tensor_3)) :: map32
2263 INTEGER :: ichar1, ichar2, ichar3, unit_nr_prv
2264
2265 unit_nr_prv = prep_output_unit(unit_nr)
2266
2267 IF (unit_nr_prv /= 0) THEN
2268 CALL dbt_get_mapping_info(tensor_1%nd_index_blk, map1_2d=map11, map2_2d=map12)
2269 CALL dbt_get_mapping_info(tensor_2%nd_index_blk, map1_2d=map21, map2_2d=map22)
2270 CALL dbt_get_mapping_info(tensor_3%nd_index_blk, map1_2d=map31, map2_2d=map32)
2271 END IF
2272
2273 IF (unit_nr_prv > 0) THEN
2274 WRITE (unit_nr_prv, '(T2,A)') "INDEX INFO"
2275 WRITE (unit_nr_prv, '(T15,A)', advance='no') "tensor index: ("
2276 DO ichar1 = 1, SIZE(indchar1)
2277 WRITE (unit_nr_prv, '(A1)', advance='no') indchar1(ichar1)
2278 END DO
2279 WRITE (unit_nr_prv, '(A)', advance='no') ") x ("
2280 DO ichar2 = 1, SIZE(indchar2)
2281 WRITE (unit_nr_prv, '(A1)', advance='no') indchar2(ichar2)
2282 END DO
2283 WRITE (unit_nr_prv, '(A)', advance='no') ") = ("
2284 DO ichar3 = 1, SIZE(indchar3)
2285 WRITE (unit_nr_prv, '(A1)', advance='no') indchar3(ichar3)
2286 END DO
2287 WRITE (unit_nr_prv, '(A)') ")"
2288
2289 WRITE (unit_nr_prv, '(T15,A)', advance='no') "matrix index: ("
2290 DO ichar1 = 1, SIZE(map11)
2291 WRITE (unit_nr_prv, '(A1)', advance='no') indchar1(map11(ichar1))
2292 END DO
2293 WRITE (unit_nr_prv, '(A1)', advance='no') "|"
2294 DO ichar1 = 1, SIZE(map12)
2295 WRITE (unit_nr_prv, '(A1)', advance='no') indchar1(map12(ichar1))
2296 END DO
2297 WRITE (unit_nr_prv, '(A)', advance='no') ") x ("
2298 DO ichar2 = 1, SIZE(map21)
2299 WRITE (unit_nr_prv, '(A1)', advance='no') indchar2(map21(ichar2))
2300 END DO
2301 WRITE (unit_nr_prv, '(A1)', advance='no') "|"
2302 DO ichar2 = 1, SIZE(map22)
2303 WRITE (unit_nr_prv, '(A1)', advance='no') indchar2(map22(ichar2))
2304 END DO
2305 WRITE (unit_nr_prv, '(A)', advance='no') ") = ("
2306 DO ichar3 = 1, SIZE(map31)
2307 WRITE (unit_nr_prv, '(A1)', advance='no') indchar3(map31(ichar3))
2308 END DO
2309 WRITE (unit_nr_prv, '(A1)', advance='no') "|"
2310 DO ichar3 = 1, SIZE(map32)
2311 WRITE (unit_nr_prv, '(A1)', advance='no') indchar3(map32(ichar3))
2312 END DO
2313 WRITE (unit_nr_prv, '(A)') ")"
2314 END IF
2315
2316 END SUBROUTINE dbt_print_contraction_index
2317
2318! **************************************************************************************************
2319!> \brief Initialize batched contraction for this tensor.
2320!>
2321!> Explanation: A batched contraction is a contraction performed in several consecutive steps
2322!> by specification of bounds in dbt_contract. This can be used to reduce memory by
2323!> a large factor. The routines dbt_batched_contract_init and
2324!> dbt_batched_contract_finalize should be called to define the scope of a batched
2325!> contraction as this enables important optimizations (adapting communication scheme to
2326!> batches and adapting process grid to multiplication algorithm). The routines
2327!> dbt_batched_contract_init and dbt_batched_contract_finalize must be
2328!> called before the first and after the last contraction step on all 3 tensors.
2329!>
2330!> Requirements:
2331!> - the tensors are in a compatible matrix layout (see documentation of
2332!> `dbt_contract`, note 2 & 3). If they are not, process grid optimizations are
2333!> disabled and a warning is issued.
2334!> - within the scope of a batched contraction, it is not allowed to access or change tensor
2335!> data except by calling dbt_contract, dbt_copy, or the tensor-array form of dbt_copy_indexed.
2336!> - the bounds affecting indices of the smallest tensor must not change in the course of a
2337!> batched contraction (todo: get rid of this requirement).
2338!>
2339!> Side effects:
2340!> - the parallel layout (process grid and distribution) of all tensors may change. In order
2341!> to disable the process grid optimization including this side effect, call this routine
2342!> only on the smallest of the 3 tensors.
2343!>
2344!> \note
2345!> Note 1: for an example of batched contraction see `examples/dbt_example.F`.
2346!> (todo: the example is outdated and should be updated).
2347!>
2348!> Note 2: it is meaningful to use this feature if the contraction consists of one batch only
2349!> but if multiple contractions involving the same 3 tensors are performed
2350!> (batched_contract_init and batched_contract_finalize must then be called before/after each
2351!> contraction call). The process grid is then optimized after the first contraction
2352!> and future contraction may profit from this optimization.
2353!>
2354!> \param batch_range_i refers to the ith tensor dimension and contains all block indices starting
2355!> a new range. The size should be the number of ranges plus one, the last
2356!> element being the block index plus one of the last block in the last range.
2357!> For internal load balancing optimizations, optionally specify the index
2358!> ranges of batched contraction.
2359!> \author Patrick Seewald
2360! **************************************************************************************************
2361 SUBROUTINE dbt_batched_contract_init(tensor, batch_range_1, batch_range_2, batch_range_3, batch_range_4)
2362 TYPE(dbt_type), INTENT(INOUT) :: tensor
2363 INTEGER, DIMENSION(:), OPTIONAL, INTENT(IN) :: batch_range_1, batch_range_2, batch_range_3, batch_range_4
2364 INTEGER, DIMENSION(ndims_tensor(tensor)) :: tdims
2365 INTEGER, DIMENSION(:), ALLOCATABLE :: batch_range_prv_1, batch_range_prv_2, batch_range_prv_3,&
2366 & batch_range_prv_4
2367 LOGICAL :: static_range
2368
2369 CALL dbt_get_info(tensor, nblks_total=tdims)
2370
2371 static_range = .true.
2372 IF (ndims_tensor(tensor) >= 1) THEN
2373 IF (PRESENT(batch_range_1)) THEN
2374 ALLOCATE (batch_range_prv_1, source=batch_range_1)
2375 static_range = .false.
2376 ELSE
2377 ALLOCATE (batch_range_prv_1(2))
2378 batch_range_prv_1(1) = 1
2379 batch_range_prv_1(2) = tdims(1) + 1
2380 END IF
2381 END IF
2382 IF (ndims_tensor(tensor) >= 2) THEN
2383 IF (PRESENT(batch_range_2)) THEN
2384 ALLOCATE (batch_range_prv_2, source=batch_range_2)
2385 static_range = .false.
2386 ELSE
2387 ALLOCATE (batch_range_prv_2(2))
2388 batch_range_prv_2(1) = 1
2389 batch_range_prv_2(2) = tdims(2) + 1
2390 END IF
2391 END IF
2392 IF (ndims_tensor(tensor) >= 3) THEN
2393 IF (PRESENT(batch_range_3)) THEN
2394 ALLOCATE (batch_range_prv_3, source=batch_range_3)
2395 static_range = .false.
2396 ELSE
2397 ALLOCATE (batch_range_prv_3(2))
2398 batch_range_prv_3(1) = 1
2399 batch_range_prv_3(2) = tdims(3) + 1
2400 END IF
2401 END IF
2402 IF (ndims_tensor(tensor) >= 4) THEN
2403 IF (PRESENT(batch_range_4)) THEN
2404 ALLOCATE (batch_range_prv_4, source=batch_range_4)
2405 static_range = .false.
2406 ELSE
2407 ALLOCATE (batch_range_prv_4(2))
2408 batch_range_prv_4(1) = 1
2409 batch_range_prv_4(2) = tdims(4) + 1
2410 END IF
2411 END IF
2412
2413 ALLOCATE (tensor%contraction_storage)
2414 tensor%contraction_storage%static = static_range
2415 IF (static_range) THEN
2416 CALL dbt_tas_batched_mm_init(tensor%matrix_rep)
2417 END IF
2418 tensor%contraction_storage%nsplit_avg = 0.0_dp
2419 tensor%contraction_storage%ibatch = 0
2420
2421 IF (ndims_tensor(tensor) == 1) THEN
2422 CALL create_array_list(tensor%contraction_storage%batch_ranges, 1, &
2423 batch_range_prv_1)
2424 END IF
2425 IF (ndims_tensor(tensor) == 2) THEN
2426 CALL create_array_list(tensor%contraction_storage%batch_ranges, 2, &
2427 batch_range_prv_1, batch_range_prv_2)
2428 END IF
2429 IF (ndims_tensor(tensor) == 3) THEN
2430 CALL create_array_list(tensor%contraction_storage%batch_ranges, 3, &
2431 batch_range_prv_1, batch_range_prv_2, batch_range_prv_3)
2432 END IF
2433 IF (ndims_tensor(tensor) == 4) THEN
2434 CALL create_array_list(tensor%contraction_storage%batch_ranges, 4, &
2435 batch_range_prv_1, batch_range_prv_2, batch_range_prv_3, batch_range_prv_4)
2436 END IF
2437
2438 END SUBROUTINE dbt_batched_contract_init
2439
2440! **************************************************************************************************
2441!> \brief finalize batched contraction. This performs all communication that has been postponed in
2442!> the contraction calls.
2443!> \author Patrick Seewald
2444! **************************************************************************************************
2445 SUBROUTINE dbt_batched_contract_finalize(tensor, unit_nr)
2446 TYPE(dbt_type), INTENT(INOUT) :: tensor
2447 INTEGER, INTENT(IN), OPTIONAL :: unit_nr
2448 LOGICAL :: do_write
2449 INTEGER :: unit_nr_prv, handle
2450
2451 CALL timeset("dbt_total", handle)
2452 unit_nr_prv = prep_output_unit(unit_nr)
2453
2454 do_write = .false.
2455
2456 IF (tensor%contraction_storage%static) THEN
2457 IF (tensor%matrix_rep%do_batched > 0) THEN
2458 IF (tensor%matrix_rep%mm_storage%batched_out) do_write = .true.
2459 END IF
2460 CALL dbt_tas_batched_mm_finalize(tensor%matrix_rep)
2461 END IF
2462
2463 IF (do_write .AND. unit_nr_prv /= 0) THEN
2464 IF (unit_nr_prv > 0) THEN
2465 WRITE (unit_nr_prv, "(T2,A)") &
2466 "FINALIZING BATCHED PROCESSING OF MATMUL"
2467 END IF
2468 CALL dbt_write_tensor_info(tensor, unit_nr_prv)
2469 CALL dbt_write_tensor_dist(tensor, unit_nr_prv)
2470 END IF
2471
2472 CALL destroy_array_list(tensor%contraction_storage%batch_ranges)
2473 DEALLOCATE (tensor%contraction_storage)
2474 CALL timestop(handle)
2475
2476 END SUBROUTINE dbt_batched_contract_finalize
2477
2478! **************************************************************************************************
2479!> \brief change the process grid of a tensor
2480!> \param nodata optionally don't copy the tensor data (then tensor is empty on returned)
2481!> \param batch_range_i refers to the ith tensor dimension and contains all block indices starting
2482!> a new range. The size should be the number of ranges plus one, the last
2483!> element being the block index plus one of the last block in the last range.
2484!> For internal load balancing optimizations, optionally specify the index
2485!> ranges of batched contraction.
2486!> \author Patrick Seewald
2487! **************************************************************************************************
2488 SUBROUTINE dbt_change_pgrid(tensor, pgrid, batch_range_1, batch_range_2, batch_range_3, batch_range_4, &
2489 nodata, pgrid_changed, unit_nr)
2490 TYPE(dbt_type), INTENT(INOUT) :: tensor
2491 TYPE(dbt_pgrid_type), INTENT(IN) :: pgrid
2492 INTEGER, DIMENSION(:), OPTIONAL, INTENT(IN) :: batch_range_1, batch_range_2, batch_range_3, batch_range_4
2493 !!
2494 LOGICAL, INTENT(IN), OPTIONAL :: nodata
2495 LOGICAL, INTENT(OUT), OPTIONAL :: pgrid_changed
2496 INTEGER, INTENT(IN), OPTIONAL :: unit_nr
2497 CHARACTER(LEN=*), PARAMETER :: routinen = 'dbt_change_pgrid'
2498 CHARACTER(default_string_length) :: name
2499 INTEGER :: handle
2500 INTEGER, ALLOCATABLE, DIMENSION(:) :: bs_1, bs_2, bs_3, bs_4, &
2501 dist_1, dist_2, dist_3, dist_4
2502 INTEGER, DIMENSION(ndims_tensor(tensor)) :: pcoord, pcoord_ref, pdims, pdims_ref, &
2503 tdims
2504 TYPE(dbt_type) :: t_tmp
2505 TYPE(dbt_distribution_type) :: dist
2506 INTEGER, DIMENSION(ndims_matrix_row(tensor)) :: map1
2507 INTEGER, &
2508 DIMENSION(ndims_matrix_column(tensor)) :: map2
2509 LOGICAL, DIMENSION(ndims_tensor(tensor)) :: mem_aware
2510 INTEGER, DIMENSION(ndims_tensor(tensor)) :: nbatch
2511 INTEGER :: ind1, ind2, batch_size, ibatch
2512
2513 IF (PRESENT(pgrid_changed)) pgrid_changed = .false.
2514 CALL mp_environ_pgrid(pgrid, pdims, pcoord)
2515 CALL mp_environ_pgrid(tensor%pgrid, pdims_ref, pcoord_ref)
2516
2517 IF (all(pdims == pdims_ref)) THEN
2518 IF (ALLOCATED(pgrid%tas_split_info) .AND. ALLOCATED(tensor%pgrid%tas_split_info)) THEN
2519 IF (pgrid%tas_split_info%ngroup == tensor%pgrid%tas_split_info%ngroup) THEN
2520 RETURN
2521 END IF
2522 END IF
2523 END IF
2524
2525 CALL timeset(routinen, handle)
2526
2527 IF (ndims_tensor(tensor) >= 1) THEN
2528 mem_aware(1) = PRESENT(batch_range_1)
2529 IF (mem_aware(1)) nbatch(1) = SIZE(batch_range_1) - 1
2530 END IF
2531 IF (ndims_tensor(tensor) >= 2) THEN
2532 mem_aware(2) = PRESENT(batch_range_2)
2533 IF (mem_aware(2)) nbatch(2) = SIZE(batch_range_2) - 1
2534 END IF
2535 IF (ndims_tensor(tensor) >= 3) THEN
2536 mem_aware(3) = PRESENT(batch_range_3)
2537 IF (mem_aware(3)) nbatch(3) = SIZE(batch_range_3) - 1
2538 END IF
2539 IF (ndims_tensor(tensor) >= 4) THEN
2540 mem_aware(4) = PRESENT(batch_range_4)
2541 IF (mem_aware(4)) nbatch(4) = SIZE(batch_range_4) - 1
2542 END IF
2543
2544 CALL dbt_get_info(tensor, nblks_total=tdims, name=name)
2545
2546 IF (ndims_tensor(tensor) >= 1) THEN
2547 ALLOCATE (bs_1(dbt_nblks_total(tensor, 1)))
2548 CALL get_ith_array(tensor%blk_sizes, 1, bs_1)
2549 ALLOCATE (dist_1(tdims(1)))
2550 dist_1 = 0
2551 IF (mem_aware(1)) THEN
2552 DO ibatch = 1, nbatch(1)
2553 ind1 = batch_range_1(ibatch)
2554 ind2 = batch_range_1(ibatch + 1) - 1
2555 batch_size = ind2 - ind1 + 1
2556 CALL dbt_default_distvec(batch_size, pdims(1), &
2557 bs_1(ind1:ind2), dist_1(ind1:ind2))
2558 END DO
2559 ELSE
2560 CALL dbt_default_distvec(tdims(1), pdims(1), bs_1, dist_1)
2561 END IF
2562 END IF
2563 IF (ndims_tensor(tensor) >= 2) THEN
2564 ALLOCATE (bs_2(dbt_nblks_total(tensor, 2)))
2565 CALL get_ith_array(tensor%blk_sizes, 2, bs_2)
2566 ALLOCATE (dist_2(tdims(2)))
2567 dist_2 = 0
2568 IF (mem_aware(2)) THEN
2569 DO ibatch = 1, nbatch(2)
2570 ind1 = batch_range_2(ibatch)
2571 ind2 = batch_range_2(ibatch + 1) - 1
2572 batch_size = ind2 - ind1 + 1
2573 CALL dbt_default_distvec(batch_size, pdims(2), &
2574 bs_2(ind1:ind2), dist_2(ind1:ind2))
2575 END DO
2576 ELSE
2577 CALL dbt_default_distvec(tdims(2), pdims(2), bs_2, dist_2)
2578 END IF
2579 END IF
2580 IF (ndims_tensor(tensor) >= 3) THEN
2581 ALLOCATE (bs_3(dbt_nblks_total(tensor, 3)))
2582 CALL get_ith_array(tensor%blk_sizes, 3, bs_3)
2583 ALLOCATE (dist_3(tdims(3)))
2584 dist_3 = 0
2585 IF (mem_aware(3)) THEN
2586 DO ibatch = 1, nbatch(3)
2587 ind1 = batch_range_3(ibatch)
2588 ind2 = batch_range_3(ibatch + 1) - 1
2589 batch_size = ind2 - ind1 + 1
2590 CALL dbt_default_distvec(batch_size, pdims(3), &
2591 bs_3(ind1:ind2), dist_3(ind1:ind2))
2592 END DO
2593 ELSE
2594 CALL dbt_default_distvec(tdims(3), pdims(3), bs_3, dist_3)
2595 END IF
2596 END IF
2597 IF (ndims_tensor(tensor) >= 4) THEN
2598 ALLOCATE (bs_4(dbt_nblks_total(tensor, 4)))
2599 CALL get_ith_array(tensor%blk_sizes, 4, bs_4)
2600 ALLOCATE (dist_4(tdims(4)))
2601 dist_4 = 0
2602 IF (mem_aware(4)) THEN
2603 DO ibatch = 1, nbatch(4)
2604 ind1 = batch_range_4(ibatch)
2605 ind2 = batch_range_4(ibatch + 1) - 1
2606 batch_size = ind2 - ind1 + 1
2607 CALL dbt_default_distvec(batch_size, pdims(4), &
2608 bs_4(ind1:ind2), dist_4(ind1:ind2))
2609 END DO
2610 ELSE
2611 CALL dbt_default_distvec(tdims(4), pdims(4), bs_4, dist_4)
2612 END IF
2613 END IF
2614
2615 CALL dbt_get_mapping_info(tensor%nd_index_blk, map1_2d=map1, map2_2d=map2)
2616 IF (ndims_tensor(tensor) == 2) THEN
2617 CALL dbt_distribution_new(dist, pgrid, dist_1, dist_2)
2618 CALL dbt_create(t_tmp, name, dist, map1, map2, bs_1, bs_2)
2619 END IF
2620 IF (ndims_tensor(tensor) == 3) THEN
2621 CALL dbt_distribution_new(dist, pgrid, dist_1, dist_2, dist_3)
2622 CALL dbt_create(t_tmp, name, dist, map1, map2, bs_1, bs_2, bs_3)
2623 END IF
2624 IF (ndims_tensor(tensor) == 4) THEN
2625 CALL dbt_distribution_new(dist, pgrid, dist_1, dist_2, dist_3, dist_4)
2626 CALL dbt_create(t_tmp, name, dist, map1, map2, bs_1, bs_2, bs_3, bs_4)
2627 END IF
2628 CALL dbt_distribution_destroy(dist)
2629
2630 IF (PRESENT(nodata)) THEN
2631 IF (.NOT. nodata) CALL dbt_copy_expert(tensor, t_tmp, move_data=.true.)
2632 ELSE
2633 CALL dbt_copy_expert(tensor, t_tmp, move_data=.true.)
2634 END IF
2635
2636 CALL dbt_copy_contraction_storage(tensor, t_tmp)
2637
2638 CALL dbt_destroy(tensor)
2639 tensor = t_tmp
2640
2641 IF (PRESENT(unit_nr)) THEN
2642 IF (unit_nr > 0) THEN
2643 WRITE (unit_nr, "(T2,A,1X,A)") "OPTIMIZED PGRID INFO FOR", trim(tensor%name)
2644 WRITE (unit_nr, "(T4,A,1X,3I6)") "process grid dimensions:", pdims
2645 CALL dbt_write_split_info(pgrid, unit_nr)
2646 END IF
2647 END IF
2648
2649 IF (PRESENT(pgrid_changed)) pgrid_changed = .true.
2650
2651 CALL timestop(handle)
2652 END SUBROUTINE dbt_change_pgrid
2653
2654! **************************************************************************************************
2655!> \brief map tensor to a new 2d process grid for the matrix representation.
2656!> \author Patrick Seewald
2657! **************************************************************************************************
2658 SUBROUTINE dbt_change_pgrid_2d(tensor, mp_comm, pdims, nodata, nsplit, dimsplit, pgrid_changed, unit_nr)
2659 TYPE(dbt_type), INTENT(INOUT) :: tensor
2660 TYPE(mp_cart_type), INTENT(IN) :: mp_comm
2661 INTEGER, DIMENSION(2), INTENT(IN), OPTIONAL :: pdims
2662 LOGICAL, INTENT(IN), OPTIONAL :: nodata
2663 INTEGER, INTENT(IN), OPTIONAL :: nsplit, dimsplit
2664 LOGICAL, INTENT(OUT), OPTIONAL :: pgrid_changed
2665 INTEGER, INTENT(IN), OPTIONAL :: unit_nr
2666 INTEGER, DIMENSION(ndims_matrix_row(tensor)) :: map1
2667 INTEGER, DIMENSION(ndims_matrix_column(tensor)) :: map2
2668 INTEGER, DIMENSION(ndims_tensor(tensor)) :: dims, nbatches
2669 TYPE(dbt_pgrid_type) :: pgrid
2670 INTEGER, DIMENSION(:), ALLOCATABLE :: batch_range_1, batch_range_2, batch_range_3, batch_range_4
2671 INTEGER, DIMENSION(:), ALLOCATABLE :: array
2672 INTEGER :: idim
2673
2674 CALL dbt_get_mapping_info(tensor%pgrid%nd_index_grid, map1_2d=map1, map2_2d=map2)
2675 CALL blk_dims_tensor(tensor, dims)
2676
2677 IF (ALLOCATED(tensor%contraction_storage)) THEN
2678 associate(batch_ranges => tensor%contraction_storage%batch_ranges)
2679 nbatches = sizes_of_arrays(tensor%contraction_storage%batch_ranges) - 1
2680 ! for good load balancing the process grid dimensions should be chosen adapted to the
2681 ! tensor dimenions. For batched contraction the tensor dimensions should be divided by
2682 ! the number of batches (number of index ranges).
2683 DO idim = 1, ndims_tensor(tensor)
2684 CALL get_ith_array(tensor%contraction_storage%batch_ranges, idim, array)
2685 dims(idim) = array(nbatches(idim) + 1) - array(1)
2686 DEALLOCATE (array)
2687 dims(idim) = dims(idim)/nbatches(idim)
2688 IF (dims(idim) <= 0) dims(idim) = 1
2689 END DO
2690 END associate
2691 END IF
2692
2693 pgrid = dbt_nd_mp_comm(mp_comm, map1, map2, pdims_2d=pdims, tdims=dims, nsplit=nsplit, dimsplit=dimsplit)
2694 IF (ALLOCATED(tensor%contraction_storage)) THEN
2695 IF (ndims_tensor(tensor) == 1) THEN
2696 CALL get_arrays(tensor%contraction_storage%batch_ranges, batch_range_1)
2697 CALL dbt_change_pgrid(tensor, pgrid, batch_range_1, &
2698 nodata=nodata, pgrid_changed=pgrid_changed, unit_nr=unit_nr)
2699 END IF
2700 IF (ndims_tensor(tensor) == 2) THEN
2701 CALL get_arrays(tensor%contraction_storage%batch_ranges, batch_range_1, batch_range_2)
2702 CALL dbt_change_pgrid(tensor, pgrid, batch_range_1, batch_range_2, &
2703 nodata=nodata, pgrid_changed=pgrid_changed, unit_nr=unit_nr)
2704 END IF
2705 IF (ndims_tensor(tensor) == 3) THEN
2706 CALL get_arrays(tensor%contraction_storage%batch_ranges, batch_range_1, batch_range_2, batch_range_3)
2707 CALL dbt_change_pgrid(tensor, pgrid, batch_range_1, batch_range_2, batch_range_3, &
2708 nodata=nodata, pgrid_changed=pgrid_changed, unit_nr=unit_nr)
2709 END IF
2710 IF (ndims_tensor(tensor) == 4) THEN
2711 CALL get_arrays(tensor%contraction_storage%batch_ranges, batch_range_1, batch_range_2, batch_range_3, batch_range_4)
2712 CALL dbt_change_pgrid(tensor, pgrid, batch_range_1, batch_range_2, batch_range_3, batch_range_4, &
2713 nodata=nodata, pgrid_changed=pgrid_changed, unit_nr=unit_nr)
2714 END IF
2715 ELSE
2716 CALL dbt_change_pgrid(tensor, pgrid, nodata=nodata, pgrid_changed=pgrid_changed, unit_nr=unit_nr)
2717 END IF
2718 CALL dbt_pgrid_destroy(pgrid)
2719
2720 END SUBROUTINE dbt_change_pgrid_2d
2721
2722END MODULE dbt_methods
static GRID_HOST_DEVICE int idx(const orbital a)
Return coset index of given orbital angular momentum.
struct tensor_ tensor
logical function, public dbcsr_has_symmetry(matrix)
...
logical function, public dbcsr_iterator_blocks_left(iterator)
...
subroutine, public dbcsr_iterator_stop(iterator)
...
subroutine, public dbcsr_desymmetrize(matrix_a, matrix_b)
...
subroutine, public dbcsr_iterator_next_block(iterator, row, column, block, block_number_argument_has_been_removed, row_size, col_size, row_offset, col_offset, transposed)
...
subroutine, public dbcsr_iterator_start(iterator, matrix, shared, dynamic, dynamic_byrows)
...
subroutine, public dbcsr_release(matrix)
...
subroutine, public dbcsr_clear(matrix)
...
subroutine, public dbcsr_put_block(matrix, row, col, block, summation)
...
subroutine, public dbm_clear(matrix)
Remove all blocks from given matrix, but does not release the underlying memory.
Definition dbm_api.F:531
Wrapper for allocating, copying and reshaping arrays.
Representation of arbitrary number of 1d integer arrays with arbitrary sizes. This is needed for gene...
subroutine, public get_arrays(list, data_1, data_2, data_3, data_4, i_selected)
Get all arrays contained in list.
subroutine, public create_array_list(list, ndata, data_1, data_2, data_3, data_4)
collects any number of arrays of different sizes into a single array (listcol_data),...
subroutine, public destroy_array_list(list)
destroy array list.
integer function, dimension(:), allocatable, public sizes_of_arrays(list)
sizes of arrays stored in list
type(array_list) function, public array_sublist(list, i_selected)
extract a subset of arrays
subroutine, public reorder_arrays(list_in, list_out, order)
reorder array list.
logical function, public check_equal(list1, list2)
check whether two array lists are equal
Methods to operate on n-dimensional tensor blocks.
Definition dbt_block.F:12
elemental logical function, public checker_tr(row, column)
Determines whether a transpose must be applied.
Definition dbt_block.F:503
logical function, public dbt_iterator_blocks_left(iterator)
Generalization of block_iterator_blocks_left for tensors.
Definition dbt_block.F:202
subroutine, public destroy_block(block)
Definition dbt_block.F:485
pure integer function, public ndims_iterator(iterator)
Number of dimensions.
Definition dbt_block.F:151
subroutine, public dbt_iterator_stop(iterator)
Generalization of block_iterator_stop for tensors.
Definition dbt_block.F:139
subroutine, public dbt_iterator_start(iterator, tensor)
Generalization of block_iterator_start for tensors.
Definition dbt_block.F:126
subroutine, public dbt_iterator_next_block(iterator, ind_nd, blk_size, blk_offset)
iterate over nd blocks of an nd rank tensor, index only (blocks must be retrieved by calling dbt_get_...
Definition dbt_block.F:166
tensor index and mapping to DBM index
Definition dbt_index.F:12
pure integer function, public ndims_mapping_row(map)
how many tensor dimensions are mapped to matrix row
Definition dbt_index.F:141
pure integer function, dimension(size(order)), public dbt_inverse_order(order)
Invert order.
Definition dbt_index.F:410
pure integer function, public ndims_mapping(map)
Definition dbt_index.F:130
subroutine, public permute_index(map_in, map_out, order)
reorder tensor index (no data)
Definition dbt_index.F:423
pure subroutine, public dbt_get_mapping_info(map, ndim_nd, ndim1_2d, ndim2_2d, dims_2d_i8, dims_2d, dims_nd, dims1_2d, dims2_2d, map1_2d, map2_2d, map_nd, base, col_major)
get mapping info
Definition dbt_index.F:176
pure integer function, public ndims_mapping_column(map)
how many tensor dimensions are mapped to matrix column
Definition dbt_index.F:151
pure integer function, dimension(map%ndim_nd), public get_nd_indices_tensor(map, ind_in)
transform 2d index to nd index, using info from index mapping.
Definition dbt_index.F:368
DBT tensor Input / Output.
Definition dbt_io.F:12
subroutine, public dbt_write_tensor_info(tensor, unit_nr, full_info)
Write tensor global info: block dimensions, full dimensions and process grid dimensions.
Definition dbt_io.F:50
subroutine, public dbt_write_tensor_dist(tensor, unit_nr)
Write info on tensor distribution & load balance.
Definition dbt_io.F:161
subroutine, public dbt_write_split_info(pgrid, unit_nr)
Definition dbt_io.F:401
integer function, public prep_output_unit(unit_nr)
Definition dbt_io.F:413
DBT tensor framework for block-sparse tensor contraction. Representation of n-rank tensors as DBT tal...
Definition dbt_methods.F:16
subroutine, public dbt_copy(tensor_in, tensor_out, order, summation, bounds, move_data, unit_nr)
Copy tensor data. Redistributes tensor data according to distributions of target and source tensor....
subroutine, public dbt_batched_contract_finalize(tensor, unit_nr)
finalize batched contraction. This performs all communication that has been postponed in the contract...
subroutine, public dbt_library_init()
Initialize the DBT library (after dbm_library_init).
type(contract_stats_type), dimension(:), allocatable, save contract_stats
subroutine, public dbt_copy_matrix_to_tensor(matrix_in, tensor_out, summation)
copy matrix to tensor.
subroutine, public dbt_contract(alpha, tensor_1, tensor_2, beta, tensor_3, contract_1, notcontract_1, contract_2, notcontract_2, map_1, map_2, bounds_1, bounds_2, bounds_3, optimize_dist, pgrid_opt_1, pgrid_opt_2, pgrid_opt_3, filter_eps, flop, move_data, retain_sparsity, unit_nr, log_verbose)
Contract tensors by multiplying matrix representations. tensor_3(map_1, map_2) := alpha * tensor_1(no...
subroutine, public dbt_library_finalize()
Finalize the DBT library (before dbm_library_finalize), which releases its buffers.
subroutine, public dbt_library_print_stats(mpi_comm, output_unit)
Print per-contraction statistics (rank-local table, totals over all ranks).
subroutine, public dbt_copy_tensor_to_matrix(tensor_in, matrix_out, summation)
copy tensor to matrix
subroutine, public dbt_clear_mempools()
Release the buffers that persist across calls (as dbcsr_clear_mempools), e.g., once a phase with larg...
subroutine, public dbt_batched_contract_init(tensor, batch_range_1, batch_range_2, batch_range_3, batch_range_4)
Initialize batched contraction for this tensor.
Routines to reshape / redistribute tensors.
subroutine, public dbt_reshape_release()
Release the communication buffers that persist across calls of dbt_reshape.
subroutine, public dbt_reshape(tensor_in, tensor_out, summation, move_data, bounds, index_map, group_mask, group_dim)
copy data (involves reshape) tensor_out = tensor_out + tensor_in move_data memory optimization: trans...
Routines to split blocks and to convert between tensors with different block sizes.
Definition dbt_split.F:12
subroutine, public dbt_split_copyback(tensor_split_in, tensor_out, summation)
Copy tensor with split blocks to tensor with original block sizes.
Definition dbt_split.F:528
subroutine, public dbt_make_compatible_blocks(tensor1, tensor2, tensor1_split, tensor2_split, order, nodata1, nodata2, move_data)
split two tensors with same total sizes but different block sizes such that they have equal block siz...
Definition dbt_split.F:789
subroutine, public dbt_crop(tensor_in, tensor_out, bounds, move_data)
Definition dbt_split.F:936
Tall-and-skinny matrices: base routines similar to DBM API, mostly wrappers around existing DBM routi...
subroutine, public dbt_tas_get_info(matrix, nblkrows_total, nblkcols_total, local_rows, local_cols, proc_row_dist, proc_col_dist, row_blk_size, col_blk_size, distribution, name)
...
subroutine, public dbt_tas_copy(matrix_b, matrix_a, summation)
Copy matrix_a to matrix_b.
subroutine, public dbt_tas_finalize(matrix)
...
type(dbt_tas_split_info) function, pointer, public dbt_tas_info(matrix)
get info on mpi grid splitting
Matrix multiplication for tall-and-skinny matrices. This uses the k-split (non-recursive) CARMA algor...
Definition dbt_tas_mm.F:18
subroutine, public dbt_tas_set_batched_state(matrix, state, opt_grid)
set state flags during batched multiplication
subroutine, public dbt_tas_batched_mm_init(matrix)
...
subroutine, public dbt_tas_batched_mm_finalize(matrix)
...
recursive subroutine, public dbt_tas_multiply(transa, transb, transc, alpha, matrix_a, matrix_b, beta, matrix_c, optimize_dist, split_opt, filter_eps, flop, move_data_a, move_data_b, retain_sparsity, simple_split, unit_nr, log_verbose)
tall-and-skinny matrix-matrix multiplication. Undocumented dummy arguments are identical to arguments...
Definition dbt_tas_mm.F:114
subroutine, public dbt_tas_batched_mm_complete(matrix, warn)
...
methods to split tall-and-skinny matrices along longest dimension. Basically, we are splitting proces...
subroutine, public dbt_tas_release_info(split_info)
...
integer, parameter, public rowsplit
integer, parameter, public colsplit
subroutine, public dbt_tas_create_split(split_info, mp_comm, split_rowcol, nsplit, own_comm, opt_nsplit)
Split Cartesian process grid using a default split heuristic.
real(dp), parameter, public default_nsplit_accept_ratio
real(dp), parameter, public default_pdims_accept_ratio
subroutine, public dbt_tas_info_hold(split_info)
...
DBT tall-and-skinny base types. Mostly wrappers around existing DBM routines.
DBT tensor framework for block-sparse tensor contraction: Types and create/destroy routines.
Definition dbt_types.F:12
subroutine, public dbt_pgrid_destroy(pgrid, keep_comm)
destroy process grid
Definition dbt_types.F:894
subroutine, public dbt_distribution_new(dist, pgrid, nd_dist_1, nd_dist_2, nd_dist_3, nd_dist_4)
Create a tensor distribution.
Definition dbt_types.F:875
subroutine, public blk_dims_tensor(tensor, dims)
tensor block dimensions
Definition dbt_types.F:1455
subroutine, public dims_tensor(tensor, dims)
tensor dimensions
Definition dbt_types.F:1227
subroutine, public dbt_copy_contraction_storage(tensor_in, tensor_out)
Definition dbt_types.F:1877
type(dbt_pgrid_type) function, public dbt_nd_mp_comm(comm_2d, map1_2d, map2_2d, dims_nd, dims1_nd, dims2_nd, pdims_2d, tdims, nsplit, dimsplit)
Create a default nd process topology that is consistent with a given 2d topology. Purpose: a nd tenso...
Definition dbt_types.F:653
subroutine, public dbt_destroy(tensor)
Destroy a tensor.
Definition dbt_types.F:1399
pure integer function, public dbt_max_nblks_local(tensor)
returns an estimate of maximum number of local blocks in tensor (irrespective of the actual number of...
Definition dbt_types.F:1839
subroutine, public dbt_get_info(tensor, nblks_total, nfull_total, nblks_local, nfull_local, pdims, my_ploc, blks_local_1, blks_local_2, blks_local_3, blks_local_4, proc_dist_1, proc_dist_2, proc_dist_3, proc_dist_4, blk_size_1, blk_size_2, blk_size_3, blk_size_4, blk_offset_1, blk_offset_2, blk_offset_3, blk_offset_4, distribution, name)
As block_get_info but for tensors.
Definition dbt_types.F:1645
subroutine, public dbt_distribution_new_expert(dist, pgrid, map1_2d, map2_2d, nd_dist_1, nd_dist_2, nd_dist_3, nd_dist_4, own_comm)
Create a tensor distribution.
Definition dbt_types.F:787
type(dbt_distribution_type) function, public dbt_distribution(tensor)
get distribution from tensor
Definition dbt_types.F:969
pure integer function, public ndims_tensor(tensor)
tensor rank
Definition dbt_types.F:1216
pure integer function, public dbt_nblks_total(tensor, idim)
total numbers of blocks along dimension idim
Definition dbt_types.F:1606
pure integer function, public dbt_get_num_blocks(tensor)
As block_get_num_blocks: get number of local blocks.
Definition dbt_types.F:1748
subroutine, public dbt_default_distvec(nblk, nproc, blk_size, dist)
get a load-balanced and randomized distribution along one tensor dimension
Definition dbt_types.F:1865
subroutine, public dbt_hold(tensor)
reference counting for tensors (only needed for communicator handle that must be freed when no longer...
Definition dbt_types.F:1177
subroutine, public dbt_clear(tensor)
Clear tensor (s.t. it does not contain any blocks).
Definition dbt_types.F:1768
subroutine, public dbt_finalize(tensor)
Finalize tensor, as block_finalize. This should be taken care of internally in DBT tensors,...
Definition dbt_types.F:1779
subroutine, public mp_environ_pgrid(pgrid, dims, task_coor)
as mp_environ but for special pgrid type
Definition dbt_types.F:768
subroutine, public dbt_get_stored_coordinates(tensor, ind_nd, processor)
Generalization of block_get_stored_coordinates for tensors.
Definition dbt_types.F:1499
pure integer function, public dbt_get_nze(tensor)
Definition dbt_types.F:1797
integer(kind=int_8) function, public dbt_get_num_blocks_total(tensor)
Get total number of blocks.
Definition dbt_types.F:1758
pure integer(int_8) function, public ndims_matrix_row(tensor)
how many tensor dimensions are mapped to matrix row
Definition dbt_types.F:1193
pure integer(int_8) function, public ndims_matrix_column(tensor)
how many tensor dimensions are mapped to matrix column
Definition dbt_types.F:1205
subroutine, public dbt_filter(tensor, eps)
As block_filter.
Definition dbt_types.F:1577
subroutine, public dbt_distribution_destroy(dist)
Destroy tensor distribution.
Definition dbt_types.F:915
subroutine, public dbt_scale(tensor, alpha)
as block_scale
Definition dbt_types.F:1788
Defines the basic variable types.
Definition kinds.F:23
integer, parameter, public int_8
Definition kinds.F:54
integer, parameter, public dp
Definition kinds.F:34
integer, parameter, public default_string_length
Definition kinds.F:57
Interface to the message passing library MPI.
All kind of helpful little routines.
Definition util.F:14