OpenMP无法预测的开销

时间:2014-12-02 02:36:08

标签: c++ performance openmp

我有一个简单的OpenMP代码,它运行四个空线程并报告时间:

#include "omp.h"
#include <sys/time.h>
#include <cstdio>
double start_time;

void process_thread() {
}

int main(){
    struct timeval tv;
    gettimeofday(&tv, NULL);
    start_time = 1000*((double)tv.tv_sec + 1e-6*(double)tv.tv_usec);
    printf("Beginning time: %.3fms   ",start_time-start_time);

    #pragma omp parallel for schedule(static,1)
    for(int i=0;i<4;i++)
        process_thread();

    gettimeofday(&tv, NULL);
    double cur_time = 1000*((double)tv.tv_sec + 1e-6*(double)tv.tv_usec);
    printf("Finishing    time:  %.3fms\n\n",cur_time-start_time);
}

没有Openmp,运行时间非常小且一致:

Beginning time: 0.000ms   Finishing time: 0.050ms
Beginning time: 0.000ms   Finishing time: 0.050ms
Beginning time: 0.000ms   Finishing time: 0.049ms
Beginning time: 0.000ms   Finishing time: 0.049ms
Beginning time: 0.000ms   Finishing time: 0.050ms
Beginning time: 0.000ms   Finishing time: 0.049ms
Beginning time: 0.000ms   Finishing time: 0.051ms
Beginning time: 0.000ms   Finishing time: 0.063ms
Beginning time: 0.000ms   Finishing time: 0.050ms
Beginning time: 0.000ms   Finishing time: 0.051ms

然而,当我使用OpenMP时,它需要更长的时间,而且时间变得非常不一致:

Beginning time: 0.000ms   Finishing time: 0.497ms
Beginning time: 0.000ms   Finishing time: 1.001ms
Beginning time: 0.000ms   Finishing time: 0.484ms
Beginning time: 0.000ms   Finishing time: 16.728ms
Beginning time: 0.000ms   Finishing time: 4.205ms
Beginning time: 0.000ms   Finishing time: 6.041ms
Beginning time: 0.000ms   Finishing time: 3.754ms
Beginning time: 0.000ms   Finishing time: 4.126ms
Beginning time: 0.000ms   Finishing time: 0.230ms
Beginning time: 0.000ms   Finishing time: 0.183ms
Beginning time: 0.000ms   Finishing time: 2.438ms
Beginning time: 0.000ms   Finishing time: 0.248ms

我的印象是OpenMP开销将在150毫秒的范围内,但这似乎更多。

我使用带有6核的XEON E5 1650和Ubuntu 12.04。我使用gcc 4.6.3。我尝试过其他OpenMP配置,但静态最适合这种情况。

造成这种波动的原因是什么?这是编译器,OpenMP运行时,操作系统还是别的错误?我该如何改进呢?

0 个答案:

没有答案