Example #1
0
int main()
{
  int numClients = 2;
  
  // client settings
  float minClientActionDelay = 0.2f;	// min wait time for a client before starting new actions
  float maxClientActionDelay = 0.4f;	// max wait time for a client before it has to complete a new action
  float clientQueryWeight = 30.0f;		// possibility of a query to happen
  float clientReplyWeight = 30.0f;		// possibility of a reply to happen
  float clientPostWeight = 40.0f;		// possibility of a new post (update) to happen
  
  // set the global MPI variabes
  gRank = gWorld.rank();
  gNumFE = numClients;
  gNumRM = gWorld.size() - numClients;

  // early out if there are not enough nodes for at least one RM
  if(numClients + 1 > gWorld.size() && gWorld.rank() == 0) {
	  std::cout << "ERROR: there are not enough nodes for at least 1 RM, please increase the number of nodes" << std::endl;
	  exit(-1);
  }

  if (gWorld.rank() == 0) {
      std::cout << " num RM: " << gNumRM << " num FE: " << gNumFE << std::endl;
  }

    Log::singleton().open("Bulletin_" + std::to_string(gRank) + ".log"); // set log file
    Log::singleton().setVerbosity(LV_Normal);

  //the last 'numClients' ranks are front ends
  if (gWorld.rank() >= gWorld.size() - numClients) {
	  std::cout << "P" << gWorld.rank() << ": assigned as a client" << std::endl;

	  // create client instance
	  Client client;

	  // set client variables as defined above
	  client.setMinActionDelay(minClientActionDelay);
	  client.setMaxActionDelay(maxClientActionDelay);
	  client.setQueryWeight(clientQueryWeight);
	  client.setReplyWeight(clientReplyWeight);
	  client.setPostWeight(clientPostWeight);

	  // run the client
	  // the client will now call the Frontend classes specific functions
	  // whenever it wants to complete an action.
	  client.run();
  }
  else
  {
	  std::cout << "P" << gWorld.rank() << ": assigned as a replicator" << std::endl;
      ReplicaManager RM;

      RM.run();
  }

  return 0;
}
Example #2
0
void collect_data(mpi::communicator local, mpi::communicator world)
{
  // The rank of the collector within the world communicator
  int master_collector = world.size() - local.size();

  if (world.rank() == master_collector) {
    while (true) {
      // Wait for a message
      mpi::status msg = world.probe();
      if (msg.tag() == msg_data_packet) {
        // Receive the packet of data
        std::vector<int> data;
        world.recv(msg.source(), msg.tag(), data);

        // Tell each of the collectors that we'll be broadcasting some data
        for (int dest = 1; dest < local.size(); ++dest)
          local.send(dest, msg_broadcast_data, msg.source());

        // Broadcast the actual data.
        broadcast(local, data, 0);
      } else if (msg.tag() == msg_finished) {
        // Receive the message
        world.recv(msg.source(), msg.tag());

        // Tell each of the collectors that we're finished
        for (int dest = 1; dest < local.size(); ++dest)
          local.send(dest, msg_finished);

        break;
      }
    }
  } else {
    while (true) {
      // Wait for a message from the master collector
      mpi::status msg = local.probe();
      if (msg.tag() == msg_broadcast_data) {
        // Receive the broadcast message
        int originator;
        local.recv(msg.source(), msg.tag(), originator);

        // Receive the data broadcasted from the master collector
        std::vector<int> data;
        broadcast(local, data, 0);

        std::cout << "Collector #" << local.rank()
                  << " is processing data from generator #" << originator
                  << "." << std::endl;
      } else if (msg.tag() == msg_finished) {
        // Receive the message
        local.recv(msg.source(), msg.tag());

        break;
      }
    }
  }
}
void TwoParticleGFContainer::computeAll_split(bool clearTerms, const boost::mpi::communicator & comm)
{
    // split communicator
    size_t ncomponents = NonTrivialElements.size();
    size_t ncolors = std::min(int(comm.size()), int(NonTrivialElements.size()));
    RealType color_size = 1.0*comm.size()/ncolors;
    std::map<int,int> proc_colors;
    std::map<int,int> elem_colors;
    std::map<int,int> color_roots;
    bool calc = false;
    for (size_t p=0; p<comm.size(); p++) {
        int color = int (1.0*p / color_size);
        proc_colors[p] = color;
        color_roots[color]=p;
    }
    for (size_t i=0; i<ncomponents; i++) {
        int color = i*ncolors/ncomponents;
        elem_colors[i] = color;
    };

    if (!comm.rank()) {
        INFO("Splitting " << ncomponents << " components in " << ncolors << " communicators");
        for (size_t i=0; i<ncomponents; i++)
            INFO("2pgf " << i << " color: " << elem_colors[i] << " color_root: " << color_roots[elem_colors[i]]);
    };
    comm.barrier();
    int comp = 0;

    boost::mpi::communicator comm_split = comm.split(proc_colors[comm.rank()]);

    for(std::map<IndexCombination4, boost::shared_ptr<TwoParticleGF> >::iterator iter = NonTrivialElements.begin(); iter != NonTrivialElements.end(); iter++, comp++) {
        bool calc = (elem_colors[comp] == proc_colors[comm.rank()]);
        if (calc) {
            INFO("C" << elem_colors[comp] << "p" << comm.rank() << ": computing 2PGF for " << iter->first);
            if (calc) static_cast<TwoParticleGF&>(*(iter->second)).compute(clearTerms, comm_split);
        };
    };
    comm.barrier();
    // distribute data
    if (!comm.rank()) INFO_NONEWLINE("Distributing 2PGF container...");
    comp = 0;
    for(std::map<IndexCombination4, boost::shared_ptr<TwoParticleGF> >::iterator iter = NonTrivialElements.begin(); iter != NonTrivialElements.end(); iter++, comp++) {
        int sender = color_roots[elem_colors[comp]];
        TwoParticleGF& chi = *((iter)->second);
        for (size_t p = 0; p<chi.parts.size(); p++) {
            //    if (comm.rank() == sender) INFO("P" << comm.rank() << " 2pgf " << p << " " << chi.parts[p]->NonResonantTerms.size());
            boost::mpi::broadcast(comm, chi.parts[p]->NonResonantTerms, sender);
            boost::mpi::broadcast(comm, chi.parts[p]->ResonantTerms, sender);
            if (comm.rank() != sender) {
                chi.setStatus(TwoParticleGF::Computed);
            };
        };
    }
    comm.barrier();
    if (!comm.rank()) INFO("done.");
}
Example #4
0
 // Posle pozadavek o praci nahodne vybranemu procesu
 void sendRequest() {
   int pn = rand() % com.size();
   srand(time(NULL));
   
   // Pokud by se nahodne cislo trefilo na moje cislo, generuj nove
   if (pn == com.rank()) {
     pn = rand() % com.size();
   }
   
   //cout << "Sending WORK_REQUEST to " << pn << endl;
   com.send(pn, WORK_REQUEST);
 }
