我有以下c库tridiag.c
:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
#define PI 3.14159265358979323846
typedef struct tridiag_matrix{
double *a;
double *b;
double *c;
} Tridiag_M;
inline
double interpl_4(double a,double b,double c,double d,double frac){
return b*(1-frac)+c*frac;
};
double cubic_interpolate( float y0, float y1, float y2, float y3, float mu ) {
double a0, a1, a2, a3, mu2;
mu2 = mu*mu;
a0 = y3 - y2 - y0 + y1; //p
a1 = y0 - y1 - a0;
a2 = y2 - y0;
a3 = y1;
return ( a0*mu*mu2 + a1*mu2 + a2*mu + a3 );
}
inline double cos_interpl(double a,double b,double frac){
double mu2=(1-cos(frac*PI))/2;
return a*(1-mu2)+b*mu2;
};
void solve_tridiagonal(Tridiag_M *t, double *r,double *x,int N) {
int in;
double *cprime=(double*) malloc(N*sizeof(double));
cprime[0] = t->c[0] / t->b[0];
x[0] = r[0] / t->b[0];
/* loop from 1 to N - 1 inclusive */
for (in = 1; in < N; in++) {
double m = 1.0 / (t->b[in] - t->a[in] * cprime[in - 1]);
cprime[in] = t->c[in] * m;
x[in] = (r[in] - t->a[in] * x[in - 1]) * m;
}
/* loop from N - 2 to 0 inclusive, safely testing loop end condition */
for (in = N - 1; in-- > 0; ){
x[in] = x[in] - cprime[in] * x[in + 1]; /*wrong cprime[in] ebasta!*/
}
/* free scratch space */
free(cprime);
}
void delay_line(double *Y, int *delay0,int *delay1,int *delay2,int *delay3,double *dev,double *out,int M,int N){
int i;
for(i=0;i<N;i++){
int k=M*i;
if(dev[i]<1){
out[i]=cubic_interpolate(Y[k+delay0[i]],Y[k+delay1[i]],Y[k+delay2[i]],Y[k+delay3[i]],dev[i]);
}
else{
out[i]=cubic_interpolate(Y[k+(delay0[i]+1)%M],Y[k+(delay1[i]+1)%M],Y[k+(delay2[i]+1)%M],Y[k+(delay3[i]+1)%M],dev[i]-1);
}
}
}
void calculate_g(double *V,double *Y,double *sherad_factor,double *sheraD,double *sheraRho,double *Yzweig,double *omega,double *g,double d_m_factor,const int n){
int i;
g[0]=d_m_factor*V[0];
for(i=1;i<n;i++){
g[i]=sherad_factor[i]*sheraD[i]*V[i]+omega[i]*omega[i]*(Y[i]+sheraRho[i]*Yzweig[i]);
}
}
我正在Windows 7 64位上使用gcc 4.5.0 / mingw32进行编译。
gcc -shared -m32 -o -Wall tridiag.so_windows tridiag.c
。干净地退出,没有任何输出。
在python 3.5.x中,我正在加载库:
# load C library
libtrisolv = np.ctypeslib.load_library(tridiagName, resources_root)
# load tridiagonal solver function and defines input
libtrisolv.solve_tridiagonal.restype = None
libtrisolv.solve_tridiagonal.argtypes = [ctypes.POINTER(TridiagMatrix), # aa
PDOUBLE, # vv
PDOUBLE, # solution
INT, # nrows
]
libtrisolv.delay_line.restype = None # TODO SPEEDUP W POINTERS!
libtrisolv.delay_line.argtypes = [PDOUBLE, # in_matrix
PINT, # delay1
PINT, # delay2
PINT, # delay1
PINT, # delay1
PDOUBLE, # dev
PDOUBLE, # YZweig
INT, # delay_buffer_length
INT # n
]
这很好。
但是,当我调用任何成员函数时,控制永远不会返回到python:
libtrisolv.delay_line(
model.Ybuffer_pointer, model.Zrp_pointer, model.Zrp1_pointer,
model.Zrp2_pointer, model.Zrp3_pointer, model.Dev_pointer,
model.YZweig_pointer, ctypes.c_int(model.YbufferLgt),
ctypes.c_int(model.n + 1))
在Mac / Linux上,大约需要3秒钟才能返回,而且确实如此。在Windows上,我让它运行了一整夜而没有返回。
这是因为我在64位系统上使用mingw32,还是发生了其他情况?
最佳答案
得到它的工作。我安装了Mingw-64与gcc (x86_64-posix-seh-rev0, Built by MinGW-W64 project) 5.3.0
并用gcc -Wall -shared -m64 -o tridiag.so_windows tridiag.c
现在运行。所以我认为这只是一个愚蠢的体系结构问题。