| 1 | // ArchTOIPipe           (C)     CEA/DAPNIA/SPP IN2P3/LAL
 | 
|---|
| 2 | //                               Eric Aubourg
 | 
|---|
| 3 | //                               Christophe Magneville
 | 
|---|
| 4 | //                               Reza Ansari
 | 
|---|
| 5 | // $Id: toimanager.cc,v 1.20 2003-02-24 14:14:51 cecile Exp $
 | 
|---|
| 6 | 
 | 
|---|
| 7 | #include "toimanager.h"
 | 
|---|
| 8 | #include <limits.h>
 | 
|---|
| 9 | #include <pthread.h>
 | 
|---|
| 10 | #include <iostream.h>
 | 
|---|
| 11 | #include <unistd.h>
 | 
|---|
| 12 | #include <map>
 | 
|---|
| 13 | 
 | 
|---|
| 14 | #ifndef MAXINT
 | 
|---|
| 15 | #define MAXINT 2147483647
 | 
|---|
| 16 | #endif
 | 
|---|
| 17 | 
 | 
|---|
| 18 | TOIManager::TOIManager() {
 | 
|---|
| 19 |   reqBegin = 0;
 | 
|---|
| 20 |   reqEnd = MAXINT;
 | 
|---|
| 21 | 
 | 
|---|
| 22 |   // -----------ajout cgt vf 19/08/2002
 | 
|---|
| 23 |   // par defaut TOISegmented
 | 
|---|
| 24 |   selectTOISegmented(1024, 20);
 | 
|---|
| 25 |   // ----------- fin ajout cgt
 | 
|---|
| 26 | 
 | 
|---|
| 27 | }
 | 
|---|
| 28 | 
 | 
|---|
| 29 | TOIManager* TOIManager::instance = NULL;
 | 
|---|
| 30 | 
 | 
|---|
| 31 | TOIManager* TOIManager::getManager() {
 | 
|---|
| 32 |   if (instance == NULL) instance =  new TOIManager();
 | 
|---|
| 33 |   return instance;
 | 
|---|
| 34 | }
 | 
|---|
| 35 | 
 | 
|---|
| 36 | // ajout vf 26/07/2002
 | 
|---|
| 37 | 
 | 
|---|
| 38 | // enregistrement d'un processeur dans la liste des processeurs pour une execution en groupe
 | 
|---|
| 39 | 
 | 
|---|
| 40 | void TOIManager::registerProcessor(TOIProcessor* proc) {
 | 
|---|
| 41 |   
 | 
|---|
| 42 |   cout << "Adding processor to TOIManager for group execution" << endl;
 | 
|---|
| 43 |   processors.push_back(proc);
 | 
|---|
| 44 |   
 | 
|---|
| 45 | }
 | 
|---|
| 46 | 
 | 
|---|
| 47 | 
 | 
|---|
| 48 | // demarrage de tous les processeurs et verification auto des samplenum pour chaque processeur parametre
 | 
|---|
| 49 | 
 | 
|---|
| 50 | void TOIManager::startAll() {
 | 
|---|
| 51 |   // verification des samplenum
 | 
|---|
| 52 |   bool samples_ok=checkSamplesLimits(1);
 | 
|---|
| 53 |   if (samples_ok) {
 | 
|---|
| 54 |     cout << "All limits ok" << endl << "Starting processors" << endl;
 | 
|---|
| 55 |   } else {
 | 
|---|
| 56 |     cout << "One or more limits ajusted for execution" << endl << "Starting processors" << endl;
 | 
|---|
| 57 |   }
 | 
|---|
| 58 | 
 | 
|---|
| 59 |   // mise a jour des limites apres verification
 | 
|---|
| 60 |   checkSamplesLimits(2);
 | 
|---|
| 61 |   checkSamplesLimits(3);
 | 
|---|
| 62 |   cout<<"Fin checks"<<endl;
 | 
|---|
| 63 |   
 | 
|---|
| 64 |    
 | 
|---|
| 65 |   // debogage affichage des limites apres calcul
 | 
|---|
| 66 |   {for (vector<TOIProcessor*>::iterator i = processors.begin();
 | 
|---|
| 67 |        i != processors.end(); i++) {
 | 
|---|
| 68 |     TOIProcessor* proc = *i;
 | 
|---|
| 69 |     proc->printLimits();
 | 
|---|
| 70 |   }
 | 
|---|
| 71 |   }
 | 
|---|
| 72 | 
 | 
|---|
| 73 |   // demarrage
 | 
|---|
| 74 |   for (vector<TOIProcessor*>::iterator i = processors.begin();
 | 
|---|
| 75 |        i != processors.end(); i++) {
 | 
|---|
| 76 |     TOIProcessor* proc = *i;
 | 
|---|
| 77 |     cout << "**********************" <<  endl;
 | 
|---|
| 78 |     cout << "starting processor " <<  endl;
 | 
|---|
| 79 |     proc->start();
 | 
|---|
| 80 |     cout << "processor started " << endl;
 | 
|---|
| 81 |   }
 | 
|---|
| 82 |   cout << "**********************" << endl;
 | 
|---|
| 83 | }
 | 
