1

我认为描述问题的最简单方法是使用简单的代码。在每个处理器上,我都动态分配了“二维数组”(通过 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. 二维数组的元素被初始化为值“i*j + rank”,其中 i 和 j 分别是行和列索引。我的理由是生成的数字很容易放弃生成该数组的处理器的等级。

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

我将非常感谢任何帮助/建议!我的想法几乎耗尽了!

4

3 回答 3

4

MPI_Gatherv, MPI_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自己搜索并查看。

解决方案是使用一大块单独分配的内存块和伴随的涂料向量 - 请参阅这个问题arralloc()答案中提到的功能。

于 2013-04-04T12:01:33.340 回答
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维数组,每个数组dim的长度为整数。但是,根本没有理由将它们彼此相邻放置 - 暗淡的数组可能分散在内存中。因此,即使从那里发送dim*dim整数array2D[0]也行不通。all_array2D是一样的;您正在创建size*dim数组,每个数组 size 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. 只有这样你才能开始推理内存布局。

于 2013-04-04T12:00:21.233 回答
1

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

  1. MPI 命令就像MPI_Gatherv()处理连续分配的内存块一样,因此使用 ' new' 来构造 2D 数组,然后输入 MPI 命令将不起作用,因为 ' new' 不能保证连续块。改用 ' calloc' 来制作这些数组(请参阅下面的代码作为示例)。
  2. @Hristolliev 的重要一点: MPI_Gatherv() 的第一个和第四个参数必须是指向 type 的第一个元素的指针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-04-04T15:00:52.143 回答