| 1 | //----------------------------------------------------------------
 | 
|---|
| 2 | // Projet BAORadio - (C) LAL/IRFU  2008-2010
 | 
|---|
| 3 | // Classes de threads de calcul de visibilites pour BAORadio 
 | 
|---|
| 4 | //----------------------------------------------------------------
 | 
|---|
| 5 | 
 | 
|---|
| 6 | #include <stdlib.h>
 | 
|---|
| 7 | #include <string.h>
 | 
|---|
| 8 | #include <unistd.h>
 | 
|---|
| 9 | #include <fstream>
 | 
|---|
| 10 | #include <signal.h>
 | 
|---|
| 11 | 
 | 
|---|
| 12 | #include "pexceptions.h"
 | 
|---|
| 13 | #include "fioarr.h"
 | 
|---|
| 14 | #include "matharr.h"
 | 
|---|
| 15 | #include "timestamp.h"
 | 
|---|
| 16 | #include "ctimer.h"
 | 
|---|
| 17 | 
 | 
|---|
| 18 | #include "brviscalc.h"
 | 
|---|
| 19 | 
 | 
|---|
| 20 | 
 | 
|---|
| 21 | //---------------------------------------------------------------------
 | 
|---|
| 22 | // Classe de traitement - calcul de visibilite pour n fibres 
 | 
|---|
| 23 | //---------------------------------------------------------------------
 | 
|---|
| 24 | 
 | 
|---|
| 25 | /* --Methode-- */
 | 
|---|
| 26 | BRVisibilityCalculator::BRVisibilityCalculator(RAcqMemZoneMgr& memgr, string outpath, uint_4 nmean, size_t nthr)
 | 
|---|
| 27 |   : BRBaseProcessor(memgr), paralex_(*this, nthr), nparthr_(nthr), 
 | 
|---|
| 28 |     outpath_(outpath), nmean_(nmean), nbcalc_(1), calcid_(0), vpdata_(2*memgr.NbFibres())
 | 
|---|
| 29 |     // , dtfos_(outpath+"visdt.fits", Fits_Create), visdt_(dtfos_, 1024, true);
 | 
|---|
| 30 | {  
 | 
|---|
| 31 |   DefineRank(1,0);
 | 
|---|
| 32 | 
 | 
|---|
| 33 |   uint_4 maxnpairs = (2*memgr_.NbFibres()+1)*memgr_.NbFibres();
 | 
|---|
| 34 |   chanids_.SetSize(2*memgr_.NbFibres());
 | 
|---|
| 35 |   chanpairnumall_.SetSize(maxnpairs);
 | 
|---|
| 36 |   chanpairsall_.SetSize(maxnpairs,2);
 | 
|---|
| 37 |   for(size_t i=0; i<2*memgr_.NbFibres(); i++)   vpdata_[i]=NULL;
 | 
|---|
| 38 |   SelectPairs();
 | 
|---|
| 39 | 
 | 
|---|
| 40 |   //  visdt_.AddFloatColumn("mfc");
 | 
|---|
| 41 |   visdt_.AddDoubleColumn("mfc");
 | 
|---|
| 42 |   visdt_.AddDoubleColumn("mtt");
 | 
|---|
| 43 |   visdt_.AddIntegerColumn("jfreq");
 | 
|---|
| 44 |   visdt_.AddIntegerColumn("numch");
 | 
|---|
| 45 |   visdt_.AddFloatColumn("vre");
 | 
|---|
| 46 |   visdt_.AddFloatColumn("vim");
 | 
|---|
| 47 | 
 | 
|---|
| 48 |   if (nmean_ < 1)  nmean_=memgr_.NbPaquets();
 | 
|---|
| 49 |   if (nmean_ < 1)  nmean_=1;
 | 
|---|
| 50 | 
 | 
|---|
| 51 |   cout << " BRVisibilityCalculator::/Info  nmean=" << nmean_ << endl;
 | 
|---|
| 52 | 
 | 
|---|
| 53 |   totnbpaq_=0;
 | 
|---|
| 54 |   numfile_=0;
 | 
|---|
| 55 |   nb_flop_=0.;
 | 
|---|
| 56 |   moyfc_=moytt_=0.;
 | 
|---|
| 57 | 
 | 
|---|
| 58 |   fgallfibok=NULL;
 | 
|---|
| 59 |   fgcktt_=false;
 | 
|---|
| 60 |   setNameId("viscalc", 0); 
 | 
|---|
| 61 | }
 | 
|---|
| 62 | 
 | 
|---|
| 63 | /* --Methode-- */
 | 
|---|
| 64 | BRVisibilityCalculator::~BRVisibilityCalculator()
 | 
|---|
| 65 | {
 | 
|---|
| 66 |   if (totnbpaq_<1)   return; 
 | 
|---|
| 67 |   cout << " ~BRVisibilityCalculator - Visibility Datatable : " << endl;
 | 
|---|
| 68 |   cout << visdt_;
 | 
|---|
| 69 |   
 | 
|---|
| 70 |   string filename;
 | 
|---|
| 71 |   filename = outpath_+"visdt.ppf";
 | 
|---|
| 72 |   if (nbcalc_>1) {
 | 
|---|
| 73 |     char sbuff[32];
 | 
|---|
| 74 |     sprintf(sbuff,"visdt_%d.ppf",(int)calcid_);
 | 
|---|
| 75 |     filename = outpath_+sbuff;
 | 
|---|
| 76 |   }
 | 
|---|
| 77 |   POutPersist po(filename);
 | 
|---|
| 78 |   po << visdt_;
 | 
|---|
| 79 |   if (calcid_ == 0) {
 | 
|---|
| 80 |     
 | 
|---|
| 81 |     if (fgcktt_) {
 | 
|---|
| 82 |       cout << " BRVisibilityCalculator -  Check TimeTag done: TotNPaqProc= " << totnbpaq_ << endl;
 | 
|---|
| 83 |       for(size_t fib=0; fib<(size_t)memgr_.NbFibres(); fib++)  {
 | 
|---|
| 84 |         cout << " BRTTCheck-Fiber[" << fib << "] NBadTT=" << vbadtt_[fib] << " NDiffTT>5="
 | 
|---|
| 85 |              << vndiff5tt_[fib] << "  NotSameTT=" << vnsamett_[fib] << endl; 
 | 
|---|
| 86 |       }
 | 
|---|
| 87 |       POutPersist pott(outpath_+"ttfcmtx.ppf");
 | 
|---|
| 88 |       pott << PPFNameTag("FC") << fcmtx_; 
 | 
|---|
| 89 |       pott << PPFNameTag("TT") << ttmtx_;     
 | 
|---|
| 90 |     }
 | 
|---|
| 91 |   }
 | 
|---|
| 92 | }
 | 
