Advertisement
Guest User

Untitled

a guest
Mar 29th, 2020
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.88 KB | None | 0 0
  1. /* */
  2. /* CS 4331: Parallel Programming Fall, 2009 */
  3. /* */
  4. /* This MPI program computes does a "ping-pong" message passing */
  5. /* performance measurement. */
  6. /* */
  7. #include <stdio.h>
  8. #include "mpi.h"
  9.  
  10. #define MAX_LEN 1 << 18 /* maximum vector length */
  11. #define TRIALS 100 /* trials for each msg length */
  12. #define PROC_0 0 /* processor 0 */
  13. #define B0_TYPE 176 /* message "types" */
  14. #define B1_TYPE 177
  15.  
  16. int main(argc,argv)
  17. int argc;
  18. char *argv[];
  19.  
  20. {
  21. int numprocs, p, /* number of processors, proc index */
  22. myid, /* this processor's "rank" */
  23. length, /* vector length */
  24. i, t;
  25.  
  26. double b0[MAX_LEN], b1[MAX_LEN]; /* vectors */
  27.  
  28. double start_time, end_time; /* "wallclock" times */
  29.  
  30. MPI_Status stat; /* MPI structure containing return */
  31. /* codes for message passing operations */
  32.  
  33. MPI_Request send_handle, recv_handle; /* For nonblocking msgs */
  34.  
  35. MPI_Init(&argc,&argv); /* initialize MPI */
  36.  
  37. MPI_Comm_size(MPI_COMM_WORLD, &numprocs); /*how many processors? */
  38. MPI_Comm_rank(MPI_COMM_WORLD, &myid); /*which one am I? */
  39.  
  40. if (myid == PROC_0)
  41. {
  42. srand48(0xFEEDFACE);
  43.  
  44. /* generate processor 0's vector */
  45.  
  46. for (i=0; i<MAX_LEN; ++i)
  47. b0[i] = (double) drand48();
  48. }
  49.  
  50. MPI_Barrier(MPI_COMM_WORLD);
  51.  
  52. /* warmup, if necessary */
  53. /*
  54. printf("# Start warmup.\n");
  55.  
  56. for (length=1; length<=MAX_LEN; length*=2)
  57. if (myid == PROC_0)
  58. {
  59. MPI_Send(b0, length, MPI_DOUBLE, 1, B1_TYPE, MPI_COMM_WORLD);
  60. MPI_Recv(b1, length, MPI_DOUBLE, 1, B0_TYPE, MPI_COMM_WORLD, &stat);
  61. }
  62. else
  63. {
  64. MPI_Recv(b1, length, MPI_DOUBLE, 0, B1_TYPE, MPI_COMM_WORLD, &stat);
  65. MPI_Send(b0, length, MPI_DOUBLE, 0, B0_TYPE, MPI_COMM_WORLD);
  66. }
  67. */
  68. if (myid == PROC_0)
  69. #ifdef BLOCKING
  70. printf("\nPing-pong measurements for blocking send/recv pairs.\n");
  71. #else
  72. printf("\nPing-pong measurements for nonblocking send/recv pairs.\n");
  73. #endif
  74. /* measure message passing speed for vectors of various lengths */
  75.  
  76. for (length=1; length<=MAX_LEN; length*=2)
  77. {
  78. MPI_Barrier(MPI_COMM_WORLD);
  79.  
  80. if (myid == PROC_0)
  81. start_time = MPI_Wtime();
  82.  
  83. for (t=0; t<TRIALS; ++t)
  84. {
  85. #ifdef BLOCKING
  86. if (myid == PROC_0)
  87. {
  88. MPI_Send(b0, length, MPI_DOUBLE, 1, B1_TYPE, MPI_COMM_WORLD);
  89. MPI_Recv(b1, length, MPI_DOUBLE, 1, B0_TYPE, MPI_COMM_WORLD, &stat);
  90. }
  91. else
  92. {
  93. MPI_Recv(b1, length, MPI_DOUBLE, 0, B1_TYPE, MPI_COMM_WORLD, &stat);
  94. MPI_Send(b0, length, MPI_DOUBLE, 0, B0_TYPE, MPI_COMM_WORLD);
  95. }
  96. #else
  97. MPI_Isend(b0, length, MPI_DOUBLE, (myid+1)%numprocs, B0_TYPE, MPI_COMM_WORLD, &send_handle);
  98. MPI_Irecv(b1, length, MPI_DOUBLE, (myid+1)%numprocs, B0_TYPE, MPI_COMM_WORLD, &recv_handle);
  99.  
  100. MPI_Wait(&send_handle, &stat);
  101. MPI_Wait(&recv_handle, &stat);
  102. #endif
  103. }
  104. if (myid == PROC_0)
  105. {
  106. end_time = MPI_Wtime();
  107.  
  108. /* gnuplot ignores output lines that begin with #. */
  109. printf("# Length = %d\tAverage time=%lf\n",
  110. length, (end_time - start_time)/(double)(2*TRIALS));
  111. printf(" %d\t %lf\n",
  112. length, (end_time - start_time)/(double)(2*TRIALS));
  113. }
  114. }
  115.  
  116. MPI_Finalize();
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement