简体   繁体   English

性能方面的OpenMP共享与firstprivate

[英]OpenMP shared vs. firstprivate performancewise

I have a #pragma omp parallel for loop inside a class method. 我在类方法中有一个#pragma omp parallel for循环。 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. 所有这些都在shared条款中声明。 My questions: 我的问题:

  • Performance wise should not make any difference declare these variables shared or firstprivate . 性能明智不应该有任何区别声明这些变量是sharedfirstprivate 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! 在此期间感谢您的建议!

  1. Well, they're not the same thing. 嗯,他们不是一回事。 With shared , they are shared between all the threads. 通过shared ,它们在所有线程之间共享。 With firstprivate , each thread gets it's own copy. 使用firstprivate ,每个线程都获得它自己的副本。 If you're only reading the variable, then it's better to leave it as shared as to avoid copying it. 如果您只是阅读变量,那么最好将其保留为shared以避免复制它。 (In C++, firstprivate will implicitly invoke the copy constructor.) (在C ++中, firstprivate将隐式调用复制构造函数。)

  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. 但如果你也写信,那么你需要确保你没有竞争条件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM