Major updates in jacobi method

This commit is contained in:
Francesc Verdugo
2024-09-09 16:59:37 +02:00
parent 4a9dc6f2c4
commit c35f674bd5
5 changed files with 1658 additions and 616 deletions

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."
]
},
{
"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",
"id": "5e8f6e6a",