|---|
| 93 | 
 | 
|---|
| 94 | /* --Methode-- */
 | 
|---|
| 95 | void BRVisibilityCalculator::DefineRank(uint_4 nbc, uint_4 cid)
 | 
|---|
| 96 | {
 | 
|---|
| 97 |   if ((nbc>6)||(cid>=nbc)) 
 | 
|---|
| 98 |     throw ParmError("BRVisibilityCalculator::DefineRank() NbCalc > 6 !");
 | 
|---|
| 99 |   nbcalc_=nbc;
 | 
|---|
| 100 |   calcid_=cid;
 | 
|---|
| 101 |   if (nbcalc_>1) {
 | 
|---|
| 102 |     uint_4 maxnpairs = (2*memgr_.NbFibres()+1)*memgr_.NbFibres();
 | 
|---|
| 103 |     uint_4 npairs=maxnpairs/nbcalc_;
 | 
|---|
| 104 |     if (calcid_==(nbcalc_-1)) 
 | 
|---|
| 105 |       SelectPairs(calcid_*npairs, maxnpairs-calcid_*npairs);
 | 
|---|
| 106 |     else 
 | 
|---|
| 107 |       SelectPairs(calcid_*npairs, npairs);
 | 
|---|
| 108 |     MemZaction mmzas[6]={MemZA_ProcA,MemZA_ProcB,MemZA_ProcC,MemZA_ProcD,MemZA_ProcE,MemZA_ProcF};
 | 
|---|
| 109 |     SetMemZAction(mmzas[calcid_]);
 | 
|---|
| 110 |     setNameId("viscalc_grp", calcid_); 
 | 
|---|
| 111 |   }
 | 
|---|
| 112 |   return ;
 | 
|---|
| 113 | }
 | 
|---|
| 114 | 
 | 
|---|
| 115 | /* --Methode-- */
 | 
|---|
| 116 | uint_4 BRVisibilityCalculator::SelectPairs(uint_4 pair1, uint_4 nbpairs)
 | 
|---|
| 117 | {
 | 
|---|
| 118 |   BRPaquet paq(memgr_.PaqSize());
 | 
|---|
| 119 |   uint_4 maxnpairs = (2*memgr_.NbFibres()+1)*memgr_.NbFibres();
 | 
|---|
| 120 | 
 | 
|---|
| 121 |   if (pair1 >= maxnpairs)  pair1=maxnpairs-1;
 | 
|---|
| 122 |   if (nbpairs > maxnpairs-pair1)  nbpairs=maxnpairs-pair1;
 | 
|---|
| 123 |   pairst_=pair1;
 | 
|---|
| 124 |   nbpairs_=nbpairs;
 | 
|---|
| 125 |   vismtx_.SetSize(nbpairs_, paq.DataSize()/4); 
 | 
|---|
| 126 | 
 | 
|---|
| 127 |   chanpairnum_.SetSize(nbpairs_);
 | 
|---|
| 128 |   chanpairs_.SetSize(nbpairs_,2);
 | 
|---|
| 129 | 
 | 
|---|
| 130 |   return nbpairs_;
 | 
|---|
| 131 | }
 | 
|---|
| 132 | 
 | 
|---|
| 133 | /* --Methode-- */
 | 
|---|
| 134 | int BRVisibilityCalculator::SelectFreqBinning(uint_4 freq1, uint_4 freq2, uint_4 nbfreq)
 | 
|---|
| 135 | {
 | 
|---|
| 136 |   jf1_=freq1; jf2_=freq2;
 | 
|---|
| 137 |   if ((jf1_<1)||(jf1_>=vismtx_.NCols()))  jf1_=1;
 | 
|---|
| 138 |   if ((jf2_<1)||(jf2_>=vismtx_.NCols())||(jf2_<jf1_))  jf2_=vismtx_.NCols()-1;
 | 
|---|
| 139 |   if (nbfreq<1) nbfreq=(jf2_-jf1_);
 | 
|---|
| 140 |   djf_=(jf2_-jf1_)/nbfreq;
 | 
|---|
| 141 |   if (djf_<1) djf_=1;
 | 
|---|
| 142 |   cout << " BRVisibilityCalculator::SelectFreqBinning/Info  JF1=" << jf1_ 
 | 
|---|
| 143 |        << " JF2=" << jf2_ << " DJF=" << djf_ << endl;
 | 
|---|
| 144 | 
 | 
|---|
| 145 | }
 | 
|---|
| 146 | 
 | 
|---|
| 147 | /* --Methode-- */
 | 
|---|
| 148 | void BRVisibilityCalculator::UpdateChanIds()
 | 
