C MPI分段故障(信号11)

C MPI分段故障(信号11),c,segmentation-fault,mpi,C,Segmentation Fault,Mpi,我已经试了两天多,想看看我犯了什么错误,但什么也找不到。我不断得到以下错误: =某个应用程序进程的错误终止 =退出代码:139 =清理剩余流程 =您可以忽略以下清理消息 因此,在MPI\u BCAST和另一个函数中,我清楚地看到了问题所在。 你能帮我弄清楚怎么了吗? 编译代码时,我键入以下内容: /usr/bin/mpicc -I/usr/include -L/usr/lib z.main.c z.mainMR.c z.mainWR.c -o 1dcode -g -lm 对于

我已经试了两天多,想看看我犯了什么错误,但什么也找不到。我不断得到以下错误:

=某个应用程序进程的错误终止

=退出代码:139

=清理剩余流程

=您可以忽略以下清理消息

因此,在
MPI\u BCAST
和另一个函数中,我清楚地看到了问题所在。 你能帮我弄清楚怎么了吗? 编译代码时,我键入以下内容:

/usr/bin/mpicc  -I/usr/include   -L/usr/lib  z.main.c  z.mainMR.c  z.mainWR.c  -o  1dcode -g  -lm
对于运行:

usr/bin/mpirun -np 2 ./1dcode dat.txt o.out.txt
例如,我的代码包含以下函数:

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <math.h>
#include <string.h>
#include "functions.h"
#include <mpi.h>
/*...................z.mainMR master function............. */
void MASTER(int argc, char *argv[], int nPROC, int nWRs, int mster)
{

/*... Define all the variables we going to use in z.mainMR function..*/
double tend, dtfactor, dtout, D, b, dx, dtexpl, dt, time;
int MM, M, maxsteps, nsteps;
FILE *datp, *outp;
/*.....Reading the data file "dat" then saving the data in o.out.....*/
datp = fopen(argv[1],"r"); // Open the file in read mode
outp = fopen(argv[argc-1],"w"); // Open output file in write mode
if(datp != NULL) // If data file is not empty continue
{
fscanf(datp,"%d %lf %lf %lf %lf %lf",&MM,&tend,&dtfactor,&dtout,&D,&b);    // read the data
fprintf(outp,"data>>>\nMM=%d\ntend=%lf\ndtfactor=%lf\ndtout=%lf\nD=%lf\nb=%lf\n",MM,tend,dtfactor,dtout,D,b);
fclose(datp); // Close the data file
fclose(outp); // Close the output file
}
else // If the file is empty then print an error message
{
    printf("There is something wrong. Maybe file is empty.\n");
}

/*.... Find dx, M, dtexpl, dt and the maxsteps........*/
dx = 1.0/ (double) MM;
M = b * MM;
dtexpl = (dx * dx) / (2.0 * D);
dt = dtfactor * dtexpl;
maxsteps = (int)( tend / dt ) + 1;

/*...Pack integers in iparms array, reals in parms array...*/
int iparms[2] = {MM,M};
double parms[4] = {dx, dt, D, b}; 
MPI_BCAST(iparms,2, MPI_INT,0,MPI_COMM_WORLD);
MPI_BCAST(parms, 4, MPI_DOUBLE,0, MPI_COMM_WORLD);
}
#包括
#包括
#包括
#包括
#包括
#包括“functions.h”
#包括
/*..z.mainMR主功能*/
无效主控(int argc、char*argv[]、int nPROC、int nWRs、int mster)
{
/*…定义我们将在z.mainMR函数中使用的所有变量*/
双趋势,dtfactor,dtout,D,b,dx,dtexpl,dt,time;
整数MM,M,最大步长,nsteps;
文件*datp,*outp;
/*..读取数据文件“dat”,然后将数据保存在o.out中*/
datp=fopen(argv[1],“r”);//以读取模式打开文件
outp=fopen(argv[argc-1],“w”);//以写模式打开输出文件
if(datp!=NULL)//如果数据文件不是空的,则继续
{
fscanf(datp,“%d%lf%lf%lf%lf”、&MM、&trend、&dtfactor、&dtout、&d、&b);//读取数据
fprintf(输出,“数据>>\nMM=%d\ntend=%lf\ndtfactor=%lf\ndtout=%lf\nD=%lf\nb=%lf\n”,MM,倾向,dtfactor,dtout,d,b);
fclose(datp);//关闭数据文件
fclose(outp);//关闭输出文件
}
else//如果文件为空,则打印错误消息
{
printf(“有问题。可能文件是空的。\n”);
}
/*..查找dx、M、dtexpl、dt和最大步长*/
dx=1.0/(双)毫米;
M=b*MM;
dtexpl=(dx*dx)/(2.0*D);
dt=dtfactor*dtexpl;
最大步长=(int)(趋势/dt)+1;
/*…在iparms数组中打包整数,在parms数组中打包实数*/
int-iparms[2]={MM,M};
双参数[4]={dx,dt,D,b};
MPI_BCAST(iparms,2,MPI_INT,0,MPI_COMM_WORLD);
MPI_BCAST(parms,4,MPI_DOUBLE,0,MPI_COMM_WORLD);
}