|---|
| 84 | 
 | 
|---|
| 85 | bool TOIManager::checkSamplesLimits(int pass)
 | 
|---|
| 86 | {
 | 
|---|
| 87 |   bool processor_ok=true;
 | 
|---|
| 88 |   bool samples_ok=true;
 | 
|---|
| 89 |   for (vector<TOIProcessor*>::iterator i = processors.begin();
 | 
|---|
| 90 |        i != processors.end(); i++) {
 | 
|---|
| 91 |     TOIProcessor* proc = *i;
 | 
|---|
| 92 |     cout << "testing processor limits  " <<  endl;
 | 
|---|
| 93 |     // test du processeur
 | 
|---|
| 94 |     
 | 
|---|
| 95 |     // test seulement pour les processor cle
 | 
|---|
| 96 |     //if (proc->getRequested()) {
 | 
|---|
| 97 |       processor_ok = proc->checkSampleLimits(pass);
 | 
|---|
| 98 |     //}
 | 
|---|
| 99 |     
 | 
|---|
| 100 |     if (processor_ok) {
 | 
|---|
| 101 |       cout << "processor limits ok " << endl;
 | 
|---|
| 102 |     } else {
 | 
|---|
| 103 |       cout << "processor limits ajusted" << endl;
 | 
|---|
| 104 |       samples_ok = false;
 | 
|---|
| 105 |     }
 | 
|---|
| 106 |   }
 | 
|---|
| 107 |   return samples_ok;
 | 
|---|
| 108 | }
 | 
|---|
| 109 | 
 | 
|---|
| 110 | // fin ajout vf
 | 
|---|
| 111 | 
 | 
|---|
| 112 | void TOIManager::setRequestedSample(int begin, int end) {
 | 
|---|
| 113 |   reqBegin = begin;
 | 
|---|
| 114 |   reqEnd   = end;
 | 
|---|
| 115 | }
 | 
|---|
| 116 | 
 | 
|---|
| 117 | int TOIManager::getRequestedBegin() {
 | 
|---|
| 118 |   return reqBegin;
 | 
|---|
| 119 | }
 | 
|---|
| 120 | 
 | 
|---|
| 121 | int TOIManager::getRequestedEnd() {
 | 
|---|
| 122 |   return reqEnd;
 | 
|---|
| 123 | }
 | 
|---|
| 124 | 
 | 
|---|
| 125 | void TOIManager::addThread(pthread_t* t) {
 | 
|---|
| 126 |   //  cout << "adding thread  "  <<  t << endl;
 | 
|---|
| 127 |   threads.push_back(t); 
 | 
|---|
| 128 | }
 | 
|---|
| 129 | 
 | 
|---|
| 130 | void TOIManager::joinAll() {
 | 
|---|
| 131 |   waitForAll();
 | 
|---|
| 132 | }
 | 
|---|
| 133 | 
 | 
|---|
| 134 | void TOIManager::waitForAll() {
 | 
|---|
| 135 |   for (vector<pthread_t*>::iterator i = threads.begin();
 | 
|---|
| 136 |        i != threads.end(); i++) {
 | 
|---|
| 137 |     pthread_t* pth = *i;
 | 
|---|
| 138 |     cout << "joining thread  " << pth <<  endl;
 | 
|---|
| 139 |     pthread_join(*pth, NULL);
 | 
|---|
| 140 |     cout << "thread joined " << pth << endl;
 | 
|---|
| 141 |   }
 | 
|---|
| 142 | }
 | 
|---|
| 143 | 
 | 
|---|
| 144 | 
 | 
|---|
| 145 | // -----------ajout cgt vf 19/08/2002
 | 
|---|
| 146 | 
 | 
|---|
| 147 | 
 | 
|---|
| 148 | void TOIManager::selectTOISegmented(int bufsz, int maxseg)
 | 
|---|
| 149 | {
 | 
|---|
| 150 |   fgSegmented = true;
 | 
|---|
| 151 |   segBuffsz = bufsz;
 | 
|---|
| 152 |   segMaxseg = maxseg;
 | 
|---|
| 153 | }
 | 
|---|
| 154 | 
 | 
|---|
| 155 | void TOIManager::selectTOISeqBuffered(int wsz)
 | 
