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

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

Two server levels: merging with trunk r1137.
There are bugs.

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