使用OpenMP加速quadcore系统上4个线程的问题

时间:2012-09-14 11:46:14

标签: c++ multithreading openmp

我在使用OpenMP的quadcore系统上使用4个线程时遇到了加速问题。使用2个线程时效率接近1但是使用4个线程减少到一半,运行时间与使用2个线程运行代码时的运行时间大致相同。我在OpenMP论坛上搜索过,之前我发现类似的问题是因为Inter turbo boost技术。请参阅此帖http://openmp.org/forum/viewtopic.php?f=3&t=1289&start=0&hilit=intel+turbo+boost

所以我试图在我的机器的所有4个处理器上禁用turbo boost但无法解决问题。

我只从上面的链接中获取了基准代码。

我有戴尔笔记本电脑,我的硬件/操作系统信息摘要如下:

OS : Linux3.0.0.12-generic , Ubuntu
KDE SC Version : 4.7.1

Processor: Intel(R) Core(TM) i7-2620M CPU @ 2.70GHz

请告诉我可能存在的其他问题,这些问题不允许我加速使用4个线程/核心。作为附加信息。我检查过所有4个线程都运行在不同的核心上。

期待您的回答。

代码:

#include <stdio.h>
#include <omp.h>
#include <math.h>


double estimate_pi(double radius, int nsteps){

   int i;
   double h=2*radius/nsteps;
   double sum=0;
   for (i=1;i<nsteps;i++){
      sum+=sqrt(pow(radius,2)-pow(-radius+i*h,2));
      //sum+=.5*sum;
   }
   sum*=h;
   sum=2*sum/(radius*radius);
   //printf("radius:%f --> %f\n",radius,sum);
   return sum;


}

int main(int argc, char* argv[]){


   double ser_est,par_est;
   long int radii_range;
   if (argc>1) radii_range=atoi(argv[1]);
   else radii_range=500;   

   int nthreads;
   if (argc>2) nthreads=atoi(argv[2]);
   else nthreads=omp_get_num_procs();

   printf("Estimating Pi by averaging %ld estimates.\n",radii_range);
   printf("OpenMP says there are %d processors available.\n",omp_get_num_procs());

   int r;
   double start, stop, serial_time, par_time;



   par_est=0;
   double tmp=0;
   ser_est=0;
   start=omp_get_wtime();
   for (r=1;r<=radii_range;r++){
      tmp=estimate_pi(r,1e6);
      ser_est+=tmp;
   }
   stop=omp_get_wtime();
   serial_time=stop-start;
   ser_est=ser_est/radii_range;

   omp_set_num_threads(nthreads);
   start=omp_get_wtime();
   #pragma omp parallel for private(r,tmp) reduction(+:par_est)
   for (r=1;r<=radii_range;r++){
      tmp=estimate_pi(r,1e6);
      par_est+=tmp;
   }
   stop=omp_get_wtime();
   par_time=stop-start;
   par_est=par_est/radii_range;

   printf("Serial Estimate: %f\nParallel Estimate:%f\n\n",ser_est,par_est);
   printf("Serial Time: %f\nParallel Time:%f\nNumber of Threads: %d\nSpeedup: %f\nEfficiency: %f\n",serial_time,par_time,nthreads,serial_time/par_time, serial_time/par_time/nthreads);


}

2 个答案:

答案 0 :(得分:5)

Core i7-2620M是具有HT的双核(因此是4个逻辑核)。 HT并不总能提高程序性能,而且改进在很大程度上取决于程序本身(对于某些内存密集型应用程序,它甚至可能会降级)。你绝对不应该期望4倍的加速,因为它没有4个物理核心。

如果你有时间,可以从这里读一下:http://software.intel.com/en-us/articles/performance-insights-to-intel-hyper-threading-technology/

答案 1 :(得分:-2)

有不同的问题。这是因为printf功能。

重新编写所有代码然后才找到它。我有一个8核心的新计算机,但开放的mp在一个CPU上只使用了4个线程,检查了所有内容:默认(无)查找错误,使用firstprivate进行只读变量,但仍然没有工作。 然后我忘了做rowid-row行的printf,然后就可以了!