fork download
  1. #include <stdio.h>
  2. #include "mpi.h"
  3.  
  4. int main(int argc, char *argv[]){
  5. int myRank;
  6. int size;
  7. int fact;
  8. int lower,upper;
  9.  
  10. int i;
  11. double local_result = 1.0;
  12. double total;
  13. MPI_Init(&argc,&argv);
  14. MPI_Comm_rank(MPI_COMM_WORLD, &myRank);
  15. MPI_Comm_size(MPI_COMM_WORLD, &size);
  16. if(myRank==0){
  17. printf("Enter a number:");
  18. scanf("%d",&fact);
  19. }
  20. MPI_Bcast(&fact, 1, MPI_INT, 0, MPI_COMM_WORLD);
  21. if(myRank==0){
  22. lower = 1;
  23. }else
  24. lower = myRank * (fact / size) + 1;
  25. if(myRank==(size-1))
  26. upper = fact;
  27. else
  28. upper = (myRank + 1) * (fact / size);
  29. for(i=lower;i<=upper;i++){
  30. local_result = local_result * (double)i;
  31. }
  32. MPI_Reduce(&local_result, &total, 1, MPI_DOUBLE, MPI_PROD, 0, MPI_COMM_WORLD);
  33.  
  34. if(myRank==0){
  35. printf("The factorial of %d is %lf, and was calculated using %d processes\n",fact,total,size);
  36. }
  37. MPI_Finalize();
  38. return 0;
  39. }
Success #stdin #stdout #stderr 0.23s 40860KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
Error: unexpected symbol in "int main"
Execution halted