Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of git+ssh://scm.gforge.inria.fr//gitroot/simgrid/simgrid
[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")) verbose = 1;
31
32         /* Allocate memory regions to communicate */
33         sbuf = (char*) malloc(SIZE);
34         rbuf = (char*) malloc(size * SIZE);
35
36         /* Touch the buffers to make sure they are allocated */
37         for (i = 0; i < SIZE; i++) sbuf[i] = '0';
38         for (i = 0; i < SIZE * size; i++) rbuf[i] = '0';
39
40         /* Time when rank 0 gathers the data */
41         MPI_Barrier(MPI_COMM_WORLD);
42         t1 = MPI_Wtime();
43         for (i = 0; i < ITER; i++) {
44                 MPI_Gather(sbuf, SIZE, MPI_BYTE, rbuf, SIZE, MPI_BYTE, 0, MPI_COMM_WORLD);
45                 MPI_Barrier(MPI_COMM_WORLD);
46         }
47         t2 = MPI_Wtime();
48         t = (t2-t1)/ITER;
49
50         /* Time when rank 1 gathers the data */
51         MPI_Barrier(MPI_COMM_WORLD);
52         t1 = MPI_Wtime();
53         for (j = 0; j < ITER; j++) {
54                 MPI_Gather(sbuf, SIZE, MPI_BYTE, rbuf, SIZE, MPI_BYTE, 1, MPI_COMM_WORLD);
55                 MPI_Barrier(MPI_COMM_WORLD);
56         }
57         t2 = MPI_Wtime();
58         ts = (t2-t1)/ITER;
59
60         if (rank == 1)
61                 MPI_Send(&ts, 1, MPI_DOUBLE, 0, 0, MPI_COMM_WORLD);
62         if (rank == 0)
63                 MPI_Recv(&ts, 1, MPI_DOUBLE, 1, 0, MPI_COMM_WORLD, &status);
64
65         /* Print out the results */
66         if (!rank) {
67                 if ((ts / t) > (1 + ERROR_MARGIN)) { /* If the difference is more than 10%, it's an error */
68                         printf("%.3f\t%.3f\n", 1000000.0 * ts, 1000000.0 * t);
69                         printf("Too much difference in performance\n");
70                 }
71                 else printf(" No Errors\n");
72         }
73         
74         MPI_Finalize();
75     free(sbuf);
76     free(rbuf);
77         return 0;
78 }