Index: trunk/AddOn/TAcq/brproc.cc
===================================================================
--- trunk/AddOn/TAcq/brproc.cc	(revision 3774)
+++ trunk/AddOn/TAcq/brproc.cc	(revision 3776)
@@ -31,15 +31,13 @@
 
 /* --Methode-- */
-BRVisibilityCalculator::BRVisibilityCalculator(RAcqMemZoneMgr& memgr, string outpath, uint_4 nmean, 
-					       uint_4 freq1, uint_4 freq2, uint_4 nbfreq, size_t nthr)
-  : BRBaseProcessor(memgr), paralex_(*this, nthr), nparthr_(nthr), outpath_(outpath), nmean_(nmean), 
-    numfreq1_(freq1), numfreq2_(freq2), nbinfreq_(nbfreq), vpdata_(2*memgr.NbFibres())
+BRVisibilityCalculator::BRVisibilityCalculator(RAcqMemZoneMgr& memgr, string outpath, uint_4 nmean, size_t nthr)
+  : BRBaseProcessor(memgr), paralex_(*this, nthr), nparthr_(nthr), 
+    outpath_(outpath), nmean_(nmean), nbcalc_(1), calcid_(0), vpdata_(2*memgr.NbFibres())
     // , dtfos_(outpath+"visdt.fits", Fits_Create), visdt_(dtfos_, 1024, true);
-
 {  
-  BRPaquet paq(memgr_.PaqSize());
-
-  vismtx_.SetSize((2*memgr_.NbFibres()+1)*memgr_.NbFibres(), paq.DataSize()/4); 
-  chanum_.SetSize(vismtx_.NRows());
+  DefineRank(1,0);
+
+  uint_4 maxnpairs = (2*memgr_.NbFibres()+1)*memgr_.NbFibres();
+  chanum_.SetSize(maxnpairs);
   sa_size_t k=0;
   for(size_t i=0; i<2*memgr_.NbFibres(); i++)  vpdata_[i]=NULL;
@@ -49,4 +47,6 @@
     }
   }
+  SelectPairs();
+
   //  visdt_.AddFloatColumn("mfc");
   visdt_.AddDoubleColumn("mfc");
@@ -60,50 +60,121 @@
   if (nmean_ < 1)  nmean_=1;
 
+  cout << " BRVisibilityCalculator::/Info  nmean=" << nmean_ << endl;
+
   totnbpaq_=0;
   numfile_=0;
   moyfc_=moytt_=0.;
 
