jeudi 17 novembre 2016

OpenMP, number of cores and accuracy of computation with random numbers

I have come across the interesting issue. I tried to evaluate the value of Pi using Monte-Carlo method with up to 12 CPU cores. And what I've found out was that accuracy of Pi has decreased in case of using 12 cores comparing to 4 cores.

Here are the results (which are stable, i.e. they are repetitive with each new run)

4 cores:

3.14159

12 cores:

3.1416

I have implemented OpenMP code with function

rand_r()

for random number generation (I know it is not very good, but it's ensured to be thread-safe). The seed had different value for each thread.

The full code is

#include <iostream>
#include <random>
#include <ctime>
#include "omp.h"
#include <stdlib.h>

using namespace std;

unsigned seed;

int main()
{
double start = time(0);

int n, N;
double x, y;



N = 1<<30;
n = 0;

double pi;



#pragma omp parallel private(x, y, seed)
{
seed = 25234 + 17 * omp_get_thread_num();

#pragma omp for reduction(+:n)
for (int i = 0; i < N; i++) {

    x = (double) rand_r(&seed) / (double) RAND_MAX;
    y = (double) rand_r(&seed) / (double) RAND_MAX;

    if (x*x + y*y <= 1)
       n++;
}

}
pi = 4. * n / (double) (N);

cout << pi << endl;

double stop = time(0);

cout << (stop - start) << endl;

return 0;
}

Is it reasonable to have worse accuracy while increasing the number of cores? Is it somehow connected with random number generation (in particular, with function rand_r)? Or is it about distribution of for-loop?




Aucun commentaire:

Enregistrer un commentaire