在 MPI 中共享数组的一部分

Sharing a part of an array in 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.

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

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

使用 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]

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

The specification of counts, types, and displacements should not cause any location on the root to be read more than once.

正如评论中的 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_Sends/MPI_Gets。