]> SALOME platform Git repositories - modules/yacs.git/blob - src/runtime/SalomeHPContainer.cxx
Salome HOME
e057a5f1ef556b212460fee6ba6bf82b037442c4
[modules/yacs.git] / src / runtime / SalomeHPContainer.cxx
1 // Copyright (C) 2006-2016  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 ////////////////
220
221 SalomeHPContainer::SalomeHPContainer():SalomeHPContainerBase(new SalomeHPContainerVectOfHelper)
222 {
223 }
224
225 SalomeHPContainer::SalomeHPContainer(const SalomeHPContainer& other):SalomeHPContainerBase(other),_sct(other._sct),_componentNames(other._componentNames)
226 {
227 }
228
229 std::size_t SalomeHPContainer::getNumberOfFreePlace() const
230 {
231   return _launchModeType->getNumberOfFreePlace();
232 }
233
234 void SalomeHPContainer::allocateFor(const std::vector<const Task *>& nodes)
235 {
236   _launchModeType->allocateFor(nodes);
237 }
238
239 SalomeHPContainer::~SalomeHPContainer()
240 {
241 }
242
243 std::string SalomeHPContainer::getKind() const
244 {
245   return KIND;
246 }
247
248 std::string SalomeHPContainer::getDiscreminantStrOfThis(const Task *askingNode) const
249 {
250   YACS::BASES::AutoCppPtr<SalomeContainerTmpForHP> tmpCont(SalomeContainerTmpForHP::BuildFrom(this,askingNode));
251   return tmpCont->getDiscreminantStrOfThis(askingNode);
252 }
253
254 void SalomeHPContainer::start(const Task *askingNode) throw(Exception)
255 {
256   startInternal(askingNode,_sct,_componentNames);
257 }
258
259 /*!
260  * It is not a bug here ! clone for homogeneous container is not supposed to be copied !
261  */
262 Container *SalomeHPContainer::clone() const
263 {
264   incrRef();
265   return const_cast<SalomeHPContainer*>(this);
266 }
267
268 Container *SalomeHPContainer::cloneAlways() const
269 {
270   return new SalomeHPContainer(*this);
271 }
272
273 int SalomeHPContainer::getNumberOfCoresPerWorker() const
274 {
275   return _sct.getNumberOfCoresPerWorker();
276 }
277
278 std::map<std::string,std::string> SalomeHPContainer::getResourcePropertiesSpe(const std::string& name) const
279 {
280   return _sct.getResourceProperties(name);
281 }
282
283 void SalomeHPContainer::addComponentNameSpe(const std::string& name)
284 {
285   _componentNames.push_back(name);
286 }
287
288 void SalomeHPContainer::checkCapabilityToDealWithSpe(const ComponentInstance *inst) const throw(YACS::Exception)
289 {
290   if(inst->getKind()!=SalomeHPComponent::KIND)
291     throw Exception("SalomeHPContainer::checkCapabilityToDealWithSpe : SalomeContainer is not able to deal with this type of ComponentInstance.");
292 }
293
294 void SalomeHPContainer::forYourTestsOnly(ForTestOmlyHPContCls *data) const
295 {
296   data->setContainerType("HPContainer");
297 }
298
299 //////////////////////////////////
300
301 SalomeHPContainerShared::SalomeHPContainerShared(YACS::BASES::AutoConstRefCnt<PartDefinition> pd, SalomeHPContainerVectOfHelper *resShared, SalomeHPContainerBase *directFather):SalomeHPContainerBase(resShared,false),_pd(pd)
302 {
303   if(!directFather)
304     throw Exception("SalomeHPContainerShared : NULL pointer not allowed !");
305   _directFather.takeRef(directFather);
306 }
307
308 std::string SalomeHPContainerShared::getKind() const
309 {
310   return SalomeHPContainer::KIND;
311 }
312
313 void SalomeHPContainerShared::prepareMaskForExecution() const
314 {
315   _idsOfKernelContainers=_pd->computeWorkerIdsCovered(getNumberOfCoresPerWorker());
316 }
317
318 /*!
319  * It is not a bug here ! clone for homogeneous container is not supposed to be copied !
320  */
321 Container *SalomeHPContainerShared::clone() const
322 {
323   incrRef();
324   return const_cast<SalomeHPContainerShared*>(this);
325 }
326
327 Container *SalomeHPContainerShared::cloneAlways() const
328 {
329   throw Exception("SalomeHPContainerShared::cloneAlways : you are not supposed to be in this situation ! This type of container has only existence during execution !");
330 }
331
332 void SalomeHPContainerShared::start(const Task *askingNode) throw(Exception)
333 {
334   SalomeContainerToolsSpreadOverTheResDecorator sct(&getTheBoss()->getContainerInfo(),_pd->getPlayGround(),_launchModeType,askingNode);
335   startInternal(askingNode,sct,getTheBoss()->getComponentNames());
336 }
337
338 void SalomeHPContainerShared::allocateFor(const std::vector<const Task *>& nodes)
339 {
340   _launchModeType->allocateForAmong(_idsOfKernelContainers,nodes);
341 }
342
343 std::size_t SalomeHPContainerShared::getNumberOfFreePlace() const
344 {
345   return _launchModeType->getNumberOfFreePlaceAmong(_idsOfKernelContainers);
346 }
347
348 void SalomeHPContainerShared::forYourTestsOnly(ForTestOmlyHPContCls *data) const
349 {
350   data->setContainerType("HPContainerShared");
351   data->setPD(_pd);
352   data->setIDS(_idsOfKernelContainers);
353 }
354
355 /*
356  * SalomeHPContainerVectOfHelper is an holder of vector of SalomeContainerMonoHelper (holding itself a Kernel Container)
357  * SalomeContainerTools is a Engines::ContainerParameters holder. It is the data keeper for GiveContainer invokation.
358  * 
359  */