source: XIOS2/trunk/src/server.cpp @ 2428

Last change on this file since 2428 was 2428, checked in by jderouillat, 19 months ago

Backport the XIOS3 system to log the memory consumption (commit ID [2418-2420,2425-2426])

  • Property copyright set to
    Software name : XIOS (Xml I/O Server)
    http://forge.ipsl.jussieu.fr/ioserver
    Creation date : January 2009
    Licence : CeCCIL version2
    see license file in root directory : Licence_CeCILL_V2-en.txt
    or http://www.cecill.info/licences/Licence_CeCILL_V2-en.html
    Holder : CEA/LSCE (Laboratoire des Sciences du CLimat et de l'Environnement)
    CNRS/IPSL (Institut Pierre Simon Laplace)
    Project Manager : Yann Meurdesoif
    yann.meurdesoif@cea.fr
  • Property svn:eol-style set to native
File size: 32.7 KB
RevLine 
[490]1#include "globalScopeData.hpp"
[591]2#include "xios_spl.hpp"
[300]3#include "cxios.hpp"
[342]4#include "server.hpp"
[983]5#include "client.hpp"
[300]6#include "type.hpp"
7#include "context.hpp"
[352]8#include "object_template.hpp"
[300]9#include "oasis_cinterface.hpp"
10#include <boost/functional/hash.hpp>
11#include <boost/algorithm/string.hpp>
[382]12#include "mpi.hpp"
[347]13#include "tracer.hpp"
14#include "timer.hpp"
[2428]15#include "mem_checker.hpp"
[492]16#include "event_scheduler.hpp"
[1587]17#include "string_tools.hpp"
[300]18
[335]19namespace xios
[490]20{
[1639]21    MPI_Comm CServer::intraComm ;
22    std::list<MPI_Comm> CServer::interCommLeft ;
23    std::list<MPI_Comm> CServer::interCommRight ;
24    std::list<MPI_Comm> CServer::contextInterComms;
25    std::list<MPI_Comm> CServer::contextIntraComms;
[1021]26    int CServer::serverLevel = 0 ;
[1148]27    int CServer::nbContexts = 0;
[983]28    bool CServer::isRoot = false ;
[1077]29    int CServer::rank_ = INVALID_RANK;
[490]30    StdOFStream CServer::m_infoStream;
[523]31    StdOFStream CServer::m_errorStream;
[490]32    map<string,CContext*> CServer::contextList ;
[1152]33    vector<int> CServer::sndServerGlobalRanks;
[300]34    bool CServer::finished=false ;
35    bool CServer::is_MPI_Initialized ;
[597]36    CEventScheduler* CServer::eventScheduler = 0;
[983]37
38//---------------------------------------------------------------
39/*!
40 * \fn void CServer::initialize(void)
[1054]41 * Creates intraComm for each possible type of servers (classical, primary or secondary).
[1148]42 * Creates interComm and stores them into the following lists:
[1054]43 *   classical server -- interCommLeft
44 *   primary server -- interCommLeft and interCommRight
[1148]45 *   secondary server -- interCommLeft for each pool.
[1234]46 *   IMPORTANT: CXios::usingServer2 should NOT be used beyond this function. Use CServer::serverLevel instead.
[983]47 */
[300]48    void CServer::initialize(void)
49    {
50      int initialized ;
[1639]51      MPI_Initialized(&initialized) ;
[300]52      if (initialized) is_MPI_Initialized=true ;
53      else is_MPI_Initialized=false ;
[1152]54      int rank ;
[490]55
[300]56      // Not using OASIS
57      if (!CXios::usingOasis)
58      {
[490]59
60        if (!is_MPI_Initialized)
[300]61        {
[1639]62          MPI_Init(NULL, NULL);
[300]63        }
[359]64        CTimer::get("XIOS").resume() ;
[490]65
66        boost::hash<string> hashString ;
[1021]67        unsigned long hashServer = hashString(CXios::xiosCodeId);
[490]68
[300]69        unsigned long* hashAll ;
[1152]70        unsigned long* srvLevelAll ;
[490]71
[300]72        int size ;
73        int myColor ;
74        int i,c ;
[1639]75        MPI_Comm newComm;
[490]76
[1639]77        MPI_Comm_size(CXios::globalComm, &size) ;
78        MPI_Comm_rank(CXios::globalComm, &rank_);
[1009]79
[300]80        hashAll=new unsigned long[size] ;
[1639]81        MPI_Allgather(&hashServer, 1, MPI_LONG, hashAll, 1, MPI_LONG, CXios::globalComm) ;
[490]82
[1021]83        map<unsigned long, int> colors ;
[300]84        map<unsigned long, int> leaders ;
85        map<unsigned long, int>::iterator it ;
[490]86
[1152]87        // (1) Establish client leaders, distribute processes between two server levels
[1234]88        std::vector<int> srvRanks;
[300]89        for(i=0,c=0;i<size;i++)
90        {
91          if (colors.find(hashAll[i])==colors.end())
92          {
93            colors[hashAll[i]]=c ;
94            leaders[hashAll[i]]=i ;
95            c++ ;
96          }
[1152]97          if (CXios::usingServer2)
[1180]98            if (hashAll[i] == hashServer)
99              srvRanks.push_back(i);
100        }
[1234]101
102        if (CXios::usingServer2)
[1180]103        {
[1234]104          int reqNbProc = srvRanks.size()*CXios::ratioServer2/100.;
105          if (reqNbProc<1 || reqNbProc==srvRanks.size())
[1152]106          {
[1234]107            error(0)<<"WARNING: void CServer::initialize(void)"<<endl
108                << "It is impossible to dedicate the requested number of processes = "<<reqNbProc
109                <<" to secondary server. XIOS will run in the classical server mode."<<endl;
[1152]110          }
[1180]111          else
112          {
[1519]113            if (CXios::nbPoolsServer2 == 0) CXios::nbPoolsServer2 = reqNbProc;
[1243]114            int firstSndSrvRank = srvRanks.size()*(100.-CXios::ratioServer2)/100. ;
115            int poolLeader = firstSndSrvRank;
116//*********** (1) Comment out the line below to set one process per pool
[1519]117            sndServerGlobalRanks.push_back(srvRanks[poolLeader]);
[1243]118            int nbPools = CXios::nbPoolsServer2;
119            if ( nbPools > reqNbProc || nbPools < 1)
120            {
121              error(0)<<"WARNING: void CServer::initialize(void)"<<endl
122                  << "It is impossible to allocate the requested number of pools = "<<nbPools
123                  <<" on the secondary server. It will be set so that there is one process per pool."<<endl;
124              nbPools = reqNbProc;
125            }
126            int remainder = ((int) (srvRanks.size()*CXios::ratioServer2/100.)) % nbPools;
127            int procsPerPool = ((int) (srvRanks.size()*CXios::ratioServer2/100.)) / nbPools;
[1234]128            for (i=0; i<srvRanks.size(); i++)
129            {
[1243]130              if (i >= firstSndSrvRank)
[1234]131              {
[1243]132                if (rank_ == srvRanks[i])
133                {
134                  serverLevel=2;
135                }
136                poolLeader += procsPerPool;
137                if (remainder != 0)
138                {
139                  ++poolLeader;
140                  --remainder;
141                }
142//*********** (2) Comment out the two lines below to set one process per pool
[1519]143                if (poolLeader < srvRanks.size())
144                  sndServerGlobalRanks.push_back(srvRanks[poolLeader]);
[1243]145//*********** (3) Uncomment the line below to set one process per pool
[1519]146//                sndServerGlobalRanks.push_back(srvRanks[i]);
[1234]147              }
148              else
149              {
150                if (rank_ == srvRanks[i]) serverLevel=1;
151              }
152            }
[1243]153            if (serverLevel==2)
154            {
155              info(50)<<"The number of secondary server pools is "<< sndServerGlobalRanks.size() <<endl ;
156              for (i=0; i<sndServerGlobalRanks.size(); i++)
157              {
158                if (rank_>= sndServerGlobalRanks[i])
159                {
160                  if ( i == sndServerGlobalRanks.size()-1)
161                  {
162                    myColor = colors.size() + sndServerGlobalRanks[i];
163                  }
164                  else if (rank_< sndServerGlobalRanks[i+1])
165                  {
166                    myColor = colors.size() + sndServerGlobalRanks[i];
167                    break;
168                  }
169                }
170              }
171            }
[1180]172          }
[300]173        }
[1243]174
[1152]175        // (2) Create intraComm
[1243]176        if (serverLevel != 2) myColor=colors[hashServer];
[1639]177        MPI_Comm_split(CXios::globalComm, myColor, rank_, &intraComm) ;
[1152]178
179        // (3) Create interComm
[1021]180        if (serverLevel == 0)
[983]181        {
182          int clientLeader;
183          for(it=leaders.begin();it!=leaders.end();it++)
184          {
185            if (it->first!=hashServer)
186            {
187              clientLeader=it->second ;
188              int intraCommSize, intraCommRank ;
[1639]189              MPI_Comm_size(intraComm,&intraCommSize) ;
190              MPI_Comm_rank(intraComm,&intraCommRank) ;
[1142]191              info(50)<<"intercommCreate::server (classical mode) "<<rank_<<" intraCommSize : "<<intraCommSize
[983]192                       <<" intraCommRank :"<<intraCommRank<<"  clientLeader "<< clientLeader<<endl ;
[490]193
[1639]194              MPI_Intercomm_create(intraComm, 0, CXios::globalComm, clientLeader, 0, &newComm) ;
[1180]195              interCommLeft.push_back(newComm) ;
[983]196            }
197          }
198        }
[1021]199        else if (serverLevel == 1)
[983]200        {
[1054]201          int clientLeader, srvSndLeader;
202          int srvPrmLeader ;
[1152]203
[1021]204          for (it=leaders.begin();it!=leaders.end();it++)
[983]205          {
[1021]206            if (it->first != hashServer)
[983]207            {
[1021]208              clientLeader=it->second ;
209              int intraCommSize, intraCommRank ;
[1639]210              MPI_Comm_size(intraComm, &intraCommSize) ;
211              MPI_Comm_rank(intraComm, &intraCommRank) ;
[1142]212              info(50)<<"intercommCreate::server (server level 1) "<<rank_<<" intraCommSize : "<<intraCommSize
[1021]213                       <<" intraCommRank :"<<intraCommRank<<"  clientLeader "<< clientLeader<<endl ;
[1639]214              MPI_Intercomm_create(intraComm, 0, CXios::globalComm, clientLeader, 0, &newComm) ;
[1021]215              interCommLeft.push_back(newComm) ;
[983]216            }
[1021]217          }
[1009]218
[1152]219          for (int i = 0; i < sndServerGlobalRanks.size(); ++i)
[983]220          {
[1054]221            int intraCommSize, intraCommRank ;
[1639]222            MPI_Comm_size(intraComm, &intraCommSize) ;
223            MPI_Comm_rank(intraComm, &intraCommRank) ;
[1142]224            info(50)<<"intercommCreate::client (server level 1) "<<rank_<<" intraCommSize : "<<intraCommSize
[1152]225                <<" intraCommRank :"<<intraCommRank<<"  clientLeader "<< sndServerGlobalRanks[i]<<endl ;
[1639]226            MPI_Intercomm_create(intraComm, 0, CXios::globalComm, sndServerGlobalRanks[i], 1, &newComm) ;
[1054]227            interCommRight.push_back(newComm) ;
[1021]228          }
[1168]229        }
[1021]230        else
231        {
232          int clientLeader;
233          clientLeader = leaders[hashString(CXios::xiosCodeId)];
234          int intraCommSize, intraCommRank ;
[1639]235          MPI_Comm_size(intraComm, &intraCommSize) ;
236          MPI_Comm_rank(intraComm, &intraCommRank) ;
[1142]237          info(50)<<"intercommCreate::server (server level 2) "<<rank_<<" intraCommSize : "<<intraCommSize
[1021]238                   <<" intraCommRank :"<<intraCommRank<<"  clientLeader "<< clientLeader<<endl ;
[983]239
[1639]240          MPI_Intercomm_create(intraComm, 0, CXios::globalComm, clientLeader, 1, &newComm) ;
[1021]241          interCommLeft.push_back(newComm) ;
[1168]242        }
[983]243
244        delete [] hashAll ;
245
[300]246      }
247      // using OASIS
248      else
249      {
[1152]250        int size;
[1130]251        int myColor;
[1234]252        int* srvGlobalRanks;
[490]253        if (!is_MPI_Initialized) oasis_init(CXios::xiosCodeId);
254
[359]255        CTimer::get("XIOS").resume() ;
[1639]256        MPI_Comm localComm;
[655]257        oasis_get_localcomm(localComm);
[1639]258        MPI_Comm_rank(localComm,&rank_) ;
[655]259
[1234]260//      (1) Create server intraComm
[1130]261        if (!CXios::usingServer2)
[1167]262        {
[1639]263          MPI_Comm_dup(localComm, &intraComm);
[1167]264        }
[1130]265        else
266        {
[1180]267          int globalRank;
[1639]268          MPI_Comm_size(localComm,&size) ;
269          MPI_Comm_rank(CXios::globalComm,&globalRank) ;
[1234]270          srvGlobalRanks = new int[size] ;
[1639]271          MPI_Allgather(&globalRank, 1, MPI_INT, srvGlobalRanks, 1, MPI_INT, localComm) ;
[1152]272
[1234]273          int reqNbProc = size*CXios::ratioServer2/100.;
274          if (reqNbProc < 1 || reqNbProc == size)
[1130]275          {
[1234]276            error(0)<<"WARNING: void CServer::initialize(void)"<<endl
277                << "It is impossible to dedicate the requested number of processes = "<<reqNbProc
278                <<" to secondary server. XIOS will run in the classical server mode."<<endl;
[1639]279            MPI_Comm_dup(localComm, &intraComm);
[1130]280          }
281          else
282          {
[1243]283            int firstSndSrvRank = size*(100.-CXios::ratioServer2)/100. ;
284            int poolLeader = firstSndSrvRank;
285//*********** (1) Comment out the line below to set one process per pool
286//            sndServerGlobalRanks.push_back(srvGlobalRanks[poolLeader]);
287            int nbPools = CXios::nbPoolsServer2;
288            if ( nbPools > reqNbProc || nbPools < 1)
289            {
290              error(0)<<"WARNING: void CServer::initialize(void)"<<endl
291                  << "It is impossible to allocate the requested number of pools = "<<nbPools
292                  <<" on the secondary server. It will be set so that there is one process per pool."<<endl;
293              nbPools = reqNbProc;
294            }
295            int remainder = ((int) (size*CXios::ratioServer2/100.)) % nbPools;
296            int procsPerPool = ((int) (size*CXios::ratioServer2/100.)) / nbPools;
[1234]297            for (int i=0; i<size; i++)
298            {
[1243]299              if (i >= firstSndSrvRank)
[1234]300              {
[1243]301                if (globalRank == srvGlobalRanks[i])
302                {
303                  serverLevel=2;
304                }
305                poolLeader += procsPerPool;
306                if (remainder != 0)
307                {
308                  ++poolLeader;
309                  --remainder;
310                }
311//*********** (2) Comment out the two lines below to set one process per pool
312//                if (poolLeader < size)
313//                  sndServerGlobalRanks.push_back(srvGlobalRanks[poolLeader]);
314//*********** (3) Uncomment the line below to set one process per pool
[1234]315                sndServerGlobalRanks.push_back(srvGlobalRanks[i]);
316              }
317              else
318              {
319                if (globalRank == srvGlobalRanks[i]) serverLevel=1;
320              }
321            }
[1243]322            if (serverLevel==2)
323            {
324              info(50)<<"The number of secondary server pools is "<< sndServerGlobalRanks.size() <<endl ;
325              for (int i=0; i<sndServerGlobalRanks.size(); i++)
326              {
327                if (globalRank>= sndServerGlobalRanks[i])
328                {
329                  if (i == sndServerGlobalRanks.size()-1)
330                  {
331                    myColor = sndServerGlobalRanks[i];
332                  }
333                  else if (globalRank< sndServerGlobalRanks[i+1])
334                  {
335                    myColor = sndServerGlobalRanks[i];
336                    break;
337                  }
338                }
339              }
340            }
341            if (serverLevel != 2) myColor=0;
[1639]342            MPI_Comm_split(localComm, myColor, rank_, &intraComm) ;
[1130]343          }
[1234]344        }
[1130]345
[300]346        string codesId=CXios::getin<string>("oasis_codes_id") ;
[1587]347        vector<string> oasisCodeId=splitRegex(codesId,"\\s*,\\s*") ;
348 
[300]349        vector<string>::iterator it ;
350
[1639]351        MPI_Comm newComm ;
[300]352        int globalRank ;
[1639]353        MPI_Comm_rank(CXios::globalComm,&globalRank);
[490]354
[1234]355//      (2) Create interComms with models
[1587]356        for(it=oasisCodeId.begin();it!=oasisCodeId.end();it++)
[300]357        {
358          oasis_get_intercomm(newComm,*it) ;
[1234]359          if ( serverLevel == 0 || serverLevel == 1)
[1150]360          {
[1130]361            interCommLeft.push_back(newComm) ;
[1639]362            if (rank_==0) MPI_Send(&globalRank,1,MPI_INT,0,0,newComm) ;
[1150]363          }
[1180]364        }
365
[1234]366//      (3) Create interComms between primary and secondary servers
[1243]367        int intraCommSize, intraCommRank ;
[1639]368        MPI_Comm_size(intraComm,&intraCommSize) ;
369        MPI_Comm_rank(intraComm, &intraCommRank) ;
[1243]370
[1180]371        if (serverLevel == 1)
372        {
373          for (int i = 0; i < sndServerGlobalRanks.size(); ++i)
[1130]374          {
[1180]375            int srvSndLeader = sndServerGlobalRanks[i];
[1243]376            info(50)<<"intercommCreate::client (server level 1) "<<globalRank<<" intraCommSize : "<<intraCommSize
377                <<" intraCommRank :"<<intraCommRank<<"  clientLeader "<< srvSndLeader<<endl ;
[1639]378            MPI_Intercomm_create(intraComm, 0, CXios::globalComm, srvSndLeader, 0, &newComm) ;
[1180]379            interCommRight.push_back(newComm) ;
[1130]380          }
[300]381        }
[1180]382        else if (serverLevel == 2)
383        {
[1244]384          info(50)<<"intercommCreate::server (server level 2)"<<globalRank<<" intraCommSize : "<<intraCommSize
[1243]385                   <<" intraCommRank :"<<intraCommRank<<"  clientLeader "<< srvGlobalRanks[0] <<endl ;
[1639]386          MPI_Intercomm_create(intraComm, 0, CXios::globalComm, srvGlobalRanks[0], 0, &newComm) ;
[1180]387          interCommLeft.push_back(newComm) ;
388        }
[1184]389        if (CXios::usingServer2) delete [] srvGlobalRanks ;
[1587]390
391        bool oasisEnddef=CXios::getin<bool>("call_oasis_enddef",true) ;
392        if (!oasisEnddef) oasis_enddef() ;
[300]393      }
[490]394
[1184]395
[1639]396      MPI_Comm_rank(intraComm, &rank) ;
[1152]397      if (rank==0) isRoot=true;
[490]398      else isRoot=false;
[492]399     
400      eventScheduler = new CEventScheduler(intraComm) ;
[300]401    }
[490]402
[300]403    void CServer::finalize(void)
404    {
[361]405      CTimer::get("XIOS").suspend() ;
[697]406     
[492]407      delete eventScheduler ;
[655]408
[1639]409      for (std::list<MPI_Comm>::iterator it = contextInterComms.begin(); it != contextInterComms.end(); it++)
[2029]410        /* MPI_Comm_free(&(*it)) */; // WARNING remove freeing communicator !! --> deadlock raised, to be checked
[983]411
[1639]412      for (std::list<MPI_Comm>::iterator it = contextIntraComms.begin(); it != contextIntraComms.end(); it++)
413        MPI_Comm_free(&(*it));
[1071]414
[992]415//      for (std::list<MPI_Comm>::iterator it = interComm.begin(); it != interComm.end(); it++)
416//        MPI_Comm_free(&(*it));
417
[1077]418//        for (std::list<MPI_Comm>::iterator it = interCommLeft.begin(); it != interCommLeft.end(); it++)
419//          MPI_Comm_free(&(*it));
[983]420
[1639]421        for (std::list<MPI_Comm>::iterator it = interCommRight.begin(); it != interCommRight.end(); it++)
[2029]422          /* MPI_Comm_free(&(*it)) */ ; // WARNING remove freeing communicator !! --> deadlock raised, to be checked
[992]423
[1639]424      MPI_Comm_free(&intraComm);
[655]425
[2428]426      CMemChecker::logMem( "CServer::finalize", true );
[300]427      if (!is_MPI_Initialized)
[490]428      {
[300]429        if (CXios::usingOasis) oasis_finalize();
[1639]430        else MPI_Finalize() ;
[300]431      }
[347]432      report(0)<<"Performance report : Time spent for XIOS : "<<CTimer::get("XIOS server").getCumulatedTime()<<endl  ;
433      report(0)<<"Performance report : Time spent in processing events : "<<CTimer::get("Process events").getCumulatedTime()<<endl  ;
434      report(0)<<"Performance report : Ratio : "<<CTimer::get("Process events").getCumulatedTime()/CTimer::get("XIOS server").getCumulatedTime()*100.<<"%"<<endl  ;
[1158]435      report(100)<<CTimer::getAllCumulatedTime()<<endl ;
[2428]436      report(100)<<CMemChecker::getAllCumulatedMem()<<endl ;
[300]437    }
[490]438
[300]439     void CServer::eventLoop(void)
440     {
441       bool stop=false ;
[490]442
[347]443       CTimer::get("XIOS server").resume() ;
[300]444       while(!stop)
445       {
446         if (isRoot)
447         {
448           listenContext();
[1148]449           listenRootContext();
[1587]450           listenOasisEnddef() ;
451           listenRootOasisEnddef() ;
[300]452           if (!finished) listenFinalize() ;
453         }
454         else
455         {
456           listenRootContext();
[1587]457           listenRootOasisEnddef() ;
[300]458           if (!finished) listenRootFinalize() ;
459         }
[490]460
[300]461         contextEventLoop() ;
462         if (finished && contextList.empty()) stop=true ;
[956]463         eventScheduler->checkEvent() ;
[300]464       }
[347]465       CTimer::get("XIOS server").suspend() ;
[300]466     }
[490]467
[300]468     void CServer::listenFinalize(void)
469     {
[1639]470        list<MPI_Comm>::iterator it, itr;
[300]471        int msg ;
472        int flag ;
[490]473
[992]474        for(it=interCommLeft.begin();it!=interCommLeft.end();it++)
[300]475        {
[1639]476           MPI_Status status ;
[347]477           traceOff() ;
[1639]478           MPI_Iprobe(0,0,*it,&flag,&status) ;
[347]479           traceOn() ;
[300]480           if (flag==true)
481           {
[1639]482              MPI_Recv(&msg,1,MPI_INT,0,0,*it,&status) ;
[1054]483              info(20)<<" CServer : Receive client finalize"<<endl ;
484              // Sending server finalize message to secondary servers (if any)
485              for(itr=interCommRight.begin();itr!=interCommRight.end();itr++)
486              {
[1639]487                MPI_Send(&msg,1,MPI_INT,0,0,*itr) ;
[1054]488              }
[2029]489              /* MPI_Comm_free(&(*it)); */ // WARNING remove freeing communicator !! --> deadlock raised, to be checked
[992]490              interCommLeft.erase(it) ;
[300]491              break ;
492            }
493         }
[490]494
[1054]495         if (interCommLeft.empty())
[300]496         {
497           int i,size ;
[1639]498           MPI_Comm_size(intraComm,&size) ;
499           MPI_Request* requests= new MPI_Request[size-1] ;
500           MPI_Status* status= new MPI_Status[size-1] ;
[490]501
[1639]502           for(int i=1;i<size;i++) MPI_Isend(&msg,1,MPI_INT,i,4,intraComm,&requests[i-1]) ;
503           MPI_Waitall(size-1,requests,status) ;
[300]504
505           finished=true ;
506           delete [] requests ;
507           delete [] status ;
508         }
509     }
[490]510
511
[300]512     void CServer::listenRootFinalize()
513     {
514        int flag ;
[1639]515        MPI_Status status ;
[300]516        int msg ;
[490]517
[347]518        traceOff() ;
[1639]519        MPI_Iprobe(0,4,intraComm, &flag, &status) ;
[347]520        traceOn() ;
[300]521        if (flag==true)
522        {
[1639]523           MPI_Recv(&msg,1,MPI_INT,0,4,intraComm,&status) ;
[300]524           finished=true ;
525        }
526      }
[490]527
[1587]528
529   /*!
530    * Root process is listening for an order sent by client to call "oasis_enddef".
531    * The root client of a compound send the order (tag 5). It is probed and received.
532    * When the order has been received from each coumpound, the server root process ping the order to the root processes of the secondary levels of servers (if any).
533    * After, it also inform (asynchronous call) other processes of the communicator that the oasis_enddef call must be done
534    */
535   
536     void CServer::listenOasisEnddef(void)
537     {
538        int flag ;
[1639]539        MPI_Status status ;
540        list<MPI_Comm>::iterator it;
[1587]541        int msg ;
542        static int nbCompound=0 ;
543        int size ;
544        static bool sent=false ;
[1639]545        static MPI_Request* allRequests ;
546        static MPI_Status* allStatus ;
[1587]547
548
549        if (sent)
550        {
[1639]551          MPI_Comm_size(intraComm,&size) ;
552          MPI_Testall(size,allRequests, &flag, allStatus) ;
[1587]553          if (flag==true)
554          {
555            delete [] allRequests ;
556            delete [] allStatus ;
557            sent=false ;
558          }
559        }
560       
561
562        for(it=interCommLeft.begin();it!=interCommLeft.end();it++)
563        {
[1639]564           MPI_Status status ;
[1587]565           traceOff() ;
[1639]566           MPI_Iprobe(0,5,*it,&flag,&status) ;  // tags oasis_endded = 5
[1587]567           traceOn() ;
568           if (flag==true)
569           {
[1639]570              MPI_Recv(&msg,1,MPI_INT,0,5,*it,&status) ; // tags oasis_endded = 5
[1587]571              nbCompound++ ;
572              if (nbCompound==interCommLeft.size())
573              {
[1639]574                for (std::list<MPI_Comm>::iterator it = interCommRight.begin(); it != interCommRight.end(); it++)
[1587]575                {
[1639]576                   MPI_Send(&msg,1,MPI_INT,0,5,*it) ; // tags oasis_endded = 5
[1587]577                }
[1639]578                MPI_Comm_size(intraComm,&size) ;
579                allRequests= new MPI_Request[size] ;
580                allStatus= new MPI_Status[size] ;
581                for(int i=0;i<size;i++) MPI_Isend(&msg,1,MPI_INT,i,5,intraComm,&allRequests[i]) ; // tags oasis_endded = 5
[1587]582                sent=true ;
583              }
584           }
585        }
586     }
587     
588   /*!
589    * Processes probes message from root process if oasis_enddef call must be done.
590    * When the order is received it is scheduled to be treated in a synchronized way by all server processes of the communicator
591    */
592     void CServer::listenRootOasisEnddef(void)
593     {
594       int flag ;
[1639]595       MPI_Status status ;
[1587]596       const int root=0 ;
597       int msg ;
598       static bool eventSent=false ;
599
600       if (eventSent)
601       {
602         boost::hash<string> hashString;
603         size_t hashId = hashString("oasis_enddef");
604         if (eventScheduler->queryEvent(0,hashId))
605         {
606           oasis_enddef() ;
607           eventSent=false ;
608         }
609       }
610         
611       traceOff() ;
[1639]612       MPI_Iprobe(root,5,intraComm, &flag, &status) ;
[1587]613       traceOn() ;
614       if (flag==true)
615       {
[1639]616         MPI_Recv(&msg,1,MPI_INT,root,5,intraComm,&status) ; // tags oasis_endded = 5
[1587]617         boost::hash<string> hashString;
618         size_t hashId = hashString("oasis_enddef");
619         eventScheduler->registerEvent(0,hashId);
620         eventSent=true ;
621       }
622     }
623
624
625
626     
627
[300]628     void CServer::listenContext(void)
629     {
[490]630
[1639]631       MPI_Status status ;
[300]632       int flag ;
[1158]633       static char* buffer ;
[1639]634       static MPI_Request request ;
[300]635       static bool recept=false ;
636       int rank ;
637       int count ;
[490]638
[300]639       if (recept==false)
640       {
[347]641         traceOff() ;
[1639]642         MPI_Iprobe(MPI_ANY_SOURCE,1,CXios::globalComm, &flag, &status) ;
[347]643         traceOn() ;
[490]644         if (flag==true)
[300]645         {
646           rank=status.MPI_SOURCE ;
[1639]647           MPI_Get_count(&status,MPI_CHAR,&count) ;
[300]648           buffer=new char[count] ;
[1639]649           MPI_Irecv((void*)buffer,count,MPI_CHAR,rank,1,CXios::globalComm,&request) ;
[490]650           recept=true ;
[300]651         }
652       }
653       else
654       {
[347]655         traceOff() ;
[1639]656         MPI_Test(&request,&flag,&status) ;
[347]657         traceOn() ;
[300]658         if (flag==true)
659         {
660           rank=status.MPI_SOURCE ;
[1639]661           MPI_Get_count(&status,MPI_CHAR,&count) ;
[1158]662           recvContextMessage((void*)buffer,count) ;
663           delete [] buffer ;
[490]664           recept=false ;
[300]665         }
666       }
667     }
[490]668
[300]669     void CServer::recvContextMessage(void* buff,int count)
670     {
[983]671       static map<string,contextMessage> recvContextId;
[300]672       map<string,contextMessage>::iterator it ;
673       CBufferIn buffer(buff,count) ;
674       string id ;
675       int clientLeader ;
676       int nbMessage ;
677
678       buffer>>id>>nbMessage>>clientLeader ;
[490]679
[300]680       it=recvContextId.find(id) ;
681       if (it==recvContextId.end())
[490]682       {
[300]683         contextMessage msg={0,0} ;
684         pair<map<string,contextMessage>::iterator,bool> ret ;
685         ret=recvContextId.insert(pair<string,contextMessage>(id,msg)) ;
686         it=ret.first ;
[490]687       }
[300]688       it->second.nbRecv+=1 ;
689       it->second.leaderRank+=clientLeader ;
[490]690
[300]691       if (it->second.nbRecv==nbMessage)
[490]692       {
[300]693         int size ;
[1639]694         MPI_Comm_size(intraComm,&size) ;
[1148]695//         MPI_Request* requests= new MPI_Request[size-1] ;
696//         MPI_Status* status= new MPI_Status[size-1] ;
[1639]697         MPI_Request* requests= new MPI_Request[size] ;
698         MPI_Status* status= new MPI_Status[size] ;
[490]699
[1148]700         CMessage msg ;
701         msg<<id<<it->second.leaderRank;
702         int messageSize=msg.size() ;
703         void * sendBuff = new char[messageSize] ;
704         CBufferOut sendBuffer(sendBuff,messageSize) ;
705         sendBuffer<<msg ;
706
707         // Include root itself in order not to have a divergence
708         for(int i=0; i<size; i++)
[300]709         {
[1639]710           MPI_Isend(sendBuff,sendBuffer.count(),MPI_CHAR,i,2,intraComm,&requests[i]) ;
[300]711         }
712
713         recvContextId.erase(it) ;
714         delete [] requests ;
715         delete [] status ;
716
717       }
[490]718     }
719
[300]720     void CServer::listenRootContext(void)
721     {
[1639]722       MPI_Status status ;
[300]723       int flag ;
[1270]724       static std::vector<void*> buffers;
[1639]725       static std::vector<MPI_Request> requests ;
[1270]726       static std::vector<int> counts ;
[1271]727       static std::vector<bool> isEventRegistered ;
[1323]728       static std::vector<bool> isEventQueued ;
[1639]729       MPI_Request request;
[1270]730
[300]731       int rank ;
732       const int root=0 ;
[1148]733       boost::hash<string> hashString;
734       size_t hashId = hashString("RegisterContext");
[490]735
[1270]736       // (1) Receive context id from the root, save it into a buffer
[1271]737       traceOff() ;
[1639]738       MPI_Iprobe(root,2,intraComm, &flag, &status) ;
[1271]739       traceOn() ;
740       if (flag==true)
[300]741       {
[1271]742         counts.push_back(0);
[1639]743         MPI_Get_count(&status,MPI_CHAR,&(counts.back())) ;
[1271]744         buffers.push_back(new char[counts.back()]) ;
745         requests.push_back(request);
[1639]746         MPI_Irecv((void*)(buffers.back()),counts.back(),MPI_CHAR,root,2,intraComm,&(requests.back())) ;
[1271]747         isEventRegistered.push_back(false);
[1323]748         isEventQueued.push_back(false);
[1271]749         nbContexts++;
[300]750       }
[1271]751
752       for (int ctxNb = 0; ctxNb < nbContexts; ctxNb++ )
[300]753       {
[1271]754         // (2) If context id is received, register an event
[1639]755         MPI_Test(&requests[ctxNb],&flag,&status) ;
[1271]756         if (flag==true && !isEventRegistered[ctxNb])
[300]757         {
[1271]758           eventScheduler->registerEvent(ctxNb,hashId);
759           isEventRegistered[ctxNb] = true;
[300]760         }
[1271]761         // (3) If event has been scheduled, call register context
[1323]762         if (eventScheduler->queryEvent(ctxNb,hashId) && !isEventQueued[ctxNb])
[1271]763         {
764           registerContext(buffers[ctxNb],counts[ctxNb]) ;
[1323]765           isEventQueued[ctxNb] = true;
[1271]766           delete [] buffers[ctxNb] ;
767         }
[300]768       }
[1271]769
[490]770     }
771
[655]772     void CServer::registerContext(void* buff, int count, int leaderRank)
[300]773     {
774       string contextId;
[655]775       CBufferIn buffer(buff, count);
[1148]776//       buffer >> contextId;
777       buffer >> contextId>>leaderRank;
[983]778       CContext* context;
[300]779
[680]780       info(20) << "CServer : Register new Context : " << contextId << endl;
[490]781
[680]782       if (contextList.find(contextId) != contextList.end())
783         ERROR("void CServer::registerContext(void* buff, int count, int leaderRank)",
784               << "Context '" << contextId << "' has already been registred");
[490]785
[983]786       context=CContext::create(contextId);
[655]787       contextList[contextId]=context;
788
[1148]789       // Primary or classical server: create communication channel with a client
790       // (1) create interComm (with a client)
791       // (2) initialize client and server (contextClient and contextServer)
[1639]792       MPI_Comm inter;
[1054]793       if (serverLevel < 2)
794       {
[1639]795         MPI_Comm contextInterComm;
796         MPI_Intercomm_create(intraComm, 0, CXios::globalComm, leaderRank, 10+leaderRank, &contextInterComm);
797         MPI_Intercomm_merge(contextInterComm,1,&inter);
798         MPI_Barrier(inter);
799         MPI_Comm_free(&inter);
[1054]800         context->initServer(intraComm,contextInterComm);
801         contextInterComms.push_back(contextInterComm);
[1071]802
[1054]803       }
[1148]804       // Secondary server: create communication channel with a primary server
805       // (1) duplicate interComm with a primary server
806       // (2) initialize client and server (contextClient and contextServer)
807       // Remark: in the case of the secondary server there is no need to create an interComm calling MPI_Intercomm_create,
808       //         because interComm of CContext is defined on the same processes as the interComm of CServer.
809       //         So just duplicate it.
[1054]810       else if (serverLevel == 2)
811       {
[1639]812         MPI_Comm_dup(interCommLeft.front(), &inter);
[1071]813         contextInterComms.push_back(inter);
814         context->initServer(intraComm, contextInterComms.back());
[1054]815       }
816
[1148]817       // Primary server:
818       // (1) send create context message to secondary servers
819       // (2) initialize communication channels with secondary servers (create contextClient and contextServer)
[1021]820       if (serverLevel == 1)
[983]821       {
[1054]822         int i = 0, size;
[1639]823         MPI_Comm_size(intraComm, &size) ;
824         for (std::list<MPI_Comm>::iterator it = interCommRight.begin(); it != interCommRight.end(); it++, ++i)
[1054]825         {
826           StdString str = contextId +"_server_" + boost::lexical_cast<string>(i);
[1352]827           CMessage msg;
828           int messageSize;
[1077]829           msg<<str<<size<<rank_ ;
[1054]830           messageSize = msg.size() ;
831           buff = new char[messageSize] ;
832           CBufferOut buffer(buff,messageSize) ;
833           buffer<<msg ;
[1639]834           MPI_Send(buff, buffer.count(), MPI_CHAR, sndServerGlobalRanks[i], 1, CXios::globalComm) ;
835           MPI_Comm_dup(*it, &inter);
[1071]836           contextInterComms.push_back(inter);
[1639]837           MPI_Comm_dup(intraComm, &inter);
[1071]838           contextIntraComms.push_back(inter);
839           context->initClient(contextIntraComms.back(), contextInterComms.back()) ;
[1054]840           delete [] buff ;
841         }
[983]842       }
[490]843     }
844
[1377]845     void CServer::contextEventLoop(bool enableEventsProcessing /*= true*/)
[300]846     {
[1130]847       bool isFinalized ;
[300]848       map<string,CContext*>::iterator it ;
[983]849
[490]850       for(it=contextList.begin();it!=contextList.end();it++)
[300]851       {
[1130]852         isFinalized=it->second->isFinalized();
853         if (isFinalized)
[300]854         {
855           contextList.erase(it) ;
856           break ;
857         }
[1054]858         else
[1377]859           it->second->checkBuffersAndListen(enableEventsProcessing);
[300]860       }
861     }
[490]862
[1148]863     //! Get rank of the current process in the intraComm
[490]864     int CServer::getRank()
865     {
[1167]866       int rank;
[1639]867       MPI_Comm_rank(intraComm,&rank);
[1167]868       return rank;
[490]869     }
870
[1168]871     vector<int>& CServer::getSecondaryServerGlobalRanks()
872     {
873       return sndServerGlobalRanks;
874     }
875
[523]876    /*!
877    * Open a file specified by a suffix and an extension and use it for the given file buffer.
878    * The file name will be suffix+rank+extension.
879    *
880    * \param fileName[in] protype file name
881    * \param ext [in] extension of the file
882    * \param fb [in/out] the file buffer
883    */
884    void CServer::openStream(const StdString& fileName, const StdString& ext, std::filebuf* fb)
885    {
886      StdStringStream fileNameClient;
887      int numDigit = 0;
888      int size = 0;
[1021]889      int id;
[1639]890      MPI_Comm_size(CXios::globalComm, &size);
[523]891      while (size)
892      {
893        size /= 10;
894        ++numDigit;
895      }
[1167]896      id = rank_; //getRank();
[497]897
[1021]898      fileNameClient << fileName << "_" << std::setfill('0') << std::setw(numDigit) << id << ext;
[523]899      fb->open(fileNameClient.str().c_str(), std::ios::out);
900      if (!fb->is_open())
901        ERROR("void CServer::openStream(const StdString& fileName, const StdString& ext, std::filebuf* fb)",
[1542]902              << std::endl << "Can not open <" << fileNameClient.str() << "> file to write the server log(s).");
[523]903    }
[490]904
[523]905    /*!
906    * \brief Open a file stream to write the info logs
907    * Open a file stream with a specific file name suffix+rank
908    * to write the info logs.
909    * \param fileName [in] protype file name
910    */
911    void CServer::openInfoStream(const StdString& fileName)
912    {
913      std::filebuf* fb = m_infoStream.rdbuf();
914      openStream(fileName, ".out", fb);
[490]915
[523]916      info.write2File(fb);
917      report.write2File(fb);
918    }
[490]919
[523]920    //! Write the info logs to standard output
921    void CServer::openInfoStream()
922    {
923      info.write2StdOut();
924      report.write2StdOut();
925    }
[490]926
[523]927    //! Close the info logs file if it opens
928    void CServer::closeInfoStream()
929    {
930      if (m_infoStream.is_open()) m_infoStream.close();
931    }
932
933    /*!
934    * \brief Open a file stream to write the error log
935    * Open a file stream with a specific file name suffix+rank
936    * to write the error log.
937    * \param fileName [in] protype file name
938    */
939    void CServer::openErrorStream(const StdString& fileName)
940    {
941      std::filebuf* fb = m_errorStream.rdbuf();
942      openStream(fileName, ".err", fb);
943
944      error.write2File(fb);
945    }
946
947    //! Write the error log to standard error output
948    void CServer::openErrorStream()
949    {
950      error.write2StdErr();
951    }
952
953    //! Close the error log file if it opens
954    void CServer::closeErrorStream()
955    {
956      if (m_errorStream.is_open()) m_errorStream.close();
957    }
[300]958}
Note: See TracBrowser for help on using the repository browser.