diff --git a/ft8/CMakeLists.txt b/ft8/CMakeLists.txt index 7faf9e5ea..83d58952a 100644 --- a/ft8/CMakeLists.txt +++ b/ft8/CMakeLists.txt @@ -2,6 +2,7 @@ project(ft8) set(ft8_SOURCES fft.cpp + fftbuffers.cpp ft8.cpp ft8plan.cpp ft8plans.cpp @@ -14,6 +15,7 @@ set(ft8_SOURCES set(ft8_HEADERS fft.h + fftbuffers.h ft8.h ft8plan.h ft8plans.h diff --git a/ft8/fft.cpp b/ft8/fft.cpp index 282d5ea6f..c0fbf4a4b 100644 --- a/ft8/fft.cpp +++ b/ft8/fft.cpp @@ -26,14 +26,19 @@ #include "util.h" #include "ft8plan.h" #include "ft8plans.h" +#include "fftbuffers.h" namespace FT8 { FFTEngine::FFTEngine() -{} +{ + m_fftBuffers = new FFTBuffers(); +} FFTEngine::~FFTEngine() -{} +{ + delete m_fftBuffers; +} // // do just one FFT on samples[i0..i0+block] @@ -57,15 +62,13 @@ std::vector> FFTEngine::one_fft( // assert((int)samples.size() - i0 >= block); - int m_in_allocated = 0; float *m_in = (float *)samples.data() + i0; if ((((unsigned long long)m_in) % 16) != 0) { // m_in must be on a 16-byte boundary for FFTW. - m_in = (float *)fftwf_malloc(sizeof(float) * p->n_); + m_in = m_fftBuffers->getR(p->n_); // assert(m_in); - m_in_allocated = 1; for (int i = 0; i < block; i++) { if (i0 + i < nsamples) @@ -79,7 +82,7 @@ std::vector> FFTEngine::one_fft( } } - fftwf_complex *m_out = (fftwf_complex *)fftwf_malloc(sizeof(fftwf_complex) * ((p->n_ / 2) + 1)); + fftwf_complex *m_out = m_fftBuffers->getC(p->n_); // assert(m_out); fftwf_execute_dft_r2c(plan, m_in, m_out); @@ -93,12 +96,6 @@ std::vector> FFTEngine::one_fft( out[bi] = std::complex(re, im); } - if (m_in_allocated) { - fftwf_free(m_in); - } - - fftwf_free(m_out); - return out; } @@ -124,8 +121,8 @@ FFTEngine::ffts_t FFTEngine::ffts(const std::vector &samples, int i0, int fftwf_plan plan = p->fwd_; // allocate our own b/c using p->m_in and p->m_out isn't thread-safe. - float *m_in = (float *)fftwf_malloc(sizeof(float) * p->n_); - fftwf_complex *m_out = (fftwf_complex *)fftwf_malloc(sizeof(fftwf_complex) * ((p->n_ / 2) + 1)); + float *m_in = m_fftBuffers->getR(p->n_); + fftwf_complex *m_out = m_fftBuffers->getC(p->n_); // assert(m_in && m_out); // float *m_in = p->r_; @@ -158,9 +155,6 @@ FFTEngine::ffts_t FFTEngine::ffts(const std::vector &samples, int i0, int } } - fftwf_free(m_in); - fftwf_free(m_out); - return bins; } @@ -183,8 +177,8 @@ std::vector> FFTEngine::one_fft_c( Plan *p = FT8Plans::GetInstance()->getPlan(block); fftwf_plan plan = p->cfwd_; - fftwf_complex *m_in = (fftwf_complex *)fftwf_malloc(block * sizeof(fftwf_complex)); - fftwf_complex *m_out = (fftwf_complex *)fftwf_malloc(block * sizeof(fftwf_complex)); + fftwf_complex *m_in = m_fftBuffers->getCCI(block); + fftwf_complex *m_out = m_fftBuffers->getCCO(block); // assert(m_in && m_out); for (int i = 0; i < block; i++) @@ -214,9 +208,6 @@ std::vector> FFTEngine::one_fft_c( out[bi] = c; } - fftwf_free(m_in); - fftwf_free(m_out); - return out; } @@ -234,8 +225,8 @@ std::vector> FFTEngine::one_fft_cc( Plan *p = FT8Plans::GetInstance()->getPlan(block); fftwf_plan plan = p->cfwd_; - fftwf_complex *m_in = (fftwf_complex *)fftwf_malloc(block * sizeof(fftwf_complex)); - fftwf_complex *m_out = (fftwf_complex *)fftwf_malloc(block * sizeof(fftwf_complex)); + fftwf_complex *m_in = m_fftBuffers->getCCI(block); + fftwf_complex *m_out = m_fftBuffers->getCCO(block); // assert(m_in && m_out); for (int i = 0; i < block; i++) @@ -266,9 +257,6 @@ std::vector> FFTEngine::one_fft_cc( out[bi] = c; } - fftwf_free(m_in); - fftwf_free(m_out); - return out; } @@ -281,8 +269,8 @@ std::vector> FFTEngine::one_ifft_cc( Plan *p = FT8Plans::GetInstance()->getPlan(block); fftwf_plan plan = p->crev_; - fftwf_complex *m_in = (fftwf_complex *)fftwf_malloc(block * sizeof(fftwf_complex)); - fftwf_complex *m_out = (fftwf_complex *)fftwf_malloc(block * sizeof(fftwf_complex)); + fftwf_complex *m_in = m_fftBuffers->getCCI(block); + fftwf_complex *m_out = m_fftBuffers->getCCO(block); // assert(m_in && m_out); for (int bi = 0; bi < block; bi++) @@ -306,9 +294,6 @@ std::vector> FFTEngine::one_ifft_cc( out[i] = c; } - fftwf_free(m_in); - fftwf_free(m_out); - return out; } @@ -320,8 +305,8 @@ std::vector FFTEngine::one_ifft(const std::vector> &b Plan *p = FT8Plans::GetInstance()->getPlan(block); fftwf_plan plan = p->rev_; - fftwf_complex *m_in = (fftwf_complex *)fftwf_malloc(sizeof(fftwf_complex) * ((p->n_ / 2) + 1)); - float *m_out = (float *)fftwf_malloc(sizeof(float) * p->n_); + fftwf_complex *m_in = m_fftBuffers->getC(p->n_); + float *m_out = m_fftBuffers->getR(p->n_); for (int bi = 0; bi < nbins; bi++) { @@ -339,9 +324,6 @@ std::vector FFTEngine::one_ifft(const std::vector> &b out[i] = m_out[i]; } - fftwf_free(m_in); - fftwf_free(m_out); - return out; } diff --git a/ft8/fft.h b/ft8/fft.h index a7ad5ec79..94be142fc 100644 --- a/ft8/fft.h +++ b/ft8/fft.h @@ -5,6 +5,10 @@ // written by Robert Morris, AB1HL // // reformatted and adapted to Qt and SDRangel context // // // +// Caution: this is intentionally not thread safe and one such engine should // +// be allocated by thread. Due to optimization of FFT buffers these buffers are // +// not shared among threads. // +// // // This program is free software; you can redistribute it and/or modify // // it under the terms of the GNU General Public License as published by // // the Free Software Foundation as version 3 of the License, or // @@ -30,6 +34,8 @@ namespace FT8 { + class FFTBuffers; + class FT8_API FFTEngine { public: @@ -47,6 +53,7 @@ public: private: std::vector> analytic(const std::vector &x); + FFTBuffers *m_fftBuffers; }; // FFTEngine } // namespace FT8 diff --git a/ft8/fftbuffers.cpp b/ft8/fftbuffers.cpp new file mode 100644 index 000000000..e1e860430 --- /dev/null +++ b/ft8/fftbuffers.cpp @@ -0,0 +1,78 @@ +/////////////////////////////////////////////////////////////////////////////////// +// Copyright (C) 2023 Edouard Griffiths, F4EXB. // +// // +// This is the code from ft8mon: https://github.com/rtmrtmrtmrtm/ft8mon // +// written by Robert Morris, AB1HL // +// reformatted and adapted to Qt and SDRangel context // +// // +// This program is free software; you can redistribute it and/or modify // +// it under the terms of the GNU General Public License as published by // +// the Free Software Foundation as version 3 of the License, or // +// (at your option) any later version. // +// // +// This program is distributed in the hope that it will be useful, // +// but WITHOUT ANY WARRANTY; without even the implied warranty of // +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // +// GNU General Public License V3 for more details. // +// // +// You should have received a copy of the GNU General Public License // +// along with this program. If not, see . // +/////////////////////////////////////////////////////////////////////////////////// +#include "fftbuffers.h" + +namespace FT8 +{ + +FFTBuffers::~FFTBuffers() +{ + for (auto& mapitem : m_rs) { + fftwf_free(mapitem.second); + } + for (auto& mapitem : m_cs) { + fftwf_free(mapitem.second); + } + for (auto& mapitem : m_ccis) { + fftwf_free(mapitem.second); + } + for (auto& mapitem : m_ccos) { + fftwf_free(mapitem.second); + } +} + +float* FFTBuffers::getR(int n) +{ + if (m_rs.find(n) == m_rs.end()) { + m_rs[n] = (float *) fftwf_malloc(sizeof(float) * n); + } + + return m_rs[n]; +} + +fftwf_complex *FFTBuffers::getC(int n) +{ + if (m_cs.find(n) == m_cs.end()) { + m_cs[n] = (fftwf_complex *) fftwf_malloc(sizeof(fftwf_complex) * ((n / 2) + 1)); + } + + return m_cs[n]; +} + +fftwf_complex *FFTBuffers::getCCI(int n) +{ + if (m_ccis.find(n) == m_ccis.end()) { + m_ccis[n] = (fftwf_complex *) fftwf_malloc(n * sizeof(fftwf_complex)); + } + + return m_ccis[n]; +} + +fftwf_complex *FFTBuffers::getCCO(int n) +{ + if (m_ccos.find(n) == m_ccos.end()) { + m_ccos[n] = (fftwf_complex *) fftwf_malloc(n * sizeof(fftwf_complex)); + } + + return m_ccos[n]; +} + +} // nemespace FT8 diff --git a/ft8/fftbuffers.h b/ft8/fftbuffers.h new file mode 100644 index 000000000..5753d5c88 --- /dev/null +++ b/ft8/fftbuffers.h @@ -0,0 +1,52 @@ +/////////////////////////////////////////////////////////////////////////////////// +// Copyright (C) 2023 Edouard Griffiths, F4EXB. // +// // +// This is the code from ft8mon: https://github.com/rtmrtmrtmrtm/ft8mon // +// written by Robert Morris, AB1HL // +// reformatted and adapted to Qt and SDRangel context // +// // +// This program is free software; you can redistribute it and/or modify // +// it under the terms of the GNU General Public License as published by // +// the Free Software Foundation as version 3 of the License, or // +// (at your option) any later version. // +// // +// This program is distributed in the hope that it will be useful, // +// but WITHOUT ANY WARRANTY; without even the implied warranty of // +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // +// GNU General Public License V3 for more details. // +// // +// You should have received a copy of the GNU General Public License // +// along with this program. If not, see . // +/////////////////////////////////////////////////////////////////////////////////// + +#ifndef FFTBUFFERS_H +#define FFTBUFFERS_H + +#include +#include + +#include "export.h" + +namespace FT8 +{ + +class FT8_API FFTBuffers +{ +public: + ~FFTBuffers(); + + float* getR(int n); + fftwf_complex *getC(int n); + fftwf_complex *getCCI(int n); + fftwf_complex *getCCO(int n); + +private: + std::map m_rs; //!< R2C inputs or C2R inputs by size + std::map m_cs; //!< R2C outputs or C2R inputs by size + std::map m_ccis; //!< C2C inputs/outputs by size + std::map m_ccos; //!< C2C outputs/inputs by size +}; + +} // FFTBuffers + +#endif // FFTBUFFERS_H diff --git a/ft8/ft8plans.cpp b/ft8/ft8plans.cpp index ce135fbc1..1b9ce0258 100644 --- a/ft8/ft8plans.cpp +++ b/ft8/ft8plans.cpp @@ -38,7 +38,7 @@ FT8Plans::~FT8Plans() qDebug("FT8::FT8Plans::~FT8Plans: %lu plans to delete", m_plans.size()); for (auto& plan : m_plans) { - delete plan; + delete plan.second; } } @@ -55,19 +55,14 @@ Plan *FT8Plans::getPlan(int n) { QMutexLocker mlock(&m_globalPlanMutex); - for (auto& plan : m_plans) - { - if ((plan->n_ == n) && (plan->type_ == Plan::M_FFTW_TYPE)) { - return plan; - } + if (m_plans.find(n) != m_plans.end()) { + return m_plans[n]; } fftwf_set_timelimit(5); - Plan *p = new Plan(n); - m_plans.push_back(p); - - return p; + m_plans[n] = new Plan(n); + return m_plans[n]; } } diff --git a/ft8/ft8plans.h b/ft8/ft8plans.h index 0ba4107d3..4567c4738 100644 --- a/ft8/ft8plans.h +++ b/ft8/ft8plans.h @@ -21,7 +21,7 @@ #ifndef ft8plans_h #define ft8plans_h -#include +#include #include #include "export.h" @@ -45,7 +45,7 @@ protected: static FT8Plans *m_instance; private: - std::vector m_plans; + std::map m_plans; static QMutex m_globalPlanMutex; };