namespace tf { /** @page kmeans_cudaflow k-means Clustering (cudaFlow) Following up on @ref kmeans, this page studies how to accelerate a k-means workload on a GPU using tf::cudaFlow. @tableofcontents @section DefineTheKMeansKernels Define the k-means Kernels Recall that the k-means algorithm has the following steps: We observe Step 2 and Step 3 of the algorithm are parallelizable across individual points for use to harness the power of GPU:
  1. for every data point, find the nearest centroid (L2 distance or other measurements) and assign the point to it
  2. for every centroid, move the centroid to the average of the points assigned to that centroid
  3. .
At a fine-grained level, we request one GPU thread to work on one point for Step 2 and one GPU thread to work on one centroid for Step 3. @code{.cpp} // px/py: 2D points // N: number of points // mx/my: centroids // K: number of clusters // sx/sy/c: storage to compute the average __global__ void assign_clusters( float* px, float* py, int N, float* mx, float* my, float* sx, float* sy, int K, int* c ) { const int index = blockIdx.x * blockDim.x + threadIdx.x; if (index >= N) { return; } // Make global loads once. float x = px[index]; float y = py[index]; float best_dance = FLT_MAX; int best_k = 0; for (int k = 0; k < K; ++k) { float d = L2(x, y, mx[k], my[k]); if (d < best_d) { best_d = d; best_k = k; } } atomicAdd(&sx[best_k], x); atomicAdd(&sy[best_k], y); atomicAdd(&c [best_k], 1); } // mx/my: centroids, sx/sy/c: storage to compute the average __global__ void compute_new_means( float* mx, float* my, float* sx, float* sy, int* c ) { int k = threadIdx.x; int count = max(1, c[k]); // turn 0/0 to 0/1 mx[k] = sx[k] / count; my[k] = sy[k] / count; } @endcode When we recompute the cluster centroids to be the mean of all points assigned to a particular centroid, multiple GPU threads may access the sum arrays, @c sx and @c sy, and the count array, @c c. To avoid data race, we use a simple @c atomicAdd method. @section DefineTheKMeanscudaFlow Define the k-means cudaFlow Based on the two kernels, we can define the %cudaFlow for the k-means workload below: @code{.cpp} // N: number of points // K: number of clusters // M: number of iterations // px/py: 2D point vector void kmeans_gpu( int N, int K, int M, cconst std::vector& px, const std::vector& py ) { std::vector h_mx, h_my; float *d_px, *d_py, *d_mx, *d_my, *d_sx, *d_sy, *d_c; for(int i=0; i @dotfile images/kmeans_3.dot The main %cudaFlow task, @c update_means, must not run before all required data has settled down. It precedes a condition task that circles back to itself until we reach @c M iterations. When iteration completes, the condition task directs the execution path to the %cudaFlow, @c h2d, to copy the results of clusters to @c h_mx and @c h_my and then deallocate all GPU memory. @section KMeanscudaFlowBenchmarking Benchmarking We run three versions of k-means, sequential CPU, parallel CPUs, and one GPU, on a machine of 12 Intel i7-8700 CPUs at 3.20 GHz and a Nvidia RTX 2080 GPU using various numbers of 2D point counts and iterations.
| N | K | M | CPU Sequential | CPU Parallel | GPU | | :-: | :-: | :-: | :-: | :-: | :-: | | 10 | 5 | 10 | 0.14 ms | 77 ms | 1 ms | | 100 | 10 | 100 | 0.56 ms | 86 ms | 7 ms | | 1000 | 10 | 1000 | 10 ms | 98 ms | 55 ms | | 10000 | 10 | 10000 | 1006 ms | 713 ms | 458 ms | | 100000 | 10 | 100000 | 102483 ms | 49966 ms | 7952 ms |
When the number of points is larger than 10K, both parallel CPU and GPU implementations start to pick up the speed over than the sequential version. We can see that using the built-in predicate, tf::cudaFlow::offload_n, can avoid repetitively creating the graph over and over, resulting in two times faster than conditional tasking. */ }