Advertisement
Guest User

Untitled

a guest
Dec 13th, 2017
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.17 KB | None | 0 0
  1. # include "mpi.h"
  2. # include <stdlib.h>
  3. # include <stdio.h>
  4. # include <math.h>
  5. # include <time.h>
  6.  
  7. double cpu_time ( void ) {
  8.   double value;
  9.  
  10.   value = ( double ) clock ( )
  11.         / ( double ) CLOCKS_PER_SEC;
  12.  
  13.   return value;
  14. }
  15.  
  16. int prime_number ( int n ) {
  17.   int i;
  18.   int j;
  19.   int prime;
  20.   int total;
  21.  
  22.   total = 0;
  23.  
  24.   for ( i = 2; i <= n; i++ )
  25.   {
  26.     prime = 1;
  27.     for ( j = 2; j < i; j++ )
  28.     {
  29.       if ( ( i % j ) == 0 )
  30.       {
  31.         prime = 0;
  32.         break;
  33.       }
  34.     }
  35.     total = total + prime;
  36.   }
  37.   return total;
  38. }
  39.  
  40. int prime_numberp ( int n ) {
  41.   int i;
  42.   int j;
  43.   int prime;
  44.   int total, local, start;
  45.   int rank, size, master = 0;
  46.  
  47.   MPI_Comm_rank(MPI_COMM_WORLD, &rank);
  48.   MPI_Comm_size(MPI_COMM_WORLD, &size);
  49.  
  50.   MPI_Recv(&start, 1, MPI_INT, master, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
  51.  
  52.   total = 0;
  53.   //printf ("Start for thread %d is %d", rank, start);
  54.  
  55.   for ( i = start; i <= n; i = i + size - 1)
  56.   {
  57.     prime = 1;
  58.     for ( j = 2; j < i; j++ )
  59.     {
  60.       if ( ( i % j ) == 0 )
  61.       {
  62.         prime = 0;
  63.         break;
  64.       }
  65.     }
  66.     total = total + prime;
  67.   }
  68.   return total;
  69. }
  70.  
  71. void timestamp ( void ){
  72. # define TIME_SIZE 40
  73.  
  74.   static char time_buffer[TIME_SIZE];
  75.   const struct tm *tm;
  76.   size_t len;
  77.   time_t now;
  78.  
  79.   now = time ( NULL );
  80.   tm = localtime ( &now );
  81.  
  82.   len = strftime ( time_buffer, TIME_SIZE, "%d %B %Y %I:%M:%S %p", tm );
  83.  
  84.   printf ( "%s\n", time_buffer );
  85.  
  86.   return;
  87. # undef TIME_SIZE
  88. }
  89.  
  90. void test ( int n_lo, int n_hi, int n_factor );
  91. void testp ( int n_lo, int n_hi, int n_factor );
  92.  
  93. int main (int argc, char *argv[] ) {
  94.   MPI_Init(NULL, NULL);
  95.   int n_factor;
  96.   int n_hi;
  97.   int n_lo;
  98.   int rank, size, master = 0;
  99.   double ctimes, ctimep;
  100.  
  101.   MPI_Comm_rank(MPI_COMM_WORLD, &rank);
  102.   MPI_Comm_size(MPI_COMM_WORLD, &size);
  103.  
  104.   if (rank == master) {
  105.       timestamp ( );
  106.       printf ( "\n" );
  107.       printf ( "PRIME TEST SEQUENTIAL\n" );
  108.  
  109.       if ( argc !=4 ) {
  110.         n_lo = 1;
  111.         n_hi = 131072;
  112.         n_factor = 2;
  113.       } else {
  114.         n_lo = atoi(argv[1]);
  115.         n_hi = atoi(argv[2]);
  116.         n_factor = atoi(argv[3]);
  117.       }  
  118.       ctimes = cpu_time ( );
  119.       test ( n_lo, n_hi, n_factor );  
  120.       ctimes = cpu_time ( ) - ctimes;
  121.       printf ( "\n" );
  122.       printf ( "PRIME TEST PARALLEL\n" );
  123.       ctimep = cpu_time ( );
  124.   }
  125.   testp ( n_lo, n_hi, n_factor );
  126.  
  127.  
  128.   if (rank == master) {
  129.       ctimep = cpu_time ( ) - ctimep;
  130.       printf("\n============================================\n");
  131.       printf("SEQUENTIAL - Execution time: %f\n", ctimes);
  132.       printf("PARALLEL - Execution time: %f\n", ctimep);
  133.       printf("\n============================================\n");
  134.       printf ( "\n" );
  135.       printf ( "PRIME_TEST\n" );
  136.       printf ( "  Normal end of execution.\n" );
  137.       printf ( "\n" );
  138.       timestamp ( );
  139.      
  140.      
  141.   }
  142.   MPI_Finalize();
  143.   return 0;
  144. }
  145.  
  146. void test ( int n_lo, int n_hi, int n_factor ) {
  147.   int i;
  148.   int n;
  149.   int primes;
  150.   double ctime;
  151.  
  152.   printf ( "\n" );
  153.   printf ( "SEQUENTIAL EXECUTION\n" );
  154.   printf ( "  Call PRIME_NUMBER to count the primes from 1 to N.\n" );
  155.   printf ( "\n" );
  156.   printf ( "         N        Pi          Time\n" );
  157.   printf ( "\n" );
  158.  
  159.   n = n_lo;
  160.  
  161.   while ( n <= n_hi )
  162.   {
  163.     ctime = cpu_time ( );
  164.  
  165.     primes = prime_number ( n );
  166.  
  167.     ctime = cpu_time ( ) - ctime;
  168.  
  169.     printf ( "  %8d  %8d  %14f\n", n, primes, ctime );
  170.     n = n * n_factor;
  171.   }
  172.  
  173.   return;
  174. }
  175.  
  176. void testp ( int n_lo, int n_hi, int n_factor ) {
  177.   int i;
  178.   int n;
  179.   int primes;
  180.   double ctime;
  181.   int rank, size, master = 0;
  182.   int local;
  183.   int start;
  184.   int flag;
  185.   int loop = 1;
  186.  
  187.   MPI_Comm_rank(MPI_COMM_WORLD, &rank);
  188.   MPI_Comm_size(MPI_COMM_WORLD, &size);
  189.  
  190.   if (rank == master) {
  191.       printf ( "\n" );
  192.       printf ( "PARALLEL EXECUTION\n" );
  193.       printf ( "  Call PRIME_NUMBER to count the primes from 1 to N.\n" );
  194.       printf ( "\n" );
  195.       printf ( "         N        Pi          Time\n" );
  196.       printf ( "\n" );
  197.      
  198.      
  199.    
  200.     n = n_lo;  
  201.   }
  202.  
  203.  
  204.   while ( loop )
  205.   {
  206.     if (rank == master){
  207.         ctime = cpu_time ( );  
  208.        
  209.         for (i=1; i<size; i++) {
  210.             MPI_Send(&flag, 1, MPI_INT, i, 0, MPI_COMM_WORLD);
  211.            
  212.             if (flag) {
  213.                 start = i+1;
  214.                
  215.                 MPI_Send(&n, 1, MPI_INT, i, 0, MPI_COMM_WORLD);
  216.                
  217.                 MPI_Send(&start, 1, MPI_INT, i, 0, MPI_COMM_WORLD);
  218.             }
  219.             else {
  220.                 loop = 0;
  221.                 MPI_Send(&loop, 1, MPI_INT, i, 0, MPI_COMM_WORLD);
  222.             }
  223.         }
  224.     }
  225.    
  226.     if (rank != master) {
  227.         MPI_Recv(&flag, 1, MPI_INT, master, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
  228.         if (flag) {
  229.             MPI_Recv(&n, 1, MPI_INT, master, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
  230.            
  231.             local = prime_numberp ( n );
  232.            
  233.             MPI_Send(&local, 1, MPI_INT, master, 0, MPI_COMM_WORLD);
  234.         }
  235.         else MPI_Recv(&loop, 1, MPI_INT, master, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
  236.     }
  237.    
  238.     if (rank == master && flag) {
  239.         primes = 0;
  240.        
  241.         for (i = 1; i < size; i++) {
  242.             MPI_Recv( &local, 1, MPI_INT, MPI_ANY_SOURCE, MPI_ANY_TAG, MPI_COMM_WORLD, MPI_STATUS_IGNORE );
  243.            
  244.             primes = primes + local;
  245.         }
  246.        
  247.         ctime = cpu_time ( ) - ctime;
  248.  
  249.         printf ( "  %8d  %8d  %14f\n", n, primes, ctime );
  250.        
  251.     n = n * n_factor;
  252.     if (n > n_hi) flag = 0;
  253.     }
  254.    
  255.   }
  256.  
  257.   return;
  258. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement