试图将动态数组传递到进程时的分割故障
我试图将动态数组从0过程传递到1,反之亦然。在过程1中获得细分故障。所有矩阵都按预期打印。在这种情况下,有什么问题?
int main(int argc, char **argv){
MPI_Init(&argc,&argv);
int n;
cin >> n;
int *matrix = new int[n*n];
int *matrix2 = new int[n*n];
int i,j,ProcNum,ProcRank;
MPI_Comm_size(MPI_COMM_WORLD, &ProcNum);
MPI_Comm_rank(MPI_COMM_WORLD, &ProcRank);
MPI_Status status;
if (ProcRank == 0){
/* Filling matrices here with i*n + j values*/
MPI_Send(&n, 1, MPI_INT, 1, 1, MPI_COMM_WORLD);
MPI_Send(&(matrix[0]), n*n, MPI_INT, 1, 2, MPI_COMM_WORLD);
MPI_Recv(&(matrix[0]), n*n, MPI_INT, 1, 2, MPI_COMM_WORLD, &status);
print_matrix(matrix,n);
}
if (ProcRank > 0) {
MPI_Recv(&n, 1, MPI_INT, 0, 1, MPI_COMM_WORLD, &status);
MPI_Recv(&(matrix[0]), n*n, MPI_INT, 0, 2, MPI_COMM_WORLD, &status);
/*passing i-j values to matrix and printing them here*/
MPI_Send(&(matrix[0]), n*n, MPI_INT, 0, 2, MPI_COMM_WORLD);
}
MPI_Finalize();
delete matrix;
delete matrix2;
return EXIT_SUCCESS;
}
I tried to pass dynamic array from 0 process to 1 and vice versa. Getting segmentation fault in process 1. All matrices printed as expected. What could be the problem in this situation?
int main(int argc, char **argv){
MPI_Init(&argc,&argv);
int n;
cin >> n;
int *matrix = new int[n*n];
int *matrix2 = new int[n*n];
int i,j,ProcNum,ProcRank;
MPI_Comm_size(MPI_COMM_WORLD, &ProcNum);
MPI_Comm_rank(MPI_COMM_WORLD, &ProcRank);
MPI_Status status;
if (ProcRank == 0){
/* Filling matrices here with i*n + j values*/
MPI_Send(&n, 1, MPI_INT, 1, 1, MPI_COMM_WORLD);
MPI_Send(&(matrix[0]), n*n, MPI_INT, 1, 2, MPI_COMM_WORLD);
MPI_Recv(&(matrix[0]), n*n, MPI_INT, 1, 2, MPI_COMM_WORLD, &status);
print_matrix(matrix,n);
}
if (ProcRank > 0) {
MPI_Recv(&n, 1, MPI_INT, 0, 1, MPI_COMM_WORLD, &status);
MPI_Recv(&(matrix[0]), n*n, MPI_INT, 0, 2, MPI_COMM_WORLD, &status);
/*passing i-j values to matrix and printing them here*/
MPI_Send(&(matrix[0]), n*n, MPI_INT, 0, 2, MPI_COMM_WORLD);
}
MPI_Finalize();
delete matrix;
delete matrix2;
return EXIT_SUCCESS;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
并行程序中的交互式输入总是很危险的。您的MPI过程通常是通过SSH连接开始的,因此它们可能不会获得终端输入。流程零很可能会,因此我主张仅在Zero上阅读
n
,然后将其广播。Interactive input in parallel programs is always dangerous. Your MPI processes are often started through an ssh connection, and so they will probably not get the terminal input. Process zero most likely will, so I'd advocate reading
n
only on process zero and then broadcasting it.