| 1 | /******************************************************************************
|
|---|
| 2 | * FILE: mpi_prime.c
|
|---|
| 3 | * DESCRIPTION:
|
|---|
| 4 | * Generates prime numbers. All tasks distribute the work evenly, taking
|
|---|
| 5 | * every nth number, where n is the stride computed as: (rank *2) + 1
|
|---|
| 6 | * so that even numbers are automatically skipped. The method of using
|
|---|
| 7 | * stride is preferred over contiguous blocks of numbers, since numbers
|
|---|
| 8 | * in the higher range require more work to compute and may result in
|
|---|
| 9 | * load imbalance. This program demonstrates embarrassing parallelism.
|
|---|
| 10 | * Collective communications calls are used to reduce the only two data
|
|---|
| 11 | * elements requiring communications: the number of primes found and
|
|---|
| 12 | * the largest prime.
|
|---|
| 13 | * AUTHOR: Blaise Barney 11/25/95 - adapted from version contributed by
|
|---|
| 14 | * Richard Ng & Wong Sze Cheong during MHPCC Singapore Workshop (8/22/95).
|
|---|
| 15 | * LAST REVISED: 04/13/05
|
|---|
| 16 | ******************************************************************************/
|
|---|
| 17 | #include <mpi.h>
|
|---|
| 18 | #include <civlmpi.cvh>
|
|---|
| 19 | #include <stdio.h>
|
|---|
| 20 | #include <stdlib.h>
|
|---|
| 21 | #include <math.h>
|
|---|
| 22 |
|
|---|
| 23 | #define LIMIT 8 /* Increase this to find more primes */
|
|---|
| 24 | #define FIRST 0 /* Rank of first task */
|
|---|
| 25 |
|
|---|
| 26 | int isprime(int n) {
|
|---|
| 27 | int i,squareroot;
|
|---|
| 28 | if (n>10) {
|
|---|
| 29 | squareroot = (int) sqrt(n);
|
|---|
| 30 | for (i=3; i<=squareroot; i=i+2)
|
|---|
| 31 | if ((n%i)==0)
|
|---|
| 32 | return 0;
|
|---|
| 33 | return 1;
|
|---|
| 34 | }
|
|---|
| 35 | /* Assume first four primes are counted elsewhere. Forget everything else */
|
|---|
| 36 | else
|
|---|
| 37 | return 0;
|
|---|
| 38 | }
|
|---|
| 39 |
|
|---|
| 40 |
|
|---|
| 41 | int main (int argc, char *argv[])
|
|---|
| 42 | {
|
|---|
| 43 | int
|
|---|
| 44 | ntasks, /* total number of tasks in partitiion */
|
|---|
| 45 | rank, /* task identifier */
|
|---|
| 46 | n, /* loop variable */
|
|---|
| 47 | pc, /* prime counter */
|
|---|
| 48 | pcsum, /* number of primes found by all tasks */
|
|---|
| 49 | foundone, /* most recent prime found */
|
|---|
| 50 | maxprime, /* largest prime found */
|
|---|
| 51 | mystart, /* where to start calculating */
|
|---|
| 52 | stride; /* calculate every nth number */
|
|---|
| 53 |
|
|---|
| 54 | double start_time,end_time;
|
|---|
| 55 |
|
|---|
| 56 | MPI_Init(&argc,&argv);
|
|---|
| 57 | MPI_Comm_rank(MPI_COMM_WORLD,&rank);
|
|---|
| 58 | MPI_Comm_size(MPI_COMM_WORLD,&ntasks);
|
|---|
| 59 | if (((ntasks%2) !=0) || ((LIMIT%ntasks) !=0)) {
|
|---|
| 60 | printf("Sorry - this exercise requires an even number of tasks.\n");
|
|---|
| 61 | printf("evenly divisible into %d. Try 4 or 8.\n",LIMIT);
|
|---|
| 62 | MPI_Finalize();
|
|---|
| 63 | exit(0);
|
|---|
| 64 | }
|
|---|
| 65 |
|
|---|
| 66 | start_time = MPI_Wtime(); /* Initialize start time */
|
|---|
| 67 | mystart = (rank*2)+1; /* Find my starting point - must be odd number */
|
|---|
| 68 | stride = ntasks*2; /* Determine stride, skipping even numbers */
|
|---|
| 69 | pc=0; /* Initialize prime counter */
|
|---|
| 70 | foundone = 0; /* Initialize */
|
|---|
| 71 |
|
|---|
| 72 | /******************** task with rank 0 does this part ********************/
|
|---|
| 73 | if (rank == FIRST) {
|
|---|
| 74 | printf("Using %d tasks to scan %d numbers\n",ntasks,LIMIT);
|
|---|
| 75 | pc = 4; /* Assume first four primes are counted here */
|
|---|
| 76 | for (n=mystart; n<=LIMIT; n=n+stride) {
|
|---|
| 77 | if (isprime(n)) {
|
|---|
| 78 | pc++;
|
|---|
| 79 | foundone = n;
|
|---|
| 80 | /***** Optional: print each prime as it is found
|
|---|
| 81 | printf("%d\n",foundone);
|
|---|
| 82 | *****/
|
|---|
| 83 | }
|
|---|
| 84 | }
|
|---|
| 85 | MPI_Reduce(&pc,&pcsum,1,MPI_INT,MPI_SUM,FIRST,MPI_COMM_WORLD);
|
|---|
| 86 | MPI_Reduce(&foundone,&maxprime,1,MPI_INT,MPI_MAX,FIRST,MPI_COMM_WORLD);
|
|---|
| 87 | end_time=MPI_Wtime();
|
|---|
| 88 | printf("Done. Largest prime is %d Total primes %d\n",maxprime,pcsum);
|
|---|
| 89 | printf("Wallclock time elapsed: %.2lf seconds\n",end_time-start_time);
|
|---|
| 90 | }
|
|---|
| 91 |
|
|---|
| 92 |
|
|---|
| 93 | /******************** all other tasks do this part ***********************/
|
|---|
| 94 | if (rank > FIRST) {
|
|---|
| 95 | for (n=mystart; n<=LIMIT; n=n+stride) {
|
|---|
| 96 | if (isprime(n)) {
|
|---|
| 97 | pc++;
|
|---|
| 98 | foundone = n;
|
|---|
| 99 | /***** Optional: print each prime as it is found
|
|---|
| 100 | printf("%d\n",foundone);
|
|---|
| 101 | *****/
|
|---|
| 102 | }
|
|---|
| 103 | }
|
|---|
| 104 | MPI_Reduce(&pc,&pcsum,1,MPI_INT,MPI_SUM,FIRST,MPI_COMM_WORLD);
|
|---|
| 105 | MPI_Reduce(&foundone,&maxprime,1,MPI_INT,MPI_MAX,FIRST,MPI_COMM_WORLD);
|
|---|
| 106 | }
|
|---|
| 107 |
|
|---|
| 108 | MPI_Finalize();
|
|---|
| 109 | }
|
|---|