|---|
| 149 | {
 | 
|---|
| 150 | 
 | 
|---|
| 151 |   for(size_t i=0; i<memgr_.NbFibres(); i++) { 
 | 
|---|
| 152 |     chanids_(2*i)=memgr_.FiberId(i)*2-1; 
 | 
|---|
| 153 |     chanids_(2*i+1)=memgr_.FiberId(i)*2; 
 | 
|---|
| 154 |   } 
 | 
|---|
| 155 |   sa_size_t k=0;     // numero de ligne dans la matrice des visibilites 
 | 
|---|
| 156 |   for(size_t i=0; i<vpdata_.size(); i++) {
 | 
|---|
| 157 |     for(size_t j=i; j<vpdata_.size(); j++) {
 | 
|---|
| 158 |       chanpairnumall_(k)=chanids_(i)*CHANPAIRCONVFAC+chanids_(j);
 | 
|---|
| 159 |       chanpairsall_(k,0)=chanids_(i);  chanpairsall_(k,1)=chanids_(j);     k++;
 | 
|---|
| 160 |     }
 | 
|---|
| 161 |   }
 | 
|---|
| 162 |   sa_size_t kpair=0;  
 | 
|---|
| 163 |   k=0;  // numero de ligne dans la matrice des visibilites 
 | 
|---|
| 164 |   for(size_t i=0; i<vpdata_.size(); i++) {
 | 
|---|
| 165 |     for(size_t j=i; j<vpdata_.size(); j++) {
 | 
|---|
| 166 |       kpair++;
 | 
|---|
| 167 |       if (kpair<(pairst_+1))  continue;
 | 
|---|
| 168 |       if (kpair>=(pairst_+nbpairs_+1))  break;
 | 
|---|
| 169 |       chanpairnum_(k)=chanids_(i)*CHANPAIRCONVFAC+chanids_(j);
 | 
|---|
| 170 |       chanpairs_(k,0)=chanids_(i);  chanpairs_(k,1)=chanids_(j);     k++;
 | 
|---|
| 171 |     }
 | 
|---|
| 172 |   }
 | 
|---|
| 173 | 
 | 
|---|
| 174 |   string filename;
 | 
|---|
| 175 |   filename = outpath_+"chanum.ppf";
 | 
|---|
| 176 |   if (nbcalc_>1) {
 | 
|---|
| 177 |     char sbuff[32];
 | 
|---|
| 178 |     sprintf(sbuff,"chanum_%d.ppf",(int)calcid_);
 | 
|---|
| 179 |     filename = outpath_+sbuff;
 | 
|---|
| 180 |   }
 | 
|---|
| 181 |   POutPersist poc(outpath_+"chanum.ppf");
 | 
|---|
| 182 |   poc << PPFNameTag("chanids") << chanids_; 
 | 
|---|
| 183 |   poc << PPFNameTag("chanpairs") << chanpairs_; 
 | 
|---|
| 184 |   poc << PPFNameTag("chanpairnum") << chanpairnum_; 
 | 
|---|
| 185 |   poc << PPFNameTag("chanpairsall") << chanpairsall_; 
 | 
|---|
| 186 |   poc << PPFNameTag("chanpairnumal") << chanpairnumall_; 
 | 
|---|
| 187 |   cout << "BRVisibilityCalculator[" << calcid_ << "]::UpdateChanIds() Channel Ids/Pairs saved to PPF file " 
 | 
|---|
| 188 |        << filename << endl; 
 | 
|---|
| 189 |   cout << " ... ChannelPairs= " ;
 | 
|---|
| 190 |   for(sa_size_t ir=0; ir<chanpairs_.NRows(); ir++) {
 | 
|---|
| 191 |     if (ir%10)  cout << endl;
 | 
|---|
| 192 |     cout << "(" << chanpairs_(ir,0) << "," << chanpairs_(ir,1) << ") ";
 | 
|---|
| 193 |   }
 | 
|---|
| 194 |   cout << endl;
 | 
|---|
| 195 |   return;
 | 
|---|
| 196 | }
 | 
|---|
| 197 | 
 | 
|---|
| 198 | /* --Methode-- */
 | 
|---|
| 199 | int BRVisibilityCalculator::ActivateTimeTagCheck(uint_8 maxnpaq)
 | 
|---|
| 200 | {
 | 
|---|
| 201 |   mindeltatt_=memgr_.PaqSize()/2;
 | 
|---|
| 202 |   if (mindeltatt_<1) mindeltatt_=1;
 | 
|---|
| 203 |   fcmtx_.SetSize(memgr_.NbFibres(), maxnpaq);
 | 
|---|
| 204 |   ttmtx_.SetSize(memgr_.NbFibres(), maxnpaq);
 | 
|---|
| 205 |   vlasttt_.resize(memgr_.NbFibres(), 0);
 | 
|---|
| 206 |   vbadtt_.resize(memgr_.NbFibres(), 0);
 | 
|---|
| 207 |   vnsamett_.resize(memgr_.NbFibres(), 0);
 | 
|---|
| 208 |   vndiff5tt_.resize(memgr_.NbFibres(), 0);
 | 
|---|
| 209 | 
 | 
|---|
| 210 |   fgcktt_=true;
 | 
|---|
| 211 |   cout << " BRVisibilityCalculator::ActivateTimeTagCheck() - TT/Fc matrix NCols=" << maxnpaq 
 | 
|---|
| 212 |        << " MinDeltaTT=" << mindeltatt_ << endl;
 | 
|---|
| 213 | 
 | 
|---|
| 214 |   return 0;
 | 
|---|
| 215 | }
 | 
|---|
| 216 | 
 | 
|---|
| 217 | 
 | 
|---|
| 218 | /* --Methode-- */
 | 
|---|
| 219 | void BRVisibilityCalculator::run()
 | 
