Index: trunk/AddOn/TAcq/brproc.cc
===================================================================
--- trunk/AddOn/TAcq/brproc.cc	(revision 3724)
+++ trunk/AddOn/TAcq/brproc.cc	(revision 3726)
@@ -76,4 +76,5 @@
        << " JF2=" << jf2_ << " DJF=" << djf_ << endl;
 
+  fgallfibok=NULL;
   fgcktt_=false;
 }
@@ -105,11 +106,115 @@
 void BRVisibilityCalculator::run()
 {
-  if (nparthr_ > 1) {
+  if (nparthr_ < 2)  return BRBaseProcessor::run();
+  // Execution multithread parallele
+  setRC(1);	
+  int rc=0;
+  try {
+    cout << " BRVisibilityCalculator::run() - Starting " << " NFibers=" << memgr_.NbFibres() 
+	 << " NChan=" << 2*memgr_.NbFibres() << endl;	
+    if ((nmean_%memgr_.NbPaquets())!=0) {
+      uint_4 mnmean = (nmean_/memgr_.NbPaquets()+1)*memgr_.NbPaquets();
+      cout << " BRVisibilityCalculator::run()/Info changing nmean=" << nmean_  << " to multiple of"
+	   << " memgr_.NbPaquets() -> " << mnmean << endl;
+      nmean_=mnmean;
+    }
     paralex_.SetParallelTask(*this);
     cout << " BRVisibilityCalculator::run()/Info : starting ParallelExecutor with nThreads=" 
 	 << paralex_.nThreads() << " ... " << endl;
     paralex_.start();
-  }
-  return BRBaseProcessor::run();
+
+    fgallfibok = new bool[memgr_.NbPaquets()];
+
+    size_t paqsz=memgr_.PaqSize();
+    bool fgrun=true;
+    while (fgrun) {
+      if (stop_) break;
+      if (memgr_.GetRunState() == MemZR_Stopped) break;
+      int mid = memgr_.FindMemZoneId(MemZA_ProcA);
+      Byte* buffg = memgr_.GetMemZone(mid);
+      if (buffg == NULL) {
+	cout << "BRVisibilityCalculator::run()/ERROR memgr.GetMemZone(" << mid << ") -> NULL" << endl;
+	setRC(7);      fgrun=false;		
+	break;	
+      }
+      for(size_t fib=0; fib<(size_t)memgr_.NbFibres(); fib++) {
+	fbuff_[fib] = memgr_.GetMemZone(mid,fib);
+	if (fbuff_[fib] == NULL) { // cela ne devrait pas arriver 
+	  cout << "BRBaseProcessor::run()/ERROR memgr.GetMemZone(" << mid << "," << fib << ") -> NULL" << endl;
+	  setRC(9);	  fgrun=false;		
+	  break;
+	}
+      }
+
+      if (totnbpaq_%nmean_ == 0) {
+	if (totnbpaq_ > 0) {
+	  moyfc_/=nmean_;
+	  moytt_/=nmean_;
+	  vismtx_.Info()["MeanFC"] = moyfc_;
+	  vismtx_.Info()["MeanTT"] = moytt_;
+	  vismtx_.Info()["NPAQSUM"] = nmean_;
+	  
+	  // ATTENTION : Matrice visibilites non moyennee 
+	  char nfile[32];
+	  sprintf(nfile,"vismtx%d.ppf",numfile_);
+	  string flnm=outpath_+nfile;
+	  POutPersist po(flnm);
+	  po << vismtx_;
+	  cout << numfile_ << "-BRVisibilityCalculator::run() NPaqProc="
+	       << totnbpaq_ << "  -> Visibility Matrix in " << flnm << endl;
+	  FillVisibTable(moyfc_, moytt_);
+	  numfile_++;
+	}
+	vismtx_ = complex<r_4>((r_4)0.,(r_4)0.);
+	moyfc_=moytt_=0.;
+      }
+
+      for(size_t jp=0; jp<memgr_.NbPaquets(); jp++) {   // boucle sur les paquets d'une zone  
+	fgallfibok[jp]=fgokallfibers_=true;
+	for(size_t fib=0; fib<(size_t)memgr_.NbFibres(); fib++) {
+	  vpaq_[fib].Set(fbuff_[fib]+jp*paqsz);
+	  vfgok_[fib] = vpchk_[fib].Check(vpaq_[fib],curfc_[fib]);
+	  if (!vfgok_[fib])  fgallfibok[jp]=fgokallfibers_=false;
+	}
+	if (fgokallfibers_)  {
+	  if (totprocnpaq_==0) {
+	    for(size_t fib=0; fib<(size_t)memgr_.NbFibres(); fib++)  {
+	      fcfirst_[fib]=curfc_[fib];
+	      ttfirst_[fib]=vpaq_[fib].TimeTag();
+	    }
+	  }
+	  totprocnpaq_++;
+	  moyfc_ += curfc_[0];
+	  moytt_ += (vpaq_[0].TimeTag()-ttfirst_[0]);
+	  if (fgcktt_)  CheckTimeTag();
+	  totnbpaq_++;
+	}
+      }  // Fin de boucle sur les paquets 
+      
+      // Execution parallele  pour calcul des visibilites par bandes de frequence
+      int rcpex=paralex_.execute();
+      if (rcpex!=0)  cout << " BRVisibilityCalculator::run() / Error Rc[paralex_.execute()]=" << rcpex << endl;
+
+      memgr_.FreeMemZone(mid, MemZS_ProcA);
+    } // Fin de boucle sur les zones a traiter 
+    //------------------------------------
+    cout << " --------- END BRVisibilityCalculator::run() , TotNbProcPaq=" << totprocnpaq_ << endl;
+    /*
+    for(size_t fib=0; fib<(size_t)memgr_.NbFibres(); fib++)  vpchk_[fib].Print();
+    cout << " ------------------------------------ " << endl;
+    */
+    delete[] fgallfibok;
+  }
+  catch (std::exception& exc) {
+    cout << " BRBaseProcessor::run()/catched std::exception " << exc.what() << endl;
+    setRC(98);	
+    return; 
+  }
+  catch(...) {
+    cout << " BRBaseProcessor::run()/catched unknown ... exception " << endl;
+    setRC(99);	
+    return; 
+  }
+  
 }
 
