简体   繁体   English

OpenMP部分比单线程运行得慢

[英]OpenMP sections run slower than single thread

Why is the parallel application taking more time to execute than the one with the single thread? 为什么并行应用程序比单线程应用程序花费更多时间? I am using an 8 CPU computer with Ubuntu 14.04. 我正在使用带有Ubuntu 14.04的8 CPU计算机。 The code is just my simple way to test omp parallel sections, the aim later is to run two different functions in two different threads at the same time, so I do not want to use #pragma omp parallel for. 代码只是我测试omp并行部分的简单方法,后来的目的是在两个不同的线程中同时运行两个不同的函数,所以我不想使用#pragma omp parallel for。

parallel: 平行:

int main()
{
int k = 0;
int m = 0;

omp_set_num_threads(2);

#pragma omp parallel
{
    #pragma omp sections
    {
        #pragma omp section
        {
            for( k = 0; k < 1e9; k++ ){};
        }

        #pragma omp section
        {
            for( m = 0; m < 1e9; m++ ){};
        }
    }
}

return 0;

}

and the single thread: 和单线程:

int main()
{
int m = 0;
int k = 0;

for( k = 0; k < 1e9; k++ ){};


for( m = 0; m < 1e9; m++ ){};

return 0;

}

If the compiler would not optimise the loops, then the parallel code would suffer from false sharing because m and k are very likely to end up in the same cache line. 如果编译器不优化循环,则并行代码将遭受错误共享,因为mk很可能最终在同一缓存行中。 Make the variables private : 将变量设为private

#pragma omp parallel private(k,m)
{
    #pragma omp sections
    {
        #pragma omp section
        {
            for( k = 0; k < 1e9; k++ ){};
        }

        #pragma omp section
        {
            for( m = 0; m < 1e9; m++ ){};
        }
    }
}

At high optimisation levels, the compiler could drop the loops altogether. 在高优化级别,编译器可以完全放弃循环。 But then the parallel version will still have the added overhead of spawning the OpenMP worker threads and joining them afterwards, which will make it slower than the sequential version. 但是,并行版本仍然会产生额外的开销,产生OpenMP工作线程并在之后加入它们,这将使它比顺序版本慢。

In above test code compiler itself optimizing the code. 在上面的测试代码编译器本身优化代码。 You need to change your test code. 您需要更改测试代码。 Depending on number of thread you are creating also add an overhead. 根据您创建的线程数量,还会增加开销。 Also refer, Amdahl's Law. 同时参考Amdahl定律。

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

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