python - Ctypes segmentation fault -
so i'm trying speedup python script loading data files , storing in array. realized if repeat procedure around 1020 times don't ask me why arrive @ segmentation fault. code loading data is:
import os,sys import numpy np import pandas pd import ctypes ct version = 0.1 try: path = os.path.join(os.path.dirname(os.path.realpath(__file__)), "_vec") except nameerror: path = "./_im7" if not(sys.platform in ('win32', 'cygwin')): path += '.so.'+str(version) libfunctions = ct.cdll.loadlibrary(path) else: libfunctions = ct.cdll.loadlibrary(path) libfunctions.readvec.restype = ct.c_void_p libfunctions.readvec.argtypes = [ct.c_char_p, np.ctypeslib.ndpointer(ct.c_float), \ ct.c_int,ct.c_int,ct.c_int] def readvecct(filename,nx,ny,nz): # supposing moment naming scheme piv__vxy.case piv__vxy.geo not changes should # not case appropriate changes have made corresponding file # ctypes data_temp = np.zeros((2*ny*nx,1),dtype=np.dtype('f4')) libfunctions.readvec(ct.c_char_p(filename),data_temp,ct.c_int(nx), ct.c_int(ny),ct.c_int(3)) # w value if (nz)>1: return data_temp[:nx*ny].reshape(ny, nx), data_temp[nx*ny:2*nx*ny].reshape(ny, nx), data_temp[2*nx*ny:].reshape(ny, nx) else: return data_temp[:nx*ny].reshape(ny, nx), data_temp[nx*ny:].reshape(ny, nx)
the underlying c function:
#include <stdio.h> void readvec(const char *fname, float *data, int nx, int ny,int skiprows) { //void cfun(const double * indata, int rowcount, int colcount, double * outdata) { int i,j,check; size_t length; file *file; char buffer[1024]; char *buffer_ptr = &buffer[0]; //puts("here go!"); file = fopen(fname, "r"); //printf("nx: %d ny: %d skiprows: %d \n",nx,ny,skiprows); //printf("filename %s \n",fname); (i=0;i<=skiprows;i++){ check=getline(&buffer_ptr,&length,file); //printf("buffer: %s \n",buffer); if (check==-1){ puts("error"); } } (i = 0; < ny; i++) { (j=0;j<nx;j++){ check=fscanf(file,"%f",&data[i*nx+j]); //printf("data %s\n",buffer); if (check==-1){ puts("error"); } } } (i = 0; < ny; i++) { (j=0;j<nx;j++){ check=fscanf(file,"%f",&data[ny*nx+i*nx+j]); if (check==-1){ puts("error"); } } } //fclose(file); //puts("done!"); }
and simple test produces error:
import time import numpy np import libvec.libvec vec import matplotlib.pyplot plt tmp_geo = '/mnt/shared/projects/morphing/users/jschelle/raw_treated_201509/res_u_8-ms-dp-4-mmce_sma_0-mm-mcf_0-hz/u_8-ms-dp-4-mmce_sma_0-mm-mcf_0-hz_20150824_seq_01/paraview/piv__vxy.geo' tmp_file = '/mnt/shared/projects/morphing/users/jschelle/raw_treated_201509/res_u_8-ms-dp-4-mmce_sma_0-mm-mcf_0-hz/u_8-ms-dp-4-mmce_sma_0-mm-mcf_0-hz_20150824_seq_01/paraview/piv__vxy_01019.vec' tmp_case = '/mnt/shared/projects/morphing/users/jschelle/raw_treated_201509/res_u_8-ms-dp-4-mmce_sma_0-mm-mcf_0-hz/u_8-ms-dp-4-mmce_sma_0-mm-mcf_0-hz_20150824_seq_01/paraview/piv__vxy.case' x,y,z = vec.readgeo(tmp_geo) nx = len(x) ny = len(y) nz = len(z) iterations = 1100 start_time = time.time() in range(iterations): u,v = vec.readvecct(tmp_file,nx,ny,nz); print("ctypes --- %f seconds ---" % ((time.time() - start_time)/iterations))
now when number of iterations 1000 no problem occurs @ 1100 segmentation fault. imagine has sth memory management don't know how fix or start! appreciated.
thx lot in advance
j
turns out:
fclose(file)
was commented not directly lead error @ same time leads segfault after n iterations
thank everyone!
Comments
Post a Comment