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

Last change on this file since 511 was 511, checked in by mhnguyen, 9 years ago

Seperating database of context on "client" side and "server" side

+) Add one more context in contex client in case of attached mode
+) Do some minor changements to make sure everything fine in case of attached mode
+) Replace buffer group with the new options

Test
+) On Curie
+) Connection mode: Attached and seperated
+) File mode: one and multiple
+) All tests passed

  • 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: 8.8 KB
Line 
1#include "globalScopeData.hpp"
2#include "xmlioserver_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    int CClient::serverLeader ;
20    bool CClient::is_MPI_Initialized ;
21    int CClient::rank = INVALID_RANK;
22    StdOFStream CClient::m_infoStream;
23
24    void CClient::initialize(const string& codeId,MPI_Comm& localComm,MPI_Comm& returnComm)
25    {
26      int initialized ;
27      MPI_Initialized(&initialized) ;
28      if (initialized) is_MPI_Initialized=true ;
29      else is_MPI_Initialized=false ;
30
31// don't use OASIS
32      if (!CXios::usingOasis)
33      {
34// localComm doesn't given
35        if (localComm == MPI_COMM_NULL)
36        {
37          if (!is_MPI_Initialized)
38          {
39            int argc=0;
40            char** argv=NULL;
41            MPI_Init(&argc,&argv) ;
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          if (CXios::usingServer)
91          {
92            int clientLeader=leaders[hashClient] ;
93            serverLeader=leaders[hashServer] ;
94
95            int intraCommSize, intraCommRank ;
96            MPI_Comm_size(intraComm,&intraCommSize) ;
97            MPI_Comm_rank(intraComm,&intraCommRank) ;
98            info(50)<<"intercommCreate::client "<<rank<<" intraCommSize : "<<intraCommSize
99                 <<" intraCommRank :"<<intraCommRank<<"  clientLeader "<< serverLeader<<endl ;
100            MPI_Intercomm_create(intraComm,0,CXios::globalComm,serverLeader,0,&interComm) ;
101          }
102          else
103          {
104            MPI_Comm_dup(intraComm,&interComm) ;
105          }
106          delete [] hashAll ;
107        }
108        // localComm argument is given
109        else
110        {
111          if (CXios::usingServer)
112          {
113            //ERROR("void CClient::initialize(const string& codeId,MPI_Comm& localComm,MPI_Comm& returnComm)", << " giving a local communictor is not compatible with using server mode") ;
114          }
115          else
116          {
117            MPI_Comm_dup(localComm,&intraComm) ;
118            MPI_Comm_dup(intraComm,&interComm) ;
119          }
120        }
121      }
122      // using OASIS
123      else
124      {
125        // localComm doesn't given
126        if (localComm == MPI_COMM_NULL)
127        {
128          if (!is_MPI_Initialized) oasis_init(codeId) ;
129          oasis_get_localcomm(intraComm) ;
130        }
131        else MPI_Comm_dup(localComm,&intraComm) ;
132        CTimer::get("XIOS").resume() ;
133        CTimer::get("XIOS init").resume() ;
134
135        if (CXios::usingServer)
136        {
137          MPI_Status status ;
138          MPI_Comm_rank(intraComm,&rank) ;
139
140          oasis_get_intercomm(interComm,CXios::xiosCodeId) ;
141          if (rank==0) MPI_Recv(&serverLeader,1, MPI_INT, 0, 0, interComm, &status) ;
142          MPI_Bcast(&serverLeader,1,MPI_INT,0,intraComm) ;
143
144        }
145        else MPI_Comm_dup(intraComm,&interComm) ;
146      }
147
148      MPI_Comm_dup(intraComm,&returnComm) ;
149    }
150
151
152    void CClient::registerContext(const string& id,MPI_Comm contextComm)
153    {
154      CContext::setCurrent(id) ;
155      CContext* context=CContext::create(id);
156      StdString idServer(id);
157      idServer += "_server";
158
159      if (!CXios::isServer)
160      {
161        int size,rank,globalRank ;
162        size_t message_size ;
163        int leaderRank ;
164        MPI_Comm contextInterComm ;
165
166        MPI_Comm_size(contextComm,&size) ;
167        MPI_Comm_rank(contextComm,&rank) ;
168        MPI_Comm_rank(CXios::globalComm,&globalRank) ;
169        if (rank!=0) globalRank=0 ;
170
171
172        CMessage msg ;
173        msg<<idServer<<size<<globalRank ;
174//        msg<<id<<size<<globalRank ;
175
176        int messageSize=msg.size() ;
177        void * buff = new char[messageSize] ;
178        CBufferOut buffer(buff,messageSize) ;
179        buffer<<msg ;
180
181        MPI_Send(buff,buffer.count(),MPI_CHAR,serverLeader,1,CXios::globalComm) ;
182        delete [] buff ;
183
184        MPI_Intercomm_create(contextComm,0,CXios::globalComm,serverLeader,10+globalRank,&contextInterComm) ;
185        info(10)<<"Register new Context : "<<id<<endl ;
186
187        MPI_Comm inter ;
188        MPI_Intercomm_merge(contextInterComm,0,&inter) ;
189        MPI_Barrier(inter) ;
190
191        context->initClient(contextComm,contextInterComm) ;
192      }
193      else
194      {
195        MPI_Comm contextInterComm ;
196        MPI_Comm_dup(contextComm,&contextInterComm) ;
197        CContext* contextServer = CContext::create(idServer);
198
199        // Firstly, initialize context on client side
200        context->initClient(contextComm,contextInterComm, contextServer);
201
202
203        // Secondly, initialize context on server side
204        contextServer->initServer(contextComm,contextInterComm);
205
206        // Finally, we should return current context to context client
207        CContext::setCurrent(id);
208//        context->initServer(contextComm,contextInterComm) ;
209      }
210    }
211
212    void CClient::finalize(void)
213    {
214      int rank ;
215      int msg=0 ;
216      if (!CXios::isServer)
217      {
218        MPI_Comm_rank(intraComm,&rank) ;
219        if (rank==0)
220        {
221          MPI_Send(&msg,1,MPI_INT,0,0,interComm) ;
222        }
223      }
224
225     CTimer::get("XIOS finalize").suspend() ;
226     CTimer::get("XIOS").suspend() ;
227
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 ;
234      report(0) <<" Performance report : total time spent for XIOS : "<< CTimer::get("XIOS").getCumulatedTime()<<" s"<<endl ;
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 ;
238//      report(0)<< " Memory report : Current buffer_size : "<<CXios::bufferSize<<endl ;
239      report(0)<< " Memory report : Minimum buffer size required : "<<maxRequestSize*2<< " bytes" << endl ;
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   }
242
243   int CClient::getRank()
244   {
245     return rank;
246   }
247
248     /*!
249      * \brief Open file stream to write in
250      *   Opening a file stream with a specific file name suffix-client+rank
251      * \param [in] protype file name
252     */
253     void CClient::openInfoStream(const StdString& fileName)
254     {
255       std::filebuf* fb = m_infoStream.rdbuf();
256       StdStringStream fileNameClient;
257       int numDigit = 0;
258       int size = 0;
259       MPI_Comm_size(CXios::globalComm, &size);
260       while (size)
261       {
262         size /= 10;
263         ++numDigit;
264       }
265
266       fileNameClient << fileName << "_" << std::setfill('0') << std::setw(numDigit) << getRank() << ".out";
267       fb->open(fileNameClient.str().c_str(), std::ios::out);
268       if (!fb->is_open())
269       ERROR("void CClient::openInfoStream(const StdString& fileName)",
270            <<endl<< "Can not open <"<<fileNameClient<<"> file to write" );
271
272       info.write2File(fb);
273       report.write2File(fb);
274     }
275
276     //! Write out to standard output
277     void CClient::openInfoStream()
278     {
279       info.write2StdOut();
280       report.write2StdOut();
281     }
282
283     //! Close file if it opens
284     void CClient::closeInfoStream()
285     {
286       if (m_infoStream.is_open()) m_infoStream.close();
287     }
288
289
290}
Note: See TracBrowser for help on using the repository browser.