source: XIOS/trunk/src/client.cpp @ 1638

Last change on this file since 1638 was 1638, checked in by yushan, 5 years ago

dev on ADA

  • 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.4 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"
[1638]11//#include "mpi_wrapper.hpp"
[347]12#include "timer.hpp"
[400]13#include "buffer_client.hpp"
[1587]14#include "string_tools.hpp"
[300]15
[335]16namespace xios
[490]17{
[300]18
[1638]19    ep_lib::MPI_Comm CClient::intraComm ;
20    ep_lib::MPI_Comm CClient::interComm ;
21    std::list<ep_lib::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;
[1638]27    ep_lib::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
[1638]38    void CClient::initialize(const string& codeId, ep_lib::MPI_Comm& localComm, ep_lib::MPI_Comm& returnComm)
[300]39    {
40      int initialized ;
[1638]41      ep_lib::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
[1638]50        if (localComm == EP_COMM_NULL)
[300]51        {
[490]52          if (!is_MPI_Initialized)
[300]53          {
[1638]54            ep_lib::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 ;
[1638]66          ep_lib::MPI_Comm newComm ;
[490]67
[1638]68          ep_lib::MPI_Comm_size(CXios::globalComm,&size) ;
[490]69
[1638]70          ep_lib::MPI_Comm_rank(CXios::globalComm,&rank_);
71
[300]72          hashAll=new unsigned long[size] ;
[490]73
[1638]74          ep_lib::MPI_Allgather(&hashClient,1,EP_LONG,hashAll,1,EP_LONG,CXios::globalComm) ;
[300]75
76          map<unsigned long, int> colors ;
77          map<unsigned long, int> leaders ;
[490]78
[300]79          for(i=0,c=0;i<size;i++)
80          {
81            if (colors.find(hashAll[i])==colors.end())
82            {
83              colors[hashAll[i]] =c ;
84              leaders[hashAll[i]]=i ;
85              c++ ;
86            }
87          }
[490]88
[491]89          // Verify whether we are on server mode or not
90          CXios::setNotUsingServer();
91          for (i=0; i < size; ++i)
92          {
[1158]93            if (hashServer == hashAll[i])
[491]94            {
95              CXios::setUsingServer();
96              break;
97            }
98          }
99
[1243]100          myColor=colors[hashClient];
[1638]101          ep_lib::MPI_Comm_split(CXios::globalComm,myColor,rank_,&intraComm) ;
[300]102
103          if (CXios::usingServer)
[490]104          {
[300]105            int clientLeader=leaders[hashClient] ;
[1148]106            serverLeader=leaders[hashServer] ;
[493]107            int intraCommSize, intraCommRank ;
[1638]108            ep_lib::MPI_Comm_size(intraComm,&intraCommSize) ;
109            ep_lib::MPI_Comm_rank(intraComm,&intraCommRank) ;
[1148]110            info(50)<<"intercommCreate::client "<<rank_<<" intraCommSize : "<<intraCommSize
111                   <<" intraCommRank :"<<intraCommRank<<"  clientLeader "<< serverLeader<<endl ;
[1638]112             ep_lib::MPI_Intercomm_create(intraComm, 0, CXios::globalComm, serverLeader, 0, &interComm) ;
[1152]113             //rank_ = intraCommRank;
[300]114          }
115          else
116          {
[1638]117            ep_lib::MPI_Comm_dup(intraComm,&interComm) ;
[300]118          }
119          delete [] hashAll ;
120        }
121        // localComm argument is given
[490]122        else
[300]123        {
124          if (CXios::usingServer)
[490]125          {
[300]126            //ERROR("void CClient::initialize(const string& codeId,MPI_Comm& localComm,MPI_Comm& returnComm)", << " giving a local communictor is not compatible with using server mode") ;
127          }
128          else
129          {
[1638]130            ep_lib::MPI_Comm_dup(localComm,&intraComm) ;
131            ep_lib::MPI_Comm_dup(intraComm,&interComm) ;
[300]132          }
133        }
134      }
135      // using OASIS
136      else
137      {
[983]138        // localComm isn't given
[1638]139        if (localComm == EP_COMM_NULL)
[300]140        {
141          if (!is_MPI_Initialized) oasis_init(codeId) ;
[655]142          oasis_get_localcomm(localComm) ;
[300]143        }
[1638]144        ep_lib::MPI_Comm_dup(localComm,&intraComm) ;
[655]145
[359]146        CTimer::get("XIOS").resume() ;
[1158]147        CTimer::get("XIOS init/finalize").resume() ;
[511]148
149        if (CXios::usingServer)
[300]150        {
[1638]151          ep_lib::MPI_Status status ;
152          ep_lib::MPI_Comm_rank(intraComm,&rank_) ;
[506]153
[300]154          oasis_get_intercomm(interComm,CXios::xiosCodeId) ;
[1638]155          if (rank_==0) ep_lib::MPI_Recv(&serverLeader,1, EP_INT, 0, 0, interComm, &status) ;
156          ep_lib::MPI_Bcast(&serverLeader,1,EP_INT,0,intraComm) ;
[300]157        }
[1638]158        else ep_lib::MPI_Comm_dup(intraComm,&interComm) ;
[300]159      }
[490]160
[1638]161      ep_lib::MPI_Comm_dup(intraComm,&returnComm) ;
[300]162    }
[490]163
[983]164///---------------------------------------------------------------
165/*!
166 * \fn void CClient::registerContext(const string& id, MPI_Comm contextComm)
[1021]167 * \brief Sends a request to create a context to server. Creates client/server contexts.
[983]168 * \param [in] id id of context.
169 * \param [in] contextComm.
[1021]170 * Function is only called by client.
[983]171 */
[1638]172    void CClient::registerContext(const string& id, ep_lib::MPI_Comm contextComm)
[300]173    {
[346]174      CContext::setCurrent(id) ;
[511]175      CContext* context=CContext::create(id);
176      StdString idServer(id);
177      idServer += "_server";
[490]178
[983]179      if (CXios::isServer && !context->hasServer)
180      // Attached mode
[300]181      {
[1638]182        ep_lib::MPI_Comm contextInterComm ;
183        ep_lib::MPI_Comm_dup(contextComm,&contextInterComm) ;
[983]184        CContext* contextServer = CContext::create(idServer);
185
186        // Firstly, initialize context on client side
187        context->initClient(contextComm,contextInterComm, contextServer);
188
189        // Secondly, initialize context on server side
190        contextServer->initServer(contextComm,contextInterComm, context);
191
192        // Finally, we should return current context to context client
193        CContext::setCurrent(id);
194
195        contextInterComms.push_back(contextInterComm);
196      }
197      else
198      {
[300]199        int size,rank,globalRank ;
200        size_t message_size ;
201        int leaderRank ;
[1638]202        ep_lib::MPI_Comm contextInterComm ;
[490]203
[1638]204        ep_lib::MPI_Comm_size(contextComm,&size) ;
205        ep_lib::MPI_Comm_rank(contextComm,&rank) ;
206        ep_lib::MPI_Comm_rank(CXios::globalComm,&globalRank) ;
[300]207        if (rank!=0) globalRank=0 ;
[490]208
[300]209        CMessage msg ;
[511]210        msg<<idServer<<size<<globalRank ;
211//        msg<<id<<size<<globalRank ;
[300]212
213        int messageSize=msg.size() ;
[1158]214        char * buff = new char[messageSize] ;
215        CBufferOut buffer((void*)buff,messageSize) ;
[300]216        buffer<<msg ;
[490]217
[1638]218        ep_lib::MPI_Send((void*)buff,buffer.count(),EP_CHAR,serverLeader,1,CXios::globalComm) ;
[1158]219
[1638]220        ep_lib::MPI_Intercomm_create(contextComm,0,CXios::globalComm,serverLeader,10+globalRank,&contextInterComm) ;
[1148]221        info(10)<<"Register new Context : "<<id<<endl ;
[1638]222        ep_lib::MPI_Comm inter ;
223        ep_lib::MPI_Intercomm_merge(contextInterComm,0,&inter) ;
224        ep_lib::MPI_Barrier(inter) ;
[490]225
[1148]226        context->initClient(contextComm,contextInterComm) ;
[490]227
[1148]228        contextInterComms.push_back(contextInterComm);
[1638]229        ep_lib::MPI_Comm_free(&inter);
[1009]230        delete [] buff ;
[300]231
232      }
233    }
[490]234
[1587]235/*!
236 * \fn void CClient::callOasisEnddef(void)
237 * \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
238 * Function is only called by client.
239 */
240    void CClient::callOasisEnddef(void)
241    {
242      bool oasisEnddef=CXios::getin<bool>("call_oasis_enddef",true) ;
243      if (!oasisEnddef) ERROR("void CClient::callOasisEnddef(void)", <<"Function xios_oasis_enddef called but variable <call_oasis_enddef> is set to false."<<endl
244                                                                     <<"Variable <call_oasis_enddef> must be set to true"<<endl) ;
245      if (CXios::isServer)
246      // Attached mode
247      {
248        // nothing to do   
249      }
250      else
251      {
252        int rank ;
253        int msg=0 ;
254
[1638]255        ep_lib::MPI_Comm_rank(intraComm,&rank) ;
[1587]256        if (rank==0) 
257        {
[1638]258          ep_lib::MPI_Send(&msg,1,EP_INT,0,5,interComm) ; // tags oasis_endded = 5
[1587]259        }
260
261      }
262    }
263
264
[300]265    void CClient::finalize(void)
266    {
267      int rank ;
268      int msg=0 ;
[697]269
[1638]270      ep_lib::MPI_Comm_rank(intraComm,&rank) ;
[697]271 
[992]272      if (!CXios::isServer)
[300]273      {
[1638]274        ep_lib::MPI_Comm_rank(intraComm,&rank) ;
[490]275        if (rank==0)
[332]276        {
[1638]277          ep_lib::MPI_Send(&msg,1,EP_INT,0,0,interComm) ;
[332]278        }
[300]279      }
[490]280
[1638]281      for (std::list<ep_lib::MPI_Comm>::iterator it = contextInterComms.begin(); it != contextInterComms.end(); it++)
282        ep_lib::MPI_Comm_free(&(*it));
283      ep_lib::MPI_Comm_free(&interComm);
284      ep_lib::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();
[1638]292        else ep_lib::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 ;
[400]305   }
[490]306
[1148]307    /*!
[1243]308    * Return global rank without oasis and current rank in model intraComm in case of oasis
[1148]309    */
[490]310   int CClient::getRank()
311   {
[1148]312     return rank_;
[490]313   }
314
[523]315    /*!
316    * Open a file specified by a suffix and an extension and use it for the given file buffer.
317    * The file name will be suffix+rank+extension.
318    *
319    * \param fileName[in] protype file name
320    * \param ext [in] extension of the file
321    * \param fb [in/out] the file buffer
322    */
323    void CClient::openStream(const StdString& fileName, const StdString& ext, std::filebuf* fb)
324    {
325      StdStringStream fileNameClient;
326      int numDigit = 0;
327      int size = 0;
[1233]328      int rank;
[1638]329      ep_lib::MPI_Comm_size(CXios::globalComm, &size);
[523]330      while (size)
331      {
332        size /= 10;
333        ++numDigit;
334      }
[497]335
[1233]336      if (CXios::usingOasis)
337      {
[1638]338        ep_lib::MPI_Comm_rank(CXios::globalComm,&rank);
[1233]339        fileNameClient << fileName << "_" << std::setfill('0') << std::setw(numDigit) << rank << ext;
340      }
341      else
342        fileNameClient << fileName << "_" << std::setfill('0') << std::setw(numDigit) << getRank() << ext;
343
344
[523]345      fb->open(fileNameClient.str().c_str(), std::ios::out);
346      if (!fb->is_open())
347        ERROR("void CClient::openStream(const StdString& fileName, const StdString& ext, std::filebuf* fb)",
[1542]348              << std::endl << "Can not open <" << fileNameClient.str() << "> file to write the client log(s).");
[523]349    }
[490]350
[523]351    /*!
352    * \brief Open a file stream to write the info logs
353    * Open a file stream with a specific file name suffix+rank
354    * to write the info logs.
355    * \param fileName [in] protype file name
356    */
357    void CClient::openInfoStream(const StdString& fileName)
358    {
359      std::filebuf* fb = m_infoStream.rdbuf();
360      openStream(fileName, ".out", fb);
[490]361
[523]362      info.write2File(fb);
363      report.write2File(fb);
364    }
[490]365
[523]366    //! Write the info logs to standard output
367    void CClient::openInfoStream()
368    {
369      info.write2StdOut();
370      report.write2StdOut();
371    }
[490]372
[523]373    //! Close the info logs file if it opens
374    void CClient::closeInfoStream()
375    {
376      if (m_infoStream.is_open()) m_infoStream.close();
377    }
[490]378
[523]379    /*!
380    * \brief Open a file stream to write the error log
381    * Open a file stream with a specific file name suffix+rank
382    * to write the error log.
383    * \param fileName [in] protype file name
384    */
385    void CClient::openErrorStream(const StdString& fileName)
386    {
387      std::filebuf* fb = m_errorStream.rdbuf();
388      openStream(fileName, ".err", fb);
389
390      error.write2File(fb);
391    }
392
393    //! Write the error log to standard error output
394    void CClient::openErrorStream()
395    {
396      error.write2StdErr();
397    }
398
399    //! Close the error log file if it opens
400    void CClient::closeErrorStream()
401    {
402      if (m_errorStream.is_open()) m_errorStream.close();
403    }
[300]404}
Note: See TracBrowser for help on using the repository browser.