MPI,我更改了一个非常简单的程序以使用 INT 而不是 CHAR 作为消息缓冲区的数据类型,但现在它失败了,这是为什么?

MPI, I changed a very simple program to use INT instead of CHAR as the data type of the messages buffer and now it fails, why is that?

我将 the MPI example on the wikipedia page 修改为使用整数而不是字符。

结果是:

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

int main(int argc, char **argv)
{
    int buf[4];
    int my_rank, num_procs;

    /* Initialize the infrastructure necessary for communication */
    MPI_Init(&argc, &argv);

    /* Identify this process */
    MPI_Comm_rank(MPI_COMM_WORLD, &my_rank);

    /* Find out how many total processes are active */
    MPI_Comm_size(MPI_COMM_WORLD, &num_procs);
    int test[4] = {0, 1, 2};

    /* Until this point, all programs have been doing exactly the same.
       Here, we check the rank to distinguish the roles of the programs */
    if (my_rank == 0) {
        int other_rank;
        printf("We have %i processes.\n", num_procs);

        /* Send messages to all other processes */
        for (other_rank = 1; other_rank < num_procs; other_rank++)
        {
            memcpy(buf, test, 4 * sizeof(int));
            MPI_Send(buf, sizeof(buf), MPI_INT, other_rank,
                     0, MPI_COMM_WORLD);
        }

        /* Receive messages from all other process */
        for (other_rank = 1; other_rank < num_procs; other_rank++)
        {
            MPI_Recv(buf, sizeof(buf), MPI_INT, other_rank,
                     0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
            printf("%d\n", buf[other_rank]);
        }

    } else {

        /* Receive message from process #0 */
        MPI_Recv(buf, sizeof(buf), MPI_INT, 0,
                 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
        memcpy(buf, test, 4 * sizeof(int));
        MPI_Send(buf, sizeof(buf), MPI_INT, 0,
                 0, MPI_COMM_WORLD);

    }

    /* Tear down the communication infrastructure */
    MPI_Finalize();
    return 0;
}

显然代码应该做与之前传递整数数组而不是字符串的错误相同的事情,实际上我收到了这个错误:

YOUR APPLICATION TERMINATED WITH THE EXIT STRING: Illegal instruction
(signal 4) This typically refers to a problem with your application.
Please see the FAQ page for debugging suggestions

我这里有什么地方做错了吗?

我正在使用这个简单的命令进行编译和执行:

mpicc example.c && mpiexec -n 4 ./a.out

评论 int MPI_Send(const void *buf, int count, MPI_Datatype datatype, int dest, int tag, MPI_Comm comm) 和其他人。

count 是发送缓冲区中的元素数,而不是它的 size

代码应该是:

// MPI_Send(buf, sizeof(buf), MPI_INT, other_rank, 0, MPI_COMM_WORLD);`
MPI_Send(buf, sizeof buf /sizeof buf[0], MPI_INT, other_rank, 0, MPI_COMM_WORLD);`

检查 MPI_Recv(); 和所有类似的电话。