Home > front end >  (C) MPI Processes Fail to Respond After For Loop
(C) MPI Processes Fail to Respond After For Loop

Time:06-18

Suppose that I have a code block that attempts to calculate the first 15 numbers in the Fibonacci sequence and distributes each unique number among 3 processes (MPI_Send) using a for loop as shown in the code block below.

int main(int argc, char* argv[]) {

    int rank, size, recieve_data1, recieve_data2;
    MPI_Init(NULL, NULL);
    MPI_Status status;
    MPI_Comm_rank(MPI_COMM_WORLD, &rank);
    MPI_Comm_size(MPI_COMM_WORLD, &size);

    printf("Available ranks are: %d \n \n", rank); // first rank rollcall
    fflush(stdout);

    int num1 = 1; int num2 = 1;
    int RecieveNum; int SumNum;

    for
        (int n = 0; n < 16; n  ) {
        if
            (rank == 0) {

            // perform the fibb sequence algorithim
            SumNum = num1   num2;
            num1 = num2;
            num2 = SumNum;

            // define the sorting algorithim
            int DeliverTo = (n % 3)   1;

            // send calculated result
            MPI_Send(&SumNum, 1, MPI_INT, DeliverTo, 1, MPI_COMM_WORLD);
        }
        else {
            // recieve the element integer
            MPI_Recv(&RecieveNum, 1, MPI_INT, 0, 1, MPI_COMM_WORLD, &status);

            // print and flush the buffer
            printf("I am process rank %d, and I recieved the number %d. \n", rank, RecieveNum);
            fflush(stdout);
        }
    }
    printf("Available ranks are: %d \n \n", rank); // second rank roll call
    fflush(stdout);

/*

more code that I run...

*/

    MPI_Finalize();
    return 0;
}

Before the first for loop is called, processes 0,1,2,3 all respond to the printf(Available ranks are: %d \n \n", rank); on line 25. However, after executing the first for loop and using a second printf, only process 0 responds. I was expecting all 4 processes 0 - 3 to respond again after the execution of the for loop. To solve this problem, I isolated this section of code and attempted to debug for several hours with no sucess. This particular issue proves to be problematic, as I have additional code (not shown here for the sake of being concise), that will access the numbers generated from this sequence.

Finally, I am running the code by building the solution, running the VS terminal as an administrator, and typing mpiexec -n 4 my_file_name.exe. No build errors or complication mistakes occurred. From what I can see (correct me if I'm wrong), all processes hang after completing the for loop, but I am unsure why or how to fix it.

After searching the website, I did not see anything that answered this question (from my point of view). I am a bit of an MPI (and Stack Overflow) newbie, so any code pointers are also welcomed. Thanks

CodePudding user response:

You have process zero compute who to send to. And then everyone does a receive. That means that all processes that are not the computed receiver will hang.

This scenario where you send to a dynamically computed receiver is not easy to do in MPI. You either need to

  • send a message to all other processes "no, I have nothing for you", or
  • send a message to all, and all-but-one process ignores the data, or
  • use one sided operations where you MPI_Put the data on the computer receiver.
  •  Tags:  
  • c mpi
  • Related