1 /* Copyright (c) 2014-2022. 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>
10 #include <xbt/dynar.h>
16 int main(int argc, char **argv){
21 xbt_dynar_t requests = xbt_dynar_new(sizeof(int), NULL);
24 int err = MPI_Init(&argc, &argv);
25 if(err != MPI_SUCCESS){
26 printf("MPI initialization failed !\n");
30 MC_ignore(&status.count, sizeof status.count);
32 /* Get number of processes */
33 MPI_Comm_size(MPI_COMM_WORLD, &size);
34 /* Get id of this process */
35 MPI_Comm_rank(MPI_COMM_WORLD, &rank);
37 if(rank == 0){ /* Coordinator */
40 MPI_Recv(&recv_buff, 1, MPI_INT, MPI_ANY_SOURCE, MPI_ANY_TAG, MPI_COMM_WORLD, &status);
41 if(status.MPI_TAG == REQUEST_TAG){
43 printf("CS already used. Queue the request.\n");
44 xbt_dynar_push(requests, &recv_buff);
46 printf("CS idle. Grant immediately.\n");
47 MPI_Send(&rank, 1, MPI_INT, recv_buff, GRANT_TAG, MPI_COMM_WORLD);
51 if(!xbt_dynar_is_empty(requests)){
52 printf("CS release. Grant to queued requests (queue size: %lu)", xbt_dynar_length(requests));
53 xbt_dynar_shift(requests, &recv_buff);
54 MPI_Send(&rank, 1, MPI_INT, recv_buff, GRANT_TAG, MPI_COMM_WORLD);
57 printf("CS release. Resource now idle.\n");
64 printf("%d asks the request.\n", rank);
65 MPI_Send(&rank, 1, MPI_INT, 0, REQUEST_TAG, MPI_COMM_WORLD);
67 MPI_Recv(&recv_buff, 1, MPI_INT, 0, MPI_ANY_TAG, MPI_COMM_WORLD, &status);
69 printf("%d got the answer. Release it.\n", rank);
70 MPI_Send(&rank, 1, MPI_INT, 0, RELEASE_TAG, MPI_COMM_WORLD);