Merge pull request #42 from fverdugo/francesc

Changes in notebook for Jacobi among others
This commit is contained in:
Francesc Verdugo 2024-09-09 17:06:12 +02:00 committed by GitHub
commit c7cba5f3cd
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 131727 additions and 130193 deletions

View File

@ -122,8 +122,8 @@ makedocs(;
#"Distributed computing with MPI" => "mpi_tutorial.md", #"Distributed computing with MPI" => "mpi_tutorial.md",
"Matrix-matrix multiplication"=>"matrix_matrix.md", "Matrix-matrix multiplication"=>"matrix_matrix.md",
"MPI (point-to-point)" => "julia_mpi.md", "MPI (point-to-point)" => "julia_mpi.md",
"Jacobi method" => "jacobi_method.md",
"MPI (collectives)" => "mpi_collectives.md", "MPI (collectives)" => "mpi_collectives.md",
"Jacobi method" => "jacobi_method.md",
#"All pairs of shortest paths" => "asp.md", #"All pairs of shortest paths" => "asp.md",
#"Gaussian elimination" => "LEQ.md", #"Gaussian elimination" => "LEQ.md",
#"Traveling salesperson problem" => "tsp.md", #"Traveling salesperson problem" => "tsp.md",

File diff suppressed because one or more lines are too long

Before

Width:  |  Height:  |  Size: 6.5 MiB

After

Width:  |  Height:  |  Size: 6.1 MiB

File diff suppressed because one or more lines are too long

View File

@ -1497,23 +1497,22 @@
"function matmul_mpi_3!(C,A,B)\n", "function matmul_mpi_3!(C,A,B)\n",
"```\n", "```\n",
"\n", "\n",
"Assume that the input matrices `A` and `B` are given only on rank 0, the other ranks get dummy matrices with zero rows and zero columns to save memory. You need to communicate the required parts to other ranks. For simplicity you can assume that `A` and `B` are square matrices and that the number of rows is a multiple of the number of processes (on rank 0). The result `C` should be overwritten only on rank 0. You can use the following cell to implement and check your result." "Assume that the input matrices `A` and `B` are given only on rank 0, the other ranks get dummy matrices with zero rows and zero columns to save memory. You need to communicate the required parts to other ranks. For simplicity you can assume that `A` and `B` are square matrices and that the number of rows is a multiple of the number of processes (on rank 0). The result `C` should be overwritten only on rank 0. You can use the following cell to implement and check your result. Copy the code below to a file called `ex1.jl`. Modify the file (e.g. with vscode). Run it from the Julia REPL using the `run` function as explained in the [Getting Started tutorial](https://www.francescverdugo.com/XM_40017/dev/getting_started_with_julia/#Running-MPI-code)."
] ]
}, },
{ {
"cell_type": "code", "cell_type": "markdown",
"execution_count": null, "id": "4fa53366",
"id": "265c4593",
"metadata": {}, "metadata": {},
"outputs": [],
"source": [ "source": [
"code = quote\n", "```julia\n",
" using MPI\n", "# ex1.jl (begin)\n",
" MPI.Init()\n", "using MPI\n",
" function matmul_mpi_3!(C,A,B)\n", "MPI.Init()\n",
"function matmul_mpi_3!(C,A,B)\n",
" # Implement here\n", " # Implement here\n",
" end\n", "end\n",
" function testit(load)\n", "function testit(load)\n",
" comm = MPI.COMM_WORLD\n", " comm = MPI.COMM_WORLD\n",
" rank = MPI.Comm_rank(comm)\n", " rank = MPI.Comm_rank(comm)\n",
" if rank == 0\n", " if rank == 0\n",
@ -1528,15 +1527,16 @@
" matmul_mpi_3!(C,A,B)\n", " matmul_mpi_3!(C,A,B)\n",
" if rank == 0\n", " if rank == 0\n",
" if !(C ≈ A*B)\n", " if !(C ≈ A*B)\n",
" println(\"Check not passed\")\n", " println(\"Test failed 😢\")\n",
" else\n", " else\n",
" println(\"Check passed!\")\n", " println(\"Test passed 🥳\")\n",
" end\n", " end\n",
" end\n", " end\n",
" end\n",
" testit(100)\n",
"end\n", "end\n",
"run(`$(mpiexec()) -np 4 julia --project=. -e $code`);" "testit(100)\n",
"end\n",
"# ex1.jl (end)\n",
"```"
] ]
}, },
{ {
@ -1548,7 +1548,7 @@
"\n", "\n",
"Implement this \"simple\" algorithm (the telephone game):\n", "Implement this \"simple\" algorithm (the telephone game):\n",
"\n", "\n",
"Rank 0 generates a message (an integer). Rank 0 sends the message to rank 1. Rank 1 receives the message, increments the message by 1, and sends the result to rank 2. Rank 2 receives the message, increments the message by 1, and sends the result to rank 3. Etc. The last rank sends back the message to rank 0 closing the ring. See the next figure. Implement the communications using MPI. Do not use `Distributed`.\n" "Rank 0 generates a message (an integer). Rank 0 sends the message to rank 1. Rank 1 receives the message, increments the message by 1, and sends the result to rank 2. Rank 2 receives the message, increments the message by 1, and sends the result to rank 3. Etc. The last rank sends back the message to rank 0 closing the ring. See the next figure. Implement the communications using MPI. Do not use `Distributed`. Use a text editor and the Julia REPL. Do not try to implement the code in a notebook.\n"
] ]
}, },
{ {

View File

@ -898,77 +898,6 @@
"After learning this material and the previous MPI notebook, you have a solid basis to start implementing sophisticated parallel algorithms using MPI." "After learning this material and the previous MPI notebook, you have a solid basis to start implementing sophisticated parallel algorithms using MPI."
] ]
}, },
{
"cell_type": "markdown",
"id": "c6b23485",
"metadata": {},
"source": [
"## Exercises"
]
},
{
"cell_type": "markdown",
"id": "90dc58bb",
"metadata": {},
"source": [
"### Exercise 1\n",
"\n",
"In the parallel implementation of the Jacobi method in previous notebook, we assumed that the method runs for a given number of iterations. However, other stopping criteria are used in practice. The following sequential code implements a version of Jacobi in which the method iterates until the norm of the difference between u and u_new is below a tolerance.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "0fcb0cd6",
"metadata": {},
"outputs": [],
"source": [
"function jacobi_with_tol(n,tol)\n",
" u = zeros(n+2)\n",
" u[1] = -1\n",
" u[end] = 1\n",
" u_new = copy(u)\n",
" increment = similar(u)\n",
" while true\n",
" for i in 2:(n+1)\n",
" u_new[i] = 0.5*(u[i-1]+u[i+1])\n",
" end\n",
" increment .= u_new .- u\n",
" norm_increment = 0.0\n",
" for i in 1:n\n",
" increment_i = increment[i]\n",
" norm_increment += increment_i*increment_i\n",
" end\n",
" norm_increment = sqrt(norm_increment)\n",
" if norm_increment < tol*n\n",
" return u_new\n",
" end\n",
" u, u_new = u_new, u\n",
" end\n",
" u\n",
"end"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "dbf0c3b8",
"metadata": {},
"outputs": [],
"source": [
"n = 10\n",
"tol = 1e-12\n",
"jacobi_with_tol(n,tol)"
]
},
{
"cell_type": "markdown",
"id": "aab1455e",
"metadata": {},
"source": [
"Implement a parallel version of this algorithm. Recommended: start with the parallel implementation given in the previous notebook (see function `jacobi_mpi`) and introduce the new stopping criteria. Think carefully about which MPI operations you need to use in this case."
]
},
{ {
"cell_type": "markdown", "cell_type": "markdown",
"id": "5e8f6e6a", "id": "5e8f6e6a",