c++ - 在 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++ 中解决这个问题。
解决方案
一个不符合标准的实现,使用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。
推荐阅读
- r - 如何修复ggplot2中的X轴编号顺序?
- c# - 使用 c# 从三个组合框中获取日期为 dd/mm/yyyy
- r - 在 R 中找不到函数“knnImputation”
- sql - 在 Laravel Eloquent 中编写此查询的最佳方式是什么?
- android - 如何更改 VPN 弹出对话框权限?
- java - 如何解决“线程中的异常”AWT-EventQueue-0“java.lang.NullPointerException”?
- reactjs - 在同一个 laravel 应用程序项目中托管 react 应用程序和 laravel 后端(管理面板)
- composer-php - 从 vcs 存储库中找不到包
- python - 如何从字典中删除键和值浮动
- error-handling - 如何获取 DXGI_ERROR 描述?