【问题标题】:Sending a column of a dynamically allocated 2d array in MPI在 MPI 中发送一列动态分配的二维数组
【发布时间】:2017-06-26 09:31:32
【问题描述】:

我正在做一个项目,我将使用 MPI 进行并行编程,我将使用点对点(发送/接收)通信和集体通信(MPI_Gatherv,...), 我将本地数组分配为连续的二维数组,我需要将数组的边缘列发送到另一个数组,我尝试了如下代码所示。 现在,下面的代码产生了几乎正确的结果,除了接收到的数组中的一个元素发生了奇怪的变化,如下所示

2 3 3 3 3
2 3 3 3 3
2 3 3 3 3
2 3 3 3 3
2 0 3 3 3

元素b[4][1] = 0!!是问题所在,虽然它不在数据类型g_col 的范围内,但我不明白为什么要修改此元素,我使用MPI_Get_count 检查了接收消息,它显示接收到5 个元素(这是正确的) , 那么这个元素是如何变化的呢? 我正在使用下面显示的方法分配数组以在MPI_Gatherv 中使用,因为我创建了一个子数组并使用调整大小的数据类型来收集全局数组中的本地数组

我正在使用 4 个内核运行代码。

提前致谢。

    #include <iostream>
    #include <mpi.h>
    #include <cmath>
    #include <cstdlib>

    using namespace std;

    // Allocate a contiguous  2d array 
    int** crt_arr_2d(int im, int jm){
    int** arr;
    arr = (int**)calloc(im, sizeof(int*));
    arr[0] = (int*)calloc(im*jm, sizeof(int));
    for (int i = 0; i < im; i++){
        arr[i] = arr[0] + i*jm;
    }
    return arr;
}

    // De-allocate 2d array
    void dstr_arr_2d(int** ar, int im){
    free(ar[0]);
    free(ar);
}

int main(int argc, char* argv[]){

    MPI_Init(&argc, &argv);
    MPI_Comm World;
    World = MPI_COMM_WORLD;
    int proc, nprocs;
    MPI_Comm_rank(World, &proc);
    MPI_Comm_size(World, &nprocs);

    int im_local = 5; // Number of rows in local array
    int jm_local = 5; // Number of columns in local array

    int** a = crt_arr_2d(im_local, jm_local);
    int** b = crt_arr_2d(im_local, jm_local);

    // Initialize data
    if (proc == 0){
        for (int i = 0; i < im_local; i++){
            for (int j = 0; j < jm_local; j++){
                a[i][j] = 2;
            }
        }
    }
    if (proc == 1){
        for (int i = 0; i < im_local; i++){
            for (int j = 0; j < jm_local; j++){
                b[i][j] = 3;
            }
        }
    }

    MPI_Datatype g_col;
    MPI_Type_vector(im_local, 1, jm_local, MPI_INT, &g_col);
    MPI_Type_commit(&g_col);

    if (proc == 0){
        MPI_Send(&a[0][4], im_local, g_col, 1, 1, World);
    }

    if (proc == 1){
        MPI_Recv(&b[0][0], im_local, g_col, 0, 1, World, MPI_STATUSES_IGNORE);
        for (int i = 0; i < im_local; i++){
            for (int j = 0; j < jm_local; j++){
                cout << b[i][j] << " ";
            }
            cout << endl;
        }
    }

    MPI_Barrier(World);

    //MPI_Type_free(&g_col);
    //dstr_arr_2d(a,im_local);
    //dstr_arr_2d(b,im_local);

    MPI_Finalize();
    return 0;
}

【问题讨论】:

    标签: c++ mpi dynamic-arrays


    【解决方案1】:

    如果我正确理解您想要实现的目标,那么您应该使用count=1 而不是count=im_local 调用MPI_Send()MPI_Recv()

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-08-17
      • 2019-08-12
      • 2012-11-03
      • 2021-07-23
      • 2013-12-12
      • 1970-01-01
      • 2012-03-05
      相关资源
      最近更新 更多