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

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

First working version with compression by secondary servers.

  • 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: 14.6 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    vector <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::xiosCodeIdPrm);
60          unsigned long* hashAll ;
61          int size ;
62          int myColor ;
63          int i,c ;
64          MPI_Comm newComm ;
65
66          MPI_Comm_size(CXios::globalComm,&size) ;
67          MPI_Comm_rank(CXios::globalComm,&rank);
68
69          hashAll=new unsigned long[size] ;
70
71          MPI_Allgather(&hashClient, 1, MPI_LONG, hashAll, 1, MPI_LONG, CXios::globalComm) ;
72
73          map<unsigned long, int> colors ;
74          map<unsigned long, int> leaders ;
75
76          for(i=0,c=0;i<size;i++)
77          {
78            if (colors.find(hashAll[i])==colors.end())
79            {
80              colors[hashAll[i]] =c ;
81              leaders[hashAll[i]]=i ;
82              c++ ;
83            }
84          }
85
86          // Verify whether we are on server mode or not
87          CXios::setNotUsingServer();
88          for (i=0; i < size; ++i)
89          {
90            if ((hashAll[i] == hashString(CXios::xiosCodeId))
91                || (hashAll[i] == hashString(CXios::xiosCodeIdPrm))
92                || (hashAll[i] == hashString(CXios::xiosCodeIdSnd)))
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            serverLeader.push_back(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.back()<<endl ;
112             MPI_Intercomm_create(intraComm, 0, CXios::globalComm, serverLeader.back(), 0, &interComm) ;
113//             info(50)<<"intercommCreate::client "<<rank<<" intraCommSize : "<<intraCommSize
114//                    <<" intraCommRank :"<<intraCommRank<<"  clientLeader "<< serverLeader<<endl ;
115//              MPI_Intercomm_create(intraComm,0,CXios::globalComm,serverLeader,0,&interComm) ;
116          }
117          else
118          {
119            MPI_Comm_dup(intraComm,&interComm) ;
120          }
121          delete [] hashAll ;
122        }
123        // localComm argument is given
124        else
125        {
126          if (CXios::usingServer)
127          {
128            //ERROR("void CClient::initialize(const string& codeId,MPI_Comm& localComm,MPI_Comm& returnComm)", << " giving a local communictor is not compatible with using server mode") ;
129          }
130          else
131          {
132            MPI_Comm_dup(localComm,&intraComm) ;
133            MPI_Comm_dup(intraComm,&interComm) ;
134          }
135        }
136      }
137      // using OASIS
138      else
139      {
140        // localComm isn't given
141        if (localComm == MPI_COMM_NULL)
142        {
143          if (!is_MPI_Initialized) oasis_init(codeId) ;
144          oasis_get_localcomm(localComm) ;
145        }
146        MPI_Comm_dup(localComm,&intraComm) ;
147
148        CTimer::get("XIOS").resume() ;
149        CTimer::get("XIOS init").resume() ;
150
151        if (CXios::usingServer)
152        {
153          MPI_Status status ;
154          MPI_Comm_rank(intraComm,&rank) ;
155
156          oasis_get_intercomm(interComm,CXios::xiosCodeId) ;
157          if (rank==0) MPI_Recv(&serverLeader,1, MPI_INT, 0, 0, interComm, &status) ;
158          MPI_Bcast(&serverLeader,1,MPI_INT,0,intraComm) ;
159
160        }
161        else MPI_Comm_dup(intraComm,&interComm) ;
162      }
163
164      MPI_Comm_dup(intraComm,&returnComm) ;
165    }
166
167    void CClient::initializeClientOnServer(const int rank, const MPI_Comm& intraCommPrmSrv, const int srvSndLeader)
168    {
169      MPI_Comm_dup(intraCommPrmSrv, &intraComm) ;
170      serverLeader.push_back(srvSndLeader);
171      int intraCommSize, intraCommRank ;
172      MPI_Comm_size(intraComm,&intraCommSize) ;
173      MPI_Comm_rank(intraComm,&intraCommRank) ;
174      info(50)<<"intercommCreate::client "<<rank<<" intraCommSize : "<<intraCommSize
175          <<" intraCommRank :"<<intraCommRank<<"  clientLeader "<< srvSndLeader<<endl ;
176      MPI_Intercomm_create(intraComm, 0, CXios::globalComm, srvSndLeader, 0, &interComm) ;
177    }
178
179
180///---------------------------------------------------------------
181/*!
182 * \fn void CClient::registerContext(const string& id, MPI_Comm contextComm)
183 * Function creates intraComm (CClient::intraComm) for client group with id=codeId and interComm (CClient::interComm) between client and server groups.
184 * \param [in] id id of context.
185 * \param [in] contextComm.
186 */
187    void CClient::registerContext(const string& id, MPI_Comm contextComm)
188    {
189      CContext::setCurrent(id) ;
190      CContext* context=CContext::create(id);
191      StdString idServer(id);
192      idServer += "_server";
193
194      if (CXios::isServer && !context->hasServer)
195      // Attached mode
196      {
197        MPI_Comm contextInterComm ;
198        MPI_Comm_dup(contextComm,&contextInterComm) ;
199        CContext* contextServer = CContext::create(idServer);
200
201        // Firstly, initialize context on client side
202        context->initClient(contextComm,contextInterComm, contextServer);
203
204        // Secondly, initialize context on server side
205        contextServer->initServer(contextComm,contextInterComm, context);
206
207        // Finally, we should return current context to context client
208        CContext::setCurrent(id);
209
210        contextInterComms.push_back(contextInterComm);
211      }
212      else
213      {
214        int size,rank,globalRank ;
215        size_t message_size ;
216        int leaderRank ;
217        MPI_Comm contextInterComm ;
218
219        MPI_Comm_size(contextComm,&size) ;
220        MPI_Comm_rank(contextComm,&rank) ;
221        MPI_Comm_rank(CXios::globalComm,&globalRank) ;
222        if (rank!=0) globalRank=0 ;
223
224        CMessage msg ;
225        msg<<idServer<<size<<globalRank ;
226//        msg<<id<<size<<globalRank ;
227
228        int messageSize=msg.size() ;
229        void * buff = new char[messageSize] ;
230        CBufferOut buffer(buff,messageSize) ;
231        buffer<<msg ;
232
233        for (int i = 0; i < serverLeader.size(); ++i)
234        {
235          MPI_Send(buff, buffer.count(), MPI_CHAR, serverLeader[i], 1, CXios::globalComm) ;
236          MPI_Intercomm_create(contextComm, 0, CXios::globalComm, serverLeader[i], 10+globalRank, &contextInterComm) ;
237          info(10)<<"Register new Context : "<<id<<endl ;
238          MPI_Comm inter ;
239          MPI_Intercomm_merge(contextInterComm,0,&inter) ;
240          MPI_Barrier(inter) ;
241
242          context->initClient(contextComm,contextInterComm) ;
243
244          contextInterComms.push_back(contextInterComm);
245          MPI_Comm_free(&inter);
246        }
247        delete [] buff ;
248
249      }
250    }
251
252    ///---------------------------------------------------------------
253    /*!
254     * \fn void CClient::registerContext(const string& id, const int poolNb, MPI_Comm contextComm)
255     * Function creates intraComm (CClient::intraComm) for client group with id=codeId and interComm (CClient::interComm) between client and server groups.
256     * \param [in] id id of context.
257     * \param [in] contextComm.
258     */
259        void CClient::registerContextOnSrvPools(const string& id, MPI_Comm contextComm)
260        {
261          CContext::setCurrent(id) ;
262          CContext* context=CContext::create(id);
263          StdString idServer(id);
264          idServer += "_server_";
265
266          int size,rank,globalRank ;
267          size_t message_size ;
268          int leaderRank ;
269          MPI_Comm contextInterComm ;
270
271          MPI_Comm_size(contextComm,&size) ;
272          MPI_Comm_rank(contextComm,&rank) ;
273          MPI_Comm_rank(CXios::globalComm,&globalRank) ;
274          if (rank!=0) globalRank=0 ;
275
276          CMessage msg ;
277
278          int messageSize ;
279          void * buff ;
280
281          for (int i = 0; i < serverLeader.size(); ++i)
282          {
283            StdString str = idServer + boost::lexical_cast<string>(i);
284            msg<<str<<size<<globalRank ;
285            messageSize = msg.size() ;
286            buff = new char[messageSize] ;
287            CBufferOut buffer(buff,messageSize) ;
288            buffer<<msg ;
289
290            MPI_Send(buff, buffer.count(), MPI_CHAR, serverLeader[i], 1, CXios::globalComm) ;
291            MPI_Intercomm_create(contextComm, 0, CXios::globalComm, serverLeader[i], 10+globalRank, &contextInterComm) ;
292            info(10)<<"Register new Context : "<<id<<endl ;
293            MPI_Comm inter ;
294            MPI_Intercomm_merge(contextInterComm,0,&inter) ;
295            MPI_Barrier(inter) ;
296
297            context->initClient(contextComm,contextInterComm) ;
298
299            contextInterComms.push_back(contextInterComm);
300            MPI_Comm_free(&inter);
301            delete [] buff ;
302          }
303        }
304
305    void CClient::finalize(void)
306    {
307      int rank ;
308      int msg=0 ;
309
310      MPI_Comm_rank(intraComm,&rank) ;
311 
312      if (!CXios::isServer)
313      {
314        MPI_Comm_rank(intraComm,&rank) ;
315        if (rank==0)
316        {
317          MPI_Send(&msg,1,MPI_INT,0,0,interComm) ;
318        }
319      }
320
321      for (std::list<MPI_Comm>::iterator it = contextInterComms.begin(); it != contextInterComms.end(); it++)
322        MPI_Comm_free(&(*it));
323      MPI_Comm_free(&interComm);
324      MPI_Comm_free(&intraComm);
325
326      CTimer::get("XIOS finalize").suspend() ;
327      CTimer::get("XIOS").suspend() ;
328
329      if (!is_MPI_Initialized)
330      {
331        if (CXios::usingOasis) oasis_finalize();
332        else MPI_Finalize() ;
333      }
334     
335      info(20) << "Client side context is finalized"<<endl ;
336      report(0) <<" Performance report : total time spent for XIOS : "<< CTimer::get("XIOS").getCumulatedTime()<<" s"<<endl ;
337      report(0)<< " Performance report : time spent for waiting free buffer : "<< CTimer::get("Blocking time").getCumulatedTime()<<" s"<<endl ;
338      report(0)<< " Performance report : Ratio : "<< CTimer::get("Blocking time").getCumulatedTime()/CTimer::get("XIOS").getCumulatedTime()*100.<<" %"<<endl ;
339      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 ;
340//      report(0)<< " Memory report : Current buffer_size : "<<CXios::bufferSize<<endl ;
341      report(0)<< " Memory report : Minimum buffer size required : " << CClientBuffer::maxRequestSize << " bytes" << endl ;
342      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 ;
343
344   }
345
346
347   int CClient::getRank()
348   {
349     return rank;
350   }
351
352    /*!
353    * Open a file specified by a suffix and an extension and use it for the given file buffer.
354    * The file name will be suffix+rank+extension.
355    *
356    * \param fileName[in] protype file name
357    * \param ext [in] extension of the file
358    * \param fb [in/out] the file buffer
359    */
360    void CClient::openStream(const StdString& fileName, const StdString& ext, std::filebuf* fb)
361    {
362      StdStringStream fileNameClient;
363      int numDigit = 0;
364      int size = 0;
365      MPI_Comm_size(CXios::globalComm, &size);
366      while (size)
367      {
368        size /= 10;
369        ++numDigit;
370      }
371
372      fileNameClient << fileName << "_" << std::setfill('0') << std::setw(numDigit) << getRank() << ext;
373      fb->open(fileNameClient.str().c_str(), std::ios::out);
374      if (!fb->is_open())
375        ERROR("void CClient::openStream(const StdString& fileName, const StdString& ext, std::filebuf* fb)",
376              << std::endl << "Can not open <" << fileNameClient << "> file to write the client log(s).");
377    }
378
379    /*!
380    * \brief Open a file stream to write the info logs
381    * Open a file stream with a specific file name suffix+rank
382    * to write the info logs.
383    * \param fileName [in] protype file name
384    */
385    void CClient::openInfoStream(const StdString& fileName)
386    {
387      std::filebuf* fb = m_infoStream.rdbuf();
388      openStream(fileName, ".out", fb);
389
390      info.write2File(fb);
391      report.write2File(fb);
392    }
393
394    //! Write the info logs to standard output
395    void CClient::openInfoStream()
396    {
397      info.write2StdOut();
398      report.write2StdOut();
399    }
400
401    //! Close the info logs file if it opens
402    void CClient::closeInfoStream()
403    {
404      if (m_infoStream.is_open()) m_infoStream.close();
405    }
406
407    /*!
408    * \brief Open a file stream to write the error log
409    * Open a file stream with a specific file name suffix+rank
410    * to write the error log.
411    * \param fileName [in] protype file name
412    */
413    void CClient::openErrorStream(const StdString& fileName)
414    {
415      std::filebuf* fb = m_errorStream.rdbuf();
416      openStream(fileName, ".err", fb);
417
418      error.write2File(fb);
419    }
420
421    //! Write the error log to standard error output
422    void CClient::openErrorStream()
423    {
424      error.write2StdErr();
425    }
426
427    //! Close the error log file if it opens
428    void CClient::closeErrorStream()
429    {
430      if (m_errorStream.is_open()) m_errorStream.close();
431    }
432}
Note: See TracBrowser for help on using the repository browser.