diff --git a/notebooks/jacobi_2D.ipynb b/notebooks/jacobi_2D.ipynb index 0569628..dd2358f 100644 --- a/notebooks/jacobi_2D.ipynb +++ b/notebooks/jacobi_2D.ipynb @@ -2110,7 +2110,7 @@ ], "metadata": { "kernelspec": { - "display_name": "Julia 1.9.0", + "display_name": "Julia 1.9.1", "language": "julia", "name": "julia-1.9" }, @@ -2118,7 +2118,7 @@ "file_extension": ".jl", "mimetype": "application/julia", "name": "julia", - "version": "1.9.0" + "version": "1.9.1" } }, "nbformat": 4, diff --git a/notebooks/jacobi_method.ipynb b/notebooks/jacobi_method.ipynb index 3331276..1e5a758 100644 --- a/notebooks/jacobi_method.ipynb +++ b/notebooks/jacobi_method.ipynb @@ -35,17 +35,17 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": 2, "id": "1dc78750", "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "jacobi_2_check (generic function with 1 method)" + "jacobi_4_check (generic function with 1 method)" ] }, - "execution_count": 5, + "execution_count": 2, "metadata": {}, "output_type": "execute_result" } @@ -62,7 +62,9 @@ "end\n", "gauss_seidel_1_check(answer) = answer_checker(answer,\"c\")\n", "jacobi_1_check(answer) = answer_checker(answer, \"d\")\n", - "jacobi_2_check(answer) = answer_checker(answer, \"b\")" + "jacobi_2_check(answer) = answer_checker(answer, \"b\")\n", + "jacobi_3_check(answer) = answer_checker(answer, \"c\")\n", + "jacobi_4_check(anwswer) = answer_checker(answer, \"d\")" ] }, { @@ -73,7 +75,7 @@ "## The Jacobi method\n", "\n", "\n", - "The [Jacobi method](https://en.wikipedia.org/wiki/Jacobi_method) is a numerical tool to solve systems of linear algebraic equations. One of the main applications of the method is to solve boundary value problems (BVPs). I.e., given the values at the boundary (of a grid), the Jacoby method will find the interior values that fulfill a certain equation.\n", + "The [Jacobi method](https://en.wikipedia.org/wiki/Jacobi_method) is a numerical tool to solve systems of linear algebraic equations. One of the main applications of the method is to solve boundary value problems (BVPs). I.e., given the values at the boundary (of a grid), the Jacobi method will find the interior values that fulfill a certain equation.\n", "\n" ] }, @@ -147,7 +149,7 @@ "metadata": {}, "source": [ "\n", - "### Where do we can exploit parallelism?\n", + "### Where can we exploit parallelism?\n", "\n", "Look at the two nested loops in the sequential implementation:\n", "\n", @@ -161,7 +163,7 @@ "```\n", "\n", "- The outer loop cannot be parallelized. The value of `u` at step `t+1` depends on the value at the previous step `t`.\n", - "- The inner loop can be parallelized\n", + "- The inner loop can be parallelized.\n", "\n" ] }, @@ -239,20 +241,12 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": null, "id": "4edad93f", "metadata": { "scrolled": true }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "It's not correct. Keep trying! 💪\n" - ] - } - ], + "outputs": [], "source": [ "answer = \"x\" # replace x with a, b, c or d\n", "gauss_seidel_1_check(answer)" @@ -340,18 +334,10 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": null, "id": "3a03fc4c", "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "It's not correct. Keep trying! 💪\n" - ] - } - ], + "outputs": [], "source": [ "answer = \"x\" # replace x with a, b, c or d\n", "jacobi_1_check(answer)" @@ -366,7 +352,7 @@ "\n", "We consider the implementation using MPI. The programming model of MPI is generally better suited for data-parallel algorithms like this one than the task-based model provided by Distributed.jl. In any case, one can also implement it using Distributed, but it requires some extra effort to setup remote channels right for the communication between neighbor processes.\n", "\n", - "Take a look at the implementation below and try to understand it. Note that we have used MPIClustermanagers and Distributed just to run the MPI code on the notebook. When running it on a cluster MPIClustermanagers and Distributed are not needed.\n" + "Take a look at the implementation below and try to understand it. Note that we have used MPIClustermanagers and Distributed just to run the MPI code on the notebook. When running it on a cluster, MPIClustermanagers and Distributed are not needed.\n" ] }, { @@ -386,7 +372,7 @@ "metadata": {}, "outputs": [], "source": [ - "using MPIClusterManagers\n", + "using MPIClusterManagers \n", "using Distributed" ] }, @@ -407,13 +393,26 @@ { "cell_type": "code", "execution_count": null, - "id": "68851107", + "id": "a0923606", "metadata": {}, "outputs": [], "source": [ - "@everywhere workers() begin\n", + "# Test cell, remove me\n", + "u = [-1, 0, 0, 0, 0, 1]\n", + "view(u, 6:6)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "68851107", + "metadata": { + "code_folding": [] + }, + "outputs": [], + "source": [ + "@mpi_do manager begin\n", " using MPI\n", - " MPI.Initialized() || MPI.Init()\n", " comm = MPI.Comm_dup(MPI.COMM_WORLD)\n", " nw = MPI.Comm_size(comm)\n", " iw = MPI.Comm_rank(comm)+1\n", @@ -429,6 +428,7 @@ " u_new = copy(u)\n", " for t in 1:niters\n", " reqs = MPI.Request[]\n", + " # Exchange cell values with neighbors\n", " if iw != 1\n", " neig_rank = (iw-1)-1\n", " req = MPI.Isend(view(u,2:2),comm,dest=neig_rank,tag=0)\n", @@ -438,8 +438,8 @@ " end\n", " if iw != nw\n", " neig_rank = (iw+1)-1\n", - " s = n_own-1\n", - " r = n_own\n", + " s = n_own+1\n", + " r = n_own+2\n", " req = MPI.Isend(view(u,s:s),comm,dest=neig_rank,tag=0)\n", " push!(reqs,req)\n", " req = MPI.Irecv!(view(u,r:r),comm,source=neig_rank,tag=0)\n", @@ -453,6 +453,14 @@ " end\n", " u\n", " @show u\n", + " # Gather results in root process\n", + " results = zeros(n+2)\n", + " results[1] = -1\n", + " results[n+2] = 1\n", + " MPI.Gather!(view(u,2:n_own+1), view(results, 2:n+1), root=0, comm)\n", + " if iw == 1\n", + " @show results\n", + " end \n", " end\n", " niters = 100\n", " load = 4\n", @@ -485,8 +493,60 @@ "outputs": [], "source": [ "answer = \"x\" # replace x with a, b, c or d\n", - "jacobi_2_check(answer)\n", - "# TODO: think of more questions" + "jacobi_2_check(answer)" + ] + }, + { + "cell_type": "markdown", + "id": "075dd6d8", + "metadata": {}, + "source": [ + "