Aqsha Padyani
Aqsha Padyani

Reputation: 11

OpenMP - Array Insertion without False Sharing

I'm working with OpenMP in C to parallelize my program. There is a section in my program that inserts a calculated value into an array. The code will be like this:

#pragma omp parallel for
for(i=0; i<bignumber; i++) {
    arr[i] = mycalc(i);
}

From what I've learned, I think this code will occur False Sharing problem at the array arr. There are several ways I found in order to avoid this problem, such as:

  1. Array padding
  2. Scheduling with chunks

Those 2 ways require me to know how big is my processor cache size. Suppose that I want to run my program on an unknown system (I don't know how big the cache size is). Are there any workarounds for this code that doesn't require me to know the cache size? Or maybe a C code that can read the cache size of the system the program is running on?

Upvotes: 0

Views: 1005

Answers (1)

Zulan
Zulan

Reputation: 22670

First, false sharing is a performance issue - not a correctness issue. You needn't avoid it at all cost for all data accesses - but you should avoid it for the majority of data accesses.

Your simple loop pattern is unproblematic. You can stick with the implementation's default. If you want, you can use schedule(static) - unless you specify a chunk size, OpenMP will assign only one big chunk to each thread. This means you have at most two cache lines (the borders) per thread that are affected by false sharing. Statistically this won't matter.

Starting with the largest possible chunk sizes is a good default. It's only if for you reduce the chunk size for other reasons, e.g. load balancing, where you have to be careful not to get too much false sharing. Keeping the chunk size as multiples of powers of two is usually a good idea.

You should be careful with patterns like:

data[omp_get_thread_num()] = ...;

This is very prone to false sharing. You should avoid globally allocating data where small per-thread data is stored adjacently.

Upvotes: 2

Related Questions