Add solution to exercise 1 in LEQ notebook

This commit is contained in:
Gelieza K 2023-11-27 11:50:14 +01:00
parent b628e017c1
commit f60ea53aba
3 changed files with 397 additions and 276 deletions

View File

@ -37,21 +37,10 @@
},
{
"cell_type": "code",
"execution_count": 1,
"execution_count": null,
"id": "7e93809a",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"ge_dep_check (generic function with 1 method)"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"outputs": [],
"source": [
"using Printf\n",
"function answer_checker(answer,solution)\n",
@ -160,21 +149,10 @@
},
{
"cell_type": "code",
"execution_count": 2,
"execution_count": null,
"id": "e4070214",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"gaussian_elimination! (generic function with 1 method)"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"outputs": [],
"source": [
"function gaussian_elimination!(B)\n",
" n,m = size(B)\n",
@ -214,24 +192,10 @@
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": null,
"id": "eb30df0d",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"3×4 Matrix{Float64}:\n",
" 1.0 3.0 1.0 9.0\n",
" 0.0 1.0 2.0 8.0\n",
" 0.0 0.0 1.0 4.0"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"outputs": [],
"source": [
"A = Float64[1 3 1; 1 2 -1; 3 11 5]\n",
"b = Float64[9,1,35]\n",
@ -487,10 +451,11 @@
"metadata": {},
"outputs": [],
"source": [
"n = 5\n",
"n = 12\n",
"C = tridiagonal_matrix(n)\n",
"b = ones(n)\n",
"gaussian_elimination!(C)"
"B = [C b]\n",
"gaussian_elimination!(B)"
]
},
{

View File

@ -38,21 +38,10 @@
},
{
"cell_type": "code",
"execution_count": 1,
"execution_count": null,
"id": "1dc78750",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"floyd_impl_check (generic function with 1 method)"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"outputs": [],
"source": [
"using Printf\n",
"\n",
@ -118,21 +107,10 @@
},
{
"cell_type": "code",
"execution_count": 2,
"execution_count": null,
"id": "4fe447c5",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"floyd! (generic function with 1 method)"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"outputs": [],
"source": [
"function floyd!(C)\n",
" n = size(C,1)\n",
@ -158,25 +136,10 @@
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": null,
"id": "860e537c",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"4×4 Matrix{Int64}:\n",
" 0 9 6 1\n",
" 2 0 8 3\n",
" 5 3 0 6\n",
" 10 8 5 0"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"outputs": [],
"source": [
"inf = 1000\n",
"C = [\n",
@ -310,21 +273,10 @@
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": null,
"id": "75cac17e",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"floyd2! (generic function with 1 method)"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"outputs": [],
"source": [
"function floyd2!(C)\n",
" n = size(C,1)\n",
@ -350,19 +302,10 @@
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": null,
"id": "907bc8c9",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
" 1.544626 seconds (10.53 k allocations: 732.570 KiB, 1.37% compilation time)\n",
" 2.646978 seconds (8.49 k allocations: 592.948 KiB, 0.47% compilation time)\n"
]
}
],
"outputs": [],
"source": [
"n = 1000\n",
"C = rand(n,n)\n",
@ -894,6 +837,68 @@
"4. **MPI.Bcast!**: Communicate row k using `MPI.Bcast!`. One needs to know which are the rows owned by the other ranks."
]
},
{
"cell_type": "markdown",
"id": "de96ad1b",
"metadata": {},
"source": [
"## Exercise \n",
"Rewrite the worker code of the parallel ASP algorithm so it runs correctly. Use the `MPI.Bcast!` to solve the problem of overtaking messages. Note: Only use `MPI.Bcast!`, do not use other MPI directives in addition. You can test your function with the following code cell. "
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "31194529",
"metadata": {},
"outputs": [],
"source": [
"function floyd_par!(C,N)\n",
" comm = MPI.Comm_dup(MPI.COMM_WORLD)\n",
" nranks = MPI.Comm_size(comm)\n",
" rank = MPI.Comm_rank(comm)\n",
" T = eltype(C)\n",
" if rank == 0\n",
" buffer_root = Vector{T}(undef,N*N)\n",
" buffer_root[:] = transpose(C)[:]\n",
" else\n",
" buffer_root = Vector{T}(undef,0)\n",
" end \n",
" Nw = div(N,nranks)\n",
" buffer = Vector{T}(undef,Nw*N)\n",
" MPI.Scatter!(buffer_root,buffer,comm;root=0)\n",
" Cw = Matrix{T}(undef,Nw,N)\n",
" transpose(Cw)[:] = buffer\n",
" MPI.Barrier(comm)\n",
" floyd_worker_bcast!(Cw,comm)\n",
" buffer[:] = transpose(Cw)[:]\n",
" MPI.Gather!(buffer,buffer_root,comm;root=0)\n",
" if rank == 0\n",
" transpose(C)[:] = buffer_root[:]\n",
" end\n",
" C\n",
"end\n",
"\n",
"@everywhere function floyd_worker_bcast!(Cw,comm)\n",
" # Your implementation here\n",
"end\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "1b7eb4c2",
"metadata": {},
"outputs": [],
"source": [
"load = 10\n",
"n = nworkers()*load\n",
"C = rand_distance_table(n)\n",
"C_seq = floyd!(copy(C))\n",
"C_par = floyd_par!(copy(C),n)\n",
"@test C_seq == C_par"
]
},
{
"cell_type": "markdown",
"id": "c789dc7a",

File diff suppressed because one or more lines are too long