OpenMP更多线程大大增加了总时间

时间:2016-11-16 04:11:52

标签: c++ multithreading openmp

我正在为课堂写一个Pi估算器。关键是使用OpenMP估算Pi并分析使用三个单独的时间表(静态,动态和引导)提供的加速。但是当我添加线程时,我的总时间猛增。我1个线程的总时间约为21秒,2个线程的总时间约为145秒。我无法弄清楚原因。这是我的代码:

#include <stdio.h>
#include <time.h>
#include <ctime>
#include <omp.h>
#include <assert.h>
#include <cstdlib>
#include <string>
#include <iostream>
#include <stdlib.h>

using namespace std;


void computePi(int, int);

int main(int argc, char *argv[])
{
cout.precision(20);
omp_set_dynamic(0);
int i, p;
int  n;

// loop {number of iterations} [number of threads]
if (argc > 1)
{
    n = atoll(argv[1]);
    p = atoi(argv[2]);
}
else
{
    n = 10000000;
    p = 8;
}
printf("Debug: dart throws = %d \n", n);
printf("Debug: number of requested threads = %d\n", p);

omp_set_num_threads(p);

double time = omp_get_wtime();

//dispArray(a,n);
computePi(n, p);

time = omp_get_wtime() - time;
printf("Total time = %f seconds \n ", time);

return 0;
}


void computePi(int n, int p) {

omp_set_num_threads(p);


int i;
int hits = 0;
srand(time(NULL));
double timeStatic = omp_get_wtime();
#pragma omp parallel for shared(i) schedule(static) reduction(+:hits)
    for (i = 0; i<n; i++)
    {
        float r, x, xdiff, y, ydiff;

        x = (float)rand() / (float)RAND_MAX;
        y = (float)rand() / (float)RAND_MAX;

        if (y > .50)
        {
            ydiff = y - .50;
        }
        else
        {
            ydiff = .50 - y;
        }
        if (x > .50)
        {
            xdiff = x - .50;
        }
        else
        {
            xdiff = .50 - x;
        }
        xdiff *= xdiff;
        ydiff *= ydiff;
        r = sqrt(ydiff + xdiff);


        if (r <= .50)
        {
            hits += 1;
        }
    }
    timeStatic = omp_get_wtime() - timeStatic;
    float percentage;
    percentage = (float)hits / (float)n;

    cout << "Static Loop" << endl;
    cout << "Hit Percentage: " << percentage * 100 << "%" << endl;
    cout << "Pi Estimation: " << percentage * 4 << endl;
    cout << "Time Taken: " << timeStatic << endl << endl;

    hits = 0;
    double timeDynamic = omp_get_wtime();
#pragma omp parallel for shared(i) schedule(dynamic) reduction(+:hits)
    for (i = 0; i<n; i++)
    {
        float r, x, xdiff, y, ydiff;

        x = (float)rand() / (float)RAND_MAX;
        y = (float)rand() / (float)RAND_MAX;

        if (y > .50)
        {
            ydiff = y - .50;
        }
        else
        {
            ydiff = .50 - y;
        }
        if (x > .50)
        {
            xdiff = x - .50;
        }
        else
        {
            xdiff = .50 - x;
        }
        xdiff *= xdiff;
        ydiff *= ydiff;
        r = sqrt(ydiff + xdiff);


        if (r <= .50)
        {
            hits += 1;
        }
    }
    timeDynamic = omp_get_wtime() - timeDynamic;
    percentage = (float)hits / (float)n;


    cout << "Dynamic Loop" << endl;
    cout << "Hit Percentage: " << percentage * 100 << "%" << endl;
    cout << "Pi Estimation: " << percentage * 4 << endl;
    cout << "Time Taken: " << timeDynamic << endl << endl;

    hits = 0;
    double timeGuided = omp_get_wtime();
#pragma omp parallel for shared(i) schedule(guided) reduction(+:hits)
    for (i = 0; i<n; i++)
    {
        float r, x, xdiff, y, ydiff;

        x = (float)rand() / (float)RAND_MAX;
        y = (float)rand() / (float)RAND_MAX;

        if (y > .50)
        {
            ydiff = y - .50;
        }
        else
        {
            ydiff = .50 - y;
        }
        if (x > .50)
        {
            xdiff = x - .50;
        }
        else
        {
            xdiff = .50 - x;
        }
        xdiff *= xdiff;
        ydiff *= ydiff;
        r = sqrt(ydiff + xdiff);


        if (r <= .50)
        {
            hits += 1;
        }
    }
    timeGuided = omp_get_wtime() - timeGuided;
    percentage = (float)hits / (float)n;

    cout << "Guided Loop" << endl;
    cout << "Hit Percentage: " << percentage * 100 << "%" << endl;
    cout << "Pi Estimation: " << percentage * 4 << endl;
    cout << "Time Taken: " << timeGuided << endl << endl;



    return;

}

任何帮助都将不胜感激。

0 个答案:

没有答案