Mechy
Mechy

Reputation: 259

Why is my C++ MPI code freezing up on me?

I'm a noob when it comes to MPI, but this makes no sense. So I have a snip of code here that uses MPI_Recv and MPI_Send, but right after telling me my mesh size, the thing freezers before the first "I made it here." gets sent to the screen.

I don't understand why. There is literally nothing between the first "I made it here" and the last thing outputted to the screen.

Here's the snip of code

void initMesh(double* &phi, double &h, double &riptime, 
    double &deltat, int &x, int &y, int &xlength, int &ylength, int &tlength, int &ttasks, int &jtasks, int &itasks, int &tstart, int &jstart, int &istart, int &myrank, int &cores) {
    int tasksize, remains, tremains;
    MPI_Comm_rank(MPI_COMM_WORLD, &myrank);
    MPI_Comm_size(MPI_COMM_WORLD, &cores);
        if (myrank == 0) {
        cout << "How large would you like the mesh"
        <<" to be in the x-direction?" << endl;
        cin >> x;
        cout << "How large would you like the mesh"
        << " to be in the y-direction?\n";
        cin >> y;
        cout << "What is the distance between each x/y spot h (equal distance)?\n";
        cin >> h;
        cout << "How much time would you like the program to run for?\n";
        cin >> riptime;
        cout << "What would you like the time-step for the analysis to be?\n";
        cin >> deltat;
        xlength = (int) (x/h);
        ylength = (int) (y/h);
        tlength = (int) (riptime/deltat);
        cout << "Mesh x-points = " << xlength << endl;
        cout << "Mesh y-points = " << ylength << endl;
        cout << "Mesh time points = " << tlength << endl;
        cout << "I made it here!";
        }

//GOOD UP TO HERE!!! Then it freezes when I run the thing with 3 or more processors


        for (int i=1; i < cores; i++) {
            if (myrank==0) {
            cout << "I made it here!";
            MPI_Send(&xlength, 1, MPI_INT, i, 0, MPI_COMM_WORLD);
            }
            else {
            MPI_Recv(&xlength, 1, MPI_INT, 0, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
            cout << "I made it here!";
            }
        }
        for (int i=1; i < cores; i++) {
            if (myrank==0) {
            MPI_Send(&ylength, 1, MPI_INT, i, 1, MPI_COMM_WORLD);
            }
            else {
            MPI_Recv(&ylength, 1, MPI_INT, 0, 1, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
            }
        }
        for (int i=1; i < cores; i++) {
            if (myrank==0) {
            MPI_Send(&tlength, 1, MPI_INT, i, 2, MPI_COMM_WORLD);
            }
            else {
            MPI_Recv(&tlength, 1, MPI_INT, 0, 2, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
            }
        }
    cout << "I made it here!";

The above part of the code is where the trouble is now.

Upvotes: 0

Views: 831

Answers (1)

Elmar Peise
Elmar Peise

Reputation: 15413

As mentioned in the comments, you don't see "I made it here!", because you're missing the << endl.

As for the MPI: in each for loop, rank 0 seems to send something to every other rank. However, every other rank expects to receive one message for each iteration of the loop. What you want is every rank to receive just one per loop.

Actually, since you're sending the same information to every rank there are two even better alternatives:

  • Assuming each rank has the same routine input, compute the x-, y- and tlength redundantly on each process (way faster than communicating it).
  • If this is not an option, use MPI_Broadcast with rank 0 as a source.

Upvotes: 2

Related Questions