Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
85d36565b0dcdb33a3700a7eaab1d771d91181cd
[simgrid.git] / src / smpi / sample / bcbench.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <mpi.h>
4
5 #define GETTIMEOFDAY_ERROR 1
6
7 #define N_START 1
8 #define N_STOP  1024*1024
9 #define N_NEXT  (N*2)
10 #define ITER    100
11 #define ONE_MILLION 1000000.0
12 #define RAND_SEED 842270
13
14 int main(int argc, char* argv[]) {
15
16   int size, rank;
17   int N, I;
18   struct timeval *start_time, *stop_time;
19   double seconds;
20   int i, j, k;
21   char *buffer;
22   int check;
23
24   srandom(RAND_SEED);
25
26   MPI_Init(&argc, &argv);
27
28   MPI_Comm_rank(MPI_COMM_WORLD, &rank);
29   MPI_Comm_size(MPI_COMM_WORLD, &size);
30
31   if (0 == rank) {
32     start_time = (struct timeval *)malloc(sizeof(struct timeval));
33     stop_time  = (struct timeval *)malloc(sizeof(struct timeval));
34   }
35
36   for (N = N_START ; N <= N_STOP ; N = N_NEXT) {
37
38     buffer = malloc(sizeof(char) * N);
39
40     if (0 == rank) {
41       for (j = 0; j < N; j++) {
42         buffer[j] = (char)(random() % 256);
43       }
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);
47         exit(EXIT_FAILURE);
48       }
49     }
50
51     for (i = 0; i < ITER; i++) {
52       MPI_Bcast(buffer, N, MPI_BYTE, 0, MPI_COMM_WORLD);
53       if (0 == rank) {
54         for (j = 1; j < size; j++) {
55           MPI_Recv(&check, 1, MPI_INT, MPI_ANY_SOURCE, 1, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
56         }
57       } else {
58         MPI_Send(&rank, 1, MPI_INT, 0, 1, MPI_COMM_WORLD);
59       }
60     }
61
62     if (0 == rank) {
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);
66         exit(EXIT_FAILURE);
67       }
68       seconds = (double)(stop_time->tv_sec - start_time->tv_sec) + (double)(stop_time->tv_usec - start_time->tv_usec) / ONE_MILLION;
69     }
70
71     free(buffer);
72
73     if (0 == rank) {
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));
75     }
76
77   }
78
79   if (0 == rank) {
80     free(start_time);
81     free(stop_time);
82   }
83
84   MPI_Finalize();
85
86   return 0;
87 }