Mike
Mike

Reputation: 390

Issue with MPI_Sendrecv using 2D Matrix

As I know, MPI_Sendrecv requires two distinct buffers for send and receive. My following code sends (N/P) chunks of rows to P-1 processors however it does not work and gives me a frozen screen. I tried to make sure everything is correct but I don't see where is the problem here (I omitted the variables declarations to make it short)

int **M, **FinalM, **M0;
M = malloc(N * sizeof (int *));
for (i = 0; i < N; i++) {
    M[i] = malloc(N * sizeof (int));
}
FinalM = malloc(N * sizeof (int *));
for (i = 0; i < N; i++) {
    FinalM[i] = malloc(n * sizeof (int));
}

M0 = malloc(N/P * sizeof (int *));
for (i = 0; i < N/P; i++) {
    M0[i] = malloc(N * sizeof (int));
}

c = N/P; // P is Number of Processors and N rows
if (rank == 0) {
    for (i = 0; i < P; i++) {
        k = i*c;
        k1 = (i + 1) * c;
        for (j = k; j < k1; j++) {
            MPI_Sendrecv(M[j], N, MPI_INT, i, TAG, FinalM[j], N, MPI_INT, i, TAG, MPI_COMM_WORLD, &status[d]);
        }
    }
} else {

    for (i = 0; i < (N / P); i++) {
        MPI_Recv(M0[i], N, MPI_INT, 0, TAG, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
    }
}

Kindly, can someone give me a hint to solve this problem? Thank you.

Upvotes: 0

Views: 164

Answers (1)

Mad Physicist
Mad Physicist

Reputation: 114310

From the docs at https://www.open-mpi.org/doc/v1.8/man3/MPI_Sendrecv.3.php#toc7:

MPI_Sendrecv executes a blocking send and receive operation.

In your case, the master loop sends a portion of the matrix to the first worker and blocks, waiting for the worker to respond. There is no response, so it hangs forever. You need to either use just MPI_Send in the master loop, followed by another loop for MPI_Recv. The worker has to send something back using MPI_Send.

Upvotes: 1

Related Questions