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

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

log files

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