【发布时间】:2016-09-14 05:15:27
【问题描述】:
我正在尝试学习 OpenMP 以并行化我的部分代码,并且我正在尝试弄清楚为什么使用 2 个线程而不是 1 个线程时它不会更快。这是代码的最小工作示例:
#include <iostream>
#include <omp.h>
using namespace std;
class My_class
{
public :
// Constructor
My_class(int nuIterations)
: prVar_(0),
nuIters_(nuIterations)
{} // Empty
// Do something expensive involving the class' private vars
void do_calculations()
{
for (int i=0;i<nuIters_;++i){
prVar_=prVar_+i+2*i+3*i+4*i-5*i-4*i;
}
}
// Retrieve result
double getResult()
{
return prVar_;
}
private:
double prVar_;
int nuIters_;
};
int main()
{
// Initialize one object for every thread
My_class *test_object1, *test_object2;
test_object1 = new My_class(1000000000);
test_object2 = new My_class(500000000);
// Set number of threads (use one line at a time)
omp_set_num_threads(1); // One thread executes in 11.5 real seconds
//omp_set_num_threads(2); // Two threads execute in 13.2 real seconds
double start = omp_get_wtime(); // Start timer
#pragma omp parallel sections // Do calculations in parallel
{
#pragma omp section
{
test_object1->do_calculations();
}
#pragma omp section
{
test_object2->do_calculations();
}
}// End of parallel sections
// Print results
double end = omp_get_wtime();
cout<<"Res 1 : "<<test_object1->getResult()<<endl;
cout<<"Res 2 : "<<test_object2->getResult()<<endl;
cout<<"Time : "<<end-start<<endl;
return 0;
}
使用 g++ myomp.cpp -O0 -std=c++11 -fopenmp 编译和运行它会为 1 个和 2 个线程提供以下执行时间:
- 1 个线程:11.5 秒
- 2 个线程:13.2 秒
有什么方法可以加快 2 个线程的速度吗? 我在 4 核 Intel i7-4600U 和 Ubuntu 上运行它。
编辑:更改了大部分帖子,使其遵循指导方针。
【问题讨论】:
-
您必须以minimal reproducible example 的形式向我们提供更多信息以及您的硬件规格,否则答案只是猜测。猜测包括:写入共享缓存行、受内存限制、隐式同步、使用您不知道的共享资源或其组合。
-
感谢您的评论,我将尝试制定一个合适的示例并编辑帖子!
-
完成,希望现在有意义!
-
如果有什么安慰的话,我的 iMac 1 线程需要 8.9 秒,2 线程需要 5.6 秒。我用
-O3编译。 -
我故意关闭优化以避免影响结果,我的实际代码是使用 O3 编译的,但很难重现.. :(
标签: c++ multithreading openmp