发送数组时 MPI_Recv 发生错误

An error occured in MPI_Recv while sending an array

#include <stdio.h>
#include <stdlib.h>
#include "mpi.h"
int main(int argc, char **argv)
{
    int N;
    scanf("%d", &N);
    double *a = (double *)malloc(N * sizeof(double));
    int i, rank, size, tag = 99, tag1 = 100;
    MPI_Status status;
    MPI_Init(&argc, &argv);
    MPI_Comm_size(MPI_COMM_WORLD, &size);
    MPI_Comm_rank(MPI_COMM_WORLD, &rank);
    if (rank == 0) 
    {
        for(int j=0;j<N;++j)
        {
            a[j] = j+0.1;
        }
        for (i = 1; i < size; i++)
        {
            MPI_Send(&N, 1, MPI_INT, i, tag1, MPI_COMM_WORLD);
            MPI_Send(a, N, MPI_DOUBLE, i, tag, MPI_COMM_WORLD);
        }
    }
    else 
    {
        MPI_Recv(&N, 1, MPI_INT, 0, tag1, MPI_COMM_WORLD, &status);
        MPI_Recv(a, N, MPI_DOUBLE, 0, tag, MPI_COMM_WORLD, &status);
        // for(int j=0;j<N*2;++j)
            // printf("%d %f\n", rank, a[j]);
    }
    MPI_Barrier(MPI_COMM_WORLD);
    printf("Message from process %d : %f\n", rank, a[rank]);
    MPI_Finalize();
    return 0;
} 

我正在第 0 个进程中创建数组 'a' 并将其发送到其余进程。但是我在执行此操作时遇到以下错误。

[nikhil:8599] *** An error occurred in MPI_Recv
[nikhil:8599] *** reported by process [4228579329,1]
[nikhil:8599] *** on communicator MPI_COMM_WORLD
[nikhil:8599] *** MPI_ERR_BUFFER: invalid buffer pointer
[nikhil:8599] *** MPI_ERRORS_ARE_FATAL (processes in this communicator will now abort,
[nikhil:8599] ***    and potentially your MPI job)
[nikhil:08593] 2 more processes have sent help message help-mpi-errors.txt / mpi_errors_are_fatal
[nikhil:08593] Set MCA parameter "orte_base_help_aggregate" to 0 to see all help / error messages

任何人都可以解释为什么我会收到此错误吗?

正如您在代码中看到的那样,有一个 for 循环,其中包含一个带有注释的打印语句。奇怪的是......在取消注释该循环时。一切正常。

想法:

  1. MPI_Init 应该是您程序中的第一件事。
  2. 只有一个排名应该 scanf
  3. N 未在队列间通信,因此您正在分配未定义大小的内存。
  4. 定义变量尽可能接近它们的使用点。将 int i 放在函数的顶部是一场等待发生的灾难。
  5. 最后的屏障是不必要的。
  6. 所有列需要自己分配内存

这让我们得到这段代码:

#include <stdio.h>
#include <stdlib.h>
#include "mpi.h"

int main(int argc, char **argv){
    MPI_Init(&argc, &argv);

    const int tag = 99;
    const int tag1 = 100;

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

    double *a; //Pointer to the memory we will allocate
    int N;

    if (rank == 0){
        scanf("%d", &N);

        a = (double *)malloc(N * sizeof(double));

        for(int j=0;j<N;++j){
            a[j] = j+0.1;
        }
        for (int i = 1; i < size; i++){
            MPI_Send(&N, 1, MPI_INT, i, tag1, MPI_COMM_WORLD);
            MPI_Send(a, N, MPI_DOUBLE, i, tag, MPI_COMM_WORLD);
        }
    } else {
        MPI_Status status;
        MPI_Recv(&N, 1, MPI_INT, 0, tag1, MPI_COMM_WORLD, &status);
        //Have to allocate memory on all ranks
        a = (double *)malloc(N * sizeof(double)); 
        MPI_Recv(a, N, MPI_DOUBLE, 0, tag, MPI_COMM_WORLD, &status);
        // for(int j=0;j<N*2;++j)
            // printf("%d %f\n", rank, a[j]);
    }

    printf("Message from process %d : %f\n", rank, a[rank]);

    MPI_Finalize();
    return 0;
} 

做得更好

广播命令是你的朋友:

#include <stdio.h>
#include <stdlib.h>
#include "mpi.h"

#define MPI_Error_Check(x) {const int err=x; if(x!=MPI_SUCCESS) { fprintf(stderr, "MPI ERROR %d at %d.", err, __LINE__);}}

int main(int argc, char **argv){
    MPI_Init(&argc, &argv);

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

    int N;
    if (rank==0){
        scanf("%d", &N);
    }

    MPI_Error_Check(MPI_Bcast(&N, 1, MPI_INT, 0, MPI_COMM_WORLD));

    double *a = (double *)malloc(N * sizeof(double));

    if(rank==0){
        for(int j=0;j<N;++j){
            a[j] = j+0.1;
        }
    }

    printf("Message from process %d : N=%d\n", rank, N);

    MPI_Error_Check(MPI_Bcast(a, N, MPI_DOUBLE, 0, MPI_COMM_WORLD));

    fprintf(stderr, "Message from process %d : %f\n", rank, a[rank]);

    free(a);

    MPI_Finalize();
    return 0;
} 

做得更好

最快的交流方式就是完全不交流。在您的情况下,一旦值 N 已知,每个等级都可以自行重新创建数据:

#include <stdio.h>
#include <stdlib.h>
#include "mpi.h"

#define MPI_Error_Check(x) {const int err=x; if(x!=MPI_SUCCESS) { fprintf(stderr, "MPI ERROR %d at %d.", err, __LINE__);}}

int main(int argc, char **argv){
    MPI_Init(&argc, &argv);

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

    int N;
    if (rank==0){
        scanf("%d", &N);
    }

    MPI_Error_Check(MPI_Bcast(&N, 1, MPI_INT, 0, MPI_COMM_WORLD));

    double *a = (double *)malloc(N * sizeof(double));

    for(int j=0;j<N;++j){
        a[j] = j+0.1;
    }

    printf("Message from process %d : N=%d\n", rank, N);

    fprintf(stderr, "Message from process %d : %f\n", rank, a[rank]);

    free(a);

    MPI_Finalize();
    return 0;
}