1 | #include "servers_ressource.hpp" |
---|
2 | #include "window_manager.hpp" |
---|
3 | #include "ressources_manager.hpp" |
---|
4 | #include "pool_ressource.hpp" |
---|
5 | #include "cxios.hpp" |
---|
6 | #include "mpi.hpp" |
---|
7 | #include "timer.hpp" |
---|
8 | #include <vector> |
---|
9 | #include <string> |
---|
10 | |
---|
11 | |
---|
12 | |
---|
13 | |
---|
14 | namespace xios |
---|
15 | { |
---|
16 | using namespace std ; |
---|
17 | |
---|
18 | CServersRessource::CServersRessource(MPI_Comm serverComm) : poolRessource_(nullptr), finalizeSignal_(false) |
---|
19 | { |
---|
20 | |
---|
21 | MPI_Comm_dup(serverComm, &serverComm_) ; |
---|
22 | MPI_Comm xiosComm=CXios::getXiosComm() ; |
---|
23 | |
---|
24 | int localRank, globalRank ; |
---|
25 | MPI_Comm_rank(xiosComm,&globalRank) ; |
---|
26 | MPI_Comm_rank(serverComm_,&localRank) ; |
---|
27 | |
---|
28 | winNotify_ = new CWindowManager(serverComm_, maxBufferSize_) ; |
---|
29 | MPI_Barrier(serverComm_) ; |
---|
30 | if (localRank==localLeader_) |
---|
31 | { |
---|
32 | int commSize ; |
---|
33 | MPI_Comm_size(serverComm_,&commSize) ; |
---|
34 | CXios::getRessourcesManager()->registerServerLeader(globalRank) ; |
---|
35 | CXios::getRessourcesManager()->registerRessourcesSize(commSize) ; |
---|
36 | freeRessourcesRank_.resize(commSize) ; |
---|
37 | for(int i=0;i<commSize;i++) freeRessourcesRank_[i]=i ; |
---|
38 | } |
---|
39 | |
---|
40 | MPI_Comm_dup(serverComm_, &freeRessourcesComm_) ; |
---|
41 | |
---|
42 | } |
---|
43 | |
---|
44 | void CServersRessource::createPool(const string& poolId, const int size) |
---|
45 | { |
---|
46 | int commSize ; |
---|
47 | MPI_Comm_size(serverComm_,&commSize) ; |
---|
48 | vector<int> newFreeRessourcesRank(freeRessourcesRank_.size()-size) ; |
---|
49 | |
---|
50 | bool isPartOf ; |
---|
51 | |
---|
52 | for(int i=0, j=0; i<freeRessourcesRank_.size();i++) |
---|
53 | { |
---|
54 | if (i<size) isPartOf=true ; |
---|
55 | else |
---|
56 | { |
---|
57 | isPartOf=false ; |
---|
58 | newFreeRessourcesRank[j]=freeRessourcesRank_[i] ; |
---|
59 | j++ ; |
---|
60 | } |
---|
61 | |
---|
62 | notifyOutType_=NOTIFY_CREATE_POOL ; |
---|
63 | notifyOutCreatePool_ = make_tuple(poolId, isPartOf) ; |
---|
64 | sendNotification(freeRessourcesRank_[i]) ; |
---|
65 | } |
---|
66 | freeRessourcesRank_ = std::move(newFreeRessourcesRank) ; |
---|
67 | } |
---|
68 | |
---|
69 | void CServersRessource::finalize(void) |
---|
70 | { |
---|
71 | int commSize ; |
---|
72 | MPI_Comm_size(serverComm_,&commSize) ; |
---|
73 | |
---|
74 | for(int rank=0; rank<commSize;rank++) |
---|
75 | { |
---|
76 | notifyOutType_=NOTIFY_FINALIZE ; |
---|
77 | sendNotification(rank) ; |
---|
78 | } |
---|
79 | } |
---|
80 | |
---|
81 | void CServersRessource::sendNotification(int rank) |
---|
82 | { |
---|
83 | winNotify_->lockWindow(rank,0) ; |
---|
84 | winNotify_->pushToWindow(rank, this, &CServersRessource::notificationsDumpOut) ; |
---|
85 | winNotify_->unlockWindow(rank,0) ; |
---|
86 | } |
---|
87 | |
---|
88 | |
---|
89 | void CServersRessource::notificationsDumpOut(CBufferOut& buffer) |
---|
90 | { |
---|
91 | |
---|
92 | buffer.realloc(maxBufferSize_) ; |
---|
93 | |
---|
94 | if (notifyOutType_==NOTIFY_CREATE_POOL) |
---|
95 | { |
---|
96 | auto& arg=notifyOutCreatePool_ ; |
---|
97 | buffer << notifyOutType_ << std::get<0>(arg) << std::get<1>(arg) ; |
---|
98 | } |
---|
99 | else if (notifyOutType_==NOTIFY_FINALIZE) buffer << notifyOutType_ ; |
---|
100 | } |
---|
101 | |
---|
102 | void CServersRessource::notificationsDumpIn(CBufferIn& buffer) |
---|
103 | { |
---|
104 | if (buffer.bufferSize() == 0) notifyInType_= NOTIFY_NOTHING ; |
---|
105 | else |
---|
106 | { |
---|
107 | buffer>>notifyInType_; |
---|
108 | if (notifyInType_==NOTIFY_CREATE_POOL) |
---|
109 | { |
---|
110 | auto& arg=notifyInCreatePool_ ; |
---|
111 | buffer >> std::get<0>(arg) >> std::get<1>(arg) ; |
---|
112 | } |
---|
113 | else if (notifyInType_==NOTIFY_FINALIZE) { /*nothing to do*/} |
---|
114 | } |
---|
115 | } |
---|
116 | |
---|
117 | bool CServersRessource::eventLoop(bool serviceOnly) |
---|
118 | { |
---|
119 | CTimer::get("CServersRessource::eventLoop").resume(); |
---|
120 | double time=MPI_Wtime() ; |
---|
121 | int flag ; |
---|
122 | MPI_Iprobe(MPI_ANY_SOURCE, MPI_ANY_TAG, MPI_COMM_WORLD, &flag, MPI_STATUS_IGNORE); |
---|
123 | |
---|
124 | if (time-lastEventLoop_ > eventLoopLatency_) |
---|
125 | { |
---|
126 | checkNotifications() ; |
---|
127 | lastEventLoop_=time ; |
---|
128 | } |
---|
129 | |
---|
130 | if (poolRessource_!=nullptr) |
---|
131 | { |
---|
132 | if (poolRessource_->eventLoop(serviceOnly)) |
---|
133 | { |
---|
134 | delete poolRessource_ ; |
---|
135 | poolRessource_=nullptr ; |
---|
136 | // don't forget to free pool ressource later |
---|
137 | } |
---|
138 | } |
---|
139 | CTimer::get("CServersRessource::eventLoop").suspend(); |
---|
140 | if (poolRessource_==nullptr && finalizeSignal_) return true ; |
---|
141 | else return false ; |
---|
142 | } |
---|
143 | |
---|
144 | void CServersRessource::checkNotifications(void) |
---|
145 | { |
---|
146 | int commRank ; |
---|
147 | MPI_Comm_rank(serverComm_, &commRank) ; |
---|
148 | winNotify_->lockWindow(commRank,0) ; |
---|
149 | winNotify_->popFromWindow(commRank, this, &CServersRessource::notificationsDumpIn) ; |
---|
150 | winNotify_->unlockWindow(commRank,0) ; |
---|
151 | if (notifyInType_==NOTIFY_CREATE_POOL) createPool() ; |
---|
152 | else if (notifyInType_==NOTIFY_FINALIZE) finalizeSignal() ; |
---|
153 | } |
---|
154 | |
---|
155 | void CServersRessource::createPool(void) |
---|
156 | { |
---|
157 | auto& arg=notifyInCreatePool_ ; |
---|
158 | string poolId=get<0>(arg) ; |
---|
159 | bool isPartOf=get<1>(arg) ; |
---|
160 | |
---|
161 | int commRank ; |
---|
162 | MPI_Comm poolComm ; |
---|
163 | MPI_Comm_rank(freeRessourcesComm_,&commRank) ; |
---|
164 | MPI_Comm_split(freeRessourcesComm_, isPartOf, commRank, &poolComm) ; |
---|
165 | |
---|
166 | if (isPartOf) |
---|
167 | { |
---|
168 | poolRessource_ = new CPoolRessource(poolComm, poolId, true) ; |
---|
169 | MPI_Comm_free(&poolComm) ; |
---|
170 | } |
---|
171 | else |
---|
172 | { |
---|
173 | MPI_Comm_free(&freeRessourcesComm_) ; |
---|
174 | freeRessourcesComm_=poolComm ; |
---|
175 | } |
---|
176 | |
---|
177 | } |
---|
178 | |
---|
179 | void CServersRessource::finalizeSignal(void) |
---|
180 | { |
---|
181 | finalizeSignal_=true ; |
---|
182 | if (poolRessource_!=nullptr) poolRessource_->finalizeSignal() ; |
---|
183 | } |
---|
184 | |
---|
185 | bool CServersRessource::isServerLeader(void) |
---|
186 | { |
---|
187 | int commRank ; |
---|
188 | MPI_Comm_rank(serverComm_,&commRank) ; |
---|
189 | if (commRank==localLeader_) return true ; |
---|
190 | else return false ; |
---|
191 | } |
---|
192 | |
---|
193 | CServersRessource::~CServersRessource() |
---|
194 | { |
---|
195 | delete winNotify_ ; |
---|
196 | } |
---|
197 | } |
---|