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[])
19 struct timeval *start_time = NULL, *stop_time = NULL;
27 MPI_Init(&argc, &argv);
29 MPI_Comm_rank(MPI_COMM_WORLD, &rank);
30 MPI_Comm_size(MPI_COMM_WORLD, &size);
33 start_time = (struct timeval *) malloc(sizeof(struct timeval));
34 stop_time = (struct timeval *) malloc(sizeof(struct timeval));
37 for (N = N_START; N <= N_STOP; N = N_NEXT) {
39 buffer = malloc(sizeof(char) * N);
42 for (j = 0; j < N; j++) {
43 buffer[j] = (char) (random() % 256);
45 if (-1 == gettimeofday(start_time, NULL)) {
46 printf("couldn't set start_time on node 0!\n");
47 MPI_Abort(MPI_COMM_WORLD, GETTIMEOFDAY_ERROR);
52 for (i = 0; i < ITER; i++) {
53 MPI_Bcast(buffer, N, MPI_BYTE, 0, MPI_COMM_WORLD);
55 for (j = 1; j < size; j++) {
56 MPI_Recv(&check, 1, MPI_INT, MPI_ANY_SOURCE, 1, MPI_COMM_WORLD,
60 MPI_Send(&rank, 1, MPI_INT, 0, 1, MPI_COMM_WORLD);
65 if (-1 == gettimeofday(stop_time, NULL)) {
66 printf("couldn't set start_time on node 0!\n");
67 MPI_Abort(MPI_COMM_WORLD, GETTIMEOFDAY_ERROR);
71 (double) (stop_time->tv_sec - start_time->tv_sec) +
72 (double) (stop_time->tv_usec - start_time->tv_usec) / ONE_MILLION;
78 printf("N: %10d, iter: %d, time: %10f s, avg rate: %12f Mbps\n", N,
80 ((double) N * ITER * 8) / (1024.0 * 1024.0 * seconds));