简体   繁体   English

omp_set_num_threads(1)慢于没有openmp的原因

[英]Reasons for omp_set_num_threads(1) slower than no openmp

I believe everyone agree with the title of this post. 我相信每个人都同意该职位的标题。 Can someone point me the reason ? 有人可以指出原因吗? Any reference to that like book etc ? 对像书之类的东西有什么参考吗? I have tried to find but no luck. 我试图找到但没有运气。

I believe the reason is something about openmp has a synchronization overhead that no openmp project doesn't have. 我相信原因是关于openmp的事情,它具有同步开销,这是openmp项目所没有的。

Hope someone can expand the reason more? 希望有人可以进一步扩大原因?

Thanks 谢谢

While there is some overhead at runtime from using OpenMP even with only one thread, the more important issue is likely to be that the code transformations that the compiler has to perform to generate OpenMP code (in particular outlining the parallel region code into separate functions [done by gcc and icc; PGI do something different...]) will be affecting other code optimizations (like vectorization). 尽管仅使用一个线程就使用OpenMP在运行时会产生一些开销,但更重要的问题很可能是编译器必须执行的代码转换才能生成OpenMP代码(特别是将并行区域代码概述为单独的函数[由gcc和icc完成; PGI有所不同...])将影响其他代码优化(例如矢量化)。 Information that the compiler has in a single function that allows optimizations potentially gets lost when parts of the code are executed in the outlined functions, so the generated code may be worse. 当在概述的函数中执行部分代码时,编译器在允许优化的单个函数中拥有的信息可能会丢失,因此生成的代码可能会更糟。

As Mystical explained, it's likely due to the OpenMP overhead. 正如Mystical解释的那样,这很可能是由于OpenMP开销。 I have tried to get around this by doing for example: 我尝试通过例如以下方式解决此问题:

#pragma omp parallel for if(nthreads>1)

I thought this would only use the OpenMP overhead if nthreads>1. 我以为如果nthreads> 1,这只会使用OpenMP开销。 However, at least in Visual Studio 2012, this also has significant overhead. 但是,至少在Visual Studio 2012中,这也有很大的开销。 Therefore, in order to properly compare single threaded and multi-threaded version of a function I define two versions of the functions with and without the OpenMP pragmas. 因此,为了正确比较函数的单线程和多线程版本,我定义了带有和不带有OpenMP编译指示的函数的两个版本。

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

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