我正在用 C 学习 MPI,但不明白为什么我的代码不起作用

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

我是 MPI 的新手,我想要我的 C 程序输出这个:

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

但是当我用 mpirun -n 2 ./main 启动它时它给了我这个错误并且程序不工作:

*** 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
--------------------------------------------------------------------------

这是我的代码:

#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;
}

我不明白那个错误,我不知道如何调试它。

如果你能帮我解决那个(可能是白痴)问题,谢谢!

主要错误是你的函数被调用send(),这与libc的函数冲突,因此导致这种奇怪的行为。

为了避免使用未初始化数据引起的其他未定义行为,您还必须发送 NULL 终止字符,例如

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