5

I have a problem with my MPI C program. This is the code:

void wantEat(int p, int rank, char *state, char* stateLeft, char* stateRight){

    char *s;

    MPI_Status status ;

    /* if left or right neighbor is eating */

    if(compare(stateLeft, "eat") || compare (stateRight, "eat")){
        state = "want_Eat";
        printf("%s : I wait for eating\n", nomPhilosophe(rank));

        /* the process have to send his new state to his neighbors */

        MPI_Send(state, strlen(state)+1, MPI_CHAR,
                (rank - 1 + p) % p, 0, MPI_COMM_WORLD);
        MPI_Send(state, strlen(state)+1, MPI_CHAR,
                (rank + 1) % p, 0, MPI_COMM_WORLD);

        /* if only left neighbor is eating */

        if(compare(stateLeft,"eat") &&  !compare(stateRight,"eat")){

            /* Wait for left neighbor finishes eating */

            MPI_Recv(stateLeft, 6, MPI_CHAR, (rank - 1 + p) % p, 0,
                    MPI_COMM_WORLD, &status);
            /* and eat */
            state = "eat";
        }
        /* if only right neighbor is eating  */

        if(compare(stateRight,"eat") &&  !compare(stateLeft,"eat")){

            /* wait for right neighbor message */
            MPI_Recv(stateRight, 6, MPI_CHAR, (rank + 1) % p, 0,
                    MPI_COMM_WORLD, &status);
            /* and eat */
            state = "eat";
        }
        /* if both neighboors are eating */

        if(compare(stateRight,"eat") &&  compare(stateLeft,"eat")){

            /* wait for messages of the 2 neighbors */

            MPI_Recv(stateLeft, strlen("think")+1, MPI_CHAR, MPI_ANY_SOURCE, 0,
                    MPI_COMM_WORLD, &status);
            MPI_Recv(stateRight, strlen("think")+1, MPI_CHAR, MPI_ANY_SOURCE, 0,
                    MPI_COMM_WORLD, &status);

            /* and eat */
            state = "eat";
        }
    }

    /* if neighbors are not eating */

    else{
        /* eat */ 
        state= "eat";
    }

    /* send the new state to neighbors */

    MPI_Send(state, strlen(state)+1, MPI_CHAR,
            (rank - 1 + p) % p, 0, MPI_COMM_WORLD);
    MPI_Send(state, strlen(state)+1, MPI_CHAR,
            (rank + 1) % p, 0, MPI_COMM_WORLD);
}

int main(int argc, char* argv[]){
    int  my_rank; /* rank of process */
    int  p;       /* number of processes */

    char* state = "think";   /* state of process (think, eat, or want_eat) */
    char* stateLeft = "think";  /* state of the left neighbor of the      process  */
    char* etatD = "think";  /* state of the right neighbor of the process */

    /* start up MPI */

    MPI_Init(&argc, &argv);

    /* find out process rank */
    MPI_Comm_rank(MPI_COMM_WORLD, &my_rank); 

    /* find out number of processes */
    MPI_Comm_size(MPI_COMM_WORLD, &p); 

    think(my_rank); /* process "my_rank" is thinking */
    wantEat(p, my_rank, state, stateLeft, stateRight); /* process "my_rank" wants to eat */
    eat(my_rank); /* process "my_rank" is eating */ 



    /* shut down MPI */
    MPI_Finalize(); 


    return 0;
}

The problem is that all processes eat simultaneously without waiting for neighbors. I think that MPI sends of new states are not received by neighbors. Do you have an idea to fix this problem?

Gilles
  • 9,269
  • 4
  • 34
  • 53
test87
  • 61
  • 3
  • Your program starts and everyone is in state `"think"`. This means your if statement is skipped in all processes because no one is eating and everyone does the else statement and sets their state to `"eat"`. They then each call `MPI_Send` where they wait for their neighbour to call `MPI_Recv` which they can't because they're also stuck at `MPI_Send` and thus I imagine your program hangs? Am I missing something special in the functions you haven't shown? Why do you expect the behaviour to differ from this? – Samidamaru Dec 14 '15 at 15:06
  • In my "think" function, each process wait for a random time before it wants to eat, so processes don't want to eat at exactly same time. I think I should add an MPI_recv for each of my MPI_Send, but I don't know where. – test87 Dec 14 '15 at 15:26
  • Indeed, but each of the processes don't know anything else about their neighbours state until they `recv` it. It may be better to do a `MPI_Sendrecv` at the beginning of your `wantEat` function so that each process knows the up to date information about it's neighbours and then changes its state accordingly. `Sendrecv` will also help with hanging issues you may have as you don't have to worry then about the ordering of the sends and receives on different processes. – Samidamaru Dec 14 '15 at 15:28

0 Answers0