Index: XIOS/trunk/src/context_client.cpp
===================================================================
--- XIOS/trunk/src/context_client.cpp	(revision 591)
+++ XIOS/trunk/src/context_client.cpp	(revision 595)
@@ -21,20 +21,54 @@
     \cxtSer [in] cxtSer Pointer to context of server side. (It is only used on case of attached mode)
     */
-    CContextClient::CContextClient(CContext* parent,MPI_Comm intraComm_, MPI_Comm interComm_, CContext* cxtSer)
+    CContextClient::CContextClient(CContext* parent, MPI_Comm intraComm_, MPI_Comm interComm_, CContext* cxtSer)
      : mapBufferSize_(), parentServer(cxtSer)
     {
-      context=parent ;
-      intraComm=intraComm_ ;
-      interComm=interComm_ ;
-      MPI_Comm_rank(intraComm,&clientRank) ;
-      MPI_Comm_size(intraComm,&clientSize) ;
-
-      int flag ;
-      MPI_Comm_test_inter(interComm,&flag) ;
-      if (flag) MPI_Comm_remote_size(interComm,&serverSize);
-      else  MPI_Comm_size(interComm,&serverSize) ;
-
-      timeLine=0 ;
-
+      context = parent;
+      intraComm = intraComm_;
+      interComm = interComm_;
+      MPI_Comm_rank(intraComm, &clientRank);
+      MPI_Comm_size(intraComm, &clientSize);
+
+      int flag;
+      MPI_Comm_test_inter(interComm, &flag);
+      if (flag) MPI_Comm_remote_size(interComm, &serverSize);
+      else  MPI_Comm_size(interComm, &serverSize);
+
+      if (clientSize < serverSize)
+      {
+        int serverByClient = serverSize / clientSize;
+        int remain = serverSize % clientSize;
+        int rankStart = serverByClient * clientRank;
+
+        if (clientRank < remain)
+        {
+          serverByClient++;
+          rankStart += clientRank;
+        }
+        else
+          rankStart += remain;
+
+        for (int i = 0; i < serverByClient; i++)
+          ranksServerLeader.push_back(rankStart + i);
+      }
+      else
+      {
+        int clientByServer = clientSize / serverSize;
+        int remain = clientSize % serverSize;
+
+        if (clientRank < (clientByServer + 1) * remain)
+        {
+          if (clientRank % (clientByServer + 1) == 0)
+            ranksServerLeader.push_back(clientRank / (clientByServer + 1));
+        }
+        else
+        {
+          int rank = clientRank - (clientByServer + 1) * remain;
+          if (rank % clientByServer == 0)
+            ranksServerLeader.push_back(remain + rank / clientByServer);
+        }
+      }
+
+      timeLine = 0;
     }
 
@@ -45,30 +79,29 @@
     void CContextClient::sendEvent(CEventClient& event)
     {
-      list<int>::iterator itServer ;
-      list<int> ranks ;
-      list<int> sizes ;
-      list<int>::iterator itSize ;
-
-      ranks=event.getRanks() ;
-      if (! event.isEmpty())
-      {
-        sizes=event.getSizes() ;
-        CMessage msg ;
-
-        msg<<*(sizes.begin())<<timeLine ;
-        for(list<int>::iterator it=sizes.begin();it!=sizes.end();it++) *it+=msg.size() ;
-        list<CBufferOut*> buffList=getBuffers(ranks,sizes) ;
-
-        list<CBufferOut*>::iterator it ;
-        for(it=buffList.begin(),itSize=sizes.begin();it!=buffList.end();++it,++itSize)
-        {
-          **it<<*itSize<<timeLine ;
-        }
-        event.send(buffList) ;
-        checkBuffers(ranks) ;
-      }
-
-//      if (context->hasServer)
-      if (0 != parentServer)
+      list<int>::iterator itServer;
+      list<int> ranks;
+      list<int> sizes;
+      list<int>::iterator itSize;
+
+      ranks = event.getRanks();
+      if (!event.isEmpty())
+      {
+        sizes = event.getSizes();
+        CMessage msg;
+
+        msg << *(sizes.begin()) << timeLine;
+        for (list<int>::iterator it = sizes.begin(); it != sizes.end(); it++) *it += msg.size();
+        list<CBufferOut*> buffList = getBuffers(ranks, sizes);
+
+        list<CBufferOut*>::iterator it;
+        for (it = buffList.begin(), itSize = sizes.begin(); it != buffList.end(); ++it, ++itSize)
+        {
+          **it << *itSize << timeLine;
+        }
+        event.send(buffList);
+        checkBuffers(ranks);
+      }
+
+      if (0 != parentServer) // context->hasServer
       {
         waitEvent(ranks);
@@ -76,5 +109,5 @@
       }
 
-      timeLine++ ;
+      timeLine++;
     }
 
