0

I have a program that gives me estimation of pi, and time elapsed depends on my input numbers.
Can someone please tell me how to compile this? I have the code but it's mystery how to compile and r
un it...

* Compiler: 
*   mpicc -g -Wall -o  name name.c 
*   mpirun -np 4 name
*******************************************************************/

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

#define M 0
#define PI 3.14159265


double RandomN (double a, double b) // Random Number Generator
{
    double r; 

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

/* Main */
int main(int argc, char*argv[])
{
    long long int *n_throw=0; // Number of darts thrown

    int p_rnk,  // Processor rank
        p_sz,   // Number of processors
        NC, // Number in circle
        i,  // Toss
        llimit, // Lower limit for random numbers
        ulimit; // Upper limit for random numbers

    double pi_estimate, // Estimation of PI
           pi_sum,  // Sum of PI values from each WORKER     
           x,           // x coordinate, between -1 & +1
           y,           // y coordinate, between -1 & +1
           z,           // Sum of x^2 and y^2 (Distance Squared)
           start_time,  // Wall clock - Start time
           end_time;    // Wall clock - End time

    struct timeval stime;

    NC =0;      // Number of darts that hit the circle = 0 (Initially)
    llimit = -1;    // Lower limit for random numbers = -1
    ulimit = 1;     // Upper limit for random numbers = 1


    MPI_Init(&argc, &argv); // START MPI

    MPI_Comm_rank (MPI_COMM_WORLD, &p_rnk); // Find the rank of the process
    MPI_Comm_size (MPI_COMM_WORLD, &p_sz);  // Find the size of the process


    if (p_rnk == M){    // If using Master process then: 
    printf ("\nHello MPI World\n\n");
    printf("Number of Darts: \n");  // cout << Number of Darts:
            scanf("%lld",n_throw);  // cin >> n_throw
        printf("  Number of processes: %d \n", p_sz);   // cout << Number of processes: p_sz
        printf("  Number of darts: %Ld \n", *n_throw);  // cout << Number of darts: *n_throw

        MPI_Bcast(&n_throw, 1, MPI_LONG_LONG_INT, 0, MPI_COMM_WORLD);   // Broadcast n_throw to all processes

        start_time = MPI_Wtime();   // START Time
    }

     gettimeofday(&stime, NULL);    // Time of the day
     srand(stime.tv_usec * stime.tv_usec * stime.tv_usec * stime.tv_usec);


    for (i=1; i<= *n_throw; i++){
        x = RandomN(llimit, ulimit);    // Random double between -1 & +1 
        y = RandomN(llimit, ulimit);    // Random double between -1 & +1

        z = pow(x,2) + pow(y,2);    // Distance squared

        if (z<=1.0){ // if Distance is squared <= 1.0
            NC++;   // number in circle ++
        }
    }

    pi_estimate = 4.0 * (double)NC / (double) *n_throw; // PI estimate is (4 * number in circle) / number of tosses

    MPI_Reduce (&pi_estimate, &pi_sum, 1, MPI_DOUBLE, MPI_SUM, M, MPI_COMM_WORLD);  // Gather all the information to Master Processor.

    if (p_rnk == M) {
        pi_sum = pi_sum / p_sz;     // SUM of PI value / Processor Size

        end_time = MPI_Wtime(); // END Time

        printf("The known value of PI  : %11.10f \n", PI);      // cout << Known value of PI : PI
        printf("The estimated Value of PI  : %11.10f\n", pi_sum);   // cout << Estimated Value of PI : pi_sum
        printf("Time : %10.8f\n\n", end_time - start_time); // cout << Time : total_time
    }

    MPI_Finalize(); // End MPI

    return 0;
}
2
Contributors
1
Reply
2
Views
4 Years
Discussion Span
Last Post by dx9_programmer
0

The instructions to compile is at the very top:

Compiler:
mpicc -g -Wall -o name name.c
mpirun -np 4 name

mpicc is the compiler
-g -Wall are additional options to pass to the compiler
-o name is the output executable file called "name"
name.c is the name of your source file

mpirun appears to be the program that runs your program "name" in a special way

Edited by dx9_programmer

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.