]> SALOME platform Git repositories - modules/yacs.git/blob - src/runtime/SalomeHPContainer.cxx
Salome HOME
SALOMEHPContainer getNumberOfFreePlace and allocateFor
[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
25 #include <algorithm>
26 #include <utility>
27
28 using namespace YACS::ENGINE;
29
30 const char SalomeHPContainer::KIND[]="HPSalome";
31
32 SalomeHPContainer::SalomeHPContainer():_shutdownLevel(999)
33 {
34 }
35
36 SalomeHPContainer::SalomeHPContainer(const SalomeHPContainer& other):_componentNames(other._componentNames),_shutdownLevel(999),_sct(other._sct),_initScript(other._initScript)
37 {
38 }
39
40 void SalomeHPContainer::assignPG(const PlayGround *pg)
41 {
42   HomogeneousPoolContainer::assignPG(pg);
43   int nbOfWorkers(getPG()->getNumberOfWorkers(this->getNumberOfCoresPerWorker()));
44   this->setSizeOfPool(nbOfWorkers);
45 }
46
47 void SalomeHPContainer::setSizeOfPool(int sz)
48 {
49   _launchModeType.resize(sz);
50 }
51
52 int SalomeHPContainer::getSizeOfPool() const
53 {
54   return _launchModeType.size();
55 }
56
57 std::size_t SalomeHPContainer::getNumberOfFreePlace() const
58 {
59   return getPG()->getNumberOfFreePlace(this->getNumberOfCoresPerWorker());
60 }
61
62 class PairVecIterator : public std::iterator<
63                         std::input_iterator_tag,     // iterator_category
64                         std::pair<const Task *,std::size_t>,                    // value_type
65                         long,                        // difference_type
66                         const std::pair<const Task *,std::size_t>*,             // pointer
67                         std::pair<const Task *,std::size_t> > // reference
68 {
69   const std::vector< const Task *> *_v0;
70   const std::vector<std::size_t> *_v1;
71   std::size_t _num;
72 public:
73   explicit PairVecIterator(const std::vector< const Task * > *v0, const std::vector<std::size_t> *v1, const std::size_t num) : _v0(v0),_v1(v1),_num(num) { }
74   PairVecIterator& operator++() { _num++; return *this; }
75   bool operator==(PairVecIterator other) const { return _num == other._num; }
76   bool operator!=(PairVecIterator other) const { return !(*this == other); }
77   reference operator*() const { return std::pair<const Task *,std::size_t>((*_v0)[_num],(*_v1)[_num]); }
78 };
79
80 void SalomeHPContainer::allocateFor(const std::vector<const Task *>& nodes)
81 {
82   std::vector<std::size_t> workerIdsAllocated(getPG()->allocateFor(nodes.size(),this->getNumberOfCoresPerWorker()));
83   std::vector<std::pair<const Task *,std::size_t>> nodesAndIds(PairVecIterator(&nodes,&workerIdsAllocated,0),PairVecIterator(&nodes,&workerIdsAllocated,nodes.size()));
84   _launchModeType.allocateForCrude(nodesAndIds);
85 }
86
87 void SalomeHPContainer::release(const Task *node)
88 {
89   _launchModeType.release(node);
90 }
91
92 SalomeHPContainer::~SalomeHPContainer()
93 {
94 }
95
96 void SalomeHPContainer::lock()
97 {
98   _mutex.lock();
99 }
100
101 void SalomeHPContainer::unLock()
102 {
103   _mutex.unLock();
104 }
105
106 std::string SalomeHPContainer::getKind() const
107 {
108   return KIND;
109 }
110
111 std::string SalomeHPContainer::getDiscreminantStrOfThis(const Task *askingNode) const
112 {
113   YACS::BASES::AutoCppPtr<SalomeContainerTmpForHP> tmpCont(SalomeContainerTmpForHP::BuildFrom(this,askingNode));
114   return tmpCont->getDiscreminantStrOfThis(askingNode);
115 }
116
117 bool SalomeHPContainer::isAlreadyStarted(const Task *askingNode) const
118 {
119   const SalomeContainerMonoHelper *helper(_launchModeType.getHelperOfTaskThreadSafe(this,askingNode));
120   return helper->isAlreadyStarted(askingNode);
121 }
122
123 void SalomeHPContainer::start(const Task *askingNode) throw(YACS::Exception)
124 {
125   SalomeContainerMonoHelper *helper(_launchModeType.getHelperOfTaskThreadSafe(this,askingNode));
126   SalomeContainerTools::Start(_componentNames,helper,_sct,_shutdownLevel,this,askingNode);
127 }
128
129 void SalomeHPContainer::shutdown(int level)
130 {
131   if(level < _shutdownLevel)
132       return;
133   _shutdownLevel=999;
134   for(std::size_t i=0;i<_launchModeType.size();i++)
135     {
136       SalomeContainerMonoHelper *helper(_launchModeType.at(i));
137       helper->shutdown();
138     }
139 }
140
141 std::string SalomeHPContainer::getPlacementId(const Task *askingNode) const
142 {
143   const SalomeContainerMonoHelper *helper(0);
144   {
145     YACS::BASES::AutoLocker<Container> alckCont(const_cast<SalomeHPContainer *>(this));
146     helper=_launchModeType.getHelperOfTask(askingNode);
147   }
148   return SalomeContainerTools::GetPlacementId(helper,this,askingNode);
149 }
150
151 std::string SalomeHPContainer::getFullPlacementId(const Task *askingNode) const
152 {
153   const SalomeContainerMonoHelper *helper(0);
154   {
155     YACS::BASES::AutoLocker<Container> alckCont(const_cast<SalomeHPContainer *>(this));
156     helper=_launchModeType.getHelperOfTask(askingNode);
157   }
158   return SalomeContainerTools::GetFullPlacementId(helper,this,askingNode);
159 }
160
161 /*!
162  * It is not a bug here ! clone for homogeneous container is not supposed to be copied !
163  */
164 Container *SalomeHPContainer::clone() const
165 {
166   incrRef();
167   return const_cast<SalomeHPContainer*>(this);
168 }
169
170 Container *SalomeHPContainer::cloneAlways() const
171 {
172   return new SalomeHPContainer(*this);
173 }
174
175 void SalomeHPContainer::setProperty(const std::string& name,const std::string& value)
176 {
177   if(name==AOC_ENTRY)//no sense to set it ! It is always true ! ignore it !
178     return ;
179   else if(name==SIZE_OF_POOL_KEY)
180     {
181       std::istringstream iss(value);
182       int val(0);
183       iss >> val;
184       setSizeOfPool(val);
185     }
186   else if(name==INITIALIZE_SCRIPT_KEY)
187     {
188       _initScript=value;
189     }
190   else
191     _sct.setProperty(name,value);
192 }
193
194 std::string SalomeHPContainer::getProperty(const std::string& name) const
195 {
196   if(name==AOC_ENTRY)
197     {
198       return std::string("1");
199     }
200   else if(name==SIZE_OF_POOL_KEY)
201     {
202       std::ostringstream oss; oss << getSizeOfPool();
203       return oss.str();
204     }
205   else if(name==INITIALIZE_SCRIPT_KEY)
206     {
207       return _initScript;
208     }
209   else
210     return _sct.getProperty(name);
211 }
212
213 void SalomeHPContainer::clearProperties()
214 {
215   _initScript.clear();
216   _sct.clearProperties();
217 }
218
219 void SalomeHPContainer::addComponentName(const std::string& name)
220 {
221   _componentNames.push_back(name);
222 }
223
224 std::map<std::string,std::string> SalomeHPContainer::getProperties() const
225 {
226   std::map<std::string,std::string> ret(_sct.getProperties());
227   std::ostringstream oss; oss << getSizeOfPool();
228   ret[SIZE_OF_POOL_KEY]=oss.str();
229   if(!_initScript.empty())
230     ret[INITIALIZE_SCRIPT_KEY]=_initScript;
231   return ret;
232 }
233
234 std::map<std::string,std::string> SalomeHPContainer::getResourceProperties(const std::string& name) const
235 {
236   return _sct.getResourceProperties(name);
237 }
238
239 void SalomeHPContainer::checkCapabilityToDealWith(const ComponentInstance *inst) const throw(YACS::Exception)
240 {
241   if(inst->getKind()!=SalomeHPComponent::KIND)
242     throw Exception("SalomeHPContainer::checkCapabilityToDealWith : SalomeContainer is not able to deal with this type of ComponentInstance.");
243 }
244
245 std::vector<std::string> SalomeHPContainer::getKernelContainerNames() const
246 {
247   return _launchModeType.getKernelContainerNames(this);
248 }
249
250 int SalomeHPContainer::getNumberOfCoresPerWorker() const
251 {
252   return _sct.getNumberOfCoresPerWorker();
253 }