source: XIOS2/dev/hshepherd/reduce_output_log/src/client.cpp @ 2711

Last change on this file since 2711 was 2711, checked in by hshepherd, 9 days ago

Fix indentation

  • 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: 16.2 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 "mem_checker.hpp"
13#include "buffer_client.hpp"
14#include "string_tools.hpp"
15#include "timestats.hpp"
16
17namespace xios
18{
19
20    MPI_Comm CClient::intraComm ;
21    MPI_Comm CClient::interComm ;
22    std::list<MPI_Comm> CClient::contextInterComms;
23    int CClient::serverLeader ;
24    bool CClient::is_MPI_Initialized ;
25    int CClient::rank_ = INVALID_RANK;
26    StdOFStream CClient::m_infoStream;
27    StdOFStream CClient::m_errorStream;
28    MPI_Comm& CClient::getInterComm(void)   { return (interComm); }
29     
30///---------------------------------------------------------------
31/*!
32 * \fn void CClient::initialize(const string& codeId, MPI_Comm& localComm, MPI_Comm& returnComm)
33 * Function creates intraComm (CClient::intraComm) for client group with id=codeId and interComm (CClient::interComm) between client and server groups.
34 * \param [in] codeId identity of context.
35 * \param [in/out] localComm local communicator.
36 * \param [in/out] returnComm (intra)communicator of client group.
37 */
38
39    void CClient::initialize(const string& codeId, MPI_Comm& localComm, MPI_Comm& returnComm)
40    {
41      int initialized ;
42      MPI_Initialized(&initialized) ;
43      if (initialized) is_MPI_Initialized=true ;
44      else is_MPI_Initialized=false ;
45      int rank ;
46
47// don't use OASIS
48      if (!CXios::usingOasis)
49      {
50// localComm isn't given
51        if (localComm == MPI_COMM_NULL)
52        {
53          if (!is_MPI_Initialized)
54          {
55            MPI_Init(NULL, NULL);
56          }
57          CTimer::get("XIOS").resume() ;
58          CTimer::get("XIOS init/finalize").resume() ;
59          boost::hash<string> hashString ;
60
61          unsigned long hashClient=hashString(codeId) ;
62          unsigned long hashServer=hashString(CXios::xiosCodeId) ;
63          unsigned long* hashAll ;
64          int size ;
65          int myColor ;
66          int i,c ;
67          MPI_Comm newComm ;
68
69          MPI_Comm_size(CXios::globalComm,&size) ;
70          MPI_Comm_rank(CXios::globalComm,&rank_);
71
72          hashAll=new unsigned long[size] ;
73
74          MPI_Allgather(&hashClient,1,MPI_LONG,hashAll,1,MPI_LONG,CXios::globalComm) ;
75
76          map<unsigned long, int> colors ;
77          map<unsigned long, int> leaders ;
78
79          for(i=0,c=0;i<size;i++)
80          {
81            if (colors.find(hashAll[i])==colors.end())
82            {
83              colors[hashAll[i]] =c ;
84              leaders[hashAll[i]]=i ;
85              c++ ;
86            }
87          }
88
89          // Verify whether we are on server mode or not
90          CXios::setNotUsingServer();
91          for (i=0; i < size; ++i)
92          {
93            if (hashServer == hashAll[i])
94            {
95              CXios::setUsingServer();
96              break;
97            }
98          }
99
100          myColor=colors[hashClient];
101          MPI_Comm_split(CXios::globalComm,myColor,rank_,&intraComm) ;
102
103          if (CXios::usingServer)
104          {
105            int clientLeader=leaders[hashClient] ;
106            serverLeader=leaders[hashServer] ;
107            int intraCommSize, intraCommRank ;
108            MPI_Comm_size(intraComm,&intraCommSize) ;
109            MPI_Comm_rank(intraComm,&intraCommRank) ;
110            info(50)<<"intercommCreate::client "<<rank_<<" intraCommSize : "<<intraCommSize
111                   <<" intraCommRank :"<<intraCommRank<<"  clientLeader "<< serverLeader<<endl ;
112             MPI_Intercomm_create(intraComm, 0, CXios::globalComm, serverLeader, 0, &interComm) ;
113             //rank_ = intraCommRank;
114          }
115          else
116          {
117            MPI_Comm_dup(intraComm,&interComm) ;
118          }
119          delete [] hashAll ;
120        }
121        // localComm argument is given
122        else
123        {
124          if (CXios::usingServer)
125          {
126            //ERROR("void CClient::initialize(const string& codeId,MPI_Comm& localComm,MPI_Comm& returnComm)", << " giving a local communictor is not compatible with using server mode") ;
127          }
128          else
129          {
130            MPI_Comm_dup(localComm,&intraComm) ;
131            MPI_Comm_dup(intraComm,&interComm) ;
132          }
133        }
134      }
135      // using OASIS
136      else
137      {
138        // localComm isn't given
139        if (localComm == MPI_COMM_NULL)
140        {
141          if (!is_MPI_Initialized) oasis_init(codeId) ;
142          oasis_get_localcomm(localComm) ;
143        }
144        MPI_Comm_dup(localComm,&intraComm) ;
145
146        CTimer::get("XIOS").resume() ;
147        CTimer::get("XIOS init/finalize").resume() ;
148
149        if (CXios::usingServer)
150        {
151          MPI_Status status ;
152          MPI_Comm_rank(intraComm,&rank_) ;
153
154          oasis_get_intercomm(interComm,CXios::xiosCodeId) ;
155          if (rank_==0) MPI_Recv(&serverLeader,1, MPI_INT, 0, 0, interComm, &status) ;
156          MPI_Bcast(&serverLeader,1,MPI_INT,0,intraComm) ;
157        }
158        else MPI_Comm_dup(intraComm,&interComm) ;
159      }
160
161      MPI_Comm_dup(intraComm,&returnComm) ;
162    }
163
164///---------------------------------------------------------------
165/*!
166 * \fn void CClient::registerContext(const string& id, MPI_Comm contextComm)
167 * \brief Sends a request to create a context to server. Creates client/server contexts.
168 * \param [in] id id of context.
169 * \param [in] contextComm.
170 * Function is only called by client.
171 */
172    void CClient::registerContext(const string& id, MPI_Comm contextComm)
173    {
174      CContext::setCurrent(id) ;
175      CContext* context=CContext::create(id);
176      StdString idServer(id);
177      idServer += "_server";
178
179      if (CXios::isServer && !context->hasServer)
180      // Attached mode
181      {
182        MPI_Comm contextInterComm ;
183        MPI_Comm_dup(contextComm,&contextInterComm) ;
184        CContext* contextServer = CContext::create(idServer);
185
186        // Firstly, initialize context on client side
187        context->initClient(contextComm,contextInterComm, contextServer);
188
189        // Secondly, initialize context on server side
190        contextServer->initServer(contextComm,contextInterComm, context);
191
192        // Finally, we should return current context to context client
193        CContext::setCurrent(id);
194
195        contextInterComms.push_back(contextInterComm);
196      }
197      else
198      {
199        int size,rank,globalRank ;
200        size_t message_size ;
201        int leaderRank ;
202        MPI_Comm contextInterComm ;
203
204        MPI_Comm_size(contextComm,&size) ;
205        MPI_Comm_rank(contextComm,&rank) ;
206        MPI_Comm_rank(CXios::globalComm,&globalRank) ;
207        if (rank!=0) globalRank=0 ;
208
209        CMessage msg ;
210        msg<<idServer<<size<<globalRank ;
211//        msg<<id<<size<<globalRank ;
212
213        int messageSize=msg.size() ;
214        char * buff = new char[messageSize] ;
215        CBufferOut buffer((void*)buff,messageSize) ;
216        buffer<<msg ;
217
218        MPI_Send((void*)buff,buffer.count(),MPI_CHAR,serverLeader,1,CXios::globalComm) ;
219
220        MPI_Intercomm_create(contextComm,0,CXios::globalComm,serverLeader,10+globalRank,&contextInterComm) ;
221        info(10)<<"Register new Context : "<<id<<endl ;
222        MPI_Comm inter ;
223        MPI_Intercomm_merge(contextInterComm,0,&inter) ;
224        MPI_Barrier(inter) ;
225        MPI_Comm_free(&inter);
226
227        context->initClient(contextComm,contextInterComm) ;
228
229        contextInterComms.push_back(contextInterComm);
230        delete [] buff ;
231
232      }
233    }
234
235/*!
236 * \fn void CClient::callOasisEnddef(void)
237 * \brief Send the order to the servers to call "oasis_enddef". It must be done by each compound of models before calling oasis_enddef on client side
238 * Function is only called by client.
239 */
240    void CClient::callOasisEnddef(void)
241    {
242      bool oasisEnddef=CXios::getin<bool>("call_oasis_enddef",true) ;
243      if (!oasisEnddef) ERROR("void CClient::callOasisEnddef(void)", <<"Function xios_oasis_enddef called but variable <call_oasis_enddef> is set to false."<<endl
244                                                                     <<"Variable <call_oasis_enddef> must be set to true"<<endl) ;
245      if (CXios::isServer)
246      // Attached mode
247      {
248        // nothing to do   
249      }
250      else
251      {
252        int rank ;
253        int msg=0 ;
254
255        MPI_Comm_rank(intraComm,&rank) ;
256        if (rank==0) 
257        {
258          MPI_Send(&msg,1,MPI_INT,0,5,interComm) ; // tags oasis_endded = 5
259        }
260
261      }
262    }
263
264    std::vector<double> CClient::collate_timings(std::string timing_param)
265    {
266      int myrank, comm_size;
267      MPI_Comm_rank(intraComm, &myrank);
268      MPI_Comm_size(intraComm, &comm_size);
269
270      std::vector<double> collated_results;
271      MPI_Barrier(intraComm);
272      if (myrank == 0) {
273        double recv_val = 0.;
274        collated_results.push_back(CTimer::get(timing_param).getCumulatedTime());
275        for (int i = 1; i < comm_size; i++) {
276          MPI_Recv(&recv_val, 1, MPI_DOUBLE, i, 0, intraComm, MPI_STATUS_IGNORE);
277          collated_results.push_back(recv_val);
278        }
279      } else {
280        double snd_val = CTimer::get(timing_param).getCumulatedTime();
281        MPI_Send(&snd_val, 1, MPI_DOUBLE, 0, 0, intraComm);
282      }
283      MPI_Barrier(intraComm);
284      return collated_results;
285    }
286
287    void CClient::present_collated_timings(void)
288    {
289      std::vector<double> blocking_times = collate_timings("Blocking time");
290      // we need to suspend the XIOS init/finalize timer briefly to gather the measurements
291      CTimer::get("XIOS init/finalize").suspend();
292      std::vector<double> init_final_times = collate_timings("XIOS init/finalize");
293      CTimer::get("XIOS init/finalize").resume();
294      std::vector<double> ratio = percentage_ratio_vec_double(blocking_times, init_final_times);
295      if (rank_ == 0) {
296        int n_ranks;
297        MPI_Comm_size(intraComm, &n_ranks);
298       
299        report(0) << " Performance metrics across all client ranks" << endl;
300        report(0) << "     Collating from " << n_ranks << " ranks" << endl;
301        write_summary_timings(blocking_times, "Time spent waiting for free buffer");
302        write_summary_timings(ratio, "Waiting ratio (percentage)");
303      }
304    }
305
306    void CClient::write_summary_timings(std::vector<double>& collated_results,
307                                        std::string results_label)
308    {
309      report(0) << "  " << results_label << " average " << calc_mean_double(collated_results) << endl;
310      report(0) << "  " << results_label << " std dev " << calc_std_double(collated_results) << endl;
311      report(0) << "  " << results_label << " min " << calc_min_double(collated_results) << endl;
312      report(0) << "  " << results_label << " max " << calc_max_double(collated_results) << endl;
313    }
314
315    void CClient::finalize(void)
316    {
317      int rank ;
318      int msg=0 ;
319
320      // Do our collated summary
321      present_collated_timings();
322      MPI_Barrier(intraComm);
323
324      MPI_Comm_rank(intraComm,&rank) ;
325 
326      if (!CXios::isServer)
327      {
328        MPI_Comm_rank(intraComm,&rank) ;
329        if (rank==0)
330        {
331          MPI_Send(&msg,1,MPI_INT,0,0,interComm) ;
332        }
333      }
334
335
336      /* MPI_Comm_free(&interComm); */ // WARNING remove freeing communicator !! --> deadlock raised, to be checked
337      for (std::list<MPI_Comm>::iterator it = contextInterComms.begin(); it != contextInterComms.end(); it++)
338        /* MPI_Comm_free(&(*it)) */ ; // WARNING remove freeing communicator !! --> deadlock raised, to be checked
339      MPI_Comm_free(&intraComm);
340
341      CTimer::get("XIOS init/finalize").suspend() ;
342      CTimer::get("XIOS").suspend() ;
343
344      if (!is_MPI_Initialized)
345      {
346        if (CXios::usingOasis) oasis_finalize();
347        else MPI_Finalize() ;
348      }
349     
350      info(20) << "Client side context is finalized"<<endl ;
351
352      if (CXios::reduceLogFiles) report(0) << "Performance summary from the first rank in this intraComm." << endl;
353
354      report(0) <<" Performance report : Whole time from XIOS init and finalize: "<< CTimer::get("XIOS init/finalize").getCumulatedTime()<<" s"<<endl ;
355      report(0) <<" Performance report : total time spent for XIOS : "<< CTimer::get("XIOS").getCumulatedTime()<<" s"<<endl ;
356      report(0)<< " Performance report : time spent for waiting free buffer : "<< CTimer::get("Blocking time").getCumulatedTime()<<" s"<<endl ;
357      report(0)<< " Performance report : Ratio : "<< CTimer::get("Blocking time").getCumulatedTime()/CTimer::get("XIOS init/finalize").getCumulatedTime()*100.<<" %"<<endl ;
358      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 ;
359//      report(0)<< " Memory report : Current buffer_size : "<<CXios::bufferSize<<endl ;
360      report(0)<< " Memory report : Minimum buffer size required : " << CClientBuffer::maxRequestSize << " bytes" << endl ;
361      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 ;
362      report(100)<<CTimer::getAllCumulatedTime()<<endl ;
363      if (CXios::reportMemory)
364      {
365        report(100)<<CMemChecker::getAllCumulatedMem()<<endl ;
366      }
367   }
368
369    /*!
370    * Return global rank without oasis and current rank in model intraComm in case of oasis
371    */
372   int CClient::getRank()
373   {
374     return rank_;
375   }
376
377    /*!
378    * Open a file specified by a suffix and an extension and use it for the given file buffer.
379    * The file name will be suffix+rank+extension.
380    *
381    * \param fileName[in] protype file name
382    * \param ext [in] extension of the file
383    * \param fb [in/out] the file buffer
384    */
385    void CClient::openStream(const StdString& fileName, const StdString& ext, std::filebuf* fb)
386    {
387      StdStringStream fileNameClient;
388      int numDigit = 0;
389      int size = 0;
390      int rank;
391      MPI_Comm_size(CXios::globalComm, &size);
392      while (size)
393      {
394        size /= 10;
395        ++numDigit;
396      }
397
398      if (CXios::usingOasis)
399      {
400        MPI_Comm_rank(CXios::globalComm,&rank);
401        fileNameClient << fileName << "_" << std::setfill('0') << std::setw(numDigit) << rank << ext;
402      }
403      else
404        fileNameClient << fileName << "_" << std::setfill('0') << std::setw(numDigit) << getRank() << ext;
405
406
407      fb->open(fileNameClient.str().c_str(), std::ios::out);
408      if (!fb->is_open())
409        ERROR("void CClient::openStream(const StdString& fileName, const StdString& ext, std::filebuf* fb)",
410              << std::endl << "Can not open <" << fileNameClient.str() << "> file to write the client log(s).");
411    }
412
413    /*!
414    * \brief Check to see if we are on a rank for which we an output file
415    * If reduced output is selected we only write from a single client
416    * rank
417    */
418    bool CClient::writeLogFromRank()
419    {
420      bool do_write = false;
421      if (!CXios::reduceLogFiles)
422      {
423        return true;
424      }
425      if (rank_ == 0)
426      {
427        do_write = true;
428      }
429      return do_write;
430    }
431
432    /*!
433    * \brief Open a file stream to write the info logs
434    * Open a file stream with a specific file name suffix+rank
435    * to write the info logs.
436    * \param fileName [in] protype file name
437    */
438        void CClient::openInfoStream(const StdString& fileName)
439    {
440      if (writeLogFromRank())
441      {
442        std::filebuf* fb = m_infoStream.rdbuf();
443        openStream(fileName, ".out", fb);
444
445        info.write2File(fb);
446        report.write2File(fb);
447      }
448    }
449
450    //! Write the info logs to standard output
451    void CClient::openInfoStream()
452    {
453      if (writeLogFromRank())
454      {
455        info.write2StdOut();
456        report.write2StdOut();
457      }
458    }
459
460    //! Close the info logs file if it opens
461    void CClient::closeInfoStream()
462    {
463      if (writeLogFromRank())
464      {
465        if (m_infoStream.is_open()) m_infoStream.close();
466      }
467    }
468
469
470    /*!
471    * \brief Open a file stream to write the error log
472    * Open a file stream with a specific file name suffix+rank
473    * to write the error log.
474    * \param fileName [in] protype file name
475    */
476    void CClient::openErrorStream(const StdString& fileName)
477    {
478      if (writeLogFromRank())
479      {
480        std::filebuf* fb = m_errorStream.rdbuf();
481        openStream(fileName, ".err", fb);
482
483        error.write2File(fb);
484      }
485    }
486
487    //! Write the error log to standard error output
488    void CClient::openErrorStream()
489    {
490      if (writeLogFromRank())
491      {
492        error.write2StdErr();
493      }
494    }
495
496    //! Close the error log file if it opens
497    void CClient::closeErrorStream()
498    {
499      if (writeLogFromRank())
500      {
501        if (m_errorStream.is_open()) m_errorStream.close();
502      }
503    }
504}
Note: See TracBrowser for help on using the repository browser.