-
Notifications
You must be signed in to change notification settings - Fork 1
/
tea_solve.f90
774 lines (662 loc) · 35 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
!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
!> @details Invokes the user specified kernel for the heat conduction
MODULE tea_leaf_module
USE report_module
USE data_module
USE tea_leaf_kernel_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,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 :: numit,numit_cg,NUMIT_CHEBY,itcount
INTEGER :: est_itc, cheby_calc_steps, max_cheby_iters, info, switch_step, petsc_mod
LOGICAL :: ch_switch_check
LOGICAL, SAVE :: first=.TRUE.
LOGICAL, PARAMETER :: reflective_boundary=.TRUE. ! Run the problem with reflective boundaries (PETSc has to do this)
LOGICAL, DIMENSION(4) :: zero_boundary
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
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
initial_residual=1.0_8 ! I still need to extract the initial residual from PETSc runs to get a matching relative error
IF(coefficient .NE. RECIP_CONDUCTIVITY .AND. coefficient .NE. conductivity) THEN
CALL report_error('tea_leaf', 'unknown coefficient option')
ENDIF
error = 1e10
petsc_mod=0
cheby_calc_steps = 0
cg_calc_steps = 0
total_solve_time = timer()
DO c=1,chunks_per_task
WHERE (chunks(c)%chunk_neighbours .EQ. EXTERNAL_FACE)
zero_boundary = .TRUE.
ELSE WHERE
zero_boundary = .FALSE.
END WHERE
IF(chunks(c)%task.EQ.parallel%task) THEN
! INIT
IF (profiler_on) halo_time=timer()
fields=0
fields(FIELD_ENERGY1) = 1
fields(FIELD_DENSITY) = 1
CALL update_halo(fields,2)
IF (profiler_on) profiler%halo_exchange = profiler%halo_exchange + (timer() - halo_time)
IF (profiler_on) init_time=timer()
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)
ENDIF
IF(tl_use_cg .OR. tl_use_chebyshev .OR. tl_use_ppcg .OR. use_PETSC_kernels) THEN
! All 3 of these solvers use the CG kernels and this is also called for PETSc 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, &
zero_boundary, &
reflective_boundary, &
chunks(c)%field%density, &
chunks(c)%field%energy1, &
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%vector_Kx, &
chunks(c)%field%vector_Ky, &
rx, ry, rro, coefficient, tl_preconditioner_on)
ENDIF
! 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) profiler%halo_exchange = profiler%halo_exchange + (timer() - halo_time)
halo_time=halo_time+(timer()-halo_time)
! 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) solve_time = solve_time + (timer()-dot_product_time)
initial_residual=SQRT(rro)
IF(parallel%boss.AND.verbose_on) THEN
!$ IF(OMP_GET_THREAD_NUM().EQ.0) THEN
WRITE(g_out,*)"Initial residual ",initial_residual
!$ ENDIF
ENDIF
ELSEIF(tl_use_jacobi) THEN
IF (use_fortran_kernels) THEN
CALL tea_leaf_kernel_init(chunks(c)%field%x_min, &
chunks(c)%field%x_max, &
chunks(c)%field%y_min, &
chunks(c)%field%y_max, &
zero_boundary, &
reflective_boundary, &
chunks(c)%field%celldx, &
chunks(c)%field%celldy, &
chunks(c)%field%volume, &
chunks(c)%field%density, &
chunks(c)%field%energy1, &
chunks(c)%field%u0, &
chunks(c)%field%u, &
chunks(c)%field%vector_r, &
chunks(c)%field%vector_w, &
chunks(c)%field%vector_z, &
chunks(c)%field%vector_Kx, &
chunks(c)%field%vector_Ky, &
coefficient)
ENDIF
fields=0
fields(FIELD_U) = 1
ENDIF
IF(use_fortran_kernels) THEN
CALL tea_leaf_kernel_cheby_copy_u(chunks(c)%field%x_min,&
chunks(c)%field%x_max, &
chunks(c)%field%y_min, &
chunks(c)%field%y_max, &
chunks(c)%field%u0, &
chunks(c)%field%u)
ENDIF
IF (profiler_on) profiler%tea_init = profiler%tea_init + (timer() - init_time)
IF (profiler_on) solve_time = timer()
IF((.NOT.use_PETSC_kernels).AND.(.NOT.use_trilinos_kernels)) THEN
DO itcount=1,max_iters
iteration_time = timer()
IF (tl_ch_cg_errswitch) THEN
! either the error has got below tolerance, or it's already going - minimum 20 steps to converge eigenvalues
ch_switch_check = (cheby_calc_steps .GT. 0) .OR. (error .LE. tl_ch_cg_epslim) .AND. (itcount .GE. 20)
ELSE
! enough steps have passed and error < 1, otherwise it's nowhere near converging on eigenvalues
ch_switch_check = (itcount .GE. tl_ch_cg_presteps) .AND. (error .le. 1.0_8)
ENDIF
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 - itcount + 2
rro = error
IF(first) THEN
! calculate eigenvalues
CALL tea_calc_eigenvalues(cg_alphas, cg_betas, eigmin, eigmax, &
max_iters, itcount-1, info)
first=.FALSE.
IF (info .NE. 0) CALL report_error('tea_leaf', 'Error in calculating eigenvalues')
eigmin = eigmin * 0.95
eigmax = eigmax * 1.05
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
! currently also calculate chebyshev coefficients
! TODO least squares
CALL tea_calc_ch_coefs(ch_alphas, ch_betas, eigmin, eigmax, &
theta, tl_ppcg_inner_steps)
ENDIF
cn = eigmax/eigmin
IF (parallel%boss) THEN
!$ IF(OMP_GET_THREAD_NUM().EQ.0) THEN
WRITE(g_out,'(a,i3,a,e15.7)') "Switching after ",itcount," CG its, error ",rro
WRITE(g_out,'(a,e14.6,a,e14.6,a,e14.6,a,e14.6)')"Eigen min ",eigmin," Eigen max ",eigmax," Condition number ",cn,&
" Error ",error
WRITE(0,'(a,i3,a,e15.7)') "Switching after ",itcount," CG its, error ",rro
WRITE(0,'(a,e14.6,a,e14.6,a,e14.6,a,e14.6)')"Eigen min ",eigmin," Eigen max ",eigmax," Condition number ",cn, &
" Error ",error
!$ ENDIF
ENDIF
ENDIF
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, theta, cn, max_cheby_iters, est_itc)
cheby_calc_steps = 1
switch_step = itcount
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%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, &
ch_alphas, ch_betas, max_cheby_iters, &
rx, ry, cheby_calc_steps, tl_preconditioner_on)
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 ((itcount .GE. est_itc) .AND. (MOD(itcount, 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%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
ELSE IF (tl_use_ppcg) THEN
IF (cheby_calc_steps .EQ. 0) THEN
cheby_calc_steps = 1
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%u, &
chunks(c)%field%u0, &
chunks(c)%field%vector_r, &
chunks(c)%field%vector_Kx, &
chunks(c)%field%vector_Ky, &
rx, ry)
ENDIF
IF (profiler_on) halo_time = timer()
! update p
CALL update_halo(fields,1)
IF (profiler_on) profiler%halo_exchange = profiler%halo_exchange + (timer() - halo_time)
IF (profiler_on) solve_time = solve_time + (timer()-halo_time)
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) solve_time = solve_time + (timer()-dot_product_time)
ENDIF
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%vector_p, &
chunks(c)%field%vector_w, &
chunks(c)%field%vector_Kx, &
chunks(c)%field%vector_Ky, &
rx, ry, 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
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%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, &
alpha, rrn, tl_preconditioner_on)
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, tl_ppcg_inner_steps, c)
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%vector_r, &
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 = 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%vector_p, &
chunks(c)%field%vector_r, &
chunks(c)%field%vector_z, &
beta, tl_preconditioner_on)
ENDIF
error = rrn
rro = rrn
ENDIF
cheby_calc_steps = cheby_calc_steps + 1
ELSEIF(tl_use_cg .OR. tl_use_chebyshev .OR. tl_use_ppcg) THEN
fields(FIELD_P) = 1
cg_calc_steps = cg_calc_steps + 1
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%vector_p, &
chunks(c)%field%vector_w, &
chunks(c)%field%vector_Kx, &
chunks(c)%field%vector_Ky, &
rx, ry, 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
IF(tl_use_chebyshev .OR. tl_use_ppcg) cg_alphas(itcount) = alpha
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%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, &
alpha, rrn, tl_preconditioner_on)
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
IF(tl_use_chebyshev .OR. tl_use_ppcg) cg_betas(itcount) = 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%vector_p, &
chunks(c)%field%vector_r, &
chunks(c)%field%vector_z, &
beta, tl_preconditioner_on)
ENDIF
error = rrn
rro = rrn
ELSEIF(tl_use_jacobi) THEN
IF(use_fortran_kernels) THEN
CALL tea_leaf_kernel_solve(chunks(c)%field%x_min,&
chunks(c)%field%x_max, &
chunks(c)%field%y_min, &
chunks(c)%field%y_max, &
rx, &
ry, &
chunks(c)%field%vector_Kx, &
chunks(c)%field%vector_Ky, &
error, &
chunks(c)%field%u0, &
chunks(c)%field%u, &
chunks(c)%field%vector_r)
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) profiler%halo_exchange = profiler%halo_exchange + (timer() - halo_time)
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
error=SQRT(error)
IF(parallel%boss.AND.verbose_on) THEN
!$ IF(OMP_GET_THREAD_NUM().EQ.0) THEN
WRITE(g_out,*)"Residual ",error,error/initial_residual
!$ ENDIF
ENDIF
IF (error.LT. eps*initial_residual) EXIT
ENDDO
ENDIF
IF(use_trilinos_kernels) THEN
petsc_mod=1
CALL trilinos_solve(grid%x_cells, &
grid%y_cells, &
chunks(c)%field%left, &
chunks(c)%field%right, &
chunks(c)%field%bottom, &
chunks(c)%field%top, &
1, &
grid%x_cells, &
1, &
grid%y_cells, &
itcount, &
rx, &
ry, &
chunks(c)%field%vector_Kx,&
chunks(c)%field%vector_Ky,&
chunks(c)%field%u )
ENDIF
IF (tl_check_result) THEN
fields=0
fields(FIELD_U) = 1
IF (profiler_on) halo_time = timer()
! update u
CALL update_halo(fields,1)
IF (profiler_on) profiler%halo_exchange = profiler%halo_exchange + (timer() - halo_time)
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%u, &
chunks(c)%field%u0, &
chunks(c)%field%vector_r, &
chunks(c)%field%vector_Kx, &
chunks(c)%field%vector_Ky, &
rx, ry)
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%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 (parallel%boss) THEN
!$ IF(OMP_GET_THREAD_NUM().EQ.0) THEN
WRITE(g_out,"('Conduction error ',e14.7)") error/initial_residual
WRITE(0,"('Conduction error ',e14.7)") error/initial_residual
IF (tl_check_result) THEN
WRITE(0,"('EXACT error calculated as', e14.7)") exact_error
WRITE(g_out,"('EXACT error calculated as', e14.7)") exact_error
ENDIF
WRITE(g_out,"('Iteration count ',i8)") itcount-1+petsc_mod
WRITE(0,"('Iteration count ', i8)") itcount-1+petsc_mod
IF(tl_use_ppcg) WRITE(g_out,"('Total Iteration count ',i8)") (itcount-1)*tl_ppcg_inner_steps
IF(tl_use_ppcg) WRITE(0,"('Total Iteration count ', i8)") (itcount-1)*tl_ppcg_inner_steps
!$ ENDIF
ENDIF
! RESET
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%energy1, &
chunks(c)%field%density, &
chunks(c)%field%u)
ENDIF
IF (profiler_on) profiler%tea_reset = profiler%tea_reset + (timer() - reset_time)
halo_time=timer()
fields=0
fields(FIELD_ENERGY1) = 1
CALL update_halo(fields,1)
IF (profiler_on) profiler%halo_exchange = profiler%halo_exchange + (timer() - halo_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",itcount,"Time Per It",total_solve_time/itcount
WRITE(g_out, "(a16,f16.10,a7,i7,a16,f16.10)") "Solve Time",total_solve_time,"Its",itcount,"Time Per It",total_solve_time/itcount
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, tl_ppcg_inner_steps, c)
INTEGER :: fields(NUM_FIELDS)
INTEGER :: c, tl_ppcg_inner_steps, ppcg_cur_step
REAL(KIND=8) :: rx, ry, theta
REAL(KIND=8), DIMENSION(max_iters) :: ch_alphas, ch_betas
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%vector_r, &
chunks(c)%field%vector_sd, &
theta)
ENDIF
fields = 0
fields(FIELD_SD) = 1
! inner steps
DO ppcg_cur_step=1,tl_ppcg_inner_steps
CALL update_halo(fields,1)
IF(use_fortran_kernels) THEN
CALL tea_leaf_kernel_ppcg_inner(chunks(c)%field%x_min,&
chunks(c)%field%x_max, &
chunks(c)%field%y_min, &
chunks(c)%field%y_max, &
ppcg_cur_step, &
ch_alphas, ch_betas, &
rx, ry, &
chunks(c)%field%u, &
chunks(c)%field%vector_r, &
chunks(c)%field%vector_Kx, &
chunks(c)%field%vector_Ky, &
chunks(c)%field%vector_sd)
ENDIF
ENDDO
fields = 0
fields(FIELD_P) = 1
END SUBROUTINE tea_leaF_run_ppcg_inner_steps
SUBROUTINE tea_leaf_cheby_first_step(c, ch_alphas, ch_betas, fields, &
error, rx, ry, theta, cn, max_cheby_iters, est_itc)
IMPLICIT NONE
integer :: c, est_itc, max_cheby_iters
integer, dimension(:) :: fields
REAL(KIND=8) :: it_alpha, cn, gamm, bb, error, rx, ry, theta
REAL(KIND=8), DIMENSION(:) :: ch_alphas, ch_betas
! calculate 2 norm of u0
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%u0, &
bb)
ENDIF
!IF (profiler_on) dot_product_time=timer()
CALL tea_allsum(bb)
!IF (profiler_on) profiler%dot_product= profiler%dot_product+ (timer() - dot_product_time)
!IF (profiler_on) solve_time = solve_time + (timer()-dot_product_time)
! initialise 'p' array
IF(use_fortran_kernels) THEN
CALL tea_leaf_kernel_cheby_init(chunks(c)%field%x_min,&
chunks(c)%field%x_max, &
chunks(c)%field%y_min, &
chunks(c)%field%y_max, &
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, &
ch_alphas, ch_betas, max_cheby_iters, &
rx, ry, theta, error, tl_preconditioner_on)
ENDIF
CALL update_halo(fields,1)
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%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, &
ch_alphas, ch_betas, max_cheby_iters, &
rx, ry, 1, tl_preconditioner_on)
ENDIF
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%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)
it_alpha = EPSILON(1.0_8)*bb/(4.0_8*error)
gamm = (SQRT(cn) - 1.0_8)/(SQRT(cn) + 1.0_8)
est_itc = NINT(LOG(it_alpha)/(2.0_8*LOG(gamm)))
IF (parallel%boss) THEN
WRITE(g_out,'(a11)')"est itc"
WRITE(g_out,'(11i11)')est_itc
WRITE(0,'(a11)')"est itc"
WRITE(0,'(11i11)')est_itc
ENDIF
END SUBROUTINE tea_leaf_cheby_first_step
END MODULE tea_leaf_module