|---|
| 156 | {
 | 
|---|
| 157 |   fgSegmented = false;
 | 
|---|
| 158 |   segBuffsz = wsz;
 | 
|---|
| 159 | } 
 | 
|---|
| 160 | 
 | 
|---|
| 161 | // methode connect de cgt simplifiee et corrigee
 | 
|---|
| 162 | TOI& TOIManager::connect(TOIProcessor& prout, string out, 
 | 
|---|
| 163 |                   TOIProcessor& prin, string in, string nom, int wbsz, bool withFlag)
 | 
|---|
| 164 | {
 | 
|---|
| 165 |   TOI* toi;
 | 
|---|
| 166 |   if (nom.length() < 1) {
 | 
|---|
| 167 |     char buff[128];
 | 
|---|
| 168 |     sprintf(buff, "TOI%s_[%s-%s]", nom, in, out);
 | 
|---|
| 169 |     nom = buff;
 | 
|---|
| 170 |   }
 | 
|---|
| 171 |   if (wbsz < 16)   wbsz = segBuffsz;
 | 
|---|
| 172 |   
 | 
|---|
| 173 |   // ajout test pour eviter de creer 2 tois en sortie
 | 
|---|
| 174 |   if ((toi=prout.getOutToi(out)) == NULL) {
 | 
|---|
| 175 |     //cout << "toi cree" << endl;
 | 
|---|
| 176 |     if (fgSegmented) toi = new TOISegmented(nom, wbsz, segMaxseg);
 | 
|---|
| 177 |     else toi = new TOISeqBuffered(nom, wbsz);
 | 
|---|
| 178 |     // on ajoute le toi de sortie
 | 
|---|
| 179 |     prout.addOutput(out, toi);
 | 
|---|
| 180 |   } else {
 | 
|---|
| 181 |     //cout << "toi deja cree stop" << endl;
 | 
|---|
| 182 |   }
 | 
|---|
| 183 | 
 | 
|---|
| 184 |   if (withFlag) { // Si c'est un FITSTOIWriter
 | 
|---|
| 185 |     FITSTOIWriter* ftw = dynamic_cast< FITSTOIWriter* >(&prin);
 | 
|---|
| 186 |     if (ftw) ftw->addInput(in, toi, withFlag);
 | 
|---|
| 187 |     else prin.addInput(in, toi);
 | 
|---|
| 188 |   }
 | 
|---|
| 189 |   else prin.addInput(in, toi);
 | 
|---|
| 190 |   return(*toi);
 | 
|---|
| 191 | }
 | 
|---|
| 192 | 
 | 
|---|
| 193 | 
 | 
|---|
| 194 | TOI& TOIManager::connect(TOIProcessor& prout, const char* out, 
 | 
|---|
| 195 |                   TOIProcessor& prin, const char* in, string nom, int wbsz, bool withFlag)
 | 
|---|
| 196 | {
 | 
|---|
| 197 |   string outs = out;  
 | 
|---|
| 198 |   string ins = in;
 | 
|---|
| 199 |   return connect(prout, outs, prin, ins, nom, wbsz, withFlag);
 | 
|---|
| 200 | }
 | 
|---|
| 201 | 
 | 
|---|
| 202 | // ----------- fin ajout cgt
 | 
|---|
| 203 | 
 | 
|---|
| 204 | 
 | 
|---|
| 205 | 
 | 
|---|
| 206 | // -----------------------------------------------------------------
 | 
|---|
| 207 | //    Classe pour affichage de l'avancement des TOIProcessors 
 | 
|---|
| 208 | //                         Reza 08/2001 
 | 
|---|
| 209 | // -----------------------------------------------------------------
 | 
|---|
| 210 | 
 | 
|---|
| 211 | RzProcSampleCounter::RzProcSampleCounter()
 | 
|---|
| 212 | {
 | 
|---|
| 213 |   _msg = "SampleCounter/Info";
 | 
|---|
| 214 |   _rate = 50;
 | 
|---|
| 215 | }
 | 
|---|
| 216 | 
 | 
|---|
| 217 | RzProcSampleCounter::~RzProcSampleCounter()
 | 
|---|
| 218 | {
 | 
|---|
| 219 | }
 | 
|---|
| 220 | 
 | 
|---|
| 221 | long RzProcSampleCounter::PrintStats()
 | 
