-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolver.f90
1315 lines (1207 loc) · 55 KB
/
Solver.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
!MIT License
!
!Copyright (c) [2017] [HydroGeophysics Group, Aarhus University, Denmark]
!
!Permission is hereby granted, free of charge, to any person obtaining a copy
!of this software and associated documentation files (the "Software"), to deal
!in the Software without restriction, including without limitation the rights
!to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
!copies of the Software, and to permit persons to whom the Software is
!furnished to do so, subject to the following conditions:
!
!The above copyright notice and this permission notice shall be included in all
!copies or substantial portions of the Software.
!
!THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
!IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
!FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
!AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
!LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
!OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
!SOFTWARE.
!Tue jan, 2015
!This module contains iterative solvers for linear systems
module mSolver
use mMisc
use mSMat
implicit none
type TSolverStatus
!This type is a subtype in TSolverSettings, meant for output from the solve
logical, private :: Success=.false. !True if system converged
integer :: Status !Status is the variable that gets reported to the surrounding code, on a succesfull solve, status=nint(iter) but otherwise it can contain error messages or -1 if no convergence was reached
real*8, private :: AbsError !Average absolute error
real*8, private :: RelError !Average relative error
real*8, private :: FactorizationTime !Time spent factorizing
real*8, private :: SolverTime !Time spent solving
!MultiRHS variables
real*8, private :: Iter !Average number of iterations used
integer, private :: Iter_min !Minimum number of iterations
integer, private :: Iter_max !Maximum number of iterations
real*8, private :: AbsError_min !Minimum absolute error
real*8, private :: AbsError_max !Maximum absolute error
real*8, private :: RelError_min !Minimum relative error
real*8, private :: RelError_max !Maximum relative error
end type TSolverStatus
type TSolverSettings
!This type is meant to contain all settings needed for the solver to run, in addition it will also contain the output from the solve.
logical, private :: IsPrepared=.false. !Before solver runs it checks this to see whether solversettings have been prepared.
logical, private :: IsComplex !True if system is complex
integer, private :: UseNormalization !If True, the matrix is normalized as: D^-1*A*D^-1 (D*x) = D^-1*b, where D=sqrt(diag(A))
integer, private :: UseRCM !If True, the matrix is reordered using Reverse Cuthill-Mckee reordering
integer, private :: NoRHS !Number of right hands sides
integer, private :: PrecondMaxFill !Max number of elements each row in LUT factorization can contain (excluding the diagonal)
integer, private :: NoElements !Actual number of elements used in LUT factorization (not sure this is set)
integer, private :: MaxIter !Max number of Iterations
integer, private :: SolverType !Which solver to use
integer, private :: Inform !Set how much information solver should print, 0 = silent, 1 = basic statistics, 2 = solversettings+basic statistics, 3=full information
integer, private :: Blocksize !The minimum blocksize
integer, private :: NBlocks !The number of blocks used in the parallel preconditioner computation
integer, private :: FillingFactor !The filling factor used for computing PrecondMaxFill
real*8, private :: PrecondTolerance !DropTolerance of ILUT filling
real*8, private :: StepLimit !When to stop if convergence is not reached
real*8, private :: AbsTol !Convergence criteria
real*8, private :: RelTol !Convergence criteria
real*8, private :: DiagDom !Diagonal dominance, an empirical parameter that gives a rough measure for how difficult this system will be to solve, and hence how many elements we need in LUT factorization
type(TSolverStatus) :: Output
!RCM ordering indices
integer,dimension(:),allocatable,private :: RCMIndices !Indices returned from RCM reordering, in case it should be reversed at the end
!Normalization factor
real*8,dimension(:),allocatable,private :: Normalization !Normalization is used if the logical Variable UseNormalization is true
real*8,dimension(:),allocatable,private :: ReNormalization !Renormalization is used if the logical variable UseNormalization is true, this brings back the original Matrix afterwards.
!Block splitting
integer,dimension(:,:),allocatable, private :: Blocks !The actual blocks the matrix will be split up in during a parallel run
!LU0/LUT preconditioner
integer,dimension(:),allocatable, private :: PrecondCols !Used for LUT precondition to store the ColIdx in CSR format
integer,dimension(:),allocatable, private :: PrecondRows !Used for LUT precondition to store the RowIdx in CSR format
real*8,dimension(:),allocatable, private :: PrecondVals !Used in real LU0/LUT preconditioning to store the values.
!LU0 - complex values
complex*16,dimension(:),allocatable, private :: PrecondcVals !Used in complex LU0 preconditioning to store the values
!BLUT preconditioner
integer,dimension(:,:),allocatable, private :: BPrecondIdxs !Used for BLUT precondition to store the ColIdx in block CSR format
integer,dimension(:,:),allocatable, private :: BPrecondDiag !Used for BLUT precondition to store the RowIdx in block CSR format
real*8,dimension(:,:),allocatable, private :: BPrecondVals !Used in BLUT preconditioning to store the values of each block.
!settings for PARDISO solver
end type TSolverSettings
interface SolverAxB
module procedure SingleRHS_wrapper
end interface
contains
!****************************************************************************************
subroutine SingleRHS_wrapper(iA,iRHS,ioX,SolverSettings)
!Tue, Dec 2014
!This routine is a wrapper routine for all the our solvers which handle systems with a single righthandside.
!IO
!iA -> input matrix, in CSR format, and of the type TSparseMat
!iRHS -> a system of right hand sides, RHS(:,1) is one right hand side vector
!ioX -> on input this matrix contains the solution guess for each vector, on output it contains the found solutions
!SolverSettings -> contains all the relevant settings the solvers need, it is also where stats about the solve is saved.
! This needs to be set before calling this routine.
!
use omp_lib
use msMat
implicit none
type (TSparseMat),intent(inout) :: iA
type(TSolverSettings),intent(inout):: SolverSettings
real*8,intent(inout),dimension(:) :: iRHS
real*8,intent(inout),dimension(:) :: ioX
character*200 :: S
integer :: i,j
integer :: NoNumaNodes,Threads,ostatus,error
real*8 :: RunTimeBegin,RunTimeEnd
real*8,dimension(:),allocatable :: InvDiag,tmp
integer,parameter :: NoRowsDenseLimit=35000 !with 35000 elements the dense solver takes about 150 seconds to solve the problem
integer :: OldThreads
logical :: dummy
logical(4) :: Success
allocate(tmp(iA%NoRows))
dummy = startOpenMP(2,1)
RuntimeBegin = omp_get_wtime()
Call checkSolverSettings(SolverSettings)
if(SolverSettings%UseRCM) then
tmp(:)=iRHS(SolverSettings%RCMIndices)
iRHS=tmp
tmp(:)=ioX(SolverSettings%RCMIndices)
ioX=tmp
end if
if(SolverSettings%UseNormalization) then
do i=1,iA%NoRows
iRHS(i)=iRHS(i)*SolverSettings%Normalization(i)
ioX(i)=ioX(i)*SolverSettings%ReNormalization(i)
end do
end if
SELECT CASE (SolverSettings%SolverType)
CASE(1,2)
call SingleRHS(iA,iRHS,ioX,SolverSettings)
CASE(3)
!Pardiso (Not implemented here)
END SELECT
!If we used Normalization, we now need to renormalize our solution vector x and our input iRHS.
if(SolverSettings%UseNormalization) then
do i=1,iA%NoRows
ioX(i)=ioX(i)*SolverSettings%Normalization(i)
iRHS(i)=iRHS(i)*SolverSettings%ReNormalization(i)
end do
end if
if(SolverSettings%UseRCM) then !This is not right, it needs to be the inverse here, but should be easily fixed
do i=1,iA%NoRows
tmp(SolverSettings%RCMIndices(i)) = iRHS(i)
end do
iRHS=tmp
do i=1,iA%NoRows
tmp(SolverSettings%RCMIndices(i)) = ioX(i)
end do
ioX=tmp
end if
RuntimeEnd=omp_get_wtime()
SolverSettings%Output%SolverTime=RunTimeEnd-RunTimeBegin
if (.not.SolverSettings%Output%success) SolverSettings%Output%Status = -SolverSettings%Output%Status
call ReturnOldThreadNum()
return
end subroutine SingleRHS_wrapper
!****************************************************************************************
subroutine SingleRHS(iA,iRHS,ioX,SolverSettings)
!Tue, November 2014
!Block Preconditioned Conjugate Gradient, with uma.
!This routine solves linear systems using the Preconditioned Conjugate gradient method, as a preconditioner the method uses either SOR or BLUT depending on SolverSettings
!More information on the method can be found in the book "Iterative methods for sparse linear systems".
!IO
!iA -> input matrix, in CSR format, and of the type TSparseMat
!iRHS -> a system of right hand sides, RHS(:,1) is one right hand side vector
!ioX -> on input this matrix contains the solution guess for each vector, on output it contains the found solutions
!SolverSettings -> contains all the relevant settings the solvers need, it is also where stats about the solve is saved.
! This needs to be set before calling this routine.
!
!
use omp_lib
implicit none
type (TSparseMat),intent(inout) :: iA
type(TSolverSettings),intent(inout) :: SolverSettings
real*8,intent(in),dimension(:) :: iRHS
real*8,intent(inout),dimension(:) :: ioX
!Parallel vars
Integer :: Threads
!Local vars
integer :: i
integer,save :: j
integer :: MaxIter,NoRows,iNuma
real*8 :: rzDotProd,eps
real*8 :: alfa, beta
real*8 :: error,norm
real*8,allocatable :: r(:),z(:),p(:),Ap(:),OldX(:),StartX(:)
logical :: KeepLooping
!SOR vars
integer,dimension(:),allocatable :: DiagIdx
integer,dimension(:,:),allocatable :: BRowIdx
real*8,dimension(:),allocatable :: InvDiag
Eps=1e-6
iNuma=1
NoRows=size(iRHS)
allocate(r(NoRows))
allocate(z(NoRows))
allocate(p(NoRows))
allocate(Ap(NoRows))
allocate(OldX(NoRows))
allocate(StartX(NoRows))
OldX=ioX
StartX=ioX
!First the block parallisation
Threads=OMP_GET_MAX_THREADS ()
call SmatAmultV(iA,ioX(:),r(:))
call VectorAddition(1,iNuma,NoRows,r,iRHS) !r(:)=iRHS(:)-r(:)
SELECT CASE (SolverSettings%SolverType)
CASE(1)
allocate(DiagIdx(iA%NoRows))
call SMatGetDiagIdx(iA,DiagIdx)
allocate(InvDiag(iA%NoRows))
call SMatGetInvDiag(iA,InvDiag)
allocate(BRowIdx(iA%NoRows,2))
call SMatGetBlockRowIdx(iA,SolverSettings%Blocks,BRowIdx)
call Apply_BPrecondition_BSGS(iA%ColIdxs,iA%RowIdxs,iA%Vals,BRowIdx,DiagIdx,InvDiag,r,z,SolverSettings%Blocks)
CASE(2)
call Apply_BPrecondition_BLUT(SolverSettings%Blocks, r, z, SolverSettings%BPrecondVals, SolverSettings%BPrecondIdxs, SolverSettings%BPrecondDiag)
END SELECT
call VectorAddition(0,iNuma,NoRows,p,z) !p(:)=z(:)
do j = 1 , SolverSettings%MaxIter
call SMatAMultV(iA,p,Ap)
rzDotProd=ddot(NoRows,r,1,z,1)
alfa=rzDotProd/ddot(NoRows,Ap(:),1,p,1)
call VectorAddition(4,iNuma,NoRows,ioX,p,alfa,r,Ap) !ioX(:) = ioX(:) + alfa*p(:) , r(:) = r(:) - alfa*Ap(:)
SELECT CASE (SolverSettings%SolverType)
CASE(1)
call Apply_BPrecondition_BSGS(iA%ColIdxs,iA%RowIdxs,iA%Vals,BRowIdx,DiagIdx,InvDiag,r,z,SolverSettings%Blocks)
CASE(2)
call Apply_BPrecondition_BLUT(SolverSettings%Blocks, r, z, SolverSettings%BPrecondVals, SolverSettings%BPrecondIdxs, SolverSettings%BPrecondDiag)
END SELECT
beta = ddot(NoRows,r,1,z,1)/rzDotProd
call VectorAddition(3,iNuma,NoRows,p,z,beta) !p(:) = z(:) + beta * p(:)
!Did we converge yet?
error=0d0
norm =0d0
Error=sqrt(ddot(NoRows,r,1,r,1))
Norm=sqrt(ddot(NoRows,ioX,1,ioX,1))
if (((error/norm).le.SolverSettings%RelTol).or.(error).le.SolverSettings%AbsTol) then
SolverSettings%Output%Success=.TRUE.
SolverSettings%Output%Status = min(j,SolverSettings%MaxIter)
exit
end if
if (SolverSettings%StepLimit.gt.0) then
Keeplooping=.False.
do i=1,NoRows
if (abs(ioX(i)-OldX(i))/Norm.gt.SolverSettings%StepLimit) then
Keeplooping=.True.
exit
end if
end do
if (.NOT.keeplooping) then
exit
else
OldX(:) = ioX(:)
end if
end if
end do
SolverSettings%Output%Status=min(j,SolverSettings%MaxIter)
SolverSettings%Output%AbsError=error
SolverSettings%Output%RelError=error/norm
if (allocated(DiagIdx)) then
deallocate(DiagIdx)
end if
if (allocated(InvDiag)) then
deallocate(InvDiag)
end if
if (allocated(BRowIdx)) then
deallocate(BRowIdx)
end if
if(.NOT.SolverSettings%Output%Success) ioX=StartX
if(allocated(r)) deallocate(r)
if(allocated(z)) deallocate(z)
if(allocated(p)) deallocate(p)
if(allocated(Ap)) deallocate(Ap)
if(allocated(OldX)) deallocate(OldX)
if(allocated(StartX)) deallocate(StartX)
end subroutine SingleRHS
subroutine VectorAddition(imode,iNuma,iNoRows,ioX,iY,alfa,ioZ,iV)
!Tue, Dec 2014
!This routine does different kinds of vector additions in parallel depending on the mode selected.
!mode = 0 : ioX = iY
!mode = 1 : ioX = iY - ioX
!mode = 2 : ioX = ioX + alfa*iY
!mode = 3 : ioX = alfa*ioX + iY
!mode = 4 : ioX(:) = ioX(:) + alfa*iY(:)
! ioZ(:) = ioZ(:) - alfa*iV(:)
!IO
!imode -> selects the type of vector addition used
!iNuma, -> Number of Numa Nodes
!iNoRows -> Number of Rows
!ioX,iY,ioZ,iV -> vectors
!alfa -> scalar
implicit none
integer,intent(in) :: imode,iNuma,iNoRows
real*8,intent(inout),dimension(:) :: ioX
real*8,intent(in),dimension(:) :: iY
real*8,intent(in),optional :: alfa
real*8,intent(inout),dimension(:),optional :: ioZ
real*8,intent(in),dimension(:),optional :: iV
integer i
if (iNuma.gt.1) then
if (imode.eq.0) then
!$OMP PARALLEL DEFAULT(NONE) SHARED(iNoRows,ioX,iY) PRIVATE(I)
!$OMP DO
do i=1,iNoRows
ioX(i) = iY(i)
end do
!$OMP END DO
!$OMP END PARALLEL
elseif (imode.eq.1) then
!$OMP PARALLEL DEFAULT(NONE) SHARED(iNoRows,ioX,iY) PRIVATE(I)
!$OMP DO
do i=1,iNoRows
ioX(i) = iY(i) - ioX(i)
end do
!$OMP END DO
!$OMP END PARALLEL
elseif(imode.eq.2) then
!$OMP PARALLEL DEFAULT(NONE) SHARED(iNoRows,ioX,iY,Alfa) PRIVATE(I)
!$OMP DO
do i=1,iNoRows
ioX(i) = ioX(i) + alfa*iY(i)
end do
!$OMP END DO
!$OMP END PARALLEL
elseif(imode.eq.3) then
!$OMP PARALLEL DEFAULT(NONE) SHARED(iNoRows,ioX,iY,Alfa) PRIVATE(I)
!$OMP DO
do i=1,iNoRows
ioX(i) = alfa*ioX(i) +iY(i)
end do
!$OMP END DO
!$OMP END PARALLEL
elseif(imode.eq.4) then
!$OMP PARALLEL DEFAULT(NONE) SHARED(iNoRows,ioX,iY,alfa,ioZ,iV) PRIVATE(I)
!$OMP DO
do i=1,iNoRows
ioX(i) = ioX(i) + alfa*iY(i)
ioZ(i) = ioZ(i) - alfa*iV(i)
end do
!$OMP END DO
!$OMP END PARALLEL
end if
else
if (imode.eq.0) then
ioX(:) = iY(:)
elseif (imode.eq.1) then
ioX(:) = iY(:) - ioX(:)
elseif(imode.eq.2) then
ioX(:) = ioX(:) + alfa*iY(:)
elseif(imode.eq.3) then
ioX(:) = alfa*ioX(:) + iY(:)
elseif(imode.eq.4) then
ioX(:) = ioX(:) + alfa*iY(:)
ioZ(:) = ioZ(:) - alfa*iV(:)
end if
end if
end subroutine VectorAddition
subroutine Compute_Precondition_BLUT_real(iA,SolverSettings)
!Tue November 2014
!This routine sets up all the parameters needed for the Block LUT matrix factorization in parallel.
!After everything has been setup, then the factorization algorithm is called.
!IO
!iA -> input matrix, in CSR format, and of the type TSparseMat
!SolverSettings -> contains all the relevant settings the solvers need, it is also where stats about the solve is saved.
! This needs to be set before calling this routine.
use omp_lib
implicit none
type (TSolverSettings),intent(inout) :: SolverSettings
type (TSparseMat), intent(inout) :: iA
!Local Vars
integer :: i,Threads,NoRows,MaxRows,NoPrecondElements,SplitNoRows,ierr
integer,dimension(:),allocatable :: jw
integer,dimension(:,:),allocatable :: BRowIdx
real*8 :: Timer1, Timer2
real*8,dimension(:),allocatable :: w
Timer1=omp_get_wtime()
Threads=SolverSettings%NBlocks
allocate(BRowIdx(iA%NoRows,2))
call SMatGetBlockRowIdx(iA,SolverSettings%BlockS,BRowIdx)
MaxRows = 0
do i = 1 , threads
MaxRows = Max(MaxRows,SolverSettings%BlockS(i,2)-SolverSettings%BlockS(i,1)+1)
end do
NoPrecondElements=(2*SolverSettings%PrecondMaxFill+1)*MaxRows
Allocate(SolverSettings%BPrecondVals(NoPrecondElements,Threads))
Allocate(SolverSettings%BPrecondIdxs(NoPrecondElements,Threads))
Allocate(SolverSettings%BPrecondDiag(MaxRows,Threads))
!$OMP PARALLEL IF(StartOpenMP(2)) DEFAULT(SHARED) PRIVATE(i,SplitNoRows,w,jw,ierr,NoPrecondElements)
!$OMP DO SCHEDULE(STATIC)
do i = 1 , Threads
SplitNoRows=SolverSettings%BlockS(i,2)-SolverSettings%BlockS(i,1)+1
Allocate(w(SplitNoRows+1))
Allocate(jw(2*SplitNoRows))
NoPrecondElements=(2*SolverSettings%PrecondMaxFill)*SplitNoRows
call BLUT_Factorization_real(SplitNoRows,iA%Vals,iA%ColIdxs,BRowIdx(SolverSettings%BlockS(i,1):SolverSettings%BlockS(i,2),:),&
SolverSettings%PrecondMaxFill,SolverSettings%PrecondTolerance,SolverSettings%BPrecondVals(:,i),SolverSettings%BPrecondIdxs(:,i),&
SolverSettings%BPrecondDiag(:,i),NoPrecondElements,w,jw,SolverSettings%BlockS(i,1)-1,ierr)
!On Numa architechture we may now need to gather LU into one matrix and copy it around, depending whether we use PCG og BPCG routines.
deAllocate(w)
deAllocate(jw)
end do
!$OMP END DO
!$OMP END PARALLEL
Timer2=omp_get_wtime()
SolverSettings%Output%FactorizationTime=Timer2-Timer1
if (allocated(BRowIdx)) then
deallocate(BRowIdx)
end if
end subroutine Compute_Precondition_BLUT_real
!*************************************************************
subroutine BlockSplitter(iNoRows,iThreads,iParam,oBlocks)
!Tue, September 2014
!Splits iNoRows into iThreads Intervals.
!Each interval will have a length of iParam*n, where n is a natural number.
!The result will be saved in oBlocks, where oBlocks(i,1) is the start of the i'th interval and oBlocks(i,2) is the end of the interval
!IO
!Update 2015 Tue
!Fixed the case if iNoRows < iThreads
implicit none
Integer,intent(in) :: iNoRows,iThreads
Integer,intent(in) :: iParam
Integer,intent(out),allocatable :: oBlocks(:,:)
!Local vars
integer :: i
integer :: BlockSize
BlockSize=max(iNoRows/iThreads,1)
BlockSize =mod(IParam-mod(BlockSize,iParam),iParam)+BlockSize
Allocate(oBlocks(iThreads,2))
do i=1,iThreads
oBlocks(i,1) = (i-1)*(BlockSize)+1
oBlocks(i,2) = min(i*BlockSize,iNoRows)
end do
print*,'Blocks',oBlocks
end subroutine BlockSplitter
subroutine BLUT_Factorization_real(n,iVals,iColIdxs,iBRowIdxs,lfil,droptol,alu,jlu,ju,iwk,w,jw,Offset,ierr)
!Tue November 2014
!This routine is designed to compute an Incomplete Cholesky factorization, of a sparse linear system, limited by on a system.
!It has been made by modifying an old ILUT preconditioner made by Saad.
!A slightly modfified description of the ILUT preconditioner is found below.
!
!IO
!
!n -> Number of rows in input matrix
!iVals,iColIdxs -> Is the values and Column indexes of the input matrix stored in CSR format
!iBRowIdxs -> Is the cutoff row indices for the input matrix. iBRowIdxs(i,1) is the first element in row i we use, iBRowIdxs(i,2) is the last element in row i we use
!lfil -> The fill in parameter, each row of L and each row of U will have a maximum of lfil elements (excluding the diagonal element)
!droptol -> The minimum threshold before we drop a term.
!alu,jlu,ju -> the output LU matrix stored in modified sparse row format
!iwk -> the length of arrays alu,jlu
!w -> work array
!jw -> work array
!Offset -> The offset between the input matrix idx and the LU idx we want to save them in.
!ierr -> status of subroutine
!
!----------------------------------------------------------------------*
! *** ILUT preconditioner *** *
! incomplete LU factorization with dual truncation mechanism *
!----------------------------------------------------------------------*
!c Author: Yousef Saad *May, 5, 1990, Latest revision, August 1996 *
!c----------------------------------------------------------------------*
!c PARAMETERS
!c-----------
!c
!c on entry:
!c==========
!c n = integer. The row dimension of the matrix A. The matrix
!c
!c iVals,iColIdxs, = matrix stored in Compressed Sparse Row format.
!c iBRowIdxs is special!
!c lfil = integer. The fill-in parameter. Each row of L and each row
!c of U will have a maximum of lfil elements (excluding the
!c diagonal element). lfil must be .ge. 0.
!c ** WARNING: THE MEANING OF LFIL HAS CHANGED WITH RESPECT TO
!c EARLIER VERSIONS.
!c
!c droptol = real*8. Sets the threshold for dropping small terms in the
!c factorization. See below for details on dropping strategy.
!c
!c
!c iwk = integer. The lengths of arrays alu and jlu. If the arrays
!c are not big enough to store the ILU factorizations, ilut
!c will stop with an error message.
!c
!c On return:
!c===========
!c
!c alu,jlu = matrix stored in Modified Sparse Row (MSR) format containing
!c the L and U factors together. The diagonal (stored in
!c alu(1:n) ) is inverted. Each i-th row of the alu,jlu matrix
!c contains the i-th row of L (excluding the diagonal entry=1)
!c followed by the i-th row of U.
!c
!c ju = integer array of length n containing the pointers to
!c the beginning of each row of U in the matrix alu,jlu.
!c
!c ierr = integer. Error message with the following meaning.
!c ierr = 0 --> successful return.
!c ierr .gt. 0 --> zero pivot encountered at step number ierr.
!c ierr = -1 --> Error. input matrix may be wrong.
!c (The elimination process has generated a
!c row in L or U whose length is .gt. n.)
!c ierr = -2 --> The matrix L overflows the array al.
!c ierr = -3 --> The matrix U overflows the array alu.
!c ierr = -4 --> Illegal value for lfil.
!c ierr = -5 --> zero row encountered.
!c
!c work arrays:
!c=============
!c jw = integer work array of length 2*n.
!c w = real work array of length n+1.
!c
!c----------------------------------------------------------------------
!c w, ju (1:n) store the working array [1:ii-1 = L-part, ii:n = u]
!c jw(n+1:2n) stores nonzero indicators
!c
!c Notes:
!c ------
!c The diagonal elements of the input matrix must be nonzero (at least
!c 'structurally').
!c
!c----------------------------------------------------------------------*
!c---- Dual drop strategy works as follows. *
!c *
!c 1) Theresholding in L and U as set by droptol. Any element whose *
!c magnitude is less than some tolerance (relative to the abs *
!c value of diagonal element in u) is dropped. *
!c *
!c 2) Keeping only the largest lfil elements in the i-th row of L *
!c and the largest lfil elements in the i-th row of U (excluding *
!c diagonal elements). *
!c *
!c Flexibility: one can use droptol=0 to get a strategy based on *
!c keeping the largest elements in each row of L and U. Taking *
!c droptol .ne. 0 but lfil=n will give the usual threshold strategy *
!c (however, fill-in is then mpredictible). *
!c----------------------------------------------------------------------*
implicit none
integer,intent(in) :: n,lfil,iwk,offset
real*8,intent(in),dimension(:) :: iVals
real*8,intent(inout),dimension(:) :: alu,w
real*8,intent(in) :: droptol
integer,intent(in),dimension(:) :: iColIdxs
integer,intent(in),dimension(:,:) :: iBRowIdxs
integer,intent(inout),dimension(:):: jlu,ju,jw
integer,intent(inout) :: ierr
!c locals
integer ju0,k,j1,j2,j,ii,i,lenl,lenu,jj,jrow,jpos,length
real*8 tnorm, t, abs, s, fact
if (lfil .lt. 0) goto 998
!c-----------------------------------------------------------------------
!c initialize ju0 (points to next element to be added to alu,jlu)
!c and pointer array.
!c-----------------------------------------------------------------------
ju0 = n+2
jlu(1) = ju0
!c
!c initialize nonzero indicator array.
!c
do j=1,n
jw(n+j) = 0
end do
!c-----------------------------------------------------------------------
!c beginning of main loop.
!c-----------------------------------------------------------------------
do ii = 1, n
j1 = iBRowIdxs(ii,1)
j2 = iBRowIdxs(ii,2)
!j1=iRowIdxs(ii)
!j2=iRowIdxs(ii+1)-1
tnorm = 0.0d0
do k=j1,j2
tnorm = tnorm+abs(iVals(k))
end do
if (tnorm .eq. 0.0) goto 999
tnorm = tnorm/real(j2-j1+1)
!c
!c unpack L-part and U-part of row of A in arrays w
!c
lenu = 1
lenl = 0
jw(ii) = ii
w(ii) = 0.0
jw(n+ii) = ii
do j = j1, j2
k = iColIdxs(j)-offset
t = iVals(j)
if (k .lt. ii) then
lenl = lenl+1
jw(lenl) = k
w(lenl) = t
jw(n+k) = lenl
else if (k .eq. ii) then
w(ii) = t
else
lenu = lenu+1
jpos = ii+lenu-1
jw(jpos) = k
w(jpos) = t
jw(n+k) = jpos
endif
end do
jj = 0
length = 0
!c
!c eliminate previous rows
!c
150 jj = jj+1
if (jj .gt. lenl) goto 160
!c-----------------------------------------------------------------------
!c in order to do the elimination in the correct order we must select
!c the smallest column index among jw(k), k=jj+1, ..., lenl.
!c-----------------------------------------------------------------------
jrow = jw(jj)
k = jj
!c
!c determine smallest column index
!c
do j=jj+1,lenl
if (jw(j) .lt. jrow) then
jrow = jw(j)
k = j
end if
end do
if (k .ne. jj) then
!c exchange in jw
j = jw(jj)
jw(jj) = jw(k)
jw(k) = j
!c exchange in jr
jw(n+jrow) = jj
jw(n+j) = k
!c exchange in w
s = w(jj)
w(jj) = w(k)
w(k) = s
endif
!c
!c zero out element in row by setting jw(n+jrow) to zero.
!c
jw(n+jrow) = 0
!c
!c get the multiplier for row to be eliminated (jrow).
!c
fact = w(jj)*alu(jrow)
if (abs(fact) .le. droptol) goto 150
!c
!c combine current row and row jrow
!c
do k = ju(jrow), jlu(jrow+1)-1
s = fact*alu(k)
j = jlu(k)
jpos = jw(n+j)
if (j .ge. ii) then
!c
!c dealing with upper part.
!c
if (jpos .eq. 0) then
!c
!c this is a fill-in element
!c
lenu = lenu+1
if (lenu .gt. n) goto 995
i = ii+lenu-1
jw(i) = j
jw(n+j) = i
w(i) = - s
else
!c
!c this is not a fill-in element
!c
w(jpos) = w(jpos) - s
endif
else
!c
!c dealing with lower part.
!c
if (jpos .eq. 0) then
!c
!c this is a fill-in element
!c
lenl = lenl+1
if (lenl .gt. n) goto 995
jw(lenl) = j
jw(n+j) = lenl
w(lenl) = - s
else
!c
!c this is not a fill-in element
!c
w(jpos) = w(jpos) - s
endif
endif
end do
!c
!c store this pivot element -- (from left to right -- no danger of
!c overlap with the working elements in L (pivots).
!c
length = length+1
w(length) = fact
jw(length) = jrow
goto 150
160 continue
!c
!c reset double-pointer to zero (U-part)
!c
do k=1, lenu
jw(n+jw(ii+k-1)) = 0
end do
!c
!c update L-matrix
!c
lenl = length
length = min0(lenl,lfil)
!c
!c sort by quick-split
!c
call qsplit (w,jw,lenl,length)
!c
!c store L-part
!c
do k=1, length
if (ju0 .gt. iwk) goto 996
alu(ju0) = w(k)
jlu(ju0) = jw(k)
ju0 = ju0+1
end do
!c
!c save pointer to beginning of row ii of U
!c
ju(ii) = ju0
!c
!c update U-matrix -- first apply dropping strategy
!c
length = 0
do k=1, lenu-1
if (abs(w(ii+k)) .gt. droptol*tnorm) then
length = length+1
w(ii+length) = w(ii+k)
jw(ii+length) = jw(ii+k)
endif
enddo
lenu = length+1
length = min0(lenu,lfil)
call qsplit(w(ii+1:ii+lenu), jw(ii+1:ii+lenu), lenu-1,length)
!c
!c copy
!c
t = abs(w(ii))
if (length + ju0 .gt. iwk) goto 997
do k=ii+1,ii+length-1
jlu(ju0) = jw(k)
alu(ju0) = w(k)
t = t + abs(w(k) )
ju0 = ju0+1
end do
!c
!c store inverse of diagonal element of u
!c
if (w(ii) .eq. 0.0) w(ii) = (0.0001 + droptol)*tnorm
alu(ii) = 1.0d0/ w(ii)
!c
!c update pointer to beginning of next row of U.
!c
jlu(ii+1) = ju0
!c-----------------------------------------------------------------------
!c end main loop
!c-----------------------------------------------------------------------
end do
ierr = 0
return
!c
!c incomprehensible error. Matrix must be wrong.
!c
995 ierr = -1
return
!c
!c insufficient storage in L.
!c
996 ierr = -2
return
!c
!c insufficient storage in U.
!c
997 ierr = -3
return
!c
!c illegal lfil entered.
!c
998 ierr = -4
return
!c
!c zero row encountered
!c
999 ierr = -5
return
!c----------------end-of-ilut--------------------------------------------
end subroutine BLUT_Factorization_real
subroutine Apply_BPrecondition_BLUT(iBlocks, iRHS, oX, iLUVals, iLUIdxs, iLUDiag)
!Tue November 2014
!This routine does a forward and backward block solve with the Block LUT matrix
!
!IO
!
!iBlocks -> The blocksplitting of the Matrix
!iRHS -> The righthandside we update our solution after
!oX -> The output solution
!iLUVals,iLUIdxs,iLUDiag -> The Block LU matrix where each block is a separate matrix stored in modfied sparse row format.
integer,intent(in),dimension(:,:) :: iBlocks
real*8,intent(in),dimension(:) :: iRHS
real*8,intent(out),dimension(:) :: oX
real*8,intent(in),dimension(:,:) :: iLUVals
integer,intent(in),dimension(:,:) :: iLUIdxs, iLUDiag
!local variables
integer :: i,k,j,threads,m,tmp
threads=size(iBlocks,1)
!$OMP PARALLEL IF(StartOpenMP(2)) DEFAULT(NONE) SHARED(iBlocks,threads,iRHS,oX,iLUVals,iLUIdxs,iLUDiag) PRIVATE(k,i,j,m,tmp)
!$OMP DO SCHEDULE(STATIC)
do k=1,threads
do i = iBlocks(k,1),iblocks(k,2)
oX(i) = iRHS(i)
end do
!
! forward solve (with U^T)
!
tmp=iBlocks(k,1)-1
m=0
do i = iBlocks(k,1),iblocks(k,2)
m=m+1
oX(i) = oX(i) * iLUVals(m,k)
do j=iLUDiag(m,k),iLUIdxs(m+1,k)-1
oX(tmp+iLUIdxs(j,k)) = oX(tmp+iLUIdxs(j,k)) - iLUVals(j,k)* oX(i)
end do
end do
!
! backward solve (with L^T)
!
do i = iBlocks(k,2),iBlocks(k,1),-1
m=i-tmp
do j=iLUIdxs(m,k),iLUDiag(m,k)-1
oX(tmp+iLUIdxs(j,k)) = ox(tmp+iLUIdxs(j,k)) - iLUVals(j,k)*oX(i)
end do
end do
end do
!$OMP END DO
!$OMP END PARALLEL
end subroutine Apply_BPrecondition_BLUT
subroutine Apply_BPrecondition_BSGS(iColIdxs,iRowIdxs,iVals,iBRowIdx,iDiagIdx,iInvDiag,iRhS,ioX,Blocks)
!Tue, August 2014
!This routine does a forward and backward block solve with the Block SOR preconditioner
!This algorithm is described in 266-267 in the book "Iterative methods for sparse linear systems" in the normal non-block edition.
!Note that the SGS preconditioner is not calculated beforehand, but rather calculated on the fly during the applying of it.
!IO
!ispA -> Input, Sparse matrix of the linear system
!iDiagIdx -> Input, a vector containing indexes to the diagonal entries in the CSR matrix ispA.
!iInvDiag -> Input, a vector containing the inverse values of the diagonal element in ispA.
!iRhS -> Input, the right hand side of the linear system we try to solve.
!ioX -> Input, Solution estimate to update iteratively. Output, our new updated solution
!Blocks -> Input, contains the blocksplitting of ispA Blocks(i,1) contains the rowIdx of the first row in the i'th block, Blocks(i,2) contains the last.
implicit none
integer,intent(in),dimension(:) :: iColIdxs,iRowIdxs
real*8,intent(in),dimension(:) :: iVals
integer,intent(in),dimension(:,:) :: iBRowIdx
integer,intent(in),dimension(:) :: iDiagIdx
real*8,intent(in),dimension(:) :: iInvDiag
real*8,intent(in),dimension(:) :: iRHS
real*8,intent(inout),dimension(:) :: ioX
integer,intent(in) :: Blocks(:,:)
!Local vars
integer :: i,j,k
real*8 :: tmp
real*8 :: Factor
integer :: threads
real*8 :: omega
threads=size(Blocks,1)
!$OMP PARALLEL IF(StartOpenMP(2)) DEFAULT(NONE) SHARED(omega,Blocks,iDiagIdx,iInvDiag,iColIdxs,iRowIdxs,iVals,ioX,irhs,threads,iBRowIdx) PRIVATE(i,k,j)
!c$OMP CRITICAL
!$OMP DO SCHEDULE(STATIC)
do k=1,threads
!We start by doing the forward step
do i = Blocks(k,1),Blocks(k,2)
ioX(i) = iRHS(i)
do j = iBRowIdx(i,1) , iDiagIdx(i)-1
ioX(i) = ioX(i) - iVals(j)*iInvDiag(iColIdxs(j))*ioX(iColIdxs(j))
end do
end do
!Now comes the backward step
do i = Blocks(k,2),Blocks(k,1),-1
do j = iBRowIdx(i,2) ,iDiagIdx(i)+1,-1
ioX(i) = ioX(i) - iVals(j)*ioX(iColIdxs(j))
end do
ioX(i) = ioX(i)*iInvDiag(i)
end do
end do
!$OMP END DO
!c$OMP END CRITICAL
!$OMP END PARALLEL
end subroutine Apply_BPrecondition_BSGS
subroutine SetSolverSettings(SolverSettings,A,iUseRCM,iUseNormalization,iSolverType,iIter,iBlockSize,iFillingFactor,iStepLimit)
!Tue Dec, 2014
!This routine Sets the solversettings prior to a linear solve.
!NOTE that this also calculates the precondition matrix, which the caller takes responsibility to free. The freeing should be done by calling the FreeSolverSettings subroutine.
!any optional value you wish chosen by the subroutine should be set to -1
!IO
!SolverSettings -> This is where we save all the relevant settings about the solve we want to do - note if we have preconditioner this is also saved in here.
!A -> input matrix, in CSR format, and of the type TSparseMat
!(Optional) The rest are optional parameters which can be chosen manually, any parameter set to -1 will be chosen automatically.
!iUseNormalization -> (optional) Use Normalization
!iSolverType -> (optional) manually chooses the solvertype, if none is selected or set to -1, a fitting type will be chosen based on the problem.
! 1 SGS
! 2 BLUT
! 3 Pardiso (not implemented here)
!iIter -> (optional) manually set the maximum iterations. (default is DEFAULT_MAXITERATIONS)
!iBaseBlockSize -> (optional) The blocksize will be a multiplum of this number for all blocks if specified.
! this is used for blocksplitting to make sure we don't split in the middle of a block.
!iFillingFactor -> (optional) Determines the number of elements allowed in the IC factorization. Factor is based on the average number of elements in iA; default value 1.5
!iStepLimit -> (optional) sets the relative minimum difference an iterative step should make in the solution
! This could be important to set when using block solvers where convergence to the solution is not guaranteed
use omp_lib
use msMat
use mRCM
implicit none
type (TSolverSettings),intent(inout) :: SolverSettings
type (TSparseMat), intent(inout) :: A
integer, intent(in),optional :: iUseRCM
integer, intent(in),optional :: iUseNormalization
integer, intent(in),optional :: iSolverType
integer, intent(in),optional :: iIter
integer, intent(in),optional :: iBlockSize