mirror of
https://github.com/fverdugo/XM_40017.git
synced 2025-11-10 18:14:23 +01:00
Add communication ring examples to solutions.ipynb
This commit is contained in:
parent
9991211f35
commit
5e0531930e
@ -187,6 +187,110 @@
|
|||||||
"end"
|
"end"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"id": "2f343157",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"## Exercise: Ring communication - MPI"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"id": "a49be691",
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"using MPI\n",
|
||||||
|
"using Test\n",
|
||||||
|
"\n",
|
||||||
|
"MPI.Init()\n",
|
||||||
|
"comm = MPI.COMM_WORLD\n",
|
||||||
|
"rank = MPI.Comm_rank(comm)\n",
|
||||||
|
"id = rank + 1\n",
|
||||||
|
"root = 0\n",
|
||||||
|
"size = MPI.Comm_size(comm)\n",
|
||||||
|
"\n",
|
||||||
|
"dst = mod(rank + 1, size)\n",
|
||||||
|
"src = mod(rank - 1, size)\n",
|
||||||
|
"\n",
|
||||||
|
"send_buf = id\n",
|
||||||
|
"recv_buf = 1\n",
|
||||||
|
"\n",
|
||||||
|
"if rank == root \n",
|
||||||
|
" # Proc 1: Send id async to destination, then wait for receive\n",
|
||||||
|
" MPI.isend(send_buf, comm; dest=dst, tag=0)\n",
|
||||||
|
" recv_buf = MPI.recv(comm; source=src, tag=0)\n",
|
||||||
|
" @show recv_buf == factorial(size)\n",
|
||||||
|
" @test recv_buf == factorial(size)\n",
|
||||||
|
"else\n",
|
||||||
|
" # Other procs: receive sync and send async to next process\n",
|
||||||
|
" recv_buf = MPI.recv(comm; source=src, tag=0)\n",
|
||||||
|
" send_buf = recv_buf * id\n",
|
||||||
|
" MPI.isend(send_buf, comm; dest=dst, tag=0)\n",
|
||||||
|
"end\n",
|
||||||
|
"\n",
|
||||||
|
"MPI.Finalize()"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"id": "6cbbf074",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"## Exercise: Ring communication - Distributed.jl"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"id": "dc156523",
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"using Distributed \n",
|
||||||
|
"using Test\n",
|
||||||
|
"\n",
|
||||||
|
"np = 4\n",
|
||||||
|
"add_n = np - nprocs() \n",
|
||||||
|
"addprocs(add_n)\n",
|
||||||
|
"worker_ids = workers()\n",
|
||||||
|
"@assert nprocs() > nworkers()\n",
|
||||||
|
"\n",
|
||||||
|
"# Initialize id channel\n",
|
||||||
|
"id_chnl = RemoteChannel(()->Channel{Int}(1))\n",
|
||||||
|
"put!(id_chnl, 1)\n",
|
||||||
|
"\n",
|
||||||
|
"# Initialize data channel\n",
|
||||||
|
"job_chnl = RemoteChannel(()->Channel{Int}(1))\n",
|
||||||
|
"put!(job_chnl, 1)\n",
|
||||||
|
"\n",
|
||||||
|
"@sync for w in workers()\n",
|
||||||
|
" @spawnat w begin\n",
|
||||||
|
" pos = findfirst(worker_ids .== w) + 1\n",
|
||||||
|
" dst = mod(pos, np) + 1\n",
|
||||||
|
" src = mod(pos-2, np) + 1\n",
|
||||||
|
" while true \n",
|
||||||
|
" pred = fetch(id_chnl)\n",
|
||||||
|
" if pred == src\n",
|
||||||
|
" take!(id_chnl)\n",
|
||||||
|
" value = take!(job_chnl)\n",
|
||||||
|
" put!(job_chnl, value * pos) \n",
|
||||||
|
" put!(id_chnl, pos) \n",
|
||||||
|
" break\n",
|
||||||
|
" end\n",
|
||||||
|
" end\n",
|
||||||
|
" end\n",
|
||||||
|
"end\n",
|
||||||
|
"\n",
|
||||||
|
"res = take!(job_chnl)\n",
|
||||||
|
"@show res\n",
|
||||||
|
"@test res == factorial(np)\n",
|
||||||
|
"\n",
|
||||||
|
"rmprocs(workers())"
|
||||||
|
]
|
||||||
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
"id": "47d88e7a",
|
"id": "47d88e7a",
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user