I have a #pragma omp parallel for
loop inside a class method. Each thread readonly accesses few method local variables, few call private data and a method's parameter. All of them are declared in a shared
clause.
My questions:
shared
or firstprivate
. Right?Tomorrow I will try to profile my code. In the meantime thanks for your advice!
Well, they're not the same thing. With shared
, they are shared between all the threads. With firstprivate
, each thread gets it's own copy. If you're only reading the variable, then it's better to leave it as shared
as to avoid copying it. (In C++, firstprivate
will implicitly invoke the copy constructor.)
Correct, multiple threads reading and writing to values that sit on the same cacheline is called false sharing. The cache line will bounce back and forth between the cores that are accessing it - which can result in significant slowdown if it happens often enough.
If you're just reading data through the shared pointer, then there shouldn't be a problem. But if you're also writing to it, then you need to make sure you don't have a race condition.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With