Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Free dynamically allocated memory.
[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 int main(int argc, char *argv[])
17 {
18     char *sbuf, *rbuf;
19     int i, j;
20     double t1, t2, t, ts;
21     int rank, size;
22     MPI_Status status;
23
24     MPI_Init(&argc, &argv);
25     MPI_Comm_rank(MPI_COMM_WORLD, &rank);
26     MPI_Comm_size(MPI_COMM_WORLD, &size);
27
28     /* Allocate memory regions to communicate */
29     sbuf = (char *) malloc(SIZE);
30     rbuf = (char *) malloc(size * SIZE);
31
32     /* Touch the buffers to make sure they are allocated */
33     for (i = 0; i < SIZE; i++)
34         sbuf[i] = '0';
35     for (i = 0; i < SIZE * size; i++)
36         rbuf[i] = '0';
37
38     /* Time when rank 0 gathers the data */
39     MPI_Barrier(MPI_COMM_WORLD);
40     t1 = MPI_Wtime();
41     for (i = 0; i < ITER; i++) {
42         MPI_Gather(sbuf, SIZE, MPI_BYTE, rbuf, SIZE, MPI_BYTE, 0, MPI_COMM_WORLD);
43         MPI_Barrier(MPI_COMM_WORLD);
44     }
45     t2 = MPI_Wtime();
46     t = (t2 - t1) / ITER;
47
48     /* Time when rank 1 gathers the data */
49     MPI_Barrier(MPI_COMM_WORLD);
50     t1 = MPI_Wtime();
51     for (j = 0; j < ITER; j++) {
52         MPI_Gather(sbuf, SIZE, MPI_BYTE, rbuf, SIZE, MPI_BYTE, 1, MPI_COMM_WORLD);
53         MPI_Barrier(MPI_COMM_WORLD);
54     }
55     t2 = MPI_Wtime();
56     ts = (t2 - t1) / ITER;
57
58     if (rank == 1)
59         MPI_Send(&ts, 1, MPI_DOUBLE, 0, 0, MPI_COMM_WORLD);
60     if (rank == 0)
61         MPI_Recv(&ts, 1, MPI_DOUBLE, 1, 0, MPI_COMM_WORLD, &status);
62
63     /* Print out the results */
64     if (!rank) {
65         if ((ts / t) > (1 + ERROR_MARGIN)) {    /* If the difference is more than 10%, it's an error */
66             printf("%.3f\t%.3f\n", 1000000.0 * ts, 1000000.0 * t);
67             printf("Too much difference in performance\n");
68         }
69         else
70             printf(" No Errors\n");
71     }
72
73     free(sbuf);
74     free(rbuf);
75     MPI_Finalize();
76
77     return 0;
78 }