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

Last change on this file since 364 was 361, checked in by ymipsl, 12 years ago

Bug correction : using MPI_WTIME MPI function for tracing before MPI_Init is called.

YM

  • Property svn:eol-style set to native
File size: 6.2 KB
Line 
1#include "xmlioserver_spl.hpp"
2#include "cxios.hpp"
3#include "client.hpp"
4#include <boost/functional/hash.hpp>
5#include "type.hpp"
6#include "context.hpp"
7#include "context_client.hpp"
8#include "oasis_cinterface.hpp"
9#include <mpi.h>
10#include "timer.hpp"
11
12namespace xios
13{                     
14
15    MPI_Comm CClient::intraComm ;
16    MPI_Comm CClient::interComm ;
17    int CClient::serverLeader ;
18    bool CClient::is_MPI_Initialized ;
19   
20   
21    void CClient::initialize(const string& codeId,MPI_Comm& localComm,MPI_Comm& returnComm)
22    {
23      int initialized ;
24      MPI_Initialized(&initialized) ;
25      if (initialized) is_MPI_Initialized=true ;
26      else is_MPI_Initialized=false ;
27     
28// don't use OASIS
29      if (!CXios::usingOasis)
30      {
31// localComm doesn't given
32        if (localComm == MPI_COMM_NULL)
33        {
34          if (!is_MPI_Initialized) 
35          {
36            int argc=0;
37            char** argv=NULL;
38            MPI_Init(&argc,&argv) ;
39          }
40          CTimer::get("XIOS").resume() ;
41          CTimer::get("XIOS init").resume() ;
42          boost::hash<string> hashString ;   
43   
44          unsigned long hashClient=hashString(codeId) ;
45          unsigned long hashServer=hashString(CXios::xiosCodeId) ;
46          unsigned long* hashAll ;
47          int rank ;
48          int size ;
49          int myColor ;
50          int i,c ;
51          MPI_Comm newComm ;
52     
53          MPI_Comm_size(CXios::globalComm,&size) ;
54          MPI_Comm_rank(CXios::globalComm,&rank);
55          hashAll=new unsigned long[size] ;
56     
57          MPI_Allgather(&hashClient,1,MPI_LONG,hashAll,1,MPI_LONG,CXios::globalComm) ;
58
59          map<unsigned long, int> colors ;
60          map<unsigned long, int> leaders ;
61     
62          for(i=0,c=0;i<size;i++)
63          {
64            if (colors.find(hashAll[i])==colors.end())
65            {
66              colors[hashAll[i]] =c ;
67              leaders[hashAll[i]]=i ;
68              c++ ;
69            }
70          }
71     
72          myColor=colors[hashClient] ;
73     
74          MPI_Comm_split(CXios::globalComm,myColor,rank,&intraComm) ;
75
76          if (CXios::usingServer)
77          {     
78            int clientLeader=leaders[hashClient] ;
79            serverLeader=leaders[hashServer] ;
80            MPI_Intercomm_create(intraComm,0,CXios::globalComm,serverLeader,0,&interComm) ;
81          }
82          else
83          {
84            MPI_Comm_dup(intraComm,&interComm) ;
85          }
86          delete [] hashAll ;
87        }
88        // localComm argument is given
89        else 
90        {
91          if (CXios::usingServer)
92          {         
93            //ERROR("void CClient::initialize(const string& codeId,MPI_Comm& localComm,MPI_Comm& returnComm)", << " giving a local communictor is not compatible with using server mode") ;
94          }
95          else
96          {
97            MPI_Comm_dup(localComm,&intraComm) ;
98            MPI_Comm_dup(intraComm,&interComm) ;
99          }
100        }
101      }
102      // using OASIS
103      else
104      {
105        // localComm doesn't given
106        if (localComm == MPI_COMM_NULL)
107        {
108          if (!is_MPI_Initialized) oasis_init(codeId) ;
109          oasis_get_localcomm(intraComm) ;
110        }
111        else MPI_Comm_dup(localComm,&intraComm) ;
112        CTimer::get("XIOS").resume() ;
113        CTimer::get("XIOS init").resume() ;
114 
115        if (CXios::usingServer) 
116        {
117          MPI_Status status ;
118          int rank ;
119          MPI_Comm_rank(intraComm,&rank) ;
120
121          oasis_get_intercomm(interComm,CXios::xiosCodeId) ;
122          if (rank==0) MPI_Recv(&serverLeader,1, MPI_INT, 0, 0, interComm, &status) ;
123          MPI_Bcast(&serverLeader,1,MPI_INT,0,intraComm) ;
124           
125        }
126        else MPI_Comm_dup(intraComm,&interComm) ;
127      }
128         
129      MPI_Comm_dup(intraComm,&returnComm) ;
130    }
131   
132   
133    void CClient::registerContext(const string& id,MPI_Comm contextComm)
134    {
135      CContext::setCurrent(id) ;
136      CContext* context=CContext::create(id) ;
137       
138      if (!CXios::isServer)
139      {
140        int size,rank,globalRank ;
141        size_t message_size ;
142        int leaderRank ;
143        MPI_Comm contextInterComm ;
144     
145        MPI_Comm_size(contextComm,&size) ;
146        MPI_Comm_rank(contextComm,&rank) ;
147        MPI_Comm_rank(CXios::globalComm,&globalRank) ;
148        if (rank!=0) globalRank=0 ;
149     
150   
151        CMessage msg ;
152        msg<<id<<size<<globalRank ;
153
154        int messageSize=msg.size() ;
155        void * buff = new char[messageSize] ;
156        CBufferOut buffer(buff,messageSize) ;
157        buffer<<msg ;
158     
159        MPI_Send(buff,buffer.count(),MPI_CHAR,serverLeader,1,CXios::globalComm) ;
160        delete [] buff ;
161     
162        MPI_Intercomm_create(contextComm,0,CXios::globalComm,serverLeader,10+globalRank,&contextInterComm) ;
163        info(10)<<"Register new Context : "<<id<<endl ;
164 
165        MPI_Comm inter ;
166        MPI_Intercomm_merge(contextInterComm,0,&inter) ;
167        MPI_Barrier(inter) ;
168
169        context->initClient(contextComm,contextInterComm) ;
170      }
171      else
172      {
173        MPI_Comm contextInterComm ;
174        MPI_Comm_dup(contextComm,&contextInterComm) ;
175        context->initClient(contextComm,contextInterComm) ;
176        context->initServer(contextComm,contextInterComm) ;
177      }
178    }
179   
180    void CClient::finalize(void)
181    {
182      int rank ;
183      int msg=0 ;
184      if (!CXios::isServer)
185      {
186        MPI_Comm_rank(intraComm,&rank) ; 
187        if (rank==0) 
188        {
189          MPI_Send(&msg,1,MPI_INT,0,0,interComm) ;
190        }
191      }
192     
193     CTimer::get("XIOS finalize").suspend() ;
194     CTimer::get("XIOS").suspend() ;
195
196      if (!is_MPI_Initialized)
197      {
198        if (CXios::usingOasis) oasis_finalize();
199        else MPI_Finalize() ;
200      }
201      info(20) << "Client side context is finalized"<<endl ;
202      report(0) <<" Performance report : total time spent for XIOS : "<< CTimer::get("XIOS").getCumulatedTime()<<" s"<<endl ; 
203      report(0)<< " Performance report : time spent for waiting free buffer : "<< CTimer::get("Blocking time").getCumulatedTime()<<" s"<<endl ;
204      report(0)<< " Performance report : Ratio : "<< CTimer::get("Blocking time").getCumulatedTime()/CTimer::get("XIOS").getCumulatedTime()*100.<<" %"<<endl ;
205      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 ;
206    }
207}
Note: See TracBrowser for help on using the repository browser.