Guest User

Untitled

a guest
May 23rd, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.24 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <iostream>
  4. #include <mpi.h>
  5. #include <math.h>
  6. using namespace std;
  7.  
  8. int main(int argc, char *argv[]) {
  9.     int tid, nthreads;
  10.     char *cpu_name;
  11.     double time_initial, time_current, time;
  12.  
  13.    
  14. //////////////////////////////////////////////////////////////*/
  15.  
  16.     int n=10000, i;
  17.     double PI25DT = 3.141592653589793238462643;
  18.     double mypi, pi, h, sum, x;
  19.    
  20.    
  21.     MPI_Init(&argc, &argv);
  22.     time_initial = MPI_Wtime();
  23.     MPI_Comm_rank(MPI_COMM_WORLD, &tid);
  24.     MPI_Comm_size(MPI_COMM_WORLD, &nthreads);
  25.     cpu_name = (char *) calloc(80, sizeof(char));
  26.     gethostname(cpu_name, 80);
  27.    
  28.    while (1) {
  29.     if (tid == 0) {
  30.         cout << "Enter the number of intervals: (0 quits)"
  31.          << endl;
  32.         cin >> n;
  33.    
  34.     }
  35.     MPI::COMM_WORLD.Bcast(&n, 1, MPI::INT, 0);
  36.     if (n==0)
  37.         break;
  38.     else {
  39.         h = 1.0 / (double) n;
  40.         sum = 0.0;
  41.         for ( i = tid + 1; i <= n; i++ ) {
  42.             x = h * ((double)i - 0.5);
  43.             sum += (4.0 / (1.0 + x*x));
  44.         }
  45.         mypi = h * sum;
  46.    
  47.         MPI::COMM_WORLD.Reduce(&mypi, &pi, 1, MPI::DOUBLE,
  48.                MPI::SUM, 0);
  49.         time_current = MPI_Wtime();
  50.         time = time_current - time_initial;
  51.         printf("tid %i wait time=%.3f on machine=%s [NCPU=%i]\n",
  52.                     tid , time, cpu_name, nthreads);
  53.         if (tid == 0){
  54.             printf("pi calculated: %.25f \nand real pi is %.25f \n", pi, PI25DT);
  55.             cout<< "Error is "<<fabs(pi - PI25DT) <<endl;
  56.         }
  57.     }
  58.     }
  59.  
  60. /////////////////////////////////////////////////////////////////////////////
  61. /*int loop_min, loop_max , i , n=2;
  62. double inf = 100000000, sum =0, p_sum=0;
  63.  
  64. for(tid=0; tid < nthreads ; tid++)
  65. {
  66.     loop_min    = 1 +  (int)((tid + 0)  *  (inf-1)/nthreads);
  67.     loop_max    =      (int)((tid + 1)  *  (inf-1)/nthreads);
  68.  
  69.     for(i=loop_max-1;i>=loop_min;i--)
  70.     {
  71.         p_sum += 1.0/pow(i,(double)n);
  72.     }
  73.    
  74. }
  75. MPI_Reduce(&p_sum,&sum,1,MPI_DOUBLE,MPI_SUM,0,MPI_COMM_WORLD);
  76. */
  77. ///////////////////////////////////////////////////////////////
  78. /////////////////////////////////////////////////////////////
  79.    
  80. //    printf("zeta(2):%f\n",sum);
  81.     MPI_Finalize();
  82. //    printf("final zeta(2):%f\n",sum);
  83.     return (0);
  84. }
Add Comment
Please, Sign In to add comment