Example #5
0
void DocumentDecoder::manageTranslators(
	boost::mpi::communicator comm,
	NistXmlCorpus &testset
) {
	namespace mpi = boost::mpi;

	mpi::request reqs[2];
	int stopped = 0;

	NumberedOutputDocument translation;
	reqs[0] = comm.irecv(mpi::any_source, TAG_COLLECT, translation);
	reqs[1] = comm.irecv(mpi::any_source, TAG_STOP_COLLECTING);

	NistXmlCorpus::const_iterator it = testset.begin();
	uint docno = 0;
	for(int i = 0; i < comm.size() && it != testset.end(); ++i, ++docno, ++it) {
		LOG(logger_, debug, "S: Sending document " << docno << " to translator " << i);
		comm.send(i, TAG_TRANSLATE, std::make_pair(docno, *(*it)->asMMAXDocument()));
	}

	for(;;) {
		std::pair<mpi::status, mpi::request *> wstat = mpi::wait_any(reqs, reqs + 2);
		if(wstat.first.tag() == TAG_STOP_COLLECTING) {
			stopped++;
			LOG(logger_, debug, "C: Received STOP_COLLECTING from translator "
				<< wstat.first.source() << ", now " << stopped << " stopped translators.");
			if(stopped == comm.size()) {
				reqs[0].cancel();
				return;
			}
			*wstat.second = comm.irecv(mpi::any_source, TAG_STOP_COLLECTING);
		} else {
			LOG(logger_, debug, "C: Received translation of document " <<
				translation.first << " from translator " << wstat.first.source());
			reqs[0] = comm.irecv(mpi::any_source, TAG_COLLECT, translation);
			if(it != testset.end()) {
				LOG(logger_, debug, "S: Sending document " << docno <<
					" to translator " << wstat.first.source());
				comm.send(wstat.first.source(), TAG_TRANSLATE,
					std::make_pair(docno, *(*it)->asMMAXDocument()));
				++docno; ++it;
			} else {
				LOG(logger_, debug,
					"S: Sending STOP_TRANSLATING to translator " << wstat.first.source());
				comm.send(wstat.first.source(), TAG_STOP_TRANSLATING);
			}
			testset[translation.first]->setTranslation(translation.second);
		}
	}
}
Example #6
0
void generate_data(mpi::communicator local, mpi::communicator world)
{
  using std::srand;
  using std::rand;

  // The rank of the collector within the world communicator
  int master_collector = local.size();

  srand(time(0) + world.rank());

  // Send out several blocks of random data to the collectors.
  int num_data_blocks = rand() % 3 + 1;
  for (int block = 0; block < num_data_blocks; ++block) {
    // Generate some random data
    int num_samples = rand() % 1000;
    std::vector<int> data;
    for (int i = 0; i < num_samples; ++i) {
      data.push_back(rand());
    }

    // Send our data to the master collector process.
    std::cout << "Generator #" << local.rank() << " sends some data..."
              << std::endl;
    world.send(master_collector, msg_data_packet, data);
  }

  // Wait for all of the generators to complete
  (local.barrier)();

  // The first generator will send the message to the master collector
  // indicating that we're done.
  if (local.rank() == 0)
    world.send(master_collector, msg_finished);
}
Example #7
0
void
printit(const boost::mpi::communicator& comm,
        const std::vector<T>& things,
        const std::string& caption)
{
    if (!caption.empty() && comm.rank() == 0) {
        std::cout << caption << std::endl;
        std::cout.flush();
    }
    for (int p = 0; p < comm.size(); ++p) {
        if (comm.rank() == p) {
            std::cout << p << ": ";
            std::copy(things.begin(), things.end(),
                      std::ostream_iterator<T>(std::cout, ","));
            std::cout << std::endl;
            std::cout.flush();
        }
        comm.barrier();
    }

    comm.barrier();
    size_t global_size;
    boost::mpi::reduce(comm, things.size(), global_size, std::plus<size_t>(), 0);
    if (comm.rank() == 0) {
        if (!caption.empty()) {
            std::cout << caption;
        }
        std::cout << "Number of things: " << global_size << std::endl;
    }
    comm.barrier();
    std::cout << comm.rank() << ": leaving printit()" << std::endl;
}
Example #8
0
void TwoParticleGF::compute(bool clear, const boost::mpi::communicator & comm)
{
    if (Status < Prepared) throw (exStatusMismatch());
    if (Status >= Computed) return;
    if (!Vanishing) {
        // Create a "skeleton" class with pointers to part that can call a compute method
        pMPI::mpi_skel<ComputeAndClearWrap> skel;
        bool fill_container = m_data_.NBosonic() > 0 && m_data_.NFermionic() > 0; 
        skel.parts.reserve(parts.size());
        for (size_t i=0; i<parts.size(); i++) { 
            skel.parts.push_back(ComputeAndClearWrap(&m_data_, parts[i], clear, fill_container, 1));
            };
        std::map<pMPI::JobId, pMPI::WorkerId> job_map = skel.run(comm, true); // actual running - very costly
        int rank = comm.rank();
        int comm_size = comm.size(); 

        // Start distributing data
        //DEBUG(comm.rank() << getIndex(0) << getIndex(1) << getIndex(2) << getIndex(3) << " Start distributing data");
        comm.barrier();
         

        if (!clear) { 
            for (size_t p = 0; p<parts.size(); p++) {
                boost::mpi::broadcast(comm, parts[p]->NonResonantTerms, job_map[p]);
                boost::mpi::broadcast(comm, parts[p]->ResonantTerms, job_map[p]);
                if (rank == job_map[p]) { 
                    parts[p]->Status = TwoParticleGFPart::Computed;
                     };
                };
            comm.barrier();
        }
    };
    Status = Computed;
}
Example #9
0
void
all_gatherv_test(const mpi::communicator& comm, Generator generator,
                 std::string kind)
{
  typedef typename Generator::result_type value_type;
  using boost::mpi::all_gatherv;

  std::vector<value_type> myvalues, expected, values;
  std::vector<int>        sizes;
  for(int r = 0; r < comm.size(); ++r) {
    value_type value = generator(r);
    sizes.push_back(r+1);
    for (int k=0; k < r+1; ++k) {
      expected.push_back(value);
      if(comm.rank() == r) {
        myvalues.push_back(value);
      }
    }
  }
  if (comm.rank() == 0) {
    std::cout << "Gathering " << kind << "...";
    std::cout.flush();
  }
  
  mpi::all_gatherv(comm, myvalues, values, sizes);
  
  BOOST_CHECK(values == expected);
  
  if (comm.rank() == 0 && values == expected)
    std::cout << "OK." << std::endl;
  
  (comm.barrier)();
}
Example #10
0
void
broadcast_test(const mpi::communicator& comm, const T& bc_value,
               std::string const& kind)
{
  for (int root = 0; root < comm.size(); ++root) {
    broadcast_test(comm, bc_value, kind, root);
  }
}
Example #11
0
    void init(const mpi::communicator& comm)
    {
        PetscErrorCode ierr;
        PetscInt lo, hi;
        ierr = VecCreate(comm,&x); // CHKERRQ(ierr);
        ierr = VecSetSizes(x, PETSC_DECIDE, 5*comm.size()); // CHKERRQ(ierr);
        ierr = VecSetFromOptions(x); // CHKERRQ(ierr);

        ierr = VecGetOwnershipRange(x, &lo, &hi);
        for (PetscInt i = lo; i <= hi; ++i) {
            std::complex<double> v(i, 5*comm.size() - i - 1);
            ierr = VecSetValue(x, i, v, INSERT_VALUES);
        }

        ierr = VecAssemblyBegin(x); // CHKERRQ(ierr);
        ierr = VecAssemblyEnd(x); // CHKERRQ(ierr);
    }
void runMaster(mpi::communicator world, int size, int grid_dimension)
{
  // Start timer and go.
  boost::chrono::system_clock::time_point start = boost::chrono::system_clock::now();

  // Send
  Matrix      A(Size(size, size));
  Matrix result(Size(size, size));

  for(int row = 0; row < A.size.rows; ++row){
    for(int col = 0; col < A.size.cols; ++col){
      A.data[row][col] = (row % 11) + (col % 11);
    }
  }
  //cout << A << endl;
  //cout << "\nProduct:\n" << A*A << endl;

  // Do sequential
  if (grid_dimension == 0)
    A.square(result);

  // Else parallel
  else{
    // Split matrix up and send to slaves
    int slave_id = 1;
    int sub_matrix_sizes = size / grid_dimension;

    for(int i = 0; i < size; i += sub_matrix_sizes){
      for(int j = 0; j < size; j += sub_matrix_sizes){
        MatrixCrossSection cs = getCrossSection( A, i, j, sub_matrix_sizes);
        world.send(slave_id, 0, cs);
        slave_id ++;
      }
    }

    // Recieve
    std::vector<Matrix> saved;
    int num_slaves = world.size() -1;

    for(int i = 1; i <= num_slaves; ++i){
      Matrix r;
      world.recv(i, 0, r);
      result.insertSubMatrix(r);
    }
  }

  // Done
  boost::chrono::duration<double> sec = boost::chrono::system_clock::now() - start;
  cout << sec.count() << endl;

  // Print Result
  //cout << "\nResult:\n" << result << endl;

  //assert ( result == A*A);
}
Example #13
0
 // Poslani zpravy vsem ostatnim procesum 
 void broadcastMessage(int msgType) {
   for (int i = 0; i < com.size(); i++) {
     // Sobe nic neposilam
     if (i == com.rank()) { continue; }
     
     // Pokud jsem nasel vysledek, poslu ho
     if (msgType == FOUND || msgType == FOUND_BEST) {
       //cout << "Sending (BEST)FOUND to " << i << endl;
       com.send(i, msgType, myLongest);
     }
     
     // Pri oznameni konce vypoctu neni treba posilat zadna data
     else if (msgType == END) {
       //cout << "Sending end to " << i << endl;
       com.send(i, msgType);
     }
   }
 }
