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

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

server mode OK for both multiple and one file mode. Tested with test_client

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