使用 OpenMP 并行C++代码不会即兴发挥性能
parallelizing C++ code with OpenMP doesn't improove performance
我写了一个c++代码来计算一些数字。我试图使用OpenMP库来并行化它。它有3个嵌套的for循环,并行化的是最外层的。我用g++编译器在Linux上工作。代码可以工作,但问题是并行化并没有提高性能。在我的双核笔记本电脑(intel i5处理器关闭多线程)上,用2个线程启动它比只用一个线程执行它要花更多的时间。
下面是代码(使用:g++ source.cpp -fopenmp启动):#include <time.h>
clock_t tStart = clock();
#include <stdlib.h>
#include <iostream>
#include <iomanip>
#include <omp.h>
using namespace std;
int nThreads= 2; // switch to change the number of threads.
int main () {
int i, j, k, nz= 10;
double T= 1000, deltaT= 0.005, g= 9.80665, h=10, dz= h/nz, t, z, V, A;
int nT= T/deltaT;
#pragma omp parallel for private (j, k, t, z, V, A) num_threads(nThreads)
for (i=0; i<=nT; i++) {
t= i*deltaT;
for (j=0; j<=nz; ++j) {
z= dz*j;
for (k=0; k<=1000; k++) {
V= t*z*g*k;
A= z*g*k;
}
}
}
cout << "Time taken: " << setprecision(5) << (double)(clock() - tStart)/CLOCKS_PER_SEC << endl;
return 0;
}
好的,很好。我在测量cpu时间,结果是一致的。Time()函数给出了我需要知道的数字。问题解决了。谢谢所有人。
相关文章:
- OpenMP阵列性能较差
- OpenMP 嵌套循环处理性能
- 多个 OpenMP 线程读取(而不是写入)共享变量的性能成本?
- 在 C++ 中使用 OpenMP 并行化两个 for 循环不会提供更好的性能
- openMp的多个独立for循环的性能问题
- Linux上多核计算的低性能(openMP、boost::thread等)
- openmp:线程数量的增加会降低性能
- 为什么我的代码在不同的环境中具有OpenMP性能不同
- 在 OpenMP 中使用共享数据结构而不是私有数据结构时的性能差异
- openMP 性能不佳,共享错误
- 是否有可能通过使用openMP在fstream(文件读取)上获得一些性能提升
- openmp并行性能
- OpenMP线程多于工作对性能的影响是什么?
- OpenMP共享vs. firstprivate性能方面
- 如何提高OpenMP代码的性能?
- 用c++ OpenMP和文件传输并行化.性能问题
- 出乎意料的性能提升与OpenMP临界区
- OpenMP对性能的影响:私有指令vs.在构造中声明变量
- 在虚拟机上运行openMp算法造成的性能损失
- 在Visual Studio 2013中OpenMP的性能下降