mirror of
				https://github.com/f4exb/sdrangel.git
				synced 2025-10-31 04:50:29 -04:00 
			
		
		
		
	FT8 support: optimize FFT buffers and plan allocation
This commit is contained in:
		
							parent
							
								
									c1c64b23e4
								
							
						
					
					
						commit
						c5a9f5fe12
					
				| @ -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 | ||||
|  | ||||
							
								
								
									
										56
									
								
								ft8/fft.cpp
									
									
									
									
									
								
							
							
						
						
									
										56
									
								
								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<std::complex<float>> 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<std::complex<float>> 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<std::complex<float>> FFTEngine::one_fft( | ||||
|         out[bi] = std::complex<float>(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<float> &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<float> &samples, int i0, int | ||||
|         } | ||||
|     } | ||||
| 
 | ||||
|     fftwf_free(m_in); | ||||
|     fftwf_free(m_out); | ||||
| 
 | ||||
|     return bins; | ||||
| } | ||||
| 
 | ||||
| @ -183,8 +177,8 @@ std::vector<std::complex<float>> 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<std::complex<float>> FFTEngine::one_fft_c( | ||||
|         out[bi] = c; | ||||
|     } | ||||
| 
 | ||||
|     fftwf_free(m_in); | ||||
|     fftwf_free(m_out); | ||||
| 
 | ||||
|     return out; | ||||
| } | ||||
| 
 | ||||
| @ -234,8 +225,8 @@ std::vector<std::complex<float>> 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<std::complex<float>> FFTEngine::one_fft_cc( | ||||
|         out[bi] = c; | ||||
|     } | ||||
| 
 | ||||
|     fftwf_free(m_in); | ||||
|     fftwf_free(m_out); | ||||
| 
 | ||||
|     return out; | ||||
| } | ||||
| 
 | ||||
| @ -281,8 +269,8 @@ std::vector<std::complex<float>> 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<std::complex<float>> FFTEngine::one_ifft_cc( | ||||
|         out[i] = c; | ||||
|     } | ||||
| 
 | ||||
|     fftwf_free(m_in); | ||||
|     fftwf_free(m_out); | ||||
| 
 | ||||
|     return out; | ||||
| } | ||||
| 
 | ||||
| @ -320,8 +305,8 @@ std::vector<float> FFTEngine::one_ifft(const std::vector<std::complex<float>> &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<float> FFTEngine::one_ifft(const std::vector<std::complex<float>> &b | ||||
|         out[i] = m_out[i]; | ||||
|     } | ||||
| 
 | ||||
|     fftwf_free(m_in); | ||||
|     fftwf_free(m_out); | ||||
| 
 | ||||
|     return out; | ||||
| } | ||||
| 
 | ||||
|  | ||||
| @ -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<std::complex<float>> analytic(const std::vector<float> &x); | ||||
|     FFTBuffers *m_fftBuffers; | ||||
| }; // FFTEngine
 | ||||
| 
 | ||||
| } // namespace FT8
 | ||||
|  | ||||
							
								
								
									
										78
									
								
								ft8/fftbuffers.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										78
									
								
								ft8/fftbuffers.cpp
									
									
									
									
									
										Normal file
									
								
							| @ -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 <http://www.gnu.org/licenses/>.          //
 | ||||
| ///////////////////////////////////////////////////////////////////////////////////
 | ||||
| #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
 | ||||
							
								
								
									
										52
									
								
								ft8/fftbuffers.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										52
									
								
								ft8/fftbuffers.h
									
									
									
									
									
										Normal file
									
								
							| @ -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 <http://www.gnu.org/licenses/>.          //
 | ||||
| ///////////////////////////////////////////////////////////////////////////////////
 | ||||
| 
 | ||||
| #ifndef FFTBUFFERS_H | ||||
| #define FFTBUFFERS_H | ||||
| 
 | ||||
| #include <map> | ||||
| #include <fftw3.h> | ||||
| 
 | ||||
| #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<int, float*> m_rs; //!< R2C inputs or C2R inputs by size
 | ||||
|     std::map<int, fftwf_complex*> m_cs; //!< R2C outputs or C2R inputs by size
 | ||||
|     std::map<int, fftwf_complex*> m_ccis; //!< C2C inputs/outputs by size
 | ||||
|     std::map<int, fftwf_complex*> m_ccos; //!< C2C outputs/inputs by size
 | ||||
| }; | ||||
| 
 | ||||
| } // FFTBuffers
 | ||||
| 
 | ||||
| #endif // FFTBUFFERS_H
 | ||||
| @ -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]; | ||||
| } | ||||
| 
 | ||||
| } | ||||
|  | ||||
| @ -21,7 +21,7 @@ | ||||
| #ifndef ft8plans_h | ||||
| #define ft8plans_h | ||||
| 
 | ||||
| #include <vector> | ||||
| #include <map> | ||||
| #include <QMutex> | ||||
| 
 | ||||
| #include "export.h" | ||||
| @ -45,7 +45,7 @@ protected: | ||||
|     static FT8Plans *m_instance; | ||||
| 
 | ||||
| private: | ||||
|     std::vector<Plan*> m_plans; | ||||
|     std::map<int, Plan*> m_plans; | ||||
|     static QMutex m_globalPlanMutex; | ||||
| 
 | ||||
| }; | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user