Suppose I want to do a parallel sum of an array, here is the pseudo code:
SplitIntoNParts();
int *result = new int[N]; // this is shared among N worker threads
// worker i store its addition result in result[i]
// since worker thread i only accesses result[i], no race condition happens
for(int i = 0; i < N; i++){
// create a worker thread to do addition in part i, and store the result in result[i]
}
// join threads
// return sum of result array
I read from a lecture on parallel programming (page25)
If array elements happen to share a cache line, this leads to false sharing. – Non-shared data in the same cache line so each update invalidates the cache line … in essence “sloshing independent data” back and forth between threads.
It says sharing the result
array is false sharing, but I didn't quite get it.
What does 'each update invalidate the cache line' mean?
What does the 'false sharing' result from?
Does it mean that updates to cached array will invalidate the cache?
Or it means if one core updated its cache line, then it has to invalidate all other cores' cache lines? But if this is the case, even if we use a single variable result
to store the result (and use locks to synchronize the addition of result
) instead of using an array, this situation still can not be avoided (one core modifying result
variable still invalidates all other cores' result
)