Recently, I was working on a multi-threaded implementation of a function to calculate the Poisson distribution (amath_pdist). The goal was to divide the workload across multiple threads to improve performance, especially for large arrays. However, instead of achieving the expected speedup, I noticed a significant slowdown as the size of the array increased.
After some investigation, I discovered the culprit: false sharing. In this post, I’ll explain what false sharing is, show the original code causing the problem, and share the fixes that led to a substantial performance improvement.
False sharing happens when multiple threads work on different parts of a shared array, but their data resides in the same cache line. Cache lines are the smallest unit of data transferred between memory and the CPU cache (typically 64 bytes). If one thread writes to part of a cache line, it invalidates the line for other threads—even if they’re working on logically independent data. This unnecessary invalidation leads to significant performance degradation due to repeated reloading of cache lines.
Here’s a simplified version of my original code:
void *calculate_pdist_segment(void *data) { struct pdist_segment *segment = (struct pdist_segment *)data; size_t interval_a = segment->interval_a, interval_b = segment->interval_b; double lambda = segment->lambda; int *d = segment->data; for (size_t i = interval_a; i < interval_b; i++) { segment->pdist[i] = pow(lambda, d[i]) * exp(-lambda) / tgamma(d[i] + 1); } return NULL; } double *amath_pdist(int *data, double lambda, size_t n_elements, size_t n_threads) { double *pdist = malloc(sizeof(double) * n_elements); pthread_t threads[n_threads]; struct pdist_segment segments[n_threads]; size_t step = n_elements / n_threads; for (size_t i = 0; i < n_threads; i++) { segments[i].data = data; segments[i].lambda = lambda; segments[i].pdist = pdist; segments[i].interval_a = step * i; segments[i].interval_b = (i == n_threads - 1) ? n_elements : (step * (i + 1)); pthread_create(&threads[i], NULL, calculate_pdist_segment, &segments[i]); } for (size_t i = 0; i < n_threads; i++) { pthread_join(threads[i], NULL); } return pdist; }
In the above code:
This issue scaled poorly with larger arrays. While the boundary issue might seem small, the sheer number of iterations magnified the cost of cache invalidations, leading to seconds of unnecessary overhead.
To fix the problem, I used posix_memalign to ensure that the pdist array was aligned to 64-byte boundaries. This guarantees that threads operate on completely independent cache lines, eliminating false sharing.
Here’s the updated code:
double *amath_pdist(int *data, double lambda, size_t n_elements, size_t n_threads) { double *pdist; if (posix_memalign((void **)&pdist, 64, sizeof(double) * n_elements) != 0) { perror("Failed to allocate aligned memory"); return NULL; } pthread_t threads[n_threads]; struct pdist_segment segments[n_threads]; size_t step = n_elements / n_threads; for (size_t i = 0; i < n_threads; i++) { segments[i].data = data; segments[i].lambda = lambda; segments[i].pdist = pdist; segments[i].interval_a = step * i; segments[i].interval_b = (i == n_threads - 1) ? n_elements : (step * (i + 1)); pthread_create(&threads[i], NULL, calculate_pdist_segment, &segments[i]); } for (size_t i = 0; i < n_threads; i++) { pthread_join(threads[i], NULL); } return pdist; }
Aligned Memory:
No Cache Line Sharing:
Improved Cache Efficiency:
After applying the fix, the runtime of the amath_pdist function dropped significantly. For a dataset I was testing, the wall clock time dropped from 10.92 seconds to 0.06 seconds.
Thank you for reading!
For anyone curious about the code, you can find it here
The above is the detailed content of Understanding and Solving False Sharing in Multi-threaded Applications with an actual issue I had. For more information, please follow other related articles on the PHP Chinese website!