c++ - 为什么ubuntu 12.04下的OpenMP比串口版本慢

标签 c++ ubuntu pthreads openmp

我已经阅读了有关此主题的其他一些问题。 然而,他们并没有解决我的问题。

我编写的代码如下,我得到的 pthread 版本和 omp 版本都比串行版本慢。我很困惑。

编译环境:

Ubuntu 12.04 64bit 3.2.0-60-generic
g++ (Ubuntu 4.8.1-2ubuntu1~12.04) 4.8.1

CPU(s):                2
On-line CPU(s) list:   0,1
Thread(s) per core:    1
Vendor ID:             AuthenticAMD
CPU family:            18
Model:                 1
Stepping:              0
CPU MHz:               800.000
BogoMIPS:              3593.36
L1d cache:             64K
L1i cache:             64K
L2 cache:              512K
NUMA node0 CPU(s):     0,1

编译命令:

g++ -std=c++11 ./eg001.cpp -fopenmp

#include <cmath>
#include <cstdio>
#include <ctime>
#include <omp.h>
#include <pthread.h>

#define NUM_THREADS 5
const int sizen = 256000000;

struct Data {
    double * pSinTable;
    long tid;
};

void * compute(void * p) {
    Data * pDt = (Data *)p;
    const int start = sizen * pDt->tid/NUM_THREADS;
    const int end = sizen * (pDt->tid + 1)/NUM_THREADS;
    for(int n = start; n < end; ++n) {
        pDt->pSinTable[n] = std::sin(2 * M_PI * n / sizen);
    }
    pthread_exit(nullptr);
}

int main()
{
    double * sinTable = new double[sizen];
    pthread_t threads[NUM_THREADS];
    pthread_attr_t attr;
    pthread_attr_init(&attr);
    pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);

    clock_t start, finish;

    start = clock();
    int rc;
    Data dt[NUM_THREADS];
    for(int i = 0; i < NUM_THREADS; ++i) {
        dt[i].pSinTable = sinTable;
        dt[i].tid = i;
        rc = pthread_create(&threads[i], &attr, compute, &dt[i]);
    }//for
    pthread_attr_destroy(&attr);
    for(int i = 0; i < NUM_THREADS; ++i) {
        rc = pthread_join(threads[i], nullptr);
    }//for
    finish = clock();
    printf("from pthread: %lf\n", (double)(finish - start)/CLOCKS_PER_SEC);

    delete sinTable;
    sinTable = new double[sizen];

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

    delete sinTable;
    sinTable = new double[sizen];

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

    delete sinTable;

    pthread_exit(nullptr);
    return 0;
}

输出:

from pthread: 21.150000
from omp: 20.940000
from serial: 20.800000

我想知道这是否是我的代码的问题,所以我使用 pthread 来做同样的事情。

但是,我完全错了,我想知道这是否可能是Ubuntu在OpenMP/pthread上的问题。

我有一个 friend 也有AMD CPU和Ubuntu 12.04,并且遇到了同样的问题,所以我可能有理由相信这个问题不仅限于我。

如果有人和我有同样的问题,或者对这个问题有一些线索,提前致谢。

<小时/>

如果代码不够好,我会运行基准测试并将结果粘贴到此处:

http://pastebin.com/RquLPREc

基准网址:http://www.cs.kent.edu/~farrell/mc08/lectures/progs/openmp/microBenchmarks/src/download.html

<小时/>

新信息:

我在 Windows(没有 pthread 版本)上使用 VS2012 运行了代码。

我使用了 sizen 的 1/10,因为 Windows 不允许我分配那么大的内存,结果是:

from omp: 1.004
from serial: 1.420
from FreeNickName: 735 (this one is the suggestion improvement by @FreeNickName)

这是否表明可能是 Ubuntu 操作系统 的问题??

<小时/> <小时/>

通过使用可在操作系统之间移植的omp_get_wtime函数解决了问题。请参阅 Hristo Iliev 的回答。

<小时/>

FreeNickName 对这个有争议的话题进行了一些测试。

(抱歉,我需要在 Ubuntu 上测试它,因为 Windows 是我 friend 的。)

--1-- 从 delete 更改为 delete [] :(但没有 memset)(-std=c++11 -fopenmp)

from pthread: 13.491405
from omp: 13.023099
from serial: 20.665132
from FreeNickName: 12.022501

--2-- new 之后立即使用 memset:(-std=c++11 -fopenmp)

from pthread: 13.996505
from omp: 13.192444
from serial: 19.882127
from FreeNickName: 12.541723

--3-- new 之后立即使用 memset:(-std=c++11 -fopenmp -march=native -O2)

from pthread: 11.886978
from omp: 11.351801
from serial: 17.002865
from FreeNickName: 11.198779

--4-- new 之后立即使用 memset,并将 FreeNickName 的版本放在 OMP 之前,版本为:(-std=c++11 -fopenmp -march=native -O2)

from pthread: 11.831127
from FreeNickName: 11.571595
from omp: 11.932814
from serial: 16.976979

--5-- new后立即使用memset,并将FreeNickName的版本放在OMP版本之前,并将NUM_THREADS设置为5而不是2(我是双核)。

from pthread: 9.451775
from FreeNickName: 9.385366
from omp: 11.854656
from serial: 16.960101

最佳答案

在您的情况下,OpenMP 没有任何问题。问题在于您测量耗时的方式。

使用clock()来测量Linux(以及大多数其他类Unix操作系统)上多线程应用程序的性能是一个错误,因为它不返回挂钟(实时)时间,而是返回所有进程线程的累积 CPU 时间(在某些 Unix 版本上,甚至是所有子进程的累积 CPU 时间)。您的并行代码在 Windows 上显示出更好的性能,因为 clock() 返回实际时间而不是累积的 CPU 时间。

防止此类差异的最佳方法是使用可移植 OpenMP 计时器例程 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);

对于非 OpenMP 应用程序,您应该将 clock_gettime()CLOCK_REALTIME 时钟结合使用:

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));

关于c++ - 为什么ubuntu 12.04下的OpenMP比串口版本慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23443013/

相关文章:

c++ - 研究 C++ 警告的后果

java - 如何重新排列数组中的数据,使两个相似的项目不相邻?

python - 更改 mininet 主机中的主机名

ubuntu - 如何安装 esl-erlang、erlang-crypto 和 erlang-tools?

c - 未声明的标识符错误

c++ - 如何从随机数中取一个名字?

表示库中项目的类的 c++ 派生类

ubuntu - 如何解决 ubuntu 中 phantom js 的权限被拒绝错误?

linux - 单一提供者,单一消费者。哪个适合条件变量 : pthread_cond_t, sem_t 或 pthread_mutex_t?

c - 在 C 中使用信号量进行多线程