Parallel Programming With CUDA Tutorial (Part-4: The Floyd-Warshall Algorithm)

In this tutorial, we will tackle the famous Floyd–Warshall algorithm for finding shortest paths in a weighted graph with positive edge weights (or negative weights but with no negative cycles; we will not consider them here anyway). Why study this? Well, first of all, finding the shortest path between two nodes is a fundamental problem in graph theory. And second, it is not much big of a jump from my previous tutorial (Matrix Multiplication). So it will be really easy :D! In this tutorial I will assume you are already familiar with the Floyd–Warshall (FW) algorithm and mainly focus on how to implement it in CUDA. This is because it is a really popular algorithm and there are plenty of resources online that talks about it ( for example As usual, lets first define the problem:

Problem: Given a graph G(V, E) where V is a set of vertices and E is a set of edges we have to find the length of the shortest path between each pair of vertices.

In this tutorial, we will first see the standard single thread implementation of the FW. Then we will identify parts that can be parallelized without thread synchronization in the single thread version. We will also see a neat trick that can get rid of the branch statement in the standard implementation and make our code completely branchless. Using this knowledge we will finally implement the FW using CUDA (It will be a good idea to read Part-3, trust me!). Finally, we will benchmark it with single thread CPU version.

Above is a classic O(|V|³) implementation of the FW. Here dis[i][j] contains the distance between vertex i and j. We can see from the pseudocode above that for a given value of k the entire dis Matrix (|V|X|V|) is updated. Reading and writing are happening in this matrix simultaneously and the actual order of these operations does not matter. This makes it a really great candidate for GPU computation. So we will run the inner two loops in the GPU. The inner two loops have O(|V|²) complexity and running them on a GPU will yield O(|V|²/|P|) complexity where |P| is the number of threads. With a typical GPU, we will get O(10³) threads. So when |V|~O(10³) we will essentially have O(|V|) complexity for the inner loops and O(|V|²) overall complexity.

Now, in the first step, we will just separate the loops in two different functions. In the InnerLoops function, we have a branching condition that we need to get rid of because GPUs hate branching.

Bamm! We got rid of the branch condition using the simple trick shown above. This works because CPP treats logical true as 1 and logical false as 0. If you are still confused try out some values on that expression and compare it with code from step 1. It will start to make sense.

Finally, we just convert these two functions into CUDA. This is the exact same code from Part-3 with these two lines replacing whatever there was for matrix multiplication:

What it is doing in simple term is that in the single-thread code we were incrementing i and j with 1 in each iteration of the loops. Here we are incrementing i and j with B and T respectively in each iteration of the loops. So previously the nested loops were running for O(|V|²) as discussed earlier. Now it is running O(|V|²/(B*T)) where B*T is |P| i.e the number of threads. And we are done :D!

We will now benchmark GPU FW with CPU FW by varying the number of vertices. For this, we will use Google Colab. This is a departure from the earlier tutorials. I am doing this because Google Colab is more accessible. You can find the notebook in the link below. It already contains necessary script to initialize C++ CUDA in Colab (yes, the code is still in CPP).

Without any surprise, we can see a huge boost in the performance of the FW when using GPU. I had to display the time on the logarithmic scale (base 10) because the difference is so large. To give an idea, for 32000 vertices in the GPU the FW takes ~20 minutes while in CPU it takes ~69.7 hours (predicted by running K<|V| iterations of the outer loop). Colab gave me around ~9GB GPU memory and using this I could run the Floyd-Warshall for up to ~46000 vertices after which Colab ran out of memory (46000*46000*4 byte = 8.46 GB).



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store