osu_bibw.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. # http://mvapich.cse.ohio-state.edu/benchmarks/
  2. from mpi4py import MPI
  3. def osu_bw(
  4. BENCHMARH = "MPI Bi-Directional Bandwidth Test",
  5. skip = 10,
  6. loop = 100,
  7. window_size = 64,
  8. skip_large = 2,
  9. loop_large = 20,
  10. window_size_large = 64,
  11. large_message_size = 8192,
  12. MAX_MSG_SIZE = 1<<22,
  13. ):
  14. comm = MPI.COMM_WORLD
  15. myid = comm.Get_rank()
  16. numprocs = comm.Get_size()
  17. if numprocs != 2:
  18. if myid == 0:
  19. errmsg = "This test requires exactly two processes"
  20. else:
  21. errmsg = None
  22. raise SystemExit(errmsg)
  23. s_buf = allocate(MAX_MSG_SIZE)
  24. r_buf = allocate(MAX_MSG_SIZE)
  25. if myid == 0:
  26. print ('# %s' % (BENCHMARH,))
  27. if myid == 0:
  28. print ('# %-8s%20s' % ("Size [B]", "Bandwidth [MB/s]"))
  29. message_sizes = [2**i for i in range(30)]
  30. for size in message_sizes:
  31. if size > MAX_MSG_SIZE:
  32. break
  33. if size > large_message_size:
  34. skip = skip_large
  35. loop = loop_large
  36. window_size = window_size_large
  37. iterations = list(range(loop+skip))
  38. window_sizes = list(range(window_size))
  39. s_msg = [s_buf, size, MPI.BYTE]
  40. r_msg = [r_buf, size, MPI.BYTE]
  41. send_request = [MPI.REQUEST_NULL] * window_size
  42. recv_request = [MPI.REQUEST_NULL] * window_size
  43. #
  44. comm.Barrier()
  45. if myid == 0:
  46. for i in iterations:
  47. if i == skip:
  48. t_start = MPI.Wtime()
  49. for j in window_sizes:
  50. recv_request[j] = comm.Irecv(r_msg, 1, 10)
  51. for j in window_sizes:
  52. send_request[j] = comm.Isend(s_msg, 1, 100)
  53. MPI.Request.Waitall(send_request)
  54. MPI.Request.Waitall(recv_request)
  55. t_end = MPI.Wtime()
  56. elif myid == 1:
  57. for i in iterations:
  58. for j in window_sizes:
  59. recv_request[j] = comm.Irecv(r_msg, 0, 100)
  60. for j in window_sizes:
  61. send_request[j] = comm.Isend(s_msg, 0, 10)
  62. MPI.Request.Waitall(send_request)
  63. MPI.Request.Waitall(recv_request)
  64. #
  65. if myid == 0:
  66. MB = size / 1e6 * loop * window_size
  67. s = t_end - t_start
  68. print ('%-10d%20.2f' % (size, MB/s))
  69. def allocate(n):
  70. try:
  71. import mmap
  72. return mmap.mmap(-1, n)
  73. except (ImportError, EnvironmentError):
  74. try:
  75. from numpy import zeros
  76. return zeros(n, 'B')
  77. except ImportError:
  78. from array import array
  79. return array('B', [0]) * n
  80. if __name__ == '__main__':
  81. osu_bw()