Why OpenMP under ubuntu 12.04 is slower than serial version

前端 未结 3 1159
别跟我提以往
别跟我提以往 2020-12-03 16:13

I\'ve read some other questions on this topic. However, they didn\'t solve my problem anyway.

I wrote the code as following and I got pthread version an

3条回答
  •  天命终不由人
    2020-12-03 16:58

    There is nothing wrong with OpenMP in your case. What is wrong is the way you measure the elapsed time.

    Using clock() to measure the performance of multithreaded applications on Linux (and most other Unix-like OSes) is a mistake since it does not return the wall-clock (real) time but instead the accumulated CPU time for all process threads (and on some Unix flavours even the accumulated CPU time for all child processes). Your parallel code shows better performance on Windows since there clock() returns the real time and not the accumulated CPU time.

    The best way to prevent such discrepancies is to use the portable OpenMP timer routine omp_get_wtime():

    double start = omp_get_wtime();
    #pragma omp parallel for
    for(int n = 0; n < sizen; ++n)
        sinTable[n] = std::sin(2 * M_PI * n / sizen);
    double finish = omp_get_wtime();
    printf("from omp: %lf\n", finish - start);
    

    For non-OpenMP applications, you should use clock_gettime() with the CLOCK_REALTIME clock:

    struct timespec start, finish;
    clock_gettime(CLOCK_REALTIME, &start);
    #pragma omp parallel for
    for(int n = 0; n < sizen; ++n)
        sinTable[n] = std::sin(2 * M_PI * n / sizen);
    clock_gettime(CLOCK_REALTIME, &finish);
    printf("from omp: %lf\n", (finish.tv_sec + 1.e-9 * finish.tv_nsec) -
                              (start.tv_sec + 1.e-9 * start.tv_nsec));
    

提交回复
热议问题