Latency.cpp 2.59 KB
Newer Older
1
2
3
4
5
6
7
8
9
/*
 * Latency.cpp
 *
 *  Created on: 5 Jun 2018
 *      Author: Ben Hazelwood
 */

#include <mpi.h>
#include <iostream>
Ben Hazelwood's avatar
Ben Hazelwood committed
10
11
#include <assert.h>
#include <cstdlib>
12

13
const int NUM_TRIALS = 100;
14
15
const int NUM_PONGS  = 1e4;

Ben Hazelwood's avatar
Ben Hazelwood committed
16
17
18
19
20
21
22
int START = 0;
int FINISH = 0;
int INCR = 0;

int NUM_POINTS = 0;

const int MAX_POINTS = 1e4;
23
24
25
26
27

int main(int argc, char* argv[]) {

  MPI_Init(&argc, &argv);

Ben Hazelwood's avatar
Ben Hazelwood committed
28
29
30
31
32
33
34
35
36
37
38
39
40
  if (argc != 4) {
    std::cout << "Usage: mpirun Latency <START> <FINISH> <INCREMENT>\n";
    MPI_Abort(MPI_COMM_WORLD, 1);
  }

  START = std::stoi(argv[1]);
  FINISH = std::stoi(argv[2]);
  INCR = std::stoi(argv[3]);

  NUM_POINTS = (FINISH - START) / INCR;


  assert(NUM_POINTS < MAX_POINTS);
41
42
43
44
45
46
47
48
49
50

  int rank;
  int size;

  MPI_Comm_rank(MPI_COMM_WORLD, &rank);
  MPI_Comm_size(MPI_COMM_WORLD, &size);

  const int source = 0;
  const int dest   = size - 1;

Ben Hazelwood's avatar
Ben Hazelwood committed
51
52
53
54
55
56
57
  char m[FINISH];

  if (rank == source) {
    for (int i=0; i < FINISH; i++) {
      m[i] = '0';
    }
  }
58
59
60
61

  double t1 = 0.0;
  double t2 = 0.0;

Ben Hazelwood's avatar
Ben Hazelwood committed
62
  double bandwidth[NUM_POINTS];
63
64

  MPI_Status status;
Ben Hazelwood's avatar
Ben Hazelwood committed
65
  int counter;
66
  for (int t = 0; t < NUM_TRIALS; t++) {
67
#ifdef COMPARE_PROGRESS
68
    MPI_Sendrecv(MPI_IN_PLACE, 0, MPI_BYTE, MPI_PROC_NULL, 0, MPI_IN_PLACE, 0, MPI_BYTE, MPI_PROC_NULL, 0, MPI_COMM_SELF, MPI_STATUS_IGNORE);
69
#endif
70
    MPI_Barrier(MPI_COMM_WORLD);
Ben Hazelwood's avatar
Ben Hazelwood committed
71
72
73
74
    counter = 0;
    for (int n = START; n <= FINISH; n += INCR) {
      if (rank == source) {
        // Handshake
75
        MPI_Send(&m, 1, MPI_CHAR, dest, 0, MPI_COMM_WORLD);
Ben Hazelwood's avatar
Ben Hazelwood committed
76

77
        t1 = MPI_Wtime();
Ben Hazelwood's avatar
Ben Hazelwood committed
78
79
80
81
82
83
84
85
86
87
        for (int p = 0; p < NUM_PONGS; p++) {
          MPI_Send(&m, n, MPI_CHAR, dest, 0, MPI_COMM_WORLD);
          MPI_Recv(&m, n, MPI_CHAR, dest, 1, MPI_COMM_WORLD,  &status);
        }

        t2 = MPI_Wtime();
        double b = (sizeof(char) * n * 2.0 * (double)NUM_PONGS) / (t2 - t1);
        if (bandwidth[counter] < b) {
          bandwidth[counter] = b;
        }
88
89
      }

Ben Hazelwood's avatar
Ben Hazelwood committed
90
91
      if (rank == dest) {
        // Handshake
92
93
        MPI_Recv(&m, 1, MPI_CHAR, source, 0, MPI_COMM_WORLD, &status);

Ben Hazelwood's avatar
Ben Hazelwood committed
94
95
96
97
        for (int p = 0; p < NUM_PONGS; p++) {
          MPI_Recv(&m, n, MPI_CHAR, source, 0, MPI_COMM_WORLD,  &status);
          MPI_Send(&m, n, MPI_CHAR, source, 1, MPI_COMM_WORLD);
        }
98
      }
Ben Hazelwood's avatar
Ben Hazelwood committed
99
      counter++;
100
    }
101
102
103
104
105
#ifdef COMPARE_HASH
//    if (rank == source) {
    MPI_Sendrecv(&m, 1, MPI_CHAR, MPI_PROC_NULL, 0, MPI_IN_PLACE, 0, MPI_BYTE, MPI_PROC_NULL, 0, MPI_COMM_SELF, MPI_STATUS_IGNORE);
//    }
#endif
106
107
  }
  if (rank == source) {
Ben Hazelwood's avatar
Ben Hazelwood committed
108
109
110
111
    counter = 0;
    for (int n=START; n < FINISH; n+=INCR) {
      std::cout << "bandwidth[" << n << "]=" << bandwidth[counter] << "\n";
      counter++;
112
113
114
115
    }
  }

  MPI_Finalize();
116
  return 0;
117
118
119
}