Skip to content

Commit

Permalink
Fix resource managment and a bug in the WProjection gridder's kernel …
Browse files Browse the repository at this point in the history
…correction function.
  • Loading branch information
jaycedowell committed Jan 29, 2025
1 parent 7f20444 commit e74c449
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 71 deletions.
8 changes: 4 additions & 4 deletions lsl/common/fir.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -430,10 +430,10 @@ static PyObject *integerBeamformer(PyObject *self, PyObject *args, PyObject *kwd
/*
Cleanup
*/
// free(t1);
// free(t2);
// free(tX);
// free(tY);
apool.release(t1);
apool.release(t2);
apool.release(tX);
apool.release(tY);
}
}

Expand Down
50 changes: 25 additions & 25 deletions lsl/correlator/core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -227,14 +227,14 @@ void compute_fengine_real(long nStand,
*(valid + nFFT*i + j) = (unsigned char) cleanFactor;
}

// fftwf_free(in);
// fftwf_free(out);
mpool.release(in);
mpool.release(out);
}
// aligned64_free(rot);
apool.release(rot);

// fftwf_destroy_plan(p);
// fftwf_free(inP);
// fftwf_free(outP);
pcache.release_plan(p);
mpool.release(inP);
mpool.release(outP);

Py_END_ALLOW_THREADS

