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

Last change on this file since 490 was 490, checked in by mhnguyen, 10 years ago

Implementing print output in seperate files

+) Add an option to write information into standard output or into files,
each of which is created by a process
+) Add a new file for global data (constant, value macro, etc)
+) Do a minor change in how to generate doxygen

Test
+) On Curie, with two modes: only client (connected) and client-server
+) All tests passed, each client prints out its info in a seperate file

  • Property svn:eol-style set to native
File size: 7.5 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          myColor=colors[hashClient] ;
76
77          MPI_Comm_split(CXios::globalComm,myColor,rank,&intraComm) ;
78
79          if (CXios::usingServer)
80          {
81            int clientLeader=leaders[hashClient] ;
82            serverLeader=leaders[hashServer] ;
83            MPI_Intercomm_create(intraComm,0,CXios::globalComm,serverLeader,0,&interComm) ;
84          }
85          else
86          {
87            MPI_Comm_dup(intraComm,&interComm) ;
88          }
89          delete [] hashAll ;
90        }
91        // localComm argument is given
92        else
93        {
94          if (CXios::usingServer)
95          {
96            //ERROR("void CClient::initialize(const string& codeId,MPI_Comm& localComm,MPI_Comm& returnComm)", << " giving a local communictor is not compatible with using server mode") ;
97          }
98          else
99          {
100            MPI_Comm_dup(localComm,&intraComm) ;
101            MPI_Comm_dup(intraComm,&interComm) ;
102          }
103        }
104      }
105      // using OASIS
106      else
107      {
108        // localComm doesn't given
109        if (localComm == MPI_COMM_NULL)
110        {
111          if (!is_MPI_Initialized) oasis_init(codeId) ;
112          oasis_get_localcomm(intraComm) ;
113        }
114        else MPI_Comm_dup(localComm,&intraComm) ;
115        CTimer::get("XIOS").resume() ;
116        CTimer::get("XIOS init").resume() ;
117
118        if (CXios::usingServer)
119        {
120          MPI_Status status ;
121            MPI_Comm_rank(intraComm,&rank) ;
122          oasis_get_intercomm(interComm,CXios::xiosCodeId) ;
123          if (rank==0) MPI_Recv(&serverLeader,1, MPI_INT, 0, 0, interComm, &status) ;
124          MPI_Bcast(&serverLeader,1,MPI_INT,0,intraComm) ;
125
126        }
127        else MPI_Comm_dup(intraComm,&interComm) ;
128      }
129
130      MPI_Comm_dup(intraComm,&returnComm) ;
131    }
132
133
134    void CClient::registerContext(const string& id,MPI_Comm contextComm)
135    {
136      CContext::setCurrent(id) ;
137      CContext* context=CContext::create(id) ;
138
139      if (!CXios::isServer)
140      {
141        int size,rank,globalRank ;
142        size_t message_size ;
143        int leaderRank ;
144        MPI_Comm contextInterComm ;
145
146        MPI_Comm_size(contextComm,&size) ;
147        MPI_Comm_rank(contextComm,&rank) ;
148        MPI_Comm_rank(CXios::globalComm,&globalRank) ;
149        if (rank!=0) globalRank=0 ;
150
151
152        CMessage msg ;
153        msg<<id<<size<<globalRank ;
154
155        int messageSize=msg.size() ;
156        void * buff = new char[messageSize] ;
157        CBufferOut buffer(buff,messageSize) ;
158        buffer<<msg ;
159
160        MPI_Send(buff,buffer.count(),MPI_CHAR,serverLeader,1,CXios::globalComm) ;
161        delete [] buff ;
162
163        MPI_Intercomm_create(contextComm,0,CXios::globalComm,serverLeader,10+globalRank,&contextInterComm) ;
164        info(10)<<"Register new Context : "<<id<<endl ;
165
166        MPI_Comm inter ;
167        MPI_Intercomm_merge(contextInterComm,0,&inter) ;
168        MPI_Barrier(inter) ;
169
170        context->initClient(contextComm,contextInterComm) ;
171      }
172      else
173      {
174        MPI_Comm contextInterComm ;
175        MPI_Comm_dup(contextComm,&contextInterComm) ;
176        context->initClient(contextComm,contextInterComm) ;
177        context->initServer(contextComm,contextInterComm) ;
178      }
179    }
180
181    void CClient::finalize(void)
182    {
183      int rank ;
184      int msg=0 ;
185      if (!CXios::isServer)
186      {
187        MPI_Comm_rank(intraComm,&rank) ;
188        if (rank==0)
189        {
190          MPI_Send(&msg,1,MPI_INT,0,0,interComm) ;
191        }
192      }
193
194     CTimer::get("XIOS finalize").suspend() ;
195     CTimer::get("XIOS").suspend() ;
196
197      if (!is_MPI_Initialized)
198      {
199        if (CXios::usingOasis) oasis_finalize();
200        else MPI_Finalize() ;
201      }
202      info(20) << "Client side context is finalized"<<endl ;
203      report(0) <<" Performance report : total time spent for XIOS : "<< CTimer::get("XIOS").getCumulatedTime()<<" s"<<endl ;
204      report(0)<< " Performance report : time spent for waiting free buffer : "<< CTimer::get("Blocking time").getCumulatedTime()<<" s"<<endl ;
205      report(0)<< " Performance report : Ratio : "<< CTimer::get("Blocking time").getCumulatedTime()/CTimer::get("XIOS").getCumulatedTime()*100.<<" %"<<endl ;
206      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 ;
207      report(0)<< " Memory report : Current buffer_size : "<<CXios::bufferSize<<endl ;
208      report(0)<< " Memory report : Minimum buffer size required : "<<maxRequestSize*2<<endl ;
209      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 ;
210   }
211
212   int CClient::getRank()
213   {
214     return rank;
215   }
216
217     /*!
218      * \brief Open file stream to write in
219      *   Opening a file stream with a specific file name suffix-client+rank
220      * \param [in] protype file name
221     */
222     void CClient::openInfoStream(const StdString& fileName)
223     {
224       std::filebuf* fb = m_infoStream.rdbuf();
225       StdStringStream fileNameClient;
226       fileNameClient << fileName <<"_client_" << getRank() << ".txt";
227       fb->open(fileNameClient.str().c_str(), std::ios::out);
228       if (!fb->is_open())
229       ERROR("void CClient::openInfoStream(const StdString& fileName)",
230            <<endl<< "Can not open <"<<fileNameClient<<"> file to write" );
231
232       info.write2File(fb);
233     }
234
235     //! Write out to standard output
236     void CClient::openInfoStream()
237     {
238       info.write2StdOut();
239     }
240
241     //! Close file if it opens
242     void CClient::closeInfoStream()
243     {
244       if (m_infoStream.is_open()) m_infoStream.close();
245     }
246
247
248}
Note: See TracBrowser for help on using the repository browser.