source: XIOS/branchs/xios-1.0/src/client.cpp @ 507

Last change on this file since 507 was 507, checked in by aclsce, 10 years ago

Fixed problem encountered in the case "using_oasis=true" : automatic detection of a server does not work in coupled mode.
Back to the old method : if you want to use XIOS server mode, it is mandatory to specify "using_server=true" in iodef.xml in coupled case.

  • 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.4 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
157      if (!CXios::isServer)
158      {
159        int size,rank,globalRank ;
160        size_t message_size ;
161        int leaderRank ;
162        MPI_Comm contextInterComm ;
163
164        MPI_Comm_size(contextComm,&size) ;
165        MPI_Comm_rank(contextComm,&rank) ;
166        MPI_Comm_rank(CXios::globalComm,&globalRank) ;
167        if (rank!=0) globalRank=0 ;
168
169
170        CMessage msg ;
171        msg<<id<<size<<globalRank ;
172
173        int messageSize=msg.size() ;
174        void * buff = new char[messageSize] ;
175        CBufferOut buffer(buff,messageSize) ;
176        buffer<<msg ;
177
178        MPI_Send(buff,buffer.count(),MPI_CHAR,serverLeader,1,CXios::globalComm) ;
179        delete [] buff ;
180
181        MPI_Intercomm_create(contextComm,0,CXios::globalComm,serverLeader,10+globalRank,&contextInterComm) ;
182        info(10)<<"Register new Context : "<<id<<endl ;
183
184        MPI_Comm inter ;
185        MPI_Intercomm_merge(contextInterComm,0,&inter) ;
186        MPI_Barrier(inter) ;
187
188        context->initClient(contextComm,contextInterComm) ;
189      }
190      else
191      {
192        MPI_Comm contextInterComm ;
193        MPI_Comm_dup(contextComm,&contextInterComm) ;
194        context->initClient(contextComm,contextInterComm) ;
195        context->initServer(contextComm,contextInterComm) ;
196      }
197    }
198
199    void CClient::finalize(void)
200    {
201      int rank ;
202      int msg=0 ;
203      if (!CXios::isServer)
204      {
205        MPI_Comm_rank(intraComm,&rank) ;
206        if (rank==0)
207        {
208          MPI_Send(&msg,1,MPI_INT,0,0,interComm) ;
209        }
210      }
211
212     CTimer::get("XIOS finalize").suspend() ;
213     CTimer::get("XIOS").suspend() ;
214
215      if (!is_MPI_Initialized)
216      {
217        if (CXios::usingOasis) oasis_finalize();
218        else MPI_Finalize() ;
219      }
220      info(20) << "Client side context is finalized"<<endl ;
221      report(0) <<" Performance report : total time spent for XIOS : "<< CTimer::get("XIOS").getCumulatedTime()<<" s"<<endl ;
222      report(0)<< " Performance report : time spent for waiting free buffer : "<< CTimer::get("Blocking time").getCumulatedTime()<<" s"<<endl ;
223      report(0)<< " Performance report : Ratio : "<< CTimer::get("Blocking time").getCumulatedTime()/CTimer::get("XIOS").getCumulatedTime()*100.<<" %"<<endl ;
224      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 ;
225      report(0)<< " Memory report : Current buffer_size : "<<CXios::bufferSize<<endl ;
226      report(0)<< " Memory report : Minimum buffer size required : "<<maxRequestSize*2<<endl ;
227      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 ;
228   }
229
230   int CClient::getRank()
231   {
232     return rank;
233   }
234
235     /*!
236      * \brief Open file stream to write in
237      *   Opening a file stream with a specific file name suffix-client+rank
238      * \param [in] protype file name
239     */
240     void CClient::openInfoStream(const StdString& fileName)
241     {
242       std::filebuf* fb = m_infoStream.rdbuf();
243       StdStringStream fileNameClient;
244       int numDigit = 0;
245       int size = 0;
246       MPI_Comm_size(CXios::globalComm, &size);
247       while (size)
248       {
249         size /= 10;
250         ++numDigit;
251       }
252
253       fileNameClient << fileName << "_" << std::setfill('0') << std::setw(numDigit) << getRank() << ".out";
254       fb->open(fileNameClient.str().c_str(), std::ios::out);
255       if (!fb->is_open())
256       ERROR("void CClient::openInfoStream(const StdString& fileName)",
257            <<endl<< "Can not open <"<<fileNameClient<<"> file to write" );
258
259       info.write2File(fb);
260       report.write2File(fb);
261     }
262
263     //! Write out to standard output
264     void CClient::openInfoStream()
265     {
266       info.write2StdOut();
267       report.write2StdOut();
268     }
269
270     //! Close file if it opens
271     void CClient::closeInfoStream()
272     {
273       if (m_infoStream.is_open()) m_infoStream.close();
274     }
275
276
277}
Note: See TracBrowser for help on using the repository browser.