source: XIOS2/trunk/src/client.cpp @ 2428

Last change on this file since 2428 was 2428, checked in by jderouillat, 17 months ago

Backport the XIOS3 system to log the memory consumption (commit ID [2418-2420,2425-2426])

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