Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
remove unwanted files
[simgrid.git] / teshsuite / smpi / mpich-test / pt2pt / sendrecv3.c
1 #include "mpi.h"
2 #include <stdlib.h>
3 #include <stdio.h>
4 #include "dtypes.h"
5 #include "gcomm.h"
6
7 #if defined(NEEDS_STDLIB_PROTOTYPES)
8 #include "protofix.h"
9 #endif
10
11 int verbose = 0;
12 /*
13    This program is from mpich/tsuite/pt2pt and should be changed there only.
14    It needs gcomm and dtype from mpich/tsuite, and can be run with 
15    any number of processes > 1.
16
17    This version uses Pack to send a message and Unpack OR the datatype 
18    to receive it.
19  */
20 int main( int argc, char **argv )
21 {
22 MPI_Datatype *types;
23 void         **inbufs, **outbufs;
24 char         **names;
25 char         *packbuf, *unpackbuf;
26 int          packsize, unpacksize, position;
27 int          *counts, *bytesize, ntype;
28 MPI_Comm     comms[20];
29 int          ncomm = 20, rank, np, partner, tag, count;
30 int          i, j, k, err, toterr, world_rank;
31 int          errloc;
32 MPI_Status   status;
33 char         *obuf;
34
35 MPI_Init( &argc, &argv );
36
37 AllocateForData( &types, &inbufs, &outbufs, &counts, &bytesize, 
38                  &names, &ntype );
39 GenerateData( types, inbufs, outbufs, counts, bytesize, names, &ntype );
40
41 MPI_Comm_rank( MPI_COMM_WORLD, &world_rank );
42 MakeComms( comms, 20, &ncomm, 0 );
43
44 /* Test over a wide range of datatypes and communicators */
45 err = 0;
46 for (i=0; i<ncomm; i++) {
47     MPI_Comm_rank( comms[i], &rank );
48     MPI_Comm_size( comms[i], &np );
49     if (np < 2) continue;
50     if (world_rank == 0 && verbose) {
51         fprintf( stdout, "Testing with communicator with %d members\n", np );
52         }
53     tag = i;
54     for (j=0; j<ntype; j++) {
55         if (world_rank == 0 && verbose) 
56             fprintf( stdout, "Testing type %s\n", names[j] );
57         if (rank == 0) {
58             partner = np - 1;
59             MPI_Pack_size( counts[j], types[j], comms[i], &packsize );
60             packbuf = (char *)malloc( packsize );
61             if (!packbuf) 
62                 MPI_Abort( MPI_COMM_WORLD, 1 );
63             position = 0;
64             MPI_Pack( inbufs[j], counts[j], types[j], packbuf, packsize, 
65                       &position, comms[i] );
66             /* Send twice */
67             MPI_Send( packbuf, position, MPI_PACKED, partner, tag, comms[i] );
68             MPI_Send( packbuf, position, MPI_PACKED, partner, tag, comms[i] );
69             free( packbuf );
70             }
71         else if (rank == np-1) {
72             partner = 0;
73             obuf = outbufs[j];
74             for (k=0; k<bytesize[j]; k++) 
75                 obuf[k] = 0;
76             /* Receive directly */
77             MPI_Recv( outbufs[j], counts[j], types[j], partner, tag, comms[i],
78                       &status );
79             /* Test correct */
80             MPI_Get_count( &status, types[j], &count );
81             if (count != counts[j]) {
82                 fprintf( stderr, 
83                         "Error in counts (got %d expected %d) with type %s\n",
84                          count, counts[j], names[j] );
85                 err++;
86                 }
87             if (status.MPI_SOURCE != partner) {
88                 fprintf( stderr, 
89                         "Error in source (got %d expected %d) with type %s\n",
90                          status.MPI_SOURCE, partner, names[j] );
91                 err++;
92                 }
93             if ((errloc = CheckData( inbufs[j], outbufs[j], bytesize[j] ))) {
94                 fprintf( stderr, 
95                     "Error in data at byte %d with type %s (type %d on %d)\n", 
96                          errloc - 1, names[j], j, world_rank );
97                 err++;
98                 }
99             /* Receive packed, then unpack */
100             MPI_Pack_size( counts[j], types[j], comms[i], &unpacksize ); 
101             unpackbuf = (char *)malloc( unpacksize );
102             if (!unpackbuf) 
103                 MPI_Abort( MPI_COMM_WORLD, 1 );
104             MPI_Recv( unpackbuf, unpacksize, MPI_PACKED, partner, tag, 
105                       comms[i], &status );
106             obuf = outbufs[j];
107             for (k=0; k<bytesize[j]; k++) 
108                 obuf[k] = 0;
109             position = 0;
110             MPI_Get_count( &status, MPI_PACKED, &unpacksize );
111             MPI_Unpack( unpackbuf, unpacksize, &position, 
112                         outbufs[j], counts[j], types[j], comms[i] );
113             free( unpackbuf );
114             /* Test correct */
115 #ifdef FOO
116             /* Length is tricky; a correct code will have signaled an error 
117                in MPI_Unpack */
118             count = position;
119             if (count != counts[j]) {
120                 fprintf( stderr, 
121                 "Error in counts (got %d expected %d) with type %s (Unpack)\n",
122                          count, counts[j], names[j] );
123                 err++;
124                 }
125 #endif
126             if (status.MPI_SOURCE != partner) {
127                 fprintf( stderr, 
128                 "Error in source (got %d expected %d) with type %s (Unpack)\n",
129                          status.MPI_SOURCE, partner, names[j] );
130                 err++;
131                 }
132             if ((errloc = CheckData( inbufs[j], outbufs[j], bytesize[j] ))) {
133                 fprintf( stderr, 
134             "Error in data at byte %d with type %s (type %d on %d, Unpack)\n", 
135                         errloc - 1, names[j], j, world_rank );
136                 err++;
137                 }
138             }
139         }
140     }
141 if (err > 0) {
142     fprintf( stderr, "%d errors on %d\n", err, rank );
143     }
144 MPI_Allreduce( &err, &toterr, 1, MPI_INT, MPI_SUM, MPI_COMM_WORLD );
145  if (world_rank == 0) {
146      if (toterr == 0) {
147          printf( " No Errors\n" );
148      }
149      else {
150          printf (" Found %d errors\n", toterr );
151      }
152  }
153 FreeDatatypes( types, inbufs, outbufs, counts, bytesize, names, ntype );
154 FreeComms( comms, ncomm );
155 MPI_Barrier( MPI_COMM_WORLD );
156 MPI_Finalize();
157 return err;
158 }