Advertisement
Guest User

Untitled

a guest
Feb 20th, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.44 KB | None | 0 0
  1. /*
  2.  ============================================================================
  3.  Name        : Laborator1.c
  4.  Author      :
  5.  Version     :
  6.  Copyright   : Your copyright notice
  7.  Description : Calculate Pi in MPI
  8.  ============================================================================
  9.  */
  10. #include <mpi.h>
  11. #include <stdio.h>
  12. #include <string.h>
  13.  
  14. void
  15. calc_pi(int rank, int num_procs)
  16. {
  17.     int     i;
  18.     int     num_intervals;
  19.     double  h;
  20.     double  mypi;
  21.     double  pi;
  22.     double  sum;
  23.     double  x;
  24.  
  25.  
  26.     /* set number of intervals to calculate */
  27.     if (rank == 0) {
  28.         num_intervals = 100000000;
  29.     }
  30.  
  31.     /* tell other tasks how many intervals */
  32.     MPI_Bcast(&num_intervals, 1, MPI_INT, 0, MPI_COMM_WORLD);
  33.  
  34.     /* now everyone does their calculation */
  35.  
  36.     h = 1.0 / (double) num_intervals;
  37.     sum = 0.0;
  38.  
  39.     for (i = rank + 1; i <= num_intervals; i += num_procs) {
  40.         x = h * ((double)i - 0.5);
  41.         sum += (4.0 / (1.0 + x*x));
  42.     }
  43.  
  44.     mypi = h * sum;
  45.  
  46.     /* combine everyone's calculations */
  47.     MPI_Reduce(&mypi, &pi, 1, MPI_DOUBLE, MPI_SUM, 0, MPI_COMM_WORLD);
  48.  
  49.     if (rank == 0) {
  50.         printf("PI is approximately %.16f\n", pi);
  51.     }
  52. }
  53.  
  54. int
  55. main(int argc, char *argv[])
  56. {
  57.     int         my_rank;        /* rank of process */
  58.     int         n;      /* number of processes */
  59.     int         source;         /* rank of sender */
  60.     int         dest = 0;       /* rank of receiver */
  61.     int         tag = 0;        /* tag for messages */
  62.     char        message[100];   /* storage for message */
  63.     MPI_Status  status ;        /* return status for receive */
  64.  
  65.     int m = 3;
  66.     int s = 0;
  67.     int A[8] = {3,7,15,19,30,4,1,72};
  68.     int mask = 0;
  69.     int i = 0;
  70.     int sr;
  71.     s = A[my_rank];
  72.     mask = 0;
  73.  
  74.     /* start up MPI */
  75.  
  76.     MPI_Init(&argc, &argv);
  77.  
  78.     /* find out process rank */
  79.     MPI_Comm_rank(MPI_COMM_WORLD, &my_rank);
  80.  
  81.     /* find out number of processes */
  82.     MPI_Comm_size(MPI_COMM_WORLD, &n);
  83.  
  84.  
  85.     for (i= 0; i<m-1; i++)
  86.     {
  87.         if (my_rank && mask == 0)
  88.         {
  89.             if (my_rank && 2<<i == 0)
  90.             {
  91.                 source = my_rank^ 2<<i;
  92.  
  93.                 //receive sr from source
  94.                 MPI_Recv(sr, 1, MPI_INT, source, tag,MPI_COMM_WORLD,&status);
  95.  
  96.                 printf("%d <= \"%d\"\n",my_rank, sr);
  97.  
  98.                 s = s + sr;
  99.             }
  100.             else
  101.             {
  102.                 dest = my_rank^ 2<<i ;
  103.  
  104.                 //send s to destination
  105.                 MPI_Send(s,1, MPI_INT,dest, tag, MPI_COMM_WORLD);
  106.  
  107.                 printf("%d => \"%d\"\n",my_rank,s);
  108.  
  109.             }
  110.         }
  111.  
  112.         //mask ← mask xor 2i
  113.         mask = mask^2<<i;
  114.     }
  115.  
  116.     printf("sum is = %d",s);
  117.  
  118.     /* calculate PI */
  119.     calc_pi(my_rank, n);
  120.  
  121.     /* shut down MPI */
  122.     MPI_Finalize();
  123.  
  124.     return 0;
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement