fix segfault, cleanup and resize
This commit is contained in:
parent
0846799f28
commit
2f1925b8aa
4
setup.py
4
setup.py
@ -8,10 +8,10 @@ c_ext = setuptools.Extension("creader",
|
|||||||
include_dirs=[
|
include_dirs=[
|
||||||
np.get_include(),"src/"
|
np.get_include(),"src/"
|
||||||
],
|
],
|
||||||
extra_compile_args=['-std=c++11', '-Wall', '-Wextra'] )
|
extra_compile_args=['-std=c99', '-Wall', '-Wextra'] )
|
||||||
|
|
||||||
|
|
||||||
c_ext.language = 'c++'
|
c_ext.language = 'c'
|
||||||
setuptools.setup(
|
setuptools.setup(
|
||||||
name= 'creader',
|
name= 'creader',
|
||||||
version = '0.1',
|
version = '0.1',
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
#include "cluster_reader.h"
|
#include "cluster_reader.h"
|
||||||
|
|
||||||
int read_clusters(FILE* fp, int64_t n_clusters, Cluster* buf, int *n_left){
|
int read_clusters(FILE* fp, int64_t n_clusters, Cluster* buf, int *n_left){
|
||||||
printf("Item size: %d n_clusters: %d, n_left: %d\n", sizeof(Cluster), n_clusters,*n_left);
|
printf("Item size: %lu n_clusters: %lld, n_left: %d\n", sizeof(Cluster), n_clusters,*n_left);
|
||||||
int iframe=0, nph=*n_left;
|
int iframe=0, nph=*n_left;
|
||||||
size_t n_read=0, nph_read=0, nn=*n_left, nr=0;
|
size_t n_read=0, nph_read=0, nn=*n_left, nr=0;
|
||||||
//n_left=n_clusters;
|
//n_left=n_clusters;
|
||||||
@ -39,7 +39,7 @@ int read_clusters(FILE* fp, int64_t n_clusters, Cluster* buf, int *n_left){
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
// size_t n_read = fread(buf, sizeof(Cluster), n_clusters, fp);
|
// size_t n_read = fread(buf, sizeof(Cluster), n_clusters, fp);
|
||||||
printf("Read: %d items %d left %d\n", nph_read, n_read,*n_left);
|
printf("Read: %zu items %zu left %d\n", nph_read, n_read,*n_left);
|
||||||
return nph_read;
|
return nph_read;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -67,26 +67,15 @@ static int ClusterFileReader_init(ClusterFileReader *self, PyObject *args,
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Close file on clearing the object
|
// Custom destructor to make sure we close the file
|
||||||
static int
|
static void
|
||||||
ClusterFileReader_clear(ClusterFileReader *self)
|
ClusterFileReader_dealloc(ClusterFileReader *self)
|
||||||
{
|
{
|
||||||
if(self->fp){
|
if(self->fp){
|
||||||
printf("Closing file\n");
|
printf("Closing file\n");
|
||||||
fclose(self->fp);
|
fclose(self->fp);
|
||||||
self->fp = NULL;
|
self->fp = NULL;
|
||||||
self->n_left=0;
|
|
||||||
}
|
}
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Custom destructor, not sure this is needed
|
|
||||||
// might be an easier way to access the fclose on clear
|
|
||||||
static void
|
|
||||||
ClusterFileReader_dealloc(ClusterFileReader *self)
|
|
||||||
{
|
|
||||||
PyObject_GC_UnTrack(self);
|
|
||||||
ClusterFileReader_clear(self);
|
|
||||||
Py_TYPE(self)->tp_free((PyObject *) self);
|
Py_TYPE(self)->tp_free((PyObject *) self);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -106,6 +95,7 @@ ClusterFileReader_read(ClusterFileReader *self, PyObject *args,
|
|||||||
|
|
||||||
//Create an uninitialized numpy array
|
//Create an uninitialized numpy array
|
||||||
PyArray_Descr *dtype = cluster_dt();
|
PyArray_Descr *dtype = cluster_dt();
|
||||||
|
// PyObject *PyArray_SimpleNewFromDescr(int nd, npy_int const *dims, PyArray_Descr *descr)
|
||||||
PyObject *clusters = PyArray_SimpleNewFromDescr(ndim, dims, dtype);
|
PyObject *clusters = PyArray_SimpleNewFromDescr(ndim, dims, dtype);
|
||||||
PyArray_FILLWBYTE((PyArrayObject *)clusters, 0); //zero initialization can be removed later
|
PyArray_FILLWBYTE((PyArrayObject *)clusters, 0); //zero initialization can be removed later
|
||||||
|
|
||||||
@ -117,7 +107,22 @@ ClusterFileReader_read(ClusterFileReader *self, PyObject *args,
|
|||||||
// Here goes the looping, removing frame numbers etc.
|
// Here goes the looping, removing frame numbers etc.
|
||||||
self->n_read = read_clusters(self->fp, size, buf, &self->n_left);
|
self->n_read = read_clusters(self->fp, size, buf, &self->n_left);
|
||||||
|
|
||||||
//TODO! All kinds of error checking here!!! Resize array etc.
|
|
||||||
|
if(self->n_read != size){
|
||||||
|
//resize the array to match the number of read photons
|
||||||
|
//this will reallocate memory
|
||||||
|
|
||||||
|
//create a new_shape struct on the stack
|
||||||
|
PyArray_Dims new_shape;
|
||||||
|
|
||||||
|
//reuse dims for the shape
|
||||||
|
dims[0] = self->n_read;
|
||||||
|
new_shape.ptr = dims;
|
||||||
|
new_shape.len = 1;
|
||||||
|
|
||||||
|
// resize the array to match the number of clusters read
|
||||||
|
PyArray_Resize((PyArrayObject *)clusters, &new_shape, 1, NPY_ANYORDER);
|
||||||
|
}
|
||||||
|
|
||||||
return clusters;
|
return clusters;
|
||||||
}
|
}
|
||||||
@ -137,9 +142,8 @@ static PyTypeObject ClusterFileReaderType = {
|
|||||||
.tp_doc = PyDoc_STR("ClusterFileReader implemented in C"),
|
.tp_doc = PyDoc_STR("ClusterFileReader implemented in C"),
|
||||||
.tp_basicsize = sizeof(ClusterFileReader),
|
.tp_basicsize = sizeof(ClusterFileReader),
|
||||||
.tp_itemsize = 0,
|
.tp_itemsize = 0,
|
||||||
.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
|
.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
|
||||||
.tp_new = PyType_GenericNew,
|
.tp_new = PyType_GenericNew,
|
||||||
.tp_clear = (inquiry) ClusterFileReader_clear,
|
|
||||||
.tp_dealloc = (destructor) ClusterFileReader_dealloc,
|
.tp_dealloc = (destructor) ClusterFileReader_dealloc,
|
||||||
.tp_init = (initproc)ClusterFileReader_init,
|
.tp_init = (initproc)ClusterFileReader_init,
|
||||||
.tp_methods = ClusterFileReader_methods,
|
.tp_methods = ClusterFileReader_methods,
|
||||||
|
10
test.py
10
test.py
@ -2,7 +2,13 @@ from creader import ClusterFileReader
|
|||||||
import numpy as np
|
import numpy as np
|
||||||
|
|
||||||
maxph=100000000
|
maxph=100000000
|
||||||
|
maxph=100
|
||||||
|
|
||||||
|
from pathlib import Path
|
||||||
|
fpath = Path('/Users/erik/data/clusters/beam_En700eV_-40deg_300V_10us_d0_f0_100.clust')
|
||||||
|
# r = ClusterFileReader("/mnt/moench_data/Moench_LGAD_SIM_Nov22/moenchLGAD202211/clustW17new/beam_En800eV_-40deg_300V_10us_d0_f5_0.clust")
|
||||||
|
|
||||||
|
r = ClusterFileReader(fpath.as_posix())
|
||||||
|
|
||||||
r = ClusterFileReader("/mnt/moench_data/Moench_LGAD_SIM_Nov22/moenchLGAD202211/clustW17new/beam_En800eV_-40deg_300V_10us_d0_f5_0.clust")
|
|
||||||
a=r.read(maxph)
|
a=r.read(maxph)
|
||||||
print(a[::100000])
|
# print(a[::100000])
|
||||||
|
Reference in New Issue
Block a user