Skip to content

Commit 0c07882

Browse files
committed
Convert Fokker-Planck collision operator to 'anysv' region
Currently only supports a single ion species - `is = 1` is hard-coded in the outer loop.
1 parent 928353c commit 0c07882

9 files changed

Lines changed: 363 additions & 354 deletions

docs/src/developing.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -231,8 +231,8 @@ that share an anysv subblock are all part of the `comm_anysv_subblock[]`
231231
communicator (which is a subset of the processes in the full block, whose
232232
communicator is `comm_block[]`).
233233

234-
See also notes on debugging the 'anyv' parallelisation: [Collision operator and
235-
'anysv' region](@ref).
234+
See also notes on debugging the 'anysv' parallelisation: [Collision operator
235+
and 'anysv' region](@ref).
236236

237237
## Bounds checking
238238

moment_kinetics/src/communication.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -973,10 +973,10 @@ debugging routines need to be updated.
973973
end
974974
end
975975

976-
# Also check 'anyv' arrays, as these are synchronized by this call.
976+
# Also check 'anysv' arrays, as these are synchronized by this call.
977977
# `missing` passed as the call_site argument here indicates that the check of
978978
# call_site has already been done.
979-
_anyv_subblock_synchronize(missing)
979+
_anysv_subblock_synchronize(missing)
980980

981981
MPI.Barrier(comm_block[])
982982
end

moment_kinetics/src/fokker_planck.jl

Lines changed: 96 additions & 93 deletions
Large diffs are not rendered by default.

moment_kinetics/src/fokker_planck_calculus.jl

Lines changed: 166 additions & 160 deletions
Large diffs are not rendered by default.

moment_kinetics/src/nonlinear_solvers.jl

