source: XIOS/dev/dev_ym/XIOS_COUPLING/src/manager/pool_ressource.cpp @ 2246

Last change on this file since 2246 was 2246, checked in by ymipsl, 3 years ago
  • Update of the tranfer protocol using one sided communication
  • Introduce MPI_Improb/MPI_mrecv to listen incomming request
  • Introducing latency when looping over managers

YM

  • Property svn:eol-style set to native
  • Property svn:executable set to *
File size: 6.9 KB
Line 
1#include "pool_ressource.hpp"
2#include "services.hpp"
3#include "buffer_in.hpp"
4#include "buffer_out.hpp"
5#include "message.hpp"
6#include "type.hpp"
7#include "cxios.hpp"
8#include "timer.hpp"
9
10namespace xios
11{
12  CPoolRessource::CPoolRessource(MPI_Comm poolComm, const std::string& Id) : Id_(Id), finalizeSignal_(false)
13  {
14    int commRank, commSize ;
15    MPI_Comm_dup(poolComm, &poolComm_) ;
16    winNotify_ = new CWindowManager(poolComm_, maxBufferSize_) ;
17    MPI_Comm_rank(poolComm, &commRank) ;
18    MPI_Comm_size(poolComm, &commSize) ;
19    info(40)<<"CPoolRessource::CPoolRessource  : creating new pool : "<<Id<<endl ;
20    if (commRank==localLeader_)
21    {
22      for(int i=0; i<commSize;i++) occupancy_.insert(std::pair<char,int>(0,i)) ; 
23      int globalLeaderRank ;
24      MPI_Comm_rank(CXios::getXiosComm(),&globalLeaderRank) ;
25      CXios::getRessourcesManager()->registerPool(Id, commSize, globalLeaderRank) ;
26    }
27   
28    winNotify_->lockWindow(commRank,0) ;
29    winNotify_->updateToWindow(commRank, this, &CPoolRessource::createServiceDumpOut) ; 
30    winNotify_->unlockWindow(commRank,0) ;       
31    MPI_Barrier(poolComm_) ;
32  }
33
34  void CPoolRessource::createService(const std::string& serviceId, int type, int size, int nbPartitions)
35  {
36    // for now suppose nbPartitions=1
37   
38    auto it=occupancy_.begin() ;
39    int commSize ;
40    MPI_Comm_size(poolComm_, &commSize) ;
41    vector<bool> procs_in(commSize,false) ;
42    vector<pair<int,int>> procs_update ;
43
44    for(int i=0; i<size; i++) 
45    {
46      procs_in[it->second]=true ;
47      procs_update.push_back(std::pair<int,int>(it->first+1,it->second)) ;
48      ++it ;
49    }
50   
51    occupancy_.erase(occupancy_.begin(),it) ;
52    occupancy_.insert(procs_update.begin(),procs_update.end()) ;
53   
54    info(40)<<"CPoolRessource::createService  : notify createService to all pool members ; serviceId : "<<serviceId<<endl ;
55    for(int rank=0; rank<commSize; rank++)
56    {
57      if (procs_in[rank]) createServiceNotify(rank, serviceId, type, size, nbPartitions, true) ;
58      else createServiceNotify(rank, serviceId, type, size, nbPartitions, false) ;
59    }
60  }
61
62 
63  void CPoolRessource::createServiceNotify(int rank, const std::string& serviceId, int type, int size, int nbPartitions, 
64                                           bool in)
65  {
66    winNotify_->lockWindow(rank,0) ;
67    winNotify_->updateFromWindow(rank, this, &CPoolRessource::createServiceDumpIn) ;
68    notifications_.push_back(std::make_tuple(serviceId,type,size,nbPartitions,in)) ;
69    winNotify_->updateToWindow(rank, this, &CPoolRessource::createServiceDumpOut) ; 
70    winNotify_->unlockWindow(rank,0) ;   
71  }
72
73
74  void CPoolRessource::createServiceDumpOut(CBufferOut& buffer)
75  {
76    buffer.realloc(maxBufferSize_) ;
77   
78    buffer << (int) (notifications_.size());
79   
80    for(auto it=notifications_.begin();it!=notifications_.end(); ++it) 
81      buffer << std::get<0>(*it) << static_cast<int>(std::get<1>(*it))<< std::get<2>(*it)<< std::get<3>(*it) << std::get<4>(*it)  ;
82  }
83
84
85  void CPoolRessource::createServiceDumpIn(CBufferIn& buffer)
86  {
87    std::string serviceId ;
88    int type ;
89    int size; 
90    int nbPartitions; 
91    bool in ;
92
93    notifications_.clear() ;
94    int nbNotifications ;
95    buffer>>nbNotifications ;
96    for(int i=0;i<nbNotifications;i++) 
97    {
98      buffer>>serviceId>>type>>size>>nbPartitions>>in ;
99      notifications_.push_back(std::make_tuple(serviceId,type,size,nbPartitions,in)) ;
100    }
101  }
102
103  bool CPoolRessource::eventLoop(bool serviceOnly)
104  {
105    CTimer::get("CPoolRessource::eventLoop").resume();
106   
107    double time=MPI_Wtime() ;
108    if (time-lastEventLoop_ > eventLoopLatency_) 
109    {
110      checkCreateServiceNotification() ;
111      lastEventLoop_=time ;
112    }
113   
114    for (auto it=services_.begin(); it!=services_.end() ; ++it) 
115    {
116      if (it->second->eventLoop(serviceOnly))
117      {
118        services_.erase(it) ;
119        // don't forget to free service later
120        break ;
121      }
122    }
123    CTimer::get("CPoolRessource::eventLoop").suspend();
124    if (services_.empty() && finalizeSignal_) return true ;
125    else return false ;
126  }
127
128  void CPoolRessource::checkCreateServiceNotification(void)
129  {
130    int commRank ;
131    MPI_Comm_rank(poolComm_, &commRank) ;
132    winNotify_->lockWindow(commRank,0) ;
133    winNotify_->updateFromWindow(commRank, this, &CPoolRessource::createServiceDumpIn) ;
134   
135    if (!notifications_.empty())
136    {
137      auto info = notifications_.front() ;
138      createNewService(get<0>(info), get<1>(info), get<2>(info), get<3>(info), get<4>(info)) ;
139      notifications_.pop_front() ;
140      winNotify_->updateToWindow(commRank, this, &CPoolRessource::createServiceDumpOut) ;     
141    }
142    winNotify_->unlockWindow(commRank,0) ;
143
144  }
145
146  void CPoolRessource::createNewService(const std::string& serviceId, int type, int size, int nbPartitions, bool in)
147  {
148     
149     info(40)<<"CPoolRessource::createNewService  : receive createService notification ; serviceId : "<<serviceId<<endl ;
150     MPI_Comm serviceComm, newServiceComm ;
151     int commRank ;
152     MPI_Comm_rank(poolComm_,&commRank) ;
153     MPI_Comm_split(poolComm_, in, commRank, &serviceComm) ;
154     if (in)
155     {
156       int serviceCommSize ;
157       int serviceCommRank ;
158       MPI_Comm_size(serviceComm,&serviceCommSize) ;
159       MPI_Comm_rank(serviceComm,&serviceCommRank) ;
160
161       info(10)<<"Service  "<<serviceId<<" created "<<"  service size : "<<serviceCommSize<< "   service rank : "<<serviceCommRank
162                            <<" on rank pool "<<commRank<<endl ;
163       
164       int partitionId ; 
165       if ( serviceCommRank >= (serviceCommSize/nbPartitions+1)*(serviceCommSize%nbPartitions) )
166       {
167         int rank =  serviceCommRank - (serviceCommSize/nbPartitions+1)*(serviceCommSize%nbPartitions) ;
168         partitionId = serviceCommSize%nbPartitions +  rank / (serviceCommSize/nbPartitions) ;
169       }
170       else  partitionId = serviceCommRank / (serviceCommSize/nbPartitions + 1) ;
171
172       MPI_Comm_split(serviceComm, partitionId, commRank, &newServiceComm) ;
173       
174       MPI_Comm_size(newServiceComm,&serviceCommSize) ;
175       MPI_Comm_rank(newServiceComm,&serviceCommRank) ;
176       info(10)<<"Service  "<<serviceId<<" created "<<"  partition : " <<partitionId<<" service size : "<<serviceCommSize
177               << " service rank : "<<serviceCommRank <<" on rank pool "<<commRank<<endl ;
178     
179       services_[std::make_tuple(serviceId,partitionId)] = new CService(newServiceComm, Id_, serviceId, partitionId, type, nbPartitions) ;
180       
181       MPI_Comm_free(&newServiceComm) ;
182     }
183     MPI_Comm_free(&serviceComm) ;
184  }
185
186  void CPoolRessource::createService(MPI_Comm serviceComm, const std::string& serviceId, int partitionId, int type, int nbPartitions) // for clients & attached
187  {
188    services_[std::make_tuple(serviceId,partitionId)] = new CService(serviceComm, Id_, serviceId, partitionId, type, nbPartitions) ;
189  }
190
191
192  void CPoolRessource::finalizeSignal(void)
193  {
194    finalizeSignal_=true ;
195    for (auto it=services_.begin(); it!=services_.end() ; ++it) it->second->finalizeSignal() ;
196  }
197
198}
Note: See TracBrowser for help on using the repository browser.