From 83576f33cc2c4239d0030441a399e52bc03af6b3 Mon Sep 17 00:00:00 2001 From: Pier-Luc St-Onge Date: Mon, 13 May 2024 10:09:02 -0400 Subject: [PATCH] =?UTF-8?q?Chap=204=20-=20Collectives=20d'op=C3=A9rations?= =?UTF-8?q?=20de=20r=C3=A9duction?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 4-collectives.ipynb | 127 ++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 124 insertions(+), 3 deletions(-) diff --git a/4-collectives.ipynb b/4-collectives.ipynb index 3ea4198..ceb4012 100644 --- a/4-collectives.ipynb +++ b/4-collectives.ipynb @@ -232,8 +232,8 @@ "// void *recep, int compte_recep, MPI_Datatype type_recep,\n", "// MPI_Comm comm)\n", "\n", - "ierr = MPI_Alltoall(&a, 1, MPI_INT,\n", - " b, 1, MPI_INT, MPI_COMM_WORLD);\n", + "ierr = MPI_Alltoall(a, 1, MPI_INT,\n", + " b, 1, MPI_INT, MPI_COMM_WORLD);\n", "\n", "```" ] @@ -252,10 +252,131 @@ "```" ] }, + { + "cell_type": "markdown", + "id": "0f692ce2-2b93-4d98-90d7-770c42194d32", + "metadata": {}, + "source": [ + "## Calculs collectifs\n", + "\n", + "### Opérations de réduction\n", + "\n", + "C'est l'équivalent d'un `MPI_Gather` avec une boucle effectuant une\n", + "opération de réduction. Voici quelques opérations de réduction :" + ] + }, + { + "cell_type": "markdown", + "id": "13b3c7bb-b360-484e-83c7-0a0fd976a34f", + "metadata": {}, + "source": [ + "Opération | Constante MPI | Op([3, 5])\n", + "----------------------|---------------|-----------\n", + "Maximum | `MPI_MAX` | 5\n", + "Minimum | `MPI_MIN` | 3\n", + "Somme | `MPI_SUM` | 8\n", + "Produit | `MPI_PROD` | 15\n", + "_ET_ logique | `MPI_LAND` | Vrai\n", + "_OU_ logique | `MPI_LOR` | Vrai\n", + "_OU exclusif_ logique | `MPI_LXOR` | Faux\n", + "_ET_ binaire | `MPI_BAND` | 1 (001 = 011 & 101)\n", + "_OU_ binaire | `MPI_BOR` | 7 (111 = 011 \\| 101)\n", + "_OU exclusif_ binaire | `MPI_BXOR` | 6 (110 = 011 ^ 101)" + ] + }, + { + "cell_type": "markdown", + "id": "0c69323a-8f71-4002-ab83-b84bb3b3cda4", + "metadata": {}, + "source": [ + "### Réduction avec `MPI_Reduce`\n", + "\n", + "Voici un exemple de réduction effectuant une somme :\n", + "\n", + "![Figure MPI_Reduce](images/mpi_reduce.svg)" + ] + }, + { + "cell_type": "markdown", + "id": "0d37767c-ae8c-4c48-8136-630b23a758d1", + "metadata": {}, + "source": [ + "[En **C**](https://www.mpi-forum.org/docs/mpi-4.1/mpi41-report.pdf#subsection.6.9.1) :\n", + "\n", + "```C\n", + "// int MPI_Reduce(\n", + "// void *envoi, void *recep, int compte, MPI_Datatype type,\n", + "// MPI_Op op, int racine, MPI_Comm comm)\n", + "\n", + "ierr = MPI_Reduce(&a, &b, 1, MPI_INT,\n", + " MPI_SUM, 2, MPI_COMM_WORLD);\n", + "\n", + "```" + ] + }, + { + "cell_type": "markdown", + "id": "f61ea7f9-bab2-41ad-bd86-935121248dbe", + "metadata": {}, + "source": [ + "[En **Python**](https://mpi4py.readthedocs.io/en/latest/reference/mpi4py.MPI.Comm.html#mpi4py.MPI.Comm.reduce) :\n", + "\n", + "```Python\n", + "# reduce(envoi: Any, op: Op=SUM, racine: int = 0) -> Any | None\n", + "\n", + "b = comm.reduce(a, MPI.SUM, 2)\n", + "```" + ] + }, + { + "cell_type": "markdown", + "id": "af055858-a980-4077-81c5-11e2997ed3d4", + "metadata": {}, + "source": [ + "### Réduction et diffusion avec `MPI_Allreduce`\n", + "\n", + "C'est l'équivalent de `MPI_Reduce` + `MPI_Bcast`,\n", + "mais en plus efficace :\n", + "\n", + "![Figure MPI_Allreduce](images/mpi_allreduce.svg)" + ] + }, + { + "cell_type": "markdown", + "id": "5ce781ab-c1a5-4a62-b9f8-cd5c039e0f0a", + "metadata": {}, + "source": [ + "[En **C**](https://www.mpi-forum.org/docs/mpi-4.1/mpi41-report.pdf#subsection.6.9.6) :\n", + "\n", + "```C\n", + "// int MPI_Allreduce(\n", + "// void *envoi, void *recep, int compte, MPI_Datatype type,\n", + "// MPI_Op op, MPI_Comm comm)\n", + "\n", + "ierr = MPI_Allreduce(&a, &b, 1, MPI_INT,\n", + " MPI_SUM, MPI_COMM_WORLD);\n", + "\n", + "```" + ] + }, + { + "cell_type": "markdown", + "id": "f23cf72c-4e33-458e-95b2-0e492a8b7dca", + "metadata": {}, + "source": [ + "[En **Python**](https://mpi4py.readthedocs.io/en/latest/reference/mpi4py.MPI.Comm.html#mpi4py.MPI.Comm.allreduce) :\n", + "\n", + "```Python\n", + "# allreduce(envoi: Any, op: Op=SUM) -> Any\n", + "\n", + "b = comm.allreduce(a, MPI.SUM)\n", + "```" + ] + }, { "cell_type": "code", "execution_count": null, - "id": "a7085b5e-bf34-48b3-ba80-f2e3d9de1fc2", + "id": "2477c068-510f-48a6-b56e-24c0d911a60e", "metadata": {}, "outputs": [], "source": []