12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667 |
- #include <mpi.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <time.h>
- int main (int argc, char *argv []) {
- int message_size = atoi (argv [1]);
- int message_count = atoi (argv [2]);
- if (argc != 3) {
- printf ("usage: send_recv <message-size> <message-count>\n");
- return 1;
- }
- unsigned long throughput;
- double megabits;
- // Initialize the MPI environment
- MPI_Init(NULL, NULL);
- // Find out rank, size
- int world_rank;
- MPI_Comm_rank(MPI_COMM_WORLD, &world_rank);
- int world_size;
- MPI_Comm_size(MPI_COMM_WORLD, &world_size);
- if (world_size < 2) {
- fprintf(stderr, "World size must be greater than 1 for %s\n", argv[0]);
- MPI_Abort(MPI_COMM_WORLD, 1);
- }
- int data[message_size];
- clock_t start, end;
- long double elapsed;
- start = clock();
-
- for (int i = 0; i != message_count; i++) {
- if (world_rank == 0) {
- data[0] = 1234;
- MPI_Send(data, message_size, MPI_INT, 1, 0, MPI_COMM_WORLD);
- } else if (world_rank == 1) {
- MPI_Recv(data, message_size, MPI_INT, 0, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
- }
- }
-
- end = clock();
-
- if (world_rank == 1) {
- elapsed = end - start;
- if (elapsed == 0)
- elapsed = 1;
-
- throughput = (unsigned long)
- ((double) message_count / (double) elapsed * 1000000);
- megabits = ((double) throughput * message_size * 8) / 1000000;
-
- printf("message size: %d [B]\n", message_size);
- printf("message count: %d\n", message_count);
- printf("mean throughput: %d [msg/s]\n", (int)throughput);
- printf("mean throughput: %.3f [Mb/s]\n", (double)megabits);
- }
- MPI_Finalize();
-
- }
|