可以将std :: atomic与OpenMP安全使用 [英] Can std::atomic be safely used with OpenMP
问题描述
我目前正在尝试学习使用OpenMP的知识,我有一个问题. 这样做是否安全:
I'm currently trying to learn ow to use OpenMP and I have a question. Is it safe to do something like that :
std::atomic<double> result;
#pragma omp parallel for
for(...)
{
result+= //some stuff;
}
或者我应该使用:
double result;
#pragma omp parallel for
for(...)
{
double tmp=0;
//some stuff;
#pragma omp atomic
result+=tmp;
}
谢谢!
我知道使用数组处理最简单的方法,但是我问是因为我很好奇
Edit : I know the most simple way to handle that is using an array, but Im asking because I'm curious
推荐答案
正式地,没有.在实践中,可能.
Officially, no. In practice, probably.
虽然预期OpenMP规范的未来版本将解决以下功能,但当前使用它们可能会导致未指定的行为.
While future versions of the OpenMP specification are expected to address the following features, currently their use may result in unspecified behavior.
-
并发性
Concurrency
对标准库的添加
C ++ 11库
但是,取决于您使用的OpenMP运行时的实现,可能可以.实际上,LLVM OpenMP运行时甚至使用 std::atomic
来实现一些OpenMP规范.
However, depending on the implementation of the OpenMP runtime you use, it might be alright. In fact, the LLVM OpenMP runtime even uses std::atomic
to implement some of the OpenMP specification.
最安全的选择是仅使用OpenMP提供的内容.使用std::atomic
可以执行的任何操作,也应该仅使用OpenMP即可实现.
The safest option though is to stick with using only what OpenMP provides. Anything you can do using std::atomic
you should also be able to achieve using only OpenMP.
这篇关于可以将std :: atomic与OpenMP安全使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!