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

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

minor modification for using intelmpi

  • 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.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    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
191        MPI_Intercomm_create(contextComm,0,CXios::globalComm,serverLeader,10+globalRank,&contextInterComm) ;
192        info(10)<<"Register new Context : "<<id<<endl ;
193                     
194
195        MPI_Comm inter ;
196        MPI_Intercomm_merge(contextInterComm,0,&inter) ;
197        MPI_Barrier(inter) ;
198       
199       
200        context->initClient(contextComm,contextInterComm) ;
201       
202
203        contextInterComms.push_back(contextInterComm);
204        MPI_Comm_free(&inter);
205      }
206      else
207      {
208        MPI_Comm contextInterComm ;
209        MPI_Comm_dup(contextComm,&contextInterComm) ;
210        CContext* contextServer = CContext::create(idServer);
211
212        // Firstly, initialize context on client side
213        context->initClient(contextComm,contextInterComm, contextServer);
214
215        // Secondly, initialize context on server side
216        contextServer->initServer(contextComm,contextInterComm, context);
217
218        // Finally, we should return current context to context client
219        CContext::setCurrent(id);
220
221        contextInterComms.push_back(contextInterComm);
222      }
223    }
224
225    void CClient::finalize(void)
226    {
227      int rank ;
228      int msg=0 ;
229
230      MPI_Comm_rank(intraComm,&rank) ;
231     
232      //printf("CClient::finalize called isServer = %d\n", CXios::isServer);
233 
234      if (!CXios::isServer)
235      {
236        MPI_Comm_rank(intraComm,&rank) ;
237        if (rank==0)
238        {
239          MPI_Send(&msg,1,MPI_INT,0,0,interComm) ;
240          //printf(" CClient : send finalize sign to server 0\n");
241        }
242      }
243
244      for (std::list<MPI_Comm>::iterator it = contextInterComms.begin(); it != contextInterComms.end(); ++it)
245        MPI_Comm_free(&(*it));
246     
247      MPI_Comm_free(&interComm);
248      MPI_Comm_free(&intraComm);
249
250      CTimer::get("XIOS finalize").suspend() ;
251      CTimer::get("XIOS").suspend() ;
252
253      if (!is_MPI_Initialized)
254      {
255        if (CXios::usingOasis) oasis_finalize();
256        else {MPI_Finalize() ; printf("CClient::finalize called MPI_finalize\n");}
257      }
258     
259      info(20) << "Client side context is finalized"<<endl ;
260      report(0) <<" Performance report : total time spent for XIOS : "<< CTimer::get("XIOS").getCumulatedTime()<<" s"<<endl ;
261      report(0)<< " Performance report : time spent for waiting free buffer : "<< CTimer::get("Blocking time").getCumulatedTime()<<" s"<<endl ;
262      report(0)<< " Performance report : Ratio : "<< CTimer::get("Blocking time").getCumulatedTime()/CTimer::get("XIOS").getCumulatedTime()*100.<<" %"<<endl ;
263      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 ;
264//      report(0)<< " Memory report : Current buffer_size : "<<CXios::bufferSize<<endl ;
265      report(0)<< " Memory report : Minimum buffer size required : " << CClientBuffer::maxRequestSize << " bytes" << endl ;
266      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 ;
267   }
268
269   int CClient::getRank()
270   {
271     return rank;
272   }
273
274    /*!
275    * Open a file specified by a suffix and an extension and use it for the given file buffer.
276    * The file name will be suffix+rank+extension.
277    *
278    * \param fileName[in] protype file name
279    * \param ext [in] extension of the file
280    * \param fb [in/out] the file buffer
281    */
282    void CClient::openStream(const StdString& fileName, const StdString& ext, std::filebuf* fb)
283    {
284      StdStringStream fileNameClient;
285      int numDigit = 0;
286      int size = 0;
287      MPI_Comm_size(CXios::globalComm, &size);
288      while (size)
289      {
290        size /= 10;
291        ++numDigit;
292      }
293
294      fileNameClient << fileName << "_" << std::setfill('0') << std::setw(numDigit) << getRank() << ext;
295      fb->open(fileNameClient.str().c_str(), std::ios::out);
296      if (!fb->is_open())
297        ERROR("void CClient::openStream(const StdString& fileName, const StdString& ext, std::filebuf* fb)",
298              << std::endl << "Can not open <" << fileNameClient << "> file to write the client log(s).");
299    }
300
301    /*!
302    * \brief Open a file stream to write the info logs
303    * Open a file stream with a specific file name suffix+rank
304    * to write the info logs.
305    * \param fileName [in] protype file name
306    */
307    void CClient::openInfoStream(const StdString& fileName)
308    {
309      std::filebuf* fb = m_infoStream.rdbuf();
310      openStream(fileName, ".out", fb);
311
312      info.write2File(fb);
313      report.write2File(fb);
314    }
315
316    //! Write the info logs to standard output
317    void CClient::openInfoStream()
318    {
319      info.write2StdOut();
320      report.write2StdOut();
321    }
322
323    //! Close the info logs file if it opens
324    void CClient::closeInfoStream()
325    {
326      if (m_infoStream.is_open()) m_infoStream.close();
327    }
328
329    /*!
330    * \brief Open a file stream to write the error log
331    * Open a file stream with a specific file name suffix+rank
332    * to write the error log.
333    * \param fileName [in] protype file name
334    */
335    void CClient::openErrorStream(const StdString& fileName)
336    {
337      std::filebuf* fb = m_errorStream.rdbuf();
338      openStream(fileName, ".err", fb);
339
340      error.write2File(fb);
341    }
342
343    //! Write the error log to standard error output
344    void CClient::openErrorStream()
345    {
346      error.write2StdErr();
347    }
348
349    //! Close the error log file if it opens
350    void CClient::closeErrorStream()
351    {
352      if (m_errorStream.is_open()) m_errorStream.close();
353    }
354}
Note: See TracBrowser for help on using the repository browser.