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

Last change on this file since 2260 was 2260, checked in by ymipsl, 2 years ago

Improvment of one sided protocol

  • removed latency
  • solve dead-lock

YM

  • Property svn:eol-style set to native
  • Property svn:executable set to *
File size: 7.0 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    int flag ;
109    MPI_Iprobe(MPI_ANY_SOURCE, MPI_ANY_TAG, MPI_COMM_WORLD, &flag, MPI_STATUS_IGNORE);
110    if (time-lastEventLoop_ > eventLoopLatency_) 
111    {
112      checkCreateServiceNotification() ;
113      lastEventLoop_=time ;
114    }
115   
116    for (auto it=services_.begin(); it!=services_.end() ; ++it) 
117    {
118      if (it->second->eventLoop(serviceOnly))
119      {
120        services_.erase(it) ;
121        // don't forget to free service later
122        break ;
123      }
124    }
125    CTimer::get("CPoolRessource::eventLoop").suspend();
126    if (services_.empty() && finalizeSignal_) return true ;
127    else return false ;
128  }
129
130  void CPoolRessource::checkCreateServiceNotification(void)
131  {
132    int commRank ;
133    MPI_Comm_rank(poolComm_, &commRank) ;
134    winNotify_->lockWindow(commRank,0) ;
135    winNotify_->updateFromWindow(commRank, this, &CPoolRessource::createServiceDumpIn) ;
136   
137    if (!notifications_.empty())
138    {
139      auto info = notifications_.front() ;
140      createNewService(get<0>(info), get<1>(info), get<2>(info), get<3>(info), get<4>(info)) ;
141      notifications_.pop_front() ;
142      winNotify_->updateToWindow(commRank, this, &CPoolRessource::createServiceDumpOut) ;     
143    }
144    winNotify_->unlockWindow(commRank,0) ;
145
146  }
147
148  void CPoolRessource::createNewService(const std::string& serviceId, int type, int size, int nbPartitions, bool in)
149  {
150     
151     info(40)<<"CPoolRessource::createNewService  : receive createService notification ; serviceId : "<<serviceId<<endl ;
152     MPI_Comm serviceComm, newServiceComm ;
153     int commRank ;
154     MPI_Comm_rank(poolComm_,&commRank) ;
155     MPI_Comm_split(poolComm_, in, commRank, &serviceComm) ;
156     if (in)
157     {
158       int serviceCommSize ;
159       int serviceCommRank ;
160       MPI_Comm_size(serviceComm,&serviceCommSize) ;
161       MPI_Comm_rank(serviceComm,&serviceCommRank) ;
162
163       info(10)<<"Service  "<<serviceId<<" created "<<"  service size : "<<serviceCommSize<< "   service rank : "<<serviceCommRank
164                            <<" on rank pool "<<commRank<<endl ;
165       
166       int partitionId ; 
167       if ( serviceCommRank >= (serviceCommSize/nbPartitions+1)*(serviceCommSize%nbPartitions) )
168       {
169         int rank =  serviceCommRank - (serviceCommSize/nbPartitions+1)*(serviceCommSize%nbPartitions) ;
170         partitionId = serviceCommSize%nbPartitions +  rank / (serviceCommSize/nbPartitions) ;
171       }
172       else  partitionId = serviceCommRank / (serviceCommSize/nbPartitions + 1) ;
173
174       MPI_Comm_split(serviceComm, partitionId, commRank, &newServiceComm) ;
175       
176       MPI_Comm_size(newServiceComm,&serviceCommSize) ;
177       MPI_Comm_rank(newServiceComm,&serviceCommRank) ;
178       info(10)<<"Service  "<<serviceId<<" created "<<"  partition : " <<partitionId<<" service size : "<<serviceCommSize
179               << " service rank : "<<serviceCommRank <<" on rank pool "<<commRank<<endl ;
180     
181       services_[std::make_tuple(serviceId,partitionId)] = new CService(newServiceComm, Id_, serviceId, partitionId, type, nbPartitions) ;
182       
183       MPI_Comm_free(&newServiceComm) ;
184     }
185     MPI_Comm_free(&serviceComm) ;
186  }
187
188  void CPoolRessource::createService(MPI_Comm serviceComm, const std::string& serviceId, int partitionId, int type, int nbPartitions) // for clients & attached
189  {
190    services_[std::make_tuple(serviceId,partitionId)] = new CService(serviceComm, Id_, serviceId, partitionId, type, nbPartitions) ;
191  }
192
193
194  void CPoolRessource::finalizeSignal(void)
195  {
196    finalizeSignal_=true ;
197    for (auto it=services_.begin(); it!=services_.end() ; ++it) it->second->finalizeSignal() ;
198  }
199
200}
Note: See TracBrowser for help on using the repository browser.