1 /* Copyright (c) 2014. The SimGrid Team.
2 * All rights reserved. */
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. */
9 #include <simgrid/modelchecker.h>
15 int main(int argc, char **argv){
20 xbt_dynar_t requests = xbt_dynar_new(sizeof(int), NULL);
23 int err = MPI_Init(&argc, &argv);
24 if(err != MPI_SUCCESS){
25 printf("MPI initialization failed !\n");
29 MC_ignore(&(status.count), sizeof(status.count));
31 /* Get number of processes */
32 MPI_Comm_size(MPI_COMM_WORLD, &size);
33 /* Get id of this process */
34 MPI_Comm_rank(MPI_COMM_WORLD, &rank);
36 if(rank == 0){ /* Coordinator */
39 MPI_Recv(&recv_buff, 1, MPI_INT, MPI_ANY_SOURCE, MPI_ANY_TAG, MPI_COMM_WORLD, &status);
40 if(status.MPI_TAG == REQUEST_TAG){
42 printf("CS already used. Queue the request.\n");
43 xbt_dynar_push(requests, &recv_buff);
45 printf("CS idle. Grant immediatly.\n");
46 MPI_Send(&rank, 1, MPI_INT, recv_buff, GRANT_TAG, MPI_COMM_WORLD);
50 if(!xbt_dynar_is_empty(requests)){
51 printf("CS release. Grant to queued requests (queue size: %lu)", xbt_dynar_length(requests));
52 xbt_dynar_shift(requests, &recv_buff);
53 MPI_Send(&rank, 1, MPI_INT, recv_buff, GRANT_TAG, MPI_COMM_WORLD);
56 printf("CS release. Resource now idle.\n");
63 printf("%d asks the request.\n", rank);
64 MPI_Send(&rank, 1, MPI_INT, 0, REQUEST_TAG, MPI_COMM_WORLD);
66 MPI_Recv(&recv_buff, 1, MPI_INT, 0, MPI_ANY_TAG, MPI_COMM_WORLD, &status);
68 printf("%d got the answer. Release it.\n", rank);
69 MPI_Send(&rank, 1, MPI_INT, 0, RELEASE_TAG, MPI_COMM_WORLD);