diff --git a/slide-notebooks/deploy_notebooks.jl b/slide-notebooks/deploy_notebooks.jl index 4f4b0e7a..a2ce2a4e 100644 --- a/slide-notebooks/deploy_notebooks.jl +++ b/slide-notebooks/deploy_notebooks.jl @@ -1,6 +1,6 @@ using Literate ## include Literate scripts starting with following 3 letters in the deploy -incl = "l4_" +incl = "l5_" ## Set `sol=true` to produce output with solutions contained and hints stripts. Otherwise the other way around. sol = true ## diff --git a/slide-notebooks/notebooks/l5_1-cpu-parallel.ipynb b/slide-notebooks/notebooks/l5_1-cpu-parallel.ipynb index 9111d5f6..c04be286 100644 --- a/slide-notebooks/notebooks/l5_1-cpu-parallel.ipynb +++ b/slide-notebooks/notebooks/l5_1-cpu-parallel.ipynb @@ -465,10 +465,10 @@ "outputs": [], "cell_type": "code", "source": [ - "t_toc = ...\n", - "A_eff = ... # Effective main memory access per iteration [GB]\n", - "t_it = ... # Execution time per iteration [s]\n", - "T_eff = A_eff/t_it # Effective memory throughput [GB/s]" + "t_toc = Base.time() - t_tic\n", + "A_eff = (3*2)/1e9*nx*ny*sizeof(Float64) # Effective main memory access per iteration [GB]\n", + "t_it = t_toc/niter # Execution time per iteration [s]\n", + "T_eff = A_eff/t_it # Effective memory throughput [GB/s]" ], "metadata": {}, "execution_count": null @@ -480,7 +480,7 @@ "- Round `T_eff` to the 3rd significant digit.\n", "\n", "```julia\n", - "@printf(\"Time = %1.3f sec, ... \\n\", t_toc, ...)\n", + "@printf(\"Time = %1.3f sec, T_eff = %1.2f GB/s (niter = %d)\\n\", t_toc, round(T_eff, sigdigits=3), niter)\n", "```" ], "metadata": { @@ -508,8 +508,10 @@ "outputs": [], "cell_type": "code", "source": [ - "function Pf_diffusion_2D(;??)\n", + "function Pf_diffusion_2D(;do_check=false)\n", + " if do_check && (iter%ncheck == 0)\n", " ...\n", + " end\n", " return\n", "end" ], @@ -672,19 +674,19 @@ "outputs": [], "cell_type": "code", "source": [ - "for iy=??\n", - " for ix=??\n", - " qDx[??] -= (qDx[??] + k_ηf_dx* ?? )*_1_θ_dτ\n", + "for iy=1:ny\n", + " for ix=1:nx-1\n", + " qDx[ix+1,iy] -= (qDx[ix+1,iy] + k_ηf_dx*(Pf[ix+1,iy]-Pf[ix,iy]))*_1_θ_dτ\n", " end\n", "end\n", - "for iy=??\n", - " for ix=??\n", - " qDy[??] -= (qDy[??] + k_ηf_dy* ?? )*_1_θ_dτ\n", + "for iy=1:ny-1\n", + " for ix=1:nx\n", + " qDy[ix,iy+1] -= (qDy[ix,iy+1] + k_ηf_dy*(Pf[ix,iy+1]-Pf[ix,iy]))*_1_θ_dτ\n", " end\n", "end\n", - "for iy=??\n", - " for ix=??\n", - " Pf[??] -= ??\n", + "for iy=1:ny\n", + " for ix=1:nx\n", + " Pf[ix,iy] -= ((qDx[ix+1,iy]-qDx[ix,iy])*_dx + (qDy[ix,iy+1]-qDy[ix,iy])*_dy)*_β_dτ\n", " end\n", "end" ], @@ -709,8 +711,8 @@ "outputs": [], "cell_type": "code", "source": [ - "macro d_xa(A) esc(:( $A[??]-$A[??] )) end\n", - "macro d_ya(A) esc(:( $A[??]-$A[??] )) end" + "macro d_xa(A) esc(:( $A[ix+1,iy]-$A[ix,iy] )) end\n", + "macro d_ya(A) esc(:( $A[ix,iy+1]-$A[ix,iy] )) end" ], "metadata": {}, "execution_count": null @@ -726,19 +728,19 @@ "outputs": [], "cell_type": "code", "source": [ - "for iy=??\n", - " for ix=??\n", - " qDx[??] -= (qDx[??] + k_ηf_dx* ?? )*_1_θ_dτ\n", + "for iy=1:ny\n", + " for ix=1:nx-1\n", + " qDx[ix+1,iy] -= (qDx[ix+1,iy] + k_ηf_dx*@d_xa(Pf))*_1_θ_dτ\n", " end\n", "end\n", - "for iy=??\n", - " for ix=??\n", - " qDy[??] -= (qDy[??] + k_ηf_dy* ?? )*_1_θ_dτ\n", + "for iy=1:ny-1\n", + " for ix=1:nx\n", + " qDy[ix,iy+1] -= (qDy[ix,iy+1] + k_ηf_dy*@d_ya(Pf))*_1_θ_dτ\n", " end\n", "end\n", - "for iy=??\n", - " for ix=??\n", - " Pf[??] -= ??\n", + "for iy=1:ny\n", + " for ix=1:nx\n", + " Pf[ix,iy] -= (@d_xa(qDx)*_dx + @d_ya(qDy)*_dy)*_β_dτ\n", " end\n", "end" ], @@ -803,15 +805,28 @@ "outputs": [], "cell_type": "code", "source": [ - "function compute_flux!(...)\n", + "function compute_flux!(qDx,qDy,Pf,k_ηf_dx,k_ηf_dy,_1_θ_dτ)\n", " nx,ny=size(Pf)\n", - " ...\n", + " for iy=1:ny,\n", + " for ix=1:nx-1\n", + " qDx[ix+1,iy] -= (qDx[ix+1,iy] + k_ηf_dx*@d_xa(Pf))*_1_θ_dτ\n", + " end\n", + " end\n", + " for iy=1:ny-1\n", + " for ix=1:nx\n", + " qDy[ix,iy+1] -= (qDy[ix,iy+1] + k_ηf_dy*@d_ya(Pf))*_1_θ_dτ\n", + " end\n", + " end\n", " return nothing\n", "end\n", "\n", - "function update_Pf!(Pf,...)\n", + "function update_Pf!(Pf,qDx,qDy,_dx,_dy,_β_dτ)\n", " nx,ny=size(Pf)\n", - " ...\n", + " for iy=1:ny\n", + " for ix=1:nx\n", + " Pf[ix,iy] -= (@d_xa(qDx)*_dx + @d_ya(qDy)*_dy)*_β_dτ\n", + " end\n", + " end\n", " return nothing\n", "end" ], @@ -891,9 +906,9 @@ "outputs": [], "cell_type": "code", "source": [ - "function compute!(Pf,qDx,qDy, ???)\n", - " compute_flux!(...)\n", - " update_Pf!(...)\n", + "function compute!(Pf,qDx,qDy,k_ηf_dx,k_ηf_dy,_1_θ_dτ,_dx,_dy,_β_dτ)\n", + " compute_flux!(qDx,qDy,Pf,k_ηf_dx,k_ηf_dy,_1_θ_dτ)\n", + " update_Pf!(Pf,qDx,qDy,_dx,_dy,_β_dτ)\n", " return nothing\n", "end" ], @@ -911,8 +926,8 @@ "outputs": [], "cell_type": "code", "source": [ - "t_toc = @belapsed compute!($Pf,$qDx,$qDy,???)\n", - "niter = ???" + "t_toc = @belapsed compute!($Pf,$qDx,$qDy,$k_ηf_dx,$k_ηf_dy,$_1_θ_dτ,$_dx,$_dy,$_β_dτ)\n", + "niter = 1" ], "metadata": {}, "execution_count": null @@ -1024,11 +1039,11 @@ "file_extension": ".jl", "mimetype": "application/julia", "name": "julia", - "version": "1.10.5" + "version": "1.11.0" }, "kernelspec": { - "name": "julia-1.10", - "display_name": "Julia 1.10.5", + "name": "julia-1.11", + "display_name": "Julia 1.11.0", "language": "julia" } }, diff --git a/slide-notebooks/notebooks/l5_2-unit-test.ipynb b/slide-notebooks/notebooks/l5_2-unit-test.ipynb index 58e7178a..66dc7a82 100644 --- a/slide-notebooks/notebooks/l5_2-unit-test.ipynb +++ b/slide-notebooks/notebooks/l5_2-unit-test.ipynb @@ -314,11 +314,11 @@ "file_extension": ".jl", "mimetype": "application/julia", "name": "julia", - "version": "1.10.5" + "version": "1.11.0" }, "kernelspec": { - "name": "julia-1.10", - "display_name": "Julia 1.10.5", + "name": "julia-1.11", + "display_name": "Julia 1.11.0", "language": "julia" } }, diff --git a/website/_literate/l5_1-cpu-parallel_web.jl b/website/_literate/l5_1-cpu-parallel_web.jl index 6aece862..c47b6b6a 100644 --- a/website/_literate/l5_1-cpu-parallel_web.jl +++ b/website/_literate/l5_1-cpu-parallel_web.jl @@ -262,10 +262,10 @@ md""" - Compute the elapsed time `t_toc` at the end of the time loop and report: """ -t_toc = ... -A_eff = ... # Effective main memory access per iteration [GB] -t_it = ... # Execution time per iteration [s] -T_eff = A_eff/t_it # Effective memory throughput [GB/s] +t_toc = Base.time() - t_tic +A_eff = (3*2)/1e9*nx*ny*sizeof(Float64) # Effective main memory access per iteration [GB] +t_it = t_toc/niter # Execution time per iteration [s] +T_eff = A_eff/t_it # Effective memory throughput [GB/s] #src ######################################################################### #nb # %% A slide [markdown] {"slideshow": {"slide_type": "slide"}} @@ -274,7 +274,7 @@ md""" - Round `T_eff` to the 3rd significant digit. ```julia -@printf("Time = %1.3f sec, ... \n", t_toc, ...) +@printf("Time = %1.3f sec, T_eff = %1.2f GB/s (niter = %d)\n", t_toc, round(T_eff, sigdigits=3), niter) ``` """ @@ -286,8 +286,10 @@ md""" - Define a `do_check` flag set to `false` """ -function Pf_diffusion_2D(;??) +function Pf_diffusion_2D(;do_check=false) + if do_check && (iter%ncheck == 0) ... + end return end @@ -363,19 +365,19 @@ md""" Implement a nested loop, taking car of bounds and staggering. """ -for iy=?? - for ix=?? - qDx[??] -= (qDx[??] + k_ηf_dx* ?? )*_1_θ_dτ +for iy=1:ny + for ix=1:nx-1 + qDx[ix+1,iy] -= (qDx[ix+1,iy] + k_ηf_dx*(Pf[ix+1,iy]-Pf[ix,iy]))*_1_θ_dτ end end -for iy=?? - for ix=?? - qDy[??] -= (qDy[??] + k_ηf_dy* ?? )*_1_θ_dτ +for iy=1:ny-1 + for ix=1:nx + qDy[ix,iy+1] -= (qDy[ix,iy+1] + k_ηf_dy*(Pf[ix,iy+1]-Pf[ix,iy]))*_1_θ_dτ end end -for iy=?? - for ix=?? - Pf[??] -= ?? +for iy=1:ny + for ix=1:nx + Pf[ix,iy] -= ((qDx[ix+1,iy]-qDx[ix,iy])*_dx + (qDy[ix,iy+1]-qDy[ix,iy])*_dy)*_β_dτ end end @@ -387,25 +389,25 @@ We could now use macros to make the code nicer and clearer. Macro expression wil Let's use macros to replace the derivative implementations """ -macro d_xa(A) esc(:( $A[??]-$A[??] )) end -macro d_ya(A) esc(:( $A[??]-$A[??] )) end +macro d_xa(A) esc(:( $A[ix+1,iy]-$A[ix,iy] )) end +macro d_ya(A) esc(:( $A[ix,iy+1]-$A[ix,iy] )) end md""" And update the code within the iteration loop: """ -for iy=?? - for ix=?? - qDx[??] -= (qDx[??] + k_ηf_dx* ?? )*_1_θ_dτ +for iy=1:ny + for ix=1:nx-1 + qDx[ix+1,iy] -= (qDx[ix+1,iy] + k_ηf_dx*@d_xa(Pf))*_1_θ_dτ end end -for iy=?? - for ix=?? - qDy[??] -= (qDy[??] + k_ηf_dy* ?? )*_1_θ_dτ +for iy=1:ny-1 + for ix=1:nx + qDy[ix,iy+1] -= (qDy[ix,iy+1] + k_ηf_dy*@d_ya(Pf))*_1_θ_dτ end end -for iy=?? - for ix=?? - Pf[??] -= ?? +for iy=1:ny + for ix=1:nx + Pf[ix,iy] -= (@d_xa(qDx)*_dx + @d_ya(qDy)*_dy)*_β_dτ end end @@ -438,15 +440,28 @@ md""" Create a `compute_flux!()` and `compute_Pf!()` functions that take input and output arrays and needed scalars as argument and return nothing. """ -function compute_flux!(...) +function compute_flux!(qDx,qDy,Pf,k_ηf_dx,k_ηf_dy,_1_θ_dτ) nx,ny=size(Pf) - ... + for iy=1:ny, + for ix=1:nx-1 + qDx[ix+1,iy] -= (qDx[ix+1,iy] + k_ηf_dx*@d_xa(Pf))*_1_θ_dτ + end + end + for iy=1:ny-1 + for ix=1:nx + qDy[ix,iy+1] -= (qDy[ix,iy+1] + k_ηf_dy*@d_ya(Pf))*_1_θ_dτ + end + end return nothing end -function update_Pf!(Pf,...) +function update_Pf!(Pf,qDx,qDy,_dx,_dy,_β_dτ) nx,ny=size(Pf) - ... + for iy=1:ny + for ix=1:nx + Pf[ix,iy] -= (@d_xa(qDx)*_dx + @d_ya(qDy)*_dy)*_β_dτ + end + end return nothing end @@ -483,9 +498,9 @@ md""" The `compute!()` function: """ -function compute!(Pf,qDx,qDy, ???) - compute_flux!(...) - update_Pf!(...) +function compute!(Pf,qDx,qDy,k_ηf_dx,k_ηf_dy,_1_θ_dτ,_dx,_dy,_β_dτ) + compute_flux!(qDx,qDy,Pf,k_ηf_dx,k_ηf_dy,_1_θ_dτ) + update_Pf!(Pf,qDx,qDy,_dx,_dy,_β_dτ) return nothing end @@ -493,8 +508,8 @@ md""" can then be called using `@belapsed` to return elapsed time for a single iteration, letting `BenchmarkTools` taking car about sampling """ -t_toc = @belapsed compute!($Pf,$qDx,$qDy,???) -niter = ??? +t_toc = @belapsed compute!($Pf,$qDx,$qDy,$k_ηf_dx,$k_ηf_dy,$_1_θ_dτ,$_dx,$_dy,$_β_dτ) +niter = 1 #nb # > 💡 note: Variables need to be interpolated into the function call, thus taking a `$` in front. #md # \note{Note that variables need to be interpolated into the function call, thus taking a `$` in front.} diff --git a/website/homework.md b/website/homework.md index c32049c2..41bd8b74 100644 --- a/website/homework.md +++ b/website/homework.md @@ -11,8 +11,8 @@ hascode = false | Lect. 2 | 02.10.2024 - 23h59 CET | [Moodle (notebooks)](https://moodle-app2.let.ethz.ch/mod/assign/view.php?id=1115224), [Moodle (commit hash + PR)](https://moodle-app2.let.ethz.ch/mod/assign/view.php?id=1103813) | For the notebooks submission, submit a folder containing all exercise notebooks from [JupyterHub](https://jhub-let-04-23586.ethz.ch/hub/home). For the commit hash + PR submission, copy the git commit hash (SHA) of the final push on the branch `homework-2` and open a pull request on the `main` branch. Paste both the commit hash and the PR link on Moodle (check [Logistics](/logistics/#submission) for more details on how to set up the GitHub repository).| | Lect. 3 | 11.10.2024 - 23h59 CET | [Moodle (commit hash + PR)](https://moodle-app2.let.ethz.ch/mod/assign/view.php?id=1103821) | For the submission, copy the git commit hash (SHA) of the final push on the branch `homework-3` and open a pull request on the `main` branch. Paste both the SHA and the PR link on Moodle. | | Lect. 4 | 18.10.2024 - 23h59 CET | [Moodle (commit hash + PR)](https://moodle-app2.let.ethz.ch/mod/assign/view.php?id=1103827) | For the submission, copy the git commit hash (SHA) of the final push on the branch `homework-4` and open a pull request on the `main` branch. Paste both the SHA and the PR link on Moodle. | -