尝试发送 2D 向量的列时MPI_Scatterv分段错误

MPI_Scatterv segmentation fault when trying to send columns of a 2d vector

提问人:Amato.g 提问时间:11/12/2023 最后编辑:Amato.g 更新时间:11/13/2023 访问量:69

问:

我有一个大小为 NxM 的 2d 向量,其中 N=13 和 M=17 和 8 个过程。我想将这个 2d 向量的列分发到进程中,因此 p0 将有 3 列,而所有其他进程将有 2 列(所有列将有 13 个整数)。 我无法在我的代码中看到任何错误,但它在执行MPI_Scatterv函数时给了我分段错误。

这是我的代码:

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

#define N 13
#define M 17

int main(int argc, char *argv[]){
    int matrix[N*M];
    int menum, nproc, i, j;
    
    MPI_Init(&argc, &argv);
    
    MPI_Comm_rank(MPI_COMM_WORLD, &menum);
    MPI_Comm_size(MPI_COMM_WORLD, &nproc);
    
    //Matrix initialization
    if (menum == 0) {
        for (i = 0; i < N; i++) {
            for (j = 0; j < M; j++) {
                matrix[(i*M)+j] = i * M + j;
            }
        }
    }
    
    //Every process has its vector of size nloc*mloc
    int nloc = N;
    int mloc = menum < M%nproc ? (M/nproc)+1 : M/nproc;
    int *recv_vec = (int *)malloc(nloc*mloc*sizeof(int));
    
    //Create a custom datatype for a vector of nloc integers
    MPI_Datatype col_type;
    MPI_Type_vector(nloc, 1, M, MPI_INT, &col_type);
    MPI_Type_commit(&col_type);
    
    //Distributing data to other processes
    int sendcounts[]={3,2,2,2,2,2,2,2};
    int displs[]={0,3,5,7,9,11,13,15};
    MPI_Scatterv(matrix, sendcounts, displs, col_type, recv_vec, nloc*mloc, MPI_INT, 0, MPI_COMM_WORLD);
    
    //Freeing up memory and terminate MPI environment
    free(recv_vec);
    MPI_Type_free(&col_type);
    MPI_Finalize();

    return 0;
}

编辑:

如评论中所述,我不得不使用 ,因此可以计算以下调用的正确偏移量。MPI_Type_create_resized()MPI_Scatterv

C 并行处理 MPI

评论

0赞 Gilles Gouaillardet 11/12/2023
您需要键入,以便第二列从正确的偏移量开始。MPI_Type_create_resized()
0赞 Amato.g 11/12/2023
对不起,MPI_Scatterv不知道从哪里开始基于位移数组的第二列吗?
0赞 Gilles Gouaillardet 11/12/2023
位移的单位是派生数据类型的单位。如果没有正确的范围,则会得到错误的字节偏移量,因此需要调整派生数据类型的大小。extent
0赞 Amato.g 11/13/2023
好的,你是对的,它有效,谢谢!

答: 暂无答案