Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
f4845c6be74e094f20c96632e95bea353e9fd9eb
[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) fprintf(stderr, "comm size must be > 1\n");
34         errs++;
35         goto fn_exit;
36     }
37
38     buf = malloc(64 * 129);
39     if (buf == NULL) {
40         if (verbose) fprintf(stderr, "error allocating buffer\n");
41         errs++;
42         goto fn_exit;
43     }
44
45     for (i = 8; i < 64; i += 4) {
46         MPI_Type_vector(i, 128, 129, MPI_CHAR, &newtype);
47
48         MPI_Type_commit(&newtype);
49         memset(buf, 0, 64*129);
50
51         if (rank == 0) {
52             /* init buffer */
53             for (j=0; j < i; j++) {
54                 int k;
55                 for (k=0; k < 129; k++) {
56                     buf[129*j + k] = (char) j;
57                 }
58             }
59
60             /* send */
61             MPI_Send(buf, 1, newtype, 1, i, MPI_COMM_WORLD);
62         }
63         else if (rank == 1) {
64             /* recv */
65             MPI_Recv(buf, 1, newtype, 0, i, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
66
67             /* check buffer */
68             for (j=0; j < i; j++) {
69                 int k;
70                 for (k=0; k < 129; k++) {
71                     if (k < 128 && buf[129*j + k] != (char) j) {
72                         if (verbose) fprintf(stderr,
73                                              "(i=%d, pos=%d) should be %d but is %d\n",
74                                              i, 129*j + k, j, (int) buf[129*j + k]);
75                         errs++;
76                     }
77                     else if (k == 128 && buf[129*j + k] != (char) 0) {
78                         if (verbose) fprintf(stderr,
79                                              "(i=%d, pos=%d) should be %d but is %d\n",
80                                              i, 129*j + k, 0, (int) buf[129*j + k]);
81                         errs++;
82                     }
83                 }
84             }
85         }
86
87         MPI_Type_free(&newtype);
88     }
89
90     if (rank == 0) {
91         int recv_errs = 0;
92
93         MPI_Recv(&recv_errs, 1, MPI_INT, 1, 0, MPI_COMM_WORLD,
94                  MPI_STATUS_IGNORE);
95         if (recv_errs) {
96             if (verbose) fprintf(stderr, "%d errors reported from receiver\n",
97                                  recv_errs);
98             errs += recv_errs;
99         }
100     }
101     else if (rank == 1) {
102         MPI_Send(&errs, 1, MPI_INT, 0, 0, MPI_COMM_WORLD);
103     }
104         
105  fn_exit:
106
107     free(buf);
108     /* print message and exit */
109     if (errs) {
110         if (rank == 0) fprintf(stderr, "Found %d errors\n", errs);
111     }
112     else {
113         if (rank == 0) printf(" No Errors\n");
114     }
115     MPI_Finalize();
116     return 0;
117 }
118
119 static int parse_args(int argc, char **argv)
120 {
121     /*
122     int ret;
123
124     while ((ret = getopt(argc, argv, "v")) >= 0)
125     {
126         switch (ret) {
127             case 'v':
128                 verbose = 1;
129                 break;
130         }
131     }
132     */
133     if (argc > 1 && strcmp(argv[1], "-v") == 0)
134         verbose = 1;
135     return 0;
136 }