source: CIVL/examples/MPI/mpi_prime.c@ 50f834b

1.23 2.0 main test-branch
Last change on this file since 50f834b was 3b13e9c8, checked in by Manchun Zheng <zmanchun@…>, 12 years ago

translating all exit() call of a mpi program into a return statement; minor correction for mpi_prime.c

git-svn-id: svn://vsl.cis.udel.edu/civl/trunk@1083 fb995dde-84ed-4084-dfe6-e5aef3e2452c

  • Property mode set to 100644
File size: 3.9 KB
Line 
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 <stdio.h>
19#include <stdlib.h>
20#include <math.h>
21
22#define LIMIT 8 /* Increase this to find more primes */
23#define FIRST 0 /* Rank of first task */
24
25int isprime(int n) {
26 int i,squareroot;
27 if (n>10) {
28 squareroot = (int) sqrt(n);
29 for (i=3; i<=squareroot; i=i+2)
30 if ((n%i)==0)
31 return 0;
32 return 1;
33 }
34 /* Assume first four primes are counted elsewhere. Forget everything else */
35 else
36 return 0;
37}
38
39
40int main (int argc, char *argv[])
41{
42 int
43 ntasks, /* total number of tasks in partitiion */
44 rank, /* task identifier */
45 n, /* loop variable */
46 pc, /* prime counter */
47 pcsum, /* number of primes found by all tasks */
48 foundone, /* most recent prime found */
49 maxprime, /* largest prime found */
50 mystart, /* where to start calculating */
51 stride; /* calculate every nth number */
52
53 double start_time,end_time;
54
55 MPI_Init(&argc,&argv);
56 MPI_Comm_rank(MPI_COMM_WORLD,&rank);
57 MPI_Comm_size(MPI_COMM_WORLD,&ntasks);
58 if (((ntasks%2) !=0) || ((LIMIT%ntasks) !=0)) {
59 printf("Sorry - this exercise requires an even number of tasks.\n");
60 printf("evenly divisible into %d. Try 4 or 8.\n",LIMIT);
61 MPI_Finalize();
62 exit(0);
63 }
64
65 start_time = MPI_Wtime(); /* Initialize start time */
66 mystart = (rank*2)+1; /* Find my starting point - must be odd number */
67 stride = ntasks*2; /* Determine stride, skipping even numbers */
68 pc=0; /* Initialize prime counter */
69 foundone = 0; /* Initialize */
70
71 /******************** task with rank 0 does this part ********************/
72 if (rank == FIRST) {
73 printf("Using %d tasks to scan %d numbers\n",ntasks,LIMIT);
74 pc = 4; /* Assume first four primes are counted here */
75 for (n=mystart; n<=LIMIT; n=n+stride) {
76 if (isprime(n)) {
77 pc++;
78 foundone = n;
79 /***** Optional: print each prime as it is found
80 printf("%d\n",foundone);
81 *****/
82 }
83 }
84 MPI_Reduce(&pc,&pcsum,1,MPI_INT,MPI_SUM,FIRST,MPI_COMM_WORLD);
85 MPI_Reduce(&foundone,&maxprime,1,MPI_INT,MPI_MAX,FIRST,MPI_COMM_WORLD);
86 end_time=MPI_Wtime();
87 printf("Done. Largest prime is %d Total primes %d\n",maxprime,pcsum);
88 printf("Wallclock time elapsed: %.2lf seconds\n",end_time-start_time);
89 }
90
91
92/******************** all other tasks do this part ***********************/
93if (rank > FIRST) {
94 for (n=mystart; n<=LIMIT; n=n+stride) {
95 if (isprime(n)) {
96 pc++;
97 foundone = n;
98 /***** Optional: print each prime as it is found
99 printf("%d\n",foundone);
100 *****/
101 }
102 }
103 MPI_Reduce(&pc,&pcsum,1,MPI_INT,MPI_SUM,FIRST,MPI_COMM_WORLD);
104 MPI_Reduce(&foundone,&maxprime,1,MPI_INT,MPI_MAX,FIRST,MPI_COMM_WORLD);
105 }
106
107 MPI_Finalize();
108}
Note: See TracBrowser for help on using the repository browser.