-
Notifications
You must be signed in to change notification settings - Fork 0
/
lapack_wrap.F90
758 lines (598 loc) · 26.1 KB
/
lapack_wrap.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
!-----------------------------------------------------------------------
! $Id$
!===============================================================================
module lapack_wrap
! Description:
! Wrappers for the band diagonal and tridiagonal direct matrix
! solvers contained in the LAPACK library.
! References:
! LAPACK--Linear Algebra PACKage
! URL: <http://www.netlib.org/lapack/>
!-----------------------------------------------------------------------
use constants_clubb, only: &
fstderr ! Variable(s)
use clubb_precision, only: &
core_rknd ! Variable
implicit none
! Simple routines
public :: lapack_tridiag_solve, &
lapack_band_solve
! Expert routines
public :: lapack_tridiag_solvex, &
lapack_band_solvex
private ! Set Default Scope
contains
!-----------------------------------------------------------------------
subroutine lapack_tridiag_solvex( solve_type, ndim, nrhs, ngrdcol, &
lhs, rhs, &
soln, rcond )
! Description:
! Solves a tridiagonal system of equations (expert routine).
! References:
! <http://www.netlib.org/lapack/single/sgtsvx.f>
! <http://www.netlib.org/lapack/double/dgtsvx.f>
! Notes:
! More expensive than the simple routine, but tridiagonal
! decomposition is still relatively cheap.
!-----------------------------------------------------------------------
use clubb_precision, only: &
core_rknd ! Variable(s)
use error_code, only: &
clubb_at_least_debug_level, & ! Procedure
err_code, & ! Error Indicator
clubb_fatal_error ! Constants
use lapack_interfaces, only: &
lapack_gtsvx, & ! Procedure
lapack_isnan
implicit none
intrinsic :: kind
! ----------------------- Input variables -----------------------
character(len=*), intent(in) :: &
solve_type ! Used to write a message if this fails
integer, intent(in) :: &
ndim, & ! N-dimension of matrix
ngrdcol, & ! Number of grid columns
nrhs ! # of right hand sides to back subst. after LU-decomp.
! ----------------------- Input/Output variables -----------------------
real( kind = core_rknd ), intent(inout), dimension(3,ngrdcol,ndim) :: &
lhs ! Tridiagonal LHS
real( kind = core_rknd ), intent(inout), dimension(ngrdcol,ndim,nrhs) :: &
rhs ! RHS input
! The estimate of the reciprocal of the condition number on the LHS matrix.
! If rcond is < machine precision the matrix is singular to working
! precision, and info == ndim+1. If rcond == 0, then the LHS matrix
! is singular. This condition is indicated by a return code of info > 0.
real( kind = core_rknd ), dimension(ngrdcol), intent(out) :: rcond
! ----------------------- Output variables -----------------------
real( kind = core_rknd ), intent(out), dimension(ngrdcol,ndim,nrhs) :: &
soln ! solution
! ----------------------- Local Variables -----------------------
! These contain the decomposition of the matrix
real( kind = core_rknd ), dimension(ndim-1) :: dlf, duf
real( kind = core_rknd ), dimension(ndim) :: df
real( kind = core_rknd ), dimension(ndim-2) :: du2
integer, dimension(ndim) :: &
ipivot ! Index of pivots done during decomposition
integer, dimension(ndim) :: &
iwork ! `scrap' array
real( kind = core_rknd ), dimension(ngrdcol,nrhs) :: &
ferr, & ! Forward error estimate
berr ! Backward error estimate
real( kind = core_rknd ), dimension(3*ndim) :: &
work ! `Scrap' array
integer :: info ! Diagnostic output
integer :: i, n ! Array index
!-----------------------------------------------------------------------
! *** The LAPACK Routine ***
! SUBROUTINE SGTSVX( FACT, TRANS, N, NRHS, DL, D, DU, DLF, DF, DUF,
! $ DU2, IPIV, B, LDB, X, LDX, RCOND, FERR, BERR,
! $ WORK, IWORK, INFO )
!-----------------------------------------------------------------------
! Lapack tridiagonal matrix solver, expert version, sgtsvx for single
! or dgtsvx for double precision
do i = 1, ngrdcol
call lapack_gtsvx( "Not Factored", "No Transpose lhs", ndim, nrhs, &
lhs(3,i,2:ndim), lhs(2,i,:), lhs(1,i,1:ndim-1), &
dlf, df, duf, du2, ipivot, &
rhs(i,:,:), ndim, soln(i,:,:), ndim, rcond(i), &
ferr(i,:), berr(i,:), work, iwork, info )
end do
! Print diagnostics for when ferr is large
if ( clubb_at_least_debug_level( 2 ) .and. any( ferr > 1.e-3_core_rknd ) ) then
write(fstderr,*) "Warning, large error est. for: " // trim( solve_type )
do n = 1, nrhs
do i = 1, ngrdcol
write(fstderr,*) "grdcol #", i, "rhs # ", i, "tridiag forward error est. =", ferr(i,n)
write(fstderr,*) "grdcol #", i, "rhs # ", i, "tridiag backward error est. =", berr(i,n)
end do
end do
write(fstderr,'(2(a20,e15.6))') "rcond est. = ", rcond, &
"machine epsilon = ", epsilon( lhs(1,1,1) )
end if
select case( info )
case( :-1 )
write(fstderr,*) trim( solve_type )// &
"illegal value in argument", -info
err_code = clubb_fatal_error
case( 0 )
! Success
do i = 1, ngrdcol
if ( lapack_isnan( ndim, nrhs, soln(i,:,:) ) ) then
err_code = clubb_fatal_error
end if
end do
case( 1: )
if ( info == ndim+1 ) then
write(fstderr,*) trim( solve_type) // &
" Warning: matrix is singular to working precision."
write(fstderr,'(a,e12.5)') &
"Estimate of the reciprocal of the condition number: ", rcond
else
write(fstderr,*) solve_type// &
" singular matrix."
err_code = clubb_fatal_error
end if
end select
return
end subroutine lapack_tridiag_solvex
!-----------------------------------------------------------------------
subroutine lapack_tridiag_solve( solve_type, ndim, nrhs, ngrdcol, &
lhs, rhs, &
soln )
! Description:
! Solves a tridiagonal system of equations (simple routine)
! References:
! <http://www.netlib.org/lapack/single/sgtsv.f>
! <http://www.netlib.org/lapack/double/dgtsv.f>
!-----------------------------------------------------------------------
use clubb_precision, only: &
core_rknd ! Variable(s)
#ifdef E3SM
#ifndef NDEBUG
#if defined(ARCH_MIC_KNL) && defined(CPRINTEL)
use, intrinsic :: ieee_exceptions
#endif
#endif
#endif /*E3SM*/
use error_code, only: &
err_code, & ! Error Indicator
clubb_fatal_error ! Constants
use lapack_interfaces, only: &
lapack_gtsv, & ! Procedure
lapack_isnan
implicit none
intrinsic :: kind
! ----------------------- Input variables -----------------------
character(len=*), intent(in) :: &
solve_type ! Used to write a message if this fails
integer, intent(in) :: &
ndim, & ! N-dimension of matrix
ngrdcol, & ! Number of grid columns
nrhs ! # of right hand sides to back subst. after LU-decomp.
! ----------------------- Input/Output Variables -----------------------
real( kind = core_rknd ), intent(inout), dimension(3,ngrdcol,ndim) :: &
lhs ! Tridiagonal LHS input
real( kind = core_rknd ), intent(inout), dimension(ngrdcol,ndim,nrhs) :: &
rhs ! RHS input
! ----------------------- Output variables -----------------------
real( kind = core_rknd ), intent(out), dimension(ngrdcol,ndim,nrhs) :: &
soln ! solution
! ----------------------- Local Variables -----------------------
real( kind = core_rknd ), dimension(ndim) :: &
subd, diag, supd
integer :: &
info, & ! Diagnostic output
i ! Loop var
!-----------------------------------------------------------------------
! *** The LAPACK Routine ***
! SUBROUTINE DGTSV( N, NRHS, DL, D, DU, B, LDB, INFO )
!-----------------------------------------------------------------------
#ifdef E3SM
#ifndef NDEBUG
#if defined(ARCH_MIC_KNL) && defined(CPRINTEL)
! when floating-point exceptions are turned on, this call was failing with
! a div-by-zero on KNL with Intel/MKL. Solution was to turn off exceptions
! only here at this call (and only for machine with ARCH_MIC_KNL defined)
! (github 1183)
call ieee_set_halting_mode(IEEE_DIVIDE_BY_ZERO, .false.) ! Turn off stopping on div-by-zero only
#endif
#endif
#endif /*E3SM*/
! Interface for Lapack tridiagonal matrix solver, sgtsv for single
! or dgtsv for double precision
do i = 1, ngrdcol
call lapack_gtsv( ndim, nrhs, lhs(3,i,2:ndim), lhs(2,i,:), lhs(1,i,1:ndim-1), &
rhs(i,:,:), ndim, info )
end do
#ifdef E3SM
#ifndef NDEBUG
#if defined(ARCH_MIC_KNL) && defined(CPRINTEL)
! Turn back on stopping on div-by-zero only
call ieee_set_halting_mode(IEEE_DIVIDE_BY_ZERO, .true.)
#endif
#endif
#endif /*E3SM*/
select case( info )
case( :-1 )
write(fstderr,*) trim( solve_type )// &
" illegal value in argument", -info
err_code = clubb_fatal_error
soln = -999._core_rknd
case( 0 )
! Success
do i = 1, ngrdcol
if ( lapack_isnan( ndim, nrhs, rhs(i,:,:) ) ) then
err_code = clubb_fatal_error
end if
end do
soln = rhs
case( 1: )
write(fstderr,*) trim( solve_type )//" singular matrix."
err_code = clubb_fatal_error
soln = -999._core_rknd
end select
return
end subroutine lapack_tridiag_solve
!-----------------------------------------------------------------------
subroutine lapack_band_solvex( solve_type, nsup, nsub, &
ndim, nrhs, ngrdcol, &
lhs, rhs, &
soln, rcond )
! Description:
! Restructure and then solve a band diagonal system, with
! diagnostic output
! References:
! <http://www.netlib.org/lapack/single/sgbsvx.f>
! <http://www.netlib.org/lapack/double/dgbsvx.f>
! Notes:
! I found that due to the use of sgbcon/dgbcon it is much
! more expensive to use this on most systems than the simple
! driver. Use this version only if you don't case about compute time.
! Also note that this version equilibrates the lhs and does an iterative
! refinement of the solutions, which results in a slightly different answer
! than the simple driver does. -dschanen 24 Sep 2008
!-----------------------------------------------------------------------
use clubb_precision, only: &
core_rknd ! Variable(s)
use error_code, only: &
clubb_at_least_debug_level, & ! Procedure
err_code, & ! Error Indicator
clubb_fatal_error ! Constants
use lapack_interfaces, only: &
lapack_gbsvx, & ! Procedures
lapack_isnan
implicit none
! ------------------------------ Input Variables ------------------------------
character(len=*), intent(in) :: solve_type
integer, intent(in) :: &
nsup, & ! Number of superdiagonals
nsub, & ! Number of subdiagonals
ngrdcol, & ! Number of grid columns
ndim, & ! The order of the LHS Matrix, i.e. the # of linear equations
nrhs ! Number of RHS's to solve for
! ------------------------------ InOut Variables ------------------------------
real( kind = core_rknd ), dimension(nsup+nsub+1,ngrdcol,ndim), intent(inout) :: &
lhs ! Left hand side
real( kind = core_rknd ), dimension(ngrdcol,ndim,nrhs), intent(inout) :: &
rhs ! Right hand side(s)
! ------------------------------ Output Variables ------------------------------
real( kind = core_rknd ), dimension(ngrdcol,ndim,nrhs), intent(out) :: &
soln
! The estimate of the reciprocal condition number of matrix
! after equilibration (if done).
real( kind = core_rknd ), dimension(ngrdcol), intent(out) :: &
rcond
! ------------------------------ Local Variables ------------------------------
! Workspaces
real( kind = core_rknd ), dimension(3*ndim) :: work
integer, dimension(ndim) :: iwork
real( kind = core_rknd ), dimension(2*nsub+nsup+1,ndim) :: &
lulhs ! LU Decomposition of the LHS
integer, dimension(ndim) :: &
ipivot
real( kind = core_rknd ), dimension(ngrdcol,nrhs) :: &
ferr, berr ! Forward and backward error estimate
real( kind = core_rknd ), dimension(ndim) :: &
rscale, cscale ! Row and column scale factors for the LHS
integer :: &
info, & ! If this doesn't come back as 0, something went wrong
offset, & ! Loop iterator
imain, & ! Main diagonal of the matrix
i,n ! Loop iterator
character :: &
equed ! Row equilibration status
!-----------------------------------------------------------------------
! Reorder Matrix to use LAPACK band matrix format (5x6)
! Shift example:
! [ * * lhs(1,1) lhs(1,2) lhs(1,3) lhs(1,4) ] (2)=>
! [ * lhs(2,1) lhs(2,2) lhs(2,3) lhs(2,4) lhs(2,5) ] (1)=>
! [ lhs(3,1) lhs(3,2) lhs(3,3) lhs(3,4) lhs(3,5) lhs(3,6) ]
! <=(1) [ lhs(4,2) lhs(4,3) lhs(4,4) lhs(4,5) lhs(4,6) * ]
! <=(2) [ lhs(5,3) lhs(5,4) lhs(5,5) lhs(5,6) * * ]
! The '*' indicates unreferenced elements.
! For additional bands above and below the main diagonal, the
! shifts to the left or right increases by the distance from the
! main diagonal of the matrix.
!-----------------------------------------------------------------------
imain = nsup + 1
! For the offset, (+) is left, and (-) is right
! Sub diagonals
do i = 1, ngrdcol
do offset = 1, nsub, 1
lhs(imain+offset,i,1:ndim) = eoshift( lhs(imain+offset,i,1:ndim), offset )
end do
end do
! Super diagonals
do i = 1, ngrdcol
do offset = 1, nsup, 1
lhs(imain-offset,i,1:ndim) = eoshift( lhs(imain-offset,i,1:ndim), -offset )
end do
end do
!-----------------------------------------------------------------------
! *** The LAPACK Routine ***
! SUBROUTINE SGBSVX( FACT, TRANS, N, KL, KU, NRHS, AB, LDAB, AFB,
! $ LDAFB, IPIV, EQUED, R, C, B, LDB, X, LDX,
! $ RCOND, FERR, BERR, WORK, IWORK, INFO )
!-----------------------------------------------------------------------
! Lapack general band solver, expert version, sgbsvx for single
! or dgbsvx for double precision
do i = 1, ngrdcol
call lapack_gbsvx( 'Equilibrate lhs', 'No Transpose lhs', &
ndim, nsub, nsup, nrhs, &
lhs(:,i,:), nsup+nsub+1, lulhs, 2*nsub+nsup+1, &
ipivot, equed, rscale, cscale, &
rhs(i,:,:), ndim, soln(i,:,:), ndim, &
rcond(i), ferr(i,:), berr(i,:), work, iwork, info )
end do
! %% debug
! select case ( equed )
! case ('N')
! print *, "No equilib. was required for lhs."
! case ('R')
! print *, "Row equilib. was done on lhs."
! case ('C')
! print *, "Column equilib. was done on lhs."
! case ('B')
! print *, "Row and column equilib. was done on lhs."
! end select
! write(*,'(a,e12.5)') "Row scale : ", rscale
! write(*,'(a,e12.5)') "Column scale: ", cscale
! write(*,'(a,e12.5)') "Estimate of the reciprocal of the "//
! "condition number: ", rcond
! write(*,'(a,e12.5)') "Forward Error Estimate: ", ferr
! write(*,'(a,e12.5)') "Backward Error Estimate: ", berr
! %% end debug
! Diagnostic information
if ( clubb_at_least_debug_level( 2 ) .and. any( ferr > 1.e-3_core_rknd ) ) then
write(fstderr,*) "Warning, large error est. for: " // trim( solve_type )
do n = 1, nrhs
do i = 1, ngrdcol
write(fstderr,*) "grdcol #", i, "rhs # ", n, "band_solvex forward error est. =", ferr(i,n)
write(fstderr,*) "grdcol #", i, "rhs # ", n, "band_solvex backward error est. =", berr(i,n)
end do
end do
write(fstderr,'(2(a20,e15.6))') "rcond est. = ", rcond, &
"machine epsilon = ", epsilon( lhs(1,1,1) )
end if
select case( info )
case( :-1 )
write(fstderr,*) "in band_solvex for ", trim( solve_type ), &
": illegal value for argument", -info
err_code = clubb_fatal_error
case( 0 )
! Success!
do i = 1, ngrdcol
if ( lapack_isnan( ndim, nrhs, soln(i,:,:) ) ) then
err_code = clubb_fatal_error
end if
end do
case( 1: )
if ( info == ndim+1 ) then
write(fstderr,*) trim( solve_type )// &
" Warning: matrix singular to working precision."
write(fstderr,'(a,e12.5)') &
"Estimate of the reciprocal of the"// &
" condition number: ", rcond
else
write(fstderr,*) "in band_solvex for", trim( solve_type ), &
": singular matrix, solution not computed"
err_code = clubb_fatal_error
end if
end select
return
end subroutine lapack_band_solvex
!-----------------------------------------------------------------------
subroutine lapack_band_solve( solve_type, nsup, nsub, &
ndim, nrhs, ngrdcol, &
lhs, rhs, &
soln )
! Description:
! Restructure and then solve a band diagonal system
! References:
! <http://www.netlib.org/lapack/single/sgbsv.f>
! <http://www.netlib.org/lapack/double/dgbsv.f>
!-----------------------------------------------------------------------
use clubb_precision, only: &
core_rknd ! Variable(s)
use error_code, only: &
clubb_at_least_debug_level, &
err_code, & ! Error Indicator
clubb_fatal_error ! Constants
use lapack_interfaces, only: &
lapack_gbsv, & ! Procedures
lapack_isnan
implicit none
! ------------------------------ Input Variables ------------------------------
character(len=*), intent(in) :: solve_type
integer, intent(in) :: &
nsup, & ! Number of superdiagonals
nsub, & ! Number of subdiagonals
ngrdcol, & ! Number of grid columns
ndim, & ! The order of the LHS Matrix, i.e. the # of linear equations
nrhs ! Number of RHS's to solve for
! Note: matrix lhs is intent(in), not intent(inout)
! as in the subroutine band_solvex( )
real( kind = core_rknd ), dimension(nsup+nsub+1,ngrdcol,ndim), intent(in) :: &
lhs ! Left hand side
real( kind = core_rknd ), dimension(ngrdcol,ndim,nrhs), intent(inout) :: &
rhs ! Right hand side(s)
! ------------------------------ Output Variables ------------------------------
real( kind = core_rknd ), dimension(ngrdcol,ndim,nrhs), intent(out) :: &
soln
! ------------------------------ Local Variables ------------------------------
! Workspaces
real( kind = core_rknd ), dimension(2*nsub+nsup+1,ndim,ngrdcol) :: &
lulhs ! LU Decomposition of the LHS
integer, dimension(ndim) :: &
ipivot
integer :: &
info, & ! If this doesn't come back as 0, something went wrong
imain ! Main diagonal of the matrix
integer :: i, j, d
!-----------------------------------------------------------------------
! Reorder LU Matrix to use LAPACK band matrix format
!
! Shift example for lulhs matrix given a 5x5 lhs matrix
!
!
! lulhs =
! Columns
! 1 2 3 4 5 6 7
! Rows
! 1 [ 0 0 0 0 lhs(3,1) lhs(4,2) lhs(5,3) ]
! 2 [ 0 0 0 lhs(2,1) lhs(3,2) lhs(4,3) lhs(5,4) ]
! 3 [ 0 0 lhs(1,1) lhs(2,2) lhs(3,3) lhs(4,4) lhs(5,5) ]
! 4 [ 0 0 lhs(1,2) lhs(2,3) lhs(3,4) lhs(4,5) 0 ]
! 5 [ 0 0 lhs(1,3) lhs(2,4) lhs(3,5) 0 0 ]
!
! all lhs lhs lhs lhs lhs
! set to shifted shifted no shifted shifted
! 0 down 2 down 1 shift up 1 up 2
!
! The first nsup columns of lulhs are always set to 0;
! the rest of the columns are set to shifted
! columns of lhs. This can be thought of as taking lhs, never touching the middle column, but
! shifting the columns that are n columns to the left of the middle down by n rows, and then
! shifting the columns that are n columns to the right of the middle up by n rows, finally
! adding nsup columns of zeros onto the left of the array. This results in lulhs.
!-----------------------------------------------------------------------
! Reorder lulhs, omitting the additional 2*nsub bands
! that are used for the LU decomposition of the matrix.
imain = nsub + nsup + 1
! The first nsup rows of lulhs will contain 0s that are end-shifted lhs values. This needs
! to be handled differently so the algorithm to access lhs will not try to use out of bound
! values.
! ... nsup nsup+1 ... imain ...
! \ / \ /
! always | begins with nsup | all lhs values
! 0 0s, and decreases
! by one 0 each row
!
! ... nsup nsup+1 ... imain ...
! lulhs(:,1) = 0 0 0 0 lhs lhs
! lulhs(:,2) = 0 0 0 lhs lhs lhs
!
! Since the first nsup rows are the first rows in lulhs, we're going to access them first to
! avoid out of order memory accesses.
do i = 1, ngrdcol
do d = 1, nsup
! Add 0s to first nsup columns, and decreasing number of end-shift affected columns
do j = 1, imain-d
lulhs(j,d,i) = 0.0_core_rknd
end do
! Copy lhs values into appropriate lulhs spots
do j = imain-d+1, imain+nsub
lulhs(j,d,i) = lhs(j-nsub,i,d+j-imain)
end do
end do
end do
! After the first nsup rows are dealt with, the offset lhs values can be copied into lulhs
! until the last nsup rows are reached. This is because the last nsup rows also contain
! end-shifted values, set to 0 in the next loop.
!
! ... nsup nsup+1 ...
! \ /
! always | all lhs values
!
! ... nsup nsup+1 ...
! lulhs(:,nsup+1) = 0 0 lhs lhs
! lulhs(:,ndim-nsub) = 0 0 lhs lhs
!
! For all values not affected by end-shifting
do i = 1, ngrdcol
do d = nsup+1, ndim-nsub
! Set first nsup columns to 0
do j = 1, nsub
lulhs(j,d,i) = 0.0_core_rknd
end do
! Copy lhs values into appropriate lulhs spots
do j = imain-nsub, imain+nsub
lulhs(j,d,i) = lhs(j-nsub,i, d+j-imain)
end do
end do
end do
! The last nsup rows of lulhs will contain 0s that are end-shifted lhs values. This needs
! to be handled differently so the algorithm to access lhs will not try to use out of bound
! values.
!
!
! ... nsup nsup+1 ... imain+1 ...
! lulhs(:,ndim-nsub+1) = 0 0 lhs lhs lhs 0
! lulhs(:,ndim) = 0 0 lhs lhs 0 0
!
! | | starts with one 0, then
! always | all lhs values | then increases to nsup 0s
! | | towards ndim
! / \ / \
! ... nsup nsup+1 ... ndim-nsub+1 ... ndim
!
! Finish the lulhs setup by accessing the last values last, keeping memory access ordered
do i = 1, ngrdcol
do d = ndim-nsub+1, ndim
! Set first nsup columns to 0
do j = 1, nsub
lulhs(j,d,i) = 0.0_core_rknd
end do
! Copy lhs values into appropriate lulhs spots
do j = imain-nsup, imain-(d-ndim)
lulhs(j,d,i) = lhs(j-nsub,i, d+j-imain)
end do
! Set increasing number of end-shift affected columns to 0
do j = imain-(d-ndim)+1, imain+nsub
lulhs(j,d,i) = 0.0_core_rknd
end do
end do
end do
!-----------------------------------------------------------------------
! *** LAPACK routine ***
! SUBROUTINE DGBSV( N, KL, KU, NRHS, AB, LDAB, IPIV, B, LDB, INFO )
!-----------------------------------------------------------------------
! Lapack general band solver, sgbsv for single
! or dgbsv for double precision
do i = 1, ngrdcol
call lapack_gbsv( ndim, nsub, nsup, nrhs, lulhs(:,:,i), nsub*2+nsup+1, &
ipivot, rhs(i,:,:), ndim, info )
end do
select case( info )
case( :-1 )
write(fstderr,*) "in band_solve for ", trim( solve_type ), &
": illegal value for argument", -info
err_code = clubb_fatal_error
case( 0 )
! Success!
if ( clubb_at_least_debug_level( 1 ) ) then
do i = 1, ngrdcol
if ( lapack_isnan( ndim, nrhs, rhs(i,:,:) ) ) then
err_code = clubb_fatal_error
end if
end do
end if
soln = rhs
case( 1: )
write(fstderr,*) "in band_solve for ", trim( solve_type ), &
": singular matrix, solution not computed"
err_code = clubb_fatal_error
end select
return
end subroutine lapack_band_solve
end module lapack_wrap