|---|
| 220 | {
 | 
|---|
| 221 |   cout << " BRVisibilityCalculator[" << calcid_ << "/" << nbcalc_ 
 | 
|---|
| 222 |        << "]::run() - Starting " << " NFibers=" << memgr_.NbFibres() 
 | 
|---|
| 223 |        << " NChan=" << 2*memgr_.NbFibres() << " NPairs=" << nbpairs_ << " First:" << pairst_ << endl;   
 | 
|---|
| 224 |   
 | 
|---|
| 225 |   if (nparthr_ < 2)  return BRBaseProcessor::run();
 | 
|---|
| 226 |   // Execution multithread parallele
 | 
|---|
| 227 |   setRC(1);     
 | 
|---|
| 228 |   int rc=0;
 | 
|---|
| 229 |   try {
 | 
|---|
| 230 |     if ((nmean_%memgr_.NbPaquets())!=0) {
 | 
|---|
| 231 |       uint_4 mnmean = (nmean_/memgr_.NbPaquets()+1)*memgr_.NbPaquets();
 | 
|---|
| 232 |       cout << " BRVisibilityCalculator::run()/Info changing nmean=" << nmean_  << " to multiple of"
 | 
|---|
| 233 |            << " memgr_.NbPaquets() -> " << mnmean << endl;
 | 
|---|
| 234 |       nmean_=mnmean;
 | 
|---|
| 235 |     }
 | 
|---|
| 236 |     paralex_.SetParallelTask(*this);
 | 
|---|
| 237 |     cout << " BRVisibilityCalculator::run()/Info : starting ParallelExecutor with nThreads=" 
 | 
|---|
| 238 |          << paralex_.nThreads() << " ... " << endl;
 | 
|---|
| 239 |     paralex_.start();
 | 
|---|
| 240 | 
 | 
|---|
| 241 |     fgallfibok = new bool[memgr_.NbPaquets()];
 | 
|---|
| 242 | 
 | 
|---|
| 243 |     size_t paqsz=memgr_.PaqSize();
 | 
|---|
| 244 |     bool fgrun=true;
 | 
|---|
| 245 |     while (fgrun) {
 | 
|---|
| 246 |       if (stop_) break;
 | 
|---|
| 247 |       if (memgr_.GetRunState() == MemZR_Stopped) break;
 | 
|---|
| 248 |       int mid = memgr_.FindMemZoneId(mmact_);  // (MemZA_ProcA);
 | 
|---|
| 249 |       //      Byte* buffg = memgr_.GetMemZone(mid);
 | 
|---|
| 250 |       //      if (buffg == NULL) {
 | 
|---|
| 251 |       if (mid < 0) {
 | 
|---|
| 252 |         cout << "BRVisibilityCalculator[" << calcid_ << "]::run()/ERROR FindMemZoneId(" 
 | 
|---|
| 253 |              << (int)mmact_ << ") ->" << mid << ") -> NULL" << endl;
 | 
|---|
| 254 |         setRC(7);      fgrun=false;             
 | 
|---|
| 255 |         break;  
 | 
|---|
| 256 |       }
 | 
|---|
| 257 |       cts_=memgr_.GetAuxData(mid)->FillTime();   // get associated date/time (DATEOBS)
 | 
|---|
| 258 |       if (totnbpaq_==0) UpdateChanIds();  // Appele ici pour etre sur que le thread de remplissage a mis l'info a jour.
 | 
|---|
| 259 | 
 | 
|---|
| 260 |       for(size_t fib=0; fib<(size_t)memgr_.NbFibres(); fib++) {
 | 
|---|
| 261 |         fbuff_[fib] = memgr_.GetMemZone(mid,fib);
 | 
|---|
| 262 |         if (fbuff_[fib] == NULL) { // cela ne devrait pas arriver 
 | 
|---|
| 263 |           cout << "BRVisibilityCalculator[" << calcid_ << "]::run()/ERROR memgr.GetMemZone(" << mid << "," << fib << ") -> NULL" << endl;
 | 
|---|
| 264 |           setRC(9);       fgrun=false;          
 | 
|---|
| 265 |           break;
 | 
|---|
| 266 |         }
 | 
|---|
| 267 |       }
 | 
|---|
| 268 | 
 | 
|---|
| 269 |       if (totnbpaq_%nmean_ == 0) {
 | 
|---|
| 270 |         if (totnbpaq_ > 0) {
 | 
|---|
| 271 |           moyfc_/=nmean_;
 | 
|---|
| 272 |           moytt_/=nmean_;
 | 
|---|
| 273 |           UpdateVisMtxInfo();    // add/update keywords in the Info DVList 
 | 
|---|
| 274 |           // ATTENTION : Matrice visibilites non moyennee 
 | 
|---|
| 275 |           char nfile[48];
 | 
|---|
| 276 |           if (nbcalc_==1) 
 | 
|---|
| 277 |             sprintf(nfile,"vismtx%d.ppf",numfile_);
 | 
|---|
| 278 |           else 
 | 
|---|
| 279 |             sprintf(nfile,"vismtx_%d_%d.ppf",(int)calcid_,numfile_);
 | 
|---|
| 280 |           string flnm=outpath_+nfile;
 | 
|---|
| 281 |           POutPersist po(flnm);
 | 
|---|
| 282 |           po << vismtx_;
 | 
|---|
| 283 |           cout << numfile_ << "-BRVisCalc[" << calcid_ << "/" << nbcalc_ << "]::run() NPaqProc="
 | 
|---|
| 284 |                << totnbpaq_ << " TotMegaFLOP=" << (uint_8)TotNbMegaFLOP() << " -> VisibMtx in " << flnm << endl;
 | 
|---|
| 285 | 
 | 
|---|
| 286 |           FillVisibTable(moyfc_, moytt_);
 | 
|---|
| 287 |           numfile_++;
 | 
|---|
| 288 |         }
 | 
|---|
| 289 |         vismtx_ = complex<r_4>((r_4)0.,(r_4)0.);
 | 
|---|
| 290 |         moyfc_=moytt_=0.;
 | 
|---|
| 291 |         //      first_tmstamp_.SetNow();  // Current date and time 
 | 
|---|
| 292 |         first_tmstamp_=cts_;  // Current date and time 
 | 
|---|
| 293 |       }
 | 
|---|
| 294 | 
 | 
|---|
| 295 |       for(size_t jp=0; jp<memgr_.NbPaquets(); jp++) {   // boucle sur les paquets d'une zone  
 | 
|---|
| 296 |         fgallfibok[jp]=fgokallfibers_=true;
 | 
|---|
| 297 |         for(size_t fib=0; fib<(size_t)memgr_.NbFibres(); fib++) {
 | 
|---|
| 298 |           vpaq_[fib].Set(fbuff_[fib]+jp*paqsz);
 | 
|---|
| 299 |           vfgok_[fib] = vpchk_[fib].Check(vpaq_[fib],curfc_[fib]);
 | 
|---|
| 300 |           if (!vfgok_[fib])  fgallfibok[jp]=fgokallfibers_=false;
 | 
|---|
| 301 |         }
 | 
|---|
| 302 |         if (fgokallfibers_)  {
 | 
|---|
| 303 |           if (totprocnpaq_==0) {
 | 
|---|
| 304 |             for(size_t fib=0; fib<(size_t)memgr_.NbFibres(); fib++)  {
 | 
|---|
| 305 |               fcfirst_[fib]=curfc_[fib];
 | 
|---|
| 306 |               ttfirst_[fib]=vpaq_[fib].TimeTag();
 | 
|---|
| 307 |             }
 | 
|---|
| 308 |           }
 | 
|---|
| 309 |           totprocnpaq_++;
 | 
|---|
| 310 |           moyfc_ += curfc_[0];
 | 
|---|
| 311 |           moytt_ += (vpaq_[0].TimeTag()-ttfirst_[0]);
 | 
|---|
| 312 |           if ((fgcktt_)&&(calcid_==0))  CheckTimeTag();
 | 
|---|
| 313 |           if (totnbpaq_%nmean_ == 0) {
 | 
|---|
| 314 |             first_fc_=curfc_[0];
 | 
|---|
| 315 |             first_tt_= (vpaq_[0].TimeTag()-ttfirst_[0]);
 | 
|---|
| 316 |           }
 | 
|---|
| 317 |           totnbpaq_++;
 | 
|---|
| 318 |         }
 | 
|---|
| 319 |       }  // Fin de boucle sur les paquets 
 | 
|---|
| 320 | 
 | 
|---|
| 321 |       // Execution parallele  pour calcul des visibilites par bandes de frequence
 | 
|---|
| 322 |       int rcpex=paralex_.execute();
 | 
|---|
| 323 |       if (rcpex!=0)  cout << " BRVisibilityCalculator[" << calcid_ << "]::run() / Error Rc[paralex_.execute()]=" << rcpex << endl;
 | 
|---|
| 324 | 
 | 
|---|
| 325 |       memgr_.FreeMemZone(mid, mmsta_);  // (MemZS_ProcA);
 | 
|---|
| 326 |     } // Fin de boucle sur les zones a traiter 
 | 
|---|
| 327 |     //------------------------------------
 | 
|---|
| 328 |     cout << " --------- END BRVisibilityCalculator[" << calcid_ << "]::run() , TotNbProcPaq=" << totprocnpaq_ << endl;
 | 
|---|
| 329 |     /*
 | 
|---|
| 330 |     for(size_t fib=0; fib<(size_t)memgr_.NbFibres(); fib++)  vpchk_[fib].Print();
 | 
|---|
| 331 |     cout << " ------------------------------------ " << endl;
 | 
|---|
| 332 |     */
 | 
|---|
| 333 |     delete[] fgallfibok;
 | 
|---|
| 334 |   }
 | 
|---|
| 335 |   catch (std::exception& exc) {
 | 
|---|
| 336 |     cout << " BRVisibilityCalculator[" << calcid_ << "]::run()/catched std::exception " << exc.what() << endl;
 | 
|---|
| 337 |     setRC(98);  
 | 
|---|
| 338 |     return; 
 | 
|---|
| 339 |   }
 | 
|---|
| 340 |   catch(...) {
 | 
|---|
| 341 |     cout << " BRVisibilityCalculator[" << calcid_ << "]::run()/catched unknown ... exception " << endl;
 | 
|---|
| 342 |     setRC(99);  
 | 
|---|
| 343 |     return; 
 | 
|---|
| 344 |   }
 | 
|---|
| 345 |   
 | 
|---|
| 346 | }
 | 
