Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
fix build and dist, add missing folder
[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     }
37     
38     src  = 0;
39     dest = 1;
40
41     if (rank == src) {
42         int buf[128], position, cnt;
43         /* sender */
44
45         /* Create a datatype and send it (multiple of sizeof(int)) */
46         /* Create a send struct type */
47         oldtypes[0] = MPI_INT;
48         oldtypes[1] = MPI_CHAR;
49         blklens[0]  = 1;
50         blklens[1]  = 4*sizeof(int);
51         offsets[0]  = 0;
52         offsets[1]  = sizeof(int);
53         MPI_Type_struct( 2, blklens, offsets, oldtypes, &outtype );
54         MPI_Type_commit( &outtype );
55
56         buf[0] = 4*sizeof(int);
57         /* printf( "About to send to %d\n", dest ); */
58         MPI_Send( buf, 1, outtype, dest, 0, comm );
59         MPI_Type_free( &outtype );
60
61         /* Create a datatype and send it (not a multiple of sizeof(int)) */
62         /* Create a send struct type */
63         oldtypes[0] = MPI_INT;
64         oldtypes[1] = MPI_CHAR;
65         blklens[0]  = 1;
66         blklens[1]  = 4*sizeof(int)+1;
67         offsets[0]  = 0;
68         offsets[1]  = sizeof(int);
69         MPI_Type_struct( 2, blklens, offsets, oldtypes, &outtype );
70         MPI_Type_commit( &outtype );
71
72         buf[0] = 4*sizeof(int) + 1;
73         MPI_Send( buf, 1, outtype, dest, 1, comm );
74         MPI_Type_free( &outtype );
75
76         /* Pack data and send as packed */
77         position = 0;
78         cnt = 7;
79         MPI_Pack( &cnt, 1, MPI_INT, 
80                   buf, 128*sizeof(int), &position, comm );
81         MPI_Pack( (void*)"message", 7, MPI_CHAR,
82                   buf, 128*sizeof(int), &position, comm );
83         MPI_Send( buf, position, MPI_PACKED, dest, 2, comm );
84     }
85     else if (rank == dest) {
86         MPI_Status status;
87         int        buf[128], i, elms, count;
88
89         /* Receiver */
90         /* Create a receive struct type */
91         oldtypes[0] = MPI_INT;
92         oldtypes[1] = MPI_CHAR;
93         blklens[0]  = 1;
94         blklens[1]  = 256;
95         offsets[0]  = 0;
96         offsets[1]  = sizeof(int);
97         MPI_Type_struct( 2, blklens, offsets, oldtypes, &outtype );
98         MPI_Type_commit( &outtype );
99
100         for (i=0; i<3; i++) {
101             tag = i;
102             /* printf( "about to receive tag %d from %d\n", i, src ); */
103             MPI_Recv( buf, 1, outtype, src, tag, comm, &status );
104             MPI_Get_elements( &status, outtype, &elms );
105             if (elms != buf[0] + 1) {
106                 errs++;
107                 printf( "For test %d, Get elements gave %d but should be %d\n",
108                         i, elms, buf[0] + 1 );
109             }
110             MPI_Get_count( &status, outtype, &count );
111             if (count != MPI_UNDEFINED) {
112                 errs++;
113                 printf( "For partial send, Get_count did not return MPI_UNDEFINED\n" );
114             }
115         }
116         MPI_Type_free( &outtype );
117     }
118
119     MTest_Finalize( errs );
120     MPI_Finalize();
121     return 0;
122   
123 }