[1738] | 1 | // ArchTOIPipe (C) CEA/DAPNIA/SPP IN2P3/LAL
|
---|
| 2 | // Eric Aubourg
|
---|
| 3 | // Christophe Magneville
|
---|
| 4 | // Reza Ansari
|
---|
[1762] | 5 | // $Id: toiprocessor.cc,v 1.19 2001-11-13 16:22:47 aubourg Exp $
|
---|
[1738] | 6 |
|
---|
[1365] | 7 | #include "toiprocessor.h"
|
---|
| 8 | #include "toimanager.h"
|
---|
| 9 | #include <pthread.h>
|
---|
[1663] | 10 |
|
---|
| 11 | #ifdef HAVE_VALUES_H
|
---|
[1365] | 12 | #include <values.h>
|
---|
[1663] | 13 | #endif
|
---|
[1365] | 14 |
|
---|
[1663] | 15 | #ifndef MAXINT
|
---|
| 16 | #define MAXINT 2147483647
|
---|
| 17 | #endif
|
---|
| 18 |
|
---|
| 19 | #ifdef HAVE_STDINT_H
|
---|
| 20 | #include <stdint.h>
|
---|
| 21 | #endif
|
---|
| 22 |
|
---|
[1365] | 23 | #ifdef WITH_SOPHYA
|
---|
| 24 | #include "pexceptions.h"
|
---|
| 25 | #else
|
---|
| 26 | #include "apexceptions.h"
|
---|
| 27 | #endif
|
---|
| 28 |
|
---|
| 29 | #define pthread_mutexattr_setkind_np pthread_mutexattr_settype
|
---|
| 30 |
|
---|
| 31 | TOIProcessor::TOIProcessor() {
|
---|
| 32 | //cout << "TOIProcessor::TOIProcessor" << endl;
|
---|
| 33 | outTOIs = NULL;
|
---|
| 34 | inTOIs = NULL;
|
---|
| 35 | inited=false;
|
---|
| 36 |
|
---|
| 37 | upExtra = 0;
|
---|
| 38 | lowExtra = 0;
|
---|
| 39 | minOut = -1;
|
---|
| 40 | maxOut = -1;
|
---|
| 41 | neededHistory = 1000;
|
---|
| 42 | lastAWN = 0;
|
---|
| 43 | wontNeedValue = -1;
|
---|
| 44 |
|
---|
| 45 | }
|
---|
| 46 |
|
---|
| 47 | TOIProcessor::~TOIProcessor() {
|
---|
| 48 | delete[] outTOIs;
|
---|
| 49 | delete[] inTOIs;
|
---|
| 50 | //if (mutex)
|
---|
| 51 | pthread_mutex_destroy(&mutex);
|
---|
| 52 | //if (dataReady)
|
---|
| 53 | pthread_cond_destroy(&dataReady);
|
---|
| 54 | pthread_detach(thread);
|
---|
| 55 | }
|
---|
| 56 |
|
---|
| 57 |
|
---|
| 58 | void TOIProcessor::init() {
|
---|
| 59 | //cout << "TOIProcessor::init" << endl;
|
---|
| 60 | }
|
---|
| 61 |
|
---|
| 62 | void TOIProcessor::afterinit() {
|
---|
[1437] | 63 | int i;
|
---|
[1365] | 64 | inTOIs = new (TOI*[inIx.size()]);
|
---|
[1437] | 65 | for(i=0; i<inIx.size(); i++)
|
---|
[1439] | 66 | inTOIs[i] = NULL; // Protection-Initialisation - Reza 11/3/2001
|
---|
[1365] | 67 | outTOIs = new (TOI*[outIx.size()]);
|
---|
[1437] | 68 | for(i=0; i<outIx.size(); i++)
|
---|
[1439] | 69 | outTOIs[i] = NULL; // Protection-Initialisation - Reza 11/3/2001
|
---|
[1365] | 70 | }
|
---|
| 71 |
|
---|
| 72 | int TOIProcessor::getMinOut() {
|
---|
| 73 | //cout << name << "minout" << endl;
|
---|
| 74 | if (minOut < 0) minOut = calcMinOut();
|
---|
| 75 | //cout << name << "minout=" << minOut << endl;
|
---|
| 76 | return minOut;
|
---|
| 77 | }
|
---|
| 78 |
|
---|
| 79 | int TOIProcessor::getMaxOut() {
|
---|
| 80 | //cout << name << "maxout" << endl;
|
---|
| 81 | if (maxOut < 0) maxOut = calcMaxOut();
|
---|
| 82 | //cout << name << "maxout=" << maxOut << endl;
|
---|
| 83 | return maxOut;
|
---|
| 84 | }
|
---|
| 85 |
|
---|
| 86 | int TOIProcessor::calcMinOut() {
|
---|
| 87 | return getMinIn() + lowExtra;
|
---|
| 88 | }
|
---|
| 89 |
|
---|
| 90 | int TOIProcessor::calcMaxOut() {
|
---|
| 91 | return getMaxIn() - upExtra;
|
---|
| 92 | }
|
---|
| 93 |
|
---|
| 94 | int TOIProcessor::getMinIn() {
|
---|
| 95 | int nIn = inIx.size();
|
---|
| 96 | int minIn = 0;
|
---|
| 97 | for (int i=0; i<nIn; i++) {
|
---|
| 98 | TOI* toi = inTOIs[i];
|
---|
[1439] | 99 | if (toi == NULL) continue; // Protection - Reza 13/3/2001
|
---|
[1365] | 100 | int x = toi->getMinSn();
|
---|
| 101 | if (x > minIn) minIn = x;
|
---|
| 102 | }
|
---|
| 103 | return minIn;
|
---|
| 104 | }
|
---|
| 105 |
|
---|
| 106 | int TOIProcessor::getMaxIn() {
|
---|
[1663] | 107 | int_4 nIn = inIx.size();
|
---|
| 108 | int_4 maxIn = MAXINT;
|
---|
[1365] | 109 | for (int i=0; i<nIn; i++) {
|
---|
| 110 | TOI* toi = inTOIs[i];
|
---|
[1439] | 111 | if (toi == NULL) continue; // Protection - Reza 13/3/2001
|
---|
[1663] | 112 | int_4 x = toi->getMaxSn();
|
---|
[1365] | 113 | if (x < maxIn) maxIn = x;
|
---|
| 114 | }
|
---|
| 115 | return maxIn;
|
---|
| 116 | }
|
---|
| 117 |
|
---|
| 118 |
|
---|
| 119 | int TOIProcessor::declareInput(string toi) {
|
---|
| 120 | if (inIx.find(toi) != inIx.end())
|
---|
| 121 | throw DuplicateIdExc("TOIProcessor::declareInput : "+toi+" already declared");
|
---|
| 122 | int i = inIx.size();
|
---|
| 123 | inIx[toi] = i;
|
---|
| 124 | return i;
|
---|
| 125 | }
|
---|
| 126 |
|
---|
| 127 | int TOIProcessor::declareOutput(string toi) {
|
---|
| 128 | if (outIx.find(toi) != outIx.end())
|
---|
| 129 | throw DuplicateIdExc("TOIProcessor::declareInput : "+toi+" already declared");
|
---|
| 130 | int i = outIx.size();
|
---|
| 131 | outIx[toi] = i;
|
---|
| 132 | return i;
|
---|
| 133 | }
|
---|
| 134 |
|
---|
| 135 | int TOIProcessor::getInputTOIIndex(string toi) {
|
---|
[1367] | 136 | chkinit();
|
---|
[1365] | 137 | map<string, int>::iterator i = inIx.find(toi);
|
---|
| 138 | if (i == inIx.end()) return -1;
|
---|
| 139 | return (*i).second;
|
---|
| 140 | }
|
---|
| 141 |
|
---|
| 142 | int TOIProcessor::getOutputTOIIndex(string toi) {
|
---|
[1367] | 143 | chkinit();
|
---|
[1365] | 144 | map<string, int>::iterator i = outIx.find(toi);
|
---|
| 145 | if (i == outIx.end()) return -1;
|
---|
| 146 | return (*i).second;
|
---|
| 147 | }
|
---|
| 148 |
|
---|
[1437] | 149 | // Methodes rajoutees par Reza 11/3/2001
|
---|
| 150 | TOI* TOIProcessor::getInputTOI(int toiIndex) {
|
---|
| 151 | // chkinit();
|
---|
| 152 | if (toiIndex >= inIx.size())
|
---|
| 153 | throw RangeCheckError("TOIProcessor::getInputTOI() out of bound toiIndex");
|
---|
| 154 | TOI* toi = inTOIs[toiIndex];
|
---|
| 155 | if (toi == NULL)
|
---|
| 156 | throw NullPtrError("TOIProcessor::getInputTOI() - Not assigned TOI !");
|
---|
| 157 | return(toi);
|
---|
| 158 | }
|
---|
| 159 |
|
---|
| 160 | TOI* TOIProcessor::getOutputTOI(int toiIndex) {
|
---|
| 161 | // chkinit();
|
---|
| 162 | if (toiIndex >= outIx.size())
|
---|
| 163 | throw RangeCheckError("TOIProcessor::getOutputTOI() out of bound toiIndex");
|
---|
| 164 | TOI* toi = outTOIs[toiIndex];
|
---|
| 165 | if (toi == NULL)
|
---|
| 166 | throw NullPtrError("TOIProcessor::getOutputTOI() - Not assigned TOI !");
|
---|
| 167 | return(toi);
|
---|
| 168 | }
|
---|
| 169 |
|
---|
| 170 | bool TOIProcessor::checkInputTOIIndex(int toiIndex) {
|
---|
| 171 | if (toiIndex >= inIx.size()) return false;
|
---|
| 172 | if (inTOIs[toiIndex] == NULL) return false;
|
---|
| 173 | return true;
|
---|
| 174 | }
|
---|
| 175 |
|
---|
| 176 | bool TOIProcessor::checkOutputTOIIndex(int toiIndex) {
|
---|
| 177 | if (toiIndex >= outIx.size()) return false;
|
---|
| 178 | if (outTOIs[toiIndex] == NULL) return false;
|
---|
| 179 | return true;
|
---|
| 180 | }
|
---|
| 181 |
|
---|
[1762] | 182 | void TOIProcessor::PrintStatus(::ostream & os)
|
---|
[1437] | 183 | {
|
---|
[1439] | 184 | chkinit();
|
---|
[1437] | 185 | os << " TOIProcessor::PrintStatus() - Name= " << name
|
---|
| 186 | << " MinIn=" << getMinIn() << " MaxIn=" << getMaxIn() << endl;
|
---|
| 187 | os << " --- Inputs N= " << inIx.size() << endl;
|
---|
| 188 | int k;
|
---|
| 189 | for(k=0; k<inIx.size(); k++) {
|
---|
| 190 | os << "Input[" << k << "] : " << getInName(k) ;
|
---|
| 191 | if (inTOIs[k] != NULL)
|
---|
| 192 | os << " Connected TOI " << inTOIs[k]->getName() << endl;
|
---|
| 193 | else os << " NO TOI " << endl;
|
---|
| 194 | }
|
---|
| 195 | os << " --- Outputs N= " << outIx.size() << endl;
|
---|
| 196 | for(k=0; k<outIx.size(); k++) {
|
---|
| 197 | os << "Output[" << k << "] : " << getOutName(k) ;
|
---|
| 198 | if (outTOIs[k] != NULL)
|
---|
| 199 | os << " Connected TOI " << outTOIs[k]->getName() << endl;
|
---|
| 200 | else os << " NO TOI " << endl;
|
---|
| 201 | }
|
---|
| 202 | os << endl;
|
---|
| 203 | return;
|
---|
| 204 | }
|
---|
| 205 |
|
---|
| 206 | // Fin rajout Reza 11/3/2001
|
---|
| 207 |
|
---|
[1365] | 208 | void TOIProcessor::addInput(string name, TOI* toi) {
|
---|
| 209 | chkinit();
|
---|
| 210 | map<string, int>::iterator i = inIx.find(name);
|
---|
| 211 | if (i == inIx.end()) throw NotFoundExc("TOIProcessor::addInput "+
|
---|
| 212 | name+" not declared");
|
---|
[1439] | 213 | inTOIs[(*i).second] = toi;
|
---|
| 214 | toi->addConsumer(this); // $CHECK$ Reza 13/3/2001
|
---|
[1365] | 215 | }
|
---|
| 216 |
|
---|
| 217 | void TOIProcessor::addOutput(string name, TOI* toi) {
|
---|
| 218 | chkinit();
|
---|
| 219 | map<string, int>::iterator i = outIx.find(name);
|
---|
| 220 | if (i == outIx.end()) throw NotFoundExc("TOIProcessor::addOutput "+
|
---|
| 221 | name+" not declared");
|
---|
| 222 | toi->setProducer(this);
|
---|
| 223 | outTOIs[(*i).second] = toi;
|
---|
| 224 | }
|
---|
| 225 |
|
---|
[1367] | 226 | string TOIProcessor::getOutName(int i) {
|
---|
| 227 | if (i > outIx.size()) throw RangeCheckError("TOIProcessor::getOutName "
|
---|
| 228 | " out of bound");
|
---|
[1437] | 229 | map<string, int>::iterator j;
|
---|
| 230 | for(j=outIx.begin(); j!= outIx.end(); j++)
|
---|
| 231 | if ((*j).second == i) return (*j).first;
|
---|
| 232 |
|
---|
| 233 | throw RangeCheckError("TOIProcessor::getOutName Not found index !");
|
---|
[1367] | 234 | }
|
---|
| 235 |
|
---|
| 236 | string TOIProcessor::getInName(int i) {
|
---|
| 237 | if (i > inIx.size()) throw RangeCheckError("TOIProcessor::getInName "
|
---|
| 238 | " out of bound");
|
---|
[1437] | 239 | map<string, int>::iterator j;
|
---|
| 240 | for(j=inIx.begin(); j!= inIx.end(); j++)
|
---|
| 241 | if ((*j).second == i) return (*j).first;
|
---|
| 242 |
|
---|
| 243 | throw RangeCheckError("TOIProcessor::getOutName Not found index !");
|
---|
[1367] | 244 | }
|
---|
| 245 |
|
---|
[1365] | 246 | void TOIProcessor::run() {
|
---|
| 247 |
|
---|
| 248 | }
|
---|
| 249 |
|
---|
[1689] | 250 | void TOIProcessor::warnPutDone() {
|
---|
| 251 | int n = outIx.size();
|
---|
| 252 | for (int i=0; i<n; i++) {
|
---|
| 253 | TOI* toi = outTOIs[i];
|
---|
[1725] | 254 | if (toi) {
|
---|
| 255 | toi->putDone();
|
---|
| 256 | }
|
---|
[1689] | 257 | }
|
---|
| 258 | }
|
---|
| 259 |
|
---|
[1365] | 260 | void* TOIProcessor::ThreadStart(void* arg) {
|
---|
| 261 | TOIProcessor* p = (TOIProcessor*) arg;
|
---|
| 262 | // cout << p->name << " new thread running " << pthread_self() << endl;
|
---|
| 263 | p->run();
|
---|
[1689] | 264 | p->warnPutDone();
|
---|
[1629] | 265 | pthread_exit(NULL);
|
---|
[1365] | 266 | // cout << p->name << " thread done " << pthread_self() << endl;
|
---|
| 267 | return NULL;
|
---|
| 268 | }
|
---|
| 269 |
|
---|
| 270 | #ifdef Linux
|
---|
| 271 | #define pthread_mutexattr_settype pthread_mutexattr_setkind_np
|
---|
| 272 | #define PTHREAD_MUTEX_ERRORCHECK PTHREAD_MUTEX_ERRORCHECK_NP
|
---|
| 273 | #define pthread_mutex_setname_np(a,b,c)
|
---|
| 274 | #define pthread_cond_setname_np(a,b,c)
|
---|
| 275 | #endif
|
---|
| 276 |
|
---|
| 277 | void TOIProcessor::start() {
|
---|
| 278 | pthread_cond_init(&dataReady, NULL);
|
---|
| 279 | pthread_mutexattr_init(&mutattr);
|
---|
| 280 | // pthread_mutexattr_settype(&mutattr, PTHREAD_MUTEX_ERRORCHECK);
|
---|
| 281 | pthread_mutex_init(&mutex, &mutattr);
|
---|
| 282 | //pthread_mutex_setname_np(&mutex, (name + "_proc_mutex").c_str(), 0);
|
---|
| 283 | //pthread_cond_setname_np(&dataReady, (name + "_proc_cond").c_str(), 0);
|
---|
| 284 | //cout << name << " starting thread " << &thread << endl;
|
---|
| 285 | pthread_create(&thread, NULL, ThreadStart, this);
|
---|
| 286 | TOIManager::getManager()->addThread(&thread);
|
---|
| 287 | }
|
---|
| 288 |
|
---|
| 289 | #ifndef NO_SOPHYA
|
---|
[1464] | 290 | /* ---- l'interface va etre modifiee, NE PAS UTILISER
|
---|
[1365] | 291 | Array TOIProcessor::getData(int toiIndex, int iStart, int iEnd) {
|
---|
[1437] | 292 | TOI* toi = getInputTOI(toiIndex);
|
---|
[1365] | 293 | toi->waitForData(iStart, iEnd);
|
---|
| 294 | return toi->getData(iStart, iEnd);
|
---|
| 295 | }
|
---|
| 296 |
|
---|
| 297 | Array TOIProcessor::getError(int toiIndex, int iStart, int iEnd) {
|
---|
[1437] | 298 | TOI* toi = getInputTOI(toiIndex);
|
---|
[1365] | 299 | toi->waitForData(iStart, iEnd);
|
---|
| 300 | return toi->getError(iStart, iEnd);
|
---|
| 301 | }
|
---|
| 302 |
|
---|
| 303 | TArray<int_4> TOIProcessor::getFlag(int toiIndex, int iStart, int iEnd) {
|
---|
[1437] | 304 | TOI* toi = getInputTOI(toiIndex);
|
---|
[1365] | 305 | toi->waitForData(iStart, iEnd);
|
---|
| 306 | return toi->getFlag(iStart, iEnd);
|
---|
| 307 | }
|
---|
[1464] | 308 | l'interface va etre modifiee, NE PAS UTILISER ---- */
|
---|
[1365] | 309 | #endif
|
---|
| 310 |
|
---|
| 311 | double TOIProcessor::getData(int toiIndex, int i) {
|
---|
[1437] | 312 | TOI* toi = getInputTOI(toiIndex);
|
---|
[1740] | 313 | if (toi->needSyncOldWay()) toi->waitForData(i); // seulement pour autre que segmented
|
---|
| 314 | autoWontNeed(i);
|
---|
[1365] | 315 | return toi->getData(i);
|
---|
| 316 | }
|
---|
| 317 |
|
---|
[1532] | 318 | void TOIProcessor::getData(int toiIndex, int i, double &data, uint_8 &flag)
|
---|
[1462] | 319 | {
|
---|
| 320 | TOI* toi = getInputTOI(toiIndex);
|
---|
[1740] | 321 | if (toi->needSyncOldWay()) toi->waitForData(i); // seulement pour autre que segmented
|
---|
[1462] | 322 | toi->getData(i, data, flag);
|
---|
[1742] | 323 | autoWontNeed(i);
|
---|
[1462] | 324 | return;
|
---|
| 325 | }
|
---|
| 326 |
|
---|
[1742] | 327 | void TOIProcessor::getData(int toiIndex, int i, int n, double* d)
|
---|
| 328 | {
|
---|
| 329 | TOI* toi = getInputTOI(toiIndex);
|
---|
| 330 | if (toi->needSyncOldWay()) toi->waitForData(i+n); // seulement pour autre que segmented
|
---|
| 331 | toi->getData(i, n, d);
|
---|
| 332 | autoWontNeed(i);
|
---|
| 333 | return;
|
---|
| 334 | }
|
---|
| 335 |
|
---|
| 336 | void TOIProcessor::getData(int toiIndex, int i, int n, double* d, uint_8* f)
|
---|
| 337 | {
|
---|
| 338 | TOI* toi = getInputTOI(toiIndex);
|
---|
| 339 | if (toi->needSyncOldWay()) toi->waitForData(i+n); // seulement pour autre que segmented
|
---|
| 340 | toi->getData(i, n, d, f);
|
---|
| 341 | autoWontNeed(i);
|
---|
| 342 | return;
|
---|
| 343 | }
|
---|
| 344 |
|
---|
| 345 |
|
---|
[1462] | 346 | /*RZCMV
|
---|
[1365] | 347 | double TOIProcessor::getError(int toiIndex, int i) {
|
---|
[1437] | 348 | TOI* toi = getInputTOI(toiIndex);
|
---|
[1365] | 349 | toi->waitForData(i);
|
---|
| 350 | return toi->getError(i);
|
---|
| 351 | }
|
---|
| 352 |
|
---|
| 353 | int_4 TOIProcessor::getFlag(int toiIndex, int i) {
|
---|
[1437] | 354 | TOI* toi = getInputTOI(toiIndex);
|
---|
[1365] | 355 | toi->waitForData(i);
|
---|
| 356 | return toi->getFlag(i);
|
---|
| 357 | }
|
---|
[1462] | 358 | */
|
---|
[1365] | 359 |
|
---|
| 360 | void TOIProcessor::setNeededHistory(int nsamples) {
|
---|
| 361 | neededHistory = nsamples;
|
---|
| 362 | }
|
---|
| 363 |
|
---|
| 364 | void TOIProcessor::wontNeedBefore(int i) {
|
---|
| 365 | if (i<wontNeedValue) return;
|
---|
| 366 | wontNeedValue = i;
|
---|
| 367 | for (int j=0; j< (int) inIx.size(); j++) {
|
---|
[1490] | 368 | // $CHECK$ Reza 6/5/2001 Protection sur non connected TOI
|
---|
| 369 | if (inTOIs[j]) inTOIs[j]->wontNeedBefore(i);
|
---|
[1365] | 370 | }
|
---|
| 371 | }
|
---|
| 372 |
|
---|
| 373 | void TOIProcessor::autoWontNeed(int iCur) {
|
---|
| 374 | if (neededHistory <=0) return;
|
---|
| 375 | if (iCur < lastAWN + neededHistory) return;
|
---|
| 376 | lastAWN = iCur;
|
---|
[1750] | 377 | // cout << name << " wontNeedBefore " << iCur-neededHistory << endl;
|
---|
[1365] | 378 | wontNeedBefore(iCur-neededHistory);
|
---|
| 379 | }
|
---|
| 380 |
|
---|
| 381 | void TOIProcessor::notify() {
|
---|
[1740] | 382 | lock();
|
---|
[1365] | 383 | pthread_cond_broadcast(&dataReady);
|
---|
| 384 | unlock();
|
---|
| 385 | }
|
---|
| 386 |
|
---|
| 387 |
|
---|
[1532] | 388 | void TOIProcessor::putData(int toiIndex, int i, double value, uint_8 flg) {
|
---|
[1437] | 389 | TOI* toi = getOutputTOI(toiIndex);
|
---|
[1365] | 390 | toi->putData(i, value, flg);
|
---|
[1742] | 391 | // autoWontNeed(i); // now done on getData
|
---|
[1740] | 392 | if (toi->needSyncOldWay()) notify(); // seulement pour non segmented
|
---|
[1365] | 393 | }
|
---|
| 394 |
|
---|
[1742] | 395 | void TOIProcessor::putData(int toiIndex, int i, int n, double const* val,
|
---|
[1743] | 396 | uint_8 const* flg) {
|
---|
[1742] | 397 | TOI* toi = getOutputTOI(toiIndex);
|
---|
| 398 | toi->putData(i, n, val, flg);
|
---|
| 399 | if (toi->needSyncOldWay()) notify(); // seulement pour non segmented
|
---|
| 400 | }
|
---|
| 401 |
|
---|
[1462] | 402 | /*RZCMV
|
---|
[1365] | 403 | void TOIProcessor::putDataError(int toiIndex, int i, double value,
|
---|
| 404 | double error, int_4 flg) {
|
---|
[1437] | 405 | TOI* toi = getOutputTOI(toiIndex);
|
---|
| 406 | if (toi == NULL)
|
---|
| 407 | throw NullPtrError("TOIProcessor::putDataError() - Not assigned TOI !");
|
---|
[1365] | 408 | toi->putDataError(i, value, error, flg);
|
---|
| 409 | autoWontNeed(i);
|
---|
| 410 | notify();
|
---|
| 411 | }
|
---|
[1462] | 412 | */
|
---|
[1365] | 413 |
|
---|