More work in mpi notebooks

This commit is contained in:
Francesc Verdugo 2024-08-23 15:10:19 +02:00
parent 9bd350c484
commit 1f79584e48
2 changed files with 97 additions and 206 deletions

View File

@ -691,7 +691,7 @@
"\n", "\n",
"\n", "\n",
"<div class=\"alert alert-block alert-info\">\n", "<div class=\"alert alert-block alert-info\">\n",
"<b>Note:</b> In Julia, send and receive buffers are usually arrays or references.\n", "<b>Note:</b> Note that the C interface provides additional arguments `MPI_Datatype` (type of data to send/receive) and `count` (number of items to send/receive). In Julia, send and receive buffers are usually arrays or references, from which the data type and the count can be inferred. This is true for many other MPI functions.\n",
"</div>" "</div>"
] ]
}, },

View File

@ -90,7 +90,7 @@
}, },
{ {
"cell_type": "markdown", "cell_type": "markdown",
"id": "d7ba2558", "id": "e9e2a219",
"metadata": {}, "metadata": {},
"source": [ "source": [
"## MPI_Barrier\n", "## MPI_Barrier\n",
@ -110,7 +110,7 @@
}, },
{ {
"cell_type": "markdown", "cell_type": "markdown",
"id": "5ae78757", "id": "d9753e4b",
"metadata": {}, "metadata": {},
"source": [ "source": [
"### Example\n", "### Example\n",
@ -120,25 +120,10 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 59, "execution_count": null,
"id": "c21ec2fb", "id": "9a66c717",
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [],
{
"name": "stdout",
"output_type": "stream",
"text": [
"[rank 3] I woke up\n",
"[rank 0] I woke up\n",
"[rank 2] I woke up\n",
"[rank 1] I woke up\n",
"[rank 0] Done!\n",
"[rank 1] Done!\n",
"[rank 2] Done!\n",
"[rank 3] Done!\n"
]
}
],
"source": [ "source": [
"code = quote\n", "code = quote\n",
" using MPI\n", " using MPI\n",
@ -155,7 +140,7 @@
}, },
{ {
"cell_type": "markdown", "cell_type": "markdown",
"id": "ff3bece9", "id": "df0223ff",
"metadata": {}, "metadata": {},
"source": [ "source": [
"## MPI_Reduce\n", "## MPI_Reduce\n",
@ -183,7 +168,7 @@
}, },
{ {
"cell_type": "markdown", "cell_type": "markdown",
"id": "0538ef9f", "id": "2fc1aa28",
"metadata": {}, "metadata": {},
"source": [ "source": [
"### Example\n", "### Example\n",
@ -192,23 +177,10 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 74, "execution_count": null,
"id": "481eaa2c", "id": "341b8438",
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [],
{
"name": "stdout",
"output_type": "stream",
"text": [
"[rank 0] I am contributing with [4]\n",
"[rank 1] I am contributing with [1]\n",
"[rank 2] I am contributing with [4]\n",
"[rank 2] The sum is nothing\n",
"[rank 0] The sum is [9][rank 1] The sum is nothing\n",
"\n"
]
}
],
"source": [ "source": [
"code = quote\n", "code = quote\n",
" using MPI\n", " using MPI\n",
@ -234,7 +206,7 @@
}, },
{ {
"cell_type": "markdown", "cell_type": "markdown",
"id": "8daac465", "id": "3ea77a8f",
"metadata": {}, "metadata": {},
"source": [ "source": [
"## Reducing multiple values\n", "## Reducing multiple values\n",
@ -244,7 +216,7 @@
}, },
{ {
"cell_type": "markdown", "cell_type": "markdown",
"id": "13199da4", "id": "b26fd0b2",
"metadata": {}, "metadata": {},
"source": [ "source": [
"### Example\n", "### Example\n",
@ -254,23 +226,10 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 75, "execution_count": null,
"id": "52bcb006", "id": "1a145948",
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [],
{
"name": "stdout",
"output_type": "stream",
"text": [
"[rank 0] I am contributing with [1, 4]\n",
"[rank 1] I am contributing with [2, 4]\n",
"[rank 2] I am contributing with [3, 3]\n",
"[rank 0] The sum is [6, 11]\n",
"[rank 1] The sum is nothing\n",
"[rank 2] The sum is nothing\n"
]
}
],
"source": [ "source": [
"code = quote\n", "code = quote\n",
" using MPI\n", " using MPI\n",
@ -293,7 +252,7 @@
}, },
{ {
"cell_type": "markdown", "cell_type": "markdown",
"id": "852e2517", "id": "539df34f",
"metadata": {}, "metadata": {},
"source": [ "source": [
"## MPI_Allreduce\n", "## MPI_Allreduce\n",
@ -303,7 +262,7 @@
}, },
{ {
"cell_type": "markdown", "cell_type": "markdown",
"id": "fb83387b", "id": "be0381bb",
"metadata": {}, "metadata": {},
"source": [ "source": [
"### Example\n", "### Example\n",
@ -313,23 +272,10 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 79, "execution_count": null,
"id": "c15c908a", "id": "1595f4f5",
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [],
{
"name": "stdout",
"output_type": "stream",
"text": [
"[rank 1] I am contributing with [2, 1]\n",
"[rank 2] I am contributing with [3, 3]\n",
"[rank 0] I am contributing with [3, 3]\n",
"[rank 0] The sum is [8, 7]\n",
"[rank 1] The sum is [8, 7]\n",
"[rank 2] The sum is [8, 7]\n"
]
}
],
"source": [ "source": [
"code = quote\n", "code = quote\n",
" using MPI\n", " using MPI\n",
@ -370,7 +316,7 @@
}, },
{ {
"cell_type": "markdown", "cell_type": "markdown",
"id": "f201abe7", "id": "3d0a1e9d",
"metadata": {}, "metadata": {},
"source": [ "source": [
"### Example\n", "### Example\n",
@ -395,23 +341,10 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 71, "execution_count": null,
"id": "1f8a70c6", "id": "1f8a70c6",
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [],
{
"name": "stdout",
"output_type": "stream",
"text": [
"[rank 1] I am sending [30]\n",
"[rank 2] I am sending [40]\n",
"[rank 0] I am sending [20]\n",
"[rank 0] I received [20, 30, 40]\n",
"[rank 1] I received nothing\n",
"[rank 2] I received nothing\n"
]
}
],
"source": [ "source": [
"code = quote\n", "code = quote\n",
" using MPI\n", " using MPI\n",
@ -438,7 +371,7 @@
}, },
{ {
"cell_type": "markdown", "cell_type": "markdown",
"id": "932c815a", "id": "0f85f08d",
"metadata": {}, "metadata": {},
"source": [ "source": [
"## MPI_Allgather\n", "## MPI_Allgather\n",
@ -448,7 +381,7 @@
}, },
{ {
"cell_type": "markdown", "cell_type": "markdown",
"id": "8572570f", "id": "4bf2ef34",
"metadata": {}, "metadata": {},
"source": [ "source": [
"### Example" "### Example"
@ -456,23 +389,10 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 78, "execution_count": null,
"id": "5c9b98a2", "id": "070e1fc1",
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [],
{
"name": "stdout",
"output_type": "stream",
"text": [
"[rank 0] I am sending [20]\n",
"[rank 1] I am sending [30]\n",
"[rank 2] I am sending [40]\n",
"[rank 0] I received [20, 30, 40]\n",
"[rank 1] I received [20, 30, 40]\n",
"[rank 2] I received [20, 30, 40]\n"
]
}
],
"source": [ "source": [
"code = quote\n", "code = quote\n",
" using MPI\n", " using MPI\n",
@ -493,7 +413,7 @@
}, },
{ {
"cell_type": "markdown", "cell_type": "markdown",
"id": "cf2ea30a", "id": "fb6a9034",
"metadata": {}, "metadata": {},
"source": [ "source": [
"## MPI_Gatherv\n", "## MPI_Gatherv\n",
@ -503,7 +423,7 @@
}, },
{ {
"cell_type": "markdown", "cell_type": "markdown",
"id": "faf0951a", "id": "e0d2998c",
"metadata": {}, "metadata": {},
"source": [ "source": [
"### Example\n", "### Example\n",
@ -513,22 +433,10 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 32, "execution_count": null,
"id": "8e65fcd0", "id": "23814278",
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [],
{
"name": "stdout",
"output_type": "stream",
"text": [
"[rank 1] I am sending [10, 10]\n",
"[rank 2] I am sending [20, 20, 20]\n",
"[rank 3] I am sending [30, 30, 30, 30]\n",
"[rank 0] I am sending [0]\n",
"[rank 0] I received [0, 10, 10, 20, 20, 20, 30, 30, 30, 30]\n"
]
}
],
"source": [ "source": [
"code = quote\n", "code = quote\n",
" using MPI\n", " using MPI\n",
@ -562,7 +470,7 @@
}, },
{ {
"cell_type": "markdown", "cell_type": "markdown",
"id": "1fb65390", "id": "25c2fb1e",
"metadata": {}, "metadata": {},
"source": [ "source": [
"## How to get the amount of data to be received?\n", "## How to get the amount of data to be received?\n",
@ -572,7 +480,7 @@
}, },
{ {
"cell_type": "markdown", "cell_type": "markdown",
"id": "23c57c46", "id": "12f9e2ca",
"metadata": {}, "metadata": {},
"source": [ "source": [
"### Example\n", "### Example\n",
@ -582,22 +490,10 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 39, "execution_count": null,
"id": "b89fc364", "id": "b10430a9",
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [],
{
"name": "stdout",
"output_type": "stream",
"text": [
"[rank 1] I am sending [1, 1]\n",
"[rank 2] I am sending [2]\n",
"[rank 3] I am sending [3, 3, 3, 3]\n",
"[rank 0] I am sending [0]\n",
"[rank 0] I received [0, 1, 1, 2, 3, 3, 3, 3]\n"
]
}
],
"source": [ "source": [
"code = quote\n", "code = quote\n",
" using MPI\n", " using MPI\n",
@ -664,7 +560,7 @@
}, },
{ {
"cell_type": "markdown", "cell_type": "markdown",
"id": "1df3bc76", "id": "e55aa353",
"metadata": {}, "metadata": {},
"source": [ "source": [
"### Example\n", "### Example\n",
@ -689,22 +585,10 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 44, "execution_count": null,
"id": "5ea413fd", "id": "5ea413fd",
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [],
{
"name": "stdout",
"output_type": "stream",
"text": [
"[rank 0] I am sending: [20, 30, 40, 50]\n",
"[rank 0] I have received: 20\n",
"[rank 1] I have received: 30\n",
"[rank 2] I have received: 40\n",
"[rank 3] I have received: 50\n"
]
}
],
"source": [ "source": [
"code = quote\n", "code = quote\n",
" using MPI\n", " using MPI\n",
@ -751,7 +635,7 @@
}, },
{ {
"cell_type": "markdown", "cell_type": "markdown",
"id": "97a15ba7", "id": "97369713",
"metadata": {}, "metadata": {},
"source": [ "source": [
"### Example\n", "### Example\n",
@ -776,22 +660,10 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 46, "execution_count": null,
"id": "4de15781", "id": "4de15781",
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [],
{
"name": "stdout",
"output_type": "stream",
"text": [
"[rank 0] I am sending: 20\n",
"[rank 0] I have received: 20\n",
"[rank 1] I have received: 20\n",
"[rank 2] I have received: 20\n",
"[rank 3] I have received: 20\n"
]
}
],
"source": [ "source": [
"code = quote\n", "code = quote\n",
" using MPI\n", " using MPI\n",
@ -813,7 +685,7 @@
}, },
{ {
"cell_type": "markdown", "cell_type": "markdown",
"id": "725d8381", "id": "e83be96d",
"metadata": {}, "metadata": {},
"source": [ "source": [
"## Communicators\n", "## Communicators\n",
@ -837,7 +709,7 @@
}, },
{ {
"cell_type": "markdown", "cell_type": "markdown",
"id": "48b2b85e", "id": "556ca223",
"metadata": {}, "metadata": {},
"source": [ "source": [
"## MPI_Comm_dup\n", "## MPI_Comm_dup\n",
@ -858,7 +730,48 @@
}, },
{ {
"cell_type": "markdown", "cell_type": "markdown",
"id": "4034e3eb", "id": "9acfb792",
"metadata": {},
"source": [
"### Example\n",
"\n",
"Send value 2 from rank 2 to rank 3. Then send value 1 also from rank 2 to rank 3. Note that we are using 2 different communicators for each send receive pair in such a way that we first receive value 1 and then value 2, even if this not was the other in which the data was sent. Try also to define `comm2 = comm` and see how the result will change."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "e50d44f6",
"metadata": {},
"outputs": [],
"source": [
"code = quote\n",
" using MPI\n",
" MPI.Init()\n",
" comm = MPI.COMM_WORLD\n",
" # We create a new communicator\n",
" comm2 = MPI.Comm_dup(comm)\n",
" rank = MPI.Comm_rank(comm)\n",
" if rank == 2\n",
" sndbuf = [2]\n",
" MPI.Send(sndbuf, comm2; dest=3, tag=0)\n",
" sndbuf = [1]\n",
" MPI.Send(sndbuf, comm; dest=3, tag=0)\n",
" end\n",
" if rank == 3\n",
" rcvbuf = zeros(Int,1)\n",
" MPI.Recv!(rcvbuf, comm; source=2, tag=0)\n",
" @show rcvbuf\n",
" MPI.Recv!(rcvbuf, comm2; source=2, tag=0)\n",
" @show rcvbuf\n",
" end\n",
"end\n",
"run(`$(mpiexec()) -np 4 julia --project=. -e $code`);"
]
},
{
"cell_type": "markdown",
"id": "5e1b13eb",
"metadata": {}, "metadata": {},
"source": [ "source": [
"## MPI_Comm_split\n", "## MPI_Comm_split\n",
@ -887,7 +800,7 @@
}, },
{ {
"cell_type": "markdown", "cell_type": "markdown",
"id": "947cebc7", "id": "2b128840",
"metadata": {}, "metadata": {},
"source": [ "source": [
"### Example\n", "### Example\n",
@ -897,21 +810,10 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 87, "execution_count": null,
"id": "19825efb", "id": "b18cb74d",
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [],
{
"name": "stdout",
"output_type": "stream",
"text": [
"Hello, I am rank 0 in world and rank 0 in group 0.\n",
"Hello, I am rank 1 in world and rank 0 in group 1.\n",
"Hello, I am rank 2 in world and rank 1 in group 0.\n",
"Hello, I am rank 3 in world and rank 1 in group 1.\n"
]
}
],
"source": [ "source": [
"code = quote\n", "code = quote\n",
" using MPI\n", " using MPI\n",
@ -929,7 +831,7 @@
}, },
{ {
"cell_type": "markdown", "cell_type": "markdown",
"id": "495775c9", "id": "b22e4bf4",
"metadata": {}, "metadata": {},
"source": [ "source": [
"### Example\n", "### Example\n",
@ -939,21 +841,10 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 96, "execution_count": null,
"id": "9abb132e", "id": "4c0e504b",
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [],
{
"name": "stdout",
"output_type": "stream",
"text": [
"[rank 1, newrank 0] I am contributing with [3]\n",
"[rank 3, newrank 1] I am contributing with [3]\n",
"[rank 1, newrank 0] The sum is [6]\n",
"[rank 3, newrank 1] The sum is nothing\n"
]
}
],
"source": [ "source": [
"code = quote\n", "code = quote\n",
" using MPI\n", " using MPI\n",