【问题标题】:Using MPI_Gatherv() and MPI_Datatype to 'gather' dynamically allocated 2D arrays in C++使用 MPI_Gatherv() 和 MPI_Datatype 在 C++ 中“收集”动态分配的二维数组
【发布时间】:2013-03-25 21:48:00
【问题描述】:

我认为描述问题的最简单方法是使用简单的代码。在每个处理器上,我都动态分配了“二维数组”(通过 new*[rows],new[cols] 形式实现,请参阅下面的代码进行说明)。对或错,我正在尝试使用已提交的 MPI_Datatype 来帮助我执行 MPI_Gatherv() 以将所有数组收集到根处理器上的单个 2D 数组中。

这是代码,在它下面我突出显示了它的要点(如果编译并运行它应该很容易理解 - 它会询问您想要的数组的尺寸):

#include <iostream>
#include <string>
#include <cmath>
#include <cstdlib>
#include <time.h>

#include "mpi.h" 


using namespace std;

// A function that prints out the 2D arrays to the terminal.
void print_2Darray(int **array_in,int dim_rows, int dim_cols) {
    cout << endl;
    for (int i=0;i<dim_rows;i++) {
        for (int j=0;j<dim_cols;j++) {
            cout << array_in[i][j] << " ";
            if (j==(dim_cols-1)) {
                cout << endl;
            }
        }
    }
    cout << endl;
}


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

    MPI::Init(argc, argv);


    // Typical MPI incantations...

    int size, rank;

    size = MPI::COMM_WORLD.Get_size(); 
    rank = MPI::COMM_WORLD.Get_rank();

    cout << "size = " << size << endl;
    cout << "rank = " << rank << endl;

    sleep(1);

    // Dynamically allocate a 2D square array of user-defined size 'dim'.

    int dim;
    if (rank == 0) {
        cout << "Please enter dimensions of 2D array ( dim x dim array ): ";
        cin >> dim;
        cout << "dim = " << dim << endl;
    }   

    MPI_Bcast(&dim,1,MPI_INT,0,MPI_COMM_WORLD);

    int **array2D;
    array2D = new int*[dim];
    for (int i=0; i<dim; i++) {
        array2D[i] = new int[dim](); // the extra '()' initializes to zero.
    }

    // Fill the arrays with i*j+rank where i and j are the indices.
    for (int i=0;i<dim;i++) {
        for (int j=0;j<dim;j++) {
            array2D[i][j] = i*j + rank;
        }
    }

    // Print out the arrays.
    print_2Darray(array2D,dim,dim);

    // Commit a MPI_Datatype for these arrays.
    MPI_Datatype MPI_ARRAYROW;
    MPI_Type_contiguous(dim, MPI_INT, &MPI_ARRAYROW);
    MPI_Type_commit(&MPI_ARRAYROW);

    // Declare 'all_array2D[][]' which will contain array2D[][] from all procs.
    int **all_array2D;
    all_array2D = new int*[size*dim];
    for (int i=0; i<size*dim; i++) {
        all_array2D[i] = new int[dim]();  // the extra '()' initializes to zero.
    }

    // Print out the arrays.
    print_2Darray(all_array2D,size*dim,dim);


    // Displacement vector for MPI_Gatherv() call.
    int *displace;
    displace = (int *)calloc(size,sizeof(int));
    int *dim_list;
    dim_list = (int *)calloc(size,sizeof(int));
    int j = 0;
    for (int i=0; i<size; i++) {
        displace[i] = j;
        cout << "displace[" << i << "] = " << displace[i] << endl;
        j += dim;
        dim_list[i] = dim;
    }

    // MPI_Gatherv call.
    MPI_Barrier(MPI_COMM_WORLD);
    MPI_Gatherv(array2D,dim,MPI_ARRAYROW,all_array2D,&dim_list[rank],&displace[rank],MPI_ARRAYROW,0,MPI_COMM_WORLD);

    // Print out the arrays.
    print_2Darray(all_array2D,size*dim,dim);

    MPI::Finalize();

    return 0;
}

代码可以编译,但遇到分段错误(我使用 'mpic++' 编译并使用 'mpirun -np 2' 来使用 2 个处理器):

[unknown-78-ca-39-b4-09-4f:02306] *** Process received signal ***
[unknown-78-ca-39-b4-09-4f:02306] Signal: Segmentation fault (11)
[unknown-78-ca-39-b4-09-4f:02306] Signal code: Address not mapped (1)
[unknown-78-ca-39-b4-09-4f:02306] Failing at address: 0x0
[unknown-78-ca-39-b4-09-4f:02306] [ 0] 2   libSystem.B.dylib                   0x00007fff844021ba _sigtramp + 26
[unknown-78-ca-39-b4-09-4f:02306] [ 1] 3   ???                                 0x0000000000000001 0x0 + 1
[unknown-78-ca-39-b4-09-4f:02306] [ 2] 4   gatherv2Darrays.x                   0x00000001000010c2 main + 1106
[unknown-78-ca-39-b4-09-4f:02306] [ 3] 5   gatherv2Darrays.x                   0x0000000100000a98 start + 52
[unknown-78-ca-39-b4-09-4f:02306] *** End of error message ***
mpirun noticed that job rank 0 with PID 2306 on node unknown-78-ca-39-b4-09-4f.home exited on signal 11 (Segmentation fault). 
1 additional process aborted (not shown)

