forked from hxmhuang/dm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdm_mat.F90
3642 lines (3070 loc) · 111 KB
/
dm_mat.F90
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
! -----------------------------------------------------------------------
!> Distributed Matrix Computing Library
! -----------------------------------------------------------------------
module dm_mat
implicit none
contains
! -----------------------------------------------------------------------
!> Generate a logical 3D matrix with 2D Mat
! -----------------------------------------------------------------------
! For example, suppose A is a 3*2*2 matrix, we will store A as
! A=[x11 x12 0 0 ]
![x21 x22 0 0 ]
![x31 x32 0 0 ]
![ 0 0 Y11 Y12 ]
![ 0 0 Y21 Y22 ]
![ 0 0 Y31 Y32 ]
subroutine mat_create(A,m,n,k,isGlobal,ierr)
implicit none
#include <petsc/finclude/petscsys.h>
#include <petsc/finclude/petscvec.h>
#include <petsc/finclude/petscvec.h90>
#include <petsc/finclude/petscmat.h>
PetscInt, intent(in) :: m,n,k
PetscBool, intent(in) :: isGlobal
Mat, intent(out) :: A
PetscErrorCode, intent(out) :: ierr
!PetscLogEvent :: ievent
!call PetscLogEventRegister("mat_create",0, ievent, ierr)
!call PetscLogEventBegin(ievent,ierr)
! generate matrix A with size m*n
if(isGlobal) then
call MatCreate(PETSC_COMM_WORLD,A,ierr)
else
call MatCreate(PETSC_COMM_SELF,A,ierr);
endif
call MatSetSizes(A,PETSC_DECIDE,PETSC_DECIDE,m*k,n*k,ierr)
call MatSetFromOptions(A,ierr)
call MatSetUp(A,ierr)
!call PetscLogEventEnd(ievent,ierr)
end subroutine mat_create
subroutine mat_destroy(A,ierr)
implicit none
#include <petsc/finclude/petscsys.h>
#include <petsc/finclude/petscvec.h>
#include <petsc/finclude/petscvec.h90>
#include <petsc/finclude/petscmat.h>
Mat, intent(in)::A
PetscErrorCode,intent(out)::ierr
PetscLogEvent :: ievent
call PetscLogEventRegister("mat_destroy",0, ievent, ierr)
call PetscLogEventBegin(ievent,ierr)
! destroy matrix A
call MatDestroy(A,ierr)
call PetscLogEventEnd(ievent,ierr)
end subroutine mat_destroy
subroutine mat_view(A,ierr)
implicit none
#include <petsc/finclude/petscsys.h>
#include <petsc/finclude/petscvec.h>
#include <petsc/finclude/petscvec.h90>
#include <petsc/finclude/petscmat.h>
Mat,intent(in)::A
PetscErrorCode,intent(out)::ierr
PetscBool:: isGlobal
! view matrix A
call mat_assemble(A,ierr)
call mat_gettype(A,isGlobal,ierr)
if(isGlobal) then
call MatView(A,PETSC_VIEWER_STDOUT_WORLD, ierr)
else
call MatView(A,PETSC_VIEWER_STDOUT_SELF, ierr)
endif
end subroutine mat_view
subroutine vec_view(A,ierr)
implicit none
#include <petsc/finclude/petscsys.h>
#include <petsc/finclude/petscvec.h>
#include <petsc/finclude/petscvec.h90>
#include <petsc/finclude/petscmat.h>
Vec,intent(in)::A
PetscErrorCode,intent(out)::ierr
PetscBool:: isGlobal
! view matrix A
call vec_assemble(A,ierr)
call vec_gettype(A,isGlobal,ierr)
if(isGlobal) then
call VecView(A,PETSC_VIEWER_STDOUT_WORLD, ierr)
else
call VecView(A,PETSC_VIEWER_STDOUT_SELF, ierr)
endif
end subroutine vec_view
subroutine mat_assemble(A,ierr)
implicit none
#include <petsc/finclude/petscsys.h>
#include <petsc/finclude/petscvec.h>
#include <petsc/finclude/petscvec.h90>
#include <petsc/finclude/petscmat.h>
Mat,intent(in)::A
PetscErrorCode,intent(out)::ierr
!PetscBool:: assembled
!call MatAssembled(A,assembled,ierr)
!if(.not. assembled) then
call MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY,ierr)
call MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY,ierr)
!endif
end subroutine mat_assemble
subroutine vec_assemble(A,ierr)
implicit none
#include <petsc/finclude/petscsys.h>
#include <petsc/finclude/petscvec.h>
#include <petsc/finclude/petscvec.h90>
#include <petsc/finclude/petscmat.h>
Vec,intent(in)::A
PetscErrorCode,intent(out)::ierr
!PetscBool:: assembled
!call MatAssembled(A,assembled,ierr)
!if(.not. assembled) then
call VecAssemblyBegin(A,ierr)
call VecAssemblyEnd(A,ierr)
!endif
end subroutine vec_assemble
! -----------------------------------------------------------------------
! A=0
! -----------------------------------------------------------------------
subroutine mat_zeros(A,ierr)
implicit none
#include <petsc/finclude/petscsys.h>
#include <petsc/finclude/petscvec.h>
#include <petsc/finclude/petscvec.h90>
#include <petsc/finclude/petscmat.h>
Mat,intent(out)::A
PetscErrorCode,intent(out)::ierr
!PetscInt:: ista,iend
!PetscLogEvent :: ievent
! call PetscLogEventRegister("mat_zeros",0, ievent, ierr)
! call PetscLogEventBegin(ievent,ierr)
call MatZeroEntries(A,ierr)
! call PetscLogEventEnd(ievent,ierr)
end subroutine mat_zeros
! -----------------------------------------------------------------------
! A=1.
! -----------------------------------------------------------------------
subroutine mat_constants(A,m,n,k,alpha,ierr)
implicit none
#include <petsc/finclude/petscsys.h>
#include <petsc/finclude/petscvec.h>
#include <petsc/finclude/petscvec.h90>
#include <petsc/finclude/petscmat.h>
Mat,intent(out)::A
PetscErrorCode,intent(out)::ierr
PetscInt,intent(in):: m,n,k
PetscScalar,intent(in):: alpha
PetscInt:: ista,iend,ilocal
PetscInt,allocatable::idxm(:),idxn(:)
PetscScalar,allocatable::row(:)
integer :: i,j
PetscLogEvent :: ievent
call PetscLogEventRegister("mat_constants",0, ievent, ierr)
call PetscLogEventBegin(ievent,ierr)
call MatGetOwnershipRange(A,ista,iend,ierr)
ilocal=iend-ista
allocate(idxm(1),idxn(n),row(n))
row=alpha
do i=ista,iend-1
idxm(1)=i
do j=0,n-1
idxn(j+1)=(i/m)*n+j
enddo
!print *,"i=",i,"idxm=",idxm,"idxn=",idxn
call MatSetValues(A,1,idxm,n,idxn,row,INSERT_VALUES,ierr)
enddo
deallocate(idxm,idxn,row)
call PetscLogEventEnd(ievent,ierr)
end subroutine mat_constants
! -----------------------------------------------------------------------
!> The eye function is used to generate the simple and complex identity matrixs.
!! For example, if A is a 2*4 matrix, we can use mat_eye(A,ierr) to obtain
!> A= [1 0 0 0]
!> [0 1 0 0]
!> if A is a 4*2 matrix, then mat_eye(A,ierr) will generate
!> A= [1 0]
!> [0 1]
!> [0 0]
!> [0 0]
! -----------------------------------------------------------------------
subroutine mat_eye(A,m,n,k,ierr)
implicit none
#include <petsc/finclude/petscsys.h>
#include <petsc/finclude/petscvec.h>
#include <petsc/finclude/petscvec.h90>
#include <petsc/finclude/petscmat.h>
Mat,intent(out)::A
PetscErrorCode,intent(out)::ierr
PetscInt,intent(in)::m,n,k
PetscInt::nmin
PetscInt:: ista,iend,xpos,ypos
PetscScalar::row
integer :: i
PetscLogEvent :: ievent
call PetscLogEventRegister("mat_eye",0, ievent, ierr)
call PetscLogEventBegin(ievent,ierr)
call MatZeroEntries(A,ierr)
nmin=min(m,n)
row=1.0
call MatGetOwnershipRange(A,ista,iend,ierr)
xpos=-1
ypos=-1
do i=ista,iend-1
xpos=mod(i,m)
if(xpos<n) then
ypos=i/m*n+xpos
call MatSetValue(A,i,ypos,row,INSERT_VALUES,ierr)
endif
!print *,"i=",i,"ista:iend=",ista,iend,"xpos=",xpos,"ypos=",ypos
xpos=-1
ypos=-1
enddo
call PetscLogEventEnd(ievent,ierr)
end subroutine mat_eye
!---------------------------------------------------------------------
!>Create matrix A and then assign a sequence of numbers
!>Output: A = [1, 2, 3]
!> [4, 5, 6]
!---------------------------------------------------------------------
subroutine mat_seqs(A,m,n,k,ierr)
implicit none
#include <petsc/finclude/petscsys.h>
#include <petsc/finclude/petscvec.h>
#include <petsc/finclude/petscvec.h90>
#include <petsc/finclude/petscmat.h>
Mat,intent(out)::A
PetscErrorCode,intent(out)::ierr
PetscInt,intent(in)::m,n,k
PetscInt:: ista,iend
PetscInt, allocatable :: idxn(:), idxm(:)
PetscScalar, allocatable ::row(:)
integer :: ik
PetscLogEvent :: ievent
integer :: i, j
call PetscLogEventRegister("mat_seqs",0, ievent, ierr)
call PetscLogEventBegin(ievent,ierr)
call MatGetOwnershipRange(A,ista,iend,ierr)
allocate(idxn(n), row(n), idxm(1))
do i=ista,iend-1
ik = i / m
do j=1,n
idxn(j) = j + ik * n - 1
row(j) = j+mod(i,m)*n + ik*m*n - 1
end do
idxm(1) = i
call MatSetValues(A, 1, idxm, n, idxn, row, INSERT_VALUES, ierr)
enddo
deallocate(idxn, row, idxm)
call PetscLogEventEnd(ievent,ierr)
end subroutine mat_seqs
!---------------------------------------------------------------------
!> Create a random matrix A
!---------------------------------------------------------------------
subroutine mat_rand(A,m,n,k,ierr)
implicit none
#include <petsc/finclude/petscsys.h>
#include <petsc/finclude/petscvec.h>
#include <petsc/finclude/petscvec.h90>
#include <petsc/finclude/petscmat.h>
Mat,intent(out)::A
PetscErrorCode,intent(out)::ierr
PetscInt,intent(in)::m,n,k
PetscInt:: ista,iend
PetscInt, allocatable :: idxn(:), idxm(:)
PetscScalar, allocatable ::row(:)
integer :: ik
PetscLogEvent :: ievent
integer :: i, j, n_seed
integer :: clock
INTEGER, DIMENSION(:), ALLOCATABLE :: seed
integer :: rank
call MPI_Comm_rank(PETSC_COMM_WORLD, rank, ierr)
call random_seed(size = n_seed)
allocate(seed(n_seed))
call system_clock(count=clock)
seed = clock + rank + 37 * (/ (i - 1, i = 1, n_seed) /)
call random_seed(put = seed)
call PetscLogEventRegister("mat_rand",0, ievent, ierr)
call PetscLogEventBegin(ievent,ierr)
call MatGetOwnershipRange(A,ista,iend,ierr)
allocate(idxn(n*k), row(n*k), idxm(1))
do i=ista,iend-1
ik = i / m
idxn = [(i,i=0,n-1)] + ik * n
call RANDOM_NUMBER(row)
! do j=1,n
! idxn(j) = j + ik * n - 1
! row(j) = j
! !call RANDOM_NUMBER(row)
! end do
idxm(1) = i
call MatSetValues(A, 1, idxm, n, idxn, row, INSERT_VALUES, ierr)
enddo
deallocate(idxn, row, idxm, seed)
call PetscLogEventEnd(ievent,ierr)
end subroutine mat_rand
! -----------------------------------------------------------------------
! The vertical eye plus zero matrix
! A= [1 0 0]
! [0 1 0]
! [0 0 1]
! [0 0 0]
! [0 0 0]
! -----------------------------------------------------------------------
subroutine mat_veyezero(A,nrow1,nrow2,ierr)
implicit none
#include <petsc/finclude/petscsys.h>
#include <petsc/finclude/petscvec.h>
#include <petsc/finclude/petscvec.h90>
#include <petsc/finclude/petscmat.h>
Mat,intent(out)::A
PetscErrorCode,intent(out)::ierr
PetscInt, intent(in)::nrow1,nrow2
PetscInt:: ista,iend
PetscScalar::row
integer :: i,j
PetscLogEvent :: ievent
call PetscLogEventRegister("mat_veyezero",0, ievent, ierr)
call PetscLogEventBegin(ievent,ierr)
call mat_zeros(A,ierr)
call MatGetOwnershipRange(A,ista,iend,ierr)
do i=ista,iend-1
do j=0,nrow1-1
if(i==j) then
row=real(1.0,kind=8)
call MatSetValues(A,1,i,1,j,row,INSERT_VALUES,ierr)
endif
enddo
enddo
call PetscLogEventEnd(ievent,ierr)
end subroutine mat_veyezero
! -----------------------------------------------------------------------
! The vertical zero plus eye matrix
! A= [0 0 0]
! [0 0 0]
! [1 0 0]
! [0 1 0]
! [0 0 1]
! -----------------------------------------------------------------------
subroutine mat_vzeroeye(A,nrow1,nrow2,ierr)
implicit none
#include <petsc/finclude/petscsys.h>
#include <petsc/finclude/petscvec.h>
#include <petsc/finclude/petscvec.h90>
#include <petsc/finclude/petscmat.h>
Mat,intent(out)::A
PetscErrorCode,intent(out)::ierr
PetscInt, intent(in)::nrow1,nrow2
PetscInt:: ista,iend
PetscScalar::row
integer :: i,j
PetscLogEvent :: ievent
call PetscLogEventRegister("mat_vzeroeye",0, ievent, ierr)
call PetscLogEventBegin(ievent,ierr)
call mat_zeros(A,ierr)
call MatGetOwnershipRange(A,ista,iend,ierr)
do i=ista,iend-1
do j=0,nrow2-1
if(i==j+nrow1) then
row=real(1.0,kind=8)
call MatSetValues(A,1,i,1,j,row,INSERT_VALUES,ierr)
endif
enddo
enddo
call PetscLogEventEnd(ievent,ierr)
end subroutine mat_vzeroeye
! -----------------------------------------------------------------------
! B=A
! -----------------------------------------------------------------------
subroutine mat_copy(A,B,ierr)
implicit none
#include <petsc/finclude/petscsys.h>
#include <petsc/finclude/petscvec.h>
#include <petsc/finclude/petscvec.h90>
#include <petsc/finclude/petscmat.h>
Mat,intent(in)::A
Mat,intent(out)::B
PetscErrorCode,intent(out)::ierr
PetscLogEvent :: ievent
call PetscLogEventRegister("mat_copy",0, ievent, ierr)
call PetscLogEventBegin(ievent,ierr)
call mat_assemble(A,ierr)
call MatDuplicate(A,MAT_COPY_VALUES,B,ierr)
call PetscLogEventEnd(ievent,ierr)
end subroutine mat_copy
! -----------------------------------------------------------------------
! C=[A B]
! -----------------------------------------------------------------------
subroutine mat_yjoin(A,m1,n1,k1,B,m2,n2,k2,C,ierr)
implicit none
#include <petsc/finclude/petscsys.h>
#include <petsc/finclude/petscvec.h>
#include <petsc/finclude/petscvec.h90>
#include <petsc/finclude/petscmat.h>
Mat, intent(in):: A,B
PetscInt, intent(in) :: m1,n1,k1,m2,n2,k2
Mat, intent(out)::C
PetscErrorCode,intent(out)::ierr
PetscInt::nrow1,ncol1,nrow2,ncol2
PetscInt::col1,col2,m,n
PetscInt,allocatable::idxn1(:),idxn2(:),idxn3(:)
PetscScalar,allocatable::row1(:),row2(:),row3(:)
PetscInt:: ista,iend
PetscBool::isGlobal
integer::i
PetscLogEvent :: ievent
call PetscLogEventRegister("mat_yjoin",0, ievent, ierr)
call PetscLogEventBegin(ievent,ierr)
call MatGetSize(A,nrow1,ncol1,ierr)
call MatGetSize(B,nrow2,ncol2,ierr)
if(nrow1/=nrow2)then
print *, "Error in mat_xjoin: Matrix A and Matrix B &
&should have the same row size"
stop
endif
call mat_assemble(A,ierr)
call mat_assemble(B,ierr)
call MatGetOwnershipRange(A,ista,iend,ierr)
call mat_gettype(A,isGlobal,ierr)
call mat_create(C,m1,n1+n2,k1,isGlobal,ierr)
call mat_zeros(C,ierr)
do i=ista,iend-1
call MatGetRow(A,i,col1,PETSC_NULL_INTEGER,PETSC_NULL_SCALAR,ierr)
m=col1
call MatRestoreRow(A,i,col1,PETSC_NULL_INTEGER,PETSC_NULL_SCALAR,ierr)
call MatGetRow(B,i,col2,PETSC_NULL_INTEGER,PETSC_NULL_SCALAR,ierr)
n=col2
call MatRestoreRow(B,i,col2,PETSC_NULL_INTEGER,PETSC_NULL_SCALAR,ierr)
allocate(idxn1(m),row1(m))
allocate(idxn2(n),row2(n))
allocate(idxn3(m+n),row3(m+n))
call MatGetRow(A,i,col1,idxn1,row1,ierr)
idxn3(1:m)=i/m1*(n1+n2)+mod(idxn1,n1)
row3(1:m)=row1
call MatRestoreRow(A,i,col1,idxn1,row1,ierr)
call MatGetRow(B,i,col2,idxn2,row2,ierr)
idxn3((m+1):(m+n))=i/m1*(n1+n2)+n1+mod(idxn2,n2)
row3((m+1):(m+n))=row2
call MatRestoreRow(B,i,col2,idxn2,row2,ierr)
!print *,">i=",i,"idxn1=",idxn1,"idxn2=",idxn2,"idxn3=",idxn3,"row3=",row3
call MatSetValues(C,1,i,(m+n),idxn3,row3,INSERT_VALUES,ierr)
deallocate(idxn1,idxn2,idxn3,row1,row2,row3)
enddo
call PetscLogEventEnd(ievent,ierr)
end subroutine
! -----------------------------------------------------------------------
! C=[A]
! [B]
! -----------------------------------------------------------------------
subroutine mat_xjoin(A,m1,n1,k1,B,m2,n2,k2,C,ierr)
implicit none
#include <petsc/finclude/petscsys.h>
#include <petsc/finclude/petscvec.h>
#include <petsc/finclude/petscvec.h90>
#include <petsc/finclude/petscmat.h>
Mat, intent(in) :: A,B
PetscInt, intent(in):: m1,n1,k1,m2,n2,k2
Mat, intent(out) :: C
PetscErrorCode, intent(out) :: ierr
Mat :: W1,W2,W3
PetscBool :: isGlobal
PetscLogEvent :: ievent
call PetscLogEventRegister("mat_xjoin",0, ievent, ierr)
call PetscLogEventBegin(ievent,ierr)
!call mat_assemble(A,ierr)
!call mat_assemble(B,ierr)
call mat_trans(A,W1,ierr)
call mat_trans(B,W2,ierr)
!print *, "W1="
!call mat_view(W1,ierr)
!print *, "W2="
!call mat_view(W2,ierr)
call mat_gettype(A,isGlobal,ierr)
!call mat_create(W3,n1,m1+m2,k1,isGlobal,ierr)
!call mat_zeros(W3,ierr)
call mat_yjoin(W1,n1,m1,k1,W2,n2,m2,k2,W3,ierr)
!print *, "W3="
!call mat_view(W3,ierr)
!call mat_destroy(C,ierr)
call mat_trans(W3,C,ierr)
call mat_destroy(W1,ierr)
call mat_destroy(W2,ierr)
call mat_destroy(W3,ierr)
call PetscLogEventEnd(ievent,ierr)
end subroutine
! subroutine bk_mat_yjoin(A,m1,n1,k1,B,m2,n2,k2,C,ierr)
! implicit none
! #include <petsc/finclude/petscsys.h>
! #include <petsc/finclude/petscvec.h>
! #include <petsc/finclude/petscvec.h90>
! #include <petsc/finclude/petscmat.h>
! Mat, intent(in) :: A,B
! PetscInt, intent(in):: m1,n1,k1,m2,n2,k2
! Mat, intent(out) :: C
! PetscErrorCode, intent(out) :: ierr
! Mat :: W1
! Mat :: I1,I2
! PetscInt:: ista,iend
! PetscBool :: isGlobal
! PetscScalar:: alpha
! PetscInt:: nrow,xpos1,ypos1,xpos2,ypos2
! integer:: i,j
! PetscLogEvent :: ievent
! call PetscLogEventRegister("mat_yjoin",0, ievent, ierr)
! call PetscLogEventBegin(ievent,ierr)
! ! Generate two block matrixs I1 and I2, where m1*m1 means that there are m1 rows and m1 cols in the block martrix.
! ! I1=[I(m1*m1) 0(m1*m1)]
! ! [0(m2*m1) 0(m2*m1)]
! ! [0(m1*m1) I(m1*m1)]
! ! [0(m2*m1) 0(m2*m1)]
! !
! ! I2=[0(m1*m2) 0(m1*m2)]
! ! [I(m2*m2) 0(m2*m2)]
! ! [0(m1*m2) 0(m1*m2)]
! ! [0(m2*m2) I(m2*m2)]
! call mat_gettype(A,isGlobal,ierr)
! nrow=m1+m2
! call mat_create(I1,nrow,m1,k1,isGlobal,ierr)
! call mat_create(I2,nrow,m2,k1,isGlobal,ierr)
! call mat_zeros(I1,ierr)
! call mat_zeros(I2,ierr)
! call MatGetOwnershipRange(I1,ista,iend,ierr)
! alpha=real(1.0,kind=8)
! do i=ista,iend-1
! xpos1=mod(i,nrow)
! if(xpos1<m1) then
! ypos1=(i/nrow)*m1+xpos1
! call MatSetValues(I1,1,i,1,ypos1,alpha,INSERT_VALUES,ierr)
! endif
! xpos2=mod(i,nrow)
! if(xpos2>=m1) then
! ypos2=(i/nrow)*m2+mod(xpos2-m1,m2)
! call MatSetValues(I2,1,i,1,ypos2,alpha,INSERT_VALUES,ierr)
! endif
! enddo
! call mat_assemble(I1,ierr)
! call mat_assemble(I2,ierr)
! !print *, "I1="
! !call mat_view(I1,ierr)
! !print *, "I2="
! !call mat_view(I2,ierr)
! call mat_assemble(A,ierr)
! call mat_assemble(B,ierr)
! call MatMatMult(I1,A,MAT_INITIAL_MATRIX,PETSC_DEFAULT_REAL,W1,ierr)
! !print *, "W1="
! !call mat_view(W1,ierr)
! !print *, "B1="
! !call mat_view(B,ierr)
! call MatMatMult(I2,B,MAT_INITIAL_MATRIX,PETSC_DEFAULT_REAL,C,ierr)
! !print *, "C1="
! !call mat_view(C,ierr)
! call MatAXPY(C,alpha,W1,DIFFERENT_NONZERO_PATTERN,ierr)
! call mat_destroy(I1,ierr)
! call mat_destroy(I2,ierr)
! call mat_destroy(W1,ierr)
! call PetscLogEventEnd(ievent,ierr)
! end subroutine bk_mat_yjoin
! -----------------------------------------------------------------------
!> C=[A 0]
!> [0 B]
! -----------------------------------------------------------------------
subroutine mat_zjoin(A,m1,n1,k1,B,m2,n2,k2,C,ierr)
implicit none
#include <petsc/finclude/petscsys.h>
#include <petsc/finclude/petscvec.h>
#include <petsc/finclude/petscvec.h90>
#include <petsc/finclude/petscmat.h>
Mat, intent(in) :: A,B
PetscInt, intent(in) :: m1,n1,k1,m2,n2,k2
Mat, intent(out) :: C
PetscErrorCode, intent(out) :: ierr
Mat :: W1,W2,W3,W4
PetscBool :: isGlobal
PetscLogEvent :: ievent
call PetscLogEventRegister("mat_zjoin",0, ievent, ierr)
call PetscLogEventBegin(ievent,ierr)
call mat_gettype(A,isGlobal,ierr)
call mat_create(W1,m1*k2,n1*k1,1,isGlobal,ierr)
call mat_create(W2,m1*k1,n1*k2,1,isGlobal,ierr)
call mat_zeros(W1,ierr)
call mat_zeros(W2,ierr)
!print *, "W1="
!call mat_view(W1,ierr)
!print *, "W2="
!call mat_view(W2,ierr)
!print *, "A="
!call mat_view(A,ierr)
!print *, "B="
!call mat_view(B,ierr)
call mat_xjoin(A,m1*k1,n1*k1,1,W1,m1*k2,n1*k1,1,W3,ierr)
call mat_xjoin(W2,m1*k1,n1*k2,1,B,m1*k2,n1*k2,1,W4,ierr)
!print *, "W3="
!call mat_view(W3,ierr)
!print *, "W4="
!call mat_view(W4,ierr)
call mat_yjoin(W3,m1*(k1+k2),n1*k1,1,W4,m1*k1,n1*k2,1,C,ierr)
call mat_destroy(W1,ierr)
call mat_destroy(W2,ierr)
call mat_destroy(W3,ierr)
call mat_destroy(W4,ierr)
call PetscLogEventEnd(ievent,ierr)
end subroutine
! -----------------------------------------------------------------------
! C=A*B
! -----------------------------------------------------------------------
subroutine mat_mult(A,B,C,ierr)
implicit none
#include <petsc/finclude/petscsys.h>
#include <petsc/finclude/petscvec.h>
#include <petsc/finclude/petscvec.h90>
#include <petsc/finclude/petscmat.h>
Mat,intent(in):: A,B
Mat,intent(out):: C
PetscErrorCode,intent(out):: ierr
PetscLogEvent :: ievent
call PetscLogEventRegister("mat_mult",0, ievent, ierr)
call PetscLogEventBegin(ievent,ierr)
call mat_assemble(A,ierr)
call mat_assemble(B,ierr)
call MatMatMult(A,B,MAT_INITIAL_MATRIX,PETSC_DEFAULT_REAL,C,ierr)
call PetscLogEventEnd(ievent,ierr)
end subroutine mat_mult
! -----------------------------------------------------------------------
! C=A.*B
! -----------------------------------------------------------------------
subroutine mat_emult(A,m1,n1,k1,B,m2,n2,k2,C,ierr)
implicit none
#include <petsc/finclude/petscsys.h>
#include <petsc/finclude/petscvec.h>
#include <petsc/finclude/petscvec.h90>
#include <petsc/finclude/petscmat.h>
Mat,intent(in):: A,B
PetscInt,intent(in)::m1,n1,k1,m2,n2,k2
Mat,intent(out)::C
PetscErrorCode,intent(out)::ierr
PetscInt::col1,col2,m,n
PetscInt,allocatable::idxn1(:),idxn2(:),idxn3(:),idxtmp(:)
PetscScalar,allocatable::row1(:),row2(:),row3(:),rowtmp(:)
PetscInt:: ista,iend
PetscInt :: pos1,pos2,counter
PetscBool:: isGlobal
integer::i
PetscLogEvent :: ievent
call PetscLogEventRegister("mat_emult",0, ievent, ierr)
call PetscLogEventBegin(ievent,ierr)
call mat_assemble(A,ierr)
call mat_assemble(B,ierr)
call MatGetOwnershipRange(A,ista,iend,ierr)
call mat_gettype(A,isGlobal,ierr)
call mat_create(C,m1,n1,k1,isGlobal,ierr)
do i=ista,iend-1
call MatGetRow(A,i,col1,PETSC_NULL_INTEGER,PETSC_NULL_SCALAR,ierr)
m=col1
call MatRestoreRow(A,i,col1,PETSC_NULL_INTEGER,PETSC_NULL_SCALAR,ierr)
call MatGetRow(B,i,col2,PETSC_NULL_INTEGER,PETSC_NULL_SCALAR,ierr)
n=col2
call MatRestoreRow(B,i,col2,PETSC_NULL_INTEGER,PETSC_NULL_SCALAR,ierr)
if(m == 0 .or. n == 0) cycle
! if(m /= n) then
! print*, "Error: mat_emult: m does not equals to n!", "m=",m,"n=",n
! call mat_view(A, ierr)
! call mat_view(B, ierr)
! stop
! endif
allocate(idxn1(m),row1(m))
allocate(idxtmp(m),rowtmp(m))
allocate(idxn2(n),row2(n))
allocate(idxn3(min(m,n)),row3(min(m,n)))
call MatGetRow(A,i,col1,idxn1,row1,ierr)
m=col1
idxtmp=idxn1
rowtmp=row1
call MatRestoreRow(A,i,col1,idxn1,PETSC_NULL_SCALAR,ierr)
call MatGetRow(B,i,col2,idxn2,row2,ierr)
counter=0
pos1=1
pos2=1
!if(m /= n) print*, "error, m=",m, "n=",n
if(m == n) then
counter = m
row3 = rowtmp * row2
idxn3 = idxtmp
else
do while(pos1<=m .and. pos2<=n)
if(idxtmp(pos1)<idxn2(pos2)) then
pos1=pos1+1
elseif(idxtmp(pos1)==idxn2(pos2))then
counter=counter+1
idxn3(counter)=idxn2(pos2)
row3(counter)=rowtmp(pos1)*row2(pos2)
pos1=pos1+1
pos2=pos2+1
else
pos2=pos2+1
endif
enddo
endif
call MatRestoreRow(B,i,col2,idxn2,row2,ierr)
!print *,">i=",i,"idxn3=",idxn3,"row3=",row3
call MatSetValues(C,1,i,counter,idxn3(1:counter),row3(1:counter),INSERT_VALUES,ierr)
deallocate(idxn1,idxn2,idxn3,idxtmp,row1,row2,row3,rowtmp)
enddo
call PetscLogEventEnd(ievent,ierr)
end subroutine mat_emult
! -----------------------------------------------------------------------
! C=A./B
! -----------------------------------------------------------------------
subroutine mat_ediv(A,m1,n1,k1,B,m2,n2,k2,C,ierr)
implicit none
#include <petsc/finclude/petscsys.h>
#include <petsc/finclude/petscvec.h>
#include <petsc/finclude/petscvec.h90>
#include <petsc/finclude/petscmat.h>
Mat,intent(in):: A,B
Mat,intent(out)::C
PetscErrorCode,intent(out)::ierr
PetscInt::col1,col2,m,n
PetscInt:: m1,n1,k1,m2,n2,k2
PetscInt,allocatable::idxn1(:),idxn2(:),idxn3(:),idxtmp(:)
PetscScalar,allocatable::row1(:),row2(:),row3(:),rowtmp(:)
PetscInt:: ista,iend
PetscInt :: pos1,pos2,counter
PetscBool:: isGlobal
integer::i
PetscLogEvent :: ievent
call PetscLogEventRegister("mat_ediv",0, ievent, ierr)
call PetscLogEventBegin(ievent,ierr)
call mat_assemble(A,ierr)
call mat_assemble(B,ierr)
call MatGetOwnershipRange(A,ista,iend,ierr)
call mat_gettype(A,isGlobal,ierr)
call mat_create(C,m1,n1,k1,isGlobal,ierr)
do i=ista,iend-1
call MatGetRow(A,i,col1,PETSC_NULL_INTEGER,PETSC_NULL_SCALAR,ierr)
m=col1
call MatRestoreRow(A,i,col1,PETSC_NULL_INTEGER,PETSC_NULL_SCALAR,ierr)
call MatGetRow(B,i,col2,PETSC_NULL_INTEGER,PETSC_NULL_SCALAR,ierr)
n=col2
call MatRestoreRow(B,i,col2,PETSC_NULL_INTEGER,PETSC_NULL_SCALAR,ierr)
allocate(idxn1(m),row1(m))
allocate(idxtmp(m),rowtmp(m))
allocate(idxn2(n),row2(n))
allocate(idxn3(min(m,n)),row3(min(m,n)))
call MatGetRow(A,i,col1,idxn1,row1,ierr)
m=col1
idxtmp=idxn1
rowtmp=row1
call MatRestoreRow(A,i,col1,idxn1,PETSC_NULL_SCALAR,ierr)
call MatGetRow(B,i,col2,idxn2,row2,ierr)
counter=0
pos1=1
pos2=1
do while(pos1<=m .and. pos2<=n)
if(idxtmp(pos1)<idxn2(pos2)) then
pos1=pos1+1
elseif((idxtmp(pos1)==idxn2(pos2)) .and. (row2(pos2)/=0))then
counter=counter+1
idxn3(counter)=idxn2(pos2)
row3(counter)=rowtmp(pos1)/row2(pos2)
pos1=pos1+1
pos2=pos2+1
else
pos2=pos2+1
endif
enddo
call MatRestoreRow(B,i,col2,idxn2,row2,ierr)
!print *,">i=",i,"idxn3=",idxn3,"row3=",row3
call MatSetValues(C,1,i,counter,idxn3(1:counter),row3(1:counter),INSERT_VALUES,ierr)
deallocate(idxn1,idxn2,idxn3,idxtmp,row1,row2,row3,rowtmp)
enddo
call MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY,ierr)
call MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY,ierr)
call PetscLogEventEnd(ievent,ierr)
end subroutine mat_ediv
!---------------------------------------------------------
! replicate matrix A in x-direction for rn times, i.e.
! B = [A, A, ...]
!---------------------------------------------------------
subroutine mat_repx(A,m1,n1,k1,rn,B,ierr)
implicit none
#include <petsc/finclude/petscsys.h>
#include <petsc/finclude/petscvec.h>
#include <petsc/finclude/petscvec.h90>
#include <petsc/finclude/petscmat.h>
Mat,intent(in):: A
PetscInt,intent(in)::m1, n1, k1, rn
Mat,intent(out)::B
PetscErrorCode,intent(out)::ierr
PetscInt::nrow,ncol
PetscBool ::isGlobal
PetscInt :: ista, iend
PetscInt, allocatable :: idxn1(:), idxn2(:), unbiased_idx(:)
PetscScalar, allocatable :: row1(:), row2(:)
PetscInt :: tmp_col, col
PetscLogEvent :: ievent
integer :: tmp_idx
integer :: i, j, k
call PetscLogEventRegister("mat_repx", 0, ievent, ierr)
call PetscLogEventBegin(ievent, ierr)
call MatGetSize(A,nrow,ncol,ierr)
call mat_assemble(A, ierr)
call MatGetOwnershipRange(A, ista, iend, ierr)
call mat_gettype(A,isGlobal,ierr)
call mat_create(B, m1, n1*rn, k1, isGlobal, ierr)
do i=ista,iend - 1
call MatGetRow(A, i, tmp_col, PETSC_NULL_INTEGER, PETSC_NULL_SCALAR, ierr)
col = tmp_col
call MatRestoreRow(A, i, tmp_col, PETSC_NULL_INTEGER, PETSC_NULL_SCALAR, ierr)
allocate(idxn1(col), row1(col), unbiased_idx(col))
allocate(idxn2(rn*col), row2(rn*col))
call MatGetRow(A, i, tmp_col, idxn1, row1, ierr)
k = i / m1
unbiased_idx = idxn1 - k * n1
do j = 1,rn
tmp_idx = (j-1)*col + 1
idxn2(tmp_idx : tmp_idx+col-1) = k*n1*rn + (j-1)*n1 + unbiased_idx
row2(tmp_idx : tmp_idx+col-1) = row1
enddo
call MatRestoreRow(A, i, tmp_col, idxn1, row1, ierr)
call MatSetValues(B, 1, i, rn*col, idxn2, row2, INSERT_VALUES, ierr)
deallocate(idxn1, row1, idxn2, row2, unbiased_idx)
enddo
call PetscLogEventEnd(ievent, ierr)
end subroutine mat_repx
! -----------------------------------------------------------------------
! The mat_rep function is used to replicate matrix with m times in row and
! n times in column. It's name refers to the repmat function in MATLAB.
! Suppose P is an extended identity mM*M matrix and T is another
! extended N*nN identity matrix, we can compute W=P*A firstly and then compute
! B=W*T. These two stpes are faster than computing B=P*A*T directly.
! If the size of A is M*N=3*2, suppose m=3 and n=2, we have
! P= [1 0 0]
! [0 1 0]
! [0 0 1]
! [1 0 0]
! [0 1 0]
! [0 0 1]
! [1 0 0]
! [0 1 0]
! [0 0 1]
!
! T= [1 0 1 0]
! [0 1 0 1]
! -----------------------------------------------------------------------
subroutine mat_rep(A,m1,n1,k1,rm,rn,rk,B,ierr)
implicit none
#include <petsc/finclude/petscsys.h>
#include <petsc/finclude/petscvec.h>
#include <petsc/finclude/petscvec.h90>
#include <petsc/finclude/petscmat.h>
Mat,intent(in) :: A
PetscInt,intent(in) :: m1,n1,k1,rm,rn,rk
Mat,intent(out)::B
Mat :: W1,W2,W3,W4
PetscInt, allocatable :: idx_row(:)
PetscScalar, allocatable :: val_rows(:)
integer :: new_m, new_n, new_k
PetscErrorCode,intent(out)::ierr
PetscLogEvent :: ievent
IS :: ISRows, ISCols
logical :: isGlobal
integer :: COMM_TYPE
integer :: i,j,ista,iend,col
call PetscLogEventRegister("mat_rep",0, ievent, ierr)
call PetscLogEventBegin(ievent,ierr)
call mat_gettype(A, isGlobal, ierr)
if(isGlobal) then
COMM_TYPE = PETSC_COMM_WORLD
else
COMM_TYPE = PETSC_COMM_SELF
endif
call mat_repx(A, m1, n1, k1, rn, W1, ierr)
call mat_trans(W1, W2, ierr)