source: XIOS/dev/dev_olga/src/client.cpp @ 1165

Last change on this file since 1165 was 1158, checked in by oabramkina, 7 years ago

Two server levels: merging with trunk r1137.
There are bugs.

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