source: XIOS/dev/branch_yushan/src/client.cpp @ 1103

Last change on this file since 1103 was 1102, checked in by yushan, 7 years ago

clean up

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