Estimating the value of Pi using Monte Carlo | Parallel Computing Method
Given two integers N and K representing number of trials and number of total threads in parallel processing. The task is to find the estimated value of PI using the Monte Carlo algorithm using the Open Multi-processing (OpenMP) technique of parallelizing sections of the program.
Examples:
Input: N = 100000, K = 8
Output: Final Estimation of Pi = 3.146600
Input: N = 10, K = 8
Output: Final Estimation of Pi = 3.24
Input: N = 100, K = 8
Output: Final Estimation of Pi = 3.0916
Approach: The above given problem Estimating the value of Pi using Monte Carlo is already been solved using standard algorithm. Here the idea is to use parallel computing using OpenMp to solve the problem. Follow the steps below to solve the problem:
- Initialize 3 variables say x, y, and d to store the X and Y co-ordinates of a random point and the square of the distance of the random point from origin.
- Initialize 2 variables say pCircle and pSquare with values 0 to store the points lying inside circle of radius 0.5 and square of side length 1.
- Now starts the parallel processing with OpenMp together with reduction() of the following section:
- Iterate over the range [0, N] and find x and y in each iteration using srand48() and drand48() then find the square of distance of point (x, y) from origin and then if the distance is less than or equal to 1 then increment pCircle by 1.
- In each iteration of the above step, increment the count of pSquare by 1.
- Finally, after the above step calculate the value of estimated pi as below and then print the obtained value.
- Pi = 4.0 * ((double)pCircle / (double)(pSquare))
Below is the implementation of the above approach:
C++
#include <iostream>
using namespace std;
void monteCarlo( int N, int K)
{
double x, y;
double d;
int pCircle = 0;
int pSquare = 0;
int i = 0;
#pragma omp parallel firstprivate(x, y, d, i) reduction(+ : pCircle, pSquare) num_threads(K)
{
srand48(( int ) time (NULL));
for (i = 0; i < N; i++)
{
x = ( double )drand48();
y = ( double )drand48();
d = ((x * x) + (y * y));
if (d <= 1)
{
pCircle++;
}
pSquare++;
}
}
double pi = 4.0 * (( double )pCircle / ( double )(pSquare));
cout << "Final Estimation of Pi = " << pi;
}
int main()
{
int N = 100000;
int K = 8;
monteCarlo(N, K);
}
|
C
#include <omp.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void monteCarlo( int N, int K)
{
double x, y;
double d;
int pCircle = 0;
int pSquare = 0;
int i = 0;
#pragma omp parallel firstprivate(x, y, d, i) reduction(+ : pCircle, pSquare) num_threads(K)
{
srand48(( int ) time (NULL));
for (i = 0; i < N; i++) {
x = ( double )drand48();
y = ( double )drand48();
d = ((x * x) + (y * y));
if (d <= 1) {
pCircle++;
}
pSquare++;
}
}
double pi = 4.0 * (( double )pCircle / ( double )(pSquare));
printf ( "Final Estimation of Pi = %f\n" , pi);
}
int main()
{
int N = 100000;
int K = 8;
monteCarlo(N, K);
}
|
Java
import java.util.*;
class GFG {
static void monteCarlo( int N, int K)
{
double x, y;
double d;
int pCircle = 0 ;
int pSquare = 0 ;
Random rand = new Random();
for ( int i = 0 ; i < N; i++) {
x = rand.nextDouble();
y = rand.nextDouble();
d = ((x * x) + (y * y));
if (d <= 1 ) {
pCircle++;
}
pSquare++;
}
double pi
= 4.0 * (( double )pCircle / ( double )(pSquare));
System.out.println( "Final Estimation of Pi = "
+ pi);
}
public static void main(String[] args)
{
int N = 100000 ;
int K = 8 ;
monteCarlo(N, K);
}
}
|
Python3
import random
import time
def monteCarlo(N, K):
x = 0
y = 0
d = 0
pCircle = 0
pSquare = 0
random.seed(time.time())
for i in range (N):
x = random.random()
y = random.random()
d = (x * x) + (y * y)
if d < = 1 :
pCircle + = 1
pSquare + = 1
pi = 4.0 * (pCircle / pSquare)
print ( "Final Estimation of Pi = " , pi)
N = 100000
K = 8
monteCarlo(N, K)
|
C#
using System;
namespace MonteCarloPi {
class GFG
{
static void monteCarlo( int N, int K)
{
double x, y;
double d;
int pCircle = 0;
int pSquare = 0;
Random rand = new Random();
for ( int i = 0; i < N; i++) {
x = rand.NextDouble();
y = rand.NextDouble();
d = ((x * x) + (y * y));
if (d <= 1) {
pCircle++;
}
pSquare++;
}
double pi
= 4.0 * (( double )pCircle / ( double )(pSquare));
Console.WriteLine( "Final Estimation of Pi = " + pi);
}
static void Main( string [] args)
{
int N = 100000;
int K = 8;
monteCarlo(N, K);
}
}
}
|
Javascript
function monteCarlo(N, K) {
let x = 0;
let y = 0;
let d = 0;
let pCircle = 0;
let pSquare = 0;
let pi;
for (let i = 0; i < N; i++) {
x = Math.random();
y = Math.random();
d = (x * x) + (y * y);
if (d <= 1) {
pCircle++;
}
pSquare++;
pi = 4.0 * (pCircle / pSquare);
}
console.log( "Final Estimation of Pi = " + pi);
}
const N = 100000;
const K = 8;
monteCarlo(N, K);
|
Output
Final Estimation of Pi = 3.146600
Output of above C program
Time Complexity: O(N*K)
Auxiliary Space: O(1)
Last Updated :
15 Feb, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...