Parallel but slower

I use the monte carlo method to calculate pi and do the basic experience of parallel programming and openmp

the problem is that when I use 1 thread, iterations x always run faster than n threads, x iterations. Can someone tell me why?

For example, the code works like "a.out 1 1,000,000", where 1 is the threads and 1,000,000 iterations

include <omp.h>
include <stdio.h>
include <stdlib.h>
include <iostream>
include <iomanip>
include <math.h>

using namespace std;

int main (int argc, char *argv[]) {

double arrow_area_circle, pi;
float xp, yp;
int i, n;
double pitg= atan(1.0)*4.0; //for pi error
cout << "Number processors: " << omp_get_num_procs() << endl;

//Number of divisions
iterarions=atoi(argv[2]); 
arrow_area_circle = 0.0;

#pragma omp parallel num_threads(atoi(argv[1]))
{
srandom(omp_get_thread_num());

#pragma omp for private(xp, yp) reduction(+:arrow_area_circle) //*,/,-,+
for (i = 0; i < iterarions; i++) {
    xp=rand()/(float)RAND_MAX;
    yp=rand()/(float)RAND_MAX;

    if(pow(xp,2.0)+pow(yp,2.0)<=1) arrow_area_circle++;
}
}

pi = 4*arrow_area_circle / iterarions;
cout << setprecision(18) << "PI = " << pi << endl << endl;
cout << setprecision(18) << "Erro = " << pitg-pi << endl << endl;

return 0;
}
+5
source share
5 answers

, , , , . , . , - . , , .

, arrow_area_circle . , _area_circle , . arrow_area_circle ++, , . _ .

EDIT: .

+10

, - . , 1 / ( ), Pentium 4 , , Intel .) , . - , , :

  • arrow_area_circle , _area_circle ++ , . "private", _, , _area_circle .

  • rand(), , . , / ; , , rand() , rand() . , rand() , , / . arrow_area_circle ( ), rand(), . , . rand() .

, pow (,) , x * x. 300 . , .:)

+7

.

+2

rand() - . , .

+2

, , . - SPRNG

, , , .

+1