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

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

log OK with threads

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