Yury Shkliaryk Yury Shkliaryk - 5 months ago 23
C++ Question

Simultaneous writing in the same memory in parallel omp loop

I want to implement the following function that marks some elements of array by 1.

void mark(std::vector<signed char>& marker)
#pragma omp parallel for schedule(dynamic, M)
for (int i = 0; i < marker.size; i++)
marker[i] = 0;
#pragma omp parallel for schedule(dynamic, M)
for (int i = 0; i < marker.size; i++)
marker[getIndex(i)] = 1; // is it ok ?

What will happen if we try to set value of the same element to 1 in different threads at the same time? Will it be normally set to 1 or this loop may lead to unexpected behavior?

Answer Source

If you write with different threads to the very same location, you get a race condition. This is not necessarily undefined behaviour, but nevertheless it need to be avoided.

Since, you write a "1" with all threads it might be ok, but if you write real data it is probably not.

Side note: In order to have a good numerical performance, you need to work on memory which is not too close to each other. If two threads are writing to two different elements in one cache line, this chunk of memory will be invalidated for all other threads. This will lead to a cache miss and will spoil your performance gain (parallel execution might be even slower than single threaded execution).

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download