-
Notifications
You must be signed in to change notification settings - Fork 1
/
domain_submatrix_methods.F
1678 lines (1360 loc) · 63.2 KB
/
domain_submatrix_methods.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
!--------------------------------------------------------------------------------------------------!
! CP2K: A general program to perform molecular dynamics simulations !
! Copyright 2000-2024 CP2K developers group <https://cp2k.org> !
! !
! SPDX-License-Identifier: GPL-2.0-or-later !
!--------------------------------------------------------------------------------------------------!
! **************************************************************************************************
!> \brief Subroutines to handle submatrices
!> \par History
!> 2013.01 created [Rustam Z Khaliullin]
!> \author Rustam Z Khaliullin
! **************************************************************************************************
MODULE domain_submatrix_methods
USE cp_dbcsr_api, ONLY: &
dbcsr_distribution_get, dbcsr_distribution_type, dbcsr_filter, dbcsr_finalize, &
dbcsr_get_block_p, dbcsr_get_info, dbcsr_get_matrix_type, dbcsr_get_stored_coordinates, &
dbcsr_iterator_blocks_left, dbcsr_iterator_next_block, dbcsr_iterator_start, &
dbcsr_iterator_stop, dbcsr_iterator_type, dbcsr_nblkcols_total, dbcsr_nblkrows_total, &
dbcsr_reserve_block2d, dbcsr_type, dbcsr_type_antisymmetric, dbcsr_type_no_symmetry, &
dbcsr_type_symmetric, dbcsr_work_create
USE cp_log_handling, ONLY: cp_get_default_logger,&
cp_logger_get_default_unit_nr,&
cp_logger_type
USE domain_submatrix_types, ONLY: domain_map_type,&
domain_submatrix_type,&
select_row_col
USE kinds, ONLY: dp
USE message_passing, ONLY: mp_comm_null,&
mp_comm_type
#include "./base/base_uses.f90"
IMPLICIT NONE
PRIVATE
CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'domain_submatrix_methods'
PUBLIC :: copy_submatrices, copy_submatrix_data, &
release_submatrices, multiply_submatrices, add_submatrices, &
construct_submatrices, init_submatrices, &
construct_dbcsr_from_submatrices, &
set_submatrices, &
print_submatrices, maxnorm_submatrices
INTERFACE init_submatrices
MODULE PROCEDURE init_submatrices_0d
MODULE PROCEDURE init_submatrices_1d
MODULE PROCEDURE init_submatrices_2d
END INTERFACE
INTERFACE set_submatrices
MODULE PROCEDURE set_submatrix_array
MODULE PROCEDURE set_submatrix
END INTERFACE
INTERFACE copy_submatrices
MODULE PROCEDURE copy_submatrix_array
MODULE PROCEDURE copy_submatrix
END INTERFACE
INTERFACE release_submatrices
MODULE PROCEDURE release_submatrix_array
MODULE PROCEDURE release_submatrix
END INTERFACE
INTERFACE multiply_submatrices
MODULE PROCEDURE multiply_submatrices_once
MODULE PROCEDURE multiply_submatrices_array
END INTERFACE
INTERFACE add_submatrices
MODULE PROCEDURE add_submatrices_once
MODULE PROCEDURE add_submatrices_array
END INTERFACE
CONTAINS
! **************************************************************************************************
!> \brief ...
!> \param subm ...
! **************************************************************************************************
SUBROUTINE init_submatrices_0d(subm)
TYPE(domain_submatrix_type), INTENT(INOUT) :: subm
subm%domain = -1
subm%nbrows = -1
subm%nbcols = -1
subm%nrows = -1
subm%ncols = -1
subm%nnodes = -1
subm%group = mp_comm_null
END SUBROUTINE init_submatrices_0d
! **************************************************************************************************
!> \brief ...
!> \param subm ...
! **************************************************************************************************
SUBROUTINE init_submatrices_1d(subm)
TYPE(domain_submatrix_type), DIMENSION(:), &
INTENT(INOUT) :: subm
subm(:)%domain = -1
subm(:)%nbrows = -1
subm(:)%nbcols = -1
subm(:)%nrows = -1
subm(:)%ncols = -1
subm(:)%nnodes = -1
subm(:)%group = mp_comm_null
END SUBROUTINE init_submatrices_1d
! **************************************************************************************************
!> \brief ...
!> \param subm ...
! **************************************************************************************************
SUBROUTINE init_submatrices_2d(subm)
TYPE(domain_submatrix_type), DIMENSION(:, :), &
INTENT(INOUT) :: subm
subm(:, :)%domain = -1
subm(:, :)%nbrows = -1
subm(:, :)%nbcols = -1
subm(:, :)%nrows = -1
subm(:, :)%ncols = -1
subm(:, :)%nnodes = -1
subm(:, :)%group = mp_comm_null
END SUBROUTINE init_submatrices_2d
! **************************************************************************************************
!> \brief ...
!> \param original ...
!> \param copy ...
!> \param copy_data ...
! **************************************************************************************************
SUBROUTINE copy_submatrix_array(original, copy, copy_data)
TYPE(domain_submatrix_type), DIMENSION(:), &
INTENT(IN) :: original
TYPE(domain_submatrix_type), DIMENSION(:), &
INTENT(INOUT) :: copy
LOGICAL, INTENT(IN) :: copy_data
CHARACTER(len=*), PARAMETER :: routineN = 'copy_submatrix_array'
INTEGER :: handle, idomain, ndomains, ndomainsB
CALL timeset(routineN, handle)
ndomains = SIZE(original)
ndomainsB = SIZE(copy)
CPASSERT(ndomains .EQ. ndomainsB)
copy(:)%nnodes = original(:)%nnodes
copy(:)%group = original(:)%group
DO idomain = 1, ndomains
IF (original(idomain)%domain .GT. 0) THEN
CALL copy_submatrix(original(idomain), copy(idomain), copy_data)
END IF
END DO ! loop over domains
CALL timestop(handle)
END SUBROUTINE copy_submatrix_array
! **************************************************************************************************
!> \brief ...
!> \param original ...
!> \param copy ...
!> \param copy_data ...
! **************************************************************************************************
SUBROUTINE copy_submatrix(original, copy, copy_data)
TYPE(domain_submatrix_type), INTENT(IN) :: original
TYPE(domain_submatrix_type), INTENT(INOUT) :: copy
LOGICAL, INTENT(IN) :: copy_data
CHARACTER(len=*), PARAMETER :: routineN = 'copy_submatrix'
INTEGER :: handle, icol, irow
CALL timeset(routineN, handle)
copy%domain = original%domain
copy%nnodes = original%nnodes
copy%group = original%group
IF (original%domain .GT. 0) THEN
copy%nbrows = original%nbrows
copy%nbcols = original%nbcols
copy%nrows = original%nrows
copy%ncols = original%ncols
IF (.NOT. ALLOCATED(copy%dbcsr_row)) THEN
ALLOCATE (copy%dbcsr_row(original%nbrows))
ELSE
IF (SIZE(copy%dbcsr_row) .NE. SIZE(original%dbcsr_row)) THEN
DEALLOCATE (copy%dbcsr_row)
ALLOCATE (copy%dbcsr_row(original%nbrows))
END IF
END IF
IF (.NOT. ALLOCATED(copy%dbcsr_col)) THEN
ALLOCATE (copy%dbcsr_col(original%nbcols))
ELSE
IF (SIZE(copy%dbcsr_col) .NE. SIZE(original%dbcsr_col)) THEN
DEALLOCATE (copy%dbcsr_col)
ALLOCATE (copy%dbcsr_col(original%nbcols))
END IF
END IF
IF (.NOT. ALLOCATED(copy%size_brow)) THEN
ALLOCATE (copy%size_brow(original%nbrows))
ELSE
IF (SIZE(copy%size_brow) .NE. SIZE(original%size_brow)) THEN
DEALLOCATE (copy%size_brow)
ALLOCATE (copy%size_brow(original%nbrows))
END IF
END IF
IF (.NOT. ALLOCATED(copy%size_bcol)) THEN
ALLOCATE (copy%size_bcol(original%nbcols))
ELSE
IF (SIZE(copy%size_bcol) .NE. SIZE(original%size_bcol)) THEN
DEALLOCATE (copy%size_bcol)
ALLOCATE (copy%size_bcol(original%nbcols))
END IF
END IF
DO irow = 1, original%nbrows
copy%dbcsr_row(irow) = original%dbcsr_row(irow)
copy%size_brow(irow) = original%size_brow(irow)
END DO
DO icol = 1, original%nbcols
copy%dbcsr_col(icol) = original%dbcsr_col(icol)
copy%size_bcol(icol) = original%size_bcol(icol)
END DO
IF (copy_data) THEN
CALL copy_submatrix_data(original%mdata, copy)
END IF
END IF ! do not copy empty submatrix
CALL timestop(handle)
END SUBROUTINE copy_submatrix
! **************************************************************************************************
!> \brief ...
!> \param array ...
!> \param copy ...
! **************************************************************************************************
SUBROUTINE copy_submatrix_data(array, copy)
REAL(KIND=dp), DIMENSION(:, :), INTENT(IN) :: array
TYPE(domain_submatrix_type), INTENT(INOUT) :: copy
CHARACTER(len=*), PARAMETER :: routineN = 'copy_submatrix_data'
INTEGER :: ds1, ds2, handle, ms1, ms2
CALL timeset(routineN, handle)
CPASSERT(copy%domain .GT. 0)
ds1 = SIZE(array, 1)
ds2 = SIZE(array, 2)
IF (.NOT. ALLOCATED(copy%mdata)) THEN
ALLOCATE (copy%mdata(ds1, ds2))
ELSE
ms1 = SIZE(copy%mdata, 1)
ms2 = SIZE(copy%mdata, 2)
IF ((ds1 .NE. ms1) .OR. (ds2 .NE. ms2)) THEN
DEALLOCATE (copy%mdata)
ALLOCATE (copy%mdata(ds1, ds2))
END IF
END IF
copy%mdata(:, :) = array(:, :)
CALL timestop(handle)
END SUBROUTINE copy_submatrix_data
! **************************************************************************************************
!> \brief ...
!> \param submatrices ...
!> \param scalar ...
! **************************************************************************************************
SUBROUTINE set_submatrix_array(submatrices, scalar)
TYPE(domain_submatrix_type), DIMENSION(:), &
INTENT(INOUT) :: submatrices
REAL(KIND=dp), INTENT(IN) :: scalar
CHARACTER(len=*), PARAMETER :: routineN = 'set_submatrix_array'
INTEGER :: handle, idomain, ndomains
CALL timeset(routineN, handle)
ndomains = SIZE(submatrices)
DO idomain = 1, ndomains
IF (submatrices(idomain)%domain .GT. 0) THEN
CALL set_submatrix(submatrices(idomain), scalar)
END IF
END DO ! loop over domains
CALL timestop(handle)
END SUBROUTINE set_submatrix_array
! **************************************************************************************************
!> \brief ...
!> \param submatrix ...
!> \param scalar ...
! **************************************************************************************************
SUBROUTINE set_submatrix(submatrix, scalar)
TYPE(domain_submatrix_type), INTENT(INOUT) :: submatrix
REAL(KIND=dp), INTENT(IN) :: scalar
CHARACTER(len=*), PARAMETER :: routineN = 'set_submatrix'
INTEGER :: ds1, ds2, handle, ms1, ms2
CALL timeset(routineN, handle)
CPASSERT(submatrix%domain .GT. 0)
CPASSERT(submatrix%nrows .GT. 0)
CPASSERT(submatrix%ncols .GT. 0)
ds1 = submatrix%nrows
ds2 = submatrix%ncols
IF (.NOT. ALLOCATED(submatrix%mdata)) THEN
ALLOCATE (submatrix%mdata(ds1, ds2))
ELSE
ms1 = SIZE(submatrix%mdata, 1)
ms2 = SIZE(submatrix%mdata, 2)
IF ((ds1 .NE. ms1) .OR. (ds2 .NE. ms2)) THEN
DEALLOCATE (submatrix%mdata)
ALLOCATE (submatrix%mdata(ds1, ds2))
END IF
END IF
submatrix%mdata(:, :) = scalar
CALL timestop(handle)
END SUBROUTINE set_submatrix
! **************************************************************************************************
!> \brief ...
!> \param subm ...
! **************************************************************************************************
SUBROUTINE release_submatrix_array(subm)
TYPE(domain_submatrix_type), DIMENSION(:), &
INTENT(INOUT) :: subm
CHARACTER(len=*), PARAMETER :: routineN = 'release_submatrix_array'
INTEGER :: handle, idomain, ndomains
CALL timeset(routineN, handle)
ndomains = SIZE(subm)
DO idomain = 1, ndomains
CALL release_submatrix(subm(idomain))
END DO ! loop over domains
CALL timestop(handle)
END SUBROUTINE release_submatrix_array
! **************************************************************************************************
!> \brief ...
!> \param subm ...
! **************************************************************************************************
SUBROUTINE release_submatrix(subm)
TYPE(domain_submatrix_type), INTENT(INOUT) :: subm
CHARACTER(len=*), PARAMETER :: routineN = 'release_submatrix'
INTEGER :: handle
CALL timeset(routineN, handle)
subm%domain = -1
subm%nbrows = -1
subm%nbcols = -1
subm%nrows = -1
subm%ncols = -1
subm%nnodes = -1
subm%group = mp_comm_null
IF (ALLOCATED(subm%dbcsr_row)) THEN
DEALLOCATE (subm%dbcsr_row)
END IF
IF (ALLOCATED(subm%dbcsr_col)) THEN
DEALLOCATE (subm%dbcsr_col)
END IF
IF (ALLOCATED(subm%size_brow)) THEN
DEALLOCATE (subm%size_brow)
END IF
IF (ALLOCATED(subm%size_bcol)) THEN
DEALLOCATE (subm%size_bcol)
END IF
IF (ALLOCATED(subm%mdata)) THEN
DEALLOCATE (subm%mdata)
END IF
CALL timestop(handle)
END SUBROUTINE release_submatrix
! more complex routine might be necessary if submatrices are distributed
! **************************************************************************************************
!> \brief ...
!> \param transA ...
!> \param transB ...
!> \param alpha ...
!> \param A ...
!> \param B ...
!> \param beta ...
!> \param C ...
! **************************************************************************************************
SUBROUTINE multiply_submatrices_once(transA, transB, alpha, A, B, beta, C)
CHARACTER, INTENT(IN) :: transA, transB
REAL(KIND=dp), INTENT(IN) :: alpha
TYPE(domain_submatrix_type), INTENT(IN) :: A, B
REAL(KIND=dp), INTENT(IN) :: beta
TYPE(domain_submatrix_type), INTENT(INOUT) :: C
CHARACTER(len=*), PARAMETER :: routineN = 'multiply_submatrices_once'
INTEGER :: cs1, cs2, handle, icol, irow, K, K1, &
LDA, LDB, LDC, M, Mblocks, N, Nblocks
LOGICAL :: NOTA, NOTB
CALL timeset(routineN, handle)
CPASSERT(A%domain .GT. 0)
CPASSERT(B%domain .GT. 0)
CPASSERT(C%domain .GT. 0)
LDA = SIZE(A%mdata, 1)
LDB = SIZE(B%mdata, 1)
NOTA = (transA .EQ. 'N') .OR. (transA .EQ. 'n')
NOTB = (transB .EQ. 'N') .OR. (transB .EQ. 'n')
IF (NOTA) THEN
M = A%nrows
K = A%ncols
Mblocks = A%nbrows
ELSE
M = A%ncols
K = A%nrows
Mblocks = A%nbcols
END IF
IF (NOTB) THEN
K1 = B%nrows
N = B%ncols
Nblocks = B%nbcols
ELSE
K1 = B%ncols
N = B%nrows
Nblocks = B%nbrows
END IF
! these checks are for debugging only
CPASSERT(K .EQ. K1)
! conform C matrix
C%nrows = M
C%ncols = N
C%nbrows = Mblocks
C%nbcols = Nblocks
IF (ALLOCATED(C%dbcsr_row)) THEN
DEALLOCATE (C%dbcsr_row)
END IF
ALLOCATE (C%dbcsr_row(C%nbrows))
IF (ALLOCATED(C%dbcsr_col)) THEN
DEALLOCATE (C%dbcsr_col)
END IF
ALLOCATE (C%dbcsr_col(C%nbcols))
IF (ALLOCATED(C%size_brow)) THEN
DEALLOCATE (C%size_brow)
END IF
ALLOCATE (C%size_brow(C%nbrows))
IF (ALLOCATED(C%size_bcol)) THEN
DEALLOCATE (C%size_bcol)
END IF
ALLOCATE (C%size_bcol(C%nbcols))
DO irow = 1, C%nbrows
IF (NOTA) THEN
C%dbcsr_row(irow) = A%dbcsr_row(irow)
C%size_brow(irow) = A%size_brow(irow)
ELSE
C%dbcsr_row(irow) = A%dbcsr_col(irow)
C%size_brow(irow) = A%size_bcol(irow)
END IF
END DO
DO icol = 1, C%nbcols
IF (NOTB) THEN
C%dbcsr_col(icol) = B%dbcsr_col(icol)
C%size_bcol(icol) = B%size_bcol(icol)
ELSE
C%dbcsr_col(icol) = B%dbcsr_row(icol)
C%size_bcol(icol) = B%size_brow(icol)
END IF
END DO
IF (.NOT. ALLOCATED(C%mdata)) THEN
!!! cannot use non-zero beta if C is not allocated
CPASSERT(beta .EQ. 0.0_dp)
ALLOCATE (C%mdata(C%nrows, C%ncols))
ELSE
cs1 = SIZE(C%mdata, 1)
cs2 = SIZE(C%mdata, 2)
IF ((C%nrows .NE. cs1) .OR. (C%ncols .NE. cs2)) THEN
!!! cannot deallocate data if beta is non-zero
CPASSERT(beta .EQ. 0.0_dp)
DEALLOCATE (C%mdata)
ALLOCATE (C%mdata(C%nrows, C%ncols))
END IF
END IF
LDC = C%nrows
CALL DGEMM(transA, transB, M, N, K, alpha, A%mdata, LDA, B%mdata, LDB, beta, C%mdata, LDC)
C%nnodes = A%nnodes
C%group = A%group
CALL timestop(handle)
END SUBROUTINE multiply_submatrices_once
! **************************************************************************************************
!> \brief ...
!> \param transA ...
!> \param transB ...
!> \param alpha ...
!> \param A ...
!> \param B ...
!> \param beta ...
!> \param C ...
! **************************************************************************************************
SUBROUTINE multiply_submatrices_array(transA, transB, alpha, A, B, beta, C)
CHARACTER, INTENT(IN) :: transA, transB
REAL(KIND=dp), INTENT(IN) :: alpha
TYPE(domain_submatrix_type), DIMENSION(:), &
INTENT(IN) :: A, B
REAL(KIND=dp), INTENT(IN) :: beta
TYPE(domain_submatrix_type), DIMENSION(:), &
INTENT(INOUT) :: C
CHARACTER(len=*), PARAMETER :: routineN = 'multiply_submatrices_array'
INTEGER :: handle, idomain, idomainA, idomainB, &
ndomains, ndomainsB, ndomainsC
CALL timeset(routineN, handle)
ndomains = SIZE(A)
ndomainsB = SIZE(B)
ndomainsC = SIZE(C)
CPASSERT(ndomains .EQ. ndomainsB)
CPASSERT(ndomainsB .EQ. ndomainsC)
DO idomain = 1, ndomains
idomainA = A(idomain)%domain
idomainB = B(idomain)%domain
CPASSERT(idomainA .EQ. idomainB)
C(idomain)%domain = idomainA
! check if the submatrix exists
IF (idomainA .GT. 0) THEN
CALL multiply_submatrices_once(transA, transB, alpha, A(idomain), B(idomain), beta, C(idomain))
END IF ! submatrix for the domain exists
END DO ! loop over domains
CALL timestop(handle)
END SUBROUTINE multiply_submatrices_array
! more complex routine might be necessary if submatrices are distributed
! **************************************************************************************************
!> \brief ...
!> \param alpha ...
!> \param A ...
!> \param beta ...
!> \param B ...
!> \param transB ...
! **************************************************************************************************
SUBROUTINE add_submatrices_once(alpha, A, beta, B, transB)
REAL(KIND=dp), INTENT(IN) :: alpha
TYPE(domain_submatrix_type), INTENT(INOUT) :: A
REAL(KIND=dp), INTENT(IN) :: beta
TYPE(domain_submatrix_type), INTENT(IN) :: B
CHARACTER, INTENT(IN) :: transB
CHARACTER(len=*), PARAMETER :: routineN = 'add_submatrices_once'
INTEGER :: C1, C2, handle, icol, R1, R2
LOGICAL :: NOTB
CALL timeset(routineN, handle)
CPASSERT(A%domain .GT. 0)
CPASSERT(B%domain .GT. 0)
R1 = A%nrows
C1 = A%ncols
NOTB = (transB .EQ. 'N') .OR. (transB .EQ. 'n')
IF (NOTB) THEN
R2 = B%nrows
C2 = B%ncols
ELSE
R2 = B%ncols
C2 = B%nrows
END IF
! these checks are for debugging only
CPASSERT(C1 .EQ. C2)
CPASSERT(R1 .EQ. R2)
IF (NOTB) THEN
DO icol = 1, C1
A%mdata(:, icol) = alpha*A%mdata(:, icol) + beta*B%mdata(:, icol)
END DO
ELSE
DO icol = 1, C1
A%mdata(:, icol) = alpha*A%mdata(:, icol) + beta*B%mdata(icol, :)
END DO
END IF
CALL timestop(handle)
END SUBROUTINE add_submatrices_once
! **************************************************************************************************
!> \brief ...
!> \param alpha ...
!> \param A ...
!> \param beta ...
!> \param B ...
!> \param transB ...
! **************************************************************************************************
SUBROUTINE add_submatrices_array(alpha, A, beta, B, transB)
REAL(KIND=dp), INTENT(IN) :: alpha
TYPE(domain_submatrix_type), DIMENSION(:), &
INTENT(INOUT) :: A
REAL(KIND=dp), INTENT(IN) :: beta
TYPE(domain_submatrix_type), DIMENSION(:), &
INTENT(IN) :: B
CHARACTER, INTENT(IN) :: transB
CHARACTER(len=*), PARAMETER :: routineN = 'add_submatrices_array'
INTEGER :: handle, idomain, idomainA, idomainB, &
ndomains, ndomainsB
CALL timeset(routineN, handle)
ndomains = SIZE(A)
ndomainsB = SIZE(B)
CPASSERT(ndomains .EQ. ndomainsB)
DO idomain = 1, ndomains
idomainA = A(idomain)%domain
idomainB = B(idomain)%domain
CPASSERT(idomainA .EQ. idomainB)
! check if the submatrix exists
IF (idomainA .GT. 0) THEN
CALL add_submatrices_once(alpha, A(idomain), beta, B(idomain), transB)
END IF ! submatrix for the domain exists
END DO ! loop over domains
CALL timestop(handle)
END SUBROUTINE add_submatrices_array
! **************************************************************************************************
!> \brief Computes the max norm of the collection of submatrices
!> \param submatrices ...
!> \param norm ...
!> \par History
!> 2013.03 created [Rustam Z. Khaliullin]
!> \author Rustam Z. Khaliullin
! **************************************************************************************************
SUBROUTINE maxnorm_submatrices(submatrices, norm)
TYPE(domain_submatrix_type), DIMENSION(:), &
INTENT(IN) :: submatrices
REAL(KIND=dp), INTENT(OUT) :: norm
CHARACTER(len=*), PARAMETER :: routineN = 'maxnorm_submatrices'
INTEGER :: handle, idomain, ndomains
REAL(KIND=dp) :: curr_norm, send_norm
REAL(KIND=dp), ALLOCATABLE, DIMENSION(:) :: recv_norm
CALL timeset(routineN, handle)
send_norm = 0.0_dp
ndomains = SIZE(submatrices)
DO idomain = 1, ndomains
! check if the submatrix is local
IF (submatrices(idomain)%domain .GT. 0) THEN
curr_norm = MAXVAL(ABS(submatrices(idomain)%mdata))
IF (curr_norm .GT. send_norm) send_norm = curr_norm
END IF
END DO ! loop over domains
! communicate local norm to the other nodes
ALLOCATE (recv_norm(submatrices(1)%nnodes))
CALL submatrices(1)%group%allgather(send_norm, recv_norm)
norm = MAXVAL(recv_norm)
DEALLOCATE (recv_norm)
CALL timestop(handle)
END SUBROUTINE maxnorm_submatrices
! **************************************************************************************************
!> \brief Computes the sum of traces of the submatrix A.tr(B)
!> \param A ...
!> \param B ...
!> \param trace ...
!> \par History
!> 2013.03 created [Rustam Z. Khaliullin]
!> \author Rustam Z. Khaliullin
! **************************************************************************************************
SUBROUTINE trace_submatrices(A, B, trace)
TYPE(domain_submatrix_type), DIMENSION(:), &
INTENT(IN) :: A, B
REAL(KIND=dp), INTENT(OUT) :: trace
CHARACTER(len=*), PARAMETER :: routineN = 'trace_submatrices'
INTEGER :: domainA, domainB, handle, idomain, &
ndomainsA, ndomainsB
REAL(KIND=dp) :: curr_trace, send_trace
REAL(KIND=dp), ALLOCATABLE, DIMENSION(:) :: recv_trace
CALL timeset(routineN, handle)
send_trace = 0.0_dp
ndomainsA = SIZE(A)
ndomainsB = SIZE(B)
CPASSERT(ndomainsA .EQ. ndomainsB)
DO idomain = 1, ndomainsA
domainA = A(idomain)%domain
domainB = B(idomain)%domain
CPASSERT(domainA .EQ. domainB)
! check if the submatrix is local
IF (domainA .GT. 0) THEN
CPASSERT(A(idomain)%nrows .EQ. B(idomain)%nrows)
CPASSERT(A(idomain)%ncols .EQ. B(idomain)%ncols)
curr_trace = SUM(A(idomain)%mdata(:, :)*B(idomain)%mdata(:, :))
send_trace = send_trace + curr_trace
END IF
END DO ! loop over domains
! communicate local norm to the other nodes
ALLOCATE (recv_trace(A(1)%nnodes))
CALL A(1)%group%allgather(send_trace, recv_trace)
trace = SUM(recv_trace)
DEALLOCATE (recv_trace)
CALL timestop(handle)
END SUBROUTINE trace_submatrices
! **************************************************************************************************
!> \brief Constructs submatrices for each ALMO domain by collecting distributed
!> DBCSR blocks to local arrays
!> \param matrix ...
!> \param submatrix ...
!> \param distr_pattern ...
!> \param domain_map ...
!> \param node_of_domain ...
!> \param job_type ...
!> \par History
!> 2013.01 created [Rustam Z. Khaliullin]
!> \author Rustam Z. Khaliullin
! **************************************************************************************************
SUBROUTINE construct_submatrices(matrix, submatrix, distr_pattern, domain_map, &
node_of_domain, job_type)
TYPE(dbcsr_type), INTENT(IN) :: matrix
TYPE(domain_submatrix_type), DIMENSION(:), &
INTENT(INOUT) :: submatrix
TYPE(dbcsr_type), INTENT(IN) :: distr_pattern
TYPE(domain_map_type), INTENT(IN) :: domain_map
INTEGER, DIMENSION(:), INTENT(IN) :: node_of_domain
INTEGER, INTENT(IN) :: job_type
CHARACTER(len=*), PARAMETER :: routineN = 'construct_submatrices'
CHARACTER :: matrix_type
INTEGER :: block_node, block_offset, col, col_offset, col_size, dest_node, GroupID, handle, &
iBlock, icol, idomain, index_col, index_ec, index_er, index_row, index_sc, index_sr, &
iNode, ldesc, myNode, nblkcols_tot, nblkrows_tot, ndomains, ndomains2, nNodes, &
recv_size2_total, recv_size_total, row, row_size, send_size2_total, send_size_total, &
smcol, smrow, start_data
INTEGER, ALLOCATABLE, DIMENSION(:) :: first_col, first_row, offset2_block, offset_block, &
recv_data2, recv_offset2_cpu, recv_offset_cpu, recv_size2_cpu, recv_size_cpu, send_data2, &
send_offset2_cpu, send_offset_cpu, send_size2_cpu, send_size_cpu
INTEGER, ALLOCATABLE, DIMENSION(:, :) :: recv_descriptor, send_descriptor
INTEGER, DIMENSION(:), POINTER :: col_blk_size, row_blk_size
LOGICAL :: found, transp
REAL(KIND=dp) :: antifactor
REAL(KIND=dp), ALLOCATABLE, DIMENSION(:) :: recv_data, send_data
REAL(KIND=dp), DIMENSION(:), POINTER :: block_p
TYPE(dbcsr_distribution_type) :: pattern_dist
TYPE(mp_comm_type) :: group
!INTEGER, PARAMETER :: select_row_col = 1
!INTEGER, PARAMETER :: select_row = 2
! subm_row_size,&
! subm_col_size,&
CALL timeset(routineN, handle)
nblkrows_tot = dbcsr_nblkrows_total(matrix)
nblkcols_tot = dbcsr_nblkcols_total(matrix)
ndomains = nblkcols_tot ! RZK-warning not true for atomic distributions
CALL dbcsr_get_info(distr_pattern, distribution=pattern_dist)
CALL dbcsr_distribution_get(pattern_dist, numnodes=nNodes, group=GroupID, mynode=myNode)
CALL group%set_handle(groupid)
matrix_type = dbcsr_get_matrix_type(matrix)
ldesc = 2
ALLOCATE (send_descriptor(ldesc, nNodes))
ALLOCATE (recv_descriptor(ldesc, nNodes))
send_descriptor(:, :) = 0
! find: the number of blocks and their sizes that must be sent to each cpu
! loop over all domains
DO idomain = 1, ndomains
dest_node = node_of_domain(idomain)
! loop over those rows that have non-zero quencher
index_sr = 1 ! index start row
IF (idomain .GT. 1) index_sr = domain_map%index1(idomain - 1)
index_er = domain_map%index1(idomain) - 1 ! index end row
DO index_row = index_sr, index_er
row = domain_map%pairs(index_row, 1)
IF (job_type == select_row_col) THEN
! loop over those columns that have non-zero quencher
index_sc = 1 ! index start col
IF (idomain .GT. 1) index_sc = domain_map%index1(idomain - 1)
index_ec = domain_map%index1(idomain) - 1 ! index end col
ELSE
! fake loop
index_sc = 1 ! index start col
index_ec = 1 ! index end col
END IF
DO index_col = index_sc, index_ec
IF (job_type == select_row_col) THEN
col = domain_map%pairs(index_col, 1)
ELSE
col = idomain
END IF
transp = .FALSE.
CALL dbcsr_get_stored_coordinates(matrix, &
row, col, block_node)
IF (block_node .EQ. myNode) THEN
CALL dbcsr_get_block_p(matrix, row, col, block_p, found, row_size, col_size)
IF (found) THEN
send_descriptor(1, dest_node + 1) = send_descriptor(1, dest_node + 1) + 1
send_descriptor(2, dest_node + 1) = send_descriptor(2, dest_node + 1) + &
row_size*col_size
END IF
END IF
END DO ! loop over columns
END DO ! loop over rows
END DO
! simple but quadratically scaling procedure
! loop over local blocks
!CALL dbcsr_iterator_start(iter,matrix)
!DO WHILE (dbcsr_iterator_blocks_left(iter))
! CALL dbcsr_iterator_next_block(iter,row,col,data_p,&
! row_size=row_size,col_size=col_size)
! DO idomain = 1, ndomains
! IF (job_type==select_row_col) THEN
! domain_needs_block=(qblk_exists(domain_map,col,idomain)&
! .AND.qblk_exists(domain_map,row,idomain))
! ELSE
! domain_needs_block=(idomain==col&
! .AND.qblk_exists(domain_map,row,idomain))
! ENDIF
! IF (domain_needs_block) THEN
! transp=.FALSE.
! dest_node=node_of_domain(idomain)
! !CALL dbcsr_get_stored_coordinates(distr_pattern,&
! ! idomain, idomain, transp, dest_node)
! send_descriptor(1,dest_node+1)=send_descriptor(1,dest_node+1)+1
! send_descriptor(2,dest_node+1)=send_descriptor(2,dest_node+1)+&
! row_size*col_size
! ENDIF
! ENDDO
!ENDDO
!CALL dbcsr_iterator_stop(iter)
! communicate number of blocks and their sizes to the other nodes
CALL group%alltoall(send_descriptor, recv_descriptor, ldesc)
ALLOCATE (send_size_cpu(nNodes), send_offset_cpu(nNodes))
send_offset_cpu(1) = 0
send_size_cpu(1) = send_descriptor(2, 1)
DO iNode = 2, nNodes
send_size_cpu(iNode) = send_descriptor(2, iNode)
send_offset_cpu(iNode) = send_offset_cpu(iNode - 1) + &
send_size_cpu(iNode - 1)
END DO
send_size_total = send_offset_cpu(nNodes) + send_size_cpu(nNodes)
ALLOCATE (recv_size_cpu(nNodes), recv_offset_cpu(nNodes))
recv_offset_cpu(1) = 0
recv_size_cpu(1) = recv_descriptor(2, 1)
DO iNode = 2, nNodes
recv_size_cpu(iNode) = recv_descriptor(2, iNode)
recv_offset_cpu(iNode) = recv_offset_cpu(iNode - 1) + &
recv_size_cpu(iNode - 1)
END DO
recv_size_total = recv_offset_cpu(nNodes) + recv_size_cpu(nNodes)
ALLOCATE (send_size2_cpu(nNodes), send_offset2_cpu(nNodes))
send_offset2_cpu(1) = 0
send_size2_cpu(1) = 2*send_descriptor(1, 1)
DO iNode = 2, nNodes
send_size2_cpu(iNode) = 2*send_descriptor(1, iNode)
send_offset2_cpu(iNode) = send_offset2_cpu(iNode - 1) + &
send_size2_cpu(iNode - 1)
END DO