Qt OpenMP并行程序

在Qt的pro文件中加入:
QMAKE_CXXFLAGS += -fopenmp
LIBS += -lgomp -lpthread

我简单测了一下,单线程执行N次加法  和4线程执行:

Qt OpenMP并行程序_第1张图片

部分代码如下:

void Widget ::on_pushButton_clicked ()
{
    clock_t myClock = -clock();
 
  
    quint64 sum = 0;
 
  
    for(int k=1; k<=1000; k++)
    for(int j=1; j<=10000000; j++)
    {
        sum = 0;
        for(int i=1; i<=1000000; i++)
            sum += i;
    }
 
  
    myClock += clock();
 
  
    ui->label_sum->setText(QObject::tr("%1").arg(sum));
    ui->label_time->setText(QObject::tr("%1 ms").arg(myClock));
}
 
  
void Widget::on_pushButton_2_clicked()
{
    clock_t myClock = -clock();
 
  
    quint64 sum = 0;
 
  
 
  
#pragma omp parallel for num_threads(4) reduction(+:sum)
    for(int k=1; k<=1000; k++)
    for(int j=1; j<=10000000; j++)
    {
        sum = 0;
        for(int i=1; i<=1000000; i++)
            sum += i;
    }
 
  
    myClock += clock();
 
  
    ui->label_sum4->setText(QObject::tr("%1").arg(sum/4));
    ui->label_time4->setText(QObject::tr("%1 ms").arg(myClock));
}
 
  

那个Qt creator 还不错嘛...支持OMP扩展..我喜欢...

你可能感兴趣的:(扩展,qt,parallel)