>我正在考虑一个需要共享数组的问题,如下所示:
假设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]
这不能保证有效,因为子数组重叠:
计数、类型和位移的规范不应导致多次读取
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_Get
s。