-  jf1_=numfreq1_; jf2_=numfreq2_;
+  fgallfibok=NULL;
+  fgcktt_=false;
+}
+
+/* --Methode-- */
+BRVisibilityCalculator::~BRVisibilityCalculator()
+{
+  cout << " BRVisibilityCalculator - Visibility Datatable : " << endl;
+  cout << visdt_;
+  string filename;
+  filename = outpath_+"visdt.ppf";
+  if (nbcalc_>1) {
+    char sbuff[32];
+    sprintf(sbuff,"visdt_%d",(int)calcid_);
+    filename = outpath_+sbuff;
+  }
+  POutPersist po(filename);
+  po << visdt_;
+  if (calcid_ == 0) {
+    POutPersist poc(outpath_+"chanum.ppf");
+    poc << chanum_;
+  
+    if (fgcktt_) {
+      cout << " BRVisibilityCalculator -  Check TimeTag done: TotNPaqProc= " << totnbpaq_ << endl;
+      for(size_t fib=0; fib<(size_t)memgr_.NbFibres(); fib++)  {
+	cout << " BRTTCheck-Fiber[" << fib << "] NBadTT=" << vbadtt_[fib] << " NDiffTT>5="
+	     << vndiff5tt_[fib] << "  NotSameTT=" << vnsamett_[fib] << endl; 
+      }
+      POutPersist pott(outpath_+"ttfcmtx.ppf");
+      pott << PPFNameTag("FC") << fcmtx_; 
+      pott << PPFNameTag("TT") << ttmtx_;     
+    }
+  }
+}
+
+/* --Methode-- */
+void BRVisibilityCalculator::DefineRank(uint_4 nbc, uint_4 cid)
+{
+  if ((nbc>4)||(cid>=nbc)) 
+    throw ParmError("BRVisibilityCalculator::DefineRank() NbCalc > 4 !");
+  nbcalc_=nbc;
+  calcid_=cid;
+  if (nbcalc_>1) {
+    uint_4 maxnpairs = (2*memgr_.NbFibres()+1)*memgr_.NbFibres();
+    uint_4 npairs=maxnpairs/nbcalc_;
+    if (calcid_==(nbcalc_-1)) 
+      SelectPairs(calcid_*npairs, maxnpairs-calcid_*npairs);
+    else 
+      SelectPairs(calcid_*npairs, npairs);
+    MemZaction mmzas[4]={MemZA_ProcA,MemZA_ProcB,MemZA_ProcC,MemZA_ProcD};
+    SetMemZAction(mmzas[calcid_]);
+  }
+  return ;
+}
+
+/* --Methode-- */
+uint_4 BRVisibilityCalculator::SelectPairs(uint_4 pair1, uint_4 nbpairs)
+{
+  BRPaquet paq(memgr_.PaqSize());
+  uint_4 maxnpairs = (2*memgr_.NbFibres()+1)*memgr_.NbFibres();
+
+  if (pair1 >= maxnpairs)  pair1=maxnpairs-1;
+  if (nbpairs > maxnpairs-pair1)  nbpairs=maxnpairs-pair1;
+  pairst_=pair1;
+  nbpairs_=nbpairs;
+  vismtx_.SetSize(nbpairs_, paq.DataSize()/4); 
+  return nbpairs_;
+}
+
+/* --Methode-- */
+int BRVisibilityCalculator::SelectFreqBinning(uint_4 freq1, uint_4 freq2, uint_4 nbfreq)
+{
+  jf1_=freq1; jf2_=freq2;
   if ((jf1_<1)||(jf1_>=vismtx_.NCols()))  jf1_=1;
   if ((jf2_<1)||(jf2_>=vismtx_.NCols())||(jf2_<jf1_))  jf2_=vismtx_.NCols()-1;
-  if (nbinfreq_<1) nbinfreq_=1;
-  djf_=(jf2_-jf1_)/nbinfreq_;
+  if (nbfreq<1) nbfreq=1;
+  djf_=(jf2_-jf1_)/nbfreq;
   if (djf_<1) djf_=0;
-  xnt_ = new double[vismtx_.NRows()+20];
-  for(int kk=0; kk<vismtx_.NRows(); kk++)  xnt_[kk]=0.;
-
-  cout << " BRVisibilityCalculator/Info NMean= " << nmean_ << " JF1=" << jf1_ 
+  cout << " BRVisibilityCalculator::SelectFreqBinning/Info  JF1=" << jf1_ 
        << " JF2=" << jf2_ << " DJF=" << djf_ << endl;
 
-  fgallfibok=NULL;
-  fgcktt_=false;
-}
-
-/* --Methode-- */
-BRVisibilityCalculator::~BRVisibilityCalculator()
-{
-  cout << " BRVisibilityCalculator - Visibility Datatable : " << endl;
-  cout << visdt_;
-  POutPersist po(outpath_+"visdt.ppf");
-  po << visdt_;
-  POutPersist poc(outpath_+"chanum.ppf");
-  poc << chanum_;
-
-  if (fgcktt_) {
-    cout << " BRVisibilityCalculator -  Check TimeTag done: TotNPaqProc= " << totnbpaq_ << endl;
-    for(size_t fib=0; fib<(size_t)memgr_.NbFibres(); fib++)  {
-      cout << " BRTTCheck-Fiber[" << fib << "] NBadTT=" << vbadtt_[fib] << " NDiffTT>5="
-	   << vndiff5tt_[fib] << "  NotSameTT=" << vnsamett_[fib] << endl; 
-    }
-    POutPersist pott(outpath_+"ttfcmtx.ppf");
-    pott << PPFNameTag("FC") << fcmtx_; 
-    pott << PPFNameTag("TT") << ttmtx_;     
-  }
-  delete[] xnt_;
-}
+}
+
+
+/* --Methode-- */
+int BRVisibilityCalculator::ActivateTimeTagCheck(uint_8 maxnpaq)
+{
+  mindeltatt_=memgr_.PaqSize()/2;
+  if (mindeltatt_<1) mindeltatt_=1;
+  fcmtx_.SetSize(memgr_.NbFibres(), maxnpaq);
+  ttmtx_.SetSize(memgr_.NbFibres(), maxnpaq);
+  vlasttt_.resize(memgr_.NbFibres(), 0);
+  vbadtt_.resize(memgr_.NbFibres(), 0);
+  vnsamett_.resize(memgr_.NbFibres(), 0);
+  vndiff5tt_.resize(memgr_.NbFibres(), 0);
+
+  fgcktt_=true;
+  cout << " BRVisibilityCalculator::ActivateTimeTagCheck() - TT/Fc matrix NCols=" << maxnpaq 
+       << " MinDeltaTT=" << mindeltatt_ << endl;
+
+  return 0;
+}
+
 
 /* --Methode-- */
 void BRVisibilityCalculator::run()
 {
+  cout << " BRVisibilityCalculator[" << calcid_ << "/" << nbcalc_ 
+       << "]::run() - Starting " << " NFibers=" << memgr_.NbFibres() 
+       << " NChan=" << 2*memgr_.NbFibres() << " NPairs=" << nbpairs_ << " First:" << pairst_ << endl;	
+  
   if (nparthr_ < 2)  return BRBaseProcessor::run();
   // Execution multithread parallele
@@ -111,6 +182,4 @@
   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();
@@ -156,6 +225,9 @@
 	  
 	  // ATTENTION : Matrice visibilites non moyennee 
-	  char nfile[32];
-	  sprintf(nfile,"vismtx%d.ppf",numfile_);
+	  char nfile[48];
+	  if (nbcalc_==1) 
+	    sprintf(nfile,"vismtx%d.ppf",numfile_);
+	  else 
+	    sprintf(nfile,"vismtx_%d_%d.ppf",(int)calcid_,numfile_);
 	  string flnm=outpath_+nfile;
 	  POutPersist po(flnm);
@@ -187,5 +259,5 @@
 	  moyfc_ += curfc_[0];
 	  moytt_ += (vpaq_[0].TimeTag()-ttfirst_[0]);
-	  if (fgcktt_)  CheckTimeTag();
+	  if ((fgcktt_)&&(calcid_==0))  CheckTimeTag();
 	  totnbpaq_++;
 	}
