-
Notifications
You must be signed in to change notification settings - Fork 47
/
dbcsr_mm_cannon.F
3117 lines (2961 loc) · 158 KB
/
dbcsr_mm_cannon.F
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
!--------------------------------------------------------------------------------------------------!
! Copyright (C) by the DBCSR developers group - All rights reserved !
! Copyright (C) 2022 Advanced Micro Devices, Inc. - All rights reserved !
! This file is part of the DBCSR library. !
! !
! For information on the license, see the LICENSE file. !
! For further information please visit https://dbcsr.cp2k.org !
! SPDX-License-Identifier: GPL-2.0+ !
!--------------------------------------------------------------------------------------------------!
MODULE dbcsr_mm_cannon
!! First layer of the dbcsr matrix-matrix multiplication.
!! It performs the MPI parallelization according to Cannon's algorithm.
!! <b>Modification history:</b>
!! - 2010-02-23 Moved from dbcsr_operations
!! - 2011-11 Moved parameter-stack processing routines to
!! dbcsr_mm_methods.
!! - 2013-01 reorganized code (Ole Schuett)
USE dbcsr_acc_event, ONLY: acc_event_synchronize
USE dbcsr_acc_device, ONLY: acc_device_synchronize
USE dbcsr_acc_stream, ONLY: acc_stream_synchronize
USE dbcsr_acc_devmem, ONLY: acc_devmem_cptr
USE dbcsr_array_types, ONLY: array_data, &
array_exists, &
array_i1d_obj, &
array_size
USE dbcsr_block_operations, ONLY: dbcsr_block_conjg, &
dbcsr_block_copy_aa, &
dbcsr_block_real_neg, &
dbcsr_block_scale, &
dbcsr_block_transpose_aa, &
dbcsr_block_transpose
USE dbcsr_config, ONLY: dbcsr_cfg, &
use_acc
USE dbcsr_data_methods, ONLY: &
dbcsr_data_clear_pointer, dbcsr_data_ensure_size, dbcsr_data_get_size, &
dbcsr_data_get_size_referenced, dbcsr_data_hold, dbcsr_data_host2dev, dbcsr_data_init, &
dbcsr_data_new, dbcsr_data_release, dbcsr_data_set_pointer, &
dbcsr_data_set_size_referenced, dbcsr_scalar_are_equal, dbcsr_scalar_negative, &
dbcsr_scalar_one, dbcsr_get_data_p_s, dbcsr_get_data_p_d, dbcsr_get_data_p_c, dbcsr_get_data_p_z
USE dbcsr_data_types, ONLY: dbcsr_datatype_sizeof
USE dbcsr_dist_methods, ONLY: dbcsr_distribution_col_dist, &
dbcsr_distribution_has_threads, &
dbcsr_distribution_max_col_dist, &
dbcsr_distribution_max_row_dist, &
dbcsr_distribution_mp, &
dbcsr_distribution_row_dist
USE dbcsr_index_operations, ONLY: dbcsr_count_row_index, &
dbcsr_has_local_row_index, &
dbcsr_index_prune_deleted, &
dbcsr_make_index_list, &
dbcsr_make_index_local_row, &
dbcsr_repoint_index
USE dbcsr_io, ONLY: dbcsr_print
USE dbcsr_iterator_operations, ONLY: dbcsr_iterator_blocks_left, &
dbcsr_iterator_next_block, &
dbcsr_iterator_start, &
dbcsr_iterator_stop
USE dbcsr_mem_methods, ONLY: dbcsr_mempool_limit_capacity
USE dbcsr_methods, ONLY: &
dbcsr_destroy_array, dbcsr_distribution, dbcsr_get_data_type, dbcsr_get_index_memory_type, &
dbcsr_has_symmetry, dbcsr_image_dist_hold, dbcsr_image_dist_init, &
dbcsr_image_dist_release, dbcsr_max_col_size, dbcsr_max_row_size, dbcsr_nblkcols_local, &
dbcsr_nblkrows_local, dbcsr_nblkrows_total, dbcsr_release, dbcsr_valid_index
USE dbcsr_mm_common, ONLY: &
acc_transpose_blocks, calculate_norms, count_mpi_statistics, dbcsr_mm_multrec_type_p, &
dbcsr_mpi_statistics, enumerate_blk_sizes, huge_norm, local_filter, max_memory, &
memtype_abpanel_1, memtype_abpanel_2, memtype_mpi_buffer, memtype_trsbuffer_1, &
memtype_normsbuf, memtype_offsetsbuf, memtype_nelemsbuf, acc_calculate_norms, &
memtype_trsbuffer_2, product_matrix_size_guess, rec_sort_index, setup_buffer_matrix
USE dbcsr_mm_dist_operations, ONLY: dbcsr_get_local_vcols, &
dbcsr_get_local_vrows, &
dbcsr_reset_vlocals, &
image_calculator
USE dbcsr_mm_multrec, ONLY: dbcsr_mm_multrec_finalize, &
dbcsr_mm_multrec_init, &
dbcsr_mm_multrec_multiply
USE dbcsr_mp_methods, ONLY: &
dbcsr_mp_grid_setup, dbcsr_mp_group, dbcsr_mp_has_subgroups, dbcsr_mp_my_col_group, &
dbcsr_mp_my_row_group, dbcsr_mp_mynode, dbcsr_mp_mypcol, dbcsr_mp_myprow, dbcsr_mp_npcols, &
dbcsr_mp_nprows, dbcsr_mp_numnodes, dbcsr_mp_pgrid
USE dbcsr_mp_operations, ONLY: dbcsr_irecv_any, &
dbcsr_isend_any, &
hybrid_alltoall_any, &
hybrid_alltoall_i1
USE dbcsr_operations, ONLY: dbcsr_copy, &
dbcsr_crop_matrix
USE dbcsr_ptr_util, ONLY: ensure_array_size
USE dbcsr_transformations, ONLY: dbcsr_make_dense_low
USE dbcsr_types, ONLY: &
dbcsr_2d_array_type, dbcsr_data_obj, dbcsr_distribution_obj, dbcsr_imagedistribution_obj, &
dbcsr_iterator, dbcsr_memtype_type, dbcsr_meta_size, dbcsr_mp_obj, dbcsr_scalar_type, &
dbcsr_slot_home_coli, dbcsr_slot_home_pcol, dbcsr_slot_home_prow, dbcsr_slot_home_rowi, &
dbcsr_slot_home_vpcol, dbcsr_slot_home_vprow, dbcsr_slot_size, dbcsr_type, &
dbcsr_type_complex_4, dbcsr_type_complex_8, dbcsr_type_int_4, dbcsr_type_no_symmetry, &
dbcsr_type_real_4, dbcsr_type_real_8
USE dbcsr_dist_util, ONLY: count_bins, &
dbcsr_checksum
USE dbcsr_work_operations, ONLY: dbcsr_create, &
dbcsr_special_finalize, &
dbcsr_work_create
USE dbcsr_kinds, ONLY: dp, &
int_8, &
real_4, &
real_8, &
sp
USE dbcsr_machine, ONLY: default_output_unit, &
m_memory
USE dbcsr_mpiwrap, ONLY: mp_allgather, &
mp_alltoall, &
mp_environ, &
mp_irecv, &
mp_isend, &
mp_request_null, &
mp_sum, &
mp_testany, &
mp_waitall, &
mp_comm_type, &
mp_request_type
USE ISO_C_BINDING, ONLY: C_F_POINTER
#include "base/dbcsr_base_uses.f90"
!$ USE OMP_LIB, ONLY: omp_get_max_threads, omp_get_thread_num, omp_get_num_threads
IMPLICIT NONE
PRIVATE
CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'dbcsr_mm_cannon'
LOGICAL, PARAMETER :: debug_mod = .FALSE.
LOGICAL, PARAMETER :: careful_mod = .FALSE.
INTERFACE dbcsr_switch
MODULE PROCEDURE dbcsr_switch_sets
MODULE PROCEDURE dbcsr_switch_d_ptrs
END INTERFACE
PUBLIC :: multiply_cannon, make_m2s, &
multiply_cannon_g2g
CONTAINS
SUBROUTINE make_m2s(matrix, m2s, rdist, dense_rdist, &
!! Make images from the matrix (left or right)
use_dense_mult, ab_dense, predistribute, &
f_k, l_k, f_row, l_row, f_col, l_col, &
row_blk_size, col_blk_size, &
k_vmap, m_map, n_map, &
alpha)
TYPE(dbcsr_type), INTENT(IN) :: matrix
TYPE(dbcsr_2d_array_type), INTENT(OUT), POINTER :: m2s
TYPE(dbcsr_imagedistribution_obj), INTENT(INOUT) :: rdist, dense_rdist
LOGICAL, INTENT(IN) :: use_dense_mult, ab_dense
CHARACTER, INTENT(IN) :: predistribute
INTEGER, INTENT(IN) :: f_k, l_k, f_row, l_row, f_col, l_col
TYPE(array_i1d_obj), INTENT(INOUT) :: row_blk_size, col_blk_size
TYPE(array_i1d_obj), INTENT(IN) :: k_vmap, m_map, n_map
TYPE(dbcsr_scalar_type), INTENT(IN), OPTIONAL :: alpha
CHARACTER(len=*), PARAMETER :: routineN = 'make_m2s'
INTEGER :: handle, i, im, j, jm
INTEGER, DIMENSION(4) :: f_crop
LOGICAL :: do_scale, thread_redist
TYPE(array_i1d_obj) :: col_map, row_map
TYPE(dbcsr_type) :: dense_template, matrix_tmp
CALL timeset(routineN, handle)
ALLOCATE (m2s)
do_scale = .FALSE.
IF (PRESENT(alpha)) THEN
IF (.NOT. dbcsr_scalar_are_equal(alpha, dbcsr_scalar_one(alpha%data_type))) THEN
do_scale = .TRUE.
END IF
END IF
IF (do_scale) THEN
! Copy and scale matrix if alpha is not 1.
CALL dbcsr_make_images(matrix, m2s, rdist, &
predistribute=predistribute, &
no_copy_data=use_dense_mult, scale_value=alpha)
ELSE
CALL dbcsr_make_images(matrix, m2s, rdist, &
predistribute=predistribute, &
no_copy_data=use_dense_mult)
END IF
im = SIZE(m2s%mats, 1)
jm = SIZE(m2s%mats, 2)
SELECT CASE (predistribute)
CASE ('L')
f_crop = (/f_row, l_row, f_k, l_k/)
row_map = m_map
col_map = k_vmap
thread_redist = .TRUE.
CASE default
f_crop = (/f_k, l_k, f_col, l_col/)
row_map = k_vmap
col_map = n_map
thread_redist = .FALSE.
END SELECT
! Post-processing of images.
DO i = 1, im
DO j = 1, jm
CALL dbcsr_reset_vlocals(m2s%mats(i, j), rdist)
! Crop if necessary
IF (ANY(f_crop .NE. 0)) THEN
matrix_tmp = dbcsr_type()
CALL dbcsr_crop_matrix(matrix_tmp, m2s%mats(i, j), &
full_row_bounds=f_crop(1:2), &
full_column_bounds=f_crop(3:4), &
shallow_data=.FALSE.)
CALL dbcsr_release(m2s%mats(i, j))
CALL dbcsr_copy(m2s%mats(i, j), matrix_tmp, shallow_data=.TRUE.)
CALL dbcsr_release(matrix_tmp)
CALL dbcsr_reset_vlocals(m2s%mats(i, j), rdist)
END IF
END DO
END DO
IF (ab_dense) THEN
dense_template = dbcsr_type()
CALL dbcsr_create(dense_template, template=matrix, &
dist=dense_rdist%i%main, &
row_blk_size_obj=row_blk_size, col_blk_size_obj=col_blk_size)
CALL dbcsr_make_images_dense(m2s, dense_rdist, &
row_map=row_map, col_map=col_map, &
join_cols=use_dense_mult, join_rows=ab_dense, &
new_template=dense_template)
CALL dbcsr_image_dist_release(rdist)
rdist = dense_rdist
CALL dbcsr_image_dist_hold(rdist)
DO i = 1, im
DO j = 1, jm
CALL dbcsr_reset_vlocals(m2s%mats(i, j), rdist)
END DO
END DO
END IF
DO i = 1, im
DO j = 1, jm
! Convert to local-row index
CALL dbcsr_make_index_local_row(m2s%mats(i, j))
! Convert to list index
CALL dbcsr_make_index_list(m2s%mats(i, j), thread_redist=thread_redist)
END DO
END DO
IF (ab_dense) THEN
CALL dbcsr_image_dist_release(dense_rdist)
CALL dbcsr_release(dense_template)
END IF
CALL timestop(handle)
END SUBROUTINE make_m2s
SUBROUTINE dbcsr_make_images(source, normalized, target_image_dist, &
predistribute, no_copy_data, scale_value)
!! Creates row and column images of a matrix.
TYPE(dbcsr_type), INTENT(IN) :: source
!! input matrix
TYPE(dbcsr_2d_array_type), INTENT(OUT) :: normalized
!! image array of the normalized matrix
TYPE(dbcsr_imagedistribution_obj), INTENT(IN) :: target_image_dist
!! normalize to this image distribution
CHARACTER, INTENT(IN), OPTIONAL :: predistribute
!! predistribute data for multiplication
LOGICAL, INTENT(IN), OPTIONAL :: no_copy_data
!! try to not merge data at the end
TYPE(dbcsr_scalar_type), INTENT(IN), OPTIONAL :: scale_value
!! scale with this value
! ---------------------------------------------------------------------------
IF (dbcsr_cfg%use_mpi_rma%val) &
DBCSR_ABORT("RMA algo not supported here!")
IF (.NOT. dbcsr_valid_index(source)) &
DBCSR_ABORT("Matrix not initialized.")
CALL make_images(source, normalized, &
target_image_dist, desymmetrize=dbcsr_has_symmetry(source), &
predistribute=predistribute, &
no_copy_data=no_copy_data, &
scale_value=scale_value)
normalized%image_dist = target_image_dist
CALL dbcsr_image_dist_hold(normalized%image_dist)
END SUBROUTINE dbcsr_make_images
SUBROUTINE make_images(ism, ums, target_imgdist, desymmetrize, predistribute, &
no_copy_data, scale_value)
!! Makes column-based and row-based images of a matrix.
TYPE(dbcsr_type), INTENT(IN) :: ism
!! input symmetric matrix
TYPE(dbcsr_2d_array_type), INTENT(OUT) :: ums
!! normalized matrices
TYPE(dbcsr_imagedistribution_obj), INTENT(IN) :: target_imgdist
!! image distribution to normalize to
LOGICAL, INTENT(IN), OPTIONAL :: desymmetrize
!! desymmetrize a symmetric matrix
CHARACTER, INTENT(IN), OPTIONAL :: predistribute
!! predistribute data for multiplication
LOGICAL, INTENT(IN), OPTIONAL :: no_copy_data
!! try to not merge data at the end
TYPE(dbcsr_scalar_type), INTENT(IN), OPTIONAL :: scale_value
!! scale with this value
CHARACTER(len=*), PARAMETER :: routineN = 'make_images'
INTEGER, PARAMETER :: metalen = 5
LOGICAL, PARAMETER :: dbg = .FALSE.
CHARACTER :: predist_type, predist_type_fwd
INTEGER :: blk, blk_l, blk_p, bp, col, col_img, col_size, coli, data_type, dst_p, handle, &
handle2, ithread, ncol_images, nrow_images, nsymmetries, nthreads, numproc, &
nze, pcol, prow, row, row_img, row_size, rowi, sd_pos, sm_pos, src_p, stored_blk_p, &
stored_col, stored_row, symmetry_i, tr_col_size, tr_row_size, vcol, vrow
INTEGER, ALLOCATABLE, DIMENSION(:) :: myt_sdp, myt_smp, rd_disp, recv_meta, &
rm_disp, sd_disp, sdp, send_meta, &
sm_disp, smp
INTEGER, ALLOCATABLE, DIMENSION(:, :) :: all_total_send_offset, blk_ps, blks, &
myt_total_send_count, &
total_recv_count, total_send_count
INTEGER, ALLOCATABLE, DIMENSION(:, :, :, :) :: myt_send_count, recv_count, send_count
INTEGER, DIMENSION(:), CONTIGUOUS, POINTER :: col_blk_size, col_dist, col_img_dist, &
row_blk_size, row_dist, row_img_dist
INTEGER, DIMENSION(:, :), CONTIGUOUS, POINTER :: blacs2mpi
LOGICAL :: nocopy, tr
TYPE(dbcsr_data_obj) :: received_data_area, recv_data_area, &
send_data_area
TYPE(dbcsr_distribution_obj) :: old_dist, target_dist
TYPE(dbcsr_iterator) :: iter
TYPE(dbcsr_memtype_type) :: data_memory_type
TYPE(dbcsr_mp_obj) :: mp_obj
TYPE(dbcsr_scalar_type) :: scale_neg_one
TYPE(dbcsr_type) :: sm
TYPE(mp_comm_type) :: mp_group
! ---------------------------------------------------------------------------
! Check input matrix
! Set convenient variables to access input matrix info
!
CALL timeset(routineN, handle)
nocopy = .FALSE.
IF (PRESENT(no_copy_data)) nocopy = no_copy_data
sm = ism
nsymmetries = 1
IF (PRESENT(desymmetrize)) THEN
IF (desymmetrize .AND. sm%symmetry) THEN
nsymmetries = 2
END IF
END IF
SELECT CASE (predistribute)
CASE ('L', 'l')
predist_type = 'L'
predist_type_fwd = 'l'
CASE ('R', 'r')
predist_type = 'R'
predist_type_fwd = 'r'
CASE default
DBCSR_ABORT("Incorrect pre-shift specifier.")
END SELECT
data_type = sm%data_type
IF (data_type .NE. dbcsr_type_real_8 .AND. &
data_type .NE. dbcsr_type_real_4 .AND. &
data_type .NE. dbcsr_type_complex_8 .AND. &
data_type .NE. dbcsr_type_complex_4) &
DBCSR_ABORT("Invalid data type.")
scale_neg_one = dbcsr_scalar_negative(dbcsr_scalar_one(data_type))
row_blk_size => array_data(sm%row_blk_size)
col_blk_size => array_data(sm%col_blk_size)
old_dist = dbcsr_distribution(ism)
target_dist = target_imgdist%i%main
row_dist => dbcsr_distribution_row_dist(target_dist)
col_dist => dbcsr_distribution_col_dist(target_dist)
IF (sm%symmetry) THEN
IF (SIZE(row_dist) .NE. SIZE(col_dist)) &
DBCSR_WARN('Unequal row and column distributions for symmetric matrix.')
END IF
nrow_images = target_imgdist%i%row_decimation
IF (nrow_images .GT. 1) THEN
row_img_dist => array_data(target_imgdist%i%row_image)
ELSE
NULLIFY (row_img_dist)
END IF
ncol_images = target_imgdist%i%col_decimation
IF (ncol_images .GT. 1) THEN
col_img_dist => array_data(target_imgdist%i%col_image)
ELSE
NULLIFY (col_img_dist)
END IF
mp_obj = dbcsr_distribution_mp(target_dist)
blacs2mpi => dbcsr_mp_pgrid(mp_obj)
numproc = dbcsr_mp_numnodes(mp_obj)
mp_group = dbcsr_mp_group(mp_obj)
IF (dbcsr_distribution_max_row_dist(old_dist) .GT. UBOUND(blacs2mpi, 1)) &
DBCSR_ABORT('Row distribution references unexistent processor rows')
IF (dbg) THEN
IF (dbcsr_distribution_max_row_dist(old_dist) .NE. UBOUND(blacs2mpi, 1)) &
DBCSR_WARN('Range of row distribution not equal to processor rows')
END IF
IF (dbcsr_distribution_max_col_dist(old_dist) .GT. UBOUND(blacs2mpi, 2)) &
DBCSR_ABORT('Col distribution references unexistent processor cols')
IF (dbg) THEN
IF (dbcsr_distribution_max_col_dist(old_dist) .NE. UBOUND(blacs2mpi, 2)) &
DBCSR_WARN('Range of col distribution not equal to processor cols')
END IF
! Check threads configuration
!$ IF (.NOT. dbcsr_distribution_has_threads(old_dist)) &
!$ DBCSR_ABORT("Thread distribution not defined")
! Allocate shared temporary buffers
!
ALLOCATE (send_count(2, nrow_images, ncol_images, 0:numproc - 1)); send_count = 0
ALLOCATE (recv_count(2, nrow_images, ncol_images, 0:numproc - 1))
ALLOCATE (total_send_count(2, 0:numproc - 1)); total_send_count = 0
ALLOCATE (total_recv_count(2, 0:numproc - 1))
ALLOCATE (sdp(0:numproc - 1))
ALLOCATE (smp(0:numproc - 1))
ALLOCATE (sd_disp(0:numproc - 1)); sd_disp(0) = 1
ALLOCATE (sm_disp(0:numproc - 1)); sm_disp(0) = 1
ALLOCATE (rd_disp(0:numproc - 1)); rd_disp(0) = 1
ALLOCATE (rm_disp(0:numproc - 1)); rm_disp(0) = 1
ALLOCATE (all_total_send_offset(2, 0:numproc - 1))
ALLOCATE (blk_ps(nrow_images, ncol_images)); blk_ps = 1
ALLOCATE (blks(nrow_images, ncol_images)); blks = 1
!
! Allocate and init mats
!
ALLOCATE (ums%mats(nrow_images, ncol_images))
data_memory_type = memtype_abpanel_1
DO row_img = 1, nrow_images
DO col_img = 1, ncol_images
ums%mats(row_img, col_img) = dbcsr_type()
CALL dbcsr_create(ums%mats(row_img, col_img), "imaged "//sm%name, &
target_dist, &
dbcsr_type_no_symmetry, &
row_blk_size_obj=sm%row_blk_size, col_blk_size_obj=sm%col_blk_size, &
nze=0, data_type=data_type, &
max_rbs=sm%max_rbs, max_cbs=sm%max_cbs, &
row_blk_offset=sm%row_blk_offset, col_blk_offset=sm%col_blk_offset, &
thread_dist=sm%dist, &
data_memory_type=data_memory_type, &
index_memory_type=memtype_mpi_buffer)
ums%mats(row_img, col_img)%negate_real = sm%negate_real
ums%mats(row_img, col_img)%negate_imaginary = sm%negate_imaginary
END DO
END DO
nthreads = 1
!$OMP PARALLEL DEFAULT (NONE) &
!$OMP PRIVATE (ithread, symmetry_i, row_img, col_img, &
!$OMP myt_send_count, myt_total_send_count, &
!$OMP iter, row, col, blk, row_size, col_size, stored_row, stored_col, &
!$OMP prow, pcol, rowi, coli, vrow, vcol, dst_p, nze, myt_smp, myt_sdp, &
!$OMP blk_p, bp, sd_pos, sm_pos,tr, &
!$OMP tr_row_size, tr_col_size) &
!$OMP SHARED (nthreads, nsymmetries, row_img_dist, col_img_dist, &
!$OMP nrow_images, ncol_images, numproc, scale_value, &
!$OMP ums, sm, ism, target_imgdist, row_dist, col_dist,&
!$OMP predist_type_fwd, blacs2mpi, row_blk_size, col_blk_size, &
!$OMP send_count, recv_count, handle2,mp_group, &
!$OMP total_send_count, total_recv_count, recv_data_area, nocopy, &
!$OMP data_type, recv_meta, send_data_area, send_meta, &
!$OMP sd_disp, sm_disp, rd_disp, rm_disp, all_total_send_offset, blk_ps, blks, &
!$OMP received_data_area, scale_neg_one, memtype_abpanel_1)
ithread = 0
!$ ithread = omp_get_thread_num()
!$OMP MASTER
!$ nthreads = omp_get_num_threads()
!$OMP END MASTER
! Allocate thread private data
!
ALLOCATE (myt_send_count(2, nrow_images, ncol_images, 0:numproc - 1)); myt_send_count(:, :, :, :) = 0
ALLOCATE (myt_total_send_count(2, 0:numproc - 1))
! Thread-local pointers of the current adding position into the send buffers
ALLOCATE (myt_smp(0:numproc - 1), myt_sdp(0:numproc - 1))
! Count sizes for sending
!
CALL dbcsr_iterator_start(iter, ism, shared=.TRUE.)
DO WHILE (dbcsr_iterator_blocks_left(iter))
CALL dbcsr_iterator_next_block(iter, row, col, blk, &
row_size=row_size, col_size=col_size)
nze = row_size*col_size
IF (nze .EQ. 0) CYCLE
DO symmetry_i = 1, nsymmetries
IF (symmetry_i .EQ. 1) THEN
stored_row = row; stored_col = col
ELSE
IF (row .EQ. col) CYCLE
stored_row = col; stored_col = row
END IF
! Where do we send this block?
row_img = 1
IF (nrow_images .GT. 1) row_img = row_img_dist(stored_row)
col_img = 1
IF (ncol_images .GT. 1) col_img = col_img_dist(stored_col)
CALL image_calculator(target_imgdist, &
prow=prow, rowi=rowi, &
pcol=pcol, coli=coli, &
vprow=vrow, vpcol=vcol, &
myprow=row_dist(stored_row), myrowi=row_img, &
mypcol=col_dist(stored_col), mycoli=col_img, &
shifting=predist_type_fwd)
dst_p = blacs2mpi(prow, pcol)
! These counts are meant for the thread that processes this row.
myt_send_count(1, rowi, coli, dst_p) = &
myt_send_count(1, rowi, coli, dst_p) + 1
myt_send_count(2, rowi, coli, dst_p) = &
myt_send_count(2, rowi, coli, dst_p) + nze
END DO ! symmetry_i
END DO
CALL dbcsr_iterator_stop(iter)
DO dst_p = 0, numproc - 1
myt_total_send_count(1, dst_p) = SUM(myt_send_count(1, :, :, dst_p))
myt_total_send_count(2, dst_p) = SUM(myt_send_count(2, :, :, dst_p))
END DO
! Merge the send counts
!$OMP CRITICAL
send_count(:, :, :, :) = send_count(:, :, :, :) + myt_send_count(:, :, :, :)
total_send_count(:, :) = total_send_count(:, :) + myt_total_send_count(:, :)
!$OMP END CRITICAL
!$OMP BARRIER
!$OMP MASTER
CALL timeset(routineN//"_sizes", handle2)
CALL mp_alltoall(send_count, recv_count, 2*nrow_images*ncol_images, &
mp_group)
CALL timestop(handle2)
!$OMP END MASTER
!$OMP BARRIER
! Fill in the meta data structures and copy the data.
!$OMP DO
DO dst_p = 0, numproc - 1
total_recv_count(1, dst_p) = SUM(recv_count(1, :, :, dst_p))
total_recv_count(2, dst_p) = SUM(recv_count(2, :, :, dst_p))
END DO
!$OMP MASTER
! Allocate data structures needed for data exchange.
CALL dbcsr_data_init(recv_data_area)
CALL dbcsr_data_init(send_data_area)
IF (nrow_images .EQ. 1 .AND. ncol_images .EQ. 1 .OR. nocopy) THEN
! For some cases the faster dbcsr_special_finalize(reshuffle=.FALSE.) can be used.
! This basically makes this working matrix the actual data-area.
! Hence, for those cases we have to use data_memory_type already here.
CALL dbcsr_data_new(recv_data_area, data_type, SUM(total_recv_count(2, :)), memory_type=memtype_abpanel_1)
! Some MPI implementations have high overhead when encountering a new buffer.
! Therefore we also use the memory pool for the send buffer.
CALL dbcsr_data_new(send_data_area, data_type, SUM(total_send_count(2, :)), memory_type=memtype_abpanel_1)
ELSE
CALL dbcsr_data_new(recv_data_area, data_type, SUM(total_recv_count(2, :)))
CALL dbcsr_data_new(send_data_area, data_type, SUM(total_send_count(2, :)))
END IF
ALLOCATE (recv_meta(metalen*SUM(total_recv_count(1, :))))
ALLOCATE (send_meta(metalen*SUM(total_send_count(1, :))))
! Calculate displacements for processors needed for the exchanges.
DO dst_p = 1, numproc - 1
sm_disp(dst_p) = sm_disp(dst_p - 1) &
+ metalen*total_send_count(1, dst_p - 1)
sd_disp(dst_p) = sd_disp(dst_p - 1) &
+ total_send_count(2, dst_p - 1)
rm_disp(dst_p) = rm_disp(dst_p - 1) &
+ metalen*total_recv_count(1, dst_p - 1)
rd_disp(dst_p) = rd_disp(dst_p - 1) &
+ total_recv_count(2, dst_p - 1)
END DO
myt_smp(:) = sm_disp(:)
myt_sdp(:) = sd_disp(:)
IF (nthreads .GT. 1) THEN
all_total_send_offset(1, :) = myt_smp(:) + metalen*myt_total_send_count(1, :)
all_total_send_offset(2, :) = myt_sdp(:) + myt_total_send_count(2, :)
END IF
!$OMP END MASTER
!$OMP BARRIER
IF (ithread .GT. 0) THEN
!$OMP CRITICAL
myt_smp(:) = all_total_send_offset(1, :)
myt_sdp(:) = all_total_send_offset(2, :)
all_total_send_offset(1, :) &
= all_total_send_offset(1, :) + metalen*myt_total_send_count(1, :)
all_total_send_offset(2, :) &
= all_total_send_offset(2, :) + myt_total_send_count(2, :)
!$OMP END CRITICAL
ELSE
CALL dbcsr_data_init(received_data_area)
received_data_area = recv_data_area
CALL dbcsr_data_hold(received_data_area)
DO row_img = 1, nrow_images
DO col_img = 1, ncol_images
CALL dbcsr_work_create(ums%mats(row_img, col_img), &
SUM(recv_count(1, row_img, col_img, :)), n=1)
CALL dbcsr_data_hold(received_data_area)
CALL dbcsr_data_release(ums%mats(row_img, col_img)%wms(1)%data_area)
ums%mats(row_img, col_img)%wms(1)%data_area = received_data_area
END DO
END DO
END IF
!$OMP BARRIER
! Add timing call to the packing of the send buffers
!
CALL timeset(routineN//"_pack", handle2)
! Copies metadata and actual data to be sent into the send buffers.
CALL dbcsr_iterator_start(iter, ism, shared=.TRUE.)
SELECT CASE (data_type)
CASE (dbcsr_type_real_4)
CALL prepare_buffers_s(sm%negate_real, sm%negate_imaginary, &
iter, row, col, blk, blk_p, bp, &
row_size, col_size, nze, nsymmetries, symmetry_i, &
stored_row, stored_col, tr_row_size, tr_col_size, tr, &
row_img, col_img, nrow_images, ncol_images, &
row_img_dist, col_img_dist, predist_type_fwd, blacs2mpi, &
target_imgdist, prow, pcol, rowi, coli, &
row_dist, col_dist, dst_p, sm_pos, myt_smp, metalen, &
sd_pos, myt_sdp, send_meta, sd_disp, &
dbcsr_get_data_p_s(sm%data_area), &
send_data_area, scale_neg_one, scale_value)
CASE (dbcsr_type_real_8)
CALL prepare_buffers_d(sm%negate_real, sm%negate_imaginary, &
iter, row, col, blk, blk_p, bp, &
row_size, col_size, nze, nsymmetries, symmetry_i, &
stored_row, stored_col, tr_row_size, tr_col_size, tr, &
row_img, col_img, nrow_images, ncol_images, &
row_img_dist, col_img_dist, predist_type_fwd, blacs2mpi, &
target_imgdist, prow, pcol, rowi, coli, &
row_dist, col_dist, dst_p, sm_pos, myt_smp, metalen, &
sd_pos, myt_sdp, send_meta, sd_disp, &
dbcsr_get_data_p_d(sm%data_area), &
send_data_area, scale_neg_one, scale_value)
CASE (dbcsr_type_complex_4)
CALL prepare_buffers_c(sm%negate_real, sm%negate_imaginary, &
iter, row, col, blk, blk_p, bp, &
row_size, col_size, nze, nsymmetries, symmetry_i, &
stored_row, stored_col, tr_row_size, tr_col_size, tr, &
row_img, col_img, nrow_images, ncol_images, &
row_img_dist, col_img_dist, predist_type_fwd, blacs2mpi, &
target_imgdist, prow, pcol, rowi, coli, &
row_dist, col_dist, dst_p, sm_pos, myt_smp, metalen, &
sd_pos, myt_sdp, send_meta, sd_disp, &
dbcsr_get_data_p_c(sm%data_area), &
send_data_area, scale_neg_one, scale_value)
CASE (dbcsr_type_complex_8)
CALL prepare_buffers_z(sm%negate_real, sm%negate_imaginary, &
iter, row, col, blk, blk_p, bp, &
row_size, col_size, nze, nsymmetries, symmetry_i, &
stored_row, stored_col, tr_row_size, tr_col_size, tr, &
row_img, col_img, nrow_images, ncol_images, &
row_img_dist, col_img_dist, predist_type_fwd, blacs2mpi, &
target_imgdist, prow, pcol, rowi, coli, &
row_dist, col_dist, dst_p, sm_pos, myt_smp, metalen, &
sd_pos, myt_sdp, send_meta, sd_disp, &
dbcsr_get_data_p_z(sm%data_area), &
send_data_area, scale_neg_one, scale_value)
END SELECT
CALL dbcsr_iterator_stop(iter)
! Deallocate thread private data
!
DEALLOCATE (myt_send_count)
DEALLOCATE (myt_total_send_count)
DEALLOCATE (myt_smp, myt_sdp)
CALL timestop(handle2)
!$OMP END PARALLEL
! Exchange the data and metadata structures. In the interesting cases (square grids, row col distribution same),
! there are only very few processors that need to exchange data.
! The hybrid_alltoall deals with this by doing point to point communication
CALL timeset(routineN//"_data", handle2)
CALL hybrid_alltoall_any(send_data_area, total_send_count(2, :), sd_disp(:) - 1, &
recv_data_area, total_recv_count(2, :), rd_disp(:) - 1, &
mp_obj, &
most_ptp=.TRUE., remainder_ptp=.TRUE., no_hybrid=.FALSE.)
CALL hybrid_alltoall_i1( &
send_meta(:), metalen*total_send_count(1, :), sm_disp(:) - 1, &
recv_meta(:), metalen*total_recv_count(1, :), rm_disp(:) - 1, &
most_ptp=.TRUE., remainder_ptp=.TRUE., no_hybrid=.FALSE., &
mp_env=mp_obj)
CALL timestop(handle2)
! Now create the work index and/or copy the relevant data from the
! receive buffer into the local indices.
!
DO src_p = 0, numproc - 1
DO blk_l = 1, total_recv_count(1, src_p)
stored_row = recv_meta(rm_disp(src_p) + metalen*(blk_l - 1))
stored_col = recv_meta(rm_disp(src_p) + metalen*(blk_l - 1) + 1)
stored_blk_p = recv_meta(rm_disp(src_p) + metalen*(blk_l - 1) + 2)
row_img = recv_meta(rm_disp(src_p) + metalen*(blk_l - 1) + 3)
col_img = recv_meta(rm_disp(src_p) + metalen*(blk_l - 1) + 4)
nze = row_blk_size(stored_row)*col_blk_size(stored_col)
blk = blks(row_img, col_img)
blks(row_img, col_img) = blks(row_img, col_img) + 1
blk_ps(row_img, col_img) = blk_ps(row_img, col_img) + nze
ums%mats(row_img, col_img)%wms(1)%row_i(blk) = stored_row
ums%mats(row_img, col_img)%wms(1)%col_i(blk) = stored_col
ums%mats(row_img, col_img)%wms(1)%blk_p(blk) = &
SIGN(rd_disp(src_p) + ABS(stored_blk_p) - 1, stored_blk_p)
END DO
END DO
! Finalize the actual imaged matrices from the work matrices
!
DO row_img = 1, nrow_images
DO col_img = 1, ncol_images
ums%mats(row_img, col_img)%wms(1)%lastblk = blks(row_img, col_img) - 1
ums%mats(row_img, col_img)%wms(1)%datasize = blk_ps(row_img, col_img) - 1
!
CALL dbcsr_data_set_size_referenced( &
ums%mats(row_img, col_img)%wms(1)%data_area, &
ums%mats(row_img, col_img)%wms(1)%datasize)
IF (nrow_images .EQ. 1 .AND. ncol_images .EQ. 1 .OR. nocopy) THEN
CALL dbcsr_special_finalize(ums%mats(row_img, col_img), reshuffle=.FALSE.)
ELSE
CALL dbcsr_special_finalize(ums%mats(row_img, col_img), reshuffle=.TRUE.)
END IF
! Save the home process and image row and column
CALL image_calculator(target_imgdist, &
ums%mats(row_img, col_img)%index(dbcsr_slot_home_prow), &
ums%mats(row_img, col_img)%index(dbcsr_slot_home_rowi), &
ums%mats(row_img, col_img)%index(dbcsr_slot_home_pcol), &
ums%mats(row_img, col_img)%index(dbcsr_slot_home_coli), &
vprow=ums%mats(row_img, col_img)%index(dbcsr_slot_home_vprow), &
vpcol=ums%mats(row_img, col_img)%index(dbcsr_slot_home_vpcol), &
myrowi=row_img, mycoli=col_img, &
shifting=predist_type)
END DO
END DO
! Deallocate shared temporary buffers
!
DEALLOCATE (send_count, recv_count)
DEALLOCATE (total_send_count, total_recv_count)
DEALLOCATE (sdp, smp, sd_disp, sm_disp)
DEALLOCATE (rd_disp, rm_disp)
DEALLOCATE (all_total_send_offset)
DEALLOCATE (blk_ps, blks)
DEALLOCATE (recv_meta, send_meta)
CALL dbcsr_data_release(send_data_area)
CALL dbcsr_data_release(received_data_area)
CALL dbcsr_data_release(recv_data_area)
CALL timestop(handle)
END SUBROUTINE make_images
SUBROUTINE dbcsr_make_images_dense(images, new_rdist, &
row_map, col_map, join_cols, join_rows, new_template)
!! Makes dense matrices for the image matrices.
!! @note Used for making matrices dense/undense
TYPE(dbcsr_2d_array_type), INTENT(INOUT) :: images
!! current (undense) matrix images, output is the dense matrix images
TYPE(dbcsr_imagedistribution_obj), INTENT(INOUT) :: new_rdist
!! the new image distribution for dense matrices
TYPE(array_i1d_obj), INTENT(IN) :: row_map, col_map
!! mapping of current (undense) rows to dense rows
!! mapping of current (undense) columns to dense columns
LOGICAL, INTENT(IN) :: join_cols, join_rows
!! make columns dense, default is yes
!! make rows dense, default is yes
TYPE(dbcsr_type), INTENT(IN) :: new_template
!! template dense matrix for creating image matrices
CHARACTER(len=*), PARAMETER :: routineN = 'dbcsr_make_images_dense'
LOGICAL, PARAMETER :: dbg = .FALSE.
INTEGER :: handle, mat_col, mat_row, mat_vpcol, &
mat_vprow
INTEGER, DIMENSION(:), CONTIGUOUS, POINTER :: und_col_blk_offsets, und_row_blk_offsets
INTEGER, DIMENSION(dbcsr_meta_size) :: old_meta
REAL(kind=dp) :: cs
TYPE(array_i1d_obj) :: dense_local_vcols, dense_local_vrows, &
und_local_vcols, und_local_vrows
TYPE(dbcsr_imagedistribution_obj) :: old_rdist
TYPE(dbcsr_type) :: tmp_mat
! ---------------------------------------------------------------------------
CALL timeset(routineN, handle)
old_rdist = images%image_dist
!
DO mat_row = 1, images%image_dist%i%row_decimation
DO mat_col = 1, images%image_dist%i%col_decimation
IF (dbg) THEN
cs = dbcsr_checksum(images%mats(mat_row, mat_col))
WRITE (*, *) routineN//" cs pre", cs
END IF
mat_vprow = images%mats(mat_row, mat_col)%index(dbcsr_slot_home_vprow)
mat_vpcol = images%mats(mat_row, mat_col)%index(dbcsr_slot_home_vpcol)
und_row_blk_offsets => array_data(images%mats(mat_row, mat_col)%row_blk_offset)
und_col_blk_offsets => array_data(images%mats(mat_row, mat_col)%col_blk_offset)
CALL dbcsr_get_local_vrows(old_rdist, und_local_vrows, mat_vprow)
CALL dbcsr_get_local_vcols(old_rdist, und_local_vcols, mat_vpcol)
CALL dbcsr_get_local_vrows(new_rdist, dense_local_vrows, mat_vprow)
CALL dbcsr_get_local_vcols(new_rdist, dense_local_vcols, mat_vpcol)
! The old matrix has to be remembered so it is copied to
! tmp_mat.
old_meta(:) = images%mats(mat_row, mat_col)%index(1:dbcsr_meta_size)
tmp_mat = dbcsr_type()
tmp_mat = images%mats(mat_row, mat_col)
images%mats(mat_row, mat_col) = dbcsr_type()
CALL dbcsr_create(images%mats(mat_row, mat_col), template=new_template)
images%mats(mat_row, mat_col)%index(dbcsr_slot_home_prow &
:dbcsr_slot_home_vpcol) = &
old_meta(dbcsr_slot_home_prow:dbcsr_slot_home_vpcol)
CALL dbcsr_make_dense_low(tmp_mat, images%mats(mat_row, mat_col), &
array_data(und_local_vrows), array_data(und_local_vcols), &
und_row_blk_offsets, und_col_blk_offsets, &
array_data(dense_local_vrows), &
array_data(dense_local_vcols), &
array_data(new_template%row_blk_offset), &
array_data(new_template%col_blk_offset), &
array_data(row_map), array_data(col_map), join_rows, join_cols)
!
CALL dbcsr_index_prune_deleted(images%mats(mat_row, mat_col))
!
CALL dbcsr_release(tmp_mat)
IF (dbg) THEN
cs = dbcsr_checksum(images%mats(mat_row, mat_col))
WRITE (*, *) routineN//" cs pst", cs
END IF
END DO
END DO
CALL dbcsr_image_dist_release(images%image_dist)
images%image_dist = new_rdist
CALL dbcsr_image_dist_hold(images%image_dist)
CALL timestop(handle)
END SUBROUTINE dbcsr_make_images_dense
SUBROUTINE multiply_cannon(left_set, right_set, product_matrix, &
retain_sparsity, &
filter_eps, flop, keep_product_data)
!! Multiplies two DBCSR matrices
TYPE(dbcsr_2d_array_type), POINTER :: left_set, right_set
!! set of imaged left matrices
!! set of imaged right matrices
TYPE(dbcsr_type), INTENT(INOUT) :: product_matrix
!! DBCSR product matrix
LOGICAL, INTENT(IN), OPTIONAL :: retain_sparsity
!! retain the sparsity of the existing product matrix; default is no
REAL(kind=real_8), INTENT(in), OPTIONAL :: filter_eps
INTEGER(KIND=int_8), INTENT(OUT) :: flop
!! effective flop
LOGICAL, INTENT(IN) :: keep_product_data
CHARACTER(len=*), PARAMETER :: routineN = 'multiply_cannon'
INTEGER, PARAMETER :: idata = 1, ileft = 0, imeta = 2, &
iright = 2
INTEGER :: data_type, data_type_byte, handle, handle1, handle2, handle3, i, ithread, &
left_col_image, left_col_mult, left_col_nimages, left_dst_icol, left_dst_irow, &
left_dst_p, left_dst_pcol, left_dst_prow, left_dst_vcol, left_dst_vrow, left_max_nblks, &
left_max_nze, left_myfirstvcol, left_myfirstvrow, left_mypcol, left_myprow, left_npcols, &
left_nprows, left_recv_icol, left_recv_irow, left_recv_p, left_recv_pcol, left_recv_prow, &
left_recv_vcol, left_recv_vrow, left_row_image, left_row_mult, left_row_nimages, &
left_send_icol, left_send_irow, left_send_p, left_send_pcol, left_send_prow
INTEGER :: left_send_vcol, left_send_vrow, left_src_icol, left_src_irow, left_src_p, &
left_src_pcol, left_src_prow, left_src_vcol, left_src_vrow, metronome, min_nimages, &
mynode, nblkrows_used, nsteps_k, nthreads, numnodes, nvirt_k, &
output_unit, right_col_image, right_col_mult, right_col_nimages, right_dst_icol, &
right_dst_irow, right_dst_p, right_dst_pcol, right_dst_prow, right_dst_vcol, &
right_dst_vrow, right_max_nblks, right_max_nze, right_myfirstvcol, right_myfirstvrow, &
right_mypcol, right_myprow, right_npcols, right_nprows, right_recv_icol, right_recv_irow
INTEGER :: right_recv_p, right_recv_pcol, right_recv_prow, right_recv_vcol, right_recv_vrow, &
right_row_image, right_row_mult, right_row_nimages, right_send_icol, right_send_irow, &
right_send_p, right_send_pcol, right_send_prow, right_send_vcol, right_send_vrow, &
right_src_icol, right_src_irow, right_src_p, right_src_pcol, right_src_prow, &
right_src_vcol, right_src_vrow, row, size_guess, size_guess_init, stat, threads_finished, &
threads_finished_read, v_ki, v_ki_left, v_ki_right
INTEGER(KIND=int_8) :: flop_single, flop_total, mem
INTEGER, ALLOCATABLE, DIMENSION(:) :: row_counts, total_row_counts
INTEGER, ALLOCATABLE, DIMENSION(:, :, :) :: left_sizes, my_sizes, right_sizes
INTEGER, ALLOCATABLE, DIMENSION(:, :, :, :) :: all_sizes
INTEGER, DIMENSION(:), POINTER, CONTIGUOUS :: col_blk_sizes2enum, enum2col_blk_sizes, &
enum2row_blk_sizes, &
left_index_rp, left_index_sp, m_sizes, n_sizes, &
right_index_rp, right_index_sp, &
row_blk_sizes2enum
INTEGER, DIMENSION(:), POINTER, CONTIGUOUS :: k_sizes
INTEGER, DIMENSION(:, :), POINTER, CONTIGUOUS :: left_pgrid, product_pgrid, right_pgrid
INTEGER, SAVE :: mult_id = 0
LOGICAL :: keep_sparsity, list_indexing, &
otf_filtering
REAL(kind=sp), ALLOCATABLE, DIMENSION(:) :: left_norms, right_norms, &
row_max_epss
REAL(kind=sp) :: filter_eps_sp
TYPE(dbcsr_2d_array_type), POINTER :: left_buffer_2, left_buffer_calc, &
left_buffer_comm, right_buffer_2, right_buffer_calc, right_buffer_comm
TYPE(dbcsr_data_obj) :: left_data_rp, left_data_sp, &
right_data_rp, right_data_sp
TYPE(dbcsr_data_obj), POINTER :: trs_stackbuf_calc, &
trs_stackbuf_comm
TYPE(dbcsr_data_obj), TARGET :: trs_stackbuf_1, trs_stackbuf_2
TYPE(dbcsr_mm_multrec_type_p), DIMENSION(:), ALLOCATABLE :: multrec
TYPE(dbcsr_mp_obj) :: left_mp_obj, product_mp_obj, &
right_mp_obj
TYPE(mp_comm_type) :: grp, mp_group
TYPE(mp_request_type), DIMENSION(:), ALLOCATABLE :: left_data_rr, left_data_sr, left_index_rr, &
left_index_sr, right_data_rr, right_data_sr, right_index_rr, right_index_sr
! ---------------------------------------------------------------------------
CALL timeset(routineN, handle)
NULLIFY (trs_stackbuf_calc, trs_stackbuf_comm)
NULLIFY (row_blk_sizes2enum, enum2row_blk_sizes)
NULLIFY (col_blk_sizes2enum, enum2col_blk_sizes)
NULLIFY (k_sizes)
!
ALLOCATE (left_buffer_2, right_buffer_2)
mult_id = mult_id + 1
IF (PRESENT(retain_sparsity)) THEN
keep_sparsity = retain_sparsity
ELSE
keep_sparsity = .FALSE.
END IF
otf_filtering = PRESENT(filter_eps)
!$OMP PARALLEL DEFAULT (NONE) &
!$OMP SHARED (multrec, nthreads, product_matrix)
!$OMP MASTER
nthreads = 1
!$ nthreads = OMP_GET_NUM_THREADS()
IF (.NOT. ASSOCIATED(product_matrix%wms)) &
DBCSR_ABORT("Work matrices do not exist")
IF (SIZE(product_matrix%wms) .NE. nthreads) &
DBCSR_ABORT("Work matrices not correctly sized.")
ALLOCATE (multrec(0:nthreads - 1))
!$OMP END MASTER
!$OMP END PARALLEL
output_unit = default_output_unit
flop_total = 0
! Set up variables
data_type = dbcsr_get_data_type(product_matrix)
data_type_byte = dbcsr_datatype_sizeof(data_type)
left_row_nimages = left_set%image_dist%i%row_decimation
left_row_mult = left_set%image_dist%i%row_multiplicity
left_col_nimages = left_set%image_dist%i%col_decimation
left_col_mult = left_set%image_dist%i%col_multiplicity
right_row_nimages = right_set%image_dist%i%row_decimation
right_row_mult = right_set%image_dist%i%row_multiplicity
right_col_nimages = right_set%image_dist%i%col_decimation
right_col_mult = right_set%image_dist%i%col_multiplicity
left_mp_obj = dbcsr_distribution_mp(left_set%image_dist%i%main)
right_mp_obj = dbcsr_distribution_mp(right_set%image_dist%i%main)
product_mp_obj = dbcsr_distribution_mp(product_matrix%dist)
numnodes = dbcsr_mp_numnodes(product_mp_obj)
mynode = dbcsr_mp_mynode(product_mp_obj)
left_nprows = dbcsr_mp_nprows(left_mp_obj)
left_npcols = dbcsr_mp_npcols(left_mp_obj)
left_myprow = dbcsr_mp_myprow(left_mp_obj)
left_mypcol = dbcsr_mp_mypcol(left_mp_obj)
left_myfirstvrow = dbcsr_mp_myprow(left_mp_obj)*left_row_nimages
left_myfirstvcol = dbcsr_mp_mypcol(left_mp_obj)*left_col_nimages
right_nprows = dbcsr_mp_nprows(right_mp_obj)
right_npcols = dbcsr_mp_npcols(right_mp_obj)
right_myprow = dbcsr_mp_myprow(right_mp_obj)
right_mypcol = dbcsr_mp_mypcol(right_mp_obj)
right_myfirstvrow = dbcsr_mp_myprow(right_mp_obj)*right_row_nimages
right_myfirstvcol = dbcsr_mp_mypcol(right_mp_obj)*right_col_nimages
mp_group = dbcsr_mp_group(product_mp_obj)
left_pgrid => dbcsr_mp_pgrid(left_mp_obj)
right_pgrid => dbcsr_mp_pgrid(right_mp_obj)
product_pgrid => dbcsr_mp_pgrid(product_mp_obj)
CALL dbcsr_mp_grid_setup(product_mp_obj)
CALL dbcsr_mp_grid_setup(left_mp_obj)
CALL dbcsr_mp_grid_setup(right_mp_obj)
!
! Dummy checks
! left/right matching
IF (left_col_nimages .NE. right_row_mult) &
DBCSR_ABORT("Left/Right image mismatch")
IF (left_col_mult .NE. right_row_nimages) &
DBCSR_ABORT("Left/Right image mismatch")
IF (left_col_nimages*left_npcols .NE. right_row_nimages*right_nprows) &
DBCSR_ABORT("Left/Right total mismatch")
! product/left matching
IF (left_row_mult*dbcsr_mp_nprows(product_mp_obj) .NE. left_row_nimages*left_nprows) &
DBCSR_ABORT("Product/Left total mismatch")
! product/left matching
IF (right_col_mult*dbcsr_mp_npcols(product_mp_obj) .NE. right_col_nimages*right_npcols) &
DBCSR_ABORT("Product/Right total mismatch")
! Limitations
IF (left_row_nimages .NE. 1) &
DBCSR_ABORT("Product/Left matrix process grid mismatch")
IF (left_row_mult .NE. 1) &
DBCSR_ABORT("Product/Left matrix process grid mismatch")
IF (right_col_nimages .NE. 1) &
DBCSR_ABORT("Product/Right matrix process grid mismatch")
IF (right_col_mult .NE. 1) &