diff --git a/docs/src/solutions_for_all_notebooks.md b/docs/src/solutions_for_all_notebooks.md index 80140b3..12340c3 100644 --- a/docs/src/solutions_for_all_notebooks.md +++ b/docs/src/solutions_for_all_notebooks.md @@ -213,4 +213,58 @@ end At each call to @spawnat we will communicate O(N) and compute O(N) in a worker process just like in algorithm 1. However, we will do this work N^2/P times on average at each worker. Thus, the total communication and computation on a worker will be O(N^3/P) for both communication and computation. Thus, the communication over computation ratio will still be O(1) and thus the communication will dominate in practice, making the algorithm inefficient. +## Jacobi method + +### Exercise 1 + +```julia +@everywhere workers() begin + using MPI + comm = MPI.Comm_dup(MPI.COMM_WORLD) + function jacobi_mpi(n,niters) + nranks = MPI.Comm_size(comm) + rank = MPI.Comm_rank(comm) + if mod(n,nranks) != 0 + println("n must be a multiple of nranks") + MPI.Abort(comm,1) + end + n_own = div(n,nranks) + u = zeros(n_own+2) + u[1] = -1 + u[end] = 1 + u_new = copy(u) + for t in 1:niters + reqs = MPI.Request[] + if rank != 0 + neig_rank = rank-1 + req = MPI.Isend(view(u,2:2),comm,dest=neig_rank,tag=0) + push!(reqs,req) + req = MPI.Irecv!(view(u,1:1),comm,source=neig_rank,tag=0) + push!(reqs,req) + end + if rank != (nranks-1) + neig_rank = rank+1 + s = n_own+1 + r = n_own+2 + req = MPI.Isend(view(u,s:s),comm,dest=neig_rank,tag=0) + push!(reqs,req) + req = MPI.Irecv!(view(u,r:r),comm,source=neig_rank,tag=0) + push!(reqs,req) + end + for i in 3:n_own + u_new[i] = 0.5*(u[i-1]+u[i+1]) + end + MPI.Waitall(reqs) + for i in (2,n_own+1) + u_new[i] = 0.5*(u[i-1]+u[i+1]) + end + u, u_new = u_new, u + end + return u + end +end +``` + + + diff --git a/notebooks/figures/fig_jacobi.svg b/notebooks/figures/fig_jacobi.svg index 0049c2b..f882af6 100644 --- a/notebooks/figures/fig_jacobi.svg +++ b/notebooks/figures/fig_jacobi.svg @@ -20,6 +20,36 @@ inkscape:export-ydpi="200"> + + + + + + 1 - - - - - - (i+1,j) - (i,j) - (i-1,j) - (i,j+1) - (i,j-1) + inkscape:export-ydpi="200"> + + + + + + (i+1,j) + (i,j) + (i-1,j) + (i,j+1) + (i,j-1) + + inkscape:export-ydpi="200" + transform="translate(35.814117)"> CPU 3 CPU 2 CPU 3 CPU 2 CPU 1 + style="fill:none;fill-opacity:0.21176471;stroke:#000000;stroke-width:0.16673626;stroke-linecap:square;stroke-linejoin:miter;stroke-miterlimit:4;stroke-dasharray:none" /> + style="fill:none;fill-opacity:0.21176471;stroke:#000000;stroke-width:0.16673626;stroke-linecap:square;stroke-linejoin:miter;stroke-miterlimit:4;stroke-dasharray:none" /> + style="fill:none;fill-opacity:0.21176471;stroke:#000000;stroke-width:0.16673626;stroke-linecap:square;stroke-linejoin:miter;stroke-miterlimit:4;stroke-dasharray:none" /> + style="fill:none;fill-opacity:0.21176471;stroke:#000000;stroke-width:0.16673626;stroke-linecap:square;stroke-linejoin:miter;stroke-miterlimit:4;stroke-dasharray:none" /> - CPU 1 + + style="fill:#0000ff;fill-opacity:0.21176471;stroke:#000000;stroke-width:0.16673626;stroke-linecap:square;stroke-linejoin:miter;stroke-miterlimit:4;stroke-dasharray:none" /> + style="fill:#0000ff;fill-opacity:0.21176471;stroke:#000000;stroke-width:0.16673626;stroke-linecap:square;stroke-linejoin:miter;stroke-miterlimit:4;stroke-dasharray:none" /> + style="fill:#0000ff;fill-opacity:0.21176471;stroke:#000000;stroke-width:0.16673626;stroke-linecap:square;stroke-linejoin:miter;stroke-miterlimit:4;stroke-dasharray:none" /> CPU 3 CPU 2 CPU 3 CPU 2 CPU 3 CPU 2 CPU 3 CPU 2 CPU 3 CPU 2 k - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - CPU 3 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - ? Data updated Data used (iteration k) + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + CPU 2 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + ? Data updated Data used (iteration k) + CPU 3 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - CPU 3 - N/P - N - 1 CPU 2 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + CPU 2 + N/P + N + 1 + ? + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + CPU 2 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Data updated Data used ? + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + CPU 5 Data updated Data used ? + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + CPU 1 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Data updated Data used ? + + + + + + + + + + + + + + + + + + + + + + + + + + u + u_new + + CPU 2 ? ? ? + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + CPU 2 CPU 1 CPU 3