Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6920749
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T10:11:16+00:00 2026-05-27T10:11:16+00:00

This program estimates Pi by throwing random darts (sampling points) to a circle or

  • 0

This program estimates Pi by throwing random “darts” (sampling points) to a circle or radius=1 inscribed inside a square board of length=2. Using the relationship

Area of circle / Area of Square = Pi/4

we can estimate Pi using the same relationship expressed as

Darts Inside Circle / Darts Outside Circle = Pi/4

The program works fine when I specify NDARTS in a #define. However, when I specify NDARTS as a value that’s read via scanf and then broadcasted, it mysteriously gets stuck when more than one process is assigned via mpirun:

mpirun -np 1 ./pi_montecarlo.x

   Monte Carlo Method to estimate Pi 

Introduce Number of Darts 
10000
  Number of processes: 1 
  Number of darts: 10000 
Known value of PI  : 3.1415926535 
Estimated Value of PI  : 3.1484000000
Error Percentage   : 0.21668457
Time    : 0.00060296



mpirun -np 2 ./pi_montecarlo.x

Monte Carlo Method to estimate Pi 

Introduce Number of Darts 
10000
Number of processes: 2 
Number of darts: 10000 

^Stuck here.

Why? Is this some mpi-implementation-specific problem? Should I try another MPI implementation (I think I’m running lam)? Can you run this with at least 2 processes on your own box?

/*
mpicc -g -Wall -lm pi_montecarlo3.c -o pi_montecarlo.x 

mpirun -np 4 ./pi_montecarlo.x
*/

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#include <mpi.h>

#define MASTER 0
#define PI 3.1415926535

double pseudo_random (double a, double b) {
    double r; 
    r = ((b-a) * ((double) rand() / (double) RAND_MAX)) +a;
    return r; 
}

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

    int proc_id, 
        n_procs, 
        llimit,  
        ulimit,  
        n_circle, 
        i;      


    double pi_current, 
           pi_sum,     
           x,         
           y,         
           z,          
           error,      
           start_time, 
           end_time;   

    struct timeval stime;

    llimit = -1;
    ulimit = 1;
    n_circle =0; 

    MPI_Init(&argc, &argv); 

    MPI_Comm_rank (MPI_COMM_WORLD, &proc_id);
    MPI_Comm_size (MPI_COMM_WORLD, &n_procs);

    if (proc_id == MASTER){
        printf("\nMonte Carlo Method to estimate Pi \n\n");

            printf("Introduce Number of Darts \n");

            scanf("%lld",&NDARTS); 

        printf("  Number of processes: %d \n", n_procs);
        printf("  Number of darts: %lld \n", NDARTS);

            MPI_Bcast(&NDARTS, 1, MPI_LONG_LONG_INT, 0, MPI_COMM_WORLD);

            start_time = MPI_Wtime();
    }

    gettimeofday(&stime, NULL); 
    srand(stime.tv_usec * stime.tv_usec * stime.tv_usec * stime.tv_usec);

    for (i=1; i<=NDARTS;i++){
        x = pseudo_random(llimit, ulimit);
        y = pseudo_random(llimit, ulimit);

        z = pow(x,2) + pow(y,2);

        if (z<=1.0){
            n_circle++;
        }
    }

    pi_current = 4.0 * (double)n_circle / (double) NDARTS; 

    MPI_Reduce (&pi_current, &pi_sum, 1, MPI_DOUBLE, MPI_SUM, MASTER, MPI_COMM_WORLD);

       if (proc_id == MASTER) {
        pi_sum = pi_sum / n_procs;

        error = fabs ((pi_sum -PI) / PI) *100;

        end_time = MPI_Wtime();

        printf("Known value of PI  : %11.10f \n", PI);
        printf("Estimated Value of PI  : %11.10f\n", pi_sum);
        printf("Error Percentage   : %10.8f\n", error);
        printf("Time    : %10.8f\n\n", end_time - start_time);

    }

    MPI_Finalize();

    return 0;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T10:11:17+00:00Added an answer on May 27, 2026 at 10:11 am

    Broadcast doesn’t “push” data onto other processors.

    Almost all MPI communications requires the active participation of all processors. To send a message between two processors, for instance, the sender must call something like MPI_Send() and the receiver must call something like MPI_Recv().

    This is true for collective communications, too; for instance, you have everyone calling MPI_Reduce(). Similarly, you have to have everyone call the MPI_Bcast(), not just the one that has the original data, the “receivers” too:

    if (proc_id == MASTER){
        /* ... */
        scanf("%lld",&NDARTS); 
    }
    
    MPI_Bcast(&NDARTS, 1, MPI_LONG_LONG_INT, 0, MPI_COMM_WORLD);
    
    if (proc_id == MASTER) {
        start_time = MPI_Wtime();
    }
    
    /* ... */
    

    By the way, when you seed your random number generator, which is otherwise fine, you might want to make sure the seed is different on every processor by putting proc_id in there somewhere rather than just counting on the clocks being different enough to throw the seeds off…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This program estimates Pi by throwing random darts (sampling points) to a circle or
This program execution is not getting inside the if condition. please check the comment
This program I use has it's own variables to set when you run it,
This program stores pairs in a map, counting the number of times a word
This program reads emails (really just a .txt file structured like an email) and
This program I'm doing is about a social network, which means there are users
This program is meant to generate a dynamic array, however it gives an access
For this program #include <iostream> using std::cout; struct C { C() { cout <<
Consider this program int main() { float f = 11.22; double d = 44.55;
In this program when I debug, it is showing the nil for fileNameString. I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.