|---|
| 222 | {
 | 
|---|
| 223 |   int istart = 0;
 | 
|---|
| 224 |   int iend = 0;
 | 
|---|
| 225 |   long dns_print = 1000;
 | 
|---|
| 226 |   int dns_print_fac = _rate;
 | 
|---|
| 227 |   int nbmax_dns_print = 2;
 | 
|---|
| 228 | 
 | 
|---|
| 229 |   TOIManager* mgr = TOIManager::getManager();
 | 
|---|
| 230 |   
 | 
|---|
| 231 |   //  istart = mgr->getRequestedBegin();
 | 
|---|
| 232 |   //  iend = mgr->getRequestedEnd();
 | 
|---|
| 233 |   istart = SampleBegin();
 | 
|---|
| 234 |   iend = SampleEnd();
 | 
|---|
| 235 |   
 | 
|---|
| 236 |   dns_print = (iend-istart)/dns_print_fac;
 | 
|---|
| 237 |   if (dns_print < 1000) dns_print = ((iend-istart) < 1000) ? (iend-istart) : 1000;
 | 
|---|
| 238 |   if (dns_print < 1) dns_print = 1;
 | 
|---|
| 239 |   nbmax_dns_print = (iend-istart)/dns_print;
 | 
|---|
| 240 | 
 | 
|---|
| 241 |   cout << "RzProcSampleCounter::PrintStats() InfoMessage=" << _msg 
 | 
|---|
| 242 |        << "\n ... " << _msg << " istart=" 
 | 
|---|
| 243 |        << istart << " iend= " << iend << " dns_print= " << dns_print
 | 
|---|
| 244 |        << " nbmax_dns_print= " << nbmax_dns_print << endl;
 | 
|---|
| 245 |   // ------------------- Impression continu de stat ------------------------ 
 | 
|---|
| 246 |   long nb_dns_print = 0;
 | 
|---|
| 247 |   int nb_sleep = 0;
 | 
|---|
| 248 |   long last_sample_count = 0;
 | 
|---|
| 249 |   long processed_samples = 0;
 | 
|---|
| 250 |   long total_sample_count = dns_print*nbmax_dns_print;
 | 
|---|
| 251 |   bool alldone = false;
 | 
|---|
| 252 |   double fracperc = 0.;
 | 
|---|
| 253 |   int fperc = 0;
 | 
|---|
| 254 |   while (!alldone) {
 | 
|---|
| 255 |     processed_samples = ProcessedSampleCount();
 | 
|---|
| 256 |     if ( (processed_samples-last_sample_count > dns_print) || 
 | 
|---|
| 257 |          (processed_samples > total_sample_count-10) ) {
 | 
|---|
| 258 |       last_sample_count = processed_samples;
 | 
|---|
| 259 |       if (nb_dns_print == 0) cout << "\n";
 | 
|---|
| 260 |       nb_dns_print++;
 | 
|---|
| 261 |       fracperc = (double)processed_samples*100./(double)total_sample_count;
 | 
|---|
| 262 |       fperc = fracperc*100;
 | 
|---|
| 263 |       cout << ">>> " << _msg << ": ProcessedSampleCount()= " << last_sample_count 
 | 
|---|
| 264 |            << " Frac done = " << (double)fperc/100. << " %" << endl;
 | 
|---|
| 265 |       if (last_sample_count > total_sample_count-10)  alldone = true;
 | 
|---|
| 266 |       nb_sleep = 0;
 | 
|---|
| 267 |     }
 | 
|---|
| 268 |     else if ((nb_sleep+1)%5 == 0) {
 | 
|---|
| 269 |       fracperc = (double)processed_samples*100./(double)total_sample_count;
 | 
|---|
| 270 |       fperc = fracperc*100;
 | 
|---|
| 271 |       cout << "> " << _msg << ": ProcSamples()= " <<  processed_samples 
 | 
|---|
| 272 |            << " Done = " << " %" << (double)fperc/100.
 | 
|---|
| 273 |            << " NbSleep(1) = " << nb_sleep << endl;
 | 
|---|
| 274 |     }
 | 
|---|
| 275 |     sleep(1);  nb_sleep++;
 | 
|---|
| 276 |   }
 | 
|---|
| 277 |   
 | 
|---|
| 278 |   // -----------------------------------------------------------------------
 | 
|---|
| 279 |   
 | 
|---|
| 280 |   return last_sample_count;
 | 
|---|
| 281 | 
 | 
|---|
| 282 | }
 | 
|---|
| 283 | 
 | 
|---|
| 284 | 
 | 
|---|
| 285 | 
 | 
|---|
| 286 | 
 | 
|---|
| 287 | 
 | 
|---|
| 288 | 
 | 
|---|
| 289 | 
 | 
|---|
| 290 | 
 | 
|---|
| 291 | 
 | 
|---|
| 292 | 
 | 
|---|
| 293 | 
 | 
|---|
| 294 | 
 | 
|---|
| 295 | 
 | 
|---|
| 296 | 
 | 
|---|