-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsparse.f90
executable file
·4060 lines (3386 loc) · 133 KB
/
sparse.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
!> Define some CSR and ParCSR data structure
!>* modified by QuanSheng Wu on Apr. 28. 2015
!>* unstable version
module sparse
use prec
use WMPI
use para
implicit none
!> define CSR(compressed sparse row) format matrix
type WTCSR
!> number of local rows, for different cpu, we get different rows
integer(li) :: NumRows
!> number of columns, for row-partioning method, this value is equals to
!> the global columns of matrix, in our physics problem, NumCols=
!> GlobalNumCols= GlobalNumRows
integer(li) :: NumCols
!> number of non-zeros entities
integer(li) :: NumNonZeros
!> array of length NumRows, record the row index
!> because the index for ia is from 1 to NumRows, for this matrix, we need
!> to know its row index
integer(li), pointer :: RowIndex(:)
!> integer array of length NumRows+1 containing index of first nonzero
!> element of row
integer(li), pointer :: ia(:)
!> integer array of length NumNonZeros containing the column positions
!> of the corresponding elements in a.
integer(li), pointer :: ja(:)
!> complex data for non-zeros entity
complex(dp), pointer :: a(:)
end type WTCSR
!> Define parallel version of CSR format matrix ParCSR
type WTParCSR
!> mpi communicator
integer :: comm
!> global number of rows
integer(li) :: GlobalNumRows
!> global number of columns
integer(li) :: GlobalNumCols
!> number of non-zeros
integer(li) :: NumNonZeros
!> when doing matrix-vector multiplication, the diagonal block needn't
!> the vector data from other procs, while the offdiagonal block needn't
!> the local cpu's vector data. So, we can realize non-block communication
!> when using ParCSR.
!>* For realization, we
!> store the diagonal and offdiagonal block as CSR matrix
type(WTCSR), pointer :: Diag
!> the column index of offd is not the original global column index,
!> so we need another array ColMapOffd
type(WTCSR), pointer :: Offd
!> map column index of offd to global column index
integer(li), pointer :: ColMapOffd(:)
!> array of length NumCPUs, RowStarts(i) contains the
!> global number of the first row on proc i,
!> first_row_index = row_starts[my_id]
!> row_starts[num_procs] = global_num_rows
integer(li), pointer :: RowStarts(:)
!> array of length num_procs+1, col_starts[i] contains the
!> global number of the first column of diag on proc i,
!> first_col_diag = col_starts[my_id],
!> col_starts[num_procs] = global_num_cols */
integer(li), pointer :: ColStarts(:)
!> an flag that whether the matrix has generate sendrecv
logical :: IsComm
!> an object to store the send and recieve messages
type(WTParCSRComm), pointer :: SendRecv
end type WTParCSR
!> define a type for sequential vector
type WTSeqVec
!> length of vector
integer(li) :: length
complex(dp), pointer :: data(:)
end type WTSeqVec
!> define a type for parallel vector
type WTParVec
!> mpi communicator
integer :: comm
!> length of vector
integer(li) :: GlobalLength
integer(li) :: FirstIndex
integer(li) :: LastIndex
type(WTSeqVec), pointer :: SeqVec
end type WTParVec
!> define some interfaces
!> redefine some operators
interface assignment (=)
!* copy one parvec to another parvec
module procedure WTParVecCopy
module procedure WTParVecSetConstantValue_z
module procedure WTParVecSetConstantValue_r
end interface
interface operator (*)
!* perform two parvec multiply
module procedure WTParVecInnerProd
module procedure WTParVecScale_r1
module procedure WTParVecScale_r2
module procedure WTParVecScale_z1
module procedure WTParVecScale_z2
end interface
interface operator (.DOT.)
!* perform two parvec multiply
module procedure WTParVecInnerProdWithNoReduce
end interface
interface WTParCSRMatVec
module procedure WTParCSRMatVec_c
end interface
interface WTParVecSetConstantValue
module procedure WTParVecSetConstantValue_z
module procedure WTParVecSetConstantValue_r
end interface
interface WTParVecSetSingleValue
module procedure WTParVecSetSingleValue_r
module procedure WTParVecSetSingleValue_z
end interface
!> Maximum number of non-zero elements in the Hamiltonian matrix
integer(li), public :: MaxNumNonZeros
private
public :: operator(*), assignment(=), operator(.dot.)
public :: WTCSR
public :: WTParCSR
public :: WTParVec
public :: ConvertCooToCsr
public :: WTCSRCreate
public :: WTCSRDestroy
public :: WTCSRInitialize
public :: WTSeqVecCreate
public :: WTSeqVecDestroy
public :: WTSeqVecInitialize
public :: WTSeqVecPrint
public :: WTSeqVecInnerProd
public :: WTCSRToParCSR
public :: WTParCSRCreate
public :: WTParCSRInitialize
public :: WTParCSRDestroy
public :: WTParCSRMatVec
public :: WTGenSendRecv
public :: WTSendRecvDestroy
public :: WTParVecCreate
public :: WTParVecDestroy
public :: WTParVecInitialize
public :: WTParVecPrint
public :: WTParVecCopy
public :: WTParVecNorm
public :: WTParVecAxpy
public :: WTParVecAxpBy
public :: WTParVecInnerProd
public :: WTParVecInnerProdWithNoReduce
public :: WTParVecNormalize
public :: WTParVecGetSingleValue
public :: WTParVecSetSingleValue
public :: WTParVecSetRandomValue
public :: WTParVecSetConstantValue
public :: WTParVecSetSeqValue
public :: WTParCSRMatrixCreate
public :: csr_sort_indices
public :: csr_sum_duplicates
#if defined (INTELMKL)
public :: arpack_sparse_coo_eigs
public :: arpack_sparse_coo_eigs_nonorth
#endif
contains
!> Create a WTCSR type matrix, when create, we only give the basic
!> information of the matrix, we don't allocate memory
subroutine WTCSRCreate(nrows, ncols, maxnnz, HCSR)
implicit none
integer(li), intent(in) :: nrows
integer(li), intent(in) :: ncols
integer(li), intent(in) :: maxnnz
type(WTCSR), intent(inout) :: HCSR
integer :: ierr
!if (.not. associated(HCSR)) allocate(HCSR)
HCSR%NumRows= nrows
HCSR%NumCols= ncols
HCSR%NumNonZeros= maxnnz
HCSR%RowIndex=> Null()
if (.not.associated(HCSR%RowIndex)) then
allocate(HCSR%RowIndex(nrows), stat=ierr)
HCSR%RowIndex= 0
endif
!*initialize pointer
HCSR%ia=> Null()
HCSR%ja=> Null()
HCSR%a=> Null()
return
end subroutine WTCSRCreate
!> Free memory for CSR matrix
subroutine WTCSRDestroy(HCSR)
implicit none
type(WTCSR) :: HCSR
if (associated(HCSR%RowIndex))deallocate(HCSR%RowIndex)
if (associated(HCSR%ia))deallocate(HCSR%ia)
if (associated(HCSR%ja))deallocate(HCSR%ja)
if (associated(HCSR%a))deallocate(HCSR%a)
nullify(HCSR%RowIndex, HCSR%ia, HCSR%ja, HCSR%a)
return
end subroutine WTCSRDestroy
!> initialize hamiltoinan matrix
!> allocate memory
subroutine WTCSRInitialize(HCSR)
implicit none
type(WTCSR) :: HCSR
integer(li) :: nrows
integer(li) :: maxnnz
integer :: ierr
real(dp) :: memory
nrows= HCSR%NumRows
maxnnz= HCSR%NumNonZeros
!> calculate memory required by ia, ja, H
memory= ( nrows*4.0 & ! ia
+ maxnnz*4.0 & ! ja
+ maxnnz*16.0 & ! H
)/1024.0/1024.0 ! MB
if (stdout>0) then
write(stdout,100)'Memory required by Hamiltonian :', memory, ' MB'
endif
if (.not.associated(HCSR%ia)) then
allocate(HCSR%ia(nrows+1), stat=ierr)
HCSR%ia= 0
endif
if (.not.associated(HCSR%ja) .and. maxnnz/=0) then
allocate(HCSR%ja(maxnnz), stat=ierr)
HCSR%ja= 0
endif
if (.not.associated(HCSR%a) .and. maxnnz/=0) then
allocate(HCSR%a(maxnnz), stat=ierr)
HCSR%a = cmplx(0d0, 0d0, dp)
endif
100 format(2x,a,f9.1,a)
return
end subroutine WTCSRInitialize
!> create a vector
subroutine WTSeqVecCreate(length, vec)
implicit none
integer(li), intent(in) :: length
type(WTSeqVec) :: vec
vec%length= length
return
end subroutine WTSeqVecCreate
!> destroy a vector
subroutine WTSeqVecDestroy(vec)
implicit none
type(WTSeqVec) :: vec
if (associated(vec%data)) deallocate(vec%data)
nullify(vec%data)
return
end subroutine WTSeqVecDestroy
!> initialize a vector
subroutine WTSeqVecInitialize(vec)
implicit none
type(WTSeqVec) :: vec
integer :: ierr
vec%data=> Null()
if (.not. associated(vec%data)) &
allocate(vec%data(vec%length), stat= ierr)
vec%data= (0d0, 0d0)
return
end subroutine WTSeqVecInitialize
!> create a parallel vector
subroutine WTParVecCreate(comm, GlobalLength, ParVec)
implicit none
integer, intent(in) :: comm
integer(li), intent(in) :: GlobalLength
type(WTParVec) :: ParVec
integer(li) :: first
integer(li) :: last
integer(li) :: LocalLength
integer :: NCPUs
integer :: cpu_id
integer :: ierr
#if defined (MPI)
ParVec%Comm= comm
call mpi_comm_size(comm, NCPUS, ierr)
call mpi_comm_rank(comm, cpu_id, ierr)
#endif
call WTGenerateLocalPartition(GlobalLength, NCPUs, &
cpu_id, first, last)
ParVec%GlobalLength= GlobalLength
ParVec%FirstIndex= first
ParVec%LastIndex= last
LocalLength= last- first+ 1
ParVec%SeqVec=> Null()
if (.not.associated(ParVec%SeqVec))allocate(ParVec%SeqVec)
call WTSeqVecCreate(LocalLength, ParVec%SeqVec)
return
end subroutine WTParVecCreate
!> initialize a parallel vector
subroutine WTParVecDestroy(ParVec)
implicit none
type(WTParVec) :: ParVec
call WTSeqVecDestroy(ParVec%SeqVec)
nullify(ParVec%SeqVec)
return
end subroutine WTParVecDestroy
!> initialize a parallel vector
subroutine WTParVecInitialize(ParVec)
implicit none
type(WTParVec) :: ParVec
call WTSeqVecInitialize(ParVec%SeqVec)
return
end subroutine WTParVecInitialize
!> Create ParCSR matrix
subroutine WTParCSRCreate(comm, RowStarts, ColStarts, NumNonZerosDiag, &
NumNonZerosOffd, HParCSR)
integer, intent(in) :: comm
integer(li) :: RowStarts(:)
integer(li) :: ColStarts(:)
integer(li), intent(in) :: NumNonZerosDiag
integer(li), intent(in) :: NumNonZerosOffd
type(WTParCSR) :: HParCSR
integer(li) :: i
integer(li) :: localnumrows
integer(li) :: localnumcols
integer(li) :: GlobalNumCols
integer :: NCPUS
integer :: icpu
integer :: ierr
HParCSR%Comm= comm
HParCSR%Diag=> Null()
HParCSR%Offd=> Null()
HParCSR%ColMapOffd=> Null()
HParCSR%RowStarts=> Null()
HParCSR%ColStarts=> Null()
HParCSR%SendRecv=> Null()
!> get number of cpus and current cpu id
#if defined (MPI)
call mpi_comm_size(comm, NCPUS, ierr)
call mpi_comm_rank(comm, icpu, ierr)
#endif
allocate(HParCSR%RowStarts(NCPUs+1))
allocate(HParCSR%ColStarts(NCPUs+1))
HParCSR%RowStarts= RowStarts !< maybe some problem
HParCSR%ColStarts= ColStarts
HParCSR%GlobalNumRows= RowStarts(NCPUs+1)
HParCSR%GlobalNumCols= ColStarts(NCPUs+1)
localnumrows= RowStarts(icpu+2)- RowStarts(icpu+1)
localnumcols= ColStarts(icpu+2)- ColStarts(icpu+1)
if (.not. associated(HParCSR%diag)) allocate(HParCSR%diag)
call WTCSRCreate(localnumrows, localnumcols, NumNonZerosDiag, HParCSR%diag)
!> when create parcsr matrix, we don't know the details of the matrix,
!> numrows and numcols for offd will be changed, some arrays will be
!> reallocated if the new length is lager than the old one.
if (.not. associated(HParCSR%offd)) allocate(HParCSR%offd)
call WTCSRCreate(localnumrows, localnumcols, NumNonZerosOffd, HParCSR%Offd)
!> assign data for RowIndex
do i=RowStarts(icpu+1), RowStarts(icpu+2)-1
HParCSR%Diag%RowIndex(i-RowStarts(icpu+1)+1)= i
enddo
HParCSR%Offd%RowIndex= HParCSR%Diag%RowIndex
!> SendRecv is a pointer, when create, we allocate memory for it
if (.not. associated(HParCSR%SendRecv)) allocate(HParCSR%SendRecv)
return
end subroutine WTParCSRCreate
!> initialize ParCSR matrix
subroutine WTParCSRInitialize(HParCSR)
implicit none
type(WTParCSR) :: HParCSR
call WTCSRInitialize(HParCSR%diag)
call WTCSRInitialize(HParCSR%offd)
!> this one will be reallocate if the real columns is greater than the
!> current one
if (.not.associated(HParCSR%ColMapOffd).and.HParCSR%Offd%NumCols/=0) then
allocate(HParCSR%ColMapOffd(HParCSR%Offd%NumCols))
endif
!* commpkg
HParCSR%IsComm= .false.
return
end subroutine WTParCSRInitialize
!>> Free memory for ParCSR matrix
subroutine WTParCSRDestroy(HParCSR)
type(WTParCSR) :: HParCSR
call WTCSRDestroy(HParCSR%diag)
call WTCSRDestroy(HParCSR%offd)
if (associated(HParCSR%ColMapOffd))deallocate(HParCSR%ColMapOffd)
if (associated(HParCSR%RowStarts))deallocate(HParCSR%RowStarts)
if (associated(HParCSR%ColStarts))deallocate(HParCSR%ColStarts)
call WTSendRecvDestroy(HParCSR%sendrecv)
Nullify(HParCSR%diag, HParCSR%offd)
Nullify(HParCSR%ColStarts, HParCSR%sendrecv)
Nullify(HParCSR%ColMapOffd, HParCSR%RowStarts, HParCSR%ColStarts)
return
end subroutine WTParCSRDestroy
!> create a hamiltonian object
subroutine WTParCSRMatrixCreate(Mdim, HParCSR)
use para
implicit none
integer, intent(in) :: Mdim
type (WTParCSR), intent(out) :: HParCSR
integer(li), pointer :: RowStarts(:)
integer(li), pointer :: ColStarts(:)
integer :: ierr
integer :: NCPUS
NCPUS= 1
#if defined (MPI)
call mpi_comm_size(mpi_cmw, NCPUS, ierr)
#endif
RowStarts=> Null()
ColStarts=> Null()
allocate(RowStarts(NCPUs+1))
allocate(ColStarts(NCPUs+1))
call WTGeneratePartition(Mdim, NCPUS, RowStarts)
ColStarts= RowStarts
if (stdout>0) then
write(stdout, *)' '
write(stdout, '(2x,a)')'>> RowStarts'
write(stdout, '(5i12)')RowStarts
write(stdout, *)' '
endif
#if defined (MPI)
call WTParCSRCreate(mpi_cmw, RowStarts, ColStarts, 0, 0, HParCSR)
#endif
deallocate(RowStarts, ColStarts)
return
end subroutine WTParCSRMatrixCreate
!>> Converts CSR matrix to ParCSR matrix
subroutine WTCSRToParCSR(HCSR, HParCSR)
type(WTCSR) :: HCSR
type(WTParCSR) :: HParCSR
!> here numrows is the local rows
integer(li) :: NumRows
!> here numcols is the global columns and rows
integer(li) :: NumCols
!> number of columns for diagonal block
integer(li) :: NumColsDiag
!> number of columns for off-diagonal block, this value is unknow, and
!> should be set by checking how many columns that have nonzero entities
integer(li) :: NumColsOffd
!> mark the diagonal block
integer(li) :: FirstColDiag
integer(li) :: LastColDiag
integer(li) :: NumNonZeros
integer(li), pointer :: a_i(:)
integer(li), pointer :: a_j(:)
complex(dp), pointer :: a(:)
integer(li), pointer :: ColMapOffd(:)
!> mark 1 if the j'th column has nonzeros value, length=NumCols
integer, allocatable :: marker(:)
!> For each convert, we generate the partition once.
integer(li), allocatable :: RowStarts(:)
integer(li), allocatable :: ColStarts(:)
!> some loop index
integer :: i, j, jo, jd
integer :: counter
integer :: NCPUS
integer :: icpu
integer :: comm
integer :: ierr
!a_i=> Null()
!a_j=> Null()
!a => Null()
#if defined (MPI)
comm= HParCSR%Comm
!> get number of cpus and current cpu id
call mpi_comm_size(comm, NCPUS, ierr)
call mpi_comm_rank(comm, icpu, ierr)
#endif
NumRows= HCSR%NumRows
NumCols= HCSR%NumCols
NumNonZeros= HCSR%NumNonZeros
allocate(marker(NumCols))
allocate(RowStarts(NCPUs+1))
allocate(ColStarts(NCPUs+1))
marker= 0
RowStarts= HParCSR%RowStarts
ColStarts= HParCSR%ColStarts
NumColsDiag= ColStarts(icpu+2)- ColStarts(icpu+1)+1
NumColsOffd= 0
FirstColDiag= ColStarts(icpu+1)
LastColDiag = ColStarts(icpu+2)- 1
a_i=> HCSR%ia
a_j=> HCSR%ja
a => HCSR%a
if (NumCols.gt.NumColsDiag) then !< there are off-diagonal block
!* here we don't know the nnz for diag and offd, so we just
!* initialize ia and RowIndex
!* PS: when create HParCSR, we should set maxnnz= 0, in order that
!* we don't allocate memory for ja, a
call WTCSRInitialize(HParCSR%diag)
call WTCSRInitialize(HParCSR%offd)
jo=1
jd=1
do i=1, NumRows
HParCSR%offd%ia(i)= jo
HParCSR%diag%ia(i)= jd
!* sweep non-zero entries in each row
do j=a_i(i), a_i(i+1)-1
if (a_j(j)<FirstColDiag .or. a_j(j)>LastColDiag) then !< offd
if (marker(a_j(j))==0) then
marker(a_j(j))= 1
NumColsOffd= NumColsOffd+ 1
endif
jo= jo+ 1 !< number of non-zeros in the non-diagonal block
else
jd= jd+ 1 !< number of non-zeros in the diagonal block
endif
enddo
enddo
HParCSR%offd%ia(NumRows+1)= jo
HParCSR%diag%ia(NumRows+1)= jd
if (NumColsOffd>0) then
allocate(HParCSR%ColMapOffd(NumColsOffd))
HParCSR%ColMapOffd= 0
endif
!* count how many non-zeros columns and
!* record its global column index
counter= 1
do i=1, NumCols
if (marker(i)==1) then
HParCSR%ColMapOffd(counter)= i
marker(i)= counter !< new column order
counter= counter+ 1
endif
enddo
!* till now, we know the nnz of diag and offd, so we can allocate
!* memory for ja, a of diag and offd
HParCSR%diag%NumNonZeros= jd
call WTCSRInitialize(HParCSR%diag)
HParCSR%offd%NumNonZeros= jo
call WTCSRInitialize(HParCSR%offd)
!* set number of columns for off-diagonal block of ParCSR
HParCSR%offd%NumCols= NumColsOffd
!* get data for diag and offd
jo=1
jd=1
do i=1, NumRows
do j=a_i(i), a_i(i+1)-1
if (a_j(j)<FirstColDiag .or. a_j(j)>LastColDiag) then !< offd
HParCSR%offd%ja(jo)= marker(a_j(j))
HParCSR%offd%a(jo)= a(j)
jo= jo+ 1 !< number of non-zeros in the non-diagonal block
else
HParCSR%diag%ja(jd)= a_j(j)- FirstColDiag+ 1
HParCSR%diag%a(jd)= a(j)
jd= jd+ 1 !< number of non-zeros in the diagonal block
endif
enddo
enddo
else !< there is no off-diagonal block
HParCSR%diag%NumNonZeros= NumNonZeros
HParCSR%offd%NumNonZeros= 0
HParCSR%offd%NumCols= 0
call WTCSRInitialize(HParCSR%diag)
call WTCSRInitialize(HParCSR%offd)
do i=1, NumNonZeros
HParCSR%diag%ja(i)= a_j(i)
HParCSR%diag%a(i)= a(i)
enddo
do i=1, NumRows+1
HParCSR%diag%ia(i)= a_i(i)
HParCSR%offd%ia(i)= 0
enddo
endif
!> set rowindex for diag and offd
!diag%RowIndex= HCSR%RowIndex
!$OMP PARALLEL DO
do i=1, NumRows
HParCSR%diag%RowIndex(i)= HCSR%RowIndex(i)
enddo
!$OMP END PARALLEL DO
!offd%RowIndex= HCSR%RowIndex
!$OMP PARALLEL DO
do i=1, NumRows
HParCSR%offd%RowIndex(i)= HCSR%RowIndex(i)
enddo
!$OMP END PARALLEL DO
deallocate(marker)
return
end subroutine WTCSRToParCSR
!>> converts COO to CSR in place.
subroutine ConvertCooToCsr( n, nnz, a, ia, ja, iwk)
!*****************************************************************************80
! from http://people.sc.fsu.edu/~jburkardt/f_src/sparsekit/sparsekit.f90
!
!
!! COOCSR_INPLACE converts COO to CSR in place.
!
! Discussion:
!
! This routine converts a matrix stored in coordinate format into
! the CSR format. The conversion is done in place in that the arrays
! a,ja,ia of the result are overwritten onto the original arrays.
!
! The entries of the output matrix are not sorted (the column
! indices in each are not in increasing order) use COOCSR
! if you want them sorted.
!
! Modified:
!
! 07 January 2004
!
! Author:
!
! Youcef Saad
!
! Parameters:
!
! Input, integer N, the row dimension of the matrix.
!
! Input, integer NNZ, the number of nonzero elements in A.
!
! Input, integer JOB. When JOB = 1, the real values in A are
! filled. Otherwise A is not touched and the structure of the
! array only (i.e. JA, IA) is obtained.
!
! Input/output, real A(NNZ). On input, the matrix numeric values,
! stored in the COO format. On output, the numeric values, stored
! in CSR format.
!
! ja = integer array of length nnz containing the column positions
! of the corresponding elements in a.
!
! ia = integer array of length nnz containing the row positions
! of the corresponding elements in a.
!
! iwk = integer work array of length n.
!
! on return:
!
! Output, real A(*), integer JA(*), IA(NROW+1), the matrix in CSR
! Compressed Sparse Row format.
!
implicit none
! >> inout variables
integer(li), intent(in) :: n
integer(li), intent(in) :: nnz
integer(li), intent(inout) :: ia(nnz)
integer(li), intent(inout) :: ja(nnz)
integer(li), intent(inout) :: iwk(n+1)
complex(dp), intent(inout) :: a(nnz)
! >> local variables
integer(li) :: i
integer(li) :: inext
integer(li) :: init
integer(li) :: ipos
integer(li) :: j
integer(li) :: jnext
integer :: job
integer(li) :: k
complex(dp) :: t
complex(dp) :: tnext
logical :: values
if (nnz.eq.0)return
job=1
values = (job == 1)
!
! Find offdter array for resulting matrix.
!
iwk(1:n+1) = 0
do k = 1, nnz
i = ia(k)
iwk(i+1) = iwk(i+1) + 1
end do
iwk(1) = 1
do i = 2, n
iwk(i) = iwk(i-1) + iwk(i)
end do
!
! Loop for a cycle in chasing process.
!
init = 1
k = 0
5 continue
if ( values ) then
t = a(init)
end if
i = ia(init)
j = ja(init)
ia(init) = -1
6 continue
k = k + 1
!
! Current row number is I. Determine where to go.
!
ipos = iwk(i)
!
! Save the chased element.
!
if ( values ) then
tnext = a(ipos)
end if
inext = ia(ipos)
jnext = ja(ipos)
!
! Then occupy its location.
!
if ( values ) then
a(ipos) = t
!~ write(*,*) t
end if
ja(ipos) = j
!
! Update pointer information for next element to come in row I.
!
iwk(i) = ipos + 1
!
! Determine the next element to be chased.
!
if ( ia(ipos) < 0 ) then
!if ( ia(ipos) <= 0 ) then !changed by QS.Wu
go to 65
end if
t = tnext
i = inext
j = jnext
ia(ipos) = -1
if ( k < nnz ) then
go to 6
end if
go to 70
65 continue
init = init + 1
if ( nnz < init ) then
go to 70
end if
!if ( ia(init) <= 0 ) then !changed by QS.Wu
if ( ia(init) < 0 ) then
go to 65
end if
!
! Restart chasing.
!
go to 5
70 continue
ia(1) = 1
ia(2:n+1) = iwk(1:n)
return
end subroutine ConvertCooToCsr
!> generate commpkg for parcsr matrix A
!> added on May-12-2015 by QS.Wu
subroutine WTGenSendRecv(HParCSR)
implicit none
!> hamiltonian in parcsr matrix format
type(WTParCSR), intent(inout) :: HParCSR
!* local variables
integer(li) :: FirstColDiag
integer :: NumColsOffd
integer :: NumColsDiag
integer, pointer :: SendCPUs(:)
integer, pointer :: SendMapStarts(:)
integer(li), pointer :: SendMapElements(:)
integer, pointer :: RecvCPUs(:)
integer, pointer :: RecvVecStarts(:)
integer(li), pointer :: ColMapOffd(:)
integer(li), pointer :: ColStarts(:)
integer, pointer :: CPUMark(:)
integer, pointer :: CPUAdd(:)
integer, pointer :: tmp(:)
integer, pointer :: RecvBuf(:)
integer, pointer :: Displs(:)
integer, pointer :: info(:)
integer, pointer :: mpirequest(:)
integer, pointer :: mpistatus(:)
integer :: i, j
integer :: ierr
integer :: icpu
integer :: CPUNo
integer :: NCPUs
integer :: cpu_id
integer :: comm
integer :: VecStart
integer :: VecLen
integer :: NumRecvs
integer :: NumSends
integer :: NumElements
integer :: NumRequest
integer :: localinfo
integer(li) :: OffdCol
!* communicator
type(WTParCSRComm), pointer :: SendRecv
!* initialize null pointers
SendCPUs=> Null()
SendMapStarts=> Null()
SendMapElements=> Null()
RecvCPUs=> Null()
RecvVecStarts=> Null()
ColMapOffd=> Null()
ColStarts=> Null()
CPUMark=> Null()
CPUAdd=> Null()
tmp=> Null()
RecvBuf=> Null()
Displs=> Null()
info=> Null()
mpirequest=> Null()
mpistatus=> Null()
SendRecv=> Null()
#if defined (MPI)
comm= HParCSR%Comm
call mpi_comm_size(comm, NCPUS, ierr)
call mpi_comm_rank(comm, cpu_id, ierr)
#endif
SendRecv=> HParCSR%SendRecv
SendRecv%Comm= comm
SendRecv%NumSends= 0
SendRecv%NumRecvs= 0
SendRecv%SendCPUs=> Null()
SendRecv%SendMapStarts=> Null()
SendRecv%SendMapElements=> Null()
SendRecv%RecvCPUs=> Null()