source: XIOS/dev/branch_openmp/src/client.cpp @ 1545

Last change on this file since 1545 was 1545, checked in by yushan, 6 years ago

branch_openmp merged with trunk r1544

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