@@ -220,23 +292,4 @@
 
 /* --Methode-- */
-int BRVisibilityCalculator::ActivateTimeTagCheck(uint_8 maxnpaq)
-{
-  mindeltatt_=memgr_.PaqSize()/2;
-  if (mindeltatt_<1) mindeltatt_=1;
-  fcmtx_.SetSize(memgr_.NbFibres(), maxnpaq);
-  ttmtx_.SetSize(memgr_.NbFibres(), maxnpaq);
-  vlasttt_.resize(memgr_.NbFibres(), 0);
-  vbadtt_.resize(memgr_.NbFibres(), 0);
-  vnsamett_.resize(memgr_.NbFibres(), 0);
-  vndiff5tt_.resize(memgr_.NbFibres(), 0);
-
-  fgcktt_=true;
-  cout << " BRVisibilityCalculator::ActivateTimeTagCheck() - TT/Fc matrix NCols=" << maxnpaq 
-       << " MinDeltaTT=" << mindeltatt_ << endl;
-
-  return 0;
-}
-
-/* --Methode-- */
 int BRVisibilityCalculator::Process()
 {
@@ -256,6 +309,9 @@
 
       // ATTENTION : Matrice visibilites non moyennee 
-      char nfile[32];
-      sprintf(nfile,"vismtx%d.ppf",numfile_);
+      char nfile[48];
+      if (nbcalc_==1) 
+	sprintf(nfile,"vismtx%d.ppf",numfile_);
+      else 
+	sprintf(nfile,"vismtx_%d_%d.ppf",(int)calcid_,numfile_);
       string flnm=outpath_+nfile;
       POutPersist po(flnm);
@@ -273,4 +329,7 @@
   for(size_t i=0; i<vpdata_.size(); i++) {
     for(size_t j=i; j<vpdata_.size(); j++) {
+      size_t kpair=i*vpdata_.size()+j;
+      if (kpair<pairst_)  continue;
+      if (kpair>=(pairst_+nbpairs_))  break;
       TVector< complex<r_4> > vis = vismtx_.Row(k);   k++;
       for(sa_size_t f=1; f<vis.Size(); f++) {
@@ -283,5 +342,5 @@
   moyfc_ += curfc_[0];
   moytt_ += (vpaq_[0].TimeTag()-ttfirst_[0]);
-  if (fgcktt_)  CheckTimeTag();
+  if ((fgcktt_)&&(calcid_==0))  CheckTimeTag();
   totnbpaq_++;
   return 0;
@@ -312,4 +371,7 @@
     for(size_t i=0; i<vpdata_.size(); i++) {
       for(size_t j=i; j<vpdata_.size(); j++) {
+	size_t kpair=i*vpdata_.size()+j;
+	if (kpair<pairst_)  continue;
+	if (kpair>=(pairst_+nbpairs_))  break;
 	TVector< complex<r_4> > vis = vismtx_.Row(k);   k++;
 	for(sa_size_t f=fdeb; f<ffin; f++) {
@@ -328,14 +390,15 @@
 int BRVisibilityCalculator::FillVisibTable(double fcm, double ttm)
 {
-  xnt_[0]=fcm;  xnt_[1]=ttm/1.25e8;
+  double xnt[10];
+  xnt[0]=fcm;  xnt[1]=ttm/1.25e8;
 
   if (djf_<2) {
     for(sa_size_t rv=0; rv<vismtx_.NRows(); rv++) {
       for(sa_size_t jf=jf1_; jf<jf2_; jf++) {
-	xnt_[2]=jf;
-	xnt_[3]=chanum_(rv);
-	xnt_[4]=vismtx_(rv,jf).real()/(r_4)(nmean_);
-	xnt_[5]=vismtx_(rv,jf).imag()/(r_4)(nmean_);
-	visdt_.AddRow(xnt_);
+	xnt[2]=jf;
+	xnt[3]=chanum_(rv+pairst_);
+	xnt[4]=vismtx_(rv,jf).real()/(r_4)(nmean_);
+	xnt[5]=vismtx_(rv,jf).imag()/(r_4)(nmean_);
+	visdt_.AddRow(xnt);
       }
     }
@@ -352,9 +415,9 @@
 	  moyimag+=vismtx_(rv,jjf).imag();
 	}
-	xnt_[2]=jf+djf_/2;
-	xnt_[3]=chanum_(rv);
-	xnt_[4]=moyreal/(r_4)(nmean_*djf_);
-	xnt_[5]=moyimag/(r_4)(nmean_*djf_);
-	visdt_.AddRow(xnt_);
+	xnt[2]=jf+djf_/2;
+	xnt[3]=chanum_(rv+pairst_);
+	xnt[4]=moyreal/(r_4)(nmean_*djf_);
+	xnt[5]=moyimag/(r_4)(nmean_*djf_);
+	visdt_.AddRow(xnt);
       }
     }
@@ -396,4 +459,46 @@
   }
   return 0;
+}
+
+//-------------------------------------------------------------------------------
+// Classe Groupe (ensemble) de Calculateur de Visibilites, tourant en parallele
+//-------------------------------------------------------------------------------
+
+/* --Methode-- */
+BRVisCalcGroup::BRVisCalcGroup(size_t nbcalc, RAcqMemZoneMgr& memgr, string outpath, uint_4 nmean, size_t nthr)
+{
+  if ((nbcalc<1)||(nbcalc>4)) 
+    throw ParmError("BRVisCalcGroup::BRVisCalcGroup NbCalc > 4 !");
+  for(size_t i=0; i<nbcalc; i++) {
+    BRVisibilityCalculator * viscp=new BRVisibilityCalculator(memgr, outpath, nmean, nthr);
+    viscp->DefineRank(nbcalc, i);
+    viscalcp_.push_back(viscp); 
+  }
+}
+/* --Methode-- */
+BRVisCalcGroup::~BRVisCalcGroup()
+{
+  for(size_t i=0; i<viscalcp_.size(); i++) 
+    delete viscalcp_[i];
+}
+/* --Methode-- */
+int BRVisCalcGroup::SelectFreqBinning(uint_4 freq1, uint_4 freq2, uint_4 nbfreq)
+{
+  int rc=0;
+  for(size_t i=0; i<viscalcp_.size(); i++) 
+    rc=viscalcp_[i]->SelectFreqBinning(freq1, freq2, nbfreq);
+  return rc;
+}
+/* --Methode-- */
+void BRVisCalcGroup::start()
+{
+  for(size_t i=0; i<viscalcp_.size(); i++) 
+    viscalcp_[i]->start();
+}
+/* --Methode-- */
+void BRVisCalcGroup::join()
+{
+  for(size_t i=0; i<viscalcp_.size(); i++) 
+    viscalcp_[i]->join();
 }
 
