Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
fff1082c4f255bae81ab90ae9ce82cc1b110971e
[simgrid.git] / examples / smpi / mc / bugged1_liveness.c
1 /* Copyright (c) 2013-2015. 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 /* Run :
14   /usr/bin/time -f "clock:%e user:%U sys:%S swapped:%W exitval:%x max:%Mk" "$@" ../../../smpi_script/bin/smpirun -hostfile hostfile_bugged1_liveness -platform ../../platforms/cluster.xml --cfg=contexts/factory:ucontext --cfg=model-check/reduction:none --cfg=model-check/property:promela_bugged1_liveness --cfg=smpi/send_is_detached_thresh:0 --cfg=contexts/stack_size:128 --cfg=model-check/visited:100000 --cfg=model-check/max_depth:100000 ./bugged1_liveness */
15
16 #include <stdio.h>
17 #include <mpi.h>
18 #include <simgrid/modelchecker.h>
19
20 #define GRANT_TAG 0
21 #define REQUEST_TAG 1
22 #define RELEASE_TAG 2
23
24 int r, cs;
25
26 int main(int argc, char **argv){
27   int err, size, rank;
28   int recv_buff;
29   MPI_Status status;
30   int CS_used = 0;
31   xbt_dynar_t requests = xbt_dynar_new(sizeof(int), NULL);
32
33   /* Initialize MPI */
34   err = MPI_Init(&argc, &argv);
35   if(err !=  MPI_SUCCESS){
36     printf("MPI initialization failed !\n");
37     exit(1);
38   }
39
40   MC_automaton_new_propositional_symbol_pointer("r", &r);
41   MC_automaton_new_propositional_symbol_pointer("cs", &cs);
42
43   MC_ignore(&(status.count), sizeof(status.count));
44
45   /* Get number of processes */
46   MPI_Comm_size(MPI_COMM_WORLD, &size);
47   /* Get id of this process */
48   MPI_Comm_rank(MPI_COMM_WORLD, &rank);
49
50   if(rank == 0){ /* Coordinator */
51     while(1){
52       MPI_Recv(&recv_buff, 1, MPI_INT, MPI_ANY_SOURCE, MPI_ANY_TAG, MPI_COMM_WORLD, &status);
53       if(status.MPI_TAG == REQUEST_TAG){
54         if(CS_used){
55           printf("CS already used.\n");
56           xbt_dynar_push(requests, &recv_buff);
57         }else{
58           if(recv_buff != size - 1){
59             printf("CS idle. Grant immediatly.\n");
60             MPI_Send(&rank, 1, MPI_INT, recv_buff, GRANT_TAG, MPI_COMM_WORLD);
61             CS_used = 1;
62           }
63         }
64       }else{
65         if(!xbt_dynar_is_empty(requests)){
66           printf("CS release. Grant to queued requests (queue size: %lu)", xbt_dynar_length(requests));
67           xbt_dynar_shift(requests, &recv_buff);
68           if(recv_buff != size - 1){
69             MPI_Send(&rank, 1, MPI_INT, recv_buff, GRANT_TAG, MPI_COMM_WORLD);
70             CS_used = 1;
71           }else{
72             xbt_dynar_push(requests, &recv_buff);
73             CS_used = 0;
74           }
75         }else{
76           printf("CS release. Resource now idle.\n");
77           CS_used = 0;
78         }
79       }
80     }
81   }else{ /* Client */
82     while(1){
83       printf("%d asks the request.\n", rank);
84       MPI_Send(&rank, 1, MPI_INT, 0, REQUEST_TAG, MPI_COMM_WORLD);
85       if(rank == size - 1){
86         r = 1;
87         cs = 0;
88       }
89       MPI_Recv(&recv_buff, 1, MPI_INT, 0, MPI_ANY_TAG, MPI_COMM_WORLD, &status);
90       if(status.MPI_TAG == GRANT_TAG && rank == size - 1){
91         cs = 1;
92         r = 0;
93       }
94       printf("%d got the answer. Release it.\n", rank);
95       MPI_Send(&rank, 1, MPI_INT, 0, RELEASE_TAG, MPI_COMM_WORLD);
96       if(rank == size - 1){
97         r = 0;
98         cs = 0;
99       }
100     }
101   }
102
103   MPI_Finalize();
104
105   return 0;
106 }