-
Notifications
You must be signed in to change notification settings - Fork 0
/
tea_solve.f90
1215 lines (1032 loc) · 56.6 KB
/
tea_solve.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
!Crown Copyright 2014 AWE.
!
! This file is part of TeaLeaf.
!
! TeaLeaf is free software: you can redistribute it and/or modify it under
! the terms of the GNU General Public License as published by the
! Free Software Foundation, either version 3 of the License, or (at your option)
! any later version.
!
! TeaLeaf is distributed in the hope that it will be useful, but
! WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
! FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
! details.
!
! You should have received a copy of the GNU General Public License along with
! TeaLeaf. If not, see http://www.gnu.org/licenses/.
!> @brief Driver for the heat conduction kernel
!> @author David Beckingsale, Wayne Gaudin, Douglas Shanks
!> @details Invokes the user specified kernel for the heat conduction
MODULE tea_leaf_module
USE report_module
USE data_module
USE tea_leaf_kernel_common_module
USE tea_leaf_kernel_jacobi_module
USE tea_leaf_kernel_cg_module
USE tea_leaf_kernel_ppcg_module
USE tea_leaf_kernel_cheby_module
USE update_halo_module
IMPLICIT NONE
CONTAINS
SUBROUTINE tea_leaf()
IMPLICIT NONE
!$ INTEGER :: OMP_GET_THREAD_NUM
INTEGER :: c, n
REAL(KIND=8) :: ry,rx,rz,old_error,error,exact_error,initial_residual
INTEGER :: fields(NUM_FIELDS)
REAL(KIND=8) :: timer,halo_time,solve_time,init_time,reset_time,dot_product_time
! For CG solver
REAL(KIND=8) :: rro, pw, rrn, alpha, beta
! For chebyshev solver
REAL(KIND=8), DIMENSION(max_iters) :: cg_alphas, cg_betas
REAL(KIND=8), DIMENSION(max_iters) :: ch_alphas, ch_betas
REAL(KIND=8),SAVE :: eigmin, eigmax, theta, cn
INTEGER :: est_itc, cheby_calc_steps, max_cheby_iters, info, ppcg_inner_iters, petsc_mod, itcount
LOGICAL :: ch_switch_check
LOGICAL, SAVE :: first=.TRUE.
INTEGER :: cg_calc_steps
REAL(KIND=8) :: cg_time, ch_time, total_solve_time, ch_per_it, cg_per_it, iteration_time
cg_time = 0.0_8
ch_time = 0.0_8
cg_calc_steps = 0
ppcg_inner_iters = 0
ch_switch_check = .false.
tl_ppcg_active = .false. ! Set to false until we have the eigenvalue estimates
total_solve_time = 0.0_8
init_time = 0.0_8
halo_time = 0.0_8
solve_time = 0.0_8
dot_product_time = 0.0_8
IF(coefficient .NE. RECIP_CONDUCTIVITY .AND. coefficient .NE. conductivity) THEN
CALL report_error('tea_leaf', 'unknown coefficient option')
ENDIF
cheby_calc_steps = 0
cg_calc_steps = 0
initial_residual = 0.0_8
total_solve_time = timer()
DO c=1,chunks_per_task
IF(chunks(c)%task.EQ.parallel%task) THEN
! INIT
IF (profiler_on) init_time=timer()
fields=0
fields(FIELD_ENERGY1) = 1
fields(FIELD_DENSITY) = 1
IF (profiler_on) halo_time=timer()
CALL update_halo(fields,halo_exchange_depth)
IF (profiler_on) init_time = init_time + (timer()-halo_time)
!===================================================================================
! General set up
!===================================================================================
IF (use_fortran_kernels) THEN
rx = dt/(chunks(c)%field%celldx(chunks(c)%field%x_min)**2)
ry = dt/(chunks(c)%field%celldy(chunks(c)%field%y_min)**2)
rz = dt/(chunks(c)%field%celldz(chunks(c)%field%z_min)**2)
CALL tea_leaf_kernel_init_common(chunks(c)%field%x_min, &
chunks(c)%field%x_max, &
chunks(c)%field%y_min, &
chunks(c)%field%y_max, &
chunks(c)%field%z_min, &
chunks(c)%field%z_max, &
halo_exchange_depth, &
chunks(c)%chunk_neighbours, &
reflective_boundary, &
chunks(c)%field%density, &
chunks(c)%field%energy1, &
chunks(c)%field%u, &
chunks(c)%field%u0, &
chunks(c)%field%vector_r, &
chunks(c)%field%vector_w, &
chunks(c)%field%vector_Kx, &
chunks(c)%field%vector_Ky, &
chunks(c)%field%vector_Kz, &
chunks(c)%field%tri_cp, &
chunks(c)%field%tri_bfp, &
chunks(c)%field%vector_Mi, &
rx, ry, rz, tl_preconditioner_type, coefficient)
ENDIF
fields=0
fields(FIELD_U) = 1
IF (profiler_on) halo_time=timer()
CALL update_halo(fields,1)
IF (profiler_on) init_time = init_time + (timer()-halo_time)
!===================================================================================
! Compute the initial_residual
!===================================================================================
IF(use_fortran_kernels) THEN
CALL tea_leaf_calc_residual(chunks(c)%field%x_min,&
chunks(c)%field%x_max, &
chunks(c)%field%y_min, &
chunks(c)%field%y_max, &
chunks(c)%field%z_min, &
chunks(c)%field%z_max, &
halo_exchange_depth, &
chunks(c)%field%u, &
chunks(c)%field%u0, &
chunks(c)%field%vector_r, &
chunks(c)%field%vector_Kx, &
chunks(c)%field%vector_Ky, &
chunks(c)%field%vector_Kz, &
rx, ry, rz)
CALL tea_leaf_calc_2norm_kernel(chunks(c)%field%x_min,&
chunks(c)%field%x_max, &
chunks(c)%field%y_min, &
chunks(c)%field%y_max, &
chunks(c)%field%z_min, &
chunks(c)%field%z_max, &
halo_exchange_depth, &
chunks(c)%field%vector_r, &
initial_residual)
ENDIF
IF (profiler_on) dot_product_time=timer()
CALL tea_allsum(initial_residual)
IF (profiler_on) profiler%dot_product= profiler%dot_product+ (timer() - dot_product_time)
IF (profiler_on) init_time = init_time + (timer()-dot_product_time)
old_error = initial_residual
initial_residual=SQRT(initial_residual)
IF(parallel%boss.AND.verbose_on) THEN
!$ IF(OMP_GET_THREAD_NUM().EQ.0) THEN
WRITE(g_out,*)"Initial residual ",initial_residual
!$ ENDIF
ENDIF
IF(tl_use_cg .OR. tl_use_chebyshev .OR. tl_use_ppcg.OR. use_trilinos_kernels) THEN
! All 3 of these solvers use the CG kernels, and this is also called for Trilinos to get the initial residual
IF(use_fortran_kernels) THEN
CALL tea_leaf_kernel_init_cg_fortran(chunks(c)%field%x_min,&
chunks(c)%field%x_max, &
chunks(c)%field%y_min, &
chunks(c)%field%y_max, &
chunks(c)%field%z_min, &
chunks(c)%field%z_max, &
halo_exchange_depth, &
chunks(c)%field%vector_p, &
chunks(c)%field%vector_r, &
chunks(c)%field%vector_Mi, &
chunks(c)%field%vector_z, &
chunks(c)%field%vector_Kx, &
chunks(c)%field%vector_Ky, &
chunks(c)%field%vector_Kz, &
chunks(c)%field%tri_cp, &
chunks(c)%field%tri_bfp, &
rx, ry, rz, rro, tl_preconditioner_type)
ENDIF
! and globally sum rro
IF (profiler_on) dot_product_time=timer()
CALL tea_allsum(rro)
IF (profiler_on) profiler%dot_product= profiler%dot_product+ (timer() - dot_product_time)
IF (profiler_on) init_time = init_time + (timer()-dot_product_time)
! need to update p when using CG due to matrix/vector multiplication
fields=0
fields(FIELD_U) = 1
fields(FIELD_P) = 1
IF (profiler_on) halo_time=timer()
CALL update_halo(fields,1)
IF (profiler_on) init_time=init_time+(timer()-halo_time)
fields=0
fields(FIELD_P) = 1
ELSEIF (tl_use_jacobi) THEN
fields=0
fields(FIELD_U) = 1
ENDIF
IF (profiler_on) profiler%tea_init = profiler%tea_init + (timer() - init_time)
IF (profiler_on) solve_time = timer()
!===================================================================================
! We start the outer iteration of our solver
!===================================================================================
!============================================
! Homebrewed solvers
!============================================
IF(.NOT.use_trilinos_kernels) THEN
DO n=1,max_iters
iteration_time = timer()
!===================================================================================
! Switch for the number of CG iterations for Chebyshev and PPCG.
!===================================================================================
IF (ch_switch_check .EQV. .FALSE.) THEN
IF ((cheby_calc_steps .GT. 0)) THEN
! already started or already have good guesses for eigenvalues
ch_switch_check = .TRUE.
ELSE IF ((first .EQV. .FALSE.) .AND. tl_use_ppcg .AND. n .GT. 1) THEN
! If using PPCG, it can start almost immediately
ch_switch_check = .TRUE.
ELSE IF ((ABS(old_error) .LE. tl_ch_cg_epslim) .AND. (n .GE. tl_ch_cg_presteps)) THEN
! Error is less than set limit, and enough steps have passed to get a good eigenvalue guess
ch_switch_check = .TRUE.
ELSE
! keep doing CG (or jacobi)
ch_switch_check = .FALSE.
ENDIF
ENDIF
!===================================================================================
! If we use Chebyshev or PPCG we compute eigenvalue estimates
!===================================================================================
IF ((tl_use_chebyshev .OR. tl_use_ppcg) .AND. ch_switch_check) THEN
! on the first chebyshev steps, find the eigenvalues, coefficients,
! and expected number of iterations
IF (cheby_calc_steps .EQ. 0) THEN
! maximum number of iterations in chebyshev solver
max_cheby_iters = max_iters - n + 2
IF (first) THEN
! calculate eigenvalues
CALL tea_calc_eigenvalues(cg_alphas, cg_betas, eigmin, eigmax, &
max_iters, n-1, info)
first=.FALSE.
IF (info .NE. 0) CALL report_error('tea_leaf', 'Error in calculating eigenvalues')
eigmin = eigmin * 0.95_8
eigmax = eigmax * 1.05_8
ENDIF
IF (tl_use_chebyshev) THEN
! calculate chebyshev coefficients
CALL tea_calc_ch_coefs(ch_alphas, ch_betas, eigmin, eigmax, &
theta, max_cheby_iters)
! don't need to update p any more
fields = 0
fields(FIELD_U) = 1
ELSE IF (tl_use_ppcg) THEN
! To avoid some irritating bounds checking in ppcg inner iterations
max_cheby_iters = max_cheby_iters + halo_exchange_depth
! currently also calculate chebyshev coefficients
CALL tea_calc_ls_coefs(ch_alphas, ch_betas, eigmin, eigmax, &
theta, tl_ppcg_inner_steps)
! We have the eigenvalue estimates so turn on ppcg
tl_ppcg_active = .true.
ENDIF
cn = eigmax/eigmin
IF (parallel%boss) THEN
!$ IF(OMP_GET_THREAD_NUM().EQ.0) THEN
100 FORMAT("Eigen min",e14.6," Eigen max",e14.6," Condition number",f14.6," Error",e14.6)
WRITE(g_out,'(a,i3,a,e15.7)') "Switching after ",n," CG its, error ",rro
WRITE(g_out, 100) eigmin,eigmax,cn,error
WRITE(0,'(a,i3,a,e15.7)') "Switching after ",n," CG its, error ",rro
WRITE(0, 100) eigmin,eigmax,cn,error
!$ ENDIF
ENDIF
IF(tl_use_ppcg) THEN
!===================================================================================
! We want to reinitialise CG with PPCG applied now
!===================================================================================
! Step 1: we reinitialise z if we precondition or else we just use r
CALL tea_leaf_kernel_ppcg_init(chunks(c)%field%x_min, &
chunks(c)%field%x_max, &
chunks(c)%field%y_min, &
chunks(c)%field%y_max, &
chunks(c)%field%z_min, &
chunks(c)%field%z_max, &
halo_exchange_depth, &
chunks(c)%field%vector_p, &
chunks(c)%field%vector_r, &
chunks(c)%field%vector_z, &
rx, ry, rz, &
chunks(c)%field%vector_Kx, &
chunks(c)%field%vector_Ky, &
chunks(c)%field%vector_Kz, &
chunks(c)%field%vector_Mi, &
chunks(c)%field%tri_cp, &
chunks(c)%field%tri_bfp, &
rro, &
tl_preconditioner_type,2)
! Step 2: compute a new z from r or previous z
CALL tea_leaf_run_ppcg_inner_steps(ch_alphas, ch_betas, theta, &
rx, ry, rz, tl_ppcg_inner_steps, c, solve_time)
ppcg_inner_iters = ppcg_inner_iters + tl_ppcg_inner_steps
! Step 3: Recompute p after polynomial acceleration
CALL tea_leaf_kernel_ppcg_init(chunks(c)%field%x_min, &
chunks(c)%field%x_max, &
chunks(c)%field%y_min, &
chunks(c)%field%y_max, &
chunks(c)%field%z_min, &
chunks(c)%field%z_max, &
halo_exchange_depth, &
chunks(c)%field%vector_p, &
chunks(c)%field%vector_r, &
chunks(c)%field%vector_z, &
rx, ry, rz, &
chunks(c)%field%vector_Kx, &
chunks(c)%field%vector_Ky, &
chunks(c)%field%vector_Kz, &
chunks(c)%field%vector_Mi, &
chunks(c)%field%tri_cp, &
chunks(c)%field%tri_bfp, &
rro, &
tl_preconditioner_type,3)
! and globally sum rro
CALL tea_allsum(rro)
! need to update p when using CG due to matrix/vector multiplication
fields=0
fields(FIELD_U) = 1
fields(FIELD_P) = 1
IF (profiler_on) halo_time=timer()
CALL update_halo(fields,1)
IF (profiler_on) init_time=init_time+(timer()-halo_time)
fields=0
fields(FIELD_P) = 1
ENDIF
ENDIF
!===================================================================================
! Chebyshev Iteration
!===================================================================================
IF (tl_use_chebyshev) THEN
IF (cheby_calc_steps .EQ. 0) THEN
CALL tea_leaf_cheby_first_step(c, ch_alphas, ch_betas, fields, &
error, rx, ry, rz, theta, cn, max_cheby_iters, est_itc, solve_time)
cheby_calc_steps = 1
ELSE
IF(use_fortran_kernels) THEN
CALL tea_leaf_kernel_cheby_iterate(chunks(c)%field%x_min,&
chunks(c)%field%x_max, &
chunks(c)%field%y_min, &
chunks(c)%field%y_max, &
chunks(c)%field%z_min, &
chunks(c)%field%z_max, &
halo_exchange_depth, &
chunks(c)%field%u, &
chunks(c)%field%u0, &
chunks(c)%field%vector_p, &
chunks(c)%field%vector_r, &
chunks(c)%field%vector_Mi, &
chunks(c)%field%vector_w, &
chunks(c)%field%vector_z, &
chunks(c)%field%vector_Kx, &
chunks(c)%field%vector_Ky, &
chunks(c)%field%vector_Kz, &
chunks(c)%field%tri_cp, &
chunks(c)%field%tri_bfp, &
ch_alphas, ch_betas, max_cheby_iters, &
rx, ry, rz, cheby_calc_steps, tl_preconditioner_type)
ENDIF
! after estimated number of iterations has passed, calc resid.
! Leaving 10 iterations between each global reduction won't affect
! total time spent much if at all (number of steps spent in
! chebyshev is typically O(300+)) but will greatly reduce global
! synchronisations needed
IF ((n .GE. est_itc) .AND. (MOD(n, 10) .eq. 0)) THEN
IF(use_fortran_kernels) THEN
CALL tea_leaf_calc_2norm_kernel(chunks(c)%field%x_min,&
chunks(c)%field%x_max, &
chunks(c)%field%y_min, &
chunks(c)%field%y_max, &
chunks(c)%field%z_min, &
chunks(c)%field%z_max, &
halo_exchange_depth, &
chunks(c)%field%vector_r, &
error)
ENDIF
IF (profiler_on) dot_product_time=timer()
CALL tea_allsum(error)
IF (profiler_on) profiler%dot_product= profiler%dot_product+ (timer() - dot_product_time)
IF (profiler_on) solve_time = solve_time + (timer()-dot_product_time)
ENDIF
ENDIF
!===================================================================================
! PPCG iteration,
! First one needs an estimate of the eigenvalues from CG.
! We are essentially doing PPFCG(1) here where the F denotes that CG is flexible.
! This accounts for rounding error arising from the polynomial preconditioning
!===================================================================================
ELSE IF (tl_use_ppcg) THEN
IF(use_fortran_kernels) THEN
! w = Ap
! pw = p.w
CALL tea_leaf_kernel_solve_cg_fortran_calc_w(chunks(c)%field%x_min,&
chunks(c)%field%x_max, &
chunks(c)%field%y_min, &
chunks(c)%field%y_max, &
chunks(c)%field%z_min, &
chunks(c)%field%z_max, &
halo_exchange_depth, &
chunks(c)%field%vector_p, &
chunks(c)%field%vector_w, &
chunks(c)%field%vector_Kx, &
chunks(c)%field%vector_Ky, &
chunks(c)%field%vector_Kz, &
rx, ry, rz, pw)
ENDIF
! Now need to store r
IF(use_fortran_kernels) THEN
CALL tea_leaf_kernel_ppcg_store_r(chunks(c)%field%x_min,&
chunks(c)%field%x_max, &
chunks(c)%field%y_min, &
chunks(c)%field%y_max, &
chunks(c)%field%z_min, &
chunks(c)%field%z_max, &
halo_exchange_depth, &
chunks(c)%field%vector_r, &
chunks(c)%field%vector_rstore)
ENDIF
! Now compute r.z for alpha
IF(use_fortran_kernels) THEN
CALL tea_leaf_ppcg_calc_zrnorm_kernel(chunks(c)%field%x_min,&
chunks(c)%field%x_max, &
chunks(c)%field%y_min, &
chunks(c)%field%y_max, &
chunks(c)%field%z_min, &
chunks(c)%field%z_max, &
halo_exchange_depth, &
chunks(c)%field%vector_z, &
chunks(c)%field%vector_r, &
tl_preconditioner_type,rro)
ENDIF
IF (profiler_on) dot_product_time=timer()
CALL tea_allsum2(pw,rro)
IF (profiler_on) profiler%dot_product= profiler%dot_product+ (timer() - dot_product_time)
IF (profiler_on) solve_time = solve_time + (timer()-dot_product_time)
! alpha = z.r / (pw)
alpha = rro/pw
IF(use_fortran_kernels) THEN
CALL tea_leaf_kernel_solve_cg_fortran_calc_ur(chunks(c)%field%x_min,&
chunks(c)%field%x_max, &
chunks(c)%field%y_min, &
chunks(c)%field%y_max, &
chunks(c)%field%z_min, &
chunks(c)%field%z_max, &
halo_exchange_depth, &
chunks(c)%field%u, &
chunks(c)%field%vector_p, &
chunks(c)%field%vector_r, &
chunks(c)%field%vector_Mi, &
chunks(c)%field%vector_w, &
chunks(c)%field%vector_z, &
chunks(c)%field%tri_cp, &
chunks(c)%field%tri_bfp, &
chunks(c)%field%vector_Kx, &
chunks(c)%field%vector_Ky, &
chunks(c)%field%vector_Kz, &
rx, ry, rz, &
alpha, rrn, tl_preconditioner_type)
ENDIF
! not using rrn, so don't do a tea_allsum
CALL tea_leaf_run_ppcg_inner_steps(ch_alphas, ch_betas, theta, &
rx, ry, rz, tl_ppcg_inner_steps, c, solve_time)
ppcg_inner_iters = ppcg_inner_iters + tl_ppcg_inner_steps
! We use flexible CG, FCG(1)
IF(use_fortran_kernels) THEN
CALL tea_leaf_kernel_ppcg_calc_rrn(chunks(c)%field%x_min,&
chunks(c)%field%x_max, &
chunks(c)%field%y_min, &
chunks(c)%field%y_max, &
chunks(c)%field%z_min, &
chunks(c)%field%z_max, &
halo_exchange_depth, &
chunks(c)%field%vector_r, &
chunks(c)%field%vector_rstore, &
chunks(c)%field%vector_z, &
rrn)
ENDIF
IF (profiler_on) dot_product_time=timer()
CALL tea_allsum(rrn)
IF (profiler_on) profiler%dot_product= profiler%dot_product+ (timer() - dot_product_time)
IF (profiler_on) solve_time = solve_time + (timer()-dot_product_time)
! beta = (r.z)_{k+1} / (r.z)_{k}
beta = rrn/rro
IF(use_fortran_kernels) THEN
CALL tea_leaf_kernel_solve_cg_fortran_calc_p(chunks(c)%field%x_min,&
chunks(c)%field%x_max, &
chunks(c)%field%y_min, &
chunks(c)%field%y_max, &
chunks(c)%field%z_min, &
chunks(c)%field%z_max, &
halo_exchange_depth, &
chunks(c)%field%vector_p, &
chunks(c)%field%vector_r, &
chunks(c)%field%vector_z, &
beta, tl_preconditioner_type)
ENDIF
error = rrn
rro = rrn
ENDIF
cheby_calc_steps = cheby_calc_steps + 1
!===================================================================================
! Either: - CG iteration
! - Or if we choose Chebyshev or PPCG we need
! a few CG iterations to get an estimate of the eigenvalues.
!===================================================================================
ELSEIF(tl_use_cg .OR. tl_use_chebyshev .OR. tl_use_ppcg) THEN
fields(FIELD_P) = 1
cg_calc_steps = cg_calc_steps + 1
pw = 0.0_08
IF(use_fortran_kernels) THEN
CALL tea_leaf_kernel_solve_cg_fortran_calc_w(chunks(c)%field%x_min,&
chunks(c)%field%x_max, &
chunks(c)%field%y_min, &
chunks(c)%field%y_max, &
chunks(c)%field%z_min, &
chunks(c)%field%z_max, &
halo_exchange_depth, &
chunks(c)%field%vector_p, &
chunks(c)%field%vector_w, &
chunks(c)%field%vector_Kx, &
chunks(c)%field%vector_Ky, &
chunks(c)%field%vector_Kz, &
rx, ry, rz, pw)
ENDIF
IF (profiler_on) dot_product_time=timer()
CALL tea_allsum(pw)
IF (profiler_on) profiler%dot_product= profiler%dot_product+ (timer() - dot_product_time)
IF (profiler_on) solve_time = solve_time + (timer()-dot_product_time)
alpha = rro/pw
cg_alphas(n) = alpha
rrn = 0.0_8
IF(use_fortran_kernels) THEN
CALL tea_leaf_kernel_solve_cg_fortran_calc_ur(chunks(c)%field%x_min,&
chunks(c)%field%x_max, &
chunks(c)%field%y_min, &
chunks(c)%field%y_max, &
chunks(c)%field%z_min, &
chunks(c)%field%z_max, &
halo_exchange_depth, &
chunks(c)%field%u, &
chunks(c)%field%vector_p, &
chunks(c)%field%vector_r, &
chunks(c)%field%vector_Mi, &
chunks(c)%field%vector_w, &
chunks(c)%field%vector_z, &
chunks(c)%field%tri_cp, &
chunks(c)%field%tri_bfp, &
chunks(c)%field%vector_Kx, &
chunks(c)%field%vector_Ky, &
chunks(c)%field%vector_Kz, &
rx, ry, rz, &
alpha, rrn, tl_preconditioner_type)
ENDIF
IF (profiler_on) dot_product_time=timer()
CALL tea_allsum(rrn)
IF (profiler_on) profiler%dot_product= profiler%dot_product+ (timer() - dot_product_time)
IF (profiler_on) solve_time = solve_time + (timer()-dot_product_time)
beta = rrn/rro
cg_betas(n) = beta
IF(use_fortran_kernels) THEN
CALL tea_leaf_kernel_solve_cg_fortran_calc_p(chunks(c)%field%x_min,&
chunks(c)%field%x_max, &
chunks(c)%field%y_min, &
chunks(c)%field%y_max, &
chunks(c)%field%z_min, &
chunks(c)%field%z_max, &
halo_exchange_depth, &
chunks(c)%field%vector_p, &
chunks(c)%field%vector_r, &
chunks(c)%field%vector_z, &
beta, tl_preconditioner_type)
ENDIF
error = rrn
rro = rrn
!===================================================================================
! Jacobi iteration
!===================================================================================
ELSEIF(tl_use_jacobi) THEN
IF(use_fortran_kernels) THEN
CALL tea_leaf_kernel_jacobi_solve(chunks(c)%field%x_min,&
chunks(c)%field%x_max, &
chunks(c)%field%y_min, &
chunks(c)%field%y_max, &
chunks(c)%field%z_min, &
chunks(c)%field%z_max, &
halo_exchange_depth, &
rx, &
ry, &
rz, &
chunks(c)%field%vector_Kx, &
chunks(c)%field%vector_Ky, &
chunks(c)%field%vector_Kz, &
error, &
chunks(c)%field%u0, &
chunks(c)%field%u, &
chunks(c)%field%vector_r)
ENDIF
! error for jacobi is calculated recursively and is not very accurate,
! so do this every so often to see whether it has actually converged
! DS: This is a residual compute for Jacobi, not previously included, this
! gives a far more accurate picture of whether one has converged. This used
! to only do this every 50 iterations/
!IF (MOD(n, 50) .EQ. 0) THEN
IF (profiler_on) halo_time = timer()
CALL update_halo(fields,1)
IF (profiler_on) solve_time = solve_time + (timer()-halo_time)
IF(use_fortran_kernels) THEN
CALL tea_leaf_calc_residual(chunks(c)%field%x_min,&
chunks(c)%field%x_max, &
chunks(c)%field%y_min, &
chunks(c)%field%y_max, &
chunks(c)%field%z_min, &
chunks(c)%field%z_max, &
halo_exchange_depth, &
chunks(c)%field%u, &
chunks(c)%field%u0, &
chunks(c)%field%vector_r, &
chunks(c)%field%vector_Kx, &
chunks(c)%field%vector_Ky, &
chunks(c)%field%vector_Kz, &
rx, ry, rz)
CALL tea_leaf_calc_2norm_kernel(chunks(c)%field%x_min,&
chunks(c)%field%x_max, &
chunks(c)%field%y_min, &
chunks(c)%field%y_max, &
chunks(c)%field%z_min, &
chunks(c)%field%z_max, &
halo_exchange_depth, &
chunks(c)%field%vector_r, &
error)
ENDIF
!ENDIF
IF (profiler_on) dot_product_time=timer()
CALL tea_allsum(error)
IF (profiler_on) profiler%dot_product= profiler%dot_product+ (timer() - dot_product_time)
IF (profiler_on) solve_time = solve_time + (timer()-dot_product_time)
ENDIF
! updates u and possibly p
IF (profiler_on) halo_time = timer()
CALL update_halo(fields,1)
IF (profiler_on) solve_time = solve_time + (timer()-halo_time)
IF (profiler_on) THEN
IF (tl_use_chebyshev .AND. ch_switch_check) THEN
ch_time=ch_time+(timer()-iteration_time)
ELSE
cg_time=cg_time+(timer()-iteration_time)
ENDIF
ENDIF
!===================================================================================
! Our exit criterion:
!
! || r_{n} ||_{2} / || r_{0} ||_{2} \leq \epsilon
!
!===================================================================================
error=SQRT(error)
IF(parallel%boss.AND.verbose_on) THEN
!$ IF(OMP_GET_THREAD_NUM().EQ.0) THEN
WRITE(g_out,*)"Residual ",error
!$ ENDIF
ENDIF
IF (ABS(error) .LT. eps*initial_residual) EXIT
old_error = error
ENDDO
ENDIF
!======================================================
! Here we use trilinos to solve
!======================================================
IF (use_trilinos_kernels) THEN
itcount=0
petsc_mod=1
CALL trilinos_solve(grid%x_cells, &
grid%y_cells, &
grid%z_cells, &
chunks(c)%field%left, &
chunks(c)%field%right, &
chunks(c)%field%bottom, &
chunks(c)%field%top, &
chunks(c)%field%back, &
chunks(c)%field%front, &
1, &
grid%x_cells, &
1, &
grid%y_cells, &
1, &
grid%z_cells, &
itcount, &
rx, &
ry, &
rz, &
chunks(c)%field%vector_Kx, &
chunks(c)%field%vector_Ky, &
chunks(c)%field%vector_Kz, &
chunks(c)%field%u)
ENDIF
!======================================================
! End trilinos solve
!======================================================
IF (tl_check_result) THEN
fields = 0
fields(FIELD_U) = 1
IF (profiler_on) halo_time = timer()
CALL update_halo(fields,1)
IF (profiler_on) solve_time = solve_time + (timer()-halo_time)
IF(use_fortran_kernels) THEN
CALL tea_leaf_calc_residual(chunks(c)%field%x_min,&
chunks(c)%field%x_max, &
chunks(c)%field%y_min, &
chunks(c)%field%y_max, &
chunks(c)%field%z_min, &
chunks(c)%field%z_max, &
halo_exchange_depth, &
chunks(c)%field%u, &
chunks(c)%field%u0, &
chunks(c)%field%vector_r, &
chunks(c)%field%vector_Kx, &
chunks(c)%field%vector_Ky, &
chunks(c)%field%vector_Kz, &
rx, ry, rz)
CALL tea_leaf_calc_2norm_kernel(chunks(c)%field%x_min,&
chunks(c)%field%x_max, &
chunks(c)%field%y_min, &
chunks(c)%field%y_max, &
chunks(c)%field%z_min, &
chunks(c)%field%z_max, &
halo_exchange_depth, &
chunks(c)%field%vector_r, &
exact_error)
ENDIF
IF (profiler_on) dot_product_time=timer()
CALL tea_allsum(exact_error)
IF (profiler_on) profiler%dot_product= profiler%dot_product+ (timer() - dot_product_time)
IF (profiler_on) solve_time = solve_time + (timer()-dot_product_time)
exact_error = SQRT(exact_error)
ENDIF
IF (profiler_on) profiler%tea_solve = profiler%tea_solve + (timer() - solve_time)
IF(.NOT.use_trilinos_kernels) THEN
IF (parallel%boss) THEN
!$ IF(OMP_GET_THREAD_NUM().EQ.0) THEN
102 FORMAT('Conduction error ',e14.7)
WRITE(g_out,102) error/initial_residual
WRITE(0,102) error/initial_residual
IF (tl_check_result) THEN
101 FORMAT('EXACT error calculated as', e14.7)
WRITE(0, 101) exact_error/initial_residual
WRITE(g_out, 101) exact_error/initial_residual
ENDIF
WRITE(g_out,"('Iteration count ',i8)") n-1
WRITE(0,"('Iteration count ', i8)") n-1
IF(tl_use_ppcg) THEN
103 FORMAT('PPCG Iteration count', i8, ' (Total ',i8,')')
WRITE(g_out,103) ppcg_inner_iters, ppcg_inner_iters + n-1
WRITE(0,103) ppcg_inner_iters, ppcg_inner_iters + n-1
ENDIF
!$ ENDIF
ENDIF
ENDIF
! RESET
IF (profiler_on) reset_time=timer()
IF(use_fortran_kernels) THEN
CALL tea_leaf_kernel_finalise(chunks(c)%field%x_min, &
chunks(c)%field%x_max, &
chunks(c)%field%y_min, &
chunks(c)%field%y_max, &
chunks(c)%field%z_min, &
chunks(c)%field%z_max, &
halo_exchange_depth, &
chunks(c)%field%energy1, &
chunks(c)%field%density, &
chunks(c)%field%u)
ENDIF
fields=0
fields(FIELD_ENERGY1) = 1
IF (profiler_on) halo_time=timer()
CALL update_halo(fields,1)
IF (profiler_on) reset_time = reset_time + (timer()-halo_time)
IF (profiler_on) profiler%tea_reset = profiler%tea_reset + (timer() - reset_time)
ENDIF
ENDDO
IF (profiler_on .AND. parallel%boss) THEN
total_solve_time = (timer() - total_solve_time)
WRITE(0, "(a16,f16.10,a7,i7,a16,f16.10)") "Solve Time",total_solve_time,"Its",n,"Time Per It",total_solve_time/n
WRITE(g_out, "(a16,f16.10,a7,i7,a16,f16.10)") "Solve Time",total_solve_time,"Its",n,"Time Per It",total_solve_time/n
ENDIF
IF (profiler_on .AND. tl_use_chebyshev) THEN
CALL tea_sum(ch_time)
CALL tea_sum(cg_time)
IF (parallel%boss) THEN
cg_per_it = MERGE((cg_time/cg_calc_steps)/parallel%max_task, 0.0_8, cg_calc_steps .GT. 0)
ch_per_it = MERGE((ch_time/cheby_calc_steps)/parallel%max_task, 0.0_8, cheby_calc_steps .GT. 0)
WRITE(0, "(a3, a16, a7, a16, a7)") "", "Time", "Its", "Per it", "Ratio"
WRITE(0, "(a3, f16.10, i7, f16.10, f7.2)") &
"CG", cg_time + 0.0_8, cg_calc_steps, cg_per_it, 1.0_8
WRITE(0, "(a3, f16.10, i7, f16.10, f7.2)") "CH", ch_time + 0.0_8, cheby_calc_steps, &
ch_per_it, MERGE(ch_per_it/cg_per_it, 0.0_8, cheby_calc_steps .GT. 0)
WRITE(0, "('Chebyshev actually took ', i6, ' (' i6, ' off guess)')") &
cheby_calc_steps, cheby_calc_steps-est_itc
WRITE(g_out, "(a3, a16, a7, a16, a7)") "", "Time", "Its", "Per it", "Ratio"
WRITE(g_out, "(a3, f16.10, i7, f16.10, f7.2)") &
"CG", cg_time + 0.0_8, cg_calc_steps, cg_per_it, 1.0_8
WRITE(g_out, "(a3, f16.10, i7, f16.10, f7.2)") "CH", ch_time + 0.0_8, cheby_calc_steps, &
ch_per_it, MERGE(ch_per_it/cg_per_it, 0.0_8, cheby_calc_steps .GT. 0)
WRITE(g_out, "('Chebyshev actually took ', i6, ' (' i6, ' off guess)')") &
cheby_calc_steps, cheby_calc_steps-est_itc
ENDIF
ENDIF
END SUBROUTINE tea_leaf
SUBROUTINE tea_leaf_run_ppcg_inner_steps(ch_alphas, ch_betas, theta, &
rx, ry, rz, tl_ppcg_inner_steps, c, solve_time)
INTEGER :: fields(NUM_FIELDS)
INTEGER :: c, tl_ppcg_inner_steps, ppcg_cur_step
REAL(KIND=8) :: rx, ry, rz, theta
REAL(KIND=8) :: halo_time, timer, solve_time
REAL(KIND=8), DIMENSION(max_iters) :: ch_alphas, ch_betas
INTEGER :: x_min_bound, x_max_bound, y_min_bound, y_max_bound, z_min_bound, z_max_bound, inner_step, bounds_extra
fields = 0
fields(FIELD_U) = 1
IF (profiler_on) halo_time=timer()
CALL update_halo(fields,1)
IF (profiler_on) solve_time = solve_time + (timer() - halo_time)
IF(use_fortran_kernels) THEN
CALL tea_leaf_kernel_ppcg_init_sd(chunks(c)%field%x_min,&
chunks(c)%field%x_max, &
chunks(c)%field%y_min, &
chunks(c)%field%y_max, &
chunks(c)%field%z_min, &
chunks(c)%field%z_max, &
halo_exchange_depth, &
chunks(c)%field%vector_r, &
chunks(c)%field%vector_Kx, &
chunks(c)%field%vector_Ky, &
chunks(c)%field%vector_Kz, &
chunks(c)%field%vector_sd, &
chunks(c)%field%vector_z, &
chunks(c)%field%tri_cp, &
chunks(c)%field%tri_bfp, &
chunks(c)%field%vector_Mi, &
rx, ry, rz, theta, &
chunks(c)%field%vector_rtemp, &
chunks(c)%field%vector_utemp, &
tl_preconditioner_type)
ENDIF
! inner steps
DO ppcg_cur_step=1,tl_ppcg_inner_steps,halo_exchange_depth
fields = 0
fields(FIELD_SD) = 1
fields(FIELD_R) = 1
IF (profiler_on) halo_time = timer()
CALL update_halo(fields,halo_exchange_depth)
IF (profiler_on) solve_time = solve_time + (timer()-halo_time)