首页 > 解决方案 > 在 MPI 中共享数组的一部分

问题描述

我正在考虑一个需要共享数组的问题,如下所示:

假设int array[10]并且有3 processes这样的;

process 0 gets array[0:3] 3 is included.
process 1 gets array[3:6] 6 is included.
process 2 gets array[6:9] 9 is included.

但是我不确定如何在这样的进程之间拆分这个数组。更具体地说,这个数组是 for Sparse Matrix in CSR format,数组代表行。

我如何在 MPI C/C++ 中解决这个问题。

标签: c++mpisparse-matrixscatter

解决方案


一个不符合标准的实现,使用MPI_Scatterv

int arr[10];
const int counts[] = {4, 4, 4};
const int displs[] = {0, 3, 6};

int recv_buff[4];
MPI_Scatterv(arr, counts, displs, MPI_INT, recv_buff, 
    4, MPI_INT, 0, MPI_COMM_WORLD);

displs只是简单的偏移量,指定如下:

Original array arr[10]:
[ 0 1 2 3 4 5 6 7 8 9 ]
  ^ displs[0]
        ^ displs[1]
              ^ displs[2]

这不能保证有效,因为子数组重叠

计数、类型和位移的规范不应导致root多次读取 上的任何位置。

正如Gilles Gouaillardet在评论中指出的那样,为避免重叠,您可以调用MPI_Scatterv两次:

int arr[10];
const int counts1[] = {4, 0, 4};
const int counts2[] = {0, 4, 0};
const int displs[]  = {0, 3, 6};

int recv_buff[4];
MPI_Scatterv(arr, counts1, displs, MPI_INT, recv_buff, 
    counts1[rank], MPI_INT, 0, MPI_COMM_WORLD);
MPI_Scatterv(arr, counts2, displs, MPI_INT, recv_buff, 
    counts2[rank], MPI_INT, 0, MPI_COMM_WORLD);

或者,您可以使用普通MPI_Send的 s/ MPI_Gets。


推荐阅读