|---|
| 347 | 
 | 
|---|
| 348 | /* --Methode-- */
 | 
|---|
| 349 | int BRVisibilityCalculator::Process()
 | 
|---|
| 350 | {
 | 
|---|
| 351 |   if (totnbpaq_==0) UpdateChanIds();  // Appele ici pour etre sur que le thread de remplissage a mis l'info a jour.
 | 
|---|
| 352 |   for(size_t fib=0; fib<(size_t)memgr_.NbFibres(); fib++) {
 | 
|---|
| 353 |     vpdata_[2*fib] = vpaq_[fib].Data1C();
 | 
|---|
| 354 |     vpdata_[2*fib+1] = vpaq_[fib].Data2C();
 | 
|---|
| 355 |   }
 | 
|---|
| 356 |   
 | 
|---|
| 357 |   if (totnbpaq_%nmean_ == 0) {
 | 
|---|
| 358 |     if (totnbpaq_ > 0) {
 | 
|---|
| 359 |       moyfc_/=nmean_;
 | 
|---|
| 360 |       moytt_/=nmean_;
 | 
|---|
| 361 |       UpdateVisMtxInfo();    // add/update keywords in the Info DVList 
 | 
|---|
| 362 |       // ATTENTION : Matrice visibilites non moyennee 
 | 
|---|
| 363 |       char nfile[48];
 | 
|---|
| 364 |       if (nbcalc_==1) 
 | 
|---|
| 365 |         sprintf(nfile,"vismtx%d.ppf",numfile_);
 | 
|---|
| 366 |       else 
 | 
|---|
| 367 |         sprintf(nfile,"vismtx_%d_%d.ppf",(int)calcid_,numfile_);
 | 
|---|
| 368 |       string flnm=outpath_+nfile;
 | 
|---|
| 369 |       POutPersist po(flnm);
 | 
|---|
| 370 |       po << vismtx_;
 | 
|---|
| 371 |       cout << numfile_ << "-BRVisCalc[" << calcid_ << "/" << nbcalc_ << "]::Process() NPaqProc="
 | 
|---|
| 372 |            << totnbpaq_ << " TotMegaFLOP=" << (uint_8)TotNbMegaFLOP() << " -> VisibMtx in " << flnm << endl;
 | 
|---|
| 373 |       FillVisibTable(moyfc_, moytt_);
 | 
|---|
| 374 |       numfile_++;
 | 
|---|
| 375 |     }
 | 
|---|
| 376 |     vismtx_ = complex<r_4>((r_4)0.,(r_4)0.);
 | 
|---|
| 377 |     moyfc_=moytt_=0.;
 | 
|---|
| 378 |     first_fc_=curfc_[0];
 | 
|---|
| 379 |     first_tt_= (vpaq_[0].TimeTag()-ttfirst_[0]);
 | 
|---|
| 380 |     // first_tmstamp_.SetNow();  // Current date and time 
 | 
|---|
| 381 |     first_tmstamp_=cts_;  // Current date and time 
 | 
|---|
| 382 |   }
 | 
|---|
| 383 | 
 | 
|---|
| 384 | // kpair=numero sequentiel de la paire: 0->(0,0), 1->(0,1), 2->(0,2), 3->(0,3), 4->(1,1), 5->(1,2) ...
 | 
|---|
| 385 |   sa_size_t kpair=0;
 | 
|---|
| 386 |   sa_size_t k=0;     // numero de ligne dans la matrice des visibilites 
 | 
|---|
| 387 |   for(size_t i=0; i<vpdata_.size(); i++) {
 | 
|---|
| 388 |     for(size_t j=i; j<vpdata_.size(); j++) {
 | 
|---|
| 389 |       kpair++;
 | 
|---|
| 390 |       if (kpair<(pairst_+1))  continue;
 | 
|---|
| 391 |       if (kpair>=(pairst_+nbpairs_+1))  break;
 | 
|---|
| 392 |       TVector< complex<r_4> > vis = vismtx_.Row(k);  k++;   
 | 
|---|
| 393 |       for(sa_size_t f=1; f<vis.Size(); f++) {
 | 
|---|
| 394 |         vis(f) += complex<r_4>((r_4)vpdata_[i][f].realB(), (r_4)vpdata_[i][f].imagB()) * 
 | 
|---|
| 395 |           complex<r_4>((r_4)vpdata_[j][f].realB(), -(r_4)vpdata_[j][f].imagB());
 | 
|---|
| 396 |       }
 | 
|---|
| 397 |       nb_flop_ += (8.*(r_8)(vis.Size()-1));
 | 
|---|
| 398 |     }
 | 
|---|
| 399 |   }
 | 
|---|
| 400 | 
 | 
|---|
| 401 |   moyfc_ += curfc_[0];
 | 
|---|
| 402 |   moytt_ += (vpaq_[0].TimeTag()-ttfirst_[0]);
 | 
|---|
| 403 |   if ((fgcktt_)&&(calcid_==0))  CheckTimeTag();
 | 
|---|
| 404 |   totnbpaq_++;
 | 
|---|
| 405 |   return 0;
 | 
|---|
| 406 | }
 | 