Lines changed: 58 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ struct nl_solver_info{TH,TV,Tcsg,Tlig,Tprecon,Tpretype}
6969
precon_lowerz_vcut_inds::Vector{mk_int}
7070
precon_upperz_vcut_inds::Vector{mk_int}
7171
serial_solve::Bool
72-
anyv_region::Bool
72+
anysv_region::Bool
7373
max_nonlinear_iterations_this_step::Base.RefValue{mk_int}
7474
max_linear_iterations_this_step::Base.RefValue{mk_int}
7575
total_its_soft_limit::mk_int
@@ -89,7 +89,7 @@ for example a preconditioner object for each point in that outer loop.
8989
"""
9090
function setup_nonlinear_solve(active, input_dict, coords, outer_coords=();
9191
section_name="nonlinear_solver", default_rtol=1.0e-5,
92-
default_atol=1.0e-12, serial_solve=false, anyv_region=false,
92+
default_atol=1.0e-12, serial_solve=false, anysv_region=false,
9393
electron_p_pdf_solve=false,
9494
preconditioner_type=Val(:none), warn_unexpected=false)
9595
nl_solver_section = set_defaults_and_check_section!(
@@ -153,28 +153,30 @@ function setup_nonlinear_solve(active, input_dict, coords, outer_coords=();
153153
V = (V_ppar, V_pdf)
154154

155155
n_vcut_inds = prod(outer_coord_sizes)
156-
elseif anyv_region
157-
H = allocate_shared_float(linear_restart + 1, linear_restart; comm=comm_anyv_subblock[])
158-
c = allocate_shared_float(linear_restart + 1; comm=comm_anyv_subblock[])
159-
s = allocate_shared_float(linear_restart + 1; comm=comm_anyv_subblock[])
160-
g = allocate_shared_float(linear_restart + 1; comm=comm_anyv_subblock[])
161-
V = allocate_shared_float(reverse(coord_sizes)..., linear_restart+1; comm=comm_anyv_subblock[])
156+
elseif anysv_region
157+
H = allocate_shared_float(linear_restart + 1, linear_restart; comm=comm_anysv_subblock[])
158+
c = allocate_shared_float(linear_restart + 1; comm=comm_anysv_subblock[])
159+
s = allocate_shared_float(linear_restart + 1; comm=comm_anysv_subblock[])
160+
g = allocate_shared_float(linear_restart + 1; comm=comm_anysv_subblock[])
161+
V = allocate_shared_float(reverse(coord_sizes)..., linear_restart+1; comm=comm_anysv_subblock[])
162162
# Arrays below appear to need to be initialised to zero on setup.
163-
# This is inconvenient for anyv communicators because we need to switch
164-
# now into the special "anyv" region for this assignment,
163+
# This is inconvenient for anysv communicators because we need to switch
164+
# now into the special "anysv" region for this assignment,
165165
# to make sure that all instances of memory are initialised to zero.
166-
@begin_s_r_z_anyv_region()
167-
@begin_anyv_region()
168-
@anyv_serial_region begin
169-
H .= 0.0
170-
c .= 0.0
171-
s .= 0.0
172-
g .= 0.0
173-
V .= 0.0
166+
@begin_r_z_anysv_region()
167+
@begin_anysv_region()
168+
if anysv_subblock_rank[] 0
169+
@anysv_serial_region begin
170+
H .= 0.0
171+
c .= 0.0
172+
s .= 0.0
173+
g .= 0.0
174+
V .= 0.0
175+
end
174176
end
175-
# switch out of anyv region to avoid errors on
177+
# switch out of anysv region to avoid errors on
176178
# the next region call in initialisation, which
177-
# will not be an "anyv" call.
179+
# will not be an "anysv" call.
178180
@begin_serial_region()
179181
else
180182
H = allocate_shared_float(linear_restart + 1, linear_restart)
@@ -305,7 +307,7 @@ function setup_nonlinear_solve(active, input_dict, coords, outer_coords=();
305307
Ref(0), Ref(0), Ref(0),
306308
Ref(nl_solver_input.preconditioner_update_interval),
307309
Ref(mk_float(0.0)), zeros(mk_int, n_vcut_inds),
308-
zeros(mk_int, n_vcut_inds), serial_solve, anyv_region, Ref(0), Ref(0),
310+
zeros(mk_int, n_vcut_inds), serial_solve, anysv_region, Ref(0), Ref(0),
309311
nl_solver_input.total_its_soft_limit, preconditioner_type,
310312
nl_solver_input.preconditioner_update_interval, preconditioners)
311313
end
@@ -482,7 +484,7 @@ old_precon_iterations = nl_solver_params.precon_iterations[]
482484
V=nl_solver_params.V, rhs_delta=rhs_delta,
483485
initial_guess=nl_solver_params.linear_initial_guess,
484486
serial_solve=nl_solver_params.serial_solve,
485-
anyv_region=nl_solver_params.anyv_region,
487+
anysv_region=nl_solver_params.anysv_region,
486488
initial_delta_x_is_zero=true)
487489
linear_counter += linear_its
488490

@@ -643,27 +645,27 @@ end
643645
residual::AbstractArray{mk_float, 2},
644646
coords, rtol, atol, x::AbstractArray{mk_float, 2}) = begin
645647
# no distributed memory paralleism required when solving only in (vperp, vpa)
646-
# assumed called inside @begin_s_r_z_anyv_region()
648+
# assumed called inside @begin_r_z_anysv_region()
647649
pdf_residual = residual
648650
x_pdf = x
649651
vperp = coords.vperp
650652
vpa = coords.vpa
651653

652-
@begin_anyv_vperp_vpa_region()
654+
@begin_anysv_vperp_vpa_region()
653655
pdf_norm_square = 0.0
654656
@loop_vperp_vpa ivperp ivpa begin
655657
pdf_norm_square += (pdf_residual[ivpa,ivperp] / (rtol * abs(x_pdf[ivpa,ivperp]) + atol))^2
656658
end
657-
@_anyv_subblock_synchronize()
659+
@_anysv_subblock_synchronize()
658660
global_norm = Ref(pdf_norm_square)
659-
@timeit_debug global_timer "MPI.Reduce! comm_block" MPI.Reduce!(global_norm, +, comm_anyv_subblock[]) # global_norm is the norm_square for the block
661+
@timeit_debug global_timer "MPI.Reduce! comm_block" MPI.Reduce!(global_norm, +, comm_anysv_subblock[]) # global_norm is the norm_square for the block
660662

661-
if anyv_subblock_rank[] == 0
663+
if anysv_subblock_rank[] == 0
662664
global_norm[] = sqrt(global_norm[] / (vperp.n_global * vpa.n_global))
663665
end
664-
@_anyv_subblock_synchronize()
666+
@_anysv_subblock_synchronize()
665667

666-
@timeit_debug global_timer "MPI.Bcast! comm_block" MPI.Bcast!(global_norm, comm_anyv_subblock[]; root=0)
668+
@timeit_debug global_timer "MPI.Bcast! comm_block" MPI.Bcast!(global_norm, comm_anysv_subblock[]; root=0)
667669

668670
return global_norm[]
669671
end
@@ -838,16 +840,16 @@ end
838840
vperp = coords.vperp
839841
vpa = coords.vpa
840842

841-
@begin_anyv_vperp_vpa_region()
843+
@begin_anysv_vperp_vpa_region()
842844

843845
pdf_dot = 0.0
844846
@loop_vperp_vpa ivperp ivpa begin
845847
pdf_dot += v_pdf[ivpa,ivperp] * w_pdf[ivpa,ivperp] / (rtol * abs(x_pdf[ivpa,ivperp]) + atol)^2
846848
end
847-
@_anyv_subblock_synchronize()
849+
@_anysv_subblock_synchronize()
848850
global_dot = Ref(pdf_dot)
849-
@timeit_debug global_timer "MPI.Reduce! comm_anyv_subblock" MPI.Reduce!(global_dot, +, comm_anyv_subblock[]) # global_dot is the dot for the block
850-
if anyv_subblock_rank[] == 0
851+
@timeit_debug global_timer "MPI.Reduce! comm_anysv_subblock" MPI.Reduce!(global_dot, +, comm_anysv_subblock[]) # global_dot is the dot for the block
852+
if anysv_subblock_rank[] == 0
851853
global_dot[] = global_dot[] / (vperp.n_global * vpa.n_global)
852854
end
853855
return global_dot[]
@@ -1070,12 +1072,12 @@ end
10701072

10711073
result_pdf = result
10721074

1073-
@begin_anyv_vperp_vpa_region()
1075+
@begin_anysv_vperp_vpa_region()
10741076

10751077
@loop_vperp_vpa ivperp ivpa begin
10761078
result_pdf[ivpa,ivperp] = func()
10771079
end
1078-
@_anyv_subblock_synchronize()
1080+
@_anysv_subblock_synchronize()
10791081
return nothing
10801082
end
10811083
@timeit_debug global_timer parallel_map(
@@ -1085,12 +1087,12 @@ end
10851087
result_pdf = result
10861088
x1_pdf = x1
10871089

1088-
@begin_anyv_vperp_vpa_region()
1090+
@begin_anysv_vperp_vpa_region()
10891091

10901092
@loop_vperp_vpa ivperp ivpa begin
10911093
result_pdf[ivpa,ivperp] = func(x1_pdf[ivpa,ivperp])
10921094
end
1093-
@_anyv_subblock_synchronize()
1095+
@_anysv_subblock_synchronize()
10941096
return nothing
10951097
end
10961098
@timeit_debug global_timer parallel_map(
@@ -1100,7 +1102,7 @@ end
11001102
result_pdf = result
11011103
x1_pdf = x1
11021104

1103-
@begin_anyv_vperp_vpa_region()
1105+
@begin_anysv_vperp_vpa_region()
11041106

11051107
if isa(x2, AbstractArray)
11061108
x2_pdf = x2
@@ -1113,7 +1115,7 @@ end
11131115
result_pdf[ivpa,ivperp] = func(x1_pdf[ivpa,ivperp], x2)
11141116
end
11151117
end
1116-
@_anyv_subblock_synchronize()
1118+
@_anysv_subblock_synchronize()
11171119
return nothing
11181120
end
11191121
@timeit_debug global_timer parallel_map(
@@ -1123,7 +1125,7 @@ end
11231125
result_pdf = result
11241126
x1_pdf = x1
11251127
x2_pdf = x2
1126-
@begin_anyv_vperp_vpa_region()
1128+
@begin_anysv_vperp_vpa_region()
11271129

11281130
if isa(x3, AbstractArray)
11291131
x3_pdf = x3
@@ -1136,7 +1138,7 @@ end
11361138
result_pdf[ivpa,ivperp] = func(x1_pdf[ivpa,ivperp], x2_pdf[ivpa,ivperp], x3)
11371139
end
11381140
end
1139-
@_anyv_subblock_synchronize()
1141+
@_anysv_subblock_synchronize()
11401142
return nothing
11411143
end
11421144

@@ -1349,7 +1351,7 @@ end
13491351

13501352
ny = length(y)
13511353

1352-
@begin_anyv_vperp_vpa_region()
1354+
@begin_anysv_vperp_vpa_region()
13531355

13541356
@loop_vperp_vpa ivperp ivpa begin
13551357
for iy 1:ny
@@ -1424,7 +1426,7 @@ MGS-GMRES' in Zou (2023) [https://doi.org/10.1016/j.amc.2023.127869].
14241426
x, residual_func!, residual0, delta_x, v, w, solver_type::Val,
14251427
norm_params; coords, rtol, atol, restart, max_restarts,
14261428
left_preconditioner, right_preconditioner, H, c, s, g, V,
1427-
rhs_delta, initial_guess, serial_solve, anyv_region,
1429+
rhs_delta, initial_guess, serial_solve, anysv_region,
14281430
initial_delta_x_is_zero) = begin
14291431
# Solve (approximately?):
14301432
# J δx = residual0
@@ -1471,9 +1473,9 @@ MGS-GMRES' in Zou (2023) [https://doi.org/10.1016/j.amc.2023.127869].
14711473
parallel_map(solver_type, (w,beta) -> w/beta, select_from_V(V, 1), w, beta)
14721474
if serial_solve
14731475
g[1] = beta
1474-
elseif anyv_region
1475-
@begin_anyv_region()
1476-
@anyv_serial_region begin
1476+
elseif anysv_region
1477+
@begin_anysv_region()
1478+
@anysv_serial_region begin
14771479
g[1] = beta
14781480
end
14791481
else
@@ -1508,9 +1510,9 @@ MGS-GMRES' in Zou (2023) [https://doi.org/10.1016/j.amc.2023.127869].
15081510
w_dot_Vj = distributed_dot(solver_type, w, v, norm_params...)
15091511
if serial_solve
15101512
H[j,i] = w_dot_Vj
1511-
elseif anyv_region
1512-
@begin_anyv_region()
1513-
@anyv_serial_region begin
1513+
elseif anysv_region
1514+
@begin_anysv_region()
1515+
@anysv_serial_region begin
15141516
H[j,i] = w_dot_Vj
15151517
end
15161518
else
@@ -1524,9 +1526,9 @@ MGS-GMRES' in Zou (2023) [https://doi.org/10.1016/j.amc.2023.127869].
15241526
norm_w = distributed_norm(solver_type, w, norm_params...)
15251527
if serial_solve
15261528
H[i+1,i] = norm_w
1527-
elseif anyv_region
1528-
@begin_anyv_region()
1529-
@anyv_serial_region begin
1529+
elseif anysv_region
1530+
@begin_anysv_region()
1531+
@anysv_serial_region begin
15301532
H[i+1,i] = norm_w
15311533
end
15321534
else
@@ -1550,9 +1552,9 @@ MGS-GMRES' in Zou (2023) [https://doi.org/10.1016/j.amc.2023.127869].
15501552
H[i+1,i] = 0
15511553
g[i+1] = -s[i] * g[i]
15521554
g[i] = c[i] * g[i]
1553-
elseif anyv_region
1554-
@begin_anyv_region()
1555-
@anyv_serial_region begin
1555+
elseif anysv_region
1556+
@begin_anysv_region()
1557+
@anysv_serial_region begin
15561558
for j 1:i-1
15571559
gamma = c[j] * H[j,i] + s[j] * H[j+1,i]
15581560
H[j+1,i] = -s[j] * H[j,i] + c[j] * H[j+1,i]
@@ -1566,7 +1568,7 @@ MGS-GMRES' in Zou (2023) [https://doi.org/10.1016/j.amc.2023.127869].
15661568
g[i+1] = -s[i] * g[i]
15671569
g[i] = c[i] * g[i]
15681570
end
1569-
@_anyv_subblock_synchronize()
1571+
@_anysv_subblock_synchronize()
15701572
else
15711573
@begin_serial_region()
15721574
@serial_region begin

0 commit comments

Comments
 (0)