| 1 | //-------------------------------------------------------------------------------- | 
|---|
| 2 | // Programme d'exemple multi-threads de FFT par paquet | 
|---|
| 3 | //  Test BAO-Radio/Acq | 
|---|
| 4 | //   R. Ansari - C. Magneville  ,   Decembre 2007 | 
|---|
| 5 | // Exemples de commandes pour test (dans l'ordre ci-dessous : | 
|---|
| 6 | // csh> tmtfft OCG 1000000 XXX | 
|---|
| 7 | // csh> tmtfft STG 1000000 1024 | 
|---|
| 8 | // csh> tmtfft RDG 1000000 rg1.ppf | 
|---|
| 9 | // csh> tmtfft MTG 3000000 4 | 
|---|
| 10 | //-------------------------------------------------------------------------------- | 
|---|
| 11 |  | 
|---|
| 12 | #include "sopnamsp.h" | 
|---|
| 13 | #include "zthread.h" | 
|---|
| 14 | #include "resusage.h" | 
|---|
| 15 | #include "ctimer.h" | 
|---|
| 16 | #include "timing.h" | 
|---|
| 17 |  | 
|---|
| 18 | #include <stdlib.h> | 
|---|
| 19 | #include <stdio.h> | 
|---|
| 20 | #include <unistd.h> | 
|---|
| 21 | #include <string> | 
|---|
| 22 | #include <iostream> | 
|---|
| 23 |  | 
|---|
| 24 | #include "tvector.h" | 
|---|
| 25 | #include "fioarr.h" | 
|---|
| 26 | #include "matharr.h" | 
|---|
| 27 | #include "tarrinit.h" | 
|---|
| 28 |  | 
|---|
| 29 | #include "stsrand.h" | 
|---|
| 30 | #include "fftpserver.h" | 
|---|
| 31 | #include "fftwserver.h" | 
|---|
| 32 |  | 
|---|
| 33 | #include "FFTW/fftw3.h" | 
|---|
| 34 |  | 
|---|
| 35 |  | 
|---|
| 36 | //--------------------------------------------------------------------------------- | 
|---|
| 37 | // Definition d'une classe heritant de ZThread, pour remplir un vecteur avec | 
|---|
| 38 | // des aleatoires | 
|---|
| 39 | class MTDoFFT : public ZThread { | 
|---|
| 40 | public: | 
|---|
| 41 | MTDoFFT(TVector<r_4>& v, sa_size_t nfft=10, sa_size_t paqsz=4096); | 
|---|
| 42 | virtual void run(); | 
|---|
| 43 | inline TVector<r_4>& getSpectre() { return spectre_; } | 
|---|
| 44 | protected: | 
|---|
| 45 | string nom_; | 
|---|
| 46 | TVector<r_4> vv_; | 
|---|
| 47 | sa_size_t nbfft_;  // Nb de FFT a faire | 
|---|
| 48 | sa_size_t paqsz_;  // taille des paquets pour FFT | 
|---|
| 49 | TVector<r_4> spectre_;   // spectre moyenne | 
|---|
| 50 | TVector< complex<r_4> > cfour_;  // composant TF | 
|---|
| 51 | FFTPackServer ffts_;   // serveur FFT | 
|---|
| 52 | }; | 
|---|
| 53 |  | 
|---|
| 54 | static int mtrandId = 0;  // Pour donner un identificateur a chaque thread | 
|---|
| 55 |  | 
|---|
| 56 | MTDoFFT::MTDoFFT(TVector<r_4>& v, sa_size_t nfft, sa_size_t paqsz) | 
|---|
| 57 | : vv_(v,true),    // Partage de reference du vecteur | 
|---|
| 58 | nbfft_(nfft), paqsz_(paqsz), ffts_(true) // preserve input=true | 
|---|
| 59 | { | 
|---|
| 60 | char buff[32]; | 
|---|
| 61 | sprintf(buff, "MTDoFFT-Id=%d", mtrandId); | 
|---|
| 62 | mtrandId++; | 
|---|
| 63 | nom_ = buff; | 
|---|
| 64 | // Initialisation taille vecteurs | 
|---|
| 65 | TVector<r_4> vx(paqsz); | 
|---|
| 66 | vx = RegularSequence(); | 
|---|
| 67 | ffts_.FFTForward(vx, cfour_); | 
|---|
| 68 | spectre_.ReSize(cfour_.Size()); | 
|---|
| 69 | cout << " Thread MTDoFFT(" << nom_ << " ) Created ... " << endl; | 
|---|
| 70 | } | 
|---|
| 71 |  | 
|---|
| 72 | inline r_4 Zmod2(complex<r_4> z) | 
|---|
| 73 | { return (z.real()*z.real()+z.imag()*z.imag()); } | 
|---|
| 74 |  | 
|---|
| 75 | // Le travail fait dans chaque thread (appele par start()) | 
|---|
| 76 | void MTDoFFT::run() | 
|---|
| 77 | { | 
|---|
| 78 | Timer tm(nom_.c_str()); | 
|---|
| 79 | cout << "MTDoFFT::run() - Nom= " << nom_ << " vv.Size()= " << vv_.Size() | 
|---|
| 80 | << " PaqSz=" << paqsz_ << " NbFFT= " << nbfft_ << endl; | 
|---|
| 81 | RandomGenerator rgen; | 
|---|
| 82 | TVector<r_4> vx(paqsz_); | 
|---|
| 83 | fftwf_plan plan = fftwf_plan_dft_r2c_1d(paqsz_, vx.Data(), | 
|---|
| 84 | (fftwf_complex *)cfour_.Data(), FFTW_ESTIMATE); | 
|---|
| 85 |  | 
|---|
| 86 | sa_size_t prm = nbfft_/10; | 
|---|
| 87 | for(sa_size_t i=0; i<nbfft_; i++) { | 
|---|
| 88 | sa_size_t first = rgen.Flat01()*(vv_.Size()-paqsz_-10); | 
|---|
| 89 | sa_size_t last = first+paqsz_-1; | 
|---|
| 90 | TVector<r_4> vx = vv_(Range(first,last)); | 
|---|
| 91 | fftwf_execute(plan); | 
|---|
| 92 |  | 
|---|
| 93 | //    ffts_.FFTForward(vx, cfour_); | 
|---|
| 94 | //    for(sa_size_t j=0; j<spectre_.Size(); j++) | 
|---|
| 95 | //      spectre_(j) += Zmod2(cfour_(j)); | 
|---|
| 96 | if (i%prm == 0) | 
|---|
| 97 | cout << "MTDoFFT::run() - Nom= " << nom_ << " Done i= " << i << endl; | 
|---|
| 98 | } | 
|---|
| 99 | } | 
|---|
| 100 | // ----- Fin de la definition de la classe MTDoFFT ---- | 
|---|
| 101 | //--------------------------------------------------------------------------------- | 
|---|
| 102 |  | 
|---|
| 103 | //----------------- Les fonction de test ----------------- | 
|---|
| 104 |  | 
|---|
| 105 | //------- f3_tmtfft() | 
|---|
| 106 | int f3_tmtfft(sa_size_t VSZ, int NTH, sa_size_t nfft=10, sa_size_t paqsz=4096) | 
|---|
| 107 | { | 
|---|
| 108 | cout << "[1] f3_tmtfft/starting, VSZ= " << VSZ << " NTH= " << NTH << endl; | 
|---|
| 109 | cout << "  ... NbFFT=" << nfft << " PaqSz=" << paqsz << endl; | 
|---|
| 110 | ResourceUsage res(ResourceUsage::RU_All); | 
|---|
| 111 | Timer tm("f3_tmtfft"); | 
|---|
| 112 | vector<MTDoFFT *> vth; | 
|---|
| 113 | RandomGenerator rg; | 
|---|
| 114 | TVector<r_4> DATA(VSZ); | 
|---|
| 115 | for(sa_size_t i=0; i<VSZ; i++) | 
|---|
| 116 | DATA(i) = sin(i*M_PI*0.08654)+2*cos(i*M_PI*0.27591)+rg.Flatpm1(); | 
|---|
| 117 | tm.Split("DATA-OK"); | 
|---|
| 118 | cout << "[2] f3_tmtfft/creating threads " << endl; | 
|---|
| 119 | for(int kt=0; kt<NTH; kt++) { | 
|---|
| 120 | vth.push_back(new MTDoFFT(DATA, nfft, paqsz) ); | 
|---|
| 121 | } | 
|---|
| 122 | tm.Split("Thread-Created"); | 
|---|
| 123 | cout << "[3] f3_tmtfft/starting threads " << endl; | 
|---|
| 124 | for(int kt=0; kt<NTH; kt++)  vth[kt]->start(); | 
|---|
| 125 |  | 
|---|
| 126 | cout << "[4] f3_tmtfft/waiting for all  threads to finish " << endl; | 
|---|
| 127 | sleep(1); | 
|---|
| 128 | for(int kt=0; kt<NTH; kt++)  vth[kt]->join(); | 
|---|
| 129 | tm.Split("Threads-Finished"); | 
|---|
| 130 |  | 
|---|
| 131 | cout << "[5] f3_tmtfft/saving spectra to file mtspectra.ppf " << endl; | 
|---|
| 132 | POutPersist po("mtspectra.ppf"); | 
|---|
| 133 | for(int kt=0; kt<NTH; kt++) | 
|---|
| 134 | po << vth[kt]->getSpectre(); | 
|---|
| 135 |  | 
|---|
| 136 | cout << "[6] f3_tmtfft/deleting thread objects " << endl; | 
|---|
| 137 | for(int kt=0; kt<NTH; kt++)  delete vth[kt]; | 
|---|
| 138 |  | 
|---|
| 139 | cout << res; | 
|---|
| 140 | return 0; | 
|---|
| 141 | } | 
|---|
| 142 | //----------------- FIN des fonctions de test ----------------- | 
|---|
| 143 |  | 
|---|
| 144 | //------------------------------------------------------------- | 
|---|
| 145 | //---------------------- MAIN MAIN MAIN ----------------------- | 
|---|
| 146 | //------------------------------------------------------------- | 
|---|
| 147 |  | 
|---|
| 148 | int main(int narg, char *arg[]) | 
|---|
| 149 |  | 
|---|
| 150 | { | 
|---|
| 151 | InitTim(); | 
|---|
| 152 | SophyaInit(); | 
|---|
| 153 |  | 
|---|
| 154 | int rc = 0; | 
|---|
| 155 | if (narg < 4) { | 
|---|
| 156 | cout << " tmtfft/Error args - Usage: tmtfft VSZ NTH NbFFT [PaqSz=4096]" << endl; | 
|---|
| 157 | return 1; | 
|---|
| 158 | } | 
|---|
| 159 | sa_size_t VSZ = atoi(arg[1]); | 
|---|
| 160 | int NTH = atoi(arg[2]); | 
|---|
| 161 | sa_size_t nfft = atoi(arg[3]); | 
|---|
| 162 | sa_size_t paqsz = 4096; | 
|---|
| 163 | if (narg > 4) paqsz = atoi(arg[3]); | 
|---|
| 164 | try { | 
|---|
| 165 | f3_tmtfft(VSZ, NTH, nfft, paqsz); | 
|---|
| 166 | } | 
|---|
| 167 | catch (PThrowable exc) { | 
|---|
| 168 | cerr << "tmtfft: catched Exception " << exc.Msg() << endl; | 
|---|
| 169 | rc = 77; | 
|---|
| 170 | } | 
|---|
| 171 | catch (...) { | 
|---|
| 172 | cerr << " catched unknown (...) exception (tmtfft.cc) " << endl; | 
|---|
| 173 | rc = 78; | 
|---|
| 174 | } | 
|---|
| 175 | cout << "----------- tmtfft/END ------- " << endl; | 
|---|
| 176 | PrtTim("---END tmtfft---"); | 
|---|
| 177 | return(rc); | 
|---|
| 178 |  | 
|---|
| 179 | } | 
|---|