简体   繁体   English

MPI_Scatter分段错误(信号11)

[英]MPI_Scatter segmentation fault (signal 11)

I'm trying to Parallelize some piece of code that calculates matrices, but I'm getting segmentation fault error. 我正在尝试并行化一些用于计算矩阵的代码,但出现分段错误错误。 I already debug the code, but I cant find out the problem. 我已经调试了代码,但是找不到问题。

Here is the snippet where I declare the matrice: 这是我声明矩阵的代码段:

double **A, **B, **C, *tmp1,*tmp2,*tmp3;
int N;
int myrank, P, from, to;

N = 100;

tmp1 = (double *) malloc (sizeof(double ) * N * N);
tmp2 = (double *) malloc (sizeof(double ) * N * N);
tmp3 = (double *) malloc (sizeof(double ) * N * N);
A = (double **) malloc (sizeof(double *) * N);
B = (double **) malloc (sizeof(double *) * N);
C = (double **) malloc (sizeof(double *) * N);

from = myrank * N/P;
to = (myrank+1) * N/P;

Then here is where I inicialize the MPI 然后在这里我将MPI初始化

MPI_Init (&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &myrank);
MPI_Comm_size(MPI_COMM_WORLD, &P);

But somehow, I can't scatter... 但是不知何故,我无法分散...

MPI_Bcast(B, N*N, MPI_INT, 0, MPI_COMM_WORLD);

printf("Rank: %d\n", myrank);

MPI_Scatter(A, N*N/P, MPI_INT, &A[from], N*N/P, MPI_INT, 0, MPI_COMM_WORLD);

I can't wrote all the code here because I'm not allowed...But the problem is in Scatter it self. 我不能在这里编写所有代码,因为不允许这样做……但是问题出在它本身上。 What I'am doing wrong? 我做错了什么?

You cannot directly send a 2-D matrix ( double** ). 您不能直接发送二维矩阵( double** )。 Instead, you have to pass the address of the actual data: &A[0][0] . 相反,您必须传递实际数据的地址: &A[0][0] For receiving, you also have to pass an address to where the data is actually stored. 为了接收,您还必须将一个地址传递到实际存储数据的位置。 In your case this would be: &A[from][0] instead of &A[from] . 您的情况是: &A[from][0]而不是&A[from]

Try this as the scatter: 尝试将此作为散点图:

MPI_Scatter(&A[0][0], N*N/P, MPI_INT, &A[from][0], N*N/P, MPI_INT, 0, MPI_COMM_WORLD);

On another note, on the receiving process all values in A other than those received will remain invalid. 另外,在接收过程中, A除接收到的那些值以外的所有值都将保持无效。 You might want to consider creating a separate receive buffer of the size required: 您可能要考虑创建一个所需大小的单独接收缓冲区:

double* local_A = (double*)malloc(sizeof(double)*N*N/P);
MPI_Scatter(&A[0][0], N*N/P, MPI_INT, local_A, N*N/P, MPI_INT, 0, MPI_COMM_WORLD);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM