send_recv.c 1.6 KB

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