Expand Down Expand Up @@ -286,7 +286,7 @@ void compute_fengine_complex(long nStand,
#pragma omp parallel default(shared) private(in, i, j, k, l, secStart, cleanFactor)
#endif
{
in = (Complex32*) fftwf_malloc(sizeof(Complex32) * nChan*nTap);
in = (Complex32*) mpool.acquire<Complex32>(nChan*nTap);

#ifdef _OPENMP
#pragma omp for schedule(OMP_SCHEDULER)
Expand Down Expand Up @@ -337,12 +337,12 @@ void compute_fengine_complex(long nStand,
*(valid + nFFT*i + j) = (unsigned char) cleanFactor;
}

// fftwf_free(in);
mpool.release(in);
}
// aligned64_free(rot);
apool.release(rot);

// fftwf_destroy_plan(p);
// fftwf_free(inP);
pcache.release_plan(p);
mpool.release(inP);

Py_END_ALLOW_THREADS
}
Expand Down Expand Up @@ -446,8 +446,8 @@ static PyObject *FEngine(PyObject *self, PyObject *args, PyObject *kwds) {
dataF = (PyArrayObject*) PyArray_ZEROS(3, dims, NPY_COMPLEX64, 0);
if(dataF == NULL) {
PyErr_Format(PyExc_MemoryError, "Cannot create output array");
// aligned64_free(fifo);
// aligned64_free(frac);
apool.release(fifo);
apool.release(frac);
goto fail;
}

Expand All @@ -458,8 +458,8 @@ static PyObject *FEngine(PyObject *self, PyObject *args, PyObject *kwds) {
validF = (PyArrayObject*) PyArray_ZEROS(2, dimsV, NPY_UINT8, 0);
if(validF == NULL) {
PyErr_Format(PyExc_MemoryError, "Cannot create valid index array");
// aligned64_free(fifo);
// aligned64_free(frac);
apool.release(fifo);
apool.release(frac);
goto fail;
}

Expand Down Expand Up @@ -496,8 +496,8 @@ static PyObject *FEngine(PyObject *self, PyObject *args, PyObject *kwds) {
#undef LAUNCH_FENGINE_REAL
#undef LAUNCH_FENGINE_COMPLEX

// aligned64_free(frac);
// aligned64_free(fifo);
apool.release(frac);
apool.release(fifo);

signalsF = Py_BuildValue("(OO)", PyArray_Return(dataF), PyArray_Return(validF));

Expand Down Expand Up @@ -648,8 +648,8 @@ static PyObject *PFBEngine(PyObject *self, PyObject *args, PyObject *kwds) {
dataF = (PyArrayObject*) PyArray_ZEROS(3, dims, NPY_COMPLEX64, 0);
if(dataF == NULL) {
PyErr_Format(PyExc_MemoryError, "Cannot create output array");
// aligned64_free(fifo);
// aligned64_free(frac);
apool.release(fifo);
apool.release(frac);
goto fail;
}

Expand All @@ -660,8 +660,8 @@ static PyObject *PFBEngine(PyObject *self, PyObject *args, PyObject *kwds) {
validF = (PyArrayObject*) PyArray_ZEROS(2, dimsV, NPY_UINT8, 0);
if(validF == NULL) {
PyErr_Format(PyExc_MemoryError, "Cannot create valid index array");
// aligned64_free(fifo);
// aligned64_free(frac);
apool.release(fifo);
apool.release(frac);
goto fail;
}

Expand Down Expand Up @@ -698,9 +698,9 @@ static PyObject *PFBEngine(PyObject *self, PyObject *args, PyObject *kwds) {
#undef LAUNCH_PFBENGINE_REAL
#undef LAUNCH_PFBENGINE_COMPLEX

// aligned64_free(frac);
// aligned64_free(fifo);
// aligned64_free(pfb);
apool.release(frac);
apool.release(fifo);
apool.release(pfb);

signalsF = Py_BuildValue("(OO)", PyArray_Return(dataF), PyArray_Return(validF));

Expand All @@ -714,7 +714,7 @@ static PyObject *PFBEngine(PyObject *self, PyObject *args, PyObject *kwds) {

fail:
if( pfb != NULL ) {
// aligned64_free(pfb);
apool.release(pfb);
}
Py_XDECREF(data);
Py_XDECREF(freq);
Expand Down
4 changes: 2 additions & 2 deletions lsl/correlator/pool.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ class Aligned64BufferPool {
return;
}
}
throw std::runtime_error("Attempted to release unmanaged aligned buffer");
throw std::runtime_error("Attempted to release unmanaged aligned64 buffer");
}

std::tuple<size_t, size_t> get_stats() {
Expand Down Expand Up @@ -163,7 +163,7 @@ class FFTWBufferPool {
return;
}
}
throw std::runtime_error("Attempted to release unmanaged buffer");
throw std::runtime_error("Attempted to release unmanaged fftw buffer");
}

std::tuple<size_t, size_t> get_stats() {
Expand Down
22 changes: 11 additions & 11 deletions lsl/correlator/spec.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -143,12 +143,12 @@ void compute_spec_real(long nStand,
blas_scal(nChan, 1.0/(2*nChan*nActFFT), (psd + i*nChan), 1);
}

// fftwf_free(in);
// fftwf_free(out);
mpool.release(in);
mpool.release(out);
}
// fftwf_destroy_plan(p);
// fftwf_free(inP);
// fftwf_free(outP);
pcache.release_plan(p);
mpool.release(inP);
mpool.release(outP);

Py_END_ALLOW_THREADS
}
Expand Down Expand Up @@ -246,11 +246,11 @@ void compute_spec_complex(long nStand,
blas_scal(nChan, 1.0/(nActFFT*nChan), (psd + i*nChan), 1);
}

// fftwf_free(in);
// aligned64_free(temp2);
mpool.release(in);
apool.release(temp2);
}
// fftwf_destroy_plan(p);
// fftwf_free(inP);
pcache.release_plan(p);
mpool.release(inP);

Py_END_ALLOW_THREADS
}
Expand Down Expand Up @@ -471,7 +471,7 @@ static PyObject *PFBPSD(PyObject *self, PyObject *args, PyObject *kwds) {
#undef LAUNCH_PFB_REAL
#undef LAUNCH_PFB_COMPLEX

// aligned64_free(pfb);
apool.release(pfb);

signalsF = Py_BuildValue("O", PyArray_Return(dataF));

Expand All @@ -482,7 +482,7 @@ static PyObject *PFBPSD(PyObject *self, PyObject *args, PyObject *kwds) {

fail:
if( pfb != NULL ) {
// aligned64_free(pfb);
apool.release(pfb);
}
Py_XDECREF(data);
Py_XDECREF(dataF);
Expand Down
30 changes: 15 additions & 15 deletions lsl/correlator/stokes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -172,14 +172,14 @@ void compute_stokes_real(long nStand,
}
}

// fftwf_free(inX);
// fftwf_free(inY);
// fftwf_free(outX);
// fftwf_free(outY);
mpool.release(inX);
mpool.release(inY);
mpool.release(outX);
mpool.release(outY);
}
// fftwf_destroy_plan(p);
// fftwf_free(inP);
// fftwf_free(outP);
pcache.release_plan(p);
mpool.release(inP);
mpool.release(outP);

Py_END_ALLOW_THREADS
}
Expand Down Expand Up @@ -305,12 +305,12 @@ void compute_stokes_complex(long nStand,
}
}

