Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- I certify that this program is entirely my own work and none of it is the work of any other person.
- George Aparicio
- Write an MPI program, countprimes which will count the number of prime numbers in the numbers
- from 1 to n inclusive where n is a long integer. The value for n which can be set in the program using
- a constant should be 50,000.
- */
- #include <stdio.h>
- #include <math.h>
- #include <stdlib.h>
- #include <mpi.h>
- #define range 50000
- int countprimes(int id, long n)
- {
- int i;
- if(n == 0 || n == 1)
- {
- return 0;
- }
- else
- {
- for(i = 2; i < n; i++){
- if(n % i == 0)
- {
- return 0;
- }
- }
- }
- printf("Process %d: Prime Number: [%d]\n", id, n);//prints if it passes
- fflush(stdout);
- return 1;
- }
- //it would have the timer, the total number of primes and rank and number
- int main(int argc, char *argv[])
- {
- int id, p;
- int totalPrimes, allPrimes;
- double time;
- long i;
- const long prime = 50000;
- MPI_Init(&argc, &argv);
- //begins
- MPI_Comm_rank(MPI_COMM_WORLD, &id);
- MPI_Comm_size(MPI_COMM_WORLD, &p);
- MPI_Barrier(MPI_COMM_WORLD);
- time -= MPI_Wtime();
- totalPrimes = 0;
- for(i = id; i <= range; i += p)
- {
- totalPrimes += countprimes(id, i);
- }
- MPI_Reduce(&totalPrimes, &allPrimes, 1, MPI_INT, MPI_SUM, 0, MPI_COMM_WORLD);
- time += MPI_Wtime();
- //ends
- if(!id){
- printf("Runtime %0.6f\n", time);
- fflush(stdout);
- }
- MPI_Finalize();
- if(!id)
- {
- printf("totalPrimes prime: %d.\n", allPrimes);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement