简体   繁体   中英

Im learning MPI with C and don't understand why my code don't work

I'm new to MPI and I want my C program, which needs to be launched with two process, to output this:

Hello
Good bye
Hello
Good bye
... (20 times)

But when I start it with mpirun -n 2./main it gives me this error and the program don't work:

*** An error occurred in MPI_Send
*** on a NULL communicator
*** MPI_ERRORS_ARE_FATAL (processes in this communicator will now abort,
***    and potentially your MPI job)
[laptop:3786023] Local abort before MPI_INIT completed completed successfully, but am not able to aggregate error messages, and not able to guarantee that all other processes were killed!
--------------------------------------------------------------------------
Primary job  terminated normally, but 1 process returned
a non-zero exit code. Per user-direction, the job has been aborted.
--------------------------------------------------------------------------
*** An error occurred in MPI_Send
*** on a NULL communicator
*** MPI_ERRORS_ARE_FATAL (processes in this communicator will now abort,
***    and potentially your MPI job)
[laptop:3786024] Local abort before MPI_INIT completed completed successfully, but am not able to aggregate error messages, and not able to guarantee that all other processes were killed!
--------------------------------------------------------------------------
mpirun detected that one or more processes exited with non-zero status, thus causing
the job to be terminated. The first process to do so was:

  Process name: [[61908,1],0]
  Exit code:    1
--------------------------------------------------------------------------

Here's my code:

#include <stdio.h>
#include <stdlib.h>
#include <mpi.h>
#include <unistd.h>
#include <string.h>
#define N 32

void send (int rank)
{
    char mess[N];
    if (rank == 0)
        sprintf(mess, "Hello");
    else
        sprintf(mess, "Good bye");
    MPI_Send(mess, strlen(mess), MPI_CHAR, !rank, 0, MPI_COMM_WORLD);
}

void receive (int rank)
{
    char buf[N];
    MPI_Status status;
    MPI_Recv(buf, N, MPI_CHAR, !rank, 0, MPI_COMM_WORLD, &status);
    printf("%s\n", buf);
}

int main (int argc, char **argv)
{
    if (MPI_Init(&argc, &argv)) {
        fprintf(stderr, "Erreur MPI_Init\n");
        exit(1);
    }

    int size, rank;
    MPI_Comm_rank(MPI_COMM_WORLD, &rank);
    MPI_Comm_size(MPI_COMM_WORLD, &size);

    for (size_t i = 0; i < 20; i ++) {
        if (rank == 0) {
            send(rank);
            receive(rank);
        } else {
            receive(rank);
            send(rank);
        }
    }

    MPI_Finalize();
    return 0;
}

I don't understand that error and I don't know how to debug that.

Thank you if you can help me to solve that (probably idiot) problem !

The main error is your function is called send() , and that conflicts with the function of the libc, and hence results in this bizarre behavior.

In order to avoid an other undefined behavior caused by using uninitialized data, you also have to send the NULL terminating character, eg

MPI_Send(mess, strlen(mess)+1, MPI_CHAR, !rank, 0, MPI_COMM_WORLD);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM