gpt4 book ai didi

c++ - MPI_Scatter 一个二维数组在其他二维数组中

转载 作者:太空宇宙 更新时间:2023-11-04 02:32:51 26 4
gpt4 key购买 nike

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

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

我一直收到这个错误:

One of the processes started by mpirun has exited with a nonzero exitcode. This typically indicates that the process finished in error.If your process did not finish in error, be sure to include a "return0" 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();
}

最佳答案

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

int **矩阵;

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

关于c++ - MPI_Scatter 一个二维数组在其他二维数组中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41025693/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com