patterncMinor
Monte Carlo estimation of π
Viewed 0 times
monteestimationcarlo
Problem
My C program uses the Monte Carlo method to approximate the mathematical constant π, the ratio of a circle's circumference to its diameter (and, importantly for this code, 4 times the ratio of a circle's area to that of its bounding square).
This estimation executes more slowly when parallelised using OpenMP than it does when executed in a single thread.
I think that my random generator function is not thread safe, and that may be the cause of the slowdown.
```
#include
#include
#include
#include
#define DART 1000000 // Number of darts each player throws
// Number of random dots in the square[-1,+1]
#define MAXPLAYER 8 // Maximume number of participant players
// Number of threads
/**
Generates a random number between two params given
@param: random number scope
@return: desired random number
*/
long double fRand(long double fMin, long double fMax)
{
long double f = (double)rand() / RAND_MAX;
return fMin + f * (fMax - fMin);
}
/**
Generates random dots on the board
@param: playerDarts, total number of darts to throw
@return: score, number of darts thrown in the circle
*/
int player(int playersDarts)
{
srand(time(NULL));
long double pi, x, y;
int score = 0;
for (int i = 0; i < playersDarts; i++)
{
x = fRand(-1.0, 1.0);
y = fRand(-1.0, 1.0);
if (xx + yy < 1.0)
score++;
}
return score;
}
void main()
{
long double pi;
long const double REAL_PI = 3.141592653589;
int score = 0, playersDarts;
////////////////////////////////////////////////////
// Parallel //
////////////////////////////////////////////////////
// devide the total number of DARTS between players
playersDarts = DART / MAXPLAYER;
double beginParallel = omp_get_wtime();
#pragma omp parallel for
for (int i = 1; i <= MAXPLAYER; i++)
score += player(playersDarts);
double endParallel = omp_get_wtime();
pi = 4.0 * ((lo
This estimation executes more slowly when parallelised using OpenMP than it does when executed in a single thread.
I think that my random generator function is not thread safe, and that may be the cause of the slowdown.
```
#include
#include
#include
#include
#define DART 1000000 // Number of darts each player throws
// Number of random dots in the square[-1,+1]
#define MAXPLAYER 8 // Maximume number of participant players
// Number of threads
/**
Generates a random number between two params given
@param: random number scope
@return: desired random number
*/
long double fRand(long double fMin, long double fMax)
{
long double f = (double)rand() / RAND_MAX;
return fMin + f * (fMax - fMin);
}
/**
Generates random dots on the board
@param: playerDarts, total number of darts to throw
@return: score, number of darts thrown in the circle
*/
int player(int playersDarts)
{
srand(time(NULL));
long double pi, x, y;
int score = 0;
for (int i = 0; i < playersDarts; i++)
{
x = fRand(-1.0, 1.0);
y = fRand(-1.0, 1.0);
if (xx + yy < 1.0)
score++;
}
return score;
}
void main()
{
long double pi;
long const double REAL_PI = 3.141592653589;
int score = 0, playersDarts;
////////////////////////////////////////////////////
// Parallel //
////////////////////////////////////////////////////
// devide the total number of DARTS between players
playersDarts = DART / MAXPLAYER;
double beginParallel = omp_get_wtime();
#pragma omp parallel for
for (int i = 1; i <= MAXPLAYER; i++)
score += player(playersDarts);
double endParallel = omp_get_wtime();
pi = 4.0 * ((lo
Solution
In the code below you must realise that
OpenMP is not a magic box that will automatically make everything faster. You still need to think about synchronisation and proper algorithms. Also starting a thread does have some overhead and if the work done by the thread is small in comparison to the overhead, then you're slowing the program down.
There are plenty of resources on parallel programming on the internet, I recommend starting with doing your threading manually until you know how it works and then start using tools like OpenMP.
score is a variable that is shared between the threads. Which means that it requires synchronization, as you have omitted this synchornisation you will have undefined behaviour (incorrect result most likely).for (int i = 1; i <= MAXPLAYER; i++)
score += player(playersDarts);OpenMP is not a magic box that will automatically make everything faster. You still need to think about synchronisation and proper algorithms. Also starting a thread does have some overhead and if the work done by the thread is small in comparison to the overhead, then you're slowing the program down.
There are plenty of resources on parallel programming on the internet, I recommend starting with doing your threading manually until you know how it works and then start using tools like OpenMP.
Code Snippets
for (int i = 1; i <= MAXPLAYER; i++)
score += player(playersDarts);Context
StackExchange Code Review Q#160421, answer score: 7
Revisions (0)
No revisions yet.