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

Last change on this file since 2208 was 2208, checked in by ymipsl, 3 years ago

Missing synchronisation in pool_ressource service.
YM

  • Property svn:eol-style set to native
  • Property svn:executable set to *
File size: 6.3 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
9namespace xios
10{
11  CPoolRessource::CPoolRessource(MPI_Comm poolComm, const std::string& Id) : Id_(Id), finalizeSignal_(false)
12  {
13    int commRank, commSize ;
14    MPI_Comm_dup(poolComm, &poolComm_) ;
15    winNotify_ = new CWindowManager(poolComm_, maxBufferSize_) ;
16    MPI_Comm_rank(poolComm, &commRank) ;
17    MPI_Comm_size(poolComm, &commSize) ;
18
19
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    for(int rank=0; rank<commSize; rank++)
55    {
56      if (procs_in[rank]) createServiceNotify(rank, serviceId, type, size, nbPartitions, true) ;
57      else createServiceNotify(rank, serviceId, type, size, nbPartitions, false) ;
58    }
59  }
60
61 
62  void CPoolRessource::createServiceNotify(int rank, const std::string& serviceId, int type, int size, int nbPartitions, 
63                                           bool in)
64  {
65    winNotify_->lockWindow(rank,0) ;
66    winNotify_->updateFromWindow(rank, this, &CPoolRessource::createServiceDumpIn) ;
67    notifications_.push_back(std::make_tuple(serviceId,type,size,nbPartitions,in)) ;
68    winNotify_->updateToWindow(rank, this, &CPoolRessource::createServiceDumpOut) ; 
69    winNotify_->unlockWindow(rank,0) ;   
70  }
71
72
73  void CPoolRessource::createServiceDumpOut(CBufferOut& buffer)
74  {
75    buffer.realloc(maxBufferSize_) ;
76   
77    buffer << (int) (notifications_.size());
78   
79    for(auto it=notifications_.begin();it!=notifications_.end(); ++it) 
80      buffer << std::get<0>(*it) << static_cast<int>(std::get<1>(*it))<< std::get<2>(*it)<< std::get<3>(*it) << std::get<4>(*it)  ;
81  }
82
83
84  void CPoolRessource::createServiceDumpIn(CBufferIn& buffer)
85  {
86    std::string serviceId ;
87    int type ;
88    int size; 
89    int nbPartitions; 
90    bool in ;
91
92    notifications_.clear() ;
93    int nbNotifications ;
94    buffer>>nbNotifications ;
95    for(int i=0;i<nbNotifications;i++) 
96    {
97      buffer>>serviceId>>type>>size>>nbPartitions>>in ;
98      notifications_.push_back(std::make_tuple(serviceId,type,size,nbPartitions,in)) ;
99    }
100  }
101
102  bool CPoolRessource::eventLoop(bool serviceOnly)
103  {
104    checkCreateServiceNotification() ;
105    for (auto it=services_.begin(); it!=services_.end() ; ++it) 
106    {
107      if (it->second->eventLoop(serviceOnly))
108      {
109        services_.erase(it) ;
110        // don't forget to free service later
111        break ;
112      }
113    }
114
115    if (services_.empty() && finalizeSignal_) return true ;
116    else return false ;
117  }
118
119  void CPoolRessource::checkCreateServiceNotification(void)
120  {
121    int commRank ;
122    MPI_Comm_rank(poolComm_, &commRank) ;
123    winNotify_->lockWindow(commRank,0) ;
124    winNotify_->updateFromWindow(commRank, this, &CPoolRessource::createServiceDumpIn) ;
125   
126    if (!notifications_.empty())
127    {
128      auto info = notifications_.front() ;
129      createNewService(get<0>(info), get<1>(info), get<2>(info), get<3>(info), get<4>(info)) ;
130      notifications_.pop_front() ;
131      winNotify_->updateToWindow(commRank, this, &CPoolRessource::createServiceDumpOut) ;     
132    }
133    winNotify_->unlockWindow(commRank,0) ;
134
135  }
136
137  void CPoolRessource::createNewService(const std::string& serviceId, int type, int size, int nbPartitions, bool in)
138  {
139     MPI_Comm serviceComm, newServiceComm ;
140     int commRank ;
141     MPI_Comm_rank(poolComm_,&commRank) ;
142     MPI_Comm_split(poolComm_, in, commRank, &serviceComm) ;
143     if (in)
144     {
145       int serviceCommSize ;
146       int serviceCommRank ;
147       MPI_Comm_size(serviceComm,&serviceCommSize) ;
148       MPI_Comm_rank(serviceComm,&serviceCommRank) ;
149
150       info(10)<<"Service  "<<serviceId<<" created "<<"  service size : "<<serviceCommSize<< "   service rank : "<<serviceCommRank
151                            <<" on rank pool "<<commRank<<endl ;
152       
153       int partitionId ; 
154       if ( serviceCommRank >= (serviceCommSize/nbPartitions+1)*(serviceCommSize%nbPartitions) )
155       {
156         int rank =  serviceCommRank - (serviceCommSize/nbPartitions+1)*(serviceCommSize%nbPartitions) ;
157         partitionId = serviceCommSize%nbPartitions +  rank / (serviceCommSize/nbPartitions) ;
158       }
159       else  partitionId = serviceCommRank / (serviceCommSize/nbPartitions + 1) ;
160
161       MPI_Comm_split(serviceComm, partitionId, commRank, &newServiceComm) ;
162       
163       MPI_Comm_size(newServiceComm,&serviceCommSize) ;
164       MPI_Comm_rank(newServiceComm,&serviceCommRank) ;
165       info(10)<<"Service  "<<serviceId<<" created "<<"  partition : " <<partitionId<<" service size : "<<serviceCommSize
166               << " service rank : "<<serviceCommRank <<" on rank pool "<<commRank<<endl ;
167     
168       services_[std::make_tuple(serviceId,partitionId)] = new CService(newServiceComm, Id_, serviceId, partitionId, type, nbPartitions) ;
169       
170       MPI_Comm_free(&newServiceComm) ;
171     }
172     MPI_Comm_free(&serviceComm) ;
173  }
174
175  void CPoolRessource::createService(MPI_Comm serviceComm, const std::string& serviceId, int partitionId, int type, int nbPartitions) // for clients & attached
176  {
177    services_[std::make_tuple(serviceId,partitionId)] = new CService(serviceComm, Id_, serviceId, partitionId, type, nbPartitions) ;
178  }
179
180
181  void CPoolRessource::finalizeSignal(void)
182  {
183    finalizeSignal_=true ;
184    for (auto it=services_.begin(); it!=services_.end() ; ++it) it->second->finalizeSignal() ;
185  }
186
187}
Note: See TracBrowser for help on using the repository browser.