Ejemplo n.º 1
0
Archivo: pi.c Proyecto: agolotin/CS484
int main (int argc, char *argv[])
{
double	homepi,         /* value of pi calculated by current task */
	pisum,	        /* sum of tasks' pi values */
	pi,	        /* average of pi after "darts" is thrown */
	avepi;	        /* average pi value for all iterations */
int	taskid,	        /* task ID - also used as seed number */
	numtasks,       /* number of tasks */
	rc,             /* return code */
	i;
MPI_Status status;
setvbuf( stdout, NULL, _IONBF, 0 );

/* Obtain number of tasks and task ID */
MPI_Init(&argc,&argv);
MPI_Comm_size(MPI_COMM_WORLD,&numtasks);
MPI_Comm_rank(MPI_COMM_WORLD,&taskid);
printf ("MPI task %d has started...\n", taskid);

/* Set seed for random number generator equal to task ID */
srandom (taskid);

avepi = 0;
for (i = 0; i < ROUNDS; i++) {
   /* All tasks calculate pi using dartboard algorithm */
   homepi = dboard(DARTS);

   /* Use MPI_Reduce to sum values of homepi across all tasks 
    * Master will store the accumulated value in pisum 
    * - homepi is the send buffer
    * - pisum is the receive buffer (used by the receiving task only)
    * - the size of the message is sizeof(double)
    * - MASTER is the task that will receive the result of the reduction
    *   operation
    * - MPI_SUM is a pre-defined reduction function (double-precision
    *   floating-point vector addition).  Must be declared extern.
    * - MPI_COMM_WORLD is the group of tasks that will participate.
    */


   printf ("MPI task %d pi %10.8f\n", taskid,homepi);
   rc = MPI_Reduce(&homepi, &pisum, 1, MPI_DOUBLE, MPI_SUM,
                   MASTER, MPI_COMM_WORLD);
   if (rc != MPI_SUCCESS)
      printf("%d: failure on mpc_reduce\n", taskid);

   /* Master computes average for this iteration and all iterations */
   if (taskid == MASTER) {
      pi = pisum/numtasks;
      avepi = ((avepi * i) + pi)/(i + 1); 
      printf("   After %8d throws, average value of pi = %10.8f\n",
              (DARTS * (i + 1)),avepi);
   }    
} 
if (taskid == MASTER) 
   printf ("\nReal value of PI: 3.1415926535897 \n");

MPI_Finalize();
return 0;
}
Ejemplo n.º 2
0
int main(int argc, const char *argv[])
{
    double  homepi,
            pisum,
            pi,
            avepi = 0.;

    int     taskid,
            numtasks,
            rc;

    MPI_Init(&argc, (char***)&argv);
    MPI_Comm_size(MPI_COMM_WORLD, &numtasks);
    MPI_Comm_rank(MPI_COMM_WORLD, &taskid);

    if (taskid == MASTER) {
        printf("Parallel computing of pi (3.1415926535), using %d tasks.\n", numtasks);
    }

    printf("MPI task %d has started...\n", taskid);

    srandom((unsigned int)time(NULL));

    int     i;
    for (i = 0; i < ROUNDS; i++) {
        homepi = dboard((int)(DARTS / numtasks));

        rc = MPI_Reduce(&homepi, &pisum, 1, MPI_DOUBLE, MPI_SUM, MASTER, MPI_COMM_WORLD);
        if (rc != MPI_SUCCESS) {
            fprintf(stderr, "%d: failure on MPI_Reduce\n", taskid);
        }

        if (taskid == MASTER) {
            pi = pisum / numtasks;
            avepi = ((avepi * i) + pi) / (i + 1);

            // printf("    After %.0Lf throws, average value of pi = %.10f\n", (long double)(DARTS * (i + 1)), avepi);
        }
    }

    if (taskid == MASTER) {
        printf("Parallel computing of pi 3.1415926535\n");
        printf("                         %.10f\n", avepi);
    }

    MPI_Finalize();

    return (0);
}
Ejemplo n.º 3
0
int main(int argc, char **argv) {
  double homepi;
  char buf[BUFSIZ];
  int DARTS;
  double start, finish, elapsed;

  printf ("\nnumber of DARTS: ");
  if (fgets(buf, sizeof(buf), stdin) != NULL) { DARTS = atoi(buf); }  

  start = (double) clock() / CLOCKS_PER_SEC;
  homepi = dboard(DARTS);
  finish = (double) clock() / CLOCKS_PER_SEC;
  elapsed = finish - start;

  printf("\nFor %d darts, pi = %0.20f", DARTS, homepi);
  printf("elapsed time = %.3f sec\n\n", elapsed);

}
Ejemplo n.º 4
0
int main (int argc, char *argv[])
{
double	homepi,         /* value of pi calculated by current task */
	pi,             /* average of pi after "darts" is thrown */
	avepi,          /* average pi value for all iterations */
	pirecv,         /* pi received from worker */
	pisum;          /* sum of workers pi values */
int	taskid,         /* task ID - also used as seed number */
	numtasks,       /* number of tasks */
	source,         /* source of incoming message */ 
	mtype,          /* message type */
	rc,             /* return code */
	i, n;
MPI_Status status;

/* Obtain number of tasks and task ID */
MPI_Init(&argc,&argv);
MPI_Comm_size(MPI_COMM_WORLD,&numtasks);
MPI_Comm_rank(MPI_COMM_WORLD,&taskid);
printf ("MPI task %d has started...\n", taskid);

/* Set seed for random number generator equal to task ID */
srandom (taskid);

avepi = 0;
for (i = 0; i < ROUNDS; i++) {
   /* All tasks calculate pi using dartboard algorithm */
   homepi = dboard(DARTS);

   /* Workers send homepi to master */
   /* - Message type will be set to the iteration count */
   if (taskid != MASTER) {
      mtype = i;
      rc = MPI_Send(&homepi, 1, MPI_DOUBLE,
                    MASTER, mtype, MPI_COMM_WORLD);
      if (rc != MPI_SUCCESS)
         printf("%d: Send failure on round %d\n", taskid, mtype);
      } 
   else
      {
      /* Master receives messages from all workers */
      /* - Message type will be set to the iteration count */
      /* - Message source will be set to the wildcard DONTCARE: */
      /*   a message can be received from any task, as long as the */
      /*   message types match */
      /* - The return code will be checked, and a message displayed */
      /*   if a problem occurred */
      mtype = i;
      pisum = 0;
      for (n = 1; n < numtasks; n++) {
         rc = MPI_Recv(&pirecv, 1, MPI_DOUBLE, MPI_ANY_SOURCE,
                        mtype, MPI_COMM_WORLD, &status);
         if (rc != MPI_SUCCESS) 
            printf("%d: Receive failure on round %d\n", taskid, mtype);
         /* keep running total of pi */
         pisum = pisum + pirecv;
         }
      /* Master calculates the average value of pi for this iteration */
      pi = (pisum + homepi)/numtasks;
      /* Master calculates the average value of pi over all iterations */
      avepi = ((avepi * i) + pi)/(i + 1); 
      printf("   After %8d throws, average value of pi = %10.8f\n",
               (DARTS * (i + 1)),avepi);
      }    
   } 

if (taskid == MASTER)
   printf ("\nReal value of PI: 3.1415926535897 \n");

MPI_Finalize();
return 0;
}