|---|
| 407 | 
 | 
|---|
| 408 | /* --Methode-- */
 | 
|---|
| 409 | void BRVisibilityCalculator::UpdateVisMtxInfo()
 | 
|---|
| 410 | {
 | 
|---|
| 411 |   string ikey,ikdesc;
 | 
|---|
| 412 |   ikey="DATEOBS";  ikdesc=" Date/Time corresponding to TimeTagFirst";
 | 
|---|
| 413 |   vismtx_.Info().SetS(ikey,first_tmstamp_.ToString());
 | 
|---|
| 414 |   vismtx_.Info().SetComment(ikey,ikdesc);
 | 
|---|
| 415 |   ikey="FirstFC";  ikdesc="First FrameCounter";
 | 
|---|
| 416 |   vismtx_.Info().SetI(ikey,first_fc_);
 | 
|---|
| 417 |   vismtx_.Info().SetComment(ikey,ikdesc);
 | 
|---|
| 418 |   ikey="FirstTT";  ikdesc="First TimeTag";
 | 
|---|
| 419 |   vismtx_.Info().SetI(ikey,first_tt_);
 | 
|---|
| 420 |   vismtx_.Info().SetComment(ikey,ikdesc);
 | 
|---|
| 421 |   ikey="LastFC";  ikdesc="Last FrameCounter";
 | 
|---|
| 422 |   vismtx_.Info().SetI(ikey,curfc_[0]);
 | 
|---|
| 423 |   vismtx_.Info().SetComment(ikey,ikdesc);
 | 
|---|
| 424 |   ikey="LastTT";  ikdesc="Last TimeTag";
 | 
|---|
| 425 |   vismtx_.Info().SetI(ikey,vpaq_[0].TimeTag()-ttfirst_[0]);
 | 
|---|
| 426 |   vismtx_.Info().SetComment(ikey,ikdesc);
 | 
|---|
| 427 |   ikey="MeanFC";  ikdesc="Mean FrameCounter";
 | 
|---|
| 428 |   vismtx_.Info().SetD(ikey,moyfc_); 
 | 
|---|
| 429 |   vismtx_.Info().SetComment(ikey,ikdesc);
 | 
|---|
| 430 |   ikey="MeanTT";  ikdesc="Mean TimeTag";
 | 
|---|
| 431 |   vismtx_.Info().SetD(ikey,moytt_);
 | 
|---|
| 432 |   vismtx_.Info().SetComment(ikey,ikdesc);
 | 
|---|
| 433 |   ikey="NPAQSUM";  ikdesc="Number of paquets summed";
 | 
|---|
| 434 |   vismtx_.Info().SetI(ikey,nmean_);
 | 
|---|
| 435 | }
 | 
|---|
| 436 | 
 | 
|---|
| 437 | 
 | 
|---|
| 438 | /* --Methode-- */
 | 
|---|
| 439 | int BRVisibilityCalculator::execute(int tid)
 | 
