Version: 9.15.0
SalomeHPContainer.cxx
Go to the documentation of this file.
1 // Copyright (C) 2006-2025 CEA, EDF
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"
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 
33 {
34 }
35 
36 SalomeHPContainer::SalomeHPContainer(const SalomeHPContainer& other):_componentNames(other._componentNames),_shutdownLevel(999),_sct(other._sct),_initScript(other._initScript)
37 {
38 }
39 
41 {
43  int nbOfWorkers(getPG()->getNumberOfWorkers(this->getNumberOfCoresPerWorker()));
44  this->setSizeOfPool(nbOfWorkers);
45 }
46 
48 {
50 }
51 
53 {
54  return _launchModeType.size();
55 }
56 
58 {
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 
88 {
89  std::size_t workerId(_launchModeType.release(node));
90  getPG()->release(workerId,this->getNumberOfCoresPerWorker());
91 }
92 
94 {
95 }
96 
97 std::string SalomeHPContainer::getKind() const
98 {
99  return KIND;
100 }
101 
102 std::string SalomeHPContainer::getDiscreminantStrOfThis(const Task *askingNode) const
103 {
105  return tmpCont->getDiscreminantStrOfThis(askingNode);
106 }
107 
108 bool SalomeHPContainer::isAlreadyStarted(const Task *askingNode) const
109 {
111  return helper->isAlreadyStarted(askingNode);
112 }
113 
114 void SalomeHPContainer::start(const Task *askingNode)
115 {
119 }
120 
122 {
123  if(level < _shutdownLevel)
124  return;
125  _shutdownLevel=999;
126  for(std::size_t i=0;i<_launchModeType.size();i++)
127  {
129  helper->shutdown();
130  }
131 }
132 
133 std::string SalomeHPContainer::getPlacementId(const Task *askingNode) const
134 {
135  const SalomeContainerMonoHelper *helper(nullptr);
136  {
137  std::lock_guard<std::mutex> lg(getPG()->getLocker());
138  helper=_launchModeType.getHelperOfTask(askingNode);
139  }
140  return SalomeContainerTools::GetPlacementId(helper,this,askingNode);
141 }
142 
143 std::string SalomeHPContainer::getFullPlacementId(const Task *askingNode) const
144 {
145  const SalomeContainerMonoHelper *helper(nullptr);
146  {
147  std::lock_guard<std::mutex> lg(getPG()->getLocker());
148  helper=_launchModeType.getHelperOfTask(askingNode);
149  }
150  return SalomeContainerTools::GetFullPlacementId(helper,this,askingNode);
151 }
152 
157 {
158  incrRef();
159  return const_cast<SalomeHPContainer*>(this);
160 }
161 
163 {
164  return new SalomeHPContainer(*this);
165 }
166 
167 void SalomeHPContainer::setProperty(const std::string& name,const std::string& value)
168 {
169  if(name==AOC_ENTRY)//no sense to set it ! It is always true ! ignore it !
170  return ;
171  else if(name==SIZE_OF_POOL_KEY)
172  {
173  std::istringstream iss(value);
174  int val(0);
175  iss >> val;
176  setSizeOfPool(val);
177  }
178  else if(name==INITIALIZE_SCRIPT_KEY)
179  {
180  _initScript=value;
181  }
182  else
183  _sct.setProperty(name,value);
184 }
185 
186 std::string SalomeHPContainer::getProperty(const std::string& name) const
187 {
188  if(name==AOC_ENTRY)
189  {
190  return std::string("1");
191  }
192  else if(name==SIZE_OF_POOL_KEY)
193  {
194  std::ostringstream oss; oss << getSizeOfPool();
195  return oss.str();
196  }
197  else if(name==INITIALIZE_SCRIPT_KEY)
198  {
199  return _initScript;
200  }
201  else
202  return _sct.getProperty(name);
203 }
204 
206 {
207  _initScript.clear();
209 }
210 
211 void SalomeHPContainer::addComponentName(const std::string& name)
212 {
213  _componentNames.push_back(name);
214 }
215 
216 std::map<std::string,std::string> SalomeHPContainer::getProperties() const
217 {
218  std::map<std::string,std::string> ret(_sct.getProperties());
219  std::ostringstream oss; oss << getSizeOfPool();
220  ret[SIZE_OF_POOL_KEY]=oss.str();
221  if(!_initScript.empty())
223  return ret;
224 }
225 
226 std::map<std::string,std::string> SalomeHPContainer::getResourceProperties(const std::string& name) const
227 {
228  return _sct.getResourceProperties(name);
229 }
230 
232 {
233  if(inst->getKind()!=SalomeHPComponent::KIND)
234  throw Exception("SalomeHPContainer::checkCapabilityToDealWith : SalomeContainer is not able to deal with this type of ComponentInstance.");
235 }
236 
237 std::vector<std::string> SalomeHPContainer::getKernelContainerNames() const
238 {
240 }
241 
243 {
245 }
const std::vector< std::size_t > * _v1
bool operator!=(PairVecIterator other) const
PairVecIterator & operator++()
const std::vector< const Task * > * _v0
bool operator==(PairVecIterator other) const
PairVecIterator(const std::vector< const Task * > *v0, const std::vector< std::size_t > *v1, const std::size_t num)
reference operator*() const
Base class for all component instances.
virtual std::string getKind() const
Return the component kind.
static const char AOC_ENTRY[]
Definition: Container.hxx:88
virtual void assignPG(const PlayGround *pg)
void release(std::size_t workerId, int nbCoresPerCont) const
Definition: PlayGround.cxx:210
std::size_t getNumberOfFreePlace(int nbCoresPerCont) const
Definition: PlayGround.cxx:184
bool isAlreadyStarted(const Task *askingNode) const
static SalomeContainerTmpForHP * BuildFrom(const SalomeHPContainer *cont, const Task *askingNode)
static std::string GetPlacementId(const SalomeContainerHelper *launchModeType, const Container *cont, const Task *askingNode)
static void Start(const std::vector< std::string > &compoNames, SalomeContainerHelper *schelp, SalomeContainerToolsBase &sct, int &shutdownLevel, const Container *cont, const Task *askingNode)
static std::string GetFullPlacementId(const SalomeContainerHelper *launchModeType, const Container *cont, const Task *askingNode)
std::map< std::string, std::string > getResourceProperties(const std::string &name) const override
std::string getProperty(const std::string &name) const override
const std::map< std::string, std::string > & getProperties() const override
void setProperty(const std::string &name, const std::string &value) override
void allocateForCrude(const std::vector< std::pair< const Task *, std::size_t >> &nodes)
const SalomeContainerMonoHelper * getHelperOfTaskThreadSafe(const SalomeHPContainer *cont, const Task *node) const
std::vector< std::string > getKernelContainerNames(const SalomeHPContainer *cont) const
const SalomeContainerMonoHelper * at(std::size_t pos) const
const SalomeContainerMonoHelper * getHelperOfTask(const Task *node) const
void checkCapabilityToDealWith(const ComponentInstance *inst) const
bool isAlreadyStarted(const Task *askingNode) const
std::string getPlacementId(const Task *askingNode) const
std::vector< std::string > getKernelContainerNames() const
void allocateFor(const std::vector< const Task * > &nodes)
std::vector< std::string > _componentNames
std::map< std::string, std::string > getProperties() const
void release(const Task *node) override
void start(const Task *askingNode)
std::map< std::string, std::string > getResourceProperties(const std::string &name) const
void assignPG(const PlayGround *pg) override
void setProperty(const std::string &name, const std::string &value)
std::string getDiscreminantStrOfThis(const Task *askingNode) const
SalomeHPContainerVectOfHelper _launchModeType
std::string getFullPlacementId(const Task *askingNode) const
std::size_t getNumberOfFreePlace() const
void addComponentName(const std::string &name)
std::string getProperty(const std::string &name) const