Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
handle nested datatypes in smpi (structs of vectors for example), which previously...
[simgrid.git] / teshsuite / smpi / mpich-test / pt2pt / sndrcv.c
1 #include "mpi.h"
2 #include <stdio.h>
3 #include "dtypes.h"
4 #include "gcomm.h"
5
6 #if defined(NEEDS_STDLIB_PROTOTYPES)
7 #include "protofix.h"
8 #endif
9
10 int verbose = 0;
11 /*
12    This program is from mpich/tsuite/pt2pt and should be changed there only.
13    It needs gcomm and dtype from mpich/tsuite, and can be run with 
14    any number of processes > 1.
15
16    This version uses sendrecv and sendrecv_replace (but only in the
17    head-to-head mode).
18  */
19 int main( int argc, char **argv )
20 {
21 MPI_Datatype *types;
22 void         **inbufs, **outbufs;
23 char         **names;
24 int          *counts, *bytesize, ntype;
25 MPI_Comm     comms[20];
26 int          ncomm = 20, rank, np, partner=0, tag, count;
27 int          i, j, k, err, toterr, world_rank, errloc;
28 MPI_Status   status;
29 char         *obuf, *ibuf;
30
31 MPI_Init( &argc, &argv );
32
33 AllocateForData( &types, &inbufs, &outbufs, &counts, &bytesize, 
34                  &names, &ntype );
35 GenerateData( types, inbufs, outbufs, counts, bytesize, names, &ntype );
36
37 MPI_Comm_rank( MPI_COMM_WORLD, &world_rank );
38 MakeComms( comms, 20, &ncomm, 0 );
39
40 /* Test over a wide range of datatypes and communicators */
41 err = 0;
42 for (i=0; i<ncomm; i++) {
43     MPI_Comm_rank( comms[i], &rank );
44     MPI_Comm_size( comms[i], &np );
45     if (np < 2) continue;
46     tag = i;
47     if (rank == 0) 
48         partner = np - 1;
49     if (rank == np - 1)
50         partner = 0;
51     for (j=0; j<ntype; j++) {
52         if (world_rank == 0 && verbose) 
53             fprintf( stdout, "Testing type %s\n", names[j] );
54         if (rank == 0 || rank == np - 1) {
55             obuf = outbufs[j];
56             for (k=0; k<bytesize[j]; k++) 
57                 obuf[k] = 0;
58             MPI_Sendrecv( inbufs[j], counts[j], types[j], partner, tag, 
59                           outbufs[j], counts[j], types[j], partner, tag, 
60                           comms[i], &status );
61             /* Test correct */
62             MPI_Get_count( &status, types[j], &count );
63             if (count != counts[j]) {
64                 fprintf( stderr, 
65                         "Error in counts (got %d expected %d) with type %s\n",
66                          count, counts[j], names[j] );
67                 err++;
68                 }
69             if (status.MPI_SOURCE != partner) {
70                 fprintf( stderr, 
71                         "Error in source (got %d expected %d) with type %s\n",
72                          status.MPI_SOURCE, partner, names[j] );
73                 err++;
74                 }
75             if ((errloc = CheckData( inbufs[j], outbufs[j], bytesize[j] ))) {
76                 char *p1, *p2;
77                 fprintf( stderr, 
78                   "Error in data with type %s (type %d on %d) at byte %d\n", 
79                          names[j], j, world_rank, errloc - 1 );
80                 p1 = (char *)inbufs[j];
81                 p2 = (char *)outbufs[j];
82                 fprintf( stderr, 
83                         "Got %x expected %x\n", p1[errloc-1], p2[errloc-1] );
84                 err++;
85                 }
86             /* Now do sendrecv_replace */
87             obuf = outbufs[j];
88             ibuf = inbufs[j];
89             for (k=0; k<bytesize[j]; k++) 
90                 obuf[k] = ibuf[k];
91             /* This would be a better test if the data was different... */
92             MPI_Sendrecv_replace( obuf, counts[j], types[j], partner, tag, 
93                                   partner, tag, comms[i], &status );
94             /* Test correct */
95             MPI_Get_count( &status, types[j], &count );
96             if (count != counts[j]) {
97                 fprintf( stderr, 
98                         "Error in counts (got %d expected %d) with type %s\n",
99                          count, counts[j], names[j] );
100                 err++;
101                 }
102             if (status.MPI_SOURCE != partner) {
103                 fprintf( stderr, 
104                         "Error in source (got %d expected %d) with type %s\n",
105                          status.MPI_SOURCE, partner, names[j] );
106                 err++;
107                 }
108             if ((errloc = CheckData( inbufs[j], outbufs[j], bytesize[j] ))) {
109                 char *p1, *p2;
110                 fprintf( stderr, 
111                   "Error in data with type %s (type %d on %d) at byte %d\n", 
112                          names[j], j, world_rank, errloc - 1 );
113                 p1 = (char *)inbufs[j];
114                 p2 = (char *)outbufs[j];
115                 fprintf( stderr, 
116                         "Got %x expected %x\n", p1[errloc-1], p2[errloc-1] );
117                 err++;
118                 }
119             }
120         }
121     }
122 if (err > 0) {
123     fprintf( stderr, "%d errors on %d\n", err, rank );
124     }
125 MPI_Allreduce( &err, &toterr, 1, MPI_INT, MPI_SUM, MPI_COMM_WORLD );
126  if (world_rank == 0) {
127      if (toterr == 0) {
128          printf( " No Errors\n" );
129      }
130      else {
131          printf (" Found %d errors\n", toterr );
132      }
133  }
134 FreeDatatypes( types, inbufs, outbufs, counts, bytesize, names, ntype );
135 FreeComms( comms, ncomm );
136 MPI_Finalize();
137 return err;
138 }