|---|
| 440 | {
 | 
|---|
| 441 |   vector<TwoByteComplex*>  pvpdata(2*memgr_.NbFibres());
 | 
|---|
| 442 |   size_t paqsz=memgr_.PaqSize();
 | 
|---|
| 443 |   BRPaquet ppaq(paqsz);
 | 
|---|
| 444 | 
 | 
|---|
| 445 | 
 | 
|---|
| 446 |   sa_size_t fdelt = vismtx_.NCols()/nparthr_;
 | 
|---|
| 447 |   sa_size_t fdeb = tid*fdelt;
 | 
|---|
| 448 |   sa_size_t ffin = (tid+1)*fdelt;
 | 
|---|
| 449 | 
 | 
|---|
| 450 |   if (fdeb<1) fdeb=1;
 | 
|---|
| 451 |   if ((ffin>vismtx_.NCols())||(tid==(nparthr_-1))) ffin=vismtx_.NCols();
 | 
|---|
| 452 | 
 | 
|---|
| 453 |   for(size_t jp=0; jp<memgr_.NbPaquets(); jp++) {   // boucle sur les paquets d'une zone  
 | 
|---|
| 454 |     if (!fgallfibok[jp])  continue;
 | 
|---|
| 455 |     for(size_t fib=0; fib<(size_t)memgr_.NbFibres(); fib++) {
 | 
|---|
| 456 |       ppaq.Set(fbuff_[fib]+jp*paqsz);
 | 
|---|
| 457 |       pvpdata[2*fib] = ppaq.Data1C();
 | 
|---|
| 458 |       pvpdata[2*fib+1] = ppaq.Data2C();
 | 
|---|
| 459 |     }
 | 
|---|
| 460 | 
 | 
|---|
| 461 | 
 | 
|---|
| 462 | // kpair=numero sequentiel de la paire: 0->(0,0), 1->(0,1), 2->(0,2), 3->(0,3), 4->(1,1), 5->(1,2) ...
 | 
|---|
| 463 |     sa_size_t kpair=0;
 | 
|---|
| 464 |     sa_size_t k=0;     // numero de ligne dans la matrice des visibilites 
 | 
|---|
| 465 |     for(size_t i=0; i<vpdata_.size(); i++) {
 | 
|---|
| 466 |       for(size_t j=i; j<vpdata_.size(); j++) {
 | 
|---|
| 467 |         kpair++;
 | 
|---|
| 468 |         if (kpair<(pairst_+1))  continue;
 | 
|---|
| 469 |         if (kpair>=(pairst_+nbpairs_+1))  break;
 | 
|---|
| 470 |         TVector< complex<r_4> > vis = vismtx_.Row(k);  k++;  
 | 
|---|
| 471 |         for(sa_size_t f=fdeb; f<ffin; f++) {
 | 
|---|
| 472 |           vis(f) += complex<r_4>((r_4)pvpdata[i][f].realB(), (r_4)pvpdata[i][f].imagB()) * 
 | 
|---|
| 473 |             complex<r_4>((r_4)pvpdata[j][f].realB(), -(r_4)pvpdata[j][f].imagB());
 | 
|---|
| 474 |         }
 | 
|---|
| 475 |         nb_flop_ += (8.*(r_8)(ffin-fdeb));
 | 
|---|
| 476 |       }
 | 
|---|
| 477 |     }
 | 
|---|
| 478 | 
 | 
|---|
| 479 |   }  // Fin de boucle sur les paquets 
 | 
|---|
| 480 |   
 | 
|---|
| 481 |   return 0;
 | 
|---|
| 482 | }
 | 
|---|
| 483 | 
 | 
|---|
| 484 | /* --Methode-- */
 | 
|---|
| 485 | int BRVisibilityCalculator::FillVisibTable(double fcm, double ttm)
 | 
|---|
| 486 | {
 | 
|---|
| 487 |   double xnt[10];
 | 
|---|
| 488 |   xnt[0]=fcm;  xnt[1]=ttm/1.25e8;
 | 
|---|
| 489 | 
 | 
|---|
| 490 |   if (djf_<2) {
 | 
|---|
| 491 |     for(sa_size_t rv=0; rv<vismtx_.NRows(); rv++) {
 | 
|---|
| 492 |       for(sa_size_t jf=jf1_; jf<jf2_; jf++) {
 | 
|---|
| 493 |         xnt[2]=jf;
 | 
|---|
| 494 |         xnt[3]=chanpairnumall_(rv+pairst_);
 | 
|---|
| 495 |         xnt[4]=vismtx_(rv,jf).real()/(r_4)(nmean_);
 | 
|---|
| 496 |         xnt[5]=vismtx_(rv,jf).imag()/(r_4)(nmean_);
 | 
|---|
| 497 |         visdt_.AddRow(xnt);
 | 
|---|
| 498 |       }
 | 
|---|
| 499 |     }
 | 
|---|
| 500 |   }
 | 
|---|
| 501 |   else {
 | 
|---|
| 502 |     for(sa_size_t rv=0; rv<vismtx_.NRows(); rv++) {
 | 
|---|
| 503 |       for(sa_size_t jf=jf1_; jf<jf2_; jf+=djf_) {
 | 
|---|
| 504 |         r_4 moyreal=0.;
 | 
|---|
| 505 |         r_4 moyimag=0.;
 | 
|---|
| 506 |         sa_size_t jjfmx=jf+djf_;
 | 
|---|
| 507 |         if (jjfmx > vismtx_.NCols()) jjfmx=vismtx_.NCols();
 | 
|---|
| 508 |         for(sa_size_t jjf=jf; jjf<jjfmx; jjf++) {
 | 
|---|
| 509 |           moyreal+=vismtx_(rv,jjf).real();
 | 
|---|
| 510 |           moyimag+=vismtx_(rv,jjf).imag();
 | 
|---|
| 511 |         }
 | 
|---|
| 512 |         xnt[2]=jf+djf_/2;
 | 
|---|
| 513 |         xnt[3]=chanpairnumall_(rv+pairst_);
 | 
|---|
| 514 |         xnt[4]=moyreal/(r_4)(nmean_*djf_);
 | 
|---|
| 515 |         xnt[5]=moyimag/(r_4)(nmean_*djf_);
 | 
|---|
| 516 |         visdt_.AddRow(xnt);
 | 
|---|
| 517 |       }
 | 
|---|
| 518 |     }
 | 
|---|
| 519 |   }
 | 
|---|
| 520 |   return 0;
 | 
|---|
| 521 | }
 | 
|---|
| 522 | 
 | 
|---|
| 523 | /* --Methode-- */
 | 
|---|
| 524 | int BRVisibilityCalculator::CheckTimeTag()
 | 