@@ -85,9 +118,9 @@
     void CContextClient::sendBufferSizeEvent()
     {
-      std::map<int, CClientBuffer*>::iterator it, itE;
-      std::map<int, StdSize>::const_iterator itMap = mapBufferSize_.begin(), iteMap = mapBufferSize_.end();
+      std::map<int,CClientBuffer*>::iterator it, itE;
+      std::map<int,StdSize>::const_iterator itMap = mapBufferSize_.begin(), iteMap = mapBufferSize_.end();
 
       if (itMap == iteMap)
-         ERROR("CBufferOut*  CContextClient::sendBufferSizeEvent() ;",
+         ERROR("void CContextClient::sendBufferSizeEvent()",
               <<"No information about server buffer, that should not happen...");
 
@@ -115,26 +148,26 @@
     void CContextClient::waitEvent(list<int>& ranks)
     {
-//      context->server->setPendingEvent() ;
-//      while(checkBuffers(ranks))
+//      context->server->setPendingEvent();
+//      while (checkBuffers(ranks))
 //      {
-//        context->server->listen() ;
-//        context->server->checkPendingRequest() ;
+//        context->server->listen();
+//        context->server->checkPendingRequest();
 //      }
 //
-//      while(context->server->hasPendingEvent())
+//      while (context->server->hasPendingEvent())
 //      {
-//       context->server->eventLoop() ;
+//       context->server->eventLoop();
 //      }
 
-      parentServer->server->setPendingEvent() ;
-      while(checkBuffers(ranks))
-      {
-        parentServer->server->listen() ;
-        parentServer->server->checkPendingRequest() ;
-      }
-
-      while(parentServer->server->hasPendingEvent())
-      {
-       parentServer->server->eventLoop() ;
+      parentServer->server->setPendingEvent();
+      while (checkBuffers(ranks))
+      {
+        parentServer->server->listen();
+        parentServer->server->checkPendingRequest();
+      }
+
+      while (parentServer->server->hasPendingEvent())
+      {
+       parentServer->server->eventLoop();
       }
     }
@@ -148,41 +181,40 @@
     list<CBufferOut*> CContextClient::getBuffers(list<int>& serverList, list<int>& sizeList)
     {
-      list<int>::iterator itServer,itSize ;
-      list<CClientBuffer*> bufferList ;
-      map<int,CClientBuffer*>::iterator it ;
-      list<CClientBuffer*>::iterator itBuffer ;
-      list<CBufferOut*>  retBuffer ;
-      bool free ;
-
-      for(itServer=serverList.begin();itServer!=serverList.end();itServer++)
-      {
-        it=buffers.find(*itServer) ;
-        if (it==buffers.end())
-        {
-          newBuffer(*itServer) ;
-          it=buffers.find(*itServer) ;
-        }
-        bufferList.push_back(it->second) ;
-      }
-      free=false ;
+      list<int>::iterator itServer, itSize;
+      list<CClientBuffer*> bufferList;
+      map<int,CClientBuffer*>::iterator it;
+      list<CClientBuffer*>::iterator itBuffer;
+      list<CBufferOut*>  retBuffer;
+      bool free;
+
+      for (itServer = serverList.begin(); itServer != serverList.end(); itServer++)
+      {
+        it = buffers.find(*itServer);
+        if (it == buffers.end())
+        {
+          newBuffer(*itServer);
+          it = buffers.find(*itServer);
+        }
+        bufferList.push_back(it->second);
+      }
+      free = false;
 
       CTimer::get("Blocking time").resume();
-      while(!free)
-      {
-        free=true ;
-        for(itBuffer=bufferList.begin(),itSize=sizeList.begin(); itBuffer!=bufferList.end();itBuffer++,itSize++)
-        {
-          (*itBuffer)->checkBuffer() ;
-         free&=(*itBuffer)->isBufferFree(*itSize) ;
+      while (!free)
+      {
+        free = true;
+        for (itBuffer = bufferList.begin(), itSize = sizeList.begin(); itBuffer != bufferList.end(); itBuffer++, itSize++)
+        {
+          (*itBuffer)->checkBuffer();
+         free &= (*itBuffer)->isBufferFree(*itSize);
         }
       }
       CTimer::get("Blocking time").suspend();
 
-      for(itBuffer=bufferList.begin(),itSize=sizeList.begin(); itBuffer!=bufferList.end();itBuffer++,itSize++)
-      {
-        retBuffer.push_back((*itBuffer)->getBuffer(*itSize)) ;
-      }
-      return retBuffer ;
-
+      for (itBuffer = bufferList.begin(), itSize = sizeList.begin(); itBuffer != bufferList.end(); itBuffer++, itSize++)
+      {
+        retBuffer.push_back((*itBuffer)->getBuffer(*itSize));
+      }
+      return retBuffer;
    }
 
@@ -193,5 +225,5 @@
    void CContextClient::newBuffer(int rank)
    {
-      buffers[rank]=new CClientBuffer(interComm,rank, mapBufferSize_[rank]) ;
+      buffers[rank] = new CClientBuffer(interComm, rank, mapBufferSize_[rank]);
    }
 
@@ -202,8 +234,8 @@
    bool CContextClient::checkBuffers(void)
    {
-      map<int,CClientBuffer*>::iterator itBuff ;
-      bool pending=false ;
-      for(itBuff=buffers.begin();itBuff!=buffers.end();itBuff++) pending|=itBuff->second->checkBuffer() ;
-      return pending ;
+      map<int,CClientBuffer*>::iterator itBuff;
+      bool pending = false;
+      for (itBuff = buffers.begin(); itBuff != buffers.end(); itBuff++) pending |= itBuff->second->checkBuffer();
+      return pending;
    }
 
@@ -211,6 +243,6 @@
    void CContextClient::releaseBuffers(void)
    {
-      map<int,CClientBuffer*>::iterator itBuff ;
-      for(itBuff=buffers.begin();itBuff!=buffers.end();itBuff++) delete itBuff->second ;
+      map<int,CClientBuffer*>::iterator itBuff;
+      for (itBuff = buffers.begin(); itBuff != buffers.end(); itBuff++) delete itBuff->second;
    }
 
@@ -222,8 +254,8 @@
    bool CContextClient::checkBuffers(list<int>& ranks)
    {
-      list<int>::iterator it ;
-      bool pending=false ;
-      for(it=ranks.begin();it!=ranks.end();it++) pending|=buffers[*it]->checkBuffer() ;
-      return pending ;
+      list<int>::iterator it;
+      bool pending = false;
+      for (it = ranks.begin(); it != ranks.end(); it++) pending |= buffers[*it]->checkBuffer();
+      return pending;
    }
 
@@ -232,5 +264,5 @@
    \param [in] mapSize mapping rank of connected server to size of allocated buffer
    */
-   void CContextClient::setBufferSize(const std::map<int, StdSize>& mapSize)
+   void CContextClient::setBufferSize(const std::map<int,StdSize>& mapSize)
    {
      mapBufferSize_ = mapSize;
@@ -238,47 +270,21 @@
    }
 
-   /*!
-   Get leading server in the group of connected server
-   \return rank of leading server
-   */
-   int CContextClient::getServerLeader(void)
-   {
-     int clientByServer=clientSize/serverSize ;
-     int remain=clientSize%serverSize ;
-
-     if (clientRank<(clientByServer+1)*remain)
-     {
-       return clientRank/(clientByServer+1) ;
-     }
-     else
-     {
-       int rank=clientRank-(clientByServer+1)*remain ;
-       int nbServer=serverSize-remain ;
-       return remain+rank/clientByServer ;
-     }
-   }
-
-   /*!
-   Check if client connects to leading server
-   \return connected(true), not connected (false)
-   */
-   bool CContextClient::isServerLeader(void)
-   {
-     int clientByServer=clientSize/serverSize ;
-     int remain=clientSize%serverSize ;
-
-     if (clientRank<(clientByServer+1)*remain)
-     {
-       if (clientRank%(clientByServer+1)==0) return true ;
-       else return false ;
-     }
-     else
-     {
-       int rank=clientRank-(clientByServer+1)*remain ;
-       int nbServer=serverSize-remain ;
-       if  (rank%clientByServer==0) return true ;
-       else return false ;
-     }
-   }
+  /*!
+  Get leading server in the group of connected server
+  \return ranks of leading servers
+  */
+  const std::list<int>& CContextClient::getRanksServerLeader(void) const
+  {
+    return ranksServerLeader;
+  }
+
+  /*!
+  Check if client connects to leading server
+  \return connected(true), not connected (false)
+  */
+  bool CContextClient::isServerLeader(void) const
+  {
+    return !ranksServerLeader.empty();
+  }
 
    /*!
@@ -287,37 +293,39 @@
    void CContextClient::finalize(void)
    {
-     map<int,CClientBuffer*>::iterator itBuff ;
-     bool stop=true ;
-
-     CEventClient event(CContext::GetType(),CContext::EVENT_ID_CONTEXT_FINALIZE) ;
+     map<int,CClientBuffer*>::iterator itBuff;
+     bool stop = true;
+
+     CEventClient event(CContext::GetType(), CContext::EVENT_ID_CONTEXT_FINALIZE);
      if (isServerLeader())
      {
-       CMessage msg ;
-       event.push(getServerLeader(),1,msg) ;
-       sendEvent(event) ;
+       CMessage msg;
+       const std::list<int>& ranks = getRanksServerLeader();
+       for (std::list<int>::const_iterator itRank = ranks.begin(), itRankEnd = ranks.end(); itRank != itRankEnd; ++itRank)
+         event.push(*itRank, 1, msg);
+       sendEvent(event);
      }
-     else sendEvent(event) ;
+     else sendEvent(event);
 
      CTimer::get("Blocking time").resume();
-     while(stop)
+     while (stop)
      {
-       checkBuffers() ;
-       stop=false ;
-       for(itBuff=buffers.begin();itBuff!=buffers.end();itBuff++) stop|=itBuff->second->hasPendingRequest() ;
+       checkBuffers();
+       stop = false;
+       for (itBuff = buffers.begin(); itBuff != buffers.end(); itBuff++) stop |= itBuff->second->hasPendingRequest();
      }
      CTimer::get("Blocking time").suspend();
 
-     std::map<int, StdSize>::const_iterator itbMap = mapBufferSize_.begin(),
-                                            iteMap = mapBufferSize_.end(), itMap;
+     std::map<int,StdSize>::const_iterator itbMap = mapBufferSize_.begin(),
+                                           iteMap = mapBufferSize_.end(), itMap;
      StdSize totalBuf = 0;
      for (itMap = itbMap; itMap != iteMap; ++itMap)
      {
-       report(10)<< " Memory report : Context <"<<context->getId()<<"> : client side : memory used for buffer of each connection to server" << endl
-                 << "  +) To server with rank " << itMap->first << " : " << itMap->second << " bytes " << endl;
+       report(10) << " Memory report : Context <" << context->getId() << "> : client side : memory used for buffer of each connection to server" << endl
+                  << "  +) To server with rank " << itMap->first << " : " << itMap->second << " bytes " << endl;
        totalBuf += itMap->second;
      }
-     report(0)<< " Memory report : Context <"<<context->getId()<<"> : client side : total memory used for buffer "<<totalBuf<<" bytes"<<endl ;
-
-     releaseBuffers() ;
+     report(0) << " Memory report : Context <" << context->getId() << "> : client side : total memory used for buffer " << totalBuf << " bytes" << endl;
+
+     releaseBuffers();
    }
 }
