2016-12-07 107 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