|---|
| 525 | {
 | 
|---|
| 526 |   if (totnbpaq_==0) {
 | 
|---|
| 527 |     for(size_t fib=0; fib<(size_t)memgr_.NbFibres(); fib++)  {
 | 
|---|
| 528 |       vlasttt_[fib]=ttfirst_[fib];
 | 
|---|
| 529 |       if (ttmtx_.NCols()>0) {
 | 
|---|
| 530 |         fcmtx_(fib,totnbpaq_) = curfc_[fib]; 
 | 
|---|
| 531 |         ttmtx_(fib,totnbpaq_) = vlasttt_[fib]; 
 | 
|---|
| 532 |       }
 | 
|---|
| 533 |     }
 | 
|---|
| 534 |     return 0;
 | 
|---|
| 535 |   }
 | 
|---|
| 536 |   for(size_t fib=0; fib<(size_t)memgr_.NbFibres(); fib++) {
 | 
|---|
| 537 |     int_8 ld = (int_8)vpaq_[fib].TimeTag()-(int_8)vlasttt_[fib];
 | 
|---|
| 538 |     int_8 fd = (int_8)vpaq_[fib].TimeTag()-(int_8)ttfirst_[fib]-(int_8)vpaq_[0].TimeTag()+(int_8)ttfirst_[0];
 | 
|---|
| 539 |     /*    if ( (ld < mindeltatt_) || (fd<-5) || (fd>5)) { vbadtt_[fib]++; vnsamett_[fib]++; }
 | 
|---|
| 540 |     else {
 | 
|---|
| 541 |       if (fd!=0)  vnsamett_[fib]++;
 | 
|---|
| 542 |     }
 | 
|---|
| 543 |     */
 | 
|---|
| 544 |     if  (ld < mindeltatt_) vbadtt_[fib]++;
 | 
|---|
| 545 |     else {
 | 
|---|
| 546 |       if (fd != 0)  vnsamett_[fib]++;
 | 
|---|
| 547 |       if ((fd<-5)||(fd>5))  vndiff5tt_[fib]++;
 | 
|---|
| 548 |     }
 | 
|---|
| 549 |     vlasttt_[fib]=vpaq_[fib].TimeTag();
 | 
|---|
| 550 |     if (totnbpaq_<ttmtx_.NCols()) {
 | 
|---|
| 551 |       fcmtx_(fib,totnbpaq_) = curfc_[fib]; 
 | 
|---|
| 552 |       ttmtx_(fib,totnbpaq_) = vlasttt_[fib]; 
 | 
|---|
| 553 |     }
 | 
|---|
| 554 |   }
 | 
|---|
| 555 |   return 0;
 | 
|---|
| 556 | }
 | 
|---|
| 557 | 
 | 
|---|
| 558 | //-------------------------------------------------------------------------------
 | 
|---|
| 559 | // Classe Groupe (ensemble) de Calculateur de Visibilites, tournant en parallele
 | 
|---|
| 560 | //-------------------------------------------------------------------------------
 | 
|---|
| 561 | 
 | 
|---|
| 562 | /* --Methode-- */
 | 
|---|
| 563 | BRVisCalcGroup::BRVisCalcGroup(size_t nbcalc, RAcqMemZoneMgr& memgr, string outpath, uint_4 nmean, size_t nthr)
 | 
|---|
| 564 |   : tm_(false)
 | 
|---|
| 565 | {
 | 
|---|
| 566 |   if ((nbcalc<1)||(nbcalc>6)) 
 | 
|---|
| 567 |     throw ParmError("BRVisCalcGroup::BRVisCalcGroup NbCalc > 6 !");
 | 
|---|
| 568 |   for(size_t i=0; i<nbcalc; i++) {
 | 
|---|
| 569 |     BRVisibilityCalculator * viscp=new BRVisibilityCalculator(memgr, outpath, nmean, nthr);
 | 
|---|
| 570 |     viscp->DefineRank(nbcalc, i);
 | 
|---|
| 571 |     viscalcp_.push_back(viscp); 
 | 
|---|
| 572 |   }
 | 
|---|
| 573 | }
 | 
|---|
| 574 | /* --Methode-- */
 | 
|---|
| 575 | BRVisCalcGroup::~BRVisCalcGroup()
 | 
|---|
| 576 | {
 | 
|---|
| 577 |   for(size_t i=0; i<viscalcp_.size(); i++) 
 | 
|---|
| 578 |     delete viscalcp_[i];
 | 
|---|
| 579 | }
 | 
|---|
| 580 | /* --Methode-- */
 | 
|---|
| 581 | int BRVisCalcGroup::SelectFreqBinning(uint_4 freq1, uint_4 freq2, uint_4 nbfreq)
 | 
|---|
| 582 | {
 | 
|---|
| 583 |   int rc=0;
 | 
|---|
| 584 |   for(size_t i=0; i<viscalcp_.size(); i++) 
 | 
|---|
| 585 |     rc=viscalcp_[i]->SelectFreqBinning(freq1, freq2, nbfreq);
 | 
|---|
| 586 |   return rc;
 | 
|---|
| 587 | }
 | 
|---|
| 588 | /* --Methode-- */
 | 
|---|
| 589 | void BRVisCalcGroup::start()
 | 
|---|
| 590 | {
 | 
|---|
| 591 |   for(size_t i=0; i<viscalcp_.size(); i++) 
 | 
|---|
| 592 |     viscalcp_[i]->start();
 | 
|---|
| 593 |   tm_.SplitQ();
 | 
|---|
| 594 | }
 | 
|---|
| 595 | /* --Methode-- */
 | 
|---|
| 596 | void BRVisCalcGroup::join()
 | 
|---|
| 597 | {
 | 
|---|
| 598 |   r_8 totflop=0.;
 | 
|---|
| 599 |   for(size_t i=0; i<viscalcp_.size(); i++) {
 | 
|---|
| 600 |     viscalcp_[i]->join();
 | 
|---|
| 601 |     //    cout << " BRVisCalcGroup::join()/ VisibCalc[" << i << "]->TotNbMegaFLOP()=" 
 | 
|---|
| 602 |     //   << viscalcp_[i]->TotNbFLOP()/1024e3 << endl;
 | 
|---|
| 603 |     totflop += viscalcp_[i]->TotNbFLOP();
 | 
|---|
| 604 |   }
 | 
|---|
| 605 |   tm_.SplitQ();
 | 
|---|
| 606 |   cout << "      ----------------------------------------------------------" << endl;
 | 
|---|
| 607 |   cout << " BRVisCalcGroup::join() : Finished " << viscalcp_.size() << " VisibilityCalculator(s)" << endl;
 | 
|---|
| 608 |   cout << " ... Elaspsed time: " << tm_.PartialElapsedTimems() 
 | 
|---|
| 609 |        << " ms (total:" << tm_.TotalElapsedTimems() << ")"  << endl;
 | 
|---|
| 610 |   double mflopsrate=totflop/(r_8)tm_.PartialElapsedTimems()/(1024.);
 | 
|---|
| 611 |   cout << " ... TotalMegaFLOP= " << totflop/(1024.e3) << " @ " 
 | 
|---|
| 612 |        << mflopsrate << " MFLOP/s" << " (=" << mflopsrate/(r_8)viscalcp_.size() << "/VisibCalcObject)" << endl; 
 | 
|---|
| 613 |   cout << "      ----------------------------------------------------------" << endl;
 | 
|---|
| 614 |   return;
 | 
|---|
| 615 | }
 | 
|---|