send_recv.c 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. #include <mpi.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <time.h>
  6. int main (int argc, char *argv []) {
  7. int message_size = atoi (argv [1]);
  8. int message_count = atoi (argv [2]);
  9. unsigned long throughput;
  10. double megabits;
  11. // Initialize the MPI environment
  12. MPI_Init(NULL, NULL);
  13. // Find out rank, size
  14. int world_rank;
  15. MPI_Comm_rank(MPI_COMM_WORLD, &world_rank);
  16. int world_size;
  17. MPI_Comm_size(MPI_COMM_WORLD, &world_size);
  18. if (world_size < 2) {
  19. fprintf(stderr, "World size must be greater than 1 for %s\n", argv[0]);
  20. MPI_Abort(MPI_COMM_WORLD, 1);
  21. }
  22. int data[message_size];
  23. clock_t start, end;
  24. long double elapsed;
  25. start = clock();
  26. for (int i = 0; i != message_count; i++) {
  27. if (world_rank == 0) {
  28. data[0] = 1234;
  29. MPI_Send(data, message_size, MPI_INT, 1, 0, MPI_COMM_WORLD);
  30. } else if (world_rank == 1) {
  31. MPI_Recv(data, message_size, MPI_INT, 0, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
  32. }
  33. }
  34. end = clock();
  35. if (world_rank == 1) {
  36. elapsed = end - start;
  37. if (elapsed == 0)
  38. elapsed = 1;
  39. throughput = (unsigned long)
  40. ((double) message_count / (double) elapsed * 1000000);
  41. megabits = ((double) throughput * message_size * 8) / 1000000;
  42. printf("message size: %d [B]\n", message_size);
  43. printf("message count: %d [B]\n", message_count);
  44. printf("mean throughput: %d [msg/s]\n", (int)throughput);
  45. printf("mean throughput: %.3f [Mb/s]\n", (double)megabits);
  46. }
  47. MPI_Finalize();
  48. }