source: XIOS/dev/branch_openmp/src/client.cpp @ 1552

Last change on this file since 1552 was 1545, checked in by yushan, 6 years ago

branch_openmp merged with trunk r1544

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