Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'mc' into mc++
[simgrid.git] / examples / smpi / mc / bugged1_liveness.c
1 /* Copyright (c) 2013-2014. The SimGrid Team.
2  * All rights reserved.                                                     */
3
4 /* This program is free software; you can redistribute it and/or modify it
5  * under the terms of the license (GNU LGPL) which comes with this package. */
6
7 /***************** Centralized Mutual Exclusion Algorithm *********************/
8 /* This example implements a centralized mutual exclusion algorithm.          */
9 /* Bug : CS requests of process 1 not satisfied                                      */
10 /* LTL property checked : G(r->F(cs)); (r=request of CS, cs=CS ok)            */
11 /******************************************************************************/
12
13 #include <stdio.h>
14 #include <mpi.h>
15 #include <simgrid/modelchecker.h>
16
17 #define GRANT_TAG 0
18 #define REQUEST_TAG 1
19 #define RELEASE_TAG 2
20
21 int r, cs;
22
23 static int predR(){
24   return r;
25 }
26
27 static int predCS(){
28   return cs;
29 }
30
31
32 int main(int argc, char **argv){
33
34   int err, size, rank;
35   int recv_buff;
36   MPI_Status status;
37   int CS_used = 0;
38   xbt_dynar_t requests = xbt_dynar_new(sizeof(int), NULL);
39   
40   /* Initialize MPI */
41   err = MPI_Init(&argc, &argv);
42   if(err !=  MPI_SUCCESS){
43     printf("MPI initialization failed !\n");
44     exit(1);
45   }
46
47   MC_automaton_new_propositional_symbol("r", &predR);
48   MC_automaton_new_propositional_symbol("cs", &predCS);
49
50   MC_ignore(&(status.count), sizeof(status.count));
51
52   /* Get number of processes */
53   err = MPI_Comm_size(MPI_COMM_WORLD, &size);
54   /* Get id of this process */
55   err = MPI_Comm_rank(MPI_COMM_WORLD, &rank);
56
57   if(rank == 0){ /* Coordinator */
58     while(1){
59       MPI_Recv(&recv_buff, 1, MPI_INT, MPI_ANY_SOURCE, MPI_ANY_TAG, MPI_COMM_WORLD, &status);
60       if(status.MPI_TAG == REQUEST_TAG){
61         if(CS_used){
62           printf("CS already used.\n");
63           xbt_dynar_push(requests, &recv_buff);
64         }else{
65           if(recv_buff != 1){
66             printf("CS idle. Grant immediatly.\n");
67             MPI_Send(&rank, 1, MPI_INT, recv_buff, GRANT_TAG, MPI_COMM_WORLD);
68             CS_used = 1;
69           }
70         }
71       }else{
72         if(!xbt_dynar_is_empty(requests)){
73           printf("CS release. Grant to queued requests (queue size: %lu)", xbt_dynar_length(requests));
74           xbt_dynar_shift(requests, &recv_buff);
75           if(recv_buff != 1){
76             MPI_Send(&rank, 1, MPI_INT, recv_buff, GRANT_TAG, MPI_COMM_WORLD);
77             CS_used = 1;
78           }else{
79             xbt_dynar_push(requests, &recv_buff);
80             CS_used = 0;
81           }
82         }else{
83           printf("CS release. Resource now idle.\n");
84           CS_used = 0;
85         }
86       }
87     }
88   }else{ /* Client */
89     while(1){
90       printf("%d asks the request.\n", rank);
91       MPI_Send(&rank, 1, MPI_INT, 0, REQUEST_TAG, MPI_COMM_WORLD);
92       if(rank == 1){
93         r = 1;
94         cs = 0;
95       }
96       MPI_Recv(&recv_buff, 1, MPI_INT, 0, MPI_ANY_TAG, MPI_COMM_WORLD, &status);
97       if(status.MPI_TAG == GRANT_TAG && rank == 1){
98         cs = 1;
99         r = 0;
100       }
101       printf("%d got the answer. Release it.\n", rank);
102       MPI_Send(&rank, 1, MPI_INT, 0, RELEASE_TAG, MPI_COMM_WORLD);
103       if(rank == 1){
104         r = 0;
105         cs = 0;
106       }
107     }
108   }
109
110   MPI_Finalize();
111
112   return 0;
113 }