Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
I know that feeling, pal (fix the fixes)
[simgrid.git] / teshsuite / smpi / mpich3-test / perf / non_zero_root.c
1 /* -*- Mode: C; c-basic-offset:4 ; indent-tabs-mode:nil ; -*- */
2 /*
3  *  (C) 2008 by Argonne National Laboratory.
4  *      See COPYRIGHT in top-level directory.
5  */
6 #include "mpi.h"
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <sys/time.h>
10
11 #define SIZE 100000
12 #define ITER 1000
13
14 #define ERROR_MARGIN 0.5
15
16 static int verbose = 0;
17
18 int main(int argc, char *argv[])
19 {
20     char *sbuf, *rbuf;
21     int i, j;
22     double t1, t2, t, ts;
23     int rank, size;
24     MPI_Status status;
25
26     MPI_Init(&argc, &argv);
27     MPI_Comm_rank(MPI_COMM_WORLD, &rank);
28     MPI_Comm_size(MPI_COMM_WORLD, &size);
29
30     if (getenv("MPITEST_VERBOSE"))
31         verbose = 1;
32
33     /* Allocate memory regions to communicate */
34     sbuf = (char *) malloc(SIZE);
35     rbuf = (char *) malloc(size * SIZE);
36
37     /* Touch the buffers to make sure they are allocated */
38     for (i = 0; i < SIZE; i++)
39         sbuf[i] = '0';
40     for (i = 0; i < SIZE * size; i++)
41         rbuf[i] = '0';
42
43     /* Time when rank 0 gathers the data */
44     MPI_Barrier(MPI_COMM_WORLD);
45     t1 = MPI_Wtime();
46     for (i = 0; i < ITER; i++) {
47         MPI_Gather(sbuf, SIZE, MPI_BYTE, rbuf, SIZE, MPI_BYTE, 0, MPI_COMM_WORLD);
48         MPI_Barrier(MPI_COMM_WORLD);
49     }
50     t2 = MPI_Wtime();
51     t = (t2 - t1) / ITER;
52
53     /* Time when rank 1 gathers the data */
54     MPI_Barrier(MPI_COMM_WORLD);
55     t1 = MPI_Wtime();
56     for (j = 0; j < ITER; j++) {
57         MPI_Gather(sbuf, SIZE, MPI_BYTE, rbuf, SIZE, MPI_BYTE, 1, MPI_COMM_WORLD);
58         MPI_Barrier(MPI_COMM_WORLD);
59     }
60     t2 = MPI_Wtime();
61     ts = (t2 - t1) / ITER;
62
63     if (rank == 1)
64         MPI_Send(&ts, 1, MPI_DOUBLE, 0, 0, MPI_COMM_WORLD);
65     if (rank == 0)
66         MPI_Recv(&ts, 1, MPI_DOUBLE, 1, 0, MPI_COMM_WORLD, &status);
67
68     /* Print out the results */
69     if (!rank) {
70         if ((ts / t) > (1 + ERROR_MARGIN)) {    /* If the difference is more than 10%, it's an error */
71             printf("%.3f\t%.3f\n", 1000000.0 * ts, 1000000.0 * t);
72             printf("Too much difference in performance\n");
73         }
74         else
75             printf(" No Errors\n");
76     }
77
78     MPI_Finalize();
79
80     return 0;
81 }