C并行编程,信号:分段故障:(11)11信号代码:地址未映射(1)
我现在正在使用MPI做一个简单的并行编程。我在编译时没有遇到错误,但在运行时遇到了一些我无法理解的错误。请帮忙!谢谢你们! 源代码如下:C并行编程,信号:分段故障:(11)11信号代码:地址未映射(1),c,parallel-processing,mpi,vi,C,Parallel Processing,Mpi,Vi,我现在正在使用MPI做一个简单的并行编程。我在编译时没有遇到错误,但在运行时遇到了一些我无法理解的错误。请帮忙!谢谢你们! 源代码如下: #include <stdio.h> #include <stdlib.h> #include "mpi.h" #include "matrix.h" #define MIN(X,Y) (((X) < (Y)) ? (X) : (Y)) //IMPORTANT!! int master = 0; int numsent, i;
#include <stdio.h>
#include <stdlib.h>
#include "mpi.h"
#include "matrix.h"
#define MIN(X,Y) (((X) < (Y)) ? (X) : (Y)) //IMPORTANT!!
int master = 0;
int numsent, i;
int nrows, ncols;
double *A, *x, *b, *buffer;
int rowidx;
int sender;
double ans;
int main(int argc, char *argv[])
{
int myid;
int nproc;
MPI_Init(&argc, &argv);
MPI_Comm_size(MPI_COMM_WORLD, &nproc);
MPI_Comm_rank(MPI_COMM_WORLD, &myid);
/* CODING */
MPI_Status stat; // IMPORTANT!!
//master_stage1: master obtain the matrix A and vector X
if(myid == master)
{
printf("What is the number of rows of matrix A:\n");
scanf("%d", &nrows);
printf("what is the number of columns of matrix A:\n");
scanf("%d", &ncols);
//printf("nrows = %d, ncols = %d\n", nrows, ncols);//text
A = (double*)malloc(nrows*ncols*sizeof(double));
b = (double*)malloc(nrows*sizeof(double));
ObtainMatrixAndVector(nrows, ncols, A, x, b);
}
//master_stage2:bcast x, ncols, nrows, and p2p sent rows of A
MPI_Bcast(&ncols, 1, MPI_INT, master, MPI_COMM_WORLD);
MPI_Bcast(&nrows, 1, MPI_INT, master, MPI_COMM_WORLD);
x = (double*)malloc(ncols*sizeof(double));
MPI_Bcast(x, ncols, MPI_DOUBLE, master, MPI_COMM_WORLD);
if(myid == master)
{
numsent = 0;
for(i = 1; i <= MIN(nrows, nproc - 1); i++)
{
MPI_Send(&A[(i - 1)*ncols], ncols, MPI_DOUBLE, i, i, MPI_COMM_WORLD);
numsent++;
}
//master_stage3: receiving
for(i = 0; i <= nrows; i++)
{
MPI_Recv(&ans, 1, MPI_DOUBLE, MPI_ANY_SOURCE, MPI_ANY_TAG, MPI_COMM_WORLD, &stat);
sender = stat.MPI_SOURCE;
rowidx = stat.MPI_TAG;
b[rowidx-1] = ans;
if(numsent < nrows)
{
MPI_Send(&A[numsent*ncols], ncols, MPI_DOUBLE, sender, numsent+1, MPI_COMM_WORLD);
numsent++;
}
else
MPI_Send(buffer, ncols, MPI_DOUBLE, sender, 0, MPI_COMM_WORLD);
}
}
//Jobs Done by workers
buffer = (double*)malloc(ncols*sizeof(double));
while(1)
{
if(myid > nrows)
break;
else
{
MPI_Recv(buffer, ncols, MPI_DOUBLE, MPI_ANY_SOURCE, MPI_ANY_TAG, MPI_COMM_WORLD, &stat);
rowidx = stat.MPI_TAG;
if(rowidx == 0)
break;
ans = 0.0;
for(i = 0; i < ncols; i++)
ans += buffer[i] * x[i];
MPI_Send(&ans, 1, MPI_DOUBLE, master, rowidx, MPI_COMM_WORLD);
}
}
if(myid == master)
{
for(i = 0; i < nrows; i++)
printf("%f\n", b[i]);
}
/* CODING */
MPI_Finalize();
}
错误:
[Nicks-MAC:02138] *** Process received signal ***
[Nicks-MAC:02138] Signal: Segmentation fault: 11 (11)
[Nicks-MAC:02138] Signal code: Address not mapped (1)
[Nicks-MAC:02138] Failing at address: 0x0
[Nicks-MAC:02138] [ 0] 0 libsystem_platform.dylib 0x00007fffbf27bbba _sigtramp + 26
[Nicks-MAC:02138] [ 1] 0 a.out 0x0000000106daf0eb x + 4147
[Nicks-MAC:02138] [ 2] 0 a.out 0x0000000106dad7a1 main + 321
[Nicks-MAC:02138] [ 3] 0 libdyld.dylib 0x00007fffbf06e255 start + 1
[Nicks-MAC:02138] *** End of error message ***
--------------------------------------------------------------------------
mpirun noticed that process rank 0 with PID 0 on node Nicks-MAC exited on signal 11 (Segmentation fault: 11).
--------------------------------------------------------------------------
非常感谢大家 我想我看到了错误:
ObtainMatrixAndVector(nrows, ncols, A, x, b);
}
//master_stage2:bcast x, ncols, nrows, and p2p sent rows of A
MPI_Bcast(&ncols, 1, MPI_INT, master, MPI_COMM_WORLD);
MPI_Bcast(&nrows, 1, MPI_INT, master, MPI_COMM_WORLD);
x = (double*)malloc(ncols*sizeof(double));
在给数组分配内存之前,使用指向数组x的指针
试试这个:
A = (double*)malloc(nrows*ncols*sizeof(double));
b = (double*)malloc(nrows*sizeof(double));
x = (double*)malloc(ncols*sizeof(double));
ObtainMatrixAndVector(nrows, ncols, A, x, b);
您的代码中有许多错误
- 在调用ActainMatrix和Vector之前,无法在主机上分配
。在主机上分配之前一定要先分配。但是,您还必须使x
的其他分配仅适用于非主机x
- 同样,您也无法在主主节之前分配
缓冲区。将该分配移到该部分之前
- 您无条件地执行工作者代码。主机不应执行工作代码
- 你在这里一点一点地离开
(i=0;如果问题是关于C,为什么是C++标签?不要垃圾邮件标签!抱歉,我只需点击建议标签。现在我将删除C++标签。谢谢你的帮助亚历克斯。我尝试了你所说的但是它得到更多的错误。我是MPI的初学者。我现在迷路了。非常感谢。我会尝试你所提到的并让你知道结果。我建议。你编辑了问题,添加了你修复了我提到的问题的版本。但是保留原始版本。
ObtainMatrixAndVector(nrows, ncols, A, x, b); } //master_stage2:bcast x, ncols, nrows, and p2p sent rows of A MPI_Bcast(&ncols, 1, MPI_INT, master, MPI_COMM_WORLD); MPI_Bcast(&nrows, 1, MPI_INT, master, MPI_COMM_WORLD); x = (double*)malloc(ncols*sizeof(double));
A = (double*)malloc(nrows*ncols*sizeof(double)); b = (double*)malloc(nrows*sizeof(double)); x = (double*)malloc(ncols*sizeof(double)); ObtainMatrixAndVector(nrows, ncols, A, x, b);