2016-12-07 108 views
1

我想使用这种分配内存的特定方式将2D阵列分散到其他2D数组(每个进程一个)。MPI_Scatter在其他2D阵列中的2D阵列

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

我不断收到此错误:

One of the processes started by mpirun has exited with a nonzero exit code. This typically indicates that the process finished in error. If your process did not finish in error, be sure to include a "return 0" or "exit(0)" in your C code before exiting the application.

PID 7035 failed on node n0 (127.0.0.1) due to signal 11.

我认为这个问题是有关分散,但我是新来的平行编程,所以如果有人知道是什么问题,请帮助我。 在此先感谢。

#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(); 
} 

回答

2

与您的代码的问题是,你声明两个变量名称为矩阵:

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”也。

+0

它需要一些更多的修改,但你指出我正确的方向非常感谢你。这个项目让我困扰了几天! :) – georgep

+0

np,很高兴帮助:) – dreamcrash