在代码末尾附近执行“print_2Darray(all_array2D,size*dim,dim)”函数时会发生分段错误,其中“all_array2D”“应该”包含收集的数组。更具体地说,代码似乎为从主处理器收集的位打印“all_array2D”OK,但是当 print_2Darray() 函数开始处理来自其他处理器的位时,会给出 seg 错误。

代码要点:

  1. 我声明了一个 MPI_Datatype,它是一个连续的内存块,其大小足以存储单行二维数组。然后我使用 MPI_Gatherv() 尝试收集这些行。
  2. 代码的 sleep(1) 调用只是为了帮助用户更清楚地看到“dims”的提示,否则它会被隐藏在“size”和“rank”之间。
  3. 2D 数组的元素被初始化为值“i*j + rank”,其中 i 和 j 分别是行和列索引。我的理由是,生成的数字很容易泄露生成该数组的处理器的等级。

我想归结为我不知道 MPI_Gatherv() 动态分配数组的正确性...我应该使用 MPI_Datatypes 吗?对我来说,动态分配数组非常重要。

如果有任何帮助/建议,我将不胜感激!我的想法已经枯竭了!

【问题讨论】:

  • 我认为问题出在你的位移矢量上。如果我没记错的话(但我在这里很可能是错的,位移向量应该给出位移以位,而不是元素的数量。另外,我真的不明白你为什么要使用它们你打电话给MPI_Gatherv(..., &amp;dim_list[rank], &amp;displace[rank], ...)的方式。你确定这是给出尺寸和位移的正确方式吗?
  • 嗨@TomasLycken,我很确定我做displace[]向量的方式是有效的,因为我实际上是在破解更大的代码,他们就是这样做的......定义尺寸的方式我也遵循了更大的代码,它确实有效......您可能会问 - '那你为什么不直接复制更大的代码中所做的事情'?好吧,它们不会动态分配数组,而是使用动态分配的 typedef 指针数组。我会在早上多考虑你的观点!谢谢!
  • @TomasLycken,MPI_GATHERV/MPI_SCATTERV 的位移矢量以指定 MPI 数据类型的范围为单位。

标签: c++ parallel-processing mpi


【解决方案1】:

MPI_GathervMPI_Scatterv 以及实际上所有其他采用数组参数的 MPI 通信调用,都期望数组元素在内存中连续布局。这意味着在调用MPI_Gatherv(array2D, dim, MPI_ARRAYROW, ...) 中,MPI 期望MPI_ARRAYROW 类型的第一个元素从array2D 指向的内存位置开始,第二个元素从(BYTE*)array2D + extent_of(MPI_ARRAYROW) 开始,第三个元素从(BYTE*)array2D + 2*extent_of(MPI_ARRAYROW) 开始, 等等。这里extent_of()MPI_ARRAYROW类型的范围,可以通过调用MPI_Type_get_extent获取。

很明显,您的二维数组的行在内存中不是连续的,因为它们中的每一个都是由 new 运算符的单独调用分配的。 array2D 也不是指向数据的指针,而是指向每一行的指针向量的指针。这在 MPI 中不起作用,并且 StackOverflow 上还有无数其他问题,在这里讨论了这个事实 - 只需搜索 MPI 2D 并亲自查看。

解决方案是使用一大块单独分配的内存块和伴随的涂料向量 - 请参阅答案中提到的 this questionarralloc() 函数。

【讨论】:

  • +1;这个答案比我更详细地描述了数据布局是上述代码问题的原因。我想知道在过去几十年中,由于 C/C++ 缺乏对简单多维数组的支持,浪费了多少开发人员的时间。
  • 感谢您的回答,我想我现在可以使用代码了(我将很快在答案中发布解决方案)。您给出的重要提示是“array2D 也不是指向数据的指针,而是指向每行指针向量的指针。”!所以我用“calloc”数组替换了我的“新”数组,然后通过将数组解引用一级将指向数据的第一个指针输入 MPI_Gatherv。
【解决方案2】:

这个涉及数组分配的问题在处理 C/C++ 和 MPI 时一直出现。这个:

int **array2D;
array2D = new int*[dim];
for (int i=0; i<dim; i++) {
    array2D[i] = new int[dim](); // the extra '()' initializes to zero.
}

分配dim 1d 数组,每个dim 长度为整数。但是,根本没有理由将它们彼此相邻布置 - 暗淡的数组可能分散在内存中。所以即使从array2D[0] 发送dim*dim 整数也行不通。 all_array2D 是一样的;您正在创建 size*dim 数组,每个数组的大小为 dim,但它们彼此之间的关系谁知道,这使得您的位移可能是错误的。

要使数组在内存中连续,您需要执行类似的操作

int **array2D;
array2D = new int*[dim];
array2D[0] = new int[dim*dim];
for (int i=1; i<dim; i++) {
    array2D[i] = &(array2D[dim*i]);
}

all_array2D 也是如此。只有这样你才能开始推理内存布局。

【讨论】:

  • 谢谢,我最后使用了 calloc'ed 数组!非常感谢。
【解决方案3】:

我只是想总结一下@Hristolliev 和@JonathanDursi 帮助我找到的解决方案。

  1. MPI_Gatherv() 这样的MPI 命令与连续分配的内存块一起工作,因此使用'new' 来构造2D 数组,然后输入MPI 命令将不起作用,因为'new' 不能保证连续块。请改用“calloc”来创建这些数组(请参阅下面的代码作为示例)。
  2. @Hristolliev 的重要一点:MPI_Gatherv() 的第一个和第四个参数必须是指向MPI_ARRAYROW 类型的第一个元素的指针。将二维数组取消引用一级,例如array2D[0] 将实现这一点(再次,请参阅下面的修改后的工作代码)。

最终的工作代码如下:

#include <iostream>
#include <string>
#include <cmath>
#include <cstdlib>
#include <time.h>

#include "mpi.h" 


using namespace std;


void print_2Darray(int **array_in,int dim_rows, int dim_cols) {
    cout << endl;
    for (int i=0;i<dim_rows;i++) {
        for (int j=0;j<dim_cols;j++) {
            cout << array_in[i][j] << " ";
            if (j==(dim_cols-1)) {
                cout << endl;
            }
        }
    }
    cout << endl;
}


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

    MPI::Init(argc, argv);


    // Typical MPI incantations...

    int size, rank;

    size = MPI::COMM_WORLD.Get_size(); 
    rank = MPI::COMM_WORLD.Get_rank();

    cout << "size = " << size << endl;
    cout << "rank = " << rank << endl;

    sleep(1);

    // Dynamically allocate a 2D square array of user-defined size 'dim'.

    int dim;
    if (rank == 0) {
        cout << "Please enter dimensions of 2D array ( dim x dim array ): ";
        cin >> dim;
        cout << "dim = " << dim << endl;
    }   

    MPI_Bcast(&dim,1,MPI_INT,0,MPI_COMM_WORLD);


    // Use another way of declaring the 2D array which ensures it is contiguous in memory.
    int **array2D;
    array2D = (int **) calloc(dim,sizeof(int *));
    array2D[0] = (int *) calloc(dim*dim,sizeof(int));
    for (int i=1;i<dim;i++) {
        array2D[i] = array2D[0] + i*dim;
    }

    // Fill the arrays with i*j+rank where i and j are the indices.
    for (int i=0;i<dim;i++) {
        for (int j=0;j<dim;j++) {
            array2D[i][j] = i*j + rank;
        }
    }

    // Print out the arrays.
    print_2Darray(array2D,dim,dim);

    // Commit a MPI_Datatype for these arrays.
    MPI_Datatype MPI_ARRAYROW;
    MPI_Type_contiguous(dim, MPI_INT, &MPI_ARRAYROW);
    MPI_Type_commit(&MPI_ARRAYROW);


    // Use another way of declaring the 2D array which ensures it is contiguous in memory.
    int **all_array2D;
    all_array2D = (int **) calloc(size*dim,sizeof(int *));
    all_array2D[0] = (int *) calloc(dim*dim,sizeof(int));
    for (int i=1;i<size*dim;i++) {
        all_array2D[i] = all_array2D[0] + i*dim;
    }

    // Print out the arrays.
    print_2Darray(all_array2D,size*dim,dim);


    // Displacement vector for MPI_Gatherv() call.
    int *displace;
    displace = (int *)calloc(size,sizeof(int));
    int *dim_list;
    dim_list = (int *)calloc(size,sizeof(int));
    int j = 0;
    for (int i=0; i<size; i++) {
        displace[i] = j;
        cout << "displace[" << i << "] = " << displace[i] << endl;
        j += dim;
        dim_list[i] = dim;
        cout << "dim_list[" << i << "] = " << dim_list[i] << endl;
    }

    // MPI_Gatherv call.
    MPI_Barrier(MPI_COMM_WORLD);
    cout << "array2D[0] = " << array2D[0] << endl;
    MPI_Gatherv(array2D[0],dim,MPI_ARRAYROW,all_array2D[0],&dim_list[rank],&displace[rank],MPI_ARRAYROW,0,MPI_COMM_WORLD);


    // Print out the arrays.
    print_2Darray(all_array2D,size*dim,dim);


    MPI::Finalize();

    return 0;
}

mpic++编译。

【讨论】:

    猜你喜欢
    • 2013-11-14
    • 2021-02-03
    • 2015-09-17
    • 1970-01-01
    • 1970-01-01
    • 2015-02-10
    • 2012-02-06
    • 1970-01-01
    • 2021-01-26
    相关资源
    最近更新 更多