MPI_Scatter其他2D数组中的2D数组

时间:2016-12-07 19:24:37

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

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

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

我一直收到这个错误:

  

mpirun启动的其中一个进程已退出非零退出   码。这通常表示该过程完成错误。   如果您的流程没有完成错误,请确保包含“退货”   退出应用程序之前,C代码中的“0”或“退出(0)”。

     

由于信号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();
 }

1 个答案:

答案 0 :(得分: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”。

相关问题