Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
675f072db8c42ff7d2f703577d3bfe01794c4ade
[simgrid.git] / teshsuite / smpi / mpich3-test / pt2pt / greq1.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[] = "Simple test of generalized requests";
13 */
14
15
16 int query_fn( void *extra_state, MPI_Status *status );
17 int query_fn( void *extra_state, MPI_Status *status )
18 {
19     /* Set a default status */
20     status->MPI_SOURCE = MPI_UNDEFINED;
21     status->MPI_TAG    = MPI_UNDEFINED;
22     MPI_Status_set_cancelled( status, 0 );
23     MPI_Status_set_elements( status, MPI_BYTE, 0 );
24     return 0;
25 }
26 int free_fn( void *extra_state );
27 int free_fn( void *extra_state )
28 {
29     int *b = (int *)extra_state;
30     if (b) *b = *b - 1;
31     /* The value returned by the free function is the error code
32        returned by the wait/test function */
33     return 0;
34 }
35 int cancel_fn( void *extra_state, int complete );
36 int cancel_fn( void *extra_state, int complete )
37 {
38     return 0;
39 }
40
41 /*
42  * This is a very simple test of generalized requests.  Normally, the
43  * MPI_Grequest_complete function would be called from another routine,
44  * often running in a separate thread.  This simple code allows us to
45  * check that requests can be created, tested, and waited on in the
46  * case where the request is complete before the wait is called.  
47  *
48  * Note that MPI did *not* define a routine that can be called within
49  * test or wait to advance the state of a generalized request.  
50  * Most uses of generalized requests will need to use a separate thread.
51  */
52 int main( int argc, char *argv[] )
53 {
54     int errs = 0;
55     int counter, flag;
56     MPI_Status    status;
57     MPI_Request   request;
58
59     MTest_Init( &argc, &argv );
60
61     MPI_Grequest_start( query_fn, free_fn, cancel_fn, NULL, &request );
62     
63     MPI_Test( &request, &flag, &status );
64     if (flag) {
65         errs++;
66         fprintf( stderr, "Generalized request marked as complete\n" );
67     }
68
69     MPI_Grequest_complete( request );
70
71     MPI_Wait( &request, &status );
72
73     counter = 1;
74     MPI_Grequest_start( query_fn, free_fn, cancel_fn, &counter, &request );
75     MPI_Grequest_complete( request );
76     MPI_Wait( &request, MPI_STATUS_IGNORE );
77     
78     if (counter) {
79         errs++;
80         fprintf( stderr, "Free routine not called, or not called with extra_data" );
81     }
82
83     MTest_Finalize( errs );
84     MPI_Finalize();
85     return 0;
86 }