source: XIOS/trunk/src/client.cpp @ 630

Last change on this file since 630 was 597, checked in by rlacroix, 9 years ago

Add basic infrastructure for servers to clients communication.

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