next up previous contents
Next: poissonRod.c Up: Source Codes Previous: Source Codes   Contents

primeTest.c

* * primeTest.c * */

#include <stdio.h> #include <stdlib.h> #include <math.h> #include <string.h> #include "mpi.h"

int main (int argc, char *argv[]) int num_procs, my_rank; int discoverId=-1; // flag of id that finds the // number that divides n unsigned long i; unsigned long index = 0; // (potential) divisor of n unsigned long n; // number to be tested its // primality double wall_time; // time taken to compute it

// MPI initialization MPI_Init(&argc, &argv); wall_time = MPI_Wtime(); MPI_Comm_size(MPI_COMM_WORLD, &num_procs); MPI_Comm_rank(MPI_COMM_WORLD, &my_rank);

if(argc != 2) perror("usage: prime <long prime candidate>"); return -1;

n = atol(argv[1]);

// is n even? if(nif(my_rank == 0) index = 2; discoverId = my_rank; MPI_Bcast(&discoverId, 1 , MPI_LONG, my_rank, MPI_COMM_WORLD); else // is n divisible by number between 3 and // ceil(sqrt(n))? for(i=3 + my_rank; i<=ceil(sqrt(n)) && discoverId == -1; i+= num_procs*2) if(nindex = i; discoverId = my_rank; MPI_Bcast(&discoverId, 1 , MPI_LONG, my_rank, MPI_COMM_WORLD);

// report the result to stdout if(discoverId == -1 && my_rank == 0) printf("else if(my_rank == discoverId) printf("

wall_time = MPI_Wtime() - wall_time; if(my_rank == 0) printf("wall time MPI_Finalize();

return 0;



J S 2002-08-14