Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
MPI_Abort can theorically fail. Add a call to exit() to ensure that the program...
[simgrid.git] / teshsuite / smpi / mpich3-test / datatype / getpartelm.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 <stdio.h>
9 #include "mpitest.h"
10
11 /*
12 static char MTest_descrip[] = "Receive partial datatypes and check that\
13 MPI_Getelements gives the correct version";
14 */
15
16 int main( int argc, char *argv[] )
17 {
18     int errs = 0;
19     MPI_Datatype outtype, oldtypes[2];
20     MPI_Aint     offsets[2];
21     int          blklens[2];
22     MPI_Comm     comm;
23     int          size, rank, src, dest, tag;
24
25     MTest_Init( &argc, &argv );
26
27     comm = MPI_COMM_WORLD;
28
29     MPI_Comm_rank( comm, &rank );
30     MPI_Comm_size( comm, &size );
31     
32     if (size < 2) {
33         errs++;
34         printf( "This test requires at least 2 processes\n" );
35         MPI_Abort( MPI_COMM_WORLD, 1 );
36         exit(1);
37     }
38     
39     src  = 0;
40     dest = 1;
41
42     if (rank == src) {
43         int buf[128], position, cnt;
44         /* sender */
45
46         /* Create a datatype and send it (multiple of sizeof(int)) */
47         /* Create a send struct type */
48         oldtypes[0] = MPI_INT;
49         oldtypes[1] = MPI_CHAR;
50         blklens[0]  = 1;
51         blklens[1]  = 4*sizeof(int);
52         offsets[0]  = 0;
53         offsets[1]  = sizeof(int);
54         MPI_Type_struct( 2, blklens, offsets, oldtypes, &outtype );
55         MPI_Type_commit( &outtype );
56
57         buf[0] = 4*sizeof(int);
58         /* printf( "About to send to %d\n", dest ); */
59         MPI_Send( buf, 1, outtype, dest, 0, comm );
60         MPI_Type_free( &outtype );
61
62         /* Create a datatype and send it (not a multiple of sizeof(int)) */
63         /* Create a send struct type */
64         oldtypes[0] = MPI_INT;
65         oldtypes[1] = MPI_CHAR;
66         blklens[0]  = 1;
67         blklens[1]  = 4*sizeof(int)+1;
68         offsets[0]  = 0;
69         offsets[1]  = sizeof(int);
70         MPI_Type_struct( 2, blklens, offsets, oldtypes, &outtype );
71         MPI_Type_commit( &outtype );
72
73         buf[0] = 4*sizeof(int) + 1;
74         MPI_Send( buf, 1, outtype, dest, 1, comm );
75         MPI_Type_free( &outtype );
76
77         /* Pack data and send as packed */
78         position = 0;
79         cnt = 7;
80         MPI_Pack( &cnt, 1, MPI_INT, 
81                   buf, 128*sizeof(int), &position, comm );
82         MPI_Pack( (void*)"message", 7, MPI_CHAR,
83                   buf, 128*sizeof(int), &position, comm );
84         MPI_Send( buf, position, MPI_PACKED, dest, 2, comm );
85     }
86     else if (rank == dest) {
87         MPI_Status status;
88         int        buf[128], i, elms, count;
89
90         /* Receiver */
91         /* Create a receive struct type */
92         oldtypes[0] = MPI_INT;
93         oldtypes[1] = MPI_CHAR;
94         blklens[0]  = 1;
95         blklens[1]  = 256;
96         offsets[0]  = 0;
97         offsets[1]  = sizeof(int);
98         MPI_Type_struct( 2, blklens, offsets, oldtypes, &outtype );
99         MPI_Type_commit( &outtype );
100
101         for (i=0; i<3; i++) {
102             tag = i;
103             /* printf( "about to receive tag %d from %d\n", i, src ); */
104             MPI_Recv( buf, 1, outtype, src, tag, comm, &status );
105             MPI_Get_elements( &status, outtype, &elms );
106             if (elms != buf[0] + 1) {
107                 errs++;
108                 printf( "For test %d, Get elements gave %d but should be %d\n",
109                         i, elms, buf[0] + 1 );
110             }
111             MPI_Get_count( &status, outtype, &count );
112             if (count != MPI_UNDEFINED) {
113                 errs++;
114                 printf( "For partial send, Get_count did not return MPI_UNDEFINED\n" );
115             }
116         }
117         MPI_Type_free( &outtype );
118     }
119
120     MTest_Finalize( errs );
121     MPI_Finalize();
122     return 0;
123   
124 }