1

When I am using OpenMP without functions with the reduction(+ : sum) , the OpenMP version works fine.

#include <iostream>
#include <omp.h>
using namespace std;

int sum = 0;
void summation()
{
    sum = sum + 1;
}

int main()
{
    int i,sum;

#pragma omp parallel for reduction (+ : sum)
    for(i = 0; i < 1000000000; i++)
        summation();

#pragma omp parallel for reduction (+ : sum)
    for(i = 0; i < 1000000000; i++)
        summation();

#pragma omp parallel for reduction (+ : sum)
    for(i = 0; i < 1000000000; i++)
        summation();

    std::cerr << "Sum is=" << sum << std::endl;
}

But when I am calling a function summation over a global variable, the OpenMP version is taking even more time than the sequential version.

I would like to know the reason for the same and the changes that should be made.

4

2 回答 2

0

summation函数不使用您要减少的 OMP 共享变量。修理它:

#include <iostream>
#include <omp.h>

void summation(int& sum) { sum++; }

int main()
{
    int sum;

#pragma omp parallel for reduction (+ : sum)
    for(int i = 0; i < 1000000000; ++i)
        summation(sum);

    std::cerr << "Sum is=" << sum << '\n';
}
于 2012-07-10T11:19:30.450 回答
0

同步访问这个变量所花费的时间将远远超过使用多个内核所获得的时间——它们都将无休止地相互等待,因为只有一个变量并且只有一个内核可以在一个时间。这种设计不具备并发性,您支付的所有同步只会增加运行时间。

于 2012-07-10T11:20:58.457 回答