Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'v3_8_x'
[simgrid.git] / teshsuite / smpi / mpich-test / pt2pt / nullproc2.c
1 /*
2  *  Test for null proc handling with blocking routines
3  */
4
5
6 #include <stdio.h>
7 #include "mpi.h"
8
9 #if defined(NEEDS_STDLIB_PROTOTYPES)
10 #include "protofix.h"
11 #endif
12
13 int main( int argc, char *argv[] )
14 {
15    int             a[4];
16    int             i, nproc;
17    int             rank, right, left;
18    MPI_Status      st[2], sts[2];
19    MPI_Request     req[2];
20    int             count, errcnt = 0;
21
22    /* start up */
23    MPI_Init(&argc, &argv);
24    MPI_Comm_size(MPI_COMM_WORLD, &nproc);
25    MPI_Comm_rank(MPI_COMM_WORLD, &rank);
26
27    /* set up processor chain (Apps should use Cart_create/shift) */
28    left = (rank == 0) ? MPI_PROC_NULL : rank - 1;
29    right = (rank == nproc - 1) ? MPI_PROC_NULL : rank + 1;
30
31    /* initialize local matrix */
32    /* globally: a[i] = i, i = 1 .. 2*nproc */
33    /* locally : a[i] = 2*rank+i, i=1,2 */
34    a[0] = -1;
35    a[1] = 2*rank + 1; 
36    a[2] = 2*rank + 2; 
37    a[3] = -1;
38
39    /* start all receives and sends */
40    MPI_Isend(&a[1], 1, MPI_INT, left, 0, MPI_COMM_WORLD, &req[0]);
41    MPI_Isend(&a[2], 1, MPI_INT, right,  1, MPI_COMM_WORLD, &req[1]);
42    st[0].MPI_SOURCE = nproc;
43    st[0].MPI_TAG = -1;
44    st[1].MPI_SOURCE = nproc;
45    st[1].MPI_TAG = -1;
46    MPI_Recv(&a[0], 1, MPI_INT, left,  1, MPI_COMM_WORLD, &st[0]);
47    MPI_Recv(&a[3], 1, MPI_INT, right, 0, MPI_COMM_WORLD, &st[1]);
48    MPI_Waitall( 2, req, sts );
49
50    /* Test the end points */
51    if (left == MPI_PROC_NULL) {
52        if (st[0].MPI_TAG != MPI_ANY_TAG ||
53            st[0].MPI_SOURCE != MPI_PROC_NULL) {
54            errcnt ++;
55            fprintf( stderr, "Incorrect null status for left\n" );
56            if (st[0].MPI_SOURCE != MPI_PROC_NULL) {
57                fprintf( stderr, "Source returned was %d but should be %d\n",
58                         st[0].MPI_SOURCE, MPI_PROC_NULL );
59            }
60        }
61        MPI_Get_count( &st[0], MPI_INT, &count );
62        if (count != 0) {
63            errcnt ++;
64            fprintf( stderr, "Incorrect null status for left (count)\n" );
65            fprintf( stderr, "Count was %d but should be 0\n", count );
66        }
67    }
68    else if (right == MPI_PROC_NULL) {
69        if (st[1].MPI_TAG != MPI_ANY_TAG ||
70            st[1].MPI_SOURCE != MPI_PROC_NULL) {
71            errcnt ++;
72            fprintf( stderr, "Incorrect null status for right\n" );
73            if (st[1].MPI_SOURCE != MPI_PROC_NULL) {
74                fprintf( stderr, "Source returned was %d but should be %d\n",
75                         st[1].MPI_SOURCE, MPI_PROC_NULL );
76            }
77        }
78        MPI_Get_count( &st[1], MPI_INT, &count );
79        if (count != 0) {
80            errcnt ++;
81            fprintf( stderr, "Incorrect null status for right (count)\n" );
82            fprintf( stderr, "Count was %d but should be 0\n", count );
83        }
84    }
85    
86    /* Test results */
87    if (left == MPI_PROC_NULL) {
88        if (a[0] != -1) {
89            fprintf( stderr, "Expected -1, found %d in left partner\n", a[0] );
90            errcnt ++;
91        }
92    }
93    else {
94        if (a[0] != 2 * left + 2) {
95            fprintf( stderr, "Expected %d, found %d in left partner\n", 
96                     2 * left + 2, a[0] );
97            errcnt ++;
98        }
99    }
100
101    if (right == MPI_PROC_NULL) {
102        if (a[3] != -1) {
103            fprintf( stderr, "Expected -1, found %d in right partner\n", a[3] );
104            errcnt ++;
105        }
106    }
107    else {
108        if (a[3] != 2 * right + 1) {
109            fprintf( stderr, "Expected %d, found %d in right partner\n", 
110                     2 * right + 1, a[3] );
111            errcnt ++;
112        }
113    }
114
115    
116    i = errcnt;
117    MPI_Allreduce( &i, &errcnt, 1, MPI_INT, MPI_SUM, MPI_COMM_WORLD );
118    if (rank == 0) {
119        if (errcnt > 0) {
120            printf( "Found %d errors in the run \n", errcnt );
121        }
122        else
123            printf( "No errors in handling MPI_PROC_NULL\n" );
124    }
125    
126    /* clean up */
127    MPI_Finalize();
128    return 0;
129 }