Я изучаю MPI с C и не понимаю, почему мой код не работает - PullRequest
0 голосов
/ 11 апреля 2020

Я новичок в 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;
}

Я не понимаю эту ошибку, и я не понимаю не знаю, как это отладить.

Спасибо, если вы можете помочь мне решить эту (возможно, идиотскую) проблему!

1 Ответ

0 голосов
/ 12 апреля 2020

Основная ошибка в том, что ваша функция называется send(), и это конфликтует с функцией lib c, и, следовательно, приводит к этому странному поведению.

Во избежание другого неопределенного поведения вызванные использованием неинициализированных данных, вы также должны отправить завершающий символ NULL, например,

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