5 #define GETTIMEOFDAY_ERROR 1
8 #define N_STOP 1024*1024
11 #define ONE_MILLION 1000000.0
12 #define RAND_SEED 842270
14 int main(int argc, char* argv[]) {
18 struct timeval *start_time, *stop_time;
26 MPI_Init(&argc, &argv);
28 MPI_Comm_rank(MPI_COMM_WORLD, &rank);
29 MPI_Comm_size(MPI_COMM_WORLD, &size);
32 start_time = (struct timeval *)malloc(sizeof(struct timeval));
33 stop_time = (struct timeval *)malloc(sizeof(struct timeval));
36 for (N = N_START ; N <= N_STOP ; N = N_NEXT) {
38 buffer = malloc(sizeof(char) * N);
41 for (j = 0; j < N; j++) {
42 buffer[j] = (char)(random() % 256);
44 if (-1 == gettimeofday(start_time, NULL)) {
45 printf("couldn't set start_time on node 0!\n");
46 MPI_Abort(MPI_COMM_WORLD, GETTIMEOFDAY_ERROR);
51 for (i = 0; i < ITER; i++) {
52 MPI_Bcast(buffer, N, MPI_BYTE, 0, MPI_COMM_WORLD);
54 for (j = 1; j < size; j++) {
55 MPI_Recv(&check, 1, MPI_INT, MPI_ANY_SOURCE, 1, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
58 MPI_Send(&rank, 1, MPI_INT, 0, 1, MPI_COMM_WORLD);
63 if (-1 == gettimeofday(stop_time, NULL)) {
64 printf("couldn't set start_time on node 0!\n");
65 MPI_Abort(MPI_COMM_WORLD, GETTIMEOFDAY_ERROR);
68 seconds = (double)(stop_time->tv_sec - start_time->tv_sec) + (double)(stop_time->tv_usec - start_time->tv_usec) / ONE_MILLION;
74 printf("N: %10d, iter: %d, time: %10f s, avg rate: %12f Mbps\n", N, ITER, seconds, ((double)N * ITER * 8) / (1024.0 * 1024.0 * seconds));