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

Last change on this file since 1482 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
Line 
1#include "globalScopeData.hpp"
2#include "xios_spl.hpp"
3#include "cxios.hpp"
4#include "client.hpp"
5#include <boost/functional/hash.hpp>
6#include "type.hpp"
7#include "context.hpp"
8#include "context_client.hpp"
9#include "oasis_cinterface.hpp"
10#include "mpi.hpp"
11#include "timer.hpp"
12#include "buffer_client.hpp"
13using namespace ep_lib;
14
15namespace xios
16{
17
18    MPI_Comm CClient::intraComm ;
19    MPI_Comm CClient::interComm ;
20    //std::list<MPI_Comm> CClient::contextInterComms;
21    std::list<MPI_Comm> *CClient::contextInterComms_ptr = 0;
22    int CClient::serverLeader ;
23    bool CClient::is_MPI_Initialized ;
24    int CClient::rank_ = INVALID_RANK;
25    StdOFStream CClient::m_infoStream;
26    StdOFStream CClient::m_errorStream;
27
28    StdOFStream CClient::array_infoStream[16];
29
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)
42    {
43      int initialized ;
44      MPI_Initialized(&initialized) ;
45      if (initialized) is_MPI_Initialized=true ;
46      else is_MPI_Initialized=false ;
47      int rank ;
48
49// don't use OASIS
50      if (!CXios::usingOasis)
51      {
52// localComm isn't given
53        if (localComm == MPI_COMM_NULL)
54        {
55          if (!is_MPI_Initialized)
56          {
57            MPI_Init(NULL, NULL);
58          }
59          CTimer::get("XIOS").resume() ;
60          CTimer::get("XIOS init/finalize").resume() ;
61          boost::hash<string> hashString ;
62
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 ;
69          MPI_Comm newComm ;
70
71          MPI_Comm_size(CXios::globalComm,&size) ;
72          MPI_Comm_rank(CXios::globalComm,&rank_);
73
74          hashAll=new unsigned long[size] ;
75
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 ;
80
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          }
90
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
102          myColor=colors[hashClient];
103          MPI_Comm_split(CXios::globalComm,myColor,rank_,&intraComm) ;
104
105          if (CXios::usingServer)
106          {
107            int clientLeader=leaders[hashClient] ;
108            serverLeader=leaders[hashServer] ;
109            int intraCommSize, intraCommRank ;
110            MPI_Comm_size(intraComm,&intraCommSize) ;
111            MPI_Comm_rank(intraComm,&intraCommRank) ;
112            #pragma omp critical (_output)
113            {
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;
119          }
120          else
121          {
122            MPI_Comm_dup(intraComm,&interComm) ;
123          }
124          delete [] hashAll ;
125        }
126        // localComm argument is given
127        else
128        {
129          if (CXios::usingServer)
130          {
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
141      else
142      {
143        // localComm isn't given
144        if (localComm == MPI_COMM_NULL)
145        {
146          if (!is_MPI_Initialized) oasis_init(codeId) ;
147          oasis_get_localcomm(localComm) ;
148        }
149        MPI_Comm_dup(localComm,&intraComm) ;
150
151        CTimer::get("XIOS").resume() ;
152        CTimer::get("XIOS init/finalize").resume() ;
153
154        if (CXios::usingServer)
155        {
156          MPI_Status status ;
157          MPI_Comm_rank(intraComm,&rank_) ;
158
159          oasis_get_intercomm(interComm,CXios::xiosCodeId) ;
160          if (rank_==0) MPI_Recv(&serverLeader,1, MPI_INT, 0, 0, interComm, &status) ;
161          MPI_Bcast(&serverLeader,1,MPI_INT,0,intraComm) ;
162        }
163        else MPI_Comm_dup(intraComm,&interComm) ;
164      }
165
166      MPI_Comm_dup(intraComm,&returnComm) ;
167    }
168
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)
178    {
179      CContext::setCurrent(id) ;
180      CContext* context=CContext::create(id);
181      StdString idServer(id);
182      idServer += "_server";
183
184      if (CXios::isServer && !context->hasServer)
185      // Attached mode
186      {
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      {
206        int size,rank,globalRank ;
207        size_t message_size ;
208        int leaderRank ;
209        MPI_Comm contextInterComm ;
210
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 ;
215
216        CMessage msg ;
217        msg<<idServer<<size<<globalRank ;
218//        msg<<id<<size<<globalRank ;
219
220        int messageSize=msg.size() ;
221        char * buff = new char[messageSize] ;
222        CBufferOut buffer((void*)buff,messageSize) ;
223        buffer<<msg ;
224
225        MPI_Send((void*)buff,buffer.count(),MPI_CHAR,serverLeader,1,CXios::globalComm) ;
226
227        MPI_Intercomm_create(contextComm,0,CXios::globalComm,serverLeader,10+globalRank,&contextInterComm) ;
228        #pragma omp critical (_output)
229        info(10)<<"Register new Context : "<<id<<endl ;
230        MPI_Comm inter ;
231        MPI_Intercomm_merge(contextInterComm,0,&inter) ;
232        MPI_Barrier(inter) ;
233
234        context->initClient(contextComm,contextInterComm) ;
235
236        //contextInterComms.push_back(contextInterComm);
237        if(contextInterComms_ptr == NULL) contextInterComms_ptr = new std::list<MPI_Comm>;
238        contextInterComms_ptr->push_back(contextInterComm);
239
240        MPI_Comm_free(&inter);
241        delete [] buff ;
242
243      }
244    }
245
246    void CClient::finalize(void)
247    {
248      int rank ;
249      int msg=0 ;
250
251      MPI_Comm_rank(intraComm,&rank) ;
252 
253      if (!CXios::isServer)
254      {
255        MPI_Comm_rank(intraComm,&rank) ;
256        if (rank==0)
257        {
258          MPI_Send(&msg,1,MPI_INT,0,0,interComm) ;
259        }
260      }
261
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++)
264        MPI_Comm_free(&(*it));
265      MPI_Comm_free(&interComm);
266      MPI_Comm_free(&intraComm);
267
268      CTimer::get("XIOS init/finalize").suspend() ;
269      CTimer::get("XIOS").suspend() ;
270
271      if (!is_MPI_Initialized)
272      {
273        //if (CXios::usingOasis) oasis_finalize();
274        //else
275        MPI_Finalize() ;
276      }
277      #pragma omp critical (_output)
278      info(20) << "Client side context is finalized"<<endl ;
279
280      #pragma omp critical (_output)
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 ;
287//      report(0)<< " Memory report : Current buffer_size : "<<CXios::bufferSize<<endl ;
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 ;
291      }
292   }
293
294    /*!
295    * Return global rank without oasis and current rank in model intraComm in case of oasis
296    */
297   int CClient::getRank()
298   {
299     return rank_;
300   }
301
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;
315      int rank;
316      MPI_Comm_size(CXios::globalComm, &size);
317      while (size)
318      {
319        size /= 10;
320        ++numDigit;
321      }
322
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
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)",
335              << std::endl << "Can not open <" << fileNameClient << "> file to write the client log(s).");
336    }
337
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    {
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()]);
349
350      info.write2File(info_FB[omp_get_thread_num()]);
351      report.write2File(info_FB[omp_get_thread_num()]);
352    }
353
354    //! Write the info logs to standard output
355    void CClient::openInfoStream()
356    {
357      info.write2StdOut();
358      report.write2StdOut();
359    }
360
361    //! Close the info logs file if it opens
362    void CClient::closeInfoStream()
363    {
364      if (m_infoStream.is_open()) m_infoStream.close();
365    }
366
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    }
392}
Note: See TracBrowser for help on using the repository browser.