[英]How to Approach Segmentation Fault ( memory Leak ) Fortran function with C Interface?
我在 C 接口中调用 libport.a 的 Netlib Fortran function。 我在 function istkin_()
出现分段错误,然后在dgqex_()
() 出现段错误。 http://www.netlib.org/port/ 。 这具有上述两个功能, https://github.com/AmbikaB29/Port3 。
以下命令用于编译和执行:
nvcc --compiler-options -fpie -g -use_fast_math --disable-warnings -Wno-deprecated-gpu-targets -c test_C4.c -lstdc++
gfortran -fcheck=all,no-array-temps -fstack-protector-all -Wall -ggdb3 -Wno-main -fno-second-underscore -use_fast_math test_C4.o -L/usr/local/lib -lport
./a.out
我尝试使用以下命令在调试模式下使用 Valgrind 进行检查:
valgrind --leak-check=full --show-leak-kinds=all --track-origins=yes --verbose --log-file=valgrind-out.txt ./a.out
我发布了一个简洁的完整代码来生成错误。
// -----------------
# include <stdio.h>
# include <math.h>
# include <unistd.h>
#include <stdlib.h>
struct CSTAK;
struct {
double DS[4000];
int LS[8000];
int IS[8000];
} CSTAK;
extern void istkin_(const int ,const int );
extern void dgqex_(int NQUAD,double PQUAD[],double WQ[]);
int main(int argc, char **argv)
{
struct CSTAK;
// Quadrature Variables
const int NQUAD=25;
int i;
double PQUAD[NQUAD], WQ[NQUAD]; //Port Variables
// double *PQUAD=(double*)malloc(NQUAD*sizeof(int));
// double *WQ=(double*)malloc(NQUAD*sizeof(int));
istkin_(4000,4);// netlib port Initializes Stack
dgqex_(NQUAD,&PQUAD[1],&WQ[1]); // netlib port Guass Quadrature
for (i=0; i< NQUAD;i++)
{
printf("P[%d]=%lf\n",i,PQUAD[i] );
}
// realloc(PQUAD, 0);
// realloc(WQ,0);
free(PQUAD);
free(WQ);
}
memory 泄漏与静态定义的数组或为PQUAD
和WQ
变量动态分配的 memory 发生段错误。
Valgrind 日志文件:
堆摘要:
==18301== HEAP SUMMARY:
==18301== in use at exit: 5,444 bytes in 17 blocks
==18301== total heap usage: 21 allocs, 4 frees, 13,584 bytes allocated
注意附加信息:我检查了两个 function 与 f95/gfortran 编译并执行。 它在编译和运行时没有给出错误。
我不确定从 C 例程特别是使用 CUDA 调用 Fortran 遗留函数是否是一个好主意。 我重建了 libport 库,一切都是徒劳的。 对于 fortran &PQUAD(1) 和 C &PQUAD[0],在通过引用传递变量时是否与数组索引有关?
在等待发布上述分段/内存泄漏问题的工作版本后:
# include <stdio.h>
# include <math.h>
# include <unistd.h>
#include <stdlib.h>
struct CSTAK;
struct {
double DS[4000];
int LS[8000];
int IS[8000];
} CSTAK;
extern void istkin_(const int *,const int *);
extern void dgqex_(const int *,double *,double *);
int main(int argc, char **argv)
{
struct CSTAK;
// Quadrature Variables
const int NQUAD=25;
const int nSize=4000;
const int nByte=4;
int i;
double PQUAD[NQUAD], WQ[NQUAD]; //Port Variables
istkin_(&nSize,&nByte);// netlib port Initializes Stack
dgqex_(&NQUAD,&PQUAD[0],&WQ[0]); // netlib port Guass Quadrature
for (i=0; i< NQUAD;i++)
{
printf("P[%d]=%lf\n",i,PQUAD[i] );
}
} ```
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.