Salome HOME
Update copyrights
[modules/yacs.git] / src / runtime / SalomeHPContainer.cxx
1 // Copyright (C) 2006-2019  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #include "SalomeHPContainer.hxx"
21 #include "SalomeHPComponent.hxx"
22 #include "SalomeContainerTmpForHP.hxx"
23 #include "AutoLocker.hxx"
24 #include "AutoRefCnt.hxx"
25
26 #include <algorithm>
27
28 using namespace YACS::ENGINE;
29
30 const char SalomeHPContainer::KIND[]="HPSalome";
31
32 SalomeHPContainerBase::SalomeHPContainerBase(SalomeHPContainerVectOfHelper *resShared):_launchModeType(resShared),_shutdownLevel(999)
33 {
34 }
35
36 SalomeHPContainerBase::SalomeHPContainerBase(const SalomeHPContainerBase& other):_shutdownLevel(999),_launchModeType(new SalomeHPContainerVectOfHelper),_initScript(other._initScript)
37 {
38 }
39
40 SalomeHPContainer *SalomeHPContainerBase::getTheBoss()
41 {
42   HomogeneousPoolContainer *ret(this);
43   while(ret->getDirectFather())
44     ret=ret->getDirectFather();
45   SalomeHPContainer *retC(dynamic_cast<SalomeHPContainer *>(ret));
46   if(!retC)
47     throw Exception("SalomeHPContainerBase::getTheBoss : unexpected type of object !");
48   return retC;
49 }
50
51 const SalomeHPContainer *SalomeHPContainerBase::getTheBoss() const
52 {
53   const HomogeneousPoolContainer *ret(this);
54   while(ret->getDirectFather())
55     ret=ret->getDirectFather();
56   const SalomeHPContainer *retC(dynamic_cast<const SalomeHPContainer *>(ret));
57   if(!retC)
58     throw Exception("SalomeHPContainerBase::getTheBoss : unexpected type of object !");
59   return retC;
60 }
61
62 void SalomeHPContainerBase::startInternal(const Task *askingNode, SalomeContainerToolsBase& sct, const std::vector<std::string>& compoNames)
63 {
64   SalomeContainerMonoHelper *helper(_launchModeType->getHelperOfTaskThreadSafe(askingNode));
65   SalomeContainerTools::Start(compoNames,helper,sct,_shutdownLevel,this,askingNode);
66 }
67
68 void SalomeHPContainerBase::shutdown(int level)
69 {
70   if(level < _shutdownLevel)
71       return;
72   _shutdownLevel=999;
73   _launchModeType->shutdown();
74 }
75
76 SalomeHPContainerBase::SalomeHPContainerBase(SalomeHPContainerVectOfHelper *resShared, bool isRefEaten):_launchModeType(resShared)
77 {
78   if(!isRefEaten)
79     if(_launchModeType.isNotNull())
80       _launchModeType->incrRef();
81 }
82
83 bool SalomeHPContainerBase::isAlreadyStarted(const Task *askingNode) const
84 {
85   const SalomeContainerMonoHelper *helper(_launchModeType->getHelperOfTaskThreadSafe(askingNode));
86   return helper->isAlreadyStarted(askingNode);
87 }
88
89 void SalomeHPContainerBase::release(const Task *node)
90 {
91   _launchModeType->release(node);
92 }
93
94 void SalomeHPContainerBase::lock()
95 {
96   _launchModeType->lock();
97 }
98
99 void SalomeHPContainerBase::unLock()
100 {
101   _launchModeType->unLock();
102 }
103
104 void SalomeHPContainerBase::setSizeOfPool(int sz)
105 {
106   _launchModeType->resize(sz);
107 }
108
109 int SalomeHPContainerBase::getSizeOfPool() const
110 {
111   return _launchModeType->size();
112 }
113
114 void SalomeHPContainerBase::setProperty(const std::string& name,const std::string& value)
115 {
116   if(name==AOC_ENTRY)//no sense to set it ! It is always true ! ignore it !
117     return ;
118   else if(name==SIZE_OF_POOL_KEY)
119     {
120       std::istringstream iss(value);
121       int val(0);
122       iss >> val;
123       setSizeOfPool(val);
124     }
125   else if(name==INITIALIZE_SCRIPT_KEY)
126     {
127       _initScript=value;
128     }
129   else
130     getTheBoss()->getContainerInfo().setProperty(name,value);
131 }
132
133 std::string SalomeHPContainerBase::getProperty(const std::string& name) const
134 {
135   if(name==AOC_ENTRY)
136     {
137       return std::string("1");
138     }
139   else if(name==SIZE_OF_POOL_KEY)
140     {
141       std::ostringstream oss; oss << getSizeOfPool();
142       return oss.str();
143     }
144   else if(name==INITIALIZE_SCRIPT_KEY)
145     {
146       return _initScript;
147     }
148   else
149     return getTheBoss()->getContainerInfo().getProperty(name);
150 }
151
152 std::map<std::string,std::string> SalomeHPContainerBase::getProperties() const
153 {
154   std::map<std::string,std::string> ret(getTheBoss()->getContainerInfo().getProperties());
155   std::ostringstream oss; oss << getSizeOfPool();
156   ret[SIZE_OF_POOL_KEY]=oss.str();
157   if(!_initScript.empty())
158     ret[INITIALIZE_SCRIPT_KEY]=_initScript;
159   return ret;
160 }
161
162 void SalomeHPContainerBase::clearProperties()
163 {
164   _initScript.clear();
165   getTheBoss()->getContainerInfo().clearProperties();
166 }
167
168 std::string SalomeHPContainerBase::getPlacementId(const Task *askingNode) const
169 {
170   const SalomeContainerMonoHelper *helper(0);
171   {
172     YACS::BASES::AutoLocker<Container> alckCont(const_cast<SalomeHPContainerBase *>(this));
173     helper=_launchModeType->getHelperOfTask(askingNode);
174   }
175   return SalomeContainerTools::GetPlacementId(helper,this,askingNode);
176 }
177
178 std::string SalomeHPContainerBase::getFullPlacementId(const Task *askingNode) const
179 {
180   const SalomeContainerMonoHelper *helper(0);
181   {
182     YACS::BASES::AutoLocker<Container> alckCont(const_cast<SalomeHPContainerBase *>(this));
183     helper=_launchModeType->getHelperOfTask(askingNode);
184   }
185   return SalomeContainerTools::GetFullPlacementId(helper,this,askingNode);
186 }
187
188 std::map<std::string,std::string> SalomeHPContainerBase::getResourceProperties(const std::string& name) const
189 {
190   return getTheBoss()->getResourceProperties(name);
191 }
192
193 void SalomeHPContainerBase::addComponentName(const std::string& name)
194 {
195   getTheBoss()->addComponentNameSpe(name);
196 }
197
198 void SalomeHPContainerBase::checkCapabilityToDealWith(const ComponentInstance *inst) const throw(YACS::Exception)
199 {
200   getTheBoss()->checkCapabilityToDealWith(inst);
201 }
202
203 YACS::BASES::AutoRefCnt<HomogeneousPoolContainer> SalomeHPContainerBase::decorate(YACS::BASES::AutoConstRefCnt<PartDefinition> pd)
204 {
205   YACS::BASES::AutoRefCnt<HomogeneousPoolContainer> ret(new SalomeHPContainerShared(pd,_launchModeType,this));
206   return ret;
207 }
208
209 Engines::Container_var SalomeHPContainerBase::getContainerPtr(const Task *askingNode) const
210 {
211   const SalomeContainerMonoHelper *helper(0);
212   {
213     YACS::BASES::AutoLocker<SalomeHPContainerBase> alck(const_cast<SalomeHPContainerBase *>(this));
214     helper=_launchModeType->getHelperOfTask(askingNode);
215   }
216   return helper->getContainer(NULL);
217 }
218
219 std::vector<std::string> SalomeHPContainerBase::getKernelContainerNames() const
220 {
221   return _launchModeType->getKernelContainerNames();
222 }
223
224 ////////////////
225
226 SalomeHPContainer::SalomeHPContainer():SalomeHPContainerBase(new SalomeHPContainerVectOfHelper)
227 {
228 }
229
230 SalomeHPContainer::SalomeHPContainer(const SalomeHPContainer& other):SalomeHPContainerBase(other),_sct(other._sct),_componentNames(other._componentNames)
231 {
232 }
233
234 std::size_t SalomeHPContainer::getNumberOfFreePlace() const
235 {
236   return _launchModeType->getNumberOfFreePlace();
237 }
238
239 void SalomeHPContainer::allocateFor(const std::vector<const Task *>& nodes)
240 {
241   _launchModeType->allocateFor(nodes);
242 }
243
244 SalomeHPContainer::~SalomeHPContainer()
245 {
246 }
247
248 std::string SalomeHPContainer::getKind() const
249 {
250   return KIND;
251 }
252
253 std::string SalomeHPContainer::getDiscreminantStrOfThis(const Task *askingNode) const
254 {
255   YACS::BASES::AutoCppPtr<SalomeContainerTmpForHP> tmpCont(SalomeContainerTmpForHP::BuildFrom(this,askingNode));
256   return tmpCont->getDiscreminantStrOfThis(askingNode);
257 }
258
259 void SalomeHPContainer::start(const Task *askingNode) throw(YACS::Exception)
260 {
261   startInternal(askingNode,_sct,_componentNames);
262 }
263
264 /*!
265  * It is not a bug here ! clone for homogeneous container is not supposed to be copied !
266  */
267 Container *SalomeHPContainer::clone() const
268 {
269   incrRef();
270   return const_cast<SalomeHPContainer*>(this);
271 }
272
273 Container *SalomeHPContainer::cloneAlways() const
274 {
275   return new SalomeHPContainer(*this);
276 }
277
278 int SalomeHPContainer::getNumberOfCoresPerWorker() const
279 {
280   return _sct.getNumberOfCoresPerWorker();
281 }
282
283 std::map<std::string,std::string> SalomeHPContainer::getResourcePropertiesSpe(const std::string& name) const
284 {
285   return _sct.getResourceProperties(name);
286 }
287
288 void SalomeHPContainer::addComponentNameSpe(const std::string& name)
289 {
290   _componentNames.push_back(name);
291 }
292
293 void SalomeHPContainer::checkCapabilityToDealWithSpe(const ComponentInstance *inst) const throw(YACS::Exception)
294 {
295   if(inst->getKind()!=SalomeHPComponent::KIND)
296     throw Exception("SalomeHPContainer::checkCapabilityToDealWithSpe : SalomeContainer is not able to deal with this type of ComponentInstance.");
297 }
298
299 void SalomeHPContainer::forYourTestsOnly(ForTestOmlyHPContCls *data) const
300 {
301   data->setContainerType("HPContainer");
302 }
303
304 //////////////////////////////////
305
306 SalomeHPContainerShared::SalomeHPContainerShared(YACS::BASES::AutoConstRefCnt<PartDefinition> pd, SalomeHPContainerVectOfHelper *resShared, SalomeHPContainerBase *directFather):SalomeHPContainerBase(resShared,false),_pd(pd)
307 {
308   if(!directFather)
309     throw Exception("SalomeHPContainerShared : NULL pointer not allowed !");
310   _directFather.takeRef(directFather);
311 }
312
313 std::string SalomeHPContainerShared::getKind() const
314 {
315   return SalomeHPContainer::KIND;
316 }
317
318 void SalomeHPContainerShared::prepareMaskForExecution() const
319 {
320   _idsOfKernelContainers=_pd->computeWorkerIdsCovered(getNumberOfCoresPerWorker());
321 }
322
323 /*!
324  * It is not a bug here ! clone for homogeneous container is not supposed to be copied !
325  */
326 Container *SalomeHPContainerShared::clone() const
327 {
328   incrRef();
329   return const_cast<SalomeHPContainerShared*>(this);
330 }
331
332 Container *SalomeHPContainerShared::cloneAlways() const
333 {
334   throw Exception("SalomeHPContainerShared::cloneAlways : you are not supposed to be in this situation ! This type of container has only existence during execution !");
335 }
336
337 std::string SalomeHPContainerShared::getName() const
338 {
339   return getTheBoss()->getName();
340 }
341
342 std::string SalomeHPContainerShared::getDiscreminantStrOfThis(const Task *askingNode) const
343 {
344   return getTheBoss()->getDiscreminantStrOfThis(askingNode);
345 }
346
347 void SalomeHPContainerShared::start(const Task *askingNode) throw(YACS::Exception)
348 {
349   SalomeContainerToolsSpreadOverTheResDecorator sct(&getTheBoss()->getContainerInfo(),_pd->getPlayGround(),_launchModeType,askingNode);
350   startInternal(askingNode,sct,getTheBoss()->getComponentNames());
351 }
352
353 void SalomeHPContainerShared::allocateFor(const std::vector<const Task *>& nodes)
354 {
355   _launchModeType->allocateForAmong(_idsOfKernelContainers,nodes);
356 }
357
358 std::size_t SalomeHPContainerShared::getNumberOfFreePlace() const
359 {
360   return _launchModeType->getNumberOfFreePlaceAmong(_idsOfKernelContainers);
361 }
362
363 void SalomeHPContainerShared::forYourTestsOnly(ForTestOmlyHPContCls *data) const
364 {
365   data->setContainerType("HPContainerShared");
366   data->setPD(_pd);
367   data->setIDS(_idsOfKernelContainers);
368 }
369
370 /*
371  * SalomeHPContainerVectOfHelper is an holder of vector of SalomeContainerMonoHelper (holding itself a Kernel Container)
372  * SalomeContainerTools is a Engines::ContainerParameters holder. It is the data keeper for GiveContainer invokation.
373  * 
374  */