Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
handle nested datatypes in smpi (structs of vectors for example), which previously...
[simgrid.git] / teshsuite / smpi / mpich-test / pt2pt / probe1.c
1 /* 
2    This is a test of probe to receive a message of unknown type (used as a
3    server)
4  */
5 #include <stdio.h>
6 #include <string.h>
7 #include "mpi.h"
8 #include "test.h"
9
10 int main( int argc, char **argv ) 
11 {
12 int data, to, from, tag, maxlen, np, myid, flag, dest, src;
13 MPI_Status status, status1;
14
15 MPI_Init( &argc, &argv );
16 MPI_Comm_rank( MPI_COMM_WORLD, &myid );
17 MPI_Comm_size( MPI_COMM_WORLD, &np );
18
19 /* dest writes out the received stats; for the output to be
20    consistant (with the final check), it should be procees 0 */
21 if (argc > 1 && argv[1] && strcmp( "-alt", argv[1] ) == 0) {
22     dest = np - 1;
23     src  = 0;
24     }
25 else {
26     src  = np - 1;
27     dest = 0;
28     }
29
30 if (myid == src) {
31     to   = dest;
32     tag = 2000;
33 #ifdef VERBOSE
34     printf( "About to send\n" );
35 #endif
36     MPI_Send( &data, 1, MPI_INT, to, tag, MPI_COMM_WORLD );
37     tag = 2001;
38 #ifdef VERBOSE
39     printf( "About to send 'done'\n" );
40 #endif
41     MPI_Send( &data, 1, MPI_INT, to, tag, MPI_COMM_WORLD );
42     }
43 else {
44     /* Server loop */
45     while (1) {
46         tag    = MPI_ANY_TAG;
47         from   = MPI_ANY_SOURCE;
48         /* Should really use MPI_Probe, but functionally this will work
49            (it is less efficient, however) */
50         do {            
51             MPI_Iprobe( from, tag, MPI_COMM_WORLD, &flag, &status );
52             } while (!flag);
53         if (status.MPI_TAG == 2001) {
54 #ifdef VERBOSE
55             printf( "Received terminate message\n" );
56 #endif
57             /* Actually need to receive it ... */
58             MPI_Recv( &data, 1, MPI_INT, status.MPI_SOURCE, 
59                       status.MPI_TAG, MPI_COMM_WORLD, &status1 );
60             break;
61             }
62         if (status.MPI_TAG == 2000) {
63             MPI_Get_count( &status, MPI_INT, &maxlen );
64             if (maxlen > 1)
65                 printf( "Error; size = %d\n", maxlen );
66 #ifdef VERBOSE
67             printf( "About to receive\n" );
68 #endif
69             MPI_Recv( &data, 1, MPI_INT, status.MPI_SOURCE, 
70                       status.MPI_TAG, MPI_COMM_WORLD, &status1 );
71             }
72         }
73     }
74 MPI_Barrier( MPI_COMM_WORLD );
75 Test_Waitforall( );
76 MPI_Finalize();
77 return 0;
78 }