-
Notifications
You must be signed in to change notification settings - Fork 0
/
cp_dbcsr_operations.F
1540 lines (1336 loc) · 72.7 KB
/
cp_dbcsr_operations.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-2023 CP2K developers group <https://cp2k.org> !
! !
! SPDX-License-Identifier: GPL-2.0-or-later !
!--------------------------------------------------------------------------------------------------!
! **************************************************************************************************
!> \brief DBCSR operations in CP2K
!> \author Urban Borstnik
!> \date 2009-05-12
!> \version 0.8
!>
!> <b>Modification history:</b>
!> - Created 2009-05-12
!> - Generalized sm_fm_mulitply for matrices w/ different row/col block size (A. Bussy, 11.2018)
! **************************************************************************************************
MODULE cp_dbcsr_operations
USE mathlib, ONLY: lcm, gcd
USE cp_blacs_env, ONLY: cp_blacs_env_type
USE cp_cfm_types, ONLY: cp_cfm_type
USE dbcsr_api, ONLY: dbcsr_distribution_get, &
dbcsr_convert_sizes_to_offsets, dbcsr_add, &
dbcsr_complete_redistribute, dbcsr_copy, dbcsr_create, &
dbcsr_deallocate_matrix, &
dbcsr_desymmetrize, dbcsr_distribution_new, &
dbcsr_get_data_type, dbcsr_get_info, dbcsr_get_matrix_type, &
dbcsr_iterator_type, dbcsr_iterator_blocks_left, dbcsr_iterator_next_block, &
dbcsr_iterator_start, dbcsr_iterator_stop, &
dbcsr_multiply, dbcsr_norm, dbcsr_p_type, dbcsr_release, &
dbcsr_reserve_all_blocks, dbcsr_scale, dbcsr_type, &
dbcsr_valid_index, dbcsr_verify_matrix, &
dbcsr_distribution_type, dbcsr_distribution_release, &
dbcsr_norm_frobenius, &
dbcsr_type_antisymmetric, dbcsr_type_complex_8, dbcsr_type_no_symmetry, dbcsr_type_real_8, &
dbcsr_type_symmetric
USE cp_fm_basic_linalg, ONLY: cp_fm_gemm
USE cp_fm_struct, ONLY: cp_fm_struct_create, &
cp_fm_struct_release, &
cp_fm_struct_type
USE cp_fm_types, ONLY: cp_fm_create, &
cp_fm_get_info, &
cp_fm_release, &
cp_fm_to_fm, &
cp_fm_type
USE message_passing, ONLY: mp_para_env_type
USE distribution_2d_types, ONLY: distribution_2d_get, &
distribution_2d_type
USE kinds, ONLY: dp, default_string_length
USE message_passing, ONLY: mp_comm_type
!$ USE OMP_LIB, ONLY: omp_get_max_threads, omp_get_thread_num, omp_get_num_threads
#include "base/base_uses.f90"
IMPLICIT NONE
CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'cp_dbcsr_operations'
LOGICAL, PARAMETER :: debug_mod = .FALSE.
INTEGER, SAVE, PUBLIC :: max_elements_per_block = 32
PUBLIC :: dbcsr_multiply_local
! CP2K API emulation
PUBLIC :: copy_fm_to_dbcsr, copy_dbcsr_to_fm, &
copy_dbcsr_to_cfm, copy_cfm_to_dbcsr, &
cp_dbcsr_sm_fm_multiply, cp_dbcsr_plus_fm_fm_t, &
copy_dbcsr_to_fm_bc, copy_fm_to_dbcsr_bc, cp_fm_to_dbcsr_row_template, &
cp_dbcsr_m_by_n_from_template, cp_dbcsr_m_by_n_from_row_template, &
dbcsr_create_dist_r_unrot
! distribution_2d_type compatibility
PUBLIC :: cp_dbcsr_dist2d_to_dist
PUBLIC :: dbcsr_copy_columns_hack
! matrix set
PUBLIC :: dbcsr_allocate_matrix_set
PUBLIC :: dbcsr_deallocate_matrix_set
INTERFACE dbcsr_allocate_matrix_set
#:for ii in range(1, 6)
MODULE PROCEDURE allocate_dbcsr_matrix_set_${ii}$d
#:endfor
END INTERFACE
INTERFACE dbcsr_deallocate_matrix_set
#:for ii in range(1, 6)
MODULE PROCEDURE deallocate_dbcsr_matrix_set_${ii}$d
#:endfor
END INTERFACE
PRIVATE
CONTAINS
#:for fm, type, constr in [("fm", "REAL", "REAL"), ("cfm", "COMPLEX", "CMPLX")]
! **************************************************************************************************
!> \brief Copy a BLACS matrix to a dbcsr matrix.
!>
!> real_matrix=beta*real_matrix+alpha*fm
!> beta defaults to 0, alpha to 1
!> \param[in] fm full matrix
!> \param[out] matrix DBCSR matrix
!> \param[in] keep_sparsity (optional) retains the sparsity of the input
!> matrix
!> \date 2009-10-13
!> \par History
!> 2009-10-13 rewritten based on copy_dbcsr_to_fm
!> \author Urban Borstnik
!> \version 2.0
! **************************************************************************************************
SUBROUTINE copy_${fm}$_to_dbcsr(fm, matrix, keep_sparsity)
TYPE(cp_${fm}$_type), INTENT(IN) :: fm
TYPE(dbcsr_type), INTENT(INOUT) :: matrix
LOGICAL, INTENT(IN), OPTIONAL :: keep_sparsity
CHARACTER(LEN=*), PARAMETER :: routineN = 'copy_${fm}$_to_dbcsr'
TYPE(dbcsr_type) :: bc_mat
INTEGER :: handle
CALL timeset(routineN, handle)
CALL copy_${fm}$_to_dbcsr_bc(fm, bc_mat)
CALL dbcsr_complete_redistribute(bc_mat, matrix, keep_sparsity=keep_sparsity)
CALL dbcsr_release(bc_mat)
CALL timestop(handle)
END SUBROUTINE copy_${fm}$_to_dbcsr
! **************************************************************************************************
!> \brief Copy a BLACS matrix to a dbcsr matrix with a special block-cyclic distribution,
!> which requires no complete redistribution.
!> \param fm ...
!> \param bc_mat ...
! **************************************************************************************************
SUBROUTINE copy_${fm}$_to_dbcsr_bc(fm, bc_mat)
TYPE(cp_${fm}$_type), INTENT(IN) :: fm
TYPE(dbcsr_type), INTENT(INOUT) :: bc_mat
CHARACTER(LEN=*), PARAMETER :: routineN = 'copy_${fm}$_to_dbcsr_bc'
INTEGER :: col, handle, ncol_block, ncol_global, nrow_block, nrow_global, row
INTEGER, ALLOCATABLE, DIMENSION(:) :: first_col, first_row, last_col, last_row
INTEGER, DIMENSION(:), POINTER :: col_blk_size, row_blk_size
${type}$ (KIND=dp), DIMENSION(:, :), POINTER :: fm_block, dbcsr_block
TYPE(dbcsr_distribution_type) :: bc_dist
TYPE(dbcsr_iterator_type) :: iter
INTEGER, DIMENSION(:, :), POINTER :: pgrid
CALL timeset(routineN, handle)
#:if (type=="REAL")
IF (fm%use_sp) CPABORT("copy_${fm}$_to_dbcsr_bc: single precision not supported")
#:endif
! Create processor grid
pgrid => fm%matrix_struct%context%blacs2mpi
! Create a block-cyclic distribution compatible with the FM matrix.
nrow_block = fm%matrix_struct%nrow_block
ncol_block = fm%matrix_struct%ncol_block
nrow_global = fm%matrix_struct%nrow_global
ncol_global = fm%matrix_struct%ncol_global
NULLIFY (col_blk_size, row_blk_size)
CALL dbcsr_create_dist_block_cyclic(bc_dist, &
nrows=nrow_global, ncolumns=ncol_global, & ! Actual full matrix size
nrow_block=nrow_block, ncol_block=ncol_block, & ! BLACS parameters
group_handle=fm%matrix_struct%para_env%get_handle(), pgrid=pgrid, &
row_blk_sizes=row_blk_size, col_blk_sizes=col_blk_size) ! block-cyclic row/col sizes
! Create the block-cyclic DBCSR matrix
CALL dbcsr_create(bc_mat, "Block-cyclic ", bc_dist, &
dbcsr_type_no_symmetry, row_blk_size, col_blk_size, nze=0, &
reuse_arrays=.TRUE., data_type=dbcsr_type_${type.lower()}$_8)
CALL dbcsr_distribution_release(bc_dist)
! allocate all blocks
CALL dbcsr_reserve_all_blocks(bc_mat)
CALL calculate_fm_block_ranges(bc_mat, first_row, last_row, first_col, last_col)
! Copy the FM data to the block-cyclic DBCSR matrix. This step
! could be skipped with appropriate DBCSR index manipulation.
fm_block => fm%local_data
!$OMP PARALLEL DEFAULT(NONE) PRIVATE(iter, row, col, dbcsr_block) &
!$OMP SHARED(bc_mat, last_row, first_row, last_col, first_col, fm_block)
CALL dbcsr_iterator_start(iter, bc_mat)
DO WHILE (dbcsr_iterator_blocks_left(iter))
CALL dbcsr_iterator_next_block(iter, row, col, dbcsr_block)
dbcsr_block(:, :) = fm_block(first_row(row):last_row(row), first_col(col):last_col(col))
END DO
CALL dbcsr_iterator_stop(iter)
!$OMP END PARALLEL
CALL timestop(handle)
END SUBROUTINE copy_${fm}$_to_dbcsr_bc
! **************************************************************************************************
!> \brief Copy a DBCSR matrix to a BLACS matrix
!> \param[in] matrix DBCSR matrix
!> \param[out] fm full matrix
! **************************************************************************************************
SUBROUTINE copy_dbcsr_to_${fm}$ (matrix, fm)
TYPE(dbcsr_type), INTENT(IN) :: matrix
TYPE(cp_${fm}$_type), INTENT(IN) :: fm
CHARACTER(LEN=*), PARAMETER :: routineN = 'copy_dbcsr_to_${fm}$'
INTEGER, DIMENSION(:), POINTER :: col_blk_size, row_blk_size
INTEGER :: handle, ncol_block, nfullcols_total, group_handle, &
nfullrows_total, nrow_block
TYPE(dbcsr_type) :: bc_mat, matrix_nosym
TYPE(dbcsr_distribution_type) :: dist, bc_dist
CHARACTER(len=default_string_length) :: name
INTEGER, DIMENSION(:, :), POINTER :: pgrid
CALL timeset(routineN, handle)
! check compatibility
CALL dbcsr_get_info(matrix, &
name=name, &
distribution=dist, &
nfullrows_total=nfullrows_total, &
nfullcols_total=nfullcols_total)
CPASSERT(fm%matrix_struct%nrow_global == nfullrows_total)
CPASSERT(fm%matrix_struct%ncol_global == nfullcols_total)
! info about the full matrix
nrow_block = fm%matrix_struct%nrow_block
ncol_block = fm%matrix_struct%ncol_block
! Convert DBCSR to a block-cyclic
NULLIFY (col_blk_size, row_blk_size)
CALL dbcsr_distribution_get(dist, group=group_handle, pgrid=pgrid)
CALL dbcsr_create_dist_block_cyclic(bc_dist, &
nrows=nfullrows_total, ncolumns=nfullcols_total, &
nrow_block=nrow_block, ncol_block=ncol_block, &
group_handle=group_handle, pgrid=pgrid, &
row_blk_sizes=row_blk_size, col_blk_sizes=col_blk_size)
CALL dbcsr_create(bc_mat, "Block-cyclic"//name, bc_dist, &
dbcsr_type_no_symmetry, row_blk_size, col_blk_size, &
nze=0, data_type=dbcsr_get_data_type(matrix), &
reuse_arrays=.TRUE.)
CALL dbcsr_distribution_release(bc_dist)
CALL dbcsr_create(matrix_nosym, template=matrix, matrix_type="N")
CALL dbcsr_desymmetrize(matrix, matrix_nosym)
CALL dbcsr_complete_redistribute(matrix_nosym, bc_mat)
CALL dbcsr_release(matrix_nosym)
CALL copy_dbcsr_to_${fm}$_bc(bc_mat, fm)
CALL dbcsr_release(bc_mat)
CALL timestop(handle)
END SUBROUTINE copy_dbcsr_to_${fm}$
! **************************************************************************************************
!> \brief Copy a DBCSR_BLACS matrix to a BLACS matrix
!> \param bc_mat DBCSR matrix
!> \param[out] fm full matrix
! **************************************************************************************************
SUBROUTINE copy_dbcsr_to_${fm}$_bc(bc_mat, fm)
TYPE(dbcsr_type), INTENT(IN) :: bc_mat
TYPE(cp_${fm}$_type), INTENT(IN) :: fm
CHARACTER(LEN=*), PARAMETER :: routineN = 'copy_dbcsr_to_${fm}$_bc'
INTEGER :: col, handle, row
INTEGER, ALLOCATABLE, DIMENSION(:) :: first_col, first_row, last_col, last_row
${type}$ (KIND=dp), DIMENSION(:, :), POINTER :: dbcsr_block, fm_block
TYPE(dbcsr_iterator_type) :: iter
CALL timeset(routineN, handle)
#:if (type=="REAL")
IF (fm%use_sp) CPABORT("copy_dbcsr_to_${fm}$_bc: single precision not supported")
#:endif
CALL calculate_fm_block_ranges(bc_mat, first_row, last_row, first_col, last_col)
! Now copy data to the FM matrix
fm_block => fm%local_data
fm_block = ${constr}$ (0.0, KIND=dp)
!$OMP PARALLEL DEFAULT(NONE) PRIVATE(iter, row, col, dbcsr_block) &
!$OMP SHARED(bc_mat, last_row, first_row, last_col, first_col, fm_block)
CALL dbcsr_iterator_start(iter, bc_mat)
DO WHILE (dbcsr_iterator_blocks_left(iter))
CALL dbcsr_iterator_next_block(iter, row, col, dbcsr_block)
fm_block(first_row(row):last_row(row), first_col(col):last_col(col)) = dbcsr_block(:, :)
END DO
CALL dbcsr_iterator_stop(iter)
!$OMP END PARALLEL
CALL timestop(handle)
END SUBROUTINE copy_dbcsr_to_${fm}$_bc
#:endfor
! **************************************************************************************************
!> \brief Helper routine used to copy blocks from DBCSR into FM matrices and vice versa
!> \param bc_mat ...
!> \param first_row ...
!> \param last_row ...
!> \param first_col ...
!> \param last_col ...
!> \author Ole Schuett
! **************************************************************************************************
SUBROUTINE calculate_fm_block_ranges(bc_mat, first_row, last_row, first_col, last_col)
TYPE(dbcsr_type), INTENT(IN) :: bc_mat
INTEGER :: col, nblkcols_local, nblkcols_total, nblkrows_local, nblkrows_total, row
INTEGER, ALLOCATABLE, DIMENSION(:) :: first_col, first_row, last_col, &
last_row, local_col_sizes, &
local_row_sizes
INTEGER, DIMENSION(:), POINTER :: col_blk_size, local_cols, local_rows, &
row_blk_size
CALL dbcsr_get_info(bc_mat, &
nblkrows_total=nblkrows_total, &
nblkcols_total=nblkcols_total, &
nblkrows_local=nblkrows_local, &
nblkcols_local=nblkcols_local, &
local_rows=local_rows, &
local_cols=local_cols, &
row_blk_size=row_blk_size, &
col_blk_size=col_blk_size)
! calculate first_row and last_row
ALLOCATE (local_row_sizes(nblkrows_total))
local_row_sizes(:) = 0
IF (nblkrows_local .GE. 1) THEN
DO row = 1, nblkrows_local
local_row_sizes(local_rows(row)) = row_blk_size(local_rows(row))
END DO
END IF
ALLOCATE (first_row(nblkrows_total), last_row(nblkrows_total))
CALL dbcsr_convert_sizes_to_offsets(local_row_sizes, first_row, last_row)
! calculate first_col and last_col
ALLOCATE (local_col_sizes(nblkcols_total))
local_col_sizes(:) = 0
IF (nblkcols_local .GE. 1) THEN
DO col = 1, nblkcols_local
local_col_sizes(local_cols(col)) = col_blk_size(local_cols(col))
END DO
END IF
ALLOCATE (first_col(nblkcols_total), last_col(nblkcols_total))
CALL dbcsr_convert_sizes_to_offsets(local_col_sizes, first_col, last_col)
END SUBROUTINE calculate_fm_block_ranges
! **************************************************************************************************
!> \brief hack for dbcsr_copy_columns
!> \param matrix_b ...
!> \param matrix_a ...
!> \param ncol ...
!> \param source_start ...
!> \param target_start ...
!> \param para_env ...
!> \param blacs_env ...
!> \author vw
! **************************************************************************************************
SUBROUTINE dbcsr_copy_columns_hack(matrix_b, matrix_a, &
ncol, source_start, target_start, para_env, blacs_env)
TYPE(dbcsr_type), INTENT(INOUT) :: matrix_b
TYPE(dbcsr_type), INTENT(IN) :: matrix_a
INTEGER, INTENT(IN) :: ncol, source_start, target_start
TYPE(mp_para_env_type), POINTER :: para_env
TYPE(cp_blacs_env_type), POINTER :: blacs_env
INTEGER :: nfullcols_total, nfullrows_total
TYPE(cp_fm_struct_type), POINTER :: fm_struct
TYPE(cp_fm_type) :: fm_matrix_a, fm_matrix_b
NULLIFY (fm_struct)
CALL dbcsr_get_info(matrix_a, nfullrows_total=nfullrows_total, nfullcols_total=nfullcols_total)
CALL cp_fm_struct_create(fm_struct, context=blacs_env, nrow_global=nfullrows_total, &
ncol_global=nfullcols_total, para_env=para_env)
CALL cp_fm_create(fm_matrix_a, fm_struct, name="fm_matrix_a")
CALL cp_fm_struct_release(fm_struct)
CALL dbcsr_get_info(matrix_b, nfullrows_total=nfullrows_total, nfullcols_total=nfullcols_total)
CALL cp_fm_struct_create(fm_struct, context=blacs_env, nrow_global=nfullrows_total, &
ncol_global=nfullcols_total, para_env=para_env)
CALL cp_fm_create(fm_matrix_b, fm_struct, name="fm_matrix_b")
CALL cp_fm_struct_release(fm_struct)
CALL copy_dbcsr_to_fm(matrix_a, fm_matrix_a)
CALL copy_dbcsr_to_fm(matrix_b, fm_matrix_b)
CALL cp_fm_to_fm(fm_matrix_a, fm_matrix_b, ncol, source_start, target_start)
CALL copy_fm_to_dbcsr(fm_matrix_b, matrix_b)
CALL cp_fm_release(fm_matrix_a)
CALL cp_fm_release(fm_matrix_b)
END SUBROUTINE dbcsr_copy_columns_hack
! **************************************************************************************************
!> \brief Creates a DBCSR distribution from a distribution_2d
!> \param[in] dist2d distribution_2d
!> \param[out] dist DBCSR distribution
!> \par History
!> move form dbcsr_operation 01.2010
! **************************************************************************************************
SUBROUTINE cp_dbcsr_dist2d_to_dist(dist2d, dist)
TYPE(distribution_2d_type), INTENT(IN), TARGET :: dist2d
TYPE(dbcsr_distribution_type), INTENT(OUT) :: dist
INTEGER, DIMENSION(:, :), POINTER :: pgrid, col_dist_data, row_dist_data
TYPE(cp_blacs_env_type), POINTER :: blacs_env
TYPE(mp_para_env_type), POINTER :: para_env
TYPE(distribution_2d_type), POINTER :: dist2d_p
INTEGER, DIMENSION(:), POINTER :: row_dist, col_dist
dist2d_p => dist2d
CALL distribution_2d_get(dist2d_p, &
row_distribution=row_dist_data, &
col_distribution=col_dist_data, &
blacs_env=blacs_env)
CALL blacs_env%get(para_env=para_env, blacs2mpi=pgrid)
! map to 1D arrays
row_dist => row_dist_data(:, 1)
col_dist => col_dist_data(:, 1)
!row_cluster => row_dist_data(:, 2)
!col_cluster => col_dist_data(:, 2)
CALL dbcsr_distribution_new(dist, &
group=para_env%get_handle(), pgrid=pgrid, &
row_dist=row_dist, &
col_dist=col_dist)
END SUBROUTINE cp_dbcsr_dist2d_to_dist
! **************************************************************************************************
!> \brief multiply a dbcsr with a replicated array
!> c = alpha_scalar * A (dbscr) * b + c
!> \param[in] matrix_a DBSCR matrxx
!> \param[in] vec_b vectors b
!> \param[inout] vec_c vectors c
!> \param[in] ncol nbr of columns
!> \param[in] alpha alpha
!>
! **************************************************************************************************
SUBROUTINE dbcsr_multiply_local(matrix_a, vec_b, vec_c, ncol, alpha)
TYPE(dbcsr_type), INTENT(IN) :: matrix_a
REAL(dp), DIMENSION(:, :), INTENT(IN) :: vec_b
REAL(dp), DIMENSION(:, :), INTENT(INOUT) :: vec_c
INTEGER, INTENT(in), OPTIONAL :: ncol
REAL(dp), INTENT(IN), OPTIONAL :: alpha
CHARACTER(LEN=*), PARAMETER :: routineN = 'dbcsr_multiply_local'
INTEGER :: blk, col, coloff, my_ncol, row, rowoff, &
timing_handle
LOGICAL :: has_symm
REAL(dp) :: my_alpha, my_alpha2
REAL(dp), DIMENSION(:, :), POINTER :: data_d
TYPE(dbcsr_iterator_type) :: iter
CALL timeset(routineN, timing_handle)
my_alpha = 1.0_dp
IF (PRESENT(alpha)) my_alpha = alpha
my_ncol = SIZE(vec_b, 2)
IF (PRESENT(ncol)) my_ncol = ncol
my_alpha2 = 0.0_dp
IF (dbcsr_get_matrix_type(matrix_a) .EQ. dbcsr_type_symmetric) my_alpha2 = my_alpha
IF (dbcsr_get_matrix_type(matrix_a) .EQ. dbcsr_type_antisymmetric) my_alpha2 = -my_alpha
has_symm = (dbcsr_get_matrix_type(matrix_a) .EQ. dbcsr_type_symmetric .OR. &
dbcsr_get_matrix_type(matrix_a) .EQ. dbcsr_type_antisymmetric)
!$OMP PARALLEL DEFAULT(NONE) SHARED(matrix_a,vec_b,vec_c,ncol,my_alpha2,my_alpha,my_ncol,has_symm) &
!$OMP PRIVATE(iter,row,col,data_d,blk,rowoff,coloff)
CALL dbcsr_iterator_start(iter, matrix_a, read_only=.TRUE., dynamic=.TRUE., dynamic_byrows=.TRUE.)
DO WHILE (dbcsr_iterator_blocks_left(iter))
CALL dbcsr_iterator_next_block(iter, row, col, data_d, blk, row_offset=rowoff, col_offset=coloff)
IF (my_ncol .NE. 1) THEN
CALL dgemm('N', 'N', &
SIZE(data_d, 1), my_ncol, SIZE(data_d, 2), &
my_alpha, data_d(1, 1), SIZE(data_d, 1), &
vec_b(coloff, 1), SIZE(vec_b, 1), &
1.0_dp, vec_c(rowoff, 1), SIZE(vec_c, 1))
ELSE
CALL dgemv('N', SIZE(data_d, 1), SIZE(data_d, 2), &
my_alpha, data_d(1, 1), SIZE(data_d, 1), &
vec_b(coloff, 1), 1, &
1.0_dp, vec_c(rowoff, 1), 1)
END IF
END DO
CALL dbcsr_iterator_stop(iter)
!$OMP END PARALLEL
! FIXME ... in the symmetric case, the writes to vec_c depend on the column, not the row. This makes OMP-ing more difficult
! needs e.g. a buffer for vec_c and a reduction of that buffer.
IF (has_symm) THEN
CALL dbcsr_iterator_start(iter, matrix_a)
DO WHILE (dbcsr_iterator_blocks_left(iter))
CALL dbcsr_iterator_next_block(iter, row, col, data_d, blk, row_offset=rowoff, col_offset=coloff)
IF (row .NE. col) THEN
IF (my_ncol .NE. 1) THEN
CALL dgemm('T', 'N', &
SIZE(data_d, 2), my_ncol, SIZE(data_d, 1), &
my_alpha2, data_d(1, 1), SIZE(data_d, 1), &
vec_b(rowoff, 1), SIZE(vec_b, 1), &
1.0_dp, vec_c(coloff, 1), SIZE(vec_c, 1))
ELSE
CALL dgemv('T', SIZE(data_d, 1), SIZE(data_d, 2), &
my_alpha2, data_d(1, 1), SIZE(data_d, 1), &
vec_b(rowoff, 1), 1, &
1.0_dp, vec_c(coloff, 1), 1)
END IF
END IF
END DO
CALL dbcsr_iterator_stop(iter)
END IF
CALL timestop(timing_handle)
END SUBROUTINE dbcsr_multiply_local
! **************************************************************************************************
!> \brief multiply a dbcsr with a fm matrix
!>
!> For backwards compatibility with BLAS XGEMM, this routine supports
!> the multiplication of matrices with incompatible dimensions.
!>
!> \param[in] matrix DBCSR matrix
!> \param fm_in full matrix
!> \param fm_out full matrix
!> \param[in] ncol nbr of columns
!> \param[in] alpha alpha
!> \param[in] beta beta
!>
! **************************************************************************************************
SUBROUTINE cp_dbcsr_sm_fm_multiply(matrix, fm_in, fm_out, ncol, alpha, beta)
TYPE(dbcsr_type), INTENT(IN) :: matrix
TYPE(cp_fm_type), INTENT(IN) :: fm_in, fm_out
INTEGER, INTENT(IN) :: ncol
REAL(dp), INTENT(IN), OPTIONAL :: alpha, beta
CHARACTER(LEN=*), PARAMETER :: routineN = 'cp_dbcsr_sm_fm_multiply'
INTEGER :: k_in, k_out, timing_handle, &
timing_handle_mult, &
a_ncol, a_nrow, b_ncol, b_nrow, c_ncol, c_nrow
INTEGER, DIMENSION(:), POINTER :: col_blk_size_right_in, &
col_blk_size_right_out, row_blk_size, &
!row_cluster, col_cluster,&
row_dist, col_dist, col_blk_size
TYPE(dbcsr_type) :: in, out
REAL(dp) :: my_alpha, my_beta
TYPE(dbcsr_distribution_type) :: dist, dist_right_in, product_dist
CALL timeset(routineN, timing_handle)
my_alpha = 1.0_dp
my_beta = 0.0_dp
IF (PRESENT(alpha)) my_alpha = alpha
IF (PRESENT(beta)) my_beta = beta
! TODO
CALL cp_fm_get_info(fm_in, ncol_global=b_ncol, nrow_global=b_nrow)
CALL cp_fm_get_info(fm_out, ncol_global=c_ncol, nrow_global=c_nrow)
CALL dbcsr_get_info(matrix, nfullrows_total=a_nrow, nfullcols_total=a_ncol)
!WRITE(*,*) "cp_dbcsr_sm_fm_multiply: A ", a_nrow, "x", a_ncol
!WRITE(*,*) "cp_dbcsr_sm_fm_multiply: B ", b_nrow, "x", b_ncol
!WRITE(*,*) "cp_dbcsr_sm_fm_multiply: C ", c_nrow, "x", c_ncol
CALL cp_fm_get_info(fm_out, ncol_global=k_out)
CALL cp_fm_get_info(fm_in, ncol_global=k_in)
!write(*,*)routineN//" -----------------------------------"
!IF (k_in .NE. k_out) &
! WRITE(*,'(3(A,I5,1X),2(A,F5.2,1X))')&
! routineN//" ncol", ncol,'k_in',k_in,'k_out',k_out,&
! 'alpha',my_alpha,'beta',my_beta
IF (ncol .GT. 0 .AND. k_out .GT. 0 .AND. k_in .GT. 0) THEN
CALL dbcsr_get_info(matrix, row_blk_size=row_blk_size, col_blk_size=col_blk_size, distribution=dist)
CALL dbcsr_create_dist_r_unrot(dist_right_in, dist, k_in, col_blk_size_right_in)
CALL dbcsr_create(in, "D", dist_right_in, dbcsr_type_no_symmetry, &
col_blk_size, col_blk_size_right_in, nze=0)
CALL dbcsr_distribution_get(dist, row_dist=row_dist)
CALL dbcsr_distribution_get(dist_right_in, col_dist=col_dist)
CALL dbcsr_distribution_new(product_dist, template=dist, &
row_dist=row_dist, col_dist=col_dist)
ALLOCATE (col_blk_size_right_out(SIZE(col_blk_size_right_in)))
col_blk_size_right_out = col_blk_size_right_in
CALL match_col_sizes(col_blk_size_right_out, col_blk_size_right_in, k_out)
!if (k_in .ne. k_out) then
! write(*,*)routineN//" in cs", col_blk_size_right_in
! write(*,*)routineN//" out cs", col_blk_size_right_out
!endif
CALL dbcsr_create(out, "D", product_dist, dbcsr_type_no_symmetry, &
row_blk_size, col_blk_size_right_out, nze=0)
CALL copy_fm_to_dbcsr(fm_in, in)
IF (ncol .NE. k_out .OR. my_beta .NE. 0.0_dp) &
CALL copy_fm_to_dbcsr(fm_out, out)
CALL timeset(routineN//'_core', timing_handle_mult)
CALL dbcsr_multiply("N", "N", my_alpha, matrix, in, my_beta, out, &
last_column=ncol)
CALL timestop(timing_handle_mult)
CALL copy_dbcsr_to_fm(out, fm_out)
CALL dbcsr_release(in)
CALL dbcsr_release(out)
DEALLOCATE (col_blk_size_right_in, col_blk_size_right_out)
CALL dbcsr_distribution_release(dist_right_in)
CALL dbcsr_distribution_release(product_dist)
END IF
CALL timestop(timing_handle)
END SUBROUTINE cp_dbcsr_sm_fm_multiply
! **************************************************************************************************
!> \brief ...
!> \param sizes1 ...
!> \param sizes2 ...
!> \param full_num ...
! **************************************************************************************************
SUBROUTINE match_col_sizes(sizes1, sizes2, full_num)
INTEGER, DIMENSION(:), INTENT(INOUT) :: sizes1
INTEGER, DIMENSION(:), INTENT(IN) :: sizes2
INTEGER, INTENT(IN) :: full_num
INTEGER :: left, n1, n2, p, rm, used
n1 = SIZE(sizes1)
n2 = SIZE(sizes2)
IF (n1 .NE. n2) &
CPABORT("distributions must be equal!")
sizes1(1:n1) = sizes2(1:n1)
used = SUM(sizes1(1:n1))
! If sizes1 does not cover everything, then we increase the
! size of the last block; otherwise we reduce the blocks
! (from the end) until it is small enough.
IF (used .LT. full_num) THEN
sizes1(n1) = sizes1(n1) + full_num - used
ELSE
left = used - full_num
p = n1
DO WHILE (left .GT. 0 .AND. p .GT. 0)
rm = MIN(left, sizes1(p))
sizes1(p) = sizes1(p) - rm
left = left - rm
p = p - 1
END DO
END IF
END SUBROUTINE match_col_sizes
! **************************************************************************************************
!> \brief performs the multiplication sparse_matrix+dense_mat*dens_mat^T
!> if matrix_g is not explicitly given, matrix_v^T will be used
!> this can be important to save the necessary redistribute for a
!> different matrix_g and increase performance.
!> \param sparse_matrix ...
!> \param matrix_v ...
!> \param matrix_g ...
!> \param ncol ...
!> \param alpha ...
!> \param keep_sparsity Determines if the sparsity of sparse_matrix is retained
!> by default it is TRUE
!> \param symmetry_mode There are the following modes
!> 1: sparse_matrix += 0.5*alpha*(v*g^T+g^T*v) (symmetric update)
!> -1: sparse_matrix += 0.5*alpha*(v*g^T-g^T*v) (skewsymmetric update)
!> else: sparse_matrix += alpha*v*g^T (no symmetry, default)
!> saves some redistribution steps
! **************************************************************************************************
SUBROUTINE cp_dbcsr_plus_fm_fm_t(sparse_matrix, matrix_v, matrix_g, ncol, alpha, keep_sparsity, symmetry_mode)
TYPE(dbcsr_type), INTENT(INOUT) :: sparse_matrix
TYPE(cp_fm_type), INTENT(IN) :: matrix_v
TYPE(cp_fm_type), OPTIONAL, INTENT(IN) :: matrix_g
INTEGER, INTENT(IN) :: ncol
REAL(KIND=dp), INTENT(IN), OPTIONAL :: alpha
LOGICAL, INTENT(IN), OPTIONAL :: keep_sparsity
INTEGER, INTENT(IN), OPTIONAL :: symmetry_mode
CHARACTER(LEN=*), PARAMETER :: routineN = 'cp_dbcsr_plus_fm_fm_t_native'
INTEGER :: npcols, k, nao, timing_handle, data_type, my_symmetry_mode
INTEGER, DIMENSION(:), POINTER :: col_blk_size_left, &
col_dist_left, row_blk_size, row_dist
LOGICAL :: check_product, my_keep_sparsity
REAL(KIND=dp) :: my_alpha, norm
TYPE(dbcsr_type) :: mat_g, mat_v, sparse_matrix2, &
sparse_matrix3
TYPE(cp_fm_struct_type), POINTER :: fm_struct_tmp
TYPE(cp_fm_type) :: fm_matrix
TYPE(dbcsr_distribution_type) :: dist_left, sparse_dist
check_product = .FALSE.
CALL timeset(routineN, timing_handle)
my_keep_sparsity = .TRUE.
IF (PRESENT(keep_sparsity)) my_keep_sparsity = keep_sparsity
my_symmetry_mode = 0
IF (PRESENT(symmetry_mode)) my_symmetry_mode = symmetry_mode
NULLIFY (col_dist_left)
IF (ncol .GT. 0) THEN
IF (.NOT. dbcsr_valid_index(sparse_matrix)) &
CPABORT("sparse_matrix must pre-exist")
!
! Setup matrix_v
CALL cp_fm_get_info(matrix_v, ncol_global=k)
!WRITE(*,*)routineN//'truncated mult k, ncol',k,ncol,' PRESENT (matrix_g)',PRESENT (matrix_g)
CALL dbcsr_get_info(sparse_matrix, distribution=sparse_dist)
CALL dbcsr_distribution_get(sparse_dist, npcols=npcols, row_dist=row_dist)
CALL create_bl_distribution(col_dist_left, col_blk_size_left, k, npcols)
CALL dbcsr_distribution_new(dist_left, template=sparse_dist, &
row_dist=row_dist, col_dist=col_dist_left)
DEALLOCATE (col_dist_left)
CALL dbcsr_get_info(sparse_matrix, row_blk_size=row_blk_size, data_type=data_type)
CALL dbcsr_create(mat_v, "DBCSR matrix_v", dist_left, dbcsr_type_no_symmetry, &
row_blk_size, col_blk_size_left, nze=0, data_type=data_type)
CALL copy_fm_to_dbcsr(matrix_v, mat_v)
CALL dbcsr_verify_matrix(mat_v)
!
! Setup matrix_g
IF (PRESENT(matrix_g)) THEN
CALL dbcsr_create(mat_g, "DBCSR matrix_g", dist_left, dbcsr_type_no_symmetry, &
row_blk_size, col_blk_size_left, data_type=data_type)
CALL copy_fm_to_dbcsr(matrix_g, mat_g)
END IF
!
DEALLOCATE (col_blk_size_left)
CALL dbcsr_distribution_release(dist_left)
!
!
IF (check_product) THEN
CALL cp_fm_get_info(matrix_v, nrow_global=nao)
CALL cp_fm_struct_create(fm_struct_tmp, context=matrix_v%matrix_struct%context, nrow_global=nao, &
ncol_global=nao, para_env=matrix_v%matrix_struct%para_env)
CALL cp_fm_create(fm_matrix, fm_struct_tmp, name="fm matrix")
CALL cp_fm_struct_release(fm_struct_tmp)
CALL copy_dbcsr_to_fm(sparse_matrix, fm_matrix)
CALL dbcsr_copy(sparse_matrix3, sparse_matrix)
END IF
!
my_alpha = 1.0_dp
IF (PRESENT(alpha)) my_alpha = alpha
IF (PRESENT(matrix_g)) THEN
IF (my_symmetry_mode == 1) THEN
! Symmetric mode
CALL dbcsr_multiply("N", "T", 0.5_dp*my_alpha, mat_v, mat_g, &
1.0_dp, sparse_matrix, &
retain_sparsity=my_keep_sparsity, &
last_k=ncol)
CALL dbcsr_multiply("N", "T", 0.5_dp*my_alpha, mat_g, mat_v, &
1.0_dp, sparse_matrix, &
retain_sparsity=my_keep_sparsity, &
last_k=ncol)
ELSE IF (my_symmetry_mode == -1) THEN
! Skewsymmetric mode
CALL dbcsr_multiply("N", "T", 0.5_dp*my_alpha, mat_v, mat_g, &
1.0_dp, sparse_matrix, &
retain_sparsity=my_keep_sparsity, &
last_k=ncol)
CALL dbcsr_multiply("N", "T", -0.5_dp*my_alpha, mat_g, mat_v, &
1.0_dp, sparse_matrix, &
retain_sparsity=my_keep_sparsity, &
last_k=ncol)
ELSE
! Normal mode
CALL dbcsr_multiply("N", "T", my_alpha, mat_v, mat_g, &
1.0_dp, sparse_matrix, &
retain_sparsity=my_keep_sparsity, &
last_k=ncol)
END IF
ELSE
CALL dbcsr_multiply("N", "T", my_alpha, mat_v, mat_v, &
1.0_dp, sparse_matrix, &
retain_sparsity=my_keep_sparsity, &
last_k=ncol)
END IF
IF (check_product) THEN
IF (PRESENT(matrix_g)) THEN
IF (my_symmetry_mode == 1) THEN
CALL cp_fm_gemm("N", "T", nao, nao, ncol, 0.5_dp*my_alpha, matrix_v, matrix_g, &
1.0_dp, fm_matrix)
CALL cp_fm_gemm("N", "T", nao, nao, ncol, 0.5_dp*my_alpha, matrix_g, matrix_v, &
1.0_dp, fm_matrix)
ELSE IF (my_symmetry_mode == -1) THEN
CALL cp_fm_gemm("N", "T", nao, nao, ncol, 0.5_dp*my_alpha, matrix_v, matrix_g, &
1.0_dp, fm_matrix)
CALL cp_fm_gemm("N", "T", nao, nao, ncol, -0.5_dp*my_alpha, matrix_g, matrix_v, &
1.0_dp, fm_matrix)
ELSE
CALL cp_fm_gemm("N", "T", nao, nao, ncol, my_alpha, matrix_v, matrix_g, &
1.0_dp, fm_matrix)
END IF
ELSE
CALL cp_fm_gemm("N", "T", nao, nao, ncol, my_alpha, matrix_v, matrix_v, &
1.0_dp, fm_matrix)
END IF
CALL dbcsr_copy(sparse_matrix2, sparse_matrix)
CALL dbcsr_scale(sparse_matrix2, alpha_scalar=0.0_dp)
CALL copy_fm_to_dbcsr(fm_matrix, sparse_matrix2, keep_sparsity=my_keep_sparsity)
CALL dbcsr_add(sparse_matrix2, sparse_matrix, alpha_scalar=1.0_dp, &
beta_scalar=-1.0_dp)
CALL dbcsr_norm(sparse_matrix2, which_norm=dbcsr_norm_frobenius, &
norm_scalar=norm)
WRITE (*, *) 'nao=', nao, ' k=', k, ' ncol=', ncol, ' my_alpha=', my_alpha
WRITE (*, *) 'PRESENT (matrix_g)', PRESENT(matrix_g)
WRITE (*, *) 'matrix_type=', dbcsr_get_matrix_type(sparse_matrix)
WRITE (*, *) 'norm(sm+alpha*v*g^t - fm+alpha*v*g^t)/n=', norm/REAL(nao, dp)
IF (norm/REAL(nao, dp) .GT. 1e-12_dp) THEN
!WRITE(*,*) 'fm_matrix'
!DO j=1,SIZE(fm_matrix%local_data,2)
! DO i=1,SIZE(fm_matrix%local_data,1)
! WRITE(*,'(A,I3,A,I3,A,E26.16,A)') 'a(',i,',',j,')=',fm_matrix%local_data(i,j),';'
! ENDDO
!ENDDO
!WRITE(*,*) 'mat_v'
!CALL dbcsr_print(mat_v,matlab_format=.TRUE.)
!WRITE(*,*) 'mat_g'
!CALL dbcsr_print(mat_g,matlab_format=.TRUE.)
!WRITE(*,*) 'sparse_matrix'
!CALL dbcsr_print(sparse_matrix,matlab_format=.TRUE.)
!WRITE(*,*) 'sparse_matrix2 (-sm + sparse(fm))'
!CALL dbcsr_print(sparse_matrix2,matlab_format=.TRUE.)
!WRITE(*,*) 'sparse_matrix3 (copy of sm input)'
!CALL dbcsr_print(sparse_matrix3,matlab_format=.TRUE.)
!stop
END IF
CALL dbcsr_release(sparse_matrix2)
CALL dbcsr_release(sparse_matrix3)
CALL cp_fm_release(fm_matrix)
END IF
CALL dbcsr_release(mat_v)
IF (PRESENT(matrix_g)) CALL dbcsr_release(mat_g)
END IF
CALL timestop(timing_handle)
END SUBROUTINE cp_dbcsr_plus_fm_fm_t
! **************************************************************************************************
!> \brief Utility function to copy a specially shaped fm to dbcsr_matrix
!> The result matrix will be the matrix in dbcsr format
!> with the row blocks sizes according to the block_sizes of the template
!> and the col blocks sizes evenly blocked with the internal dbcsr conversion
!> size (32 is the current default)
!> \param matrix ...
!> \param fm_in ...
!> \param template ...
! **************************************************************************************************
SUBROUTINE cp_fm_to_dbcsr_row_template(matrix, fm_in, template)
TYPE(dbcsr_type), INTENT(INOUT) :: matrix
TYPE(cp_fm_type), INTENT(IN) :: fm_in
TYPE(dbcsr_type), INTENT(IN) :: template
INTEGER :: k_in, data_type
INTEGER, DIMENSION(:), POINTER :: col_blk_size_right_in, row_blk_size
TYPE(dbcsr_distribution_type) :: tmpl_dist, dist_right_in
CALL cp_fm_get_info(fm_in, ncol_global=k_in)
CALL dbcsr_get_info(template, distribution=tmpl_dist)
CALL dbcsr_create_dist_r_unrot(dist_right_in, tmpl_dist, k_in, col_blk_size_right_in)
CALL dbcsr_get_info(template, row_blk_size=row_blk_size, data_type=data_type)
CALL dbcsr_create(matrix, "D", dist_right_in, dbcsr_type_no_symmetry, &
row_blk_size, col_blk_size_right_in, nze=0, data_type=data_type)
CALL copy_fm_to_dbcsr(fm_in, matrix)
DEALLOCATE (col_blk_size_right_in)
CALL dbcsr_distribution_release(dist_right_in)
END SUBROUTINE cp_fm_to_dbcsr_row_template
! **************************************************************************************************
!> \brief Utility function to create an arbitrary shaped dbcsr matrix
!> with the same processor grid as the template matrix
!> both row sizes and col sizes are evenly blocked with the internal
!> dbcsr_conversion size (32 is the current default)
!> \param matrix dbcsr matrix to be created
!> \param template template dbcsr matrix giving its mp_env
!> \param m global row size of output matrix
!> \param n global col size of output matrix
!> \param sym ...
!> \param data_type ...
! **************************************************************************************************
SUBROUTINE cp_dbcsr_m_by_n_from_template(matrix, template, m, n, sym, data_type)
TYPE(dbcsr_type), INTENT(INOUT) :: matrix, template
INTEGER, INTENT(IN) :: m, n
CHARACTER, OPTIONAL, INTENT(IN) :: sym
INTEGER, OPTIONAL, INTENT(IN) :: data_type
CHARACTER :: mysym
INTEGER :: my_data_type, nprows, npcols
INTEGER, DIMENSION(:), POINTER :: col_blk_size, &
col_dist, row_blk_size, &
row_dist
TYPE(dbcsr_distribution_type) :: tmpl_dist, dist_m_n
CALL dbcsr_get_info(template, &
matrix_type=mysym, &
data_type=my_data_type, &
distribution=tmpl_dist)
IF (PRESENT(sym)) mysym = sym
IF (PRESENT(data_type)) my_data_type = data_type
NULLIFY (row_dist, col_dist)
NULLIFY (row_blk_size, col_blk_size)
!NULLIFY (row_cluster, col_cluster)
CALL dbcsr_distribution_get(tmpl_dist, nprows=nprows, npcols=npcols)
CALL create_bl_distribution(row_dist, row_blk_size, m, nprows)
CALL create_bl_distribution(col_dist, col_blk_size, n, npcols)
CALL dbcsr_distribution_new(dist_m_n, template=tmpl_dist, &
row_dist=row_dist, col_dist=col_dist, &
!row_cluster=row_cluster, col_cluster=col_cluster, &
reuse_arrays=.TRUE.)
CALL dbcsr_create(matrix, "m_n_template", dist_m_n, mysym, &
row_blk_size, col_blk_size, nze=0, data_type=my_data_type, &
reuse_arrays=.TRUE.)
CALL dbcsr_distribution_release(dist_m_n)
END SUBROUTINE cp_dbcsr_m_by_n_from_template
! **************************************************************************************************
!> \brief Utility function to create dbcsr matrix, m x n matrix (n arbitrary)
!> with the same processor grid and row distribution as the template matrix
!> col sizes are evenly blocked with the internal
!> dbcsr_conversion size (32 is the current default)
!> \param matrix dbcsr matrix to be created
!> \param template template dbcsr matrix giving its mp_env
!> \param n global col size of output matrix
!> \param sym ...
!> \param data_type ...
! **************************************************************************************************
SUBROUTINE cp_dbcsr_m_by_n_from_row_template(matrix, template, n, sym, data_type)
TYPE(dbcsr_type), INTENT(INOUT) :: matrix, template
INTEGER :: n
CHARACTER, OPTIONAL :: sym
INTEGER, OPTIONAL :: data_type
CHARACTER :: mysym
INTEGER :: my_data_type, npcols
INTEGER, DIMENSION(:), POINTER :: col_blk_size, col_dist, row_blk_size, &
row_dist
TYPE(dbcsr_distribution_type) :: dist_m_n, tmpl_dist
mysym = dbcsr_get_matrix_type(template)
IF (PRESENT(sym)) mysym = sym
my_data_type = dbcsr_get_data_type(template)
IF (PRESENT(data_type)) my_data_type = data_type
CALL dbcsr_get_info(template, distribution=tmpl_dist)
CALL dbcsr_distribution_get(tmpl_dist, &
npcols=npcols, &
row_dist=row_dist)
NULLIFY (col_dist, col_blk_size)
CALL create_bl_distribution(col_dist, col_blk_size, n, npcols)
CALL dbcsr_distribution_new(dist_m_n, template=tmpl_dist, &
row_dist=row_dist, col_dist=col_dist)
CALL dbcsr_get_info(template, row_blk_size=row_blk_size)
CALL dbcsr_create(matrix, "m_n_template", dist_m_n, mysym, &
row_blk_size, col_blk_size, nze=0, data_type=my_data_type)
DEALLOCATE (col_dist, col_blk_size)
CALL dbcsr_distribution_release(dist_m_n)
END SUBROUTINE cp_dbcsr_m_by_n_from_row_template
! **************************************************************************************************
!> \brief Distributes elements into blocks and into bins
!>
!> \param[out] block_distribution block distribution to bins
!> \param[out] block_size sizes of blocks
!> \param[in] nelements number of elements to bin
!> \param[in] nbins number of bins