Advertisement
Guest User

Untitled

a guest
Apr 22nd, 2018
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.44 KB | None | 0 0
  1. /*
  2. I certify that this program is entirely my own work and none of it is the work of any other person.
  3.     George Aparicio
  4. Write an MPI program, countprimes which will count the number of prime numbers in the numbers
  5. from 1 to n inclusive where n is a long integer. The value for n which can be set in the program using
  6. a constant should be 50,000.
  7. */
  8. #include <stdio.h>
  9. #include <math.h>
  10. #include <stdlib.h>
  11. #include <mpi.h>
  12. #define range 50000
  13.  
  14. int findprimes(int id, long n)
  15. {
  16.     int i;
  17. if(n == 0 || n == 1)
  18. {
  19.     return 0;
  20. }
  21. else
  22. {
  23.     for(i = 2; i < n/2; i++){
  24.         if(n % i == 0)
  25.         {
  26.             return 0;
  27.         }
  28.     }
  29. }
  30.     printf("Process %d) Prime Number: 5d\n", id, n);//prints if it passes
  31.     fflush(stdout);
  32.     return 1;
  33. }
  34. //it would have the timer, the total number of primes and rank and number
  35. int main(int argc, char *argv[])
  36. {
  37.     int id, p;
  38.     int total, global;     
  39.     double time;
  40.     long i;
  41.     const long prime = 50000;
  42.    
  43.     MPI_Init(&argc, &argv);
  44.     //begins
  45.     MPI_Comm_rank(MPI_COMM_WORLD, &id);
  46.     MPI_Comm_size(MPI_COMM_WORLD, &p);
  47.    
  48.     MPI_Barrier(MPI_COMM_WORLD);
  49.     time -= MPI_Wtime();
  50.    
  51.     total = 0;
  52.     for(i = id; i < range; i += p)
  53.     {
  54.         total += findprimes(id, i);
  55.     }
  56.     MPI_Reduce(&total, &global, 1, MPI_INT, MPI_SUM, 0, MPI_COMM_WORLD);
  57.    
  58.     time += MPI_Wtime();
  59.     //ends
  60.     if(!id){
  61.         printf("Runtime %0.6f\n", time);
  62.         fflush(stdout);
  63.     }
  64.     MPI_Finalize();
  65.    
  66.     if(!id)
  67.     {
  68.         printf("There is %d total prime.\n", global);
  69.        
  70.     }
  71.     return 0;
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement