试图将动态数组传递到进程时的分割故障

发布于 2025-01-27 07:28:20 字数 1084 浏览 1 评论 0原文

我试图将动态数组从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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

∝单色的世界 2025-02-03 07:28:20

并行程序中的交互式输入总是很危险的。您的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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文