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

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

branch_openmp merged with XIOS_DEV_CMIP6@1459

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