source: XIOS2/trunk/src/client.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: 13.2 KB
RevLine 
[490]1#include "globalScopeData.hpp"
[591]2#include "xios_spl.hpp"
[300]3#include "cxios.hpp"
[342]4#include "client.hpp"
[300]5#include <boost/functional/hash.hpp>
6#include "type.hpp"
7#include "context.hpp"
8#include "context_client.hpp"
9#include "oasis_cinterface.hpp"
[382]10#include "mpi.hpp"
[347]11#include "timer.hpp"
[2428]12#include "mem_checker.hpp" 
[400]13#include "buffer_client.hpp"
[1587]14#include "string_tools.hpp"
[300]15
[335]16namespace xios
[490]17{
[300]18
[1639]19    MPI_Comm CClient::intraComm ;
20    MPI_Comm CClient::interComm ;
21    std::list<MPI_Comm> CClient::contextInterComms;
[1158]22    int CClient::serverLeader ;
[300]23    bool CClient::is_MPI_Initialized ;
[1148]24    int CClient::rank_ = INVALID_RANK;
[490]25    StdOFStream CClient::m_infoStream;
[523]26    StdOFStream CClient::m_errorStream;
[1639]27    MPI_Comm& CClient::getInterComm(void)   { return (interComm); }
[1587]28     
[983]29///---------------------------------------------------------------
30/*!
31 * \fn void CClient::initialize(const string& codeId, MPI_Comm& localComm, MPI_Comm& returnComm)
32 * Function creates intraComm (CClient::intraComm) for client group with id=codeId and interComm (CClient::interComm) between client and server groups.
33 * \param [in] codeId identity of context.
34 * \param [in/out] localComm local communicator.
35 * \param [in/out] returnComm (intra)communicator of client group.
36 */
37
[1639]38    void CClient::initialize(const string& codeId, MPI_Comm& localComm, MPI_Comm& returnComm)
[300]39    {
40      int initialized ;
[1639]41      MPI_Initialized(&initialized) ;
[300]42      if (initialized) is_MPI_Initialized=true ;
43      else is_MPI_Initialized=false ;
[1152]44      int rank ;
[490]45
[300]46// don't use OASIS
47      if (!CXios::usingOasis)
48      {
[983]49// localComm isn't given
[1639]50        if (localComm == MPI_COMM_NULL)
[300]51        {
[490]52          if (!is_MPI_Initialized)
[300]53          {
[1639]54            MPI_Init(NULL, NULL);
[300]55          }
[359]56          CTimer::get("XIOS").resume() ;
[1158]57          CTimer::get("XIOS init/finalize").resume() ;
[490]58          boost::hash<string> hashString ;
59
[1158]60          unsigned long hashClient=hashString(codeId) ;
61          unsigned long hashServer=hashString(CXios::xiosCodeId) ;
[300]62          unsigned long* hashAll ;
63          int size ;
64          int myColor ;
65          int i,c ;
[1639]66          MPI_Comm newComm ;
[490]67
[1639]68          MPI_Comm_size(CXios::globalComm,&size) ;
69          MPI_Comm_rank(CXios::globalComm,&rank_);
[490]70
[300]71          hashAll=new unsigned long[size] ;
[490]72
[1639]73          MPI_Allgather(&hashClient,1,MPI_LONG,hashAll,1,MPI_LONG,CXios::globalComm) ;
[300]74
75          map<unsigned long, int> colors ;
76          map<unsigned long, int> leaders ;
[490]77
[300]78          for(i=0,c=0;i<size;i++)
79          {
80            if (colors.find(hashAll[i])==colors.end())
81            {
82              colors[hashAll[i]] =c ;
83              leaders[hashAll[i]]=i ;
84              c++ ;
85            }
86          }
[490]87
[491]88          // Verify whether we are on server mode or not
89          CXios::setNotUsingServer();
90          for (i=0; i < size; ++i)
91          {
[1158]92            if (hashServer == hashAll[i])
[491]93            {
94              CXios::setUsingServer();
95              break;
96            }
97          }
98
[1243]99          myColor=colors[hashClient];
[1639]100          MPI_Comm_split(CXios::globalComm,myColor,rank_,&intraComm) ;
[300]101
102          if (CXios::usingServer)
[490]103          {
[300]104            int clientLeader=leaders[hashClient] ;
[1148]105            serverLeader=leaders[hashServer] ;
[493]106            int intraCommSize, intraCommRank ;
[1639]107            MPI_Comm_size(intraComm,&intraCommSize) ;
108            MPI_Comm_rank(intraComm,&intraCommRank) ;
[1148]109            info(50)<<"intercommCreate::client "<<rank_<<" intraCommSize : "<<intraCommSize
110                   <<" intraCommRank :"<<intraCommRank<<"  clientLeader "<< serverLeader<<endl ;
[1639]111             MPI_Intercomm_create(intraComm, 0, CXios::globalComm, serverLeader, 0, &interComm) ;
[1152]112             //rank_ = intraCommRank;
[300]113          }
114          else
115          {
[1639]116            MPI_Comm_dup(intraComm,&interComm) ;
[300]117          }
118          delete [] hashAll ;
119        }
120        // localComm argument is given
[490]121        else
[300]122        {
123          if (CXios::usingServer)
[490]124          {
[300]125            //ERROR("void CClient::initialize(const string& codeId,MPI_Comm& localComm,MPI_Comm& returnComm)", << " giving a local communictor is not compatible with using server mode") ;
126          }
127          else
128          {
[1639]129            MPI_Comm_dup(localComm,&intraComm) ;
130            MPI_Comm_dup(intraComm,&interComm) ;
[300]131          }
132        }
133      }
134      // using OASIS
135      else
136      {
[983]137        // localComm isn't given
[1639]138        if (localComm == MPI_COMM_NULL)
[300]139        {
140          if (!is_MPI_Initialized) oasis_init(codeId) ;
[655]141          oasis_get_localcomm(localComm) ;
[300]142        }
[1639]143        MPI_Comm_dup(localComm,&intraComm) ;
[655]144
[359]145        CTimer::get("XIOS").resume() ;
[1158]146        CTimer::get("XIOS init/finalize").resume() ;
[511]147
148        if (CXios::usingServer)
[300]149        {
[1639]150          MPI_Status status ;
151          MPI_Comm_rank(intraComm,&rank_) ;
[506]152
[300]153          oasis_get_intercomm(interComm,CXios::xiosCodeId) ;
[1639]154          if (rank_==0) MPI_Recv(&serverLeader,1, MPI_INT, 0, 0, interComm, &status) ;
155          MPI_Bcast(&serverLeader,1,MPI_INT,0,intraComm) ;
[300]156        }
[1639]157        else MPI_Comm_dup(intraComm,&interComm) ;
[300]158      }
[490]159
[1639]160      MPI_Comm_dup(intraComm,&returnComm) ;
[300]161    }
[490]162
[983]163///---------------------------------------------------------------
164/*!
165 * \fn void CClient::registerContext(const string& id, MPI_Comm contextComm)
[1021]166 * \brief Sends a request to create a context to server. Creates client/server contexts.
[983]167 * \param [in] id id of context.
168 * \param [in] contextComm.
[1021]169 * Function is only called by client.
[983]170 */
[1639]171    void CClient::registerContext(const string& id, MPI_Comm contextComm)
[300]172    {
[346]173      CContext::setCurrent(id) ;
[511]174      CContext* context=CContext::create(id);
175      StdString idServer(id);
176      idServer += "_server";
[490]177
[983]178      if (CXios::isServer && !context->hasServer)
179      // Attached mode
[300]180      {
[1639]181        MPI_Comm contextInterComm ;
182        MPI_Comm_dup(contextComm,&contextInterComm) ;
[983]183        CContext* contextServer = CContext::create(idServer);
184
185        // Firstly, initialize context on client side
186        context->initClient(contextComm,contextInterComm, contextServer);
187
188        // Secondly, initialize context on server side
189        contextServer->initServer(contextComm,contextInterComm, context);
190
191        // Finally, we should return current context to context client
192        CContext::setCurrent(id);
193
194        contextInterComms.push_back(contextInterComm);
195      }
196      else
197      {
[300]198        int size,rank,globalRank ;
199        size_t message_size ;
200        int leaderRank ;
[1639]201        MPI_Comm contextInterComm ;
[490]202
[1639]203        MPI_Comm_size(contextComm,&size) ;
204        MPI_Comm_rank(contextComm,&rank) ;
205        MPI_Comm_rank(CXios::globalComm,&globalRank) ;
[300]206        if (rank!=0) globalRank=0 ;
[490]207
[300]208        CMessage msg ;
[511]209        msg<<idServer<<size<<globalRank ;
210//        msg<<id<<size<<globalRank ;
[300]211
212        int messageSize=msg.size() ;
[1158]213        char * buff = new char[messageSize] ;
214        CBufferOut buffer((void*)buff,messageSize) ;
[300]215        buffer<<msg ;
[490]216
[1639]217        MPI_Send((void*)buff,buffer.count(),MPI_CHAR,serverLeader,1,CXios::globalComm) ;
[1158]218
[1639]219        MPI_Intercomm_create(contextComm,0,CXios::globalComm,serverLeader,10+globalRank,&contextInterComm) ;
[1148]220        info(10)<<"Register new Context : "<<id<<endl ;
[1639]221        MPI_Comm inter ;
222        MPI_Intercomm_merge(contextInterComm,0,&inter) ;
223        MPI_Barrier(inter) ;
[1858]224        MPI_Comm_free(&inter);
[490]225
[1148]226        context->initClient(contextComm,contextInterComm) ;
[490]227
[1148]228        contextInterComms.push_back(contextInterComm);
[1009]229        delete [] buff ;
[300]230
231      }
232    }
[490]233
[1587]234/*!
235 * \fn void CClient::callOasisEnddef(void)
236 * \brief Send the order to the servers to call "oasis_enddef". It must be done by each compound of models before calling oasis_enddef on client side
237 * Function is only called by client.
238 */
239    void CClient::callOasisEnddef(void)
240    {
241      bool oasisEnddef=CXios::getin<bool>("call_oasis_enddef",true) ;
242      if (!oasisEnddef) ERROR("void CClient::callOasisEnddef(void)", <<"Function xios_oasis_enddef called but variable <call_oasis_enddef> is set to false."<<endl
243                                                                     <<"Variable <call_oasis_enddef> must be set to true"<<endl) ;
244      if (CXios::isServer)
245      // Attached mode
246      {
247        // nothing to do   
248      }
249      else
250      {
251        int rank ;
252        int msg=0 ;
253
[1639]254        MPI_Comm_rank(intraComm,&rank) ;
[1587]255        if (rank==0) 
256        {
[1639]257          MPI_Send(&msg,1,MPI_INT,0,5,interComm) ; // tags oasis_endded = 5
[1587]258        }
259
260      }
261    }
262
263
[300]264    void CClient::finalize(void)
265    {
266      int rank ;
267      int msg=0 ;
[697]268
[1639]269      MPI_Comm_rank(intraComm,&rank) ;
[697]270 
[992]271      if (!CXios::isServer)
[300]272      {
[1639]273        MPI_Comm_rank(intraComm,&rank) ;
[490]274        if (rank==0)
[332]275        {
[1639]276          MPI_Send(&msg,1,MPI_INT,0,0,interComm) ;
[332]277        }
[300]278      }
[490]279
[1866]280
[2029]281      /* MPI_Comm_free(&interComm); */ // WARNING remove freeing communicator !! --> deadlock raised, to be checked
[1639]282      for (std::list<MPI_Comm>::iterator it = contextInterComms.begin(); it != contextInterComms.end(); it++)
[2029]283        /* MPI_Comm_free(&(*it)) */ ; // WARNING remove freeing communicator !! --> deadlock raised, to be checked
[1639]284      MPI_Comm_free(&intraComm);
[361]285
[1158]286      CTimer::get("XIOS init/finalize").suspend() ;
[655]287      CTimer::get("XIOS").suspend() ;
288
[300]289      if (!is_MPI_Initialized)
290      {
291        if (CXios::usingOasis) oasis_finalize();
[1639]292        else MPI_Finalize() ;
[300]293      }
[697]294     
[300]295      info(20) << "Client side context is finalized"<<endl ;
[1158]296      report(0) <<" Performance report : Whole time from XIOS init and finalize: "<< CTimer::get("XIOS init/finalize").getCumulatedTime()<<" s"<<endl ;
[490]297      report(0) <<" Performance report : total time spent for XIOS : "<< CTimer::get("XIOS").getCumulatedTime()<<" s"<<endl ;
[347]298      report(0)<< " Performance report : time spent for waiting free buffer : "<< CTimer::get("Blocking time").getCumulatedTime()<<" s"<<endl ;
[1158]299      report(0)<< " Performance report : Ratio : "<< CTimer::get("Blocking time").getCumulatedTime()/CTimer::get("XIOS init/finalize").getCumulatedTime()*100.<<" %"<<endl ;
[347]300      report(0)<< " Performance report : This ratio must be close to zero. Otherwise it may be usefull to increase buffer size or numbers of server"<<endl ;
[511]301//      report(0)<< " Memory report : Current buffer_size : "<<CXios::bufferSize<<endl ;
[732]302      report(0)<< " Memory report : Minimum buffer size required : " << CClientBuffer::maxRequestSize << " bytes" << endl ;
[400]303      report(0)<< " Memory report : increasing it by a factor will increase performance, depending of the volume of data wrote in file at each time step of the file"<<endl ;
[1158]304      report(100)<<CTimer::getAllCumulatedTime()<<endl ;
[2428]305      report(100)<<CMemChecker::getAllCumulatedMem()<<endl ;
[400]306   }
[490]307
[1148]308    /*!
[1243]309    * Return global rank without oasis and current rank in model intraComm in case of oasis
[1148]310    */
[490]311   int CClient::getRank()
312   {
[1148]313     return rank_;
[490]314   }
315
[523]316    /*!
317    * Open a file specified by a suffix and an extension and use it for the given file buffer.
318    * The file name will be suffix+rank+extension.
319    *
320    * \param fileName[in] protype file name
321    * \param ext [in] extension of the file
322    * \param fb [in/out] the file buffer
323    */
324    void CClient::openStream(const StdString& fileName, const StdString& ext, std::filebuf* fb)
325    {
326      StdStringStream fileNameClient;
327      int numDigit = 0;
328      int size = 0;
[1233]329      int rank;
[1639]330      MPI_Comm_size(CXios::globalComm, &size);
[523]331      while (size)
332      {
333        size /= 10;
334        ++numDigit;
335      }
[497]336
[1233]337      if (CXios::usingOasis)
338      {
[1639]339        MPI_Comm_rank(CXios::globalComm,&rank);
[1233]340        fileNameClient << fileName << "_" << std::setfill('0') << std::setw(numDigit) << rank << ext;
341      }
342      else
343        fileNameClient << fileName << "_" << std::setfill('0') << std::setw(numDigit) << getRank() << ext;
344
345
[523]346      fb->open(fileNameClient.str().c_str(), std::ios::out);
347      if (!fb->is_open())
348        ERROR("void CClient::openStream(const StdString& fileName, const StdString& ext, std::filebuf* fb)",
[1542]349              << std::endl << "Can not open <" << fileNameClient.str() << "> file to write the client log(s).");
[523]350    }
[490]351
[523]352    /*!
353    * \brief Open a file stream to write the info logs
354    * Open a file stream with a specific file name suffix+rank
355    * to write the info logs.
356    * \param fileName [in] protype file name
357    */
358    void CClient::openInfoStream(const StdString& fileName)
359    {
360      std::filebuf* fb = m_infoStream.rdbuf();
361      openStream(fileName, ".out", fb);
[490]362
[523]363      info.write2File(fb);
364      report.write2File(fb);
365    }
[490]366
[523]367    //! Write the info logs to standard output
368    void CClient::openInfoStream()
369    {
370      info.write2StdOut();
371      report.write2StdOut();
372    }
[490]373
[523]374    //! Close the info logs file if it opens
375    void CClient::closeInfoStream()
376    {
377      if (m_infoStream.is_open()) m_infoStream.close();
378    }
[490]379
[523]380    /*!
381    * \brief Open a file stream to write the error log
382    * Open a file stream with a specific file name suffix+rank
383    * to write the error log.
384    * \param fileName [in] protype file name
385    */
386    void CClient::openErrorStream(const StdString& fileName)
387    {
388      std::filebuf* fb = m_errorStream.rdbuf();
389      openStream(fileName, ".err", fb);
390
391      error.write2File(fb);
392    }
393
394    //! Write the error log to standard error output
395    void CClient::openErrorStream()
396    {
397      error.write2StdErr();
398    }
399
400    //! Close the error log file if it opens
401    void CClient::closeErrorStream()
402    {
403      if (m_errorStream.is_open()) m_errorStream.close();
404    }
[300]405}
Note: See TracBrowser for help on using the repository browser.