source: XIOS/dev/branch_yushan/src/client.cpp @ 1070

Last change on this file since 1070 was 1070, checked in by yushan, 7 years ago

Preperation for merge from trunk

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