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

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

Fixing a bug in context initialization. Now register context is scheduled by event scheduler.
Tests on Curie: test_complete and test_xios2_cmip6.

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