Example #14
0
void 
random_scattered_vector(const boost::mpi::communicator& comm, 
                        const int& global_size,
                        std::vector<I>& local_values)
{
  int me(comm.rank());
  int nproc(comm.size());

  std::vector< std::vector<I> > toscatter(nproc);

  if (me == 0) {
    for (int i = 0; i < global_size; ++i) {
      boost::random::uniform_int_distribution<> dist(0, nproc-1);
      int p(dist(gen));
      toscatter[p].push_back(i);
    }
  }
  scatter(comm, toscatter, local_values, 0);
}
Example #15
0
static void master(mpi::communicator world){
  int ntasks, rank;  
  vector<int> data;
  int work;
  int result;

   for(int i = 0; i< 10; i++){
    data.push_back(i);
  }

   const int size_work = (int)data.size();

  rank = world.rank(); //int rank(ID) of processor
  ntasks = world.size();//int total number of processors

  for (rank = 1; rank < ntasks; ++rank) {

    get_next_work_item(work,size_work,data); 
   
     world.send(rank,WORKTAG,work);
  }
  
  int ret = get_next_work_item(work,size_work,data);

  while (ret == 0){
    mpi::status status = world.recv(mpi::any_source,mpi::any_tag,result);

      world.send(status.source(),WORKTAG,work);
     
      ret = get_next_work_item(work,size_work,data);
  }

  for (rank = 1; rank < ntasks; ++rank) {
    world.recv( mpi::any_source, mpi::any_tag,result);
  }

  for (rank = 1; rank < ntasks; ++rank) {
    world.send(rank,DIETAG,0);
  }
}
Example #16
0
void
all_gather_test(const mpi::communicator& comm, Generator generator,
                std::string kind)
{
  typedef typename Generator::result_type value_type;
  value_type value = generator(comm.rank());

  std::vector<value_type> values;
  if (comm.rank() == 0) {
    std::cout << "Gathering " << kind << "...";
    std::cout.flush();
  }

  mpi::all_gather(comm, value, values);

  std::vector<value_type> expected_values;
  for (int p = 0; p < comm.size(); ++p)
    expected_values.push_back(generator(p));
  BOOST_CHECK(values == expected_values);
  if (comm.rank() == 0 && values == expected_values)
    std::cout << "OK." << std::endl;

  (comm.barrier)();
}
Example #17
0
pair<Species_tree *,string > sim_init_LL_mpi(string tree_file,string sim_file,int outgroup,
		 const mpi::communicator  world)
{
  //########################## GLOBAL #########################
  string S_tree_string;
  tree_type* S;
  int server = 0;  
  int rank = world.rank();
  int size = world.size();
  scalar_type delta,tau,lambda,omega,delta_var,tau_var,lambda_var,omega_var,noise;
  int N;
  long seed;
  vector < vector <scalar_type> > gather_sim_O_counts;
  vector < vector <scalar_type> > gather_sim_D_counts;
  vector < vector <scalar_type> > gather_sim_T_counts;
  vector < vector <scalar_type> > gather_sim_L_counts;
  vector < vector <scalar_type> > gather_sim_O_profile;
  //########################## GLOBAL #########################

  if (rank==server)
    {
      seed=good_seed();
      cout << "#SEED:" << seed <<endl;
    }
  broadcast(world,seed,server);
  RandomTools::setSeed(seed*rank+rank);
  if (rank == server) 
    {       
      string sim_file_name=sim_file;
      string tree_file_name=tree_file;
      vector <string> forest,codes;
      string line;
      int i=0;
      ifstream file_stream (sim_file_name.c_str());
      scalar_type alpha=0.5;
      scalar_type rho=0.5;
      scalar_type height=0.5;
      noise=0;
      omega=0.5;

      delta_var=0.;
      tau_var=0.;
      lambda_var=0.;
      omega_var=0.;
      N=20;
      if (file_stream.is_open())  //  ########## read sim params ############
	{
	  while (! file_stream.eof() )
	    {
	      getline (file_stream,line);
	      if (line.find("#N")!=line.npos)
		{
		  vector<string> tokens;
		  Tokenize(line,tokens," ");
		  N=atoi(tokens[1].c_str());		    
		}

	      if (line.find("#noise")!=line.npos)
		{
		  vector<string> tokens;
		  Tokenize(line,tokens," ");
		  noise=atof(tokens[1].c_str());		    
		}
	      
	      if (line.find("#ALPHA")!=line.npos)
		{
		  vector<string> tokens;
		  Tokenize(line,tokens," ");
		  alpha=atof(tokens[1].c_str());		    
		}
	      if (line.find("#RHO")!=line.npos)
		{
		  vector<string> tokens;
		  Tokenize(line,tokens," ");
		  rho=atof(tokens[1].c_str());		    
		}
	      if (line.find("#HEIGHT")!=line.npos)
		{
		  vector<string> tokens;
		  Tokenize(line,tokens," ");
		  height=atof(tokens[1].c_str());		    
		}
	      if (line.find("#OMEGA")!=line.npos)
		{
		  vector<string> tokens;
		  Tokenize(line,tokens," ");
		  omega=atof(tokens[1].c_str());		    
		}
	      if (line.find('#VAR')!=line.npos)
		{
		  vector<string> tokens;
		  Tokenize(line,tokens," ");
		  delta_var=atof(tokens[1].c_str());		    
		  tau_var=atof(tokens[1].c_str());		    
		  lambda_var=atof(tokens[1].c_str());		    
		}
	      if (line.find("#OMEGAVAR")!=line.npos)
		{
		  vector<string> tokens;
		  Tokenize(line,tokens," ");
		  omega_var=atof(tokens[1].c_str());		    
		}

	    }
	  file_stream.close();
	}
      delta=(1-alpha)*(rho)*height;
      tau=(alpha)*(rho)*height;
      lambda=(1-rho)*height;
      if (rank==server) cout <<"D,T,L,O,N: "<< delta << " " << tau << " " << lambda << " " << omega << " " << N <<endl;
      if (rank==server) cout <<"Dv,Tv,Lv,Ov,NOISE: "<< delta_var << " " << tau_var << " " << lambda_var << " " << omega_var<<" " << noise<<endl;

      
      string S_string="";
      file_stream.open(tree_file_name.c_str());
      if (file_stream.is_open())  //  ########## read an input S tree ############
	{
	  while (! file_stream.eof() )
	    {
	      getline (file_stream,line);
	      if (line.find('(')!=line.npos && S_string=="")
		{
		  vector<string> tokens;
		  Tokenize(line,tokens," ");
		  for (int i=0;i<tokens.size();i++)
		    if (tokens[i].find('(')!=tokens[i].npos)		
		      S_string=tokens[i];
		}
	    }     
	  file_stream.close();
	}
      //########################## DISTRIBUTE ######################
      S_tree_string=S_string;
    }
  //########################## DISTRIBUTE ######################
  // SERVER CHECK POINT 1 - client trees sent -
  
  broadcast(world, N, server);   
  broadcast(world, delta, server);   
  broadcast(world, tau, server);   
  broadcast(world, lambda, server);   
  broadcast(world, omega, server);   
  broadcast(world, delta_var, server);   
  broadcast(world, tau_var, server);   
  broadcast(world, lambda_var, server);   
  broadcast(world, omega_var, server);   
  broadcast(world, noise, server);   
  
  broadcast(world, S_tree_string, server);   

  //########################## infer_tree init #######################
  
  S = TreeTemplateTools::parenthesisToTree(S_tree_string); 
  tree_type * So=S->clone(); 
  Node * root=So->getRootNode();
  double stem_len=0.1;
  if (outgroup || true)//  ########## add outgroup to root of S tree ############
    {
      Node * new_root = new Node();  
      Node * out_group = new Node();  
      root->addSon(new_root);
      new_root->addSon(out_group);      
      out_group->setDistanceToFather(1.+stem_len);
      out_group -> setName("OUT_GROUP");
      So->rootAt(new_root);
      Node * root=So->getRootNode();
      root->getSon(0)->setDistanceToFather(stem_len);
      root->getSon(1)->setDistanceToFather(stem_len);
    }
  Species_tree * infer_tree = new Species_tree(So);

  infer_tree->init_x();   

  //cout << "rank " << rank << " " << delta << " " << tau << " " << lambda << endl; 
  infer_tree->init_sim(0.1, delta, tau, lambda, delta_var*delta, tau_var*tau, lambda_var*lambda,omega,omega*omega_var,0.);      
  stringstream out;
  out<< rank << "trees.trees";
  ofstream trees_file(out.str().c_str());    
  infer_tree->event_stream=&trees_file;

  vector <string> client_trees;
  vector <string> client_codes;
  vector <string> client_stream;



  if (rank!=server)
    {
      infer_tree->emit_G_trees(N,&client_trees,&client_codes,&client_stream);         
      for (int i=0;i<client_trees.size();i++)
	{
	  if (noise>0)
	    {
	      scalar_type p0 = exp(-noise); 
	      scalar_type p=1;
	      int k=-1;
	      bool stop=false;
	      while(!stop)
		{
		  k++;
		  scalar_type u = RandomTools::giveRandomNumberBetweenZeroAndEntry(1);
		  p*=u;
		  if (p<=p0)
		    stop=true;
		}
	      for (int nni=0;nni<k;nni++)
		{
		  vector <string> allnnis=all_NNIs(client_trees[i]);
		  client_trees[i]=allnnis[RandomTools::giveIntRandomNumberBetweenZeroAndEntry(allnnis.size())];
		}
	    }
	}
      infer_tree->unrooted_register(client_trees);  
      for (int i=0;i<client_trees.size();i++)
	infer_tree->codes[client_trees[i]]=client_codes[i];  

    }
  //########################## infer_tree init #######################
  if (noise==0)
    {
      infer_tree->world=world;
      infer_tree->branchwise=1;
      infer_tree->count_labels(client_trees);  
      scalar_type pea_count = sum_counts(infer_tree,world);
      for (int i = 0;i<infer_tree->N_slices+infer_tree->tree->getNumberOfLeaves();i++)
	{
	  infer_tree->sim_O_count[i]=infer_tree->O_counts[i];
	  infer_tree->sim_D_count[i]=infer_tree->D_counts[i];
	  infer_tree->sim_T_count[i]=infer_tree->T_counts[i];
	  infer_tree->sim_L_count[i]=infer_tree->branch_zero[i];
	  infer_tree->sim_branch_sum[i]=infer_tree->branch_sum[i];
	  infer_tree->sim_branch_count[i]=infer_tree->branch_count[i];
	  infer_tree->sim_branch_genome_size[i]=infer_tree->branch_genome_size[i];
	  
	}
    }
  vector < vector <string> > gather_trees;
  gather(infer_tree->world, infer_tree->in_trees, gather_trees, server);    
  if (rank==server)
    {
      vector <string> forest;
      //GG
	for (int j=1;j<size;j++)
	  for (vector<string>::iterator tit=gather_trees[j].begin();tit!=gather_trees[j].end();tit++)
	    forest.push_back((*tit));
	infer_tree->panj_string= PANJ(S_tree_string,forest);
	cout << "#PANJ "<< infer_tree->panj_string <<endl;
	cout << "#PANJ "<< TreeTools::robinsonFouldsDistance(*TreeTemplateTools::parenthesisToTree(S_tree_string),*TreeTemplateTools::parenthesisToTree(infer_tree->panj_string)) << endl;
      }

  pair<Species_tree *,string > return_pair;
  return_pair.first=infer_tree;      
  return_pair.second=S_tree_string;       
  return return_pair;
  //########################## CLIENT #########################

}
Example #18
0
scalar_type LL_mpi(Species_tree * infer_tree,string Sstring,string mode,bool outgroup,scalar_type delta,scalar_type tau, scalar_type lambda)
{ 

  const mpi::communicator  world = infer_tree->world;
  int server = 0;  
  int rank = world.rank();
  int size = world.size();
  scalar_type ll;
  scalar_type return_ll; 

  
  if (rank==server) cout << mode << " " << Sstring <<endl; 

  broadcast(world, Sstring,server);
  tree_type * S=TreeTemplateTools::parenthesisToTree(Sstring);

  if (outgroup)
    {
      Node * root=S->getRootNode();
      
      vector <Node*> nodes=S->getNodes();
      for (vector<Node * > :: iterator it=nodes.begin();it!=nodes.end();it++)
	if ((*it)->hasFather())
	  if ((*it)->getDistanceToFather()==0)
	    (*it)->setDistanceToFather(0.001);
      
      infer_tree->node_makeclocklike_height((S->getRootNode()));    
      S->scaleTree(1./TreeTemplateTools::getDistanceBetweenAnyTwoNodes(*root,*(S->getLeaves()[0])));      
      double stem_len=0.1;
      Node * new_root = new Node();  
      Node * out_group = new Node();  
      root->addSon(new_root);
      new_root->addSon(out_group);      
      out_group->setDistanceToFather(1.+stem_len);
      out_group -> setName("OUT_GROUP");
      S->rootAt(new_root);
      root=S->getRootNode();
      if (root->getSon(0)->isLeaf())
	root->getSon(1)->setDistanceToFather(stem_len);
      else
	root->getSon(0)->setDistanceToFather(stem_len);
      //root->getSon(1)->setDistanceToFather(stem_len);
      infer_tree->reconstruct(S);      
    }
  vector <string> modes;
  Tokenize(mode,modes,"_");
  if (modes[0]=="clock" )
    {
      
      infer_tree->intp=0;
      infer_tree->w=0.;
      infer_tree->reset();
      infer_tree->branchwise=0;      
      string clock_string=Sstring;
      if (rank==server) cout << modes[0] << " " << modes[1] <<endl; 
      clock_string=DTL_clock(clock_string,infer_tree,modes[1]);
      //ll=LL_mpi(infer_tree, clock_string, "Hest",outgroup,delta,tau, lambda);
      ll=infer_tree->tmp_ll;
      infer_tree->tmp_ll=ll;
      delete S;
      if (rank==server) cout <<"$$  "<< clock_string <<endl; 
      infer_tree->tmp_Sstring=clock_string;
      infer_tree->tmp_ll=ll;
      return ll;
    }
  if (mode=="Nest" )
    {
      infer_tree->intp=0;
      infer_tree->w=0.;
      infer_tree->reset(delta,tau,lambda);
      infer_tree->branchwise=0;      
      ll=oLL(infer_tree,S,delta,tau,lambda,1,"estimate");
      //ll=oLL(infer_tree,S,delta,tau,lambda,1,"intbck");
      infer_tree->omega_avg=infer_tree->branchwise_omega[0]+infer_tree->branchwise_omega[1]+infer_tree->branchwise_omega[2];
      delete S;
      infer_tree->tmp_ll=ll;
      return ll;
    }
  if (mode=="noestimate" )
    {
      ll= oLL(infer_tree,S,delta,tau,lambda,1,"noestimate") ;
      delete S;
      return ll;
    }
  if (mode=="Pest" )
    {
      infer_tree->intp=0.9;
      infer_tree->w=0.;
      // !! no T
      infer_tree->reset();
      infer_tree->branchwise=0;      
      ll=oLL(infer_tree,S,delta,tau,lambda,1,"intbck");
      scalar_type pea_count = sum_counts(infer_tree,world);
      delete S;
      infer_tree->tmp_ll=ll;
      return ll;
    }
  if (mode=="Test" )
    {
      infer_tree->intp=0.9;
      infer_tree->w=0.;
      // !! no T
      infer_tree->reset();
      infer_tree->branchwise=0;      
      ll=oLL(infer_tree,S,delta,tau,lambda,1,"intbck");
      scalar_type pea_count = sum_counts(infer_tree,world);
      delete S;
      scalar_type Tll=0;
      for (int i = 0;i<infer_tree->N_slices+infer_tree->tree->getNumberOfLeaves()-1;i++)
	Tll+=infer_tree->T_counts[i];      
      infer_tree->tmp_ll=-Tll;
      return -Tll;
    }

  if (mode=="FASTest" )
    {
      infer_tree->intp=0.9;
      infer_tree->w=0.;
      // !! no T
      infer_tree->reset();
      infer_tree->branchwise=0;      
      ll=oLL(infer_tree,S,delta,tau,lambda,1,"intbck");
      scalar_type pea_count = sum_counts(infer_tree,world);
      infer_tree->apparent_rates(pea_count,true,true);

      //if (rank==server) cout<<"#p "<< ll  << " " << " "<< infer_tree->delta_avg << " " << infer_tree->tau_avg << " " << infer_tree->lambda_avg<< " " << infer_tree->omega_avg << endl ;
      ll=oLL(infer_tree,S,delta,tau,lambda,1,"estimate");
      infer_tree->tmp_ll=ll;
      infer_tree->omega_avg=infer_tree->branchwise_omega[0]+infer_tree->branchwise_omega[1]+infer_tree->branchwise_omega[2];
      delete S;
      return ll;
    }
  
  if (mode=="Hest" || mode=="IHest")
    {
      if (mode=="Hest")
	infer_tree->nclasses=5;
      
      // !! no T
      //tau=1e-30;
      //infer_tree->reset(0.01,1e-30,0.01);
      infer_tree->reset();
      infer_tree->intp=0.9;
      infer_tree->w=0.;
      infer_tree->branchwise=0;      	  
      ll=oLL(infer_tree,S,delta,tau,lambda,1,"intbck");
      infer_tree->tmp_ll=ll;
      scalar_type pea_count = sum_counts(infer_tree,world);
      if (rank==server) cout<<"#p "<< ll  << " " << " "<< infer_tree->delta_avg << " " << infer_tree->tau_avg << " " << infer_tree->lambda_avg<< " " << infer_tree->omega_avg <<" "<<pea_count << endl ;

      infer_tree->apparent_rates(pea_count,mode=="Hest",true);	
      
      //if (rank==server) infer_tree->print_rates();

      // ### sum events       
      scalar_type ll2=ll-2;
      while (ll>ll2 && abs(ll-ll2)>1 )
	{
	  ll2=ll;
	  // given rates estimate p_omega
	  ll=oLL(infer_tree,S,delta,tau,lambda,1,"estimate");
	  infer_tree->tmp_ll=ll;	 
	  if (ll2>ll)
	    {
	      infer_tree->recall_rates();	  
	      //scalar_type re_ll=oLL(infer_tree,S,delta,tau,lambda,1,"noestimate") ;	  
	      ll=ll2;
	      break;
	    }	  
	  infer_tree->omega_avg=infer_tree->branchwise_omega[0]+infer_tree->branchwise_omega[1]+infer_tree->branchwise_omega[2];
	  // given p_omega estimate rates
	  // formal position of remember 
	  oLL(infer_tree,S,delta,tau,lambda,1,"intbck");
	  scalar_type pea_count = sum_counts(infer_tree,world);
	  infer_tree->apparent_rates(pea_count,mode=="Hest",true);
	  
	  if (rank==server) cout<<"#e "<< ll  << " " << " "<< infer_tree->delta_avg << " " << infer_tree->tau_avg << " " << infer_tree->lambda_avg<< " " << infer_tree->omega_avg << endl ;
	  
	  // ### sum events      
	}
      return_ll=ll;
      infer_tree->tmp_ll=ll;
      delete S;
      return return_ll;
    }
}
Example #19
0
pair<Species_tree *,string > init_LL_mpi(string tree_file,string forest_file,int outgroup,
		 const mpi::communicator  world)
{
  //########################## GLOBAL #########################
  tree_type * S;
  string S_tree_string;
  string panj_string;
  int server = 0;  
  int rank = world.rank();
  int size = world.size();
  vector <string> client_trees;
  vector <string> client_codes;
  vector < vector <string> > scatter_trees;
  vector < vector <string> > scatter_codes;
  //########################## GLOBAL #########################

  if (rank == server) 
    { 
    //########################## SERVER #########################
      string forest_file_name=forest_file;
      string tree_file_name=tree_file;
      vector <string> forest,codes;
      string line;
      int i=0;
      ifstream file_stream (forest_file_name.c_str());
      if (file_stream.is_open())  //  ########## read a forest ############
	{
	  while (! file_stream.eof() )
	    {
	      getline (file_stream,line);
	      if (line.find('(')!=line.npos)
		{
		  i++;
		  forest.push_back(line);
		}
	      else if (line.find('#')!=line.npos)
		codes.push_back(line);
	    }
	  file_stream.close();
	}
      string S_string="";
      file_stream.open(tree_file_name.c_str());
      if (file_stream.is_open())  //  ########## read an input S tree ############
	{
	  while (! file_stream.eof() )
	    {
	      getline (file_stream,line);
	      if (line.find('(')!=line.npos && S_string=="")
		{
		  vector<string> tokens;
		  Tokenize(line,tokens," ");
		  for (int i=0;i<tokens.size();i++)
		    if (tokens[i].find('(')!=tokens[i].npos)		
		      S_string=tokens[i];
		}
	    }     
	  file_stream.close();
	}
      //########################## DISTRIBUTE ######################
      /// SAMPLE
      vector <int> lottery;
      for (int i=0;i<forest.size();i++)
	lottery.push_back(i);
      vector <int> winners;
      winners.resize(forest.size()/10.);
      RandomTools::getSample(lottery,winners);
      vector<string> sample_forest;
      vector<string> sample_codes;
      for (int i=0;i<winners.size();i++)
	{
	  int j=winners[i];
	  sample_forest.push_back(forest[j]);
	  sample_codes.push_back(codes[j]);
	}
      forest.clear();
      codes.clear();
      for (int i=0;i<winners.size();i++)
	{
	  forest.push_back(sample_forest[i]);
	  codes.push_back(sample_codes[i]);
	}
      /// SAMPLE

      S_tree_string=S_string;
      panj_string= PANJ(S_string,forest);
      cout << "#PANJ "<<panj_string <<endl;
      cout << "#PANJ "<< TreeTools::robinsonFouldsDistance(*TreeTemplateTools::parenthesisToTree(S_string),*TreeTemplateTools::parenthesisToTree(panj_string)) << endl;

      map <scalar_type,string> sort_trees;
      map <scalar_type,string> sort_codes;
      
      for (int i=0;i<forest.size();i++)
	{
	  tree_type * S=TreeTemplateTools::parenthesisToTree(forest[i]);
	  scalar_type treesize=S->getNumberOfLeaves();
	  while ( sort_trees.count(treesize) )
	    treesize+=RandomTools::giveRandomNumberBetweenZeroAndEntry(1e-2);
	  sort_trees[treesize]=forest[i];
	  sort_codes[treesize]=codes[i];
	  delete S;
	  
	}
      int new_i=0;
      for ( map <scalar_type,string> ::iterator it=sort_trees.begin();it!=sort_trees.end();it++)
	{
	  forest[new_i]=it->second;
	  codes[new_i]=sort_codes[it->first];
	  new_i+=1;
	}
      map <int,int> large_trees;
      for (int i=0;i<forest.size();i++)
	{
	  tree_type * S=TreeTemplateTools::parenthesisToTree(forest[i]);
	  if  (S->getNumberOfLeaves()>150)
	    {
	      large_trees[i]=S->getNumberOfLeaves();
	    }
	  //large_trees[S->getNumberOfLeaves()]++;
	  delete S;
	}



      //int cum=0;
      //for ( map <int,int> ::iterator it=large_trees.begin();it!=large_trees.end();it++)
      //	{
      //	  cum+=(*it).second;
      //	  cout << (*it).first << " " <<  (*it).second << " " << cum << endl;
      //	}
      map <int,int> scatter_sizes;
      for (int j=0;j<size;j++)
	{ 
	  vector <string> tmp;
	  scatter_trees.push_back(tmp);
	  vector <string> tmp2;
	  scatter_codes.push_back(tmp2);
	}
      //cout << "distribute" <<endl;
      
      for (int i=0;i<forest.size();i++)
	{
	  int j = i%(size-1);
	  if (large_trees.count(i)==0)
	    {
	      scatter_trees[j+1].push_back(forest[i]);
	      scatter_codes[j+1].push_back(codes[i]);
	      tree_type * S=TreeTemplateTools::parenthesisToTree(forest[i]);
	      scatter_sizes[j]+=S->getNumberOfLeaves();
	      delete S;
	    }
	  
	}
      for ( map <int,int> ::iterator it=scatter_sizes.begin();it!=scatter_sizes.end();it++)
	cout << it->first << " " << it->second << endl;
      //cout << "distribute" <<endl;

      //.optimization could come here
      //########################## DISTRIBUTE ######################

      // SERVER CHECK POINT 1 - client trees sent -
      broadcast(world, S_tree_string, server);   
      broadcast(world, panj_string, server);   
      
      scatter(world, scatter_trees, client_trees, server);
      scatter(world, scatter_codes, client_codes, server);

      //########################## count_tree init #######################
      S = TreeTemplateTools::parenthesisToTree(S_tree_string); 
      tree_type * So=S->clone(); 
      Node * root=So->getRootNode();
      double stem_len=0.1;
      //!!
      if (outgroup || true)//  ########## add outgroup to root of S tree ############
	{
	  Node * new_root = new Node();  
	  Node * out_group = new Node();  
	  root->addSon(new_root);
	  new_root->addSon(out_group);      
	  out_group->setDistanceToFather(1.+stem_len);
	  out_group -> setName("OUT_GROUP");
	  So->rootAt(new_root);
	  Node * root=So->getRootNode();
	  if (root->getSon(0)->isLeaf())
	    root->getSon(1)->setDistanceToFather(stem_len);
	  else
	    root->getSon(0)->setDistanceToFather(stem_len);
	}
      Species_tree * count_tree = new Species_tree(So);
      count_tree->panj_string=panj_string;

      count_tree->init_x();   
      //########################## count_tree init #######################
      // SERVER CHECK POINT 1 - client trees sent -
      count_tree->world=world;
      pair<Species_tree *,string > return_pair;
      return_pair.first=count_tree;      
      return_pair.second=S_tree_string;       
      return return_pair;
  //########################## SERVER #########################
    }
  else
    {
  //########################## CLIENT #########################

      // CLIENT CHECK POINT 1 - client trees recived -
      broadcast(world, S_tree_string, server);   
      broadcast(world, panj_string, server);   

      scatter(world, scatter_trees, client_trees, server);
      scatter(world, scatter_codes, client_codes, server);

          //########################## infer_tree init #######################

      S = TreeTemplateTools::parenthesisToTree(S_tree_string); 
      tree_type * So=S->clone(); 
      Node * root=So->getRootNode();
      double stem_len=0.1;
      if (outgroup || true)//  ########## add outgroup to root of S tree ############
	{
	  Node * new_root = new Node();  
	  Node * out_group = new Node();  
	  root->addSon(new_root);
	  new_root->addSon(out_group);      
	  out_group->setDistanceToFather(1.+stem_len);
	  out_group -> setName("OUT_GROUP");
	  So->rootAt(new_root);
	  Node * root=So->getRootNode();
	  root->getSon(0)->setDistanceToFather(stem_len);
	  root->getSon(1)->setDistanceToFather(stem_len);
	}
      Species_tree * infer_tree = new Species_tree(So);
      infer_tree->panj_string=panj_string;     
      infer_tree->init_x();   
      infer_tree->unrooted_register(client_trees);  
      
      for (int i=0;i<client_trees.size();i++)
	infer_tree->codes[client_trees[i]]=client_codes[i];
  
      //########################## infer_tree init #######################
      // CLIENT CHECK POINT 1 - client trees recived -
      infer_tree->world=world;
      pair<Species_tree *,string > return_pair;
      return_pair.first=infer_tree;      
      return_pair.second=S_tree_string;       
      return return_pair;
      //########################## CLIENT #########################
    }
}
Example #20
0
MCCResults mccrun_master(
  const Options& opts, const Eigen::VectorXd& vpar, unsigned int num_bins,
  const set<observables_t>& obs, const mpi::communicator& mpicomm )
{
  cout << "========== NEW MONTE CARLO CYCLE ==========" << endl;
  cout << ":: Preparing the simulation" << endl;

  HubbardModelVMC model = prepare_model( opts, vpar, mpicomm );

  vector< unique_ptr<Observable> > obscalc = prepare_obscalcs( obs, opts );
  ObservableCache obscache;

  unsigned int finished_workers = 0;
  unsigned int scheduled_bins = 0;
  unsigned int completed_bins = 0;
  unsigned int enqueued_bins  = num_bins;

  // define procedure to query the slaves for new work requests
  function<void()> mpiquery_work_requests( [&]() {
    while ( boost::optional<mpi::status> status
            = mpicomm.iprobe( mpi::any_source, MSGTAG_S_M_REQUEST_BINS ) ) {
      // receive the request and hand out new bins to the source
      mpicomm.recv( status->source(), MSGTAG_S_M_REQUEST_BINS );
      if ( enqueued_bins > 0 ) {
        mpicomm.send( status->source(), MSGTAG_M_S_DISPATCHED_BINS, 1 );
        scheduled_bins += 1;
        enqueued_bins  -= 1;
      } else {
        mpicomm.send( status->source(), MSGTAG_M_S_DISPATCHED_BINS, 0 );
        ++finished_workers;
      }
    }
  } );

  // define procedure to query the slaves for finished work
  function<void()> mpiquery_finished_work( [&]() {
    while ( boost::optional<mpi::status> status
            = mpicomm.iprobe( mpi::any_source, 2 ) ) {
      mpicomm.recv( status->source(), 2 );
      --scheduled_bins;
      ++completed_bins;
    }
  } );

  cout << ":: Equilibrating the system" << endl;

  for (
    unsigned int mcs = 0;
    mcs < opts["calc.num-mcs-equil"].as<unsigned int>();
    ++mcs ) {
    // take care of the slaves
    mpiquery_finished_work();
    mpiquery_work_requests();

    // perform a Monte Carlo step
    model.mcs();
  }

  unsigned int completed_bins_master = 0;

  cout << ":: Performing Monte Carlo cycle" << endl;
  cout << endl;
  cout << "   Progress:" << endl;

  while ( enqueued_bins > 0 ) {

    cout << '\r' << "     Bin "
         << completed_bins << "/" << num_bins;
    cout.flush();

    --enqueued_bins;
    ++scheduled_bins;

    for (
      unsigned int mcs = 0;
      mcs < opts["calc.num-binmcs"].as<unsigned int>();
      ++mcs ) {
      // take care of the slaves
      mpiquery_finished_work();
      mpiquery_work_requests();

      // perform a Monte Carlo step
      model.mcs();

      // measure observables
      for ( const unique_ptr<Observable>& o : obscalc ) {
        o->measure( model, obscache );
      }
      obscache.clear();
    }

    // tell the observables that a bin has been completed
    for ( const unique_ptr<Observable>& o : obscalc ) {
      o->completebin();
    }

    --scheduled_bins;
    ++completed_bins_master;
    ++completed_bins;
  }
  ++finished_workers;

  while ( completed_bins != num_bins ||
          static_cast<int>( finished_workers ) < mpicomm.size() ) {
    if ( boost::optional<mpi::status> status
         = mpicomm.iprobe( mpi::any_source, MSGTAG_S_M_FINISHED_BINS ) ) {
      mpicomm.recv( status->source(), MSGTAG_S_M_FINISHED_BINS );
      --scheduled_bins;
      ++completed_bins;

      cout << '\r' << "     Bin " << completed_bins << "/" << num_bins;
      cout.flush();
    }

    if ( boost::optional<mpi::status> status
         = mpicomm.iprobe( mpi::any_source, MSGTAG_S_M_REQUEST_BINS ) ) {
      // receive the request for more work
      mpicomm.recv( status->source(), MSGTAG_S_M_REQUEST_BINS );
      // tell him there is no more work
      mpicomm.send( status->source(), MSGTAG_M_S_DISPATCHED_BINS, 0 );
      ++finished_workers;
    }
  }
  assert( enqueued_bins == 0 );
  assert( scheduled_bins == 0 );

  cout << '\r' << "     Bin " << completed_bins << "/" << num_bins << endl;
  cout.flush();

  // check for floating point precision problems

  cout << endl;
  cout << "   Floating point precision control" << endl;

  vector<FPDevStat> W_devstats;
  assert( mpicomm.rank() == 0 );
  mpi::gather( mpicomm, model.get_W_devstat(), W_devstats, 0 );
  FPDevStat W_devstat_combined =
    accumulate(
      W_devstats.begin(), W_devstats.end(),
      FPDevStat( opts["fpctrl.W-deviation-target"].as<double>() )
    );
  cout << "     W: " << W_devstat_combined.recalcs
       << "/" << W_devstat_combined.misses
       << "/" << W_devstat_combined.mag1_misses << endl;

  vector<FPDevStat> T_devstats;
  assert( mpicomm.rank() == 0 );
  mpi::gather( mpicomm, model.get_T_devstat(), T_devstats, 0 );
  FPDevStat T_devstat_combined =
    accumulate(
      T_devstats.begin(), T_devstats.end(),
      FPDevStat( opts["fpctrl.T-deviation-target"].as<double>() )
    );
  cout << "     T: " << T_devstat_combined.recalcs
       << "/" << T_devstat_combined.misses
       << "/" << T_devstat_combined.mag1_misses << endl;

  if ( W_devstat_combined.mag1_misses > 0 ||
       T_devstat_combined.mag1_misses > 0 ) {
    cout << "   Precision targets missed by more than an order of magnitude!" << endl
         << "   WARNING: Your results might be unreliable!!!" << endl << endl;
  } else if ( W_devstat_combined.misses > 0 ||
              T_devstat_combined.misses > 0 ) {
    cout << "   Some precision targets were missed, but your results should be fine."
         << endl << endl;
  } else {
    cout << "   No missed precision targets." << endl << endl;
  }

  // collect results from the slaves and return results the scheduler
  MCCResults results;
  for ( const unique_ptr<Observable>& o : obscalc ) {
    o->collect_and_write_results( mpicomm, results );
  }
  results.success = true;
  return results;
}
Example #21
0
string clock_root(Species_tree * infer_tree,string Sstring,string mode)
{

  const mpi::communicator  world = infer_tree->world;
  int server = 0;  
  int mpi_rank = world.rank();
  int size = world.size();
  broadcast(world, Sstring,server);
  tree_type * S=TreeTemplateTools::parenthesisToTree(Sstring);

  map < Node*,int > do_01;
  map < Node*,int > do_12;
  map < Node*,int > do_02;
  map < pair <Node*,Node*>,int > dnodes;
  vector< pair <Node*,Node*> > seen;
  map < pair <node_type*,node_type*>, pair <node_type*,node_type*> > root_dnodes;
  map < pair <Node*,Node*>,int > roots;

  S->unroot();
  vector <Node *> S_leaves=S->getLeaves();
  vector<Node*>::iterator it,jt;
  vector <pair <Node*,Node*> > roots_vec;

  for (it = S_leaves.begin(); it!=S_leaves.end(); it++ )
    {
      Node * node = (*it);
      Node * head = node->getFather();
      if (S_leaves.size()==2)
	if (S_leaves[0]==(*it))
	  head=S_leaves[1];
	else
	  head=S_leaves[0];
      pair <Node*,Node*> tmp_pair(node,head);
      if (node->getId()>head->getId())
	{tmp_pair.first=head;tmp_pair.second=node;}
      roots[tmp_pair]++;		  
      if (roots[tmp_pair]==2)
	{
	  Node* a_root=new Node();
	  //a_root->setName(tmp_pair.first->getName() + " -0- " + tmp_pair.second->getName());
	  pair <Node*,Node*> root_dnode(a_root,a_root);
	  pair <Node*,Node*> left_pair(tmp_pair.first,tmp_pair.second);
	  pair <Node*,Node*> right_pair(tmp_pair.second,tmp_pair.first);
	  root_dnodes[root_dnode]=tmp_pair;
	  roots_vec.push_back(root_dnode);

	}
      vector <Node*> neigbours = head->getSons();
      if (head->hasFather())
	neigbours.push_back(head->getFather());
      int node_id;
      if (node==neigbours[0])
	{
	  node_id=0;
	  do_01[head]++;
	  do_02[head]++;	
	}
      else if (node==neigbours[1])
	{
	  node_id=1;
	  do_01[head]++;
	  do_12[head]++;	
	}
      else if (node==neigbours[2])
	{
	  node_id=2;       	
	  do_12[head]++;
	  do_02[head]++;	
	}
      pair <Node*,Node*> left_pair(node,head);
      pair <Node*,Node*> right_pair(node,head);     
      if ((node_id==0 || node_id==1) && do_01[head]==2)
	{
	  pair <Node*,Node*> head_pair(head,neigbours[2]);
	  if (node_id==0)
	    right_pair.first=neigbours[1];
	  else 
	    right_pair.first=neigbours[0];
	}
      if ((node_id==1 || node_id==2) && do_12[head]==2)
	{
	  pair <Node*,Node*> head_pair(head,neigbours[0]);
	  if (node_id==1)
	    right_pair.first=neigbours[2];
	  else 
	    right_pair.first=neigbours[1];
	}
      if ((node_id==0 || node_id==2) && do_02[head]==2)
	{
	  pair <Node*,Node*> head_pair(head,neigbours[1]);
	  if (node_id==0)
	    right_pair.first=neigbours[2];
	  else 
	    right_pair.first=neigbours[0];
	}
            
      for (jt=neigbours.begin();jt!=neigbours.end();jt++)
	if (node!=(*jt))
	  {
	    pair <Node*,Node*> dnode (head,(*jt));
	    dnodes[dnode]++;
	  }     
      neigbours.clear(); 
    }

  while(1)
    {
      for (map < pair <Node*,Node*>,int >::iterator dit=dnodes.begin(); dit!=dnodes.end(); dit++)
	if ((*dit).second==2)
	  {	    
	    Node * node = (*dit).first.first;
	    Node * head = (*dit).first.second;
	    pair <Node*,Node*> tmp_pair(node,head);
	    if (node->getId()>head->getId())
	      {tmp_pair.first=head;tmp_pair.second=node;}
	    roots[tmp_pair]++;	
	    if (roots[tmp_pair]==2)
	      {
		Node* a_root=new Node();
		//a_root->setName(tmp_pair.first->getName() + " -0- " + tmp_pair.second->getName());
		pair <Node*,Node*> root_dnode(a_root,a_root);
		pair <Node*,Node*> left_pair(tmp_pair.first,tmp_pair.second);
		pair <Node*,Node*> right_pair(tmp_pair.second,tmp_pair.first);
		root_dnodes[root_dnode]=tmp_pair;
		roots_vec.push_back(root_dnode);
	      }
	    vector <Node*> neigbours = head->getSons();
	    if (head->hasFather())
	      neigbours.push_back(head->getFather());
	    int node_id;
	    if (node==neigbours[0])
	      {
		node_id=0;
		do_01[head]++;
		do_02[head]++;	
	      }
	    else if (node==neigbours[1])
	      {
		node_id=1;
		do_01[head]++;
		do_12[head]++;	
	      }
	    else if (node==neigbours[2])
	      {
		node_id=2;       	
		do_12[head]++;
		do_02[head]++;	
	      }
	    pair <Node*,Node*> left_pair(node,head);
	    pair <Node*,Node*> right_pair(node,head);     
	    if ((node_id==0 || node_id==1) && do_01[head]==2)
	      {
		pair <Node*,Node*> head_pair(head,neigbours[2]);
		if (node_id==0)
		  right_pair.first=neigbours[1];
		else 
		  right_pair.first=neigbours[0];
	      }
	    if ((node_id==1 || node_id==2) && do_12[head]==2)
	      {
		pair <Node*,Node*> head_pair(head,neigbours[0]);
		if (node_id==1)
		  right_pair.first=neigbours[2];
		else 
		  right_pair.first=neigbours[1];
	      }
	    if ((node_id==0 || node_id==2) && do_02[head]==2)
	      {
		pair <Node*,Node*> head_pair(head,neigbours[1]);
		if (node_id==0)
		  right_pair.first=neigbours[2];
		else 
		  right_pair.first=neigbours[0];
	      }      
	    for (jt=neigbours.begin();jt!=neigbours.end();jt++)
	      if (node!=(*jt))
		{
		  pair <Node*,Node*> dnode (head,(*jt));
		  dnodes[dnode]++;
		}
	    seen.push_back((*dit).first);
	  }
      if (seen.size()==0)
	break;
      for (vector< pair <Node*,Node*> >::iterator sit=seen.begin();sit!=seen.end();sit++)
	dnodes.erase((*sit));
      seen.clear();
    }


  
  int root_i=0;

  string max_S;
  scalar_type max_ll=-1e30;
  for (map < pair <node_type*,node_type*>, pair <node_type*,node_type*> >::iterator rt=root_dnodes.begin();rt!=root_dnodes.end();rt++)
    {
      root_i+=1;
      Node* head=(*rt).second.first;
      Node* node=(*rt).second.second;
      vector <Node*> head_neigbours = head->getSons();
      if (head->hasFather())
	head_neigbours.push_back(head->getFather());
      vector <Node*> node_neigbours = node->getSons();
      if (head->hasFather())
	node_neigbours.push_back(node->getFather());
      int node_id=-1;
      if (node==head_neigbours[0])
	node_id=0;
      else if (node==head_neigbours[1])
	node_id=1;
      else if (node==head_neigbours[2])
	node_id=2;
      int did;
      Node * new_root = new Node();
      new_root->setName("R");
      if (( node_id==2 && head->hasFather() ) || head->isLeaf())
	{
	  did=1;
	  node->removeSon(head);
	  node->addSon(new_root);
	  new_root->addSon(head);
	  new_root->setDistanceToFather(1);
	  head->setDistanceToFather(1);

	}
      else
	{
	  did=0;
	  head->removeSon(node);
	  head->addSon(new_root);
	  new_root->addSon(node);
	  new_root->setDistanceToFather(1);
	  node->setDistanceToFather(1);

	}
      S->rootAt(new_root);
      //stringstream out;
      //out << root_i;
      //string fname=forest_file_name+".root"+out.str();
      //tree_stream.open(fname.c_str());
      vector <node_type*> tmp=S->getNodes();
      for (vector <node_type*>::iterator it=tmp.begin();it!=tmp.end();it++)
	if ((*it)->hasFather())
	{	 
	  scalar_type d=(*it)->getDistanceToFather();
	  (*it)->setDistanceToFather(1);
	}
      
      string clock_S =DTL_clock(TreeTemplateTools::treeToParenthesis(*S),infer_tree);
      scalar_type ll=LL_mpi(infer_tree,clock_S,mode);
      if (max_ll<ll)
	{
	  max_S=clock_S;
	  max_ll=ll;
	  }
      //tree_stream<<TreeTemplateTools::treeToParenthesis(*S);     
      //tree_stream.close();
    }
  return max_S;

}
Example #22
0
scalar_type sum_counts(Species_tree * infer_tree,const mpi::communicator  world)
{
  //########################## GLOBAL #########################
  int server = 0;  
  int rank = world.rank();
  int size = world.size();

  scalar_type client_ll,broadcast_ll;
  vector<scalar_type> gather_ll;
  scalar_type client_pea_count;
  scalar_type broadcast_pea_count;
  vector<scalar_type> gather_pea_count;
  scatter_scalar gather_O_counts;
  scatter_scalar gather_D_counts;
  scatter_scalar gather_T_counts;
  scatter_scalar gather_L_counts;
  scatter_scalar gather_B_counts;
  scatter_scalar gather_S_counts;
  scatter_scalar gather_F_counts;
  scatter_scalar gather_G_counts;
  //vector<  vector< vector<scalar_type> > > gather_Ttf;
  
  vector<scalar_type> broadcast_O_counts;
  vector<scalar_type> broadcast_D_counts;
  vector<scalar_type> broadcast_T_counts;
  vector<scalar_type> broadcast_branch_zero;
  vector<scalar_type> broadcast_branch_count;
  vector<scalar_type> broadcast_branch_sum;
  vector<scalar_type> broadcast_from_count;
  vector<scalar_type> broadcast_genome_size;
  int start=1;
  //########################## GLOBAL #########################

  //########################## SERVER #########################
  //########################## SERVER #########################
  if (rank != server) 
    {
      //########################## CLIENT #########################
      client_pea_count=infer_tree->tmp_pea_sum;
      client_ll=infer_tree->tmp_ll;
      //########################## CLIENT #########################

    }
  //########################## COMMON #########################
  gather(world, infer_tree->O_counts, gather_O_counts, server);           
  gather(world, infer_tree->D_counts, gather_D_counts, server);           
  gather(world, infer_tree->T_counts, gather_T_counts, server);           
  gather(world, infer_tree->branch_zero, gather_L_counts, server);           
  gather(world, infer_tree->branch_count, gather_B_counts, server);                 
  gather(world, infer_tree->branch_sum, gather_S_counts, server);              
  gather(world, infer_tree->from_count, gather_F_counts, server);       
  gather(world, infer_tree->branch_genome_size, gather_G_counts, server);       

  //TTF
  // gather(world, infer_tree->Ttf, gather_Ttf, server);       
       
  gather(world, client_pea_count, gather_pea_count, server);                 
  gather(world, client_ll, gather_ll, server);      
  //########################## COMMON #########################
  if (rank == server) 
    {

      //########################## SERVER #########################
      for (int i = 0;i<infer_tree->N_slices+infer_tree->tree->getNumberOfLeaves()-1;i++)
	{
	  broadcast_O_counts.push_back(0);
	  broadcast_D_counts.push_back(0);
	  broadcast_T_counts.push_back(0);
	  broadcast_branch_zero.push_back(0);
	  broadcast_branch_count.push_back(0);
	  broadcast_branch_sum.push_back(0);
	  broadcast_from_count.push_back(0);      
	  broadcast_genome_size.push_back(0);      
	}
      scalar_type tmp_O=0;
      scalar_type tmp_D=0;
      scalar_type tmp_T=0;
      scalar_type tmp_z=0;
      scalar_type tmp_c=0;
      scalar_type tmp_s=0;
      scalar_type tmp_f=0;
      //cout << " start summing - sum " <<endl;     
      //for (int i = 0;i<infer_tree->N_slices+infer_tree->tree->getNumberOfLeaves()-1;i++)
      //	for (int k = 0;k<infer_tree->N_slices+infer_tree->tree->getNumberOfLeaves()-1;k++)
      //	  infer_tree->Ttf[i][k]=0;
      for (int j=1;j<size;j++)
	{
	  for (int i = 0;i<infer_tree->N_slices+infer_tree->tree->getNumberOfLeaves()-1;i++)
	    {	      
	      //for (int k = 0;k<infer_tree->N_slices+infer_tree->tree->getNumberOfLeaves()-1;k++)
	      //	infer_tree->Ttf[i][k]+=gather_Ttf[j][i][k];
	      broadcast_O_counts[i]+=gather_O_counts[j][i];
	      broadcast_D_counts[i]+=gather_D_counts[j][i];
	      broadcast_T_counts[i]+=gather_T_counts[j][i];
	      broadcast_branch_zero[i]+=gather_L_counts[j][i];
	      broadcast_branch_count[i]+=gather_B_counts[j][i];
	      broadcast_branch_sum[i]+=gather_S_counts[j][i];	 
	      broadcast_from_count[i]+=gather_F_counts[j][i];	 
	      broadcast_genome_size[i]+=gather_G_counts[j][i];	 	      
	      tmp_O+=gather_O_counts[j][i];
	      tmp_D+=gather_D_counts[j][i];
	      tmp_T+=gather_T_counts[j][i];
	      tmp_z+=gather_L_counts[j][i];
	      tmp_c+=gather_B_counts[j][i];
	      tmp_s+=gather_S_counts[j][i];
	      tmp_f+=gather_F_counts[j][i];	     
	    }
	}

      /*
      for (int i = 0;i<infer_tree->N_slices+infer_tree->tree->getNumberOfLeaves()-1;i++)
	{
	  cout << "tag "<<i;
	  cout << " " <<  broadcast_O_counts[i];
	  cout << " " <<  broadcast_D_counts[i];
	  cout << " " <<  broadcast_T_counts[i];
	  cout << " " <<  broadcast_branch_zero[i];
	  cout << " " <<  broadcast_branch_sum[i];
	  cout << " " <<  broadcast_branch_count[i];
	  cout << " " <<  broadcast_from_count[i];
	  cout << endl;
	}
      */
      //if (rank==server) cout << rank << " : "<<tmp_O << " " << tmp_D << " " << tmp_T << " " << tmp_z << " " << tmp_c << " " << tmp_s << " " << tmp_f << endl; 
      broadcast_pea_count=0.;
      broadcast_ll=0.;
      //cout << " start summing - llsum " << endl;      

      for (int j=1;j<size;j++)	
	{
	  broadcast_pea_count+=gather_pea_count[j];      
	  broadcast_ll+=gather_ll[j];
	}
      // ### sum events
      //########################## SERVER #########################
      //cout << " end summing"    << endl;   

    }
  //########################## CLIENT #########################
  //########################## CLIENT #########################

  //########################## COMMON #########################  
broadcast(world, broadcast_O_counts,server);
  broadcast(world, broadcast_D_counts,server);
  broadcast(world, broadcast_T_counts,server);
  broadcast(world, broadcast_branch_zero,server);
  broadcast(world, broadcast_branch_count,server);
  broadcast(world, broadcast_branch_sum,server);
  broadcast(world, broadcast_from_count,server);
  broadcast(world, broadcast_genome_size,server);
  broadcast(world, broadcast_pea_count,server);
  broadcast(world, broadcast_ll,server);
  //########################## COMMON #########################

  //########################## SEVER #########################
  //########################## CLIENT #########################
  //  if (rank==server)
  //  {
  for (int i = 0;i<infer_tree->N_slices+infer_tree->tree->getNumberOfLeaves()-1;i++)
    {
      infer_tree->O_counts[i]=broadcast_O_counts[i];
      infer_tree->D_counts[i]=broadcast_D_counts[i];
      infer_tree->T_counts[i]=broadcast_T_counts[i];
      infer_tree->branch_zero[i]=broadcast_branch_zero[i];
      infer_tree->branch_count[i]=broadcast_branch_count[i];
      infer_tree->branch_sum[i]=broadcast_branch_sum[i];
      infer_tree->from_count[i]=broadcast_from_count[i];
      infer_tree->branch_genome_size[i]=broadcast_genome_size[i];
    }
  //infer_tree->remember_rates();
  // }
  //infer_tree->apparent_rates(broadcast_pea_count,true,true);
  //########################## CLIENT #########################
  //########################## SERVER #########################
  
 
  gather_ll.clear();
  gather_pea_count.clear();

  for (scatter_scalar::iterator it=gather_O_counts.begin();it!=gather_O_counts.end();it++) (*it).clear();
  gather_O_counts.clear();
  for (scatter_scalar::iterator it=gather_D_counts.begin();it!=gather_D_counts.end();it++) (*it).clear();
  gather_D_counts.clear();
  for (scatter_scalar::iterator it=gather_T_counts.begin();it!=gather_T_counts.end();it++) (*it).clear();
  gather_T_counts.clear();
  for (scatter_scalar::iterator it=gather_L_counts.begin();it!=gather_L_counts.end();it++) (*it).clear();
  gather_L_counts.clear();
  for (scatter_scalar::iterator it=gather_B_counts.begin();it!=gather_B_counts.end();it++) (*it).clear();
  gather_B_counts.clear();
  for (scatter_scalar::iterator it=gather_S_counts.begin();it!=gather_S_counts.end();it++) (*it).clear();
  gather_S_counts.clear();
  for (scatter_scalar::iterator it=gather_F_counts.begin();it!=gather_F_counts.end();it++) (*it).clear();
  gather_F_counts.clear();
  for (scatter_scalar::iterator it=gather_G_counts.begin();it!=gather_G_counts.end();it++) (*it).clear();
  gather_G_counts.clear();

  broadcast_O_counts.clear();
  broadcast_D_counts.clear();
  broadcast_T_counts.clear();
  broadcast_branch_zero.clear();
  broadcast_branch_count.clear();
  broadcast_branch_sum.clear();
  broadcast_from_count.clear();
  broadcast_genome_size.clear();

  return broadcast_pea_count;

}
Example #23
0
 WorkerProcess(mpi::communicator& c) :
   com(c), data(), myLongest("") { 
   token = NO_TOKEN;
   nextHop = (com.rank() + 1) % com.size();
   isWhiteProcess = true;
 }
