fork download
  1. #include "mpi.h"
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <math.h>
  5.  
  6. #define LIMIT 2500000 /* Increase this to find more primes */
  7. #define FIRST 0 /* Rank of first task */
  8.  
  9. int prime(int n) {
  10. int i,squareroot;
  11. if (n>10) {
  12. squareroot = (int) sqrt(n);
  13. for (i=3; i<=squareroot; i=i+2)
  14. if ((n%i)==0)
  15. return 0;
  16. return 1;
  17. }
  18. /* Assume first four primes are counted elsewhere. Forget everything else */
  19. else
  20. return 0;
  21. }
  22.  
  23.  
  24. int main (int argc, char *argv[])
  25. {
  26. int ntasks, /* total number of tasks in partitiion */
  27. rank, /* task identifier */
  28. n, /* loop variable */
  29. pc, /* prime counter */
  30. pcsum, /* number of primes found by all tasks */
  31. foundone, /* most recent prime found */
  32. maxprime, /* largest prime found */
  33. mystart, /* where to start calculating */
  34. stride; /* calculate every nth number */
  35.  
  36. double start_time,end_time;
  37.  
  38. MPI_Init(&argc,&argv);
  39. MPI_Comm_rank(MPI_COMM_WORLD,&rank);
  40. MPI_Comm_size(MPI_COMM_WORLD,&ntasks);
  41. if (((ntasks%2) !=0) || ((LIMIT%ntasks) !=0)) {
  42. printf("Sorry - this exercise requires an even number of tasks.\n");
  43. printf("evenly divisible into %d. Try 4 or 8.\n",LIMIT);
  44. MPI_Finalize();
  45. exit(0);
  46. }
  47.  
  48. start_time = MPI_Wtime(); /* Initialize start time */
  49. mystart = (rank*2)+1; /* Find my starting point - must be odd number */
  50. stride = ntasks*2; /* Determine stride, skipping even numbers */
  51. pc=0; /* Initialize prime counter */
  52. foundone = 0; /* Initialize */
  53.  
  54. /******************** task with rank 0 does this part ********************/
  55. if (rank == FIRST) {
  56. printf("Using %d tasks to scan %d numbers\n",ntasks,LIMIT);
  57. pc = 4; /* Assume first four primes are counted here */
  58. for (n=mystart; n<=LIMIT; n=n+stride) {
  59. if (isprime(n)) {
  60. pc++;
  61. foundone = n;
  62. /***** Optional: print each prime as it is found
  63.   printf("%d\n",foundone);
  64.   *****/
  65. }
  66. }
  67. MPI_Reduce(&pc,&pcsum,1,MPI_INT,MPI_SUM,FIRST,MPI_COMM_WORLD);
  68. MPI_Reduce(&foundone,&maxprime,1,MPI_INT,MPI_MAX,FIRST,MPI_COMM_WORLD);
  69. end_time=MPI_Wtime();
  70. printf("Done. Largest prime is %d Total primes %d\n",maxprime,pcsum);
  71. printf("Wallclock time elapsed: %.2lf seconds\n",end_time-start_time);
  72. }
  73.  
  74.  
  75. /******************** all other tasks do this part ***********************/
  76. if (rank > FIRST) {
  77. for (n=mystart; n<=LIMIT; n=n+stride) {
  78. if (isprime(n)) {
  79. pc++;
  80. foundone = n;
  81. /***** Optional: print each prime as it is found
  82.   printf("%d\n",foundone);
  83.   *****/
  84. }
  85. }
  86. MPI_Reduce(&pc,&pcsum,1,MPI_INT,MPI_SUM,FIRST,MPI_COMM_WORLD);
  87. MPI_Reduce(&foundone,&maxprime,1,MPI_INT,MPI_MAX,FIRST,MPI_COMM_WORLD);
  88. }
  89.  
  90. MPI_Finalize();
  91. }
  92.  
Success #stdin #stdout #stderr 0.26s 39436KB
stdin
34
stdout
Standard output is empty
stderr
Error: unexpected symbol in "int prime"
Execution halted