fork download
  1. from mpi4py import MPI
  2.  
  3. comm = MPI.COMM_WORLD
  4. rank = comm.rank
  5. if rank == 0:
  6. noOfIterations = int(input("Enter number of iterations : "))
  7. noOfProcesses = int(input("Enter number of processes to spawn : "))
  8. iterationsPerProcess = noOfIterations // noOfProcesses
  9. ss=0
  10. else:
  11. noOfIterations = None
  12. noOfProcesses = None
  13. iterationsPerProcess = None
  14. ss=None
  15.  
  16. def calcPi()->float:
  17. n = comm.bcast(noOfIterations, root = 0)
  18. p = comm.bcast(noOfProcesses, root = 0)
  19. i = comm.bcast(iterationsPerProcess, root = 0)
  20. s = comm.bcast(ss, root = 0)
  21. k = (2*rank)+1
  22. flag=False
  23. if p%2 == 0:
  24. if rank %2 == 0:
  25. for x in range(i):
  26. s+=4/k
  27. k = k+(2*p)
  28. else:
  29. for x in range(i):
  30. s-=4/k
  31. k = k+(2*p)
  32. else: ########## here #########
  33. if rank %2 == 0:
  34. for x in range(i):
  35. if flag == False:
  36. s+=4/k
  37. else:
  38. s-=4/k
  39. flag = not flag
  40. k = k+(2*p)
  41. else:
  42. for x in range(i):
  43. if flag == False:
  44. s-=4/k
  45. else:
  46. s+=4/k
  47. flag = not flag
  48. k = k+(2*p)
  49. newData = comm.gather(s,root=0)
  50.  
  51. if rank == 0:
  52. print(newData)
  53. print(sum(newData))
  54.  
  55. calcPi()
  56.  
Success #stdin #stdout #stderr 0.29s 38976KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
Error: unexpected symbol in "from mpi4py"
Execution halted