Example #24
0
pair < scalar_type,string> ML_nni_step(Species_tree * infer_tree,string Sstring,string mode,bool greedy)
{

  const mpi::communicator  world = infer_tree->world;
  int server = 0;  
  int mpi_rank = world.rank();
  int size = world.size();
  broadcast(world, Sstring,server);
  tree_type * S=TreeTemplateTools::parenthesisToTree(Sstring);

  // get inital set up from branch lengts
  Node * root = S->getRootNode();  
  //vector<Node * > nodes_tmp = S->getNodes();    
  vector<Node * > nodes= S->getNodes();
  //RandomTools::getSample(nodes_tmp,nodes);
  map <scalar_type,Node * > times;
  for (vector<Node * > :: iterator ni = nodes.begin(); ni != nodes.end(); ni++ )
    if (!(*ni)->isLeaf())
      {
	scalar_type h=TreeTemplateTools::getDistanceBetweenAnyTwoNodes(*(*ni),*root);
	if (times.count(h))
	  h+=1e-20;      
	times[h]=(*ni);
      }
  // register ranks 
  map <Node *,int> node_2_rank;
  map <int,Node *> rank_2_node;
  map <int,int> id_2_rank;
  map <int,int> rank_2_id;
  vector <pair <int,int> > moves;
  int rank=0;
  for (map < scalar_type,Node *> :: iterator hi = times.begin(); hi != times.end(); hi++ )
    {
      rank++;
      rank_2_node[rank]=(*hi).second;
      node_2_rank[(*hi).second]=rank;
      rank_2_id[rank]=(*hi).second->getId();
      id_2_rank[(*hi).second->getId()]=rank;

      stringstream out;out<<rank;
      (*hi).second->setBranchProperty("ID",BppString(out.str()));
    }

  // set branch lengths according to ranks 
  for (vector<Node * > :: iterator ni = nodes.begin(); ni != nodes.end(); ni++ )
    if  ((*ni)->isLeaf() )
      (*ni)->setDistanceToFather( rank+1 - node_2_rank[(*ni)->getFather()] );
    else if ((*ni)!=root)
      (*ni)->setDistanceToFather( node_2_rank[(*ni)] - node_2_rank[(*ni)->getFather()] );
      
  scalar_type max_dLL=-1e30;

  /*
  // find moves
  for (map <int,Node *> :: iterator nri = rank_2_node.begin(); nri != rank_2_node.end(); nri++ )
    if ((*nri).first!=1)
      {
	Node * node=(*nri).second;
	int r=(*nri).first;
	if (rank_2_node[r-1]->getSon(0)!=node && rank_2_node[r-1]->getSon(1)!=node)
	  {
	    pair <int,int> move;
	    move.first=r-1;
	    move.second=r;
	    moves.push_back(move);
	  }
      }
  //cout << TreeTemplateTools::treeToParenthesis(*S,false,"ID");

  scalar_type oLL=LL_mpi(infer_tree,S,mode);
  //XX
  //cout << oLL << endl;
  TreeTemplate<Node> * max_dS;
  vector<pair<int,int> > good_moves;

  map <scalar_type, TreeTemplate<Node> * > move_trees;
  nodes = S->getNodes();    
  root=S->getRootNode();

  // preform moves
  for (vector <pair <int,int> > :: iterator mi = moves.begin(); mi != moves.end(); mi++ )
    {
      //TreeTemplate<Node> * dS=S->clone();
      //vector<Node * > dS_nodes = dS->getNodes();    
      //Node * dS_root=dS->getRootNode();

      id_2_rank[rank_2_id[(*mi).first]] = (*mi).second;
      id_2_rank[rank_2_id[(*mi).second]] = (*mi).first;

      for (vector<Node * > :: iterator ni = nodes.begin(); ni != nodes.end(); ni++ )
	if  ((*ni)->isLeaf() )
	  (*ni)->setDistanceToFather( rank+1 - id_2_rank[(*ni)->getFather()->getId()] );
	else if ((*ni)!=root)
	  (*ni)->setDistanceToFather( id_2_rank[(*ni)->getId()] - id_2_rank[(*ni)->getFather()->getId()] );
      
      
      scalar_type dLL = LL_mpi(infer_tree,S,mode)-oLL;
      //cout << (*mi).first << " <-> " << (*mi).second << endl;
      //cout << dLL <<endl;
      id_2_rank[rank_2_id[(*mi).first]] = (*mi).first;
      id_2_rank[rank_2_id[(*mi).second]] = (*mi).second;

      for (vector<Node * > :: iterator ni = nodes.begin(); ni != nodes.end(); ni++ )
	if  ((*ni)->isLeaf() )
	  (*ni)->setDistanceToFather( rank+1 - id_2_rank[(*ni)->getFather()->getId()] );
	else if ((*ni)!=root)
	  (*ni)->setDistanceToFather( id_2_rank[(*ni)->getId()] - id_2_rank[(*ni)->getFather()->getId()] );
      if (dLL>max_dLL)
	{
	  max_dLL=dLL;
	  //max_dS=dS;
	}

      if(dLL>0)
	good_moves.push_back((*mi));

            
    }
  
  //cout << max_dLL << endl;
  map<int,int> moved;
  for (vector <pair <int,int> > :: iterator mi = good_moves.begin(); mi != good_moves.end(); mi++ )
    {
      if (moved.count((*mi).first)==0 && moved.count((*mi).first)==0)
	{
	  //cout << (*mi).first << " <-G-> " << (*mi).second << endl;
      
	  id_2_rank[rank_2_id[(*mi).first]] = (*mi).second;
	  id_2_rank[rank_2_id[(*mi).second]] = (*mi).first;
	  moved[(*mi).first]=1;
	  moved[(*mi).second]=1;
	}
    }

  for (vector<Node * > :: iterator ni = nodes.begin(); ni != nodes.end(); ni++ )
    if  ((*ni)->isLeaf() )
      (*ni)->setDistanceToFather( rank+1 - id_2_rank[(*ni)->getFather()->getId()] );
    else if ((*ni)!=root)
      (*ni)->setDistanceToFather( id_2_rank[(*ni)->getId()] - id_2_rank[(*ni)->getFather()->getId()] );
  */
  if (mpi_rank==server) cout << Sstring<<endl;
  if (mpi_rank==server) cout << TreeTemplateTools::treeToParenthesis(*S)<<endl;
  if (mpi_rank==server) cout << can_order(S)<<endl;

  scalar_type oLL=LL_mpi(infer_tree,can_order(S),mode);
  if (mpi_rank==server) cout << "oLL " << oLL << endl;

  scalar_type maxdLL=0;
  int max_id=-1;
  int max_father_id=-1;

  //TreeTemplate<Node> * maxdS;
  string maxdS;
  root=S->getRootNode();
  for (vector < Node *> :: iterator ni=nodes.begin();ni!=nodes.end(); ni++)
    if (!((*ni)==root) && !((*ni)->isLeaf()) )
      {
	Node * node0=(*ni)->getSon(0);
	Node * node1=(*ni)->getSon(1);
	Node * father=(*ni)->getFather();
	Node * node2;

	if (father->getSon(0)==(*ni))
	  node2=father->getSon(1);
	else
	  node2=father->getSon(0);
	
	//disolve
	(*ni)->removeSons();
	father->removeSons();
	//NNI 1.
	(*ni)->addSon(node0);       
	(*ni)->addSon(node2);
	father->addSon((*ni));
	father->addSon(node1);
	//clone out
	int old_rank=id_2_rank[(*ni)->getId()];
	for (map<int,int> :: iterator ii = id_2_rank.begin(); ii != id_2_rank.end(); ii++ )
	  if ((*ii).second<id_2_rank[(*ni)->getId()] && (*ii).second>id_2_rank[father->getId()])
	    (*ii).second+=1;
	id_2_rank[(*ni)->getId()]=id_2_rank[(*ni)->getFather()->getId()]+1;

	vector<Node * > dS_nodes = S->getNodes();    
	Node * dS_root=S->getRootNode();
	
	for (vector<Node * > :: iterator nii = dS_nodes.begin(); nii != dS_nodes.end(); nii++ )
	  if  ((*nii)->isLeaf() )
	    (*nii)->setDistanceToFather( rank+1 - id_2_rank[(*nii)->getFather()->getId()] );
	  else if ((*nii)!=dS_root)
	    (*nii)->setDistanceToFather( id_2_rank[(*nii)->getId()] - id_2_rank[(*nii)->getFather()->getId()] );		
	//TreeTemplate<Node> * dS=S->clone();
	string nni_string=can_order(S);	
	scalar_type dLL = LL_mpi(infer_tree,nni_string,mode)-oLL;
	//cout << get_name(node0) << " ^ "  << get_name(node2) << " -- " << get_name(node1) <<endl;
  
	if (dLL>maxdLL)
	  {
	    maxdLL=dLL;
	    maxdS=nni_string;//S->clone();
	    max_id=(*ni)->getId();
	    max_father_id=father->getId();
	    if (mpi_rank==server) cout << dLL << " " << oLL << endl; 
	    if (mpi_rank==server) cout << nni_string << endl; 
	    if (greedy) break;
	  }

	//disolve
	(*ni)->removeSons();
	father->removeSons();
	for (map<int,int> :: iterator ii = id_2_rank.begin(); ii != id_2_rank.end(); ii++ )
	  if ((*ii).second<old_rank+1 && (*ii).second>id_2_rank[father->getId()])
	    (*ii).second-=1;
	id_2_rank[(*ni)->getId()]=old_rank;

	//NNI 2.
	(*ni)->addSon(node1);
	(*ni)->addSon(node2);
	father->addSon((*ni));
	father->addSon(node0);	
	
	//clone out
	old_rank=id_2_rank[(*ni)->getId()];
	for (map<int,int> :: iterator ii = id_2_rank.begin(); ii != id_2_rank.end(); ii++ )
	  if ((*ii).second<id_2_rank[(*ni)->getId()] && (*ii).second>id_2_rank[father->getId()])
	    (*ii).second+=1;
	id_2_rank[(*ni)->getId()]=id_2_rank[(*ni)->getFather()->getId()]+1;

	dS_nodes = S->getNodes();    
	dS_root=S->getRootNode();
	
	for (vector<Node * > :: iterator nii = dS_nodes.begin(); nii != dS_nodes.end(); nii++ )
	  if  ((*nii)->isLeaf() )
	    (*nii)->setDistanceToFather( rank+1 - id_2_rank[(*nii)->getFather()->getId()] );
	  else if ((*nii)!=dS_root)
	    (*nii)->setDistanceToFather( id_2_rank[(*nii)->getId()] - id_2_rank[(*nii)->getFather()->getId()] );		
	//dS
	nni_string=can_order(S);	
	dLL = LL_mpi(infer_tree,nni_string,mode)-oLL;
	//cout << dLL << "nni move"<<endl;
	//cout << get_name(node1) << " ^ "  << get_name(node2) << " -- " << get_name(node0) <<endl;
	if (dLL>maxdLL)
	  {
	    maxdLL=dLL;
	    maxdS=nni_string;//S->clone();;
	    max_id=(*ni)->getId();
	    max_father_id=father->getId();
	    if (mpi_rank==server) cout << dLL << " ."<< endl; 
	    if (mpi_rank==server) cout << nni_string << endl; 

	    if (greedy) break;
	  }
	
	//disolve
	(*ni)->removeSons();
	father->removeSons();
	for (map<int,int> :: iterator ii = id_2_rank.begin(); ii != id_2_rank.end(); ii++ )
	  if ((*ii).second<old_rank+1 && (*ii).second>id_2_rank[father->getId()])
	    (*ii).second-=1;
	id_2_rank[(*ni)->getId()]=old_rank;
	
	//restore
	(*ni)->addSon(node0);
	(*ni)->addSon(node1);
	father->addSon((*ni));
	father->addSon(node2);
      }
  times.clear();
  nodes.clear();
  node_2_rank.clear();
  rank_2_node.clear();
  id_2_rank.clear();
  rank_2_id.clear();
  moves.clear();  

  pair < scalar_type,string> ML_step;

  delete S;
  ML_step.first=maxdLL;

  if (maxdLL>0)
    ML_step.second=maxdS;
  else
    ML_step.second=Sstring;
  return ML_step;
 /* if (maxdLL>0)
    {
      S=maxdS;
      max_dLL=maxdLL;
      int old_rank=id_2_rank[max_id];
      for (map<int,int> :: iterator ii = id_2_rank.begin(); ii != id_2_rank.end(); ii++ )
	if ((*ii).second<id_2_rank[max_id] && (*ii).second>id_2_rank[max_father_id])
	  (*ii).second+=1;
      id_2_rank[max_id]=id_2_rank[max_father_id]+1;      
    }
  nodes = S->getNodes();    
  root=S->getRootNode();

  for (vector<Node * > :: iterator ni = nodes.begin(); ni != nodes.end(); ni++ )    
    if  ((*ni)->isLeaf() )
      (*ni)->setDistanceToFather( rank+1 - id_2_rank[(*ni)->getFather()->getId()] );
    else if ((*ni)!=root)
      (*ni)->setDistanceToFather( id_2_rank[(*ni)->getId()] - id_2_rank[(*ni)->getFather()->getId()] );
  pair < scalar_type,string> ML_step;
  
  ML_step.first=max_dLL;
  ML_step.second=can_order(S);
  //ML_step.second=maxdS;

  return ML_step;
*/
}