Advertisement
Guest User

Untitled

a guest
Jul 31st, 2014
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.64 KB | None | 0 0
  1. char *message;
  2. message= (char*)malloc(msg_size);
  3.  
  4. int main(int argc, char **argv)
  5. {
  6.  
  7. double startwtime, endwtime;
  8. float elapsed_time, bandwidth;
  9.  
  10. int my_id, next_id; /* process id-s */
  11. int p; /* number of processes */
  12. char* message; /* storage for the message */
  13. int i, k, max_msgs, msg_size, v;
  14. MPI_Status status; /* return status for receive */
  15.  
  16.  
  17. MPI_Init( &argc, &argv );
  18. MPI_Comm_rank( MPI_COMM_WORLD, &my_id );
  19. MPI_Comm_size( MPI_COMM_WORLD, &p );
  20.  
  21. if (argc < 3)
  22. {
  23. fprintf (stderr, "need msg count and msg size as paramsn");
  24. goto EXIT;
  25. }
  26.  
  27. if ((sscanf (argv[1], "%d", &max_msgs) < 1) ||
  28. (sscanf (argv[2], "%d", &msg_size) < 1))
  29. {
  30. fprintf (stderr, "need msg count and msg size as paramsn");
  31. goto EXIT;
  32. }
  33.  
  34. **message = (char*)malloc (msg_size);**
  35. if (argc > 3) v=1; else v=0; /*are we in verbose mode*/
  36.  
  37. /* don't start timer until everybody is ok */
  38. MPI_Barrier(MPI_COMM_WORLD);
  39. int t=0;
  40. if( my_id == 0 ) {
  41. startwtime = MPI_Wtime();
  42.  
  43. // do max_msgs times:
  44. // send message of size msg_size chars to process 1
  45. // receive message of size msg_size chars from process p-1
  46. while(t<max_msgs) {
  47. MPI_Send((char *) message, msg_size, MPI_CHAR, 1 , 0, MPI_COMM_WORLD);
  48. MPI_Recv((char *) message, msg_size, MPI_CHAR, p-1, 0, MPI_COMM_WORLD, &status);
  49. t++;
  50. }
  51. MPI_Barrier(MPI_COMM_WORLD);
  52. endwtime = MPI_Wtime();
  53. elapsed_time = endwtime-startwtime;
  54. bandwidth = 2.0 * max_msgs * msg_size / (elapsed_time);
  55. printf("Number, size of messages: %3d , %3d n", max_msgs, msg_size);
  56. fflush(stdout);
  57. printf("Wallclock time = %f secondsn", elapsed_time );
  58. fflush(stdout);
  59. printf("Bandwidth = %f bytes per secondn", bandwidth);
  60. fflush(stdout);
  61. } else if( my_id == p-1 ) {
  62.  
  63. // do max_msgs times:
  64. // receive message of size msg_size from process to the left
  65. // send message of size msg_size to process to the right (p-1 sends to 0)
  66. while(t<max_msgs) {
  67. MPI_Send((char *) message, msg_size, MPI_CHAR, 0, 0, MPI_COMM_WORLD);
  68. MPI_Recv((char *) message, msg_size, MPI_CHAR, my_id-1, 0, MPI_COMM_WORLD, &status);
  69. t++;
  70. }
  71. } else {
  72. while(t<max_msgs) {
  73. MPI_Send((char *) message, msg_size, MPI_CHAR, my_id+1, 0, MPI_COMM_WORLD);
  74. MPI_Recv((char *) message, msg_size, MPI_CHAR, my_id-1, 0, MPI_COMM_WORLD, &status);
  75. t++;
  76. }
  77. }
  78.  
  79. MPI_Barrier(MPI_COMM_WORLD);
  80.  
  81. EXIT:
  82. MPI_Finalize();
  83. return 0;
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement