Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Free memory.
[simgrid.git] / teshsuite / smpi / mpich3-test / pt2pt / sendrecv3.c
1 /* -*- Mode: C; c-basic-offset:4 ; indent-tabs-mode:nil ; -*- */
2 /*
3  *
4  *  (C) 2003 by Argonne National Laboratory.
5  *      See COPYRIGHT in top-level directory.
6  */
7 #include "mpi.h"
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include "mpitest.h"
11
12 /*
13 static char MTEST_Descrip[] = "Head to head send-recv to test backoff in device when large messages are being transferred";
14 */
15
16 #define  MAX_NMSGS 100
17 int main(int argc, char *argv[])
18 {
19     int errs = 0;
20     int rank, size, source, dest, partner;
21     int i, testnum;
22     double tsend;
23     static int msgsizes[] = { 100, 1000, 10000, 100000, -1 };
24     static int nmsgs[] = { 100, 10, 10, 4 };
25     MPI_Comm comm;
26
27     MTest_Init(&argc, &argv);
28
29     comm = MPI_COMM_WORLD;
30
31     MPI_Comm_rank(comm, &rank);
32     MPI_Comm_size(comm, &size);
33     source = 0;
34     dest = 1;
35     if (size < 2) {
36         printf("This test requires at least 2 processes\n");
37         MPI_Abort(MPI_COMM_WORLD, 1);
38     }
39
40     for (testnum = 0; msgsizes[testnum] > 0; testnum++) {
41         if (rank == source || rank == dest) {
42             int nmsg = nmsgs[testnum];
43             int msgSize = msgsizes[testnum];
44             MPI_Request r[MAX_NMSGS];
45             int *buf[MAX_NMSGS];
46
47             for (i = 0; i < nmsg; i++) {
48                 buf[i] = (int *) malloc(msgSize * sizeof(int));
49                 if (!buf[i]) {
50                     fprintf(stderr, "Unable to allocate %d bytes\n", msgSize);
51                     MPI_Abort(MPI_COMM_WORLD, 1);
52                 }
53                 MTEST_VG_MEM_INIT(buf[i], msgSize * sizeof(int));
54             }
55             partner = (rank + 1) % size;
56
57             MPI_Sendrecv(MPI_BOTTOM, 0, MPI_INT, partner, 10,
58                          MPI_BOTTOM, 0, MPI_INT, partner, 10, comm, MPI_STATUS_IGNORE);
59             /* Try to fill up the outgoing message buffers */
60             for (i = 0; i < nmsg; i++) {
61                 MPI_Isend(buf[i], msgSize, MPI_INT, partner, testnum, comm, &r[i]);
62             }
63             for (i = 0; i < nmsg; i++) {
64                 MPI_Recv(buf[i], msgSize, MPI_INT, partner, testnum, comm, MPI_STATUS_IGNORE);
65             }
66             MPI_Waitall(nmsg, r, MPI_STATUSES_IGNORE);
67
68             /* Repeat the test, but make one of the processes sleep */
69             MPI_Sendrecv(MPI_BOTTOM, 0, MPI_INT, partner, 10,
70                          MPI_BOTTOM, 0, MPI_INT, partner, 10, comm, MPI_STATUS_IGNORE);
71             if (rank == dest)
72                 MTestSleep(1);
73             /* Try to fill up the outgoing message buffers */
74             tsend = MPI_Wtime();
75             for (i = 0; i < nmsg; i++) {
76                 MPI_Isend(buf[i], msgSize, MPI_INT, partner, testnum, comm, &r[i]);
77             }
78             tsend = MPI_Wtime() - tsend;
79             for (i = 0; i < nmsg; i++) {
80                 MPI_Recv(buf[i], msgSize, MPI_INT, partner, testnum, comm, MPI_STATUS_IGNORE);
81             }
82             MPI_Waitall(nmsg, r, MPI_STATUSES_IGNORE);
83
84             if (tsend > 0.5) {
85                 printf("Isends for %d messages of size %d took too long (%f seconds)\n", nmsg,
86                        msgSize, tsend);
87                 errs++;
88             }
89             MTestPrintfMsg(1, "%d Isends for size = %d took %f seconds\n", nmsg, msgSize, tsend);
90
91             for (i = 0; i < nmsg; i++) {
92                 free(buf[i]);
93             }
94         }
95     }
96
97     MTest_Finalize(errs);
98     MPI_Finalize();
99     return 0;
100 }