@@ -165,20 +270,15 @@
   }
 
-  if (nparthr_ > 1)  {  // Execution parallele 
-    int rcpex=paralex_.execute();
-    if (rcpex!=0)  cout << " BRVisibilityCalculator::Process() / Error Rc[paralex_.execute()]=" << rcpex << endl;
-  }
-  else {
-    sa_size_t k=0;
-    for(size_t i=0; i<vpdata_.size(); i++) {
-      for(size_t j=i; j<vpdata_.size(); j++) {
-	TVector< complex<r_4> > vis = vismtx_.Row(k);   k++;
-	for(sa_size_t f=1; f<vis.Size(); f++) {
-	  vis(f) += complex<r_4>((r_4)vpdata_[i][f].realB(), (r_4)vpdata_[i][f].imagB()) * 
-	    complex<r_4>((r_4)vpdata_[j][f].realB(), -(r_4)vpdata_[j][f].imagB());
-	}
-      }
-    }
-  }
+  sa_size_t k=0;
+  for(size_t i=0; i<vpdata_.size(); i++) {
+    for(size_t j=i; j<vpdata_.size(); j++) {
+      TVector< complex<r_4> > vis = vismtx_.Row(k);   k++;
+      for(sa_size_t f=1; f<vis.Size(); f++) {
+	vis(f) += complex<r_4>((r_4)vpdata_[i][f].realB(), (r_4)vpdata_[i][f].imagB()) * 
+	  complex<r_4>((r_4)vpdata_[j][f].realB(), -(r_4)vpdata_[j][f].imagB());
+      }
+    }
+  }
+
   moyfc_ += curfc_[0];
   moytt_ += (vpaq_[0].TimeTag()-ttfirst_[0]);
@@ -191,20 +291,35 @@
 int BRVisibilityCalculator::execute(int tid)
 {
+  vector<TwoByteComplex*>  pvpdata(2*memgr_.NbFibres());
+  size_t paqsz=memgr_.PaqSize();
+  BRPaquet ppaq(paqsz);
+
   sa_size_t fdelt = vismtx_.NCols()/nparthr_;
   sa_size_t fdeb = tid*fdelt;
   sa_size_t ffin = (tid+1)*fdelt;
+
   if (fdeb<1) fdeb=1;
   if ((ffin>vismtx_.NCols())||(tid==(nparthr_-1))) ffin=vismtx_.NCols();
+
+  for(size_t jp=0; jp<memgr_.NbPaquets(); jp++) {   // boucle sur les paquets d'une zone  
+    if (!fgallfibok[jp])  continue;
+    for(size_t fib=0; fib<(size_t)memgr_.NbFibres(); fib++) {
+      ppaq.Set(fbuff_[fib]+jp*paqsz);
+      pvpdata[2*fib] = ppaq.Data1C();
+      pvpdata[2*fib+1] = ppaq.Data2C();
+    }
+    sa_size_t k=0;
+    for(size_t i=0; i<vpdata_.size(); i++) {
+      for(size_t j=i; j<vpdata_.size(); j++) {
+	TVector< complex<r_4> > vis = vismtx_.Row(k);   k++;
+	for(sa_size_t f=fdeb; f<ffin; f++) {
+	  vis(f) += complex<r_4>((r_4)pvpdata[i][f].realB(), (r_4)pvpdata[i][f].imagB()) * 
+	    complex<r_4>((r_4)pvpdata[j][f].realB(), -(r_4)pvpdata[j][f].imagB());
+	}
+      }
+    }
+
+  }  // Fin de boucle sur les paquets 
   
-  sa_size_t k=0;
-  for(size_t i=0; i<vpdata_.size(); i++) {
-    for(size_t j=i; j<vpdata_.size(); j++) {
-      TVector< complex<r_4> > vis = vismtx_.Row(k);   k++;
-      for(sa_size_t f=fdeb; f<ffin; f++) {
-	vis(f) += complex<r_4>((r_4)vpdata_[i][f].realB(), (r_4)vpdata_[i][f].imagB()) * 
-	  complex<r_4>((r_4)vpdata_[j][f].realB(), -(r_4)vpdata_[j][f].imagB());
-      }
-    }
-  }
   return 0;
 }
