mirror of
https://github.com/fverdugo/XM_40017.git
synced 2025-11-24 09:24:32 +01:00
Add exercise solution for measuring TSP search overhead
This commit is contained in:
@@ -291,6 +291,194 @@
|
||||
"rmprocs(workers())"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "19641daf",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## TSP Exercise: Measure search overhead"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "f00557a0",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"## TSP serial \n",
|
||||
"function tsp_serial(connections,city)\n",
|
||||
" num_cities = length(connections)\n",
|
||||
" path=zeros(Int,num_cities)\n",
|
||||
" hops = 1\n",
|
||||
" path[hops] = city\n",
|
||||
" min_path = zeros(Int, num_cities)\n",
|
||||
" current_distance = 0\n",
|
||||
" min_distance = typemax(Int)\n",
|
||||
" # Collect search time \n",
|
||||
" search_time = @elapsed min_path, min_distance = tsp_serial_impl(connections,hops,path,current_distance, min_path, min_distance)\n",
|
||||
" (;path=min_path,distance=min_distance, search_time)\n",
|
||||
"end"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "30784da2",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"## TSP distributed\n",
|
||||
"@everywhere function tsp_dist_impl(wait_time, connections,hops,path,current_distance,min_dist_chnl, max_hops,jobs_chnl,ftr_result)\n",
|
||||
" num_cities = length(connections)\n",
|
||||
" if hops == num_cities\n",
|
||||
" min_distance = fetch(min_dist_chnl)\n",
|
||||
" if current_distance < min_distance\n",
|
||||
" take!(min_dist_chnl)\n",
|
||||
" # Collect wait time to substract from overall search time \n",
|
||||
" if ftr_result !== nothing\n",
|
||||
" wait_time += @elapsed @spawnat 1 begin\n",
|
||||
" result = fetch(ftr_result)\n",
|
||||
" result.path .= path\n",
|
||||
" result.min_distance_ref[] = current_distance\n",
|
||||
" end |> wait\n",
|
||||
" end\n",
|
||||
" put!(min_dist_chnl, current_distance)\n",
|
||||
" end\n",
|
||||
" elseif hops <= max_hops\n",
|
||||
" current_city = path[hops]\n",
|
||||
" next_hops = hops + 1\n",
|
||||
" for (next_city,distance_increment) in connections[current_city]\n",
|
||||
" if !visited(next_city,hops,path)\n",
|
||||
" path[next_hops] = next_city\n",
|
||||
" next_distance = current_distance + distance_increment\n",
|
||||
" # Collect wait time because fetch may block\n",
|
||||
" wait_time += @elapsed min_distance = fetch(min_dist_chnl)\n",
|
||||
" if next_distance < min_distance\n",
|
||||
" tsp_dist_impl(wait_time, connections,next_hops,path,next_distance,min_dist_chnl,max_hops,jobs_chnl,ftr_result)\n",
|
||||
" end\n",
|
||||
" end\n",
|
||||
" end \n",
|
||||
" else\n",
|
||||
" # Collect communication time and add to wait time\n",
|
||||
" wait_time += @elapsed if jobs_chnl !== nothing \n",
|
||||
" path_copy = copy(path) \n",
|
||||
" put!(jobs_chnl,(;hops,path=path_copy,current_distance))\n",
|
||||
" end\n",
|
||||
" end\n",
|
||||
" # Return wait time\n",
|
||||
" wait_time\n",
|
||||
"end\n",
|
||||
"\n",
|
||||
"function tsp_dist(connections,city)\n",
|
||||
" max_hops = 2\n",
|
||||
" num_cities = length(connections)\n",
|
||||
" path=zeros(Int,num_cities)\n",
|
||||
" result_path=zeros(Int, num_cities)\n",
|
||||
" wait_time = 0\n",
|
||||
" search_time = 0\n",
|
||||
" hops = 1\n",
|
||||
" path[hops] = city\n",
|
||||
" current_distance = 0\n",
|
||||
" min_distance = typemax(Int)\n",
|
||||
" jobs_chnl = RemoteChannel(()->Channel{Any}(10))\n",
|
||||
" min_dist_chnl = RemoteChannel(()->Channel{Int}(1))\n",
|
||||
" put!(min_dist_chnl, min_distance)\n",
|
||||
" ftr_result = @spawnat 1 (;path=result_path,min_distance_ref=Ref(min_distance))\n",
|
||||
" @async begin\n",
|
||||
" # Collect search time from master process\n",
|
||||
" search_time += @elapsed wait_time += tsp_dist_impl(wait_time,connections,hops,path,current_distance,min_dist_chnl,max_hops,jobs_chnl,nothing)\n",
|
||||
" for w in workers()\n",
|
||||
" put!(jobs_chnl,nothing)\n",
|
||||
" end\n",
|
||||
" end\n",
|
||||
" @sync for w in workers()\n",
|
||||
" @spawnat w begin\n",
|
||||
" path = zeros(Int, num_cities)\n",
|
||||
" max_hops = typemax(Int)\n",
|
||||
" while true\n",
|
||||
" job = take!(jobs_chnl)\n",
|
||||
" if job == nothing\n",
|
||||
" break\n",
|
||||
" end\n",
|
||||
" hops = job.hops\n",
|
||||
" path = job.path \n",
|
||||
" current_distance = job.current_distance\n",
|
||||
" min_distance = fetch(min_dist_chnl)\n",
|
||||
" if current_distance < min_distance\n",
|
||||
" # Collect search time from worker processes \n",
|
||||
" search_time += @elapsed wait_time += tsp_dist_impl(wait_time,connections,hops,path,current_distance,min_dist_chnl,max_hops,nothing,ftr_result)\n",
|
||||
" end\n",
|
||||
" end\n",
|
||||
" end\n",
|
||||
" end \n",
|
||||
" result = fetch(ftr_result)\n",
|
||||
" (;path = result.path, distance = result.min_distance_ref[], search_time, wait_time)\n",
|
||||
"end\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "694de934",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"using Distributed\n",
|
||||
"using RandomMatrix\n",
|
||||
"using Plots\n",
|
||||
"\n",
|
||||
"function generate_rand_connections(city_range, distance_range)\n",
|
||||
" # generate random connections matrix \n",
|
||||
" n_cities = rand(city_range)\n",
|
||||
" matrix = randTriangular(distance_range, n_cities; Diag=false)\n",
|
||||
"\n",
|
||||
" connections = Array{Array{Tuple{Int64,Int64},1},1}(undef, n_cities)\n",
|
||||
" for i in 1:n_cities\n",
|
||||
" connections[i] = Array{Tuple{Int64,Int64},1}(undef, n_cities)\n",
|
||||
" end\n",
|
||||
" for i in 1:n_cities\n",
|
||||
" for j in i:n_cities\n",
|
||||
" distance = matrix[i,j]\n",
|
||||
" connections[i][j] = (j,distance)\n",
|
||||
" connections[j][i] = (i,distance)\n",
|
||||
" end\n",
|
||||
" end\n",
|
||||
" return connections\n",
|
||||
"end\n",
|
||||
"\n",
|
||||
"# Run once so compile times are not measured\n",
|
||||
"distance_range = 1:100\n",
|
||||
"connections = generate_rand_connections(4:4, distance_range)\n",
|
||||
"tsp_dist(connections,1)\n",
|
||||
"tsp_serial(connections,1)\n",
|
||||
"\n",
|
||||
"# Measure runtimes of serial and parallel algorithm\n",
|
||||
"n_it = 5\n",
|
||||
"city_ranges = [4:4, 6:6, 8:8, 10:10]\n",
|
||||
"search_overhead = zeros(Float64, length(city_ranges), n_it )\n",
|
||||
"for (i, n) in enumerate(city_ranges)\n",
|
||||
" for k in 1:n_it\n",
|
||||
" connections = generate_rand_connections(n, distance_range)\n",
|
||||
" @show n, k\n",
|
||||
" path_dist, distance_dist, search_time_dist, wait_time_dist = tsp_dist(connections,1)\n",
|
||||
" path_serial, distance_serial, search_time_serial = tsp_serial(connections,1)\n",
|
||||
" # Compute search overhead as difference between distributed program and serial program\n",
|
||||
" # (without time spent communicating or waiting)\n",
|
||||
" search_overhead[i, k] = search_time_dist - wait_time_dist - search_time_serial\n",
|
||||
" end\n",
|
||||
"end\n",
|
||||
"\n",
|
||||
"min_search_oh = minimum(search_overhead, dims=2)\n",
|
||||
"city_sizes = [4,6,8,10]\n",
|
||||
"plot(city_sizes, min_search_oh, yaxis=:log, seriestype=:scatter,legend=false)\n",
|
||||
"plot!(city_sizes, min_search_oh, yaxis=:log, legend=false)\n",
|
||||
"\n",
|
||||
"xlabel!(\"Number of cities\")\n",
|
||||
"ylabel!(\"Search overhead (s)\")\n",
|
||||
"title!(\"Minimum search overhead for different problem sizes\")"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "47d88e7a",
|
||||
@@ -314,7 +502,7 @@
|
||||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "Julia 1.9.0",
|
||||
"display_name": "Julia 1.9.1",
|
||||
"language": "julia",
|
||||
"name": "julia-1.9"
|
||||
},
|
||||
@@ -322,7 +510,7 @@
|
||||
"file_extension": ".jl",
|
||||
"mimetype": "application/julia",
|
||||
"name": "julia",
|
||||
"version": "1.9.0"
|
||||
"version": "1.9.1"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
|
||||
Reference in New Issue
Block a user