// fftwf_free(inX);
// fftwf_free(inY);
// aligned64_free(temp2);
mpool.release(inX);
mpool.release(inY);
apool.release(temp2);
}
// fftwf_destroy_plan(p);
// fftwf_free(inP);
pcache.release_plan(p);
mpool.release(inP);

Py_END_ALLOW_THREADS
}
Expand Down Expand Up @@ -536,7 +536,7 @@ static PyObject *PFBPSD(PyObject *self, PyObject *args, PyObject *kwds) {
}

// Calculate the windowing function for the PFB
pfb = (double*) aligned64_malloc(sizeof(double) * (1+isReal)*nChan*nTap);
pfb = (double*) apool.acquire<double>((1+isReal)*nChan*nTap);
for(int i=0; i<(1+isReal)*nChan*nTap; i++) {
*(pfb + i) = sinc((i - (1+isReal)*nChan*nTap/2.0 + 0.5)/((1+isReal)*nChan));
*(pfb + i) *= hamming(2*NPY_PI*i/((1+isReal)*nChan*nTap));
Expand Down Expand Up @@ -583,7 +583,7 @@ static PyObject *PFBPSD(PyObject *self, PyObject *args, PyObject *kwds) {
#undef LAUNCH_PFB_REAL
#undef LAUNCH_PFB_COMPLEX

aligned64_free(pfb);
apool.release(pfb);

signalsF = Py_BuildValue("O", PyArray_Return(dataF));

Expand All @@ -595,7 +595,7 @@ static PyObject *PFBPSD(PyObject *self, PyObject *args, PyObject *kwds) {

fail:
if( pfb != NULL ) {
aligned64_free(pfb);
apool.release(pfb);
}
Py_XDECREF(dataX);
Py_XDECREF(dataY);
Expand Down
28 changes: 15 additions & 13 deletions lsl/imaging/gridder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -164,11 +164,13 @@ void compute_kernel_correction(long nPixSide,

// Inverse transform
fftwf_plan pB;
float *inP = mpool.acquire<float>(nPixSide*GRID_KERNEL_OVERSAMPLE);
pB = pcache.plan_r2r_1d(nPixSide*GRID_KERNEL_OVERSAMPLE/2,
corr_full, corr_full,
inP, inP,
FFTW_REDFT01, FFTW_MEASURE);
fftwf_execute(pB);
// fftwf_destroy_plan(pB);
fftwf_execute_r2r(pB, corr_full, corr_full);
pcache.release_plan(pB);
mpool.release(inP);

// Select what to keep
for(i=0; i<nPixSide; i++) {
Expand All @@ -190,7 +192,7 @@ void compute_kernel_correction(long nPixSide,
}

// Cleanup
// aligned64_free(corr_full);
mpool.release(corr_full);
}


Expand Down Expand Up @@ -349,23 +351,23 @@ void compute_gridding(long nVis,
}
}

// fftwf_free(suv);
// fftwf_free(sbm);
// fftwf_free(kern);
mpool.release(suv);
mpool.release(sbm);
mpool.release(kern);
}

// Correct for the kernel
compute_kernel_correction(nPixSide, kernel1D, corr);

// Cleanup
// fftwf_destroy_plan(pF);
// fftwf_destroy_plan(pR);
// fftwf_free(inP);
pcache.release_plan(pF);
pcache.release_plan(pR);
mpool.release(inP);

// aligned64_free(kernel1D);
apool.release(kernel1D);

// free(planeStart);
// free(planeStop);
apool.release(planeStart);
apool.release(planeStop);

Py_END_ALLOW_THREADS
}
Expand Down
2 changes: 1 addition & 1 deletion lsl/sim/simfast.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ void compute_visibility(long nBL,
}
}

// aligned64_free(tempVis);
apool.release(tempVis);
}

Py_END_ALLOW_THREADS
Expand Down

0 comments on commit e74c449

Please sign in to comment.