这有一个正式的答案:您将
MPI\u Bcast
拼写为
MPI\u Bcast
。我本以为这会在您试图访问一个不存在的函数时抛出一个链接错误,但显然它不存在


我猜您的MPI实现在同一个头文件中定义了Fortran和C MPI函数。然后,您的程序意外地调用了Fortran函数
MPI\u BCAST
,并且类型没有相加(
MPI\u INTEGER
(Fortran)不一定是
MPI\u INT
(C)),运行时错误是由于MPICH的一个特殊特性和C语言的一个特性的不幸组合

MPICH在单个库文件中提供C和Fortran接口代码:

000000000007c7a0 W MPI_BCAST
00000000000cd180 W MPI_Bcast
000000000007c7a0 W PMPI_BCAST
00000000000cd180 T PMPI_Bcast
000000000007c7a0 W mpi_bcast
000000000007c7a0 W mpi_bcast_
000000000007c7a0 W mpi_bcast__
000000000007c7a0 W pmpi_bcast
000000000007c7a0 T pmpi_bcast_
000000000007c7a0 W pmpi_bcast__
Fortran调用以各种别名导出,以便同时支持许多不同的Fortran编译器,包括全大写
MPI\u BCAST
MPI\u BCAST
本身未在
MPI.h
中声明,但ANSI C允许调用函数而无需前面的原型声明。通过将
-std=C99
传递给编译器来启用C99会导致出现关于
MPI\u BCAST
函数隐式声明的警告。另外,
-Wall
也会导致警告。代码将无法与OpenMPI链接,OpenMPI在一个单独的库中提供Fortran接口,
mpicc
不会链接到该库

即使代码正确编译和链接,Fortran函数也希望所有参数都通过引用传递。此外,Fortran MPI调用会在返回错误代码的位置使用额外的输出参数。因此,分割错误


为了防止将来出现此类错误,请使用
-Wall-Werror
进行编译,这样可以尽早发现类似问题。

为什么要否决我的问题?这是我在这个网站上的第一个问题,我真的需要帮助。如果我输入了一些错误或把它弄得一团糟,我向您道歉。您是如何清楚地推断问题出在
MPI\u BCAST
中的?除了C函数调用实际上拼写为
MPI\u Bcast
,我看不出显示的MPI调用有任何问题。哇!救了我的命。这是我第一次使用mpi。。问题出在拼写上!哈哈..天哪..试了两天。。我只是从某个地方复制了这个函数,它是MPI_BCAST而不是MPI_BCAST。多谢各位
000000000007c7a0 W MPI_BCAST
00000000000cd180 W MPI_Bcast
000000000007c7a0 W PMPI_BCAST
00000000000cd180 T PMPI_Bcast
000000000007c7a0 W mpi_bcast
000000000007c7a0 W mpi_bcast_
000000000007c7a0 W mpi_bcast__
000000000007c7a0 W pmpi_bcast
000000000007c7a0 T pmpi_bcast_
000000000007c7a0 W pmpi_bcast__