From 86e2f88e7b50f7876f22abed23ee82ef8f5b8e59 Mon Sep 17 00:00:00 2001 From: Francesc Verdugo Date: Mon, 2 Sep 2024 13:30:43 +0200 Subject: [PATCH] Adding MPI collectives notebook --- docs/make.jl | 1 + notebooks/mpi_collectives.ipynb | 111 ++++++++++++++++++++++++++++++-- 2 files changed, 105 insertions(+), 7 deletions(-) diff --git a/docs/make.jl b/docs/make.jl index 14944b2..b08fabd 100644 --- a/docs/make.jl +++ b/docs/make.jl @@ -123,6 +123,7 @@ makedocs(; "Matrix-matrix multiplication"=>"matrix_matrix.md", "MPI (point-to-point)" => "julia_mpi.md", "Jacobi method" => "jacobi_method.md", + "MPI (collectives)" => "mpi_collectives.md", #"All pairs of shortest paths" => "asp.md", #"Gaussian elimination" => "LEQ.md", #"Traveling salesperson problem" => "tsp.md", diff --git a/notebooks/mpi_collectives.ipynb b/notebooks/mpi_collectives.ipynb index 00894f6..fbba57f 100644 --- a/notebooks/mpi_collectives.ipynb +++ b/notebooks/mpi_collectives.ipynb @@ -39,7 +39,7 @@ "source": [ "## Collective communication\n", "\n", - "MPI provides collective communication functions for communication involving multiple processes. Some usual collective functions are:\n", + "MPI provides a set of routines for communication involving multiple processes. These are called *collective communication* operations. Some usual collective operations are:\n", "\n", "\n", "- `MPI_Barrier`: Synchronize all processes\n", @@ -61,9 +61,9 @@ "id": "4ffa5e56", "metadata": {}, "source": [ - "## Why collective primitives?\n", + "## Why collective operations?\n", "\n", - "Point-to-point communication primitives provide all the building blocks needed in parallel programs and could be used to implement the collective functions described above. Then, why does MPI provide collective communication directives? There are several reasons:\n", + "Point-to-point communication functions provide all the building blocks needed in parallel programs and could be used to implement the collective functions described above. Then, why does MPI provide collective communication functions? There are several reasons:\n", "\n", "- Ease of use: It is handy for users to have these functions readily available instead of having to implement them.\n", "- Performance: Library implementations typically use efficient algorithms (such as reduction trees).\n", @@ -77,6 +77,8 @@ "source": [ "## Semantics of collective operations\n", "\n", + "These are key properties of collective operations:\n", + "\n", "\n", "- Completeness: All the collective communication directives above are *complete* operations. Thus, it is safe to use and reset the buffers once the function returns.\n", "- Standard mode: Collective directives are in standard mode only, like `MPI_Send`. Assuming that they block is erroneous, assuming that they do not block is also erroneous.\n", @@ -84,7 +86,7 @@ "\n", "\n", "
\n", - "Note: Recent versions of the MPI standard also include non-blocking (incomplete) versions of collective operations (not covered in this notebook). A particularly funny one is the non-blocking barrier `MPI_Ibarrier`.\n", + "Note: Recent versions of the MPI standard also include non-blocking (i.e., incomplete) versions of collective operations (not covered in this notebook). A particularly funny one is the non-blocking barrier `MPI_Ibarrier`.\n", "
" ] }, @@ -145,7 +147,7 @@ "source": [ "## MPI_Reduce\n", "\n", - "Combines values provided by different processors according to a given reduction operation. The result is received in a single process (called the root process).\n", + "This function combines values provided by different processors according to a given reduction operation. The result is received in a single process (called the root process).\n", "\n", "In Julia:\n", "```julia\n", @@ -702,7 +704,7 @@ "When you write an MPI program it is very likely that you are going to use libraries that also use MPI to send messages. Ideally, these libraries should not interfere with application messages. Using tags to isolate the messages send by your application does not solve the problem. MPI communicators fix this problem as they provided an isolated communication context. For instance, `MPI_SEND` and `MPI_RECV` specify a communicator. `MPI_RECV` can only receive messages sent to same communicator. The same is also true for collective communication directives. If two libraries use different communicators, their message will never interfere. In particular it is recommended to never use the default communicator, `MPI_COMM_WORLD`, directly when working with other libraries. A new isolated communicator can be created with `MPI_Comm_dup`.\n", "\n", "\n", - "### Process groups\n", + "### Groups of processes\n", "\n", "On the other hand, imagine that we want to use an MPI communication directive like `MPI_Gather`, but we only want a subset of the processes to participate in the operation. So far, we have used always the default communication `MPI_COMM_WORLD`, which represents all processes. Thus, by using this communicator, we are including all processes in the operation. We can create other communicators that contain only a subset of processes. To this end, we can use function `MPI_Comm_split`.\n" ] @@ -793,7 +795,7 @@ "\n", "There are two key parameters:\n", "\n", - "- `color`: all processes with the same color will be grouped in the same communicator.\n", + "- `color`: all processes with the same color will be grouped in the same new communicator.\n", "- `key`: The processes will be ranked in the new communicator according to key, breaking ties with the rank in the old communicator. \n", "\n" ] @@ -872,6 +874,101 @@ "run(`$(mpiexec()) -np 4 julia --project=. -e $code`);" ] }, + { + "cell_type": "markdown", + "id": "d465ebce", + "metadata": {}, + "source": [ + "Try to run the code without splitting the communicator. I.e., replace `newcomm = MPI.Comm_split(comm, color, key)` with `newcomm = comm`. Try to figure out what will happen before executing the code." + ] + }, + { + "cell_type": "markdown", + "id": "d334aea1", + "metadata": {}, + "source": [ + "## Conclusion\n", + "\n", + "- MPI also defines operations involving several processes called, collective operations.\n", + "- These are provided both for convenience and performance.\n", + "- The semantics are equivalent to \"standard mode\" `MPI_Send`, but there are also non-blocking versions (not discussed in this notebook).\n", + "- Discovering message sizes is often done by communicating the message size, instead of using `MPI_Probe`.\n", + "- Finally, we discussed MPI communicators. They provide two key features: isolated communication context and creating groups of processes. They are useful, for instance, to combine different libraries using MPI in the same application, and to use collective operations in a subset of the processes.\n", + "\n", + "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",