Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
add mpich3 topo tests
[simgrid.git] / teshsuite / smpi / mpich3-test / comm / probe-intercomm.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 <stdlib.h>
10 #include <string.h>
11 #include "mpitest.h"
12
13 /*
14 static char MTEST_Descrip[] = "Test MPI_Probe() for an intercomm";
15 */
16 #define MAX_DATA_LEN 100
17
18 int main( int argc, char *argv[] )
19 {
20     int errs = 0, recvlen, isLeft;
21     MPI_Status status;
22     int rank, size;
23     MPI_Comm  intercomm;
24     char buf[MAX_DATA_LEN];
25     const char *test_str = "test";
26
27     MTest_Init( &argc, &argv );
28
29     MPI_Comm_rank( MPI_COMM_WORLD, &rank );
30     MPI_Comm_size( MPI_COMM_WORLD, &size );
31
32     if (size < 2) {
33         fprintf( stderr, "This test requires at least two processes." );
34         MPI_Abort( MPI_COMM_WORLD, 1 );
35         exit(1);
36     }
37
38     while (MTestGetIntercomm( &intercomm, &isLeft, 2 )) {
39         if (intercomm == MPI_COMM_NULL) continue;
40
41         MPI_Comm_rank(intercomm, &rank);
42
43         /* 0 ranks on each side communicate, everyone else does nothing */
44         if(rank == 0) {
45             if (isLeft) {
46                 recvlen = -1;
47                 MPI_Probe(0, 0, intercomm, &status);
48                 MPI_Get_count(&status, MPI_CHAR, &recvlen);
49                 if (recvlen != (strlen(test_str) + 1)) {
50                     printf(" Error: recvlen (%d) != strlen(\"%s\")+1 (%d)\n", recvlen, test_str, (int)strlen(test_str) + 1);
51                     ++errs;
52                 }
53                 buf[0] = '\0';
54                 MPI_Recv(buf, recvlen, MPI_CHAR, 0, 0, intercomm, &status);
55                 if (strcmp(test_str,buf)) {
56                     printf(" Error: strcmp(test_str,buf)!=0\n");
57                     ++errs;
58                 }
59             }
60             else {
61                 strncpy(buf, test_str, 5);
62                 MPI_Send(buf, strlen(buf)+1, MPI_CHAR, 0, 0, intercomm);
63             }
64         }
65         MTestFreeComm(&intercomm);
66     }
67
68     MTest_Finalize( errs );
69     MPI_Finalize();
70     return 0;
71 }