【问题标题】:MPI_Scatter a 2D array in other 2D arraysMPI_Scatter 一个二维数组在其他二维数组中
【发布时间】:2016-12-07 19:24:37
【问题描述】:

我想使用这种特定的内存分配方式将二维数组分散到其他二维数组中(每个进程一个)。

int (*matrix)[cols] = malloc(sizeof *matrix* rows);
 

我不断收到此错误:

由 mpirun 启动的进程之一以非零退出退出 代码。这通常表示过程错误地完成。 如果您的流程没有错误完成,请务必包含“return 0" 或 "exit(0)" 在您的 C 代码中退出应用程序。

由于信号 11,PID 7035 在节点 n0 (127.0.0.1) 上失败。

我认为问题是分散的,但我是并行编程的新手,所以如果有人知道问题是什么,请帮助我。 提前致谢。

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "mpi.h"

int main(int argc, char** argv) {

  int my_rank;
  int p;
  int root;
  int rows = 0;
  int cols = 0;
  int **matrix;

  int i, j;
  int local_rows;
  int answer = 0;
  int broke = 0;

  MPI_Init(& argc, & argv);
  MPI_Comm_rank(MPI_COMM_WORLD, & my_rank);
  MPI_Comm_size(MPI_COMM_WORLD, & p);

  if (my_rank == 0) {

    do {
        printf("Enter Dimensions NxN\n");
        scanf("%d", & rows);
        scanf("%d", & cols);
        if (cols != rows) {
            printf("Columns must be the same as rows,enter dimensions again.\n");
        }
    } while (rows != cols);        
    int (*matrix)[cols] = malloc(sizeof *matrix* rows);

    printf("Fill array %dx%d\n", rows, cols);
    for (i = 0; i < rows; i++) {
        for (j = 0; j < cols; j++) {
            scanf("%d",&matrix[i][j]);

        }
    }

    printf("\n");
    for (i = 0; i < rows; i++) {
        for (j = 0; j < cols; j++) {
            printf("%d ",matrix[i][j]);
        }
        printf("\n");
    }
  }

  root = 0;
  MPI_Bcast(&rows, 1, MPI_INT, root, MPI_COMM_WORLD);
  MPI_Bcast(&cols, 1, MPI_INT, root, MPI_COMM_WORLD);
  local_rows = rows / p;


  int (*local_matrix)[rows] = malloc(sizeof *local_matrix* local_rows);

  MPI_Scatter(matrix, local_rows*rows, MPI_INT,local_matrix, local_rows*rows, MPI_INT, 0,   MPI_COMM_WORLD);

  printf("\nLocal matrix fo the process %d is :\n", my_rank);

  for (i = 0; i < local_rows; i++) {
      for (j = 0; j < cols; j++) {
        printf("%d ", local_matrix[i][j]);
    }
    printf("\n");
  }
  if (my_rank==0){
     free(matrix);
     free(local_matrix);
  }   
  MPI_Finalize();
}

【问题讨论】:

    标签: c++ c multidimensional-array parallel-processing mpi


    【解决方案1】:

    您的代码的问题在于您使用名称矩阵声明了两个变量:

    int **matrix;

    int (*matrix)[cols] = malloc(sizeof *matrix* rows);

    由于后者是在 if (my_rank == 0) {..} 中声明的,因此变量开始在分散符MPI_Scatter(matrix, local_rows*rows, MPI_INT,local_matrix, local_rows*rows, MPI_INT, 0, MPI_COMM_WORLD);中使用

    是第一个,未分配的,而不是您为其分配空间的。这就是您收到错误消息的原因。

    试试这个:

    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    #include "mpi.h"
    
    int main(int argc, char** argv) {
    
    int my_rank;
    int p;
    int root;
    int rows = 0;
    int cols = 0;
    
    int i, j;
    int local_rows;
    int answer = 0;
    int broke = 0;
    MPI_Init(& argc, & argv);
    MPI_Comm_rank(MPI_COMM_WORLD, & my_rank);
    MPI_Comm_size(MPI_COMM_WORLD, & p);
    
    int (*matrix)[cols];
    
    if (my_rank == 0) {
    
        do {
            printf("Enter Dimensions NxN\n");
            scanf("%d", & rows);
            scanf("%d", & cols);
            if (cols != rows) {
                printf("Columns must be the same as rows,enter dimensions again.\n");
            }
    
        } while (rows != cols);        
    
        matrix = malloc(sizeof *matrix * rows);
    
        printf("Fill array %dx%d\n", rows, cols);
        for (i = 0; i < rows; i++) {
            for (j = 0; j < cols; j++) {
                scanf("%d",&matrix[i][j]);
    
            }
        }
    
        printf("\n");
        for (i = 0; i < rows; i++) {
            for (j = 0; j < cols; j++) {
                printf("%d ",matrix[i][j]);
            }
            printf("\n");
        }
    }
    
    root = 0;
    MPI_Bcast(&rows, 1, MPI_INT, root, MPI_COMM_WORLD);
    MPI_Bcast(&cols, 1, MPI_INT, root, MPI_COMM_WORLD);
    local_rows = rows / p;
    
    // Changed from the original
    int (*local_matrix)[cols] = malloc(sizeof *local_matrix* local_rows);
    
    printf("R = (%d, %d, %d) \n",my_rank, local_rows, cols);
    
    
    if(my_rank == 0)
    {
        printf("\n");
        for (i = 0; i < rows; i++) {
            for (j = 0; j < cols; j++) {
                printf("%d ",matrix[i][j]);
            }
            printf("\n");
        }
    }
    
    
    MPI_Scatter(matrix, local_rows*cols, MPI_INT,local_matrix, 
                local_rows*cols, MPI_INT, 0,   MPI_COMM_WORLD);
    

    ...

    顺便说一句,我认为你的意思是:

    int (*local_matrix)[cols] = malloc(sizeof *local_matrix* local_rows);

    而不是

    int (*local_matrix)[rows] = malloc(sizeof *local_matrix* local_rows);

    也不要忘记为从站释放“local_matrix”。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-11-30
      • 2011-07-27
      • 2021-04-28
      • 2021-06-04
      • 1970-01-01
      • 2016-02-20
      • 2022-12-07
      • 2021-12-31
      相关资源
      最近更新 更多