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

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

Two servel levels: correcting initialization in case of non-contiguous server ranks.
Tests on Curie: test_complete.

  • 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      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").resume() ;
57          boost::hash<string> hashString ;
58
59          unsigned long hashClient = hashString(codeId) ;
60          unsigned long hashServer = hashString(CXios::xiosCodeId);
61//          unsigned long hashServer = hashString(CXios::xiosCodeIdPrm);
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 (hashAll[i] == hashString(CXios::xiosCodeId))
93            {
94              CXios::setUsingServer();
95              break;
96            }
97          }
98
99//          myColor=colors[hashClient];
100          myColor=leaders[hashClient] ;
101          MPI_Comm_split(CXios::globalComm,myColor,rank_,&intraComm) ;
102
103          if (CXios::usingServer2)
104            MPI_Allgather(&hashClient, 1, MPI_LONG, hashAll, 1, MPI_LONG, CXios::globalComm) ;
105
106          if (CXios::usingServer)
107          {
108            int clientLeader=leaders[hashClient] ;
109            serverLeader=leaders[hashServer] ;
110            int intraCommSize, intraCommRank ;
111            MPI_Comm_size(intraComm,&intraCommSize) ;
112            MPI_Comm_rank(intraComm,&intraCommRank) ;
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             //rank_ = intraCommRank;
117          }
118          else
119          {
120            MPI_Comm_dup(intraComm,&interComm) ;
121          }
122          delete [] hashAll ;
123        }
124        // localComm argument is given
125        else
126        {
127          if (CXios::usingServer)
128          {
129            //ERROR("void CClient::initialize(const string& codeId,MPI_Comm& localComm,MPI_Comm& returnComm)", << " giving a local communictor is not compatible with using server mode") ;
130          }
131          else
132          {
133            MPI_Comm_dup(localComm,&intraComm) ;
134            MPI_Comm_dup(intraComm,&interComm) ;
135          }
136        }
137      }
138      // using OASIS
139      else
140      {
141        // localComm isn't given
142        if (localComm == MPI_COMM_NULL)
143        {
144          if (!is_MPI_Initialized) oasis_init(codeId) ;
145          oasis_get_localcomm(localComm) ;
146        }
147        MPI_Comm_dup(localComm,&intraComm) ;
148
149        CTimer::get("XIOS").resume() ;
150        CTimer::get("XIOS init").resume() ;
151
152        if (CXios::usingServer)
153        {
154          MPI_Status status ;
155          MPI_Comm_rank(intraComm,&rank) ;
156
157          oasis_get_intercomm(interComm,CXios::xiosCodeId) ;
158          if (rank==0) MPI_Recv(&serverLeader,1, MPI_INT, 0, 0, interComm, &status) ;
159          MPI_Bcast(&serverLeader,1,MPI_INT,0,intraComm) ;
160
161        }
162        else MPI_Comm_dup(intraComm,&interComm) ;
163      }
164
165      MPI_Comm_dup(intraComm,&returnComm) ;
166    }
167
168///---------------------------------------------------------------
169/*!
170 * \fn void CClient::registerContext(const string& id, MPI_Comm contextComm)
171 * \brief Sends a request to create a context to server. Creates client/server contexts.
172 * \param [in] id id of context.
173 * \param [in] contextComm.
174 * Function is only called by client.
175 */
176    void CClient::registerContext(const string& id, MPI_Comm contextComm)
177    {
178      CContext::setCurrent(id) ;
179      CContext* context=CContext::create(id);
180      StdString idServer(id);
181      idServer += "_server";
182
183      if (CXios::isServer && !context->hasServer)
184      // Attached mode
185      {
186        MPI_Comm contextInterComm ;
187        MPI_Comm_dup(contextComm,&contextInterComm) ;
188        CContext* contextServer = CContext::create(idServer);
189
190        // Firstly, initialize context on client side
191        context->initClient(contextComm,contextInterComm, contextServer);
192
193        // Secondly, initialize context on server side
194        contextServer->initServer(contextComm,contextInterComm, context);
195
196        // Finally, we should return current context to context client
197        CContext::setCurrent(id);
198
199        contextInterComms.push_back(contextInterComm);
200      }
201      else
202      {
203        int size,rank,globalRank ;
204        size_t message_size ;
205        int leaderRank ;
206        MPI_Comm contextInterComm ;
207
208        MPI_Comm_size(contextComm,&size) ;
209        MPI_Comm_rank(contextComm,&rank) ;
210        MPI_Comm_rank(CXios::globalComm,&globalRank) ;
211        if (rank!=0) globalRank=0 ;
212
213        CMessage msg ;
214        msg<<idServer<<size<<globalRank ;
215//        msg<<id<<size<<globalRank ;
216
217        int messageSize=msg.size() ;
218        void * buff = new char[messageSize] ;
219        CBufferOut buffer(buff,messageSize) ;
220        buffer<<msg ;
221
222        MPI_Send(buff, buffer.count(), MPI_CHAR, serverLeader, 1, CXios::globalComm) ;
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 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 : total time spent for XIOS : "<< CTimer::get("XIOS").getCumulatedTime()<<" s"<<endl ;
270      report(0)<< " Performance report : time spent for waiting free buffer : "<< CTimer::get("Blocking time").getCumulatedTime()<<" s"<<endl ;
271      report(0)<< " Performance report : Ratio : "<< CTimer::get("Blocking time").getCumulatedTime()/CTimer::get("XIOS").getCumulatedTime()*100.<<" %"<<endl ;
272      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 ;
273//      report(0)<< " Memory report : Current buffer_size : "<<CXios::bufferSize<<endl ;
274      report(0)<< " Memory report : Minimum buffer size required : " << CClientBuffer::maxRequestSize << " bytes" << endl ;
275      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 ;
276
277   }
278
279    /*!
280    * Return rank in model intraComm
281    */
282   int CClient::getRank()
283   {
284     return rank_;
285   }
286
287    /*!
288    * Open a file specified by a suffix and an extension and use it for the given file buffer.
289    * The file name will be suffix+rank+extension.
290    *
291    * \param fileName[in] protype file name
292    * \param ext [in] extension of the file
293    * \param fb [in/out] the file buffer
294    */
295    void CClient::openStream(const StdString& fileName, const StdString& ext, std::filebuf* fb)
296    {
297      StdStringStream fileNameClient;
298      int numDigit = 0;
299      int size = 0;
300      MPI_Comm_size(CXios::globalComm, &size);
301      while (size)
302      {
303        size /= 10;
304        ++numDigit;
305      }
306
307      fileNameClient << fileName << "_" << std::setfill('0') << std::setw(numDigit) << getRank() << ext;
308      fb->open(fileNameClient.str().c_str(), std::ios::out);
309      if (!fb->is_open())
310        ERROR("void CClient::openStream(const StdString& fileName, const StdString& ext, std::filebuf* fb)",
311              << std::endl << "Can not open <" << fileNameClient << "> file to write the client log(s).");
312    }
313
314    /*!
315    * \brief Open a file stream to write the info logs
316    * Open a file stream with a specific file name suffix+rank
317    * to write the info logs.
318    * \param fileName [in] protype file name
319    */
320    void CClient::openInfoStream(const StdString& fileName)
321    {
322      std::filebuf* fb = m_infoStream.rdbuf();
323      openStream(fileName, ".out", fb);
324
325      info.write2File(fb);
326      report.write2File(fb);
327    }
328
329    //! Write the info logs to standard output
330    void CClient::openInfoStream()
331    {
332      info.write2StdOut();
333      report.write2StdOut();
334    }
335
336    //! Close the info logs file if it opens
337    void CClient::closeInfoStream()
338    {
339      if (m_infoStream.is_open()) m_infoStream.close();
340    }
341
342    /*!
343    * \brief Open a file stream to write the error log
344    * Open a file stream with a specific file name suffix+rank
345    * to write the error log.
346    * \param fileName [in] protype file name
347    */
348    void CClient::openErrorStream(const StdString& fileName)
349    {
350      std::filebuf* fb = m_errorStream.rdbuf();
351      openStream(fileName, ".err", fb);
352
353      error.write2File(fb);
354    }
355
356    //! Write the error log to standard error output
357    void CClient::openErrorStream()
358    {
359      error.write2StdErr();
360    }
361
362    //! Close the error log file if it opens
363    void CClient::closeErrorStream()
364    {
365      if (m_errorStream.is_open()) m_errorStream.close();
366    }
367}
Note: See TracBrowser for help on using the repository browser.