[英]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: 我的问题:
shared
or firstprivate
. 性能明智不应该有任何区别声明这些变量是shared
或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. 通过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
将隐式调用复制构造函数。)
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. 但如果你也写信,那么你需要确保你没有竞争条件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.