I'm currently trying to parallelize an astar algorithm and I'm trying to split the graph based on the number of processes. The structure of the graph is similar to a grid and Here is my attempt
if __name__ == "__main__":
comm = MPI.COMM_WORLD
rank = comm.Get_rank()
size = comm.Get_size()
file_path = "grid_graph.txt"
graph = None
assigned_nodes = []
if rank == 0:
graph = load_graph(file_path)
graph = comm.bcast(graph, root=0)
total_nodes = len(graph)
num_processes = comm.Get_size()
base_num_nodes = total_nodes // num_processes
remaining_nodes = total_nodes % num_processes
start_index = rank * base_num_nodes
end_index = (rank + 1) * base_num_nodes
if rank < remaining_nodes:
start_index += rank
end_index += rank + 1
else:
start_index += remaining_nodes
end_index += remaining_nodes
assigned_nodes = list(graph.keys())[start_index:end_index]
all_assigned_nodes = comm.gather(assigned_nodes, root=0)
if rank == 0:
for process_rank, nodes in enumerate(all_assigned_nodes):
print("Process", process_rank, "assigned nodes:", nodes)
print("Process", rank, "assigned nodes:", assigned_nodes)
start = Node((0, 0))
goal = Node((2, 2))
try:
start_time = time.time()
path = astar(start, goal, graph)
print("Process", rank, "Path found:", path)
print("Process", rank, "Total cost:", total_cost(path, graph))
print("Process", rank, "Execution time:", time.time() - start_time, "seconds")
except ValueError:
print("Process", rank, "No path found")
the problem here is that some of the nodes in the subgraphs have no neighbors. How do I partition the graph so that all the nodes in the graph are connected or at least there is a searchable path?