|
| 1 | +## PARAMETER INITIALIZATION |
| 2 | +function init_params(; ns=64, nt=100, kwargs...) |
| 3 | + L = 10.0 # physical domain length |
| 4 | + D = 1.0 # diffusion coefficient |
| 5 | + ds = L / ns # grid spacing |
| 6 | + dt = ds^2 / D / 8.2 # time step |
| 7 | + cs = range(start=ds / 2, stop=L - ds / 2, length=ns) .- 0.5 * L # vector of coord points |
| 8 | + nout = floor(Int, nt / 5) # plotting frequency |
| 9 | + return (; L, D, ns, nt, ds, dt, cs, nout, kwargs...) |
| 10 | +end |
| 11 | + |
| 12 | +function init_params_mpi(; dims, coords, ns=64, nt=100, kwargs...) |
| 13 | + L = 10.0 # physical domain length |
| 14 | + D = 1.0 # diffusion coefficient |
| 15 | + nx_g = dims[1] * (ns - 2) + 2 # global number of grid points along dim 1 |
| 16 | + ny_g = dims[2] * (ns - 2) + 2 # global number of grid points along dim 2 |
| 17 | + dx = L / nx_g # grid spacing |
| 18 | + dy = L / ny_g # grid spacing |
| 19 | + dt = min(dx, dy)^2 / D / 8.2 # time step |
| 20 | + x0 = coords[1] * (ns - 2) * dx # coords shift to get global coords on local process |
| 21 | + y0 = coords[2] * (ns - 2) * dy # coords shift to get global coords on local process |
| 22 | + xcs = LinRange(x0 + dx / 2, x0 + ns * dx - dx / 2, ns) .- 0.5 .* L # local vector of global coord points |
| 23 | + ycs = LinRange(y0 + dy / 2, y0 + ns * dx - dy / 2, ns) .- 0.5 .* L # local vector of global coord points |
| 24 | + return (; L, D, ns, nt, dx, dy, dt, xcs, ycs, kwargs...) |
| 25 | +end |
| 26 | + |
| 27 | +function init_params_gpu(; ns=64, nt=100, kwargs...) |
| 28 | + L = 10.0 # physical domain length |
| 29 | + D = 1.0 # diffusion coefficient |
| 30 | + ds = L / ns # grid spacing |
| 31 | + dt = ds^2 / D / 8.2 # time step |
| 32 | + cs = range(start=ds / 2, stop=L - ds / 2, length=ns) .- 0.5 * L # vector of coord points |
| 33 | + nout = floor(Int, nt / 5) # plotting frequency |
| 34 | + nthreads = 32, 8 # number of threads per block |
| 35 | + nblocks = cld.(ns, nthreads) # number of blocks |
| 36 | + return (; L, D, ns, nt, ds, dt, cs, nout, nthreads, nblocks, kwargs...) |
| 37 | +end |
| 38 | + |
| 39 | +function init_params_gpu_mpi(; dims, coords, ns=64, nt=100, kwargs...) |
| 40 | + L = 10.0 # physical domain length |
| 41 | + D = 1.0 # diffusion coefficient |
| 42 | + nx_g = dims[1] * (ns - 2) + 2 # global number of grid points along dim 1 |
| 43 | + ny_g = dims[2] * (ns - 2) + 2 # global number of grid points along dim 2 |
| 44 | + dx = L / nx_g # grid spacing |
| 45 | + dy = L / ny_g # grid spacing |
| 46 | + dt = min(dx, dy)^2 / D / 8.2 # time step |
| 47 | + x0 = coords[1] * (ns - 2) * dx # coords shift to get global coords on local process |
| 48 | + y0 = coords[2] * (ns - 2) * dy # coords shift to get global coords on local process |
| 49 | + xcs = LinRange(x0 + dx / 2, x0 + ns * dx - dx / 2, ns) .- 0.5 * L # local vector of global coord points |
| 50 | + ycs = LinRange(y0 + dy / 2, y0 + ns * dy - dy / 2, ns) .- 0.5 * L # local vector of global coord points |
| 51 | + nthreads = 32, 8 # number of threads per block |
| 52 | + nblocks = cld.(ns, nthreads) # number of blocks |
| 53 | + return (; L, D, ns, nt, dx, dy, dt, xcs, ycs, nthreads, nblocks, kwargs...) |
| 54 | +end |
| 55 | + |
| 56 | +## ARRAY INITIALIZATION |
| 57 | +function init_arrays_with_flux(params) |
| 58 | + (; cs, ns) = params |
| 59 | + C = @. exp(-cs^2 - (cs')^2) |
| 60 | + qx = zeros(ns - 1, ns - 2) |
| 61 | + qy = zeros(ns - 2, ns - 1) |
| 62 | + return C, qx, qy |
| 63 | +end |
| 64 | + |
| 65 | +function init_arrays(params) |
| 66 | + (; cs) = params |
| 67 | + C = @. exp(-cs^2 - (cs')^2) |
| 68 | + C2 = copy(C) |
| 69 | + return C, C2 |
| 70 | +end |
| 71 | + |
| 72 | +function init_arrays_mpi(params) |
| 73 | + (; xcs, ycs) = params |
| 74 | + C = @. exp(-xcs^2 - (ycs')^2) |
| 75 | + C2 = copy(C) |
| 76 | + return C, C2 |
| 77 | +end |
| 78 | + |
| 79 | +function init_arrays_gpu(params) |
| 80 | + (; cs) = params |
| 81 | + C = CuArray(@. exp(-cs^2 - (cs')^2)) |
| 82 | + C2 = copy(C) |
| 83 | + return C, C2 |
| 84 | +end |
| 85 | + |
| 86 | +function init_arrays_gpu_mpi(params) |
| 87 | + (; xcs, ycs) = params |
| 88 | + C = CuArray(@. exp(-xcs^2 - (ycs')^2)) |
| 89 | + C2 = copy(C) |
| 90 | + return C, C2 |
| 91 | +end |
| 92 | + |
| 93 | +## VISUALIZATION & PRINTING |
| 94 | +function maybe_init_visualization(params, C) |
| 95 | + if params.do_visualize |
| 96 | + fig = Figure(; size=(500, 400), fontsize=14) |
| 97 | + ax = Axis(fig[1, 1][1, 1]; aspect=DataAspect(), title="C") |
| 98 | + plt = heatmap!(ax, params.cs, params.cs, Array(C); colormap=:turbo, colorrange=(0, 1)) |
| 99 | + cb = Colorbar(fig[1, 1][1, 2], plt) |
| 100 | + display(fig) |
| 101 | + return fig, plt |
| 102 | + end |
| 103 | + return nothing, nothing |
| 104 | +end |
| 105 | + |
| 106 | +function maybe_update_visualization(params, fig, plt, C, it) |
| 107 | + if params.do_visualize && (it % params.nout == 0) |
| 108 | + plt[3] = Array(C) |
| 109 | + display(fig) |
| 110 | + end |
| 111 | + return nothing |
| 112 | +end |
| 113 | + |
| 114 | +function print_perf(params, t_toc) |
| 115 | + (; ns, nt) = params |
| 116 | + @printf("Time = %1.4e s, T_eff = %1.2f GB/s \n", t_toc, round((2 / 1e9 * ns^2 * sizeof(Float64)) / (t_toc / (nt - 10)), sigdigits=6)) |
| 117 | + return nothing |
| 118 | +end |
0 commit comments