Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Free memory.
[simgrid.git] / teshsuite / smpi / mpich3-test / pt2pt / sendrecv2.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 "mpitestconf.h"
9 #include <stdio.h>
10 #include <stdlib.h>
11 #ifdef HAVE_STRING_H
12 #include <string.h>
13 #endif
14
15 static int verbose = 0;
16
17 static int parse_args(int argc, char **argv);
18
19 int main(int argc, char *argv[])
20 {
21     int i, j, errs = 0;
22     int rank, size;
23     MPI_Datatype newtype;
24     char *buf = NULL;
25
26     MPI_Init(&argc, &argv);
27     parse_args(argc, argv);
28
29     MPI_Comm_rank(MPI_COMM_WORLD, &rank);
30     MPI_Comm_size(MPI_COMM_WORLD, &size);
31
32     if (size < 2) {
33         if (verbose)
34             fprintf(stderr, "comm size must be > 1\n");
35         errs++;
36         goto fn_exit;
37     }
38
39     buf = malloc(64 * 129);
40     if (buf == NULL) {
41         if (verbose)
42             fprintf(stderr, "error allocating buffer\n");
43         errs++;
44         goto fn_exit;
45     }
46
47     for (i = 8; i < 64; i += 4) {
48         MPI_Type_vector(i, 128, 129, MPI_CHAR, &newtype);
49
50         MPI_Type_commit(&newtype);
51         memset(buf, 0, 64 * 129);
52
53         if (rank == 0) {
54             /* init buffer */
55             for (j = 0; j < i; j++) {
56                 int k;
57                 for (k = 0; k < 129; k++) {
58                     buf[129 * j + k] = (char) j;
59                 }
60             }
61
62             /* send */
63             MPI_Send(buf, 1, newtype, 1, i, MPI_COMM_WORLD);
64         }
65         else if (rank == 1) {
66             /* recv */
67             MPI_Recv(buf, 1, newtype, 0, i, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
68
69             /* check buffer */
70             for (j = 0; j < i; j++) {
71                 int k;
72                 for (k = 0; k < 129; k++) {
73                     if (k < 128 && buf[129 * j + k] != (char) j) {
74                         if (verbose)
75                             fprintf(stderr,
76                                     "(i=%d, pos=%d) should be %d but is %d\n",
77                                     i, 129 * j + k, j, (int) buf[129 * j + k]);
78                         errs++;
79                     }
80                     else if (k == 128 && buf[129 * j + k] != (char) 0) {
81                         if (verbose)
82                             fprintf(stderr,
83                                     "(i=%d, pos=%d) should be %d but is %d\n",
84                                     i, 129 * j + k, 0, (int) buf[129 * j + k]);
85                         errs++;
86                     }
87                 }
88             }
89         }
90
91         MPI_Type_free(&newtype);
92     }
93
94     if (rank == 0) {
95         int recv_errs = 0;
96
97         MPI_Recv(&recv_errs, 1, MPI_INT, 1, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
98         if (recv_errs) {
99             if (verbose)
100                 fprintf(stderr, "%d errors reported from receiver\n", recv_errs);
101             errs += recv_errs;
102         }
103     }
104     else if (rank == 1) {
105         MPI_Send(&errs, 1, MPI_INT, 0, 0, MPI_COMM_WORLD);
106     }
107
108   fn_exit:
109
110     free(buf);
111     /* print message and exit */
112     if (errs) {
113         if (rank == 0)
114             fprintf(stderr, "Found %d errors\n", errs);
115     }
116     else {
117         if (rank == 0)
118             printf(" No Errors\n");
119     }
120     MPI_Finalize();
121     return 0;
122 }
123
124 static int parse_args(int argc, char **argv)
125 {
126     /*
127      * int ret;
128      *
129      * while ((ret = getopt(argc, argv, "v")) >= 0)
130      * {
131      * switch (ret) {
132      * case 'v':
133      * verbose = 1;
134      * break;
135      * }
136      * }
137      */
138     if (argc > 1 && strcmp(argv[1], "-v") == 0)
139         verbose = 1;
140     return 0;
141 }