Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
remove unwanted files
[simgrid.git] / teshsuite / smpi / mpich-test / pt2pt / third.c
1 /*
2     third - test program that tests queueing by sending messages with various
3             tags, receiving them in particular order.
4  */
5
6 #include <stdio.h>
7 #include <string.h>
8 #include "mpi.h"
9 #include "test.h"
10 #ifdef HAVE_UNISTD_H
11 /* For sleep */
12 #include <unistd.h>
13 #endif
14
15 #ifndef HAVE_SLEEP
16 void sleep( int secs )
17 {
18 #ifdef VX_WORKS
19     /* Also needs include <time.h>? */
20     struct timespec rqtp = { 10, 0 };
21     nanosleep(&rqtp, NULL);
22 #else
23     double t;
24     t = MPI_Wtime();
25     while (MPI_Wtime() - t < (double)secs) ;
26 #endif
27 }
28 #endif
29
30 /* Define VERBOSE to get printed output */
31 int main( int argc, char **argv )
32 {
33     int rank, size, to, from, tag, count;
34     int src, dest, waiter;
35     int st_count;
36 #ifdef VERBOSE
37     int st_source, st_tag;
38 #endif
39     MPI_Status status;
40     char data[100];
41     MPI_Request rq[2];
42     MPI_Status statuses[2];
43
44     MPI_Init( &argc, &argv );
45     MPI_Comm_rank( MPI_COMM_WORLD, &rank );
46     MPI_Comm_size( MPI_COMM_WORLD, &size );
47 /*
48     src  = size - 1;
49     dest = 0;
50  */
51     src = 0;
52     dest = size - 1;
53     /* waiter = dest; */        /* Receiver delays, so msgs unexpected */
54     /* waiter = src;  */        /* Sender delays, so recvs posted      */
55     waiter = 10000;             /* nobody waits */
56
57     if (rank == src)
58     {
59         if (waiter == src)
60             sleep(10);
61         to     = dest;
62         tag    = 2001;
63         sprintf(data,"First message, type 2001");
64         count = strlen(data) + 1;
65         MPI_Isend( data, count, MPI_CHAR, to, tag, MPI_COMM_WORLD, &rq[0] );
66 #ifdef VERBOSE  
67         printf("%d sent :%s:\n", rank, data );
68 #endif
69         tag    = 2002;
70         sprintf(data,"Second message, type 2002");
71         count = strlen(data) + 1;
72         MPI_Isend( data, count, MPI_CHAR, to, tag, MPI_COMM_WORLD, &rq[1] );
73         MPI_Waitall( 2, rq, statuses );
74 #ifdef VERBOSE  
75         printf("%d sent :%s:\n", rank, data );
76 #endif
77     }
78     else
79     if (rank == dest)
80     {
81         if (waiter == dest)
82             sleep(10);
83         from  = MPI_ANY_SOURCE;
84         count = 100;            
85
86         tag   = 2002;
87         MPI_Recv(data, count, MPI_CHAR, from, tag, MPI_COMM_WORLD, &status ); 
88
89         MPI_Get_count( &status, MPI_CHAR, &st_count );
90         if (st_count != strlen("Second message, type 2002") + 1) {
91             printf( "Received wrong length!\n" );
92             }
93 #ifdef VERBOSE  
94         st_source = status.MPI_SOURCE;
95         st_tag    = status.MPI_TAG;
96         printf( "Status info: source = %d, tag = %d, count = %d\n",
97                 st_source, st_tag, st_count );
98         printf( "%d received :%s:\n", rank, data);
99 #endif
100         tag   = 2001;
101         MPI_Recv(data, count, MPI_CHAR, from, tag, MPI_COMM_WORLD, &status ); 
102
103         MPI_Get_count( &status, MPI_CHAR, &st_count );
104         if (st_count != strlen("First message, type 2001") + 1) {
105             printf( "Received wrong length!\n" );
106             }
107 #ifdef VERBOSE  
108         st_source = status.MPI_SOURCE;
109         st_tag    = status.MPI_TAG;\
110         printf( "Status info: source = %d, tag = %d, count = %d\n",
111                 st_source, st_tag, st_count );
112         printf( "%d received :%s:\n", rank, data);
113 #endif
114     }
115 #ifdef VERBOSE  
116     printf( "Process %d exiting\n", rank );
117 #endif
118     Test_Waitforall( );
119     MPI_Finalize();
120     return 0;
121 }