8

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:

  • Performance wise should not make any difference declare these variables shared or firstprivate. Right?
  • Is the same true if I'm not careful about making variable not sharing the same cache line?
  • If one of the shared variables is a pointer and inside the thread I read a value through it, is there an aliasing problem like in ordinary loops?

Tomorrow I will try to profile my code. In the meantime thanks for your advice!

mvalle
  • 265
  • 3
  • 8

1 Answers1

13
  1. 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.)

  2. 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.

  3. 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.

Mysticial
  • 464,885
  • 45
  • 335
  • 332
  • False sharing does not occur when only reading the variable, that's my understanding on modern processors at least. So your 2nd point is very ambiguous, I'd suggest editing your answer to clearly tell that OP will _not_ be affected by false sharing and should not care about it. – Saiph Dec 21 '18 at 15:41