the_martian
the_martian

Reputation: 634

For loop exiting after one loop - MPI

I'm working on a send/receive MPI program and for some reason the loop only iterates once and then exits. Does anyone have any idea why?

The code:

#include <mpi.h>
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[]) {
    int rank;
    int b2, a1;
    MPI_Status status;
    MPI_Init(NULL, NULL);
    MPI_Comm_rank(MPI_COMM_WORLD, &rank);

    for (int j = 0; j < 100; j++) {
        if (rank == 0) {
            a1 = 10;
            MPI_Sendrecv(&a1, 1, MPI_INT, 1, 99,
                    &b2, 1, MPI_INT, 1, 99,
                    MPI_COMM_WORLD, MPI_STATUS_IGNORE);

        } else {
            a1 = 20;
            MPI_Sendrecv(&a1, 1, MPI_INT, 0, 99,
                    &b2, 1, MPI_INT, 0, 99,
                    MPI_COMM_WORLD, MPI_STATUS_IGNORE);
        }
    }

    printf("Process %d: received %d\n", rank, b2);
    MPI_Finalize();

}

The output (This should be repeating multiple times but this is all I get):

Process 1: received 10
Process 0: received 20

Upvotes: 0

Views: 152

Answers (1)

John Zwinck
John Zwinck

Reputation: 249582

Your printf() is not in the loop. It is no surprise that it is only printed once.

Upvotes: 2

Related Questions