Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Check return code, and report errors.
[simgrid.git] / teshsuite / smpi / mpich-test / pt2pt / getelm.c
1 /*
2  * This is a test of getting the number of basic elements
3  */
4
5 #include "mpi.h"
6 #include <stdio.h>
7
8 #if defined(NEEDS_STDLIB_PROTOTYPES)
9 #include "protofix.h"
10 #endif
11
12 typedef struct { 
13     int len;
14     double data[1000];
15     } buf_t;
16
17 int main( int argc, char **argv )
18 {
19     int err = 0, toterr;
20     MPI_Datatype contig1, varstruct1, oldtypes[2], varstruct2;
21     MPI_Aint     displs[2];
22     int          blens[2];
23     MPI_Comm     comm;
24     MPI_Status   status;
25     int          world_rank;
26     int          rank, size, partner, count, i;
27     int          send_ibuf[4], recv_ibuf[4];
28     buf_t        send_buf, recv_buf;
29
30     MPI_Init( &argc, &argv );
31     MPI_Comm_rank( MPI_COMM_WORLD, &world_rank );
32
33 /* Form the datatypes */
34     MPI_Type_contiguous( 4, MPI_INT, &contig1 );
35     MPI_Type_commit( &contig1 );
36     blens[0] = 1;
37     blens[1] = 1000;
38     oldtypes[0] = MPI_INT;
39     oldtypes[1] = MPI_DOUBLE;
40 /* Note that the displacement for the data is probably double aligned */
41     MPI_Address( &send_buf.len, &displs[0] );
42     MPI_Address( &send_buf.data[0], &displs[1] );
43 /* Make relative */
44     displs[1] = displs[1] - displs[0];
45     displs[0] = 0;
46     MPI_Type_struct( 2, blens, displs, oldtypes, &varstruct1 );
47     MPI_Type_commit( &varstruct1 );
48
49     comm = MPI_COMM_WORLD;
50
51     MPI_Comm_size( comm, &size );
52     MPI_Comm_rank( comm, &rank );
53
54     if (size < 2) {
55         fprintf( stderr, "This test requires at least 2 processes\n" );
56         MPI_Abort( MPI_COMM_WORLD, 1 );
57     }
58
59     if (rank == size - 1) {
60         partner = 0;
61         /* Send contiguous data */
62         for (i=0; i<4; i++) 
63             send_ibuf[i] = i;
64         MPI_Send( send_ibuf, 1, contig1, partner, 0, comm );
65
66         /* Send partial structure */
67         blens[1] = 23;
68         MPI_Type_struct( 2, blens, displs, oldtypes, &varstruct2 );
69         MPI_Type_commit( &varstruct2 );
70
71         MPI_Send( &send_buf, 1, varstruct2, partner, 1, comm );
72         MPI_Type_free( &varstruct2 );
73
74         /* Send NO data */
75         MPI_Send( MPI_BOTTOM, 0, MPI_INT, partner, 2, comm );
76     }
77     else if (rank == 0) {
78         partner = size - 1;
79         MPI_Recv( recv_ibuf, 1, contig1, partner, 0, comm, &status );
80         MPI_Get_count( &status, MPI_INT, &count );
81         if (count != 4) {
82             err++;
83             fprintf( stderr, 
84                      "Wrong count for contig recv MPI_INT; got %d expected %d\n",
85                      count, 4 );
86         }
87         MPI_Get_count( &status, contig1, &count );
88         if (count != 1) {
89             err++;
90             fprintf( stderr, 
91                      "Wrong count for contig recv (contig); got %d expected %d\n",
92                      count, 1 );
93         }
94         MPI_Get_elements( &status, contig1, &count );
95         if (count != 4) {
96             err++;
97             fprintf( stderr, 
98                      "Wrong elements for contig recv contig; got %d expected %d\n",
99                      count, 4 );
100         }
101
102         /* Now, try the partial structure */
103         MPI_Recv( &recv_buf, 1, varstruct1, partner, 1, comm, &status );
104         MPI_Get_elements( &status, varstruct1, &count );
105         if (count != 24) {
106             err++;
107             fprintf( stderr, 
108                      "Wrong number of elements for struct recv; got %d expected %d\n", 
109                      count, 24 );
110         }
111
112         {
113             /* Receive nothing using a 0-sized type */
114             MPI_Datatype ztype;
115             MPI_Type_contiguous( 0, MPI_INT, &ztype );
116             MPI_Type_commit( &ztype );
117             MPI_Recv( &recv_buf, 10, ztype, partner, 2, comm, &status );
118             /* Current clarification requires 0 for the result */
119             MPI_Get_elements( &status, ztype, &count );
120             if (count != 0) {
121                 err++;
122                 fprintf( stderr, 
123                          "Wrong number of elements for 0-size datatype; got %d\n",
124                          count );
125             }
126             MPI_Get_count( &status, ztype, &count );
127             if (count != 0) {
128                 err++;
129                 fprintf( stderr, 
130                          "Wrong count for 0-size datatype; got %d\n",
131                          count );
132             }
133             MPI_Type_free( &ztype );
134         }
135     }
136     MPI_Type_free( &contig1 );
137     MPI_Type_free( &varstruct1 );
138     
139     MPI_Allreduce( &err, &toterr, 1, MPI_INT, MPI_SUM, MPI_COMM_WORLD );
140     if (world_rank == 0) {
141             if (toterr == 0) 
142                 printf( " No Errors\n" );
143             else
144                 printf( "Found %d errors in MPI_Get_elements\n", toterr );
145     }
146     MPI_Finalize( );
147     return toterr;
148 }