2016-12-07 8 views
1

この特定のメモリ割り当て方法を使用して、2D配列を他の2D配列に散らばりたい(各プロセスに1つずつ)。MPI_他の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.

を私はこの問題は、スキャッタ上だと思うが、私は誰にも問題が私を助けてくださいが何であるかを知っている場合ので、プログラミング平行に新しいです。 ありがとうございます。

int **matrix;

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

、後者が内部で宣言されてから:

#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

あなたのコードの問題は、あなたが名前行列を持つ2つの変数を宣言したということですif(my_rank == 0){..}変数beginはスキャッタで使用されます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);

0123を

また、スレーブの "local_matrix"も解放することを忘れないでください。

+0

これ以上の修正が必要でしたが、正しい方向に私を指摘しました。ありがとうございました。このプロジェクトは数日間私を悩ませていました! :) – georgep

+0

np、喜んで助けてください:) – dreamcrash