1 // Copyright (C) 2006-2016 CEA/DEN, EDF R&D
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.
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.
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
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #include "SalomeHPContainer.hxx"
21 #include "SalomeHPComponent.hxx"
22 #include "SalomeContainerTmpForHP.hxx"
23 #include "AutoLocker.hxx"
28 using namespace YACS::ENGINE;
30 const char SalomeHPContainer::KIND[]="HPSalome";
32 SalomeHPContainer::SalomeHPContainer():_shutdownLevel(999)
36 SalomeHPContainer::SalomeHPContainer(const SalomeHPContainer& other):_componentNames(other._componentNames),_shutdownLevel(999),_sct(other._sct),_initScript(other._initScript)
40 void SalomeHPContainer::assignPG(const PlayGround *pg)
42 HomogeneousPoolContainer::assignPG(pg);
43 int nbOfWorkers(getPG()->getNumberOfWorkers(this->getNumberOfCoresPerWorker()));
44 this->setSizeOfPool(nbOfWorkers);
47 void SalomeHPContainer::setSizeOfPool(int sz)
49 _launchModeType.resize(sz);
52 int SalomeHPContainer::getSizeOfPool() const
54 return _launchModeType.size();
57 std::size_t SalomeHPContainer::getNumberOfFreePlace() const
59 return getPG()->getNumberOfFreePlace(this->getNumberOfCoresPerWorker());
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
69 const std::vector< const Task *> *_v0;
70 const std::vector<std::size_t> *_v1;
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]); }
80 void SalomeHPContainer::allocateFor(const std::vector<const Task *>& nodes)
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);
87 void SalomeHPContainer::release(const Task *node)
89 _launchModeType.release(node);
92 SalomeHPContainer::~SalomeHPContainer()
96 void SalomeHPContainer::lock()
101 void SalomeHPContainer::unLock()
106 std::string SalomeHPContainer::getKind() const
111 std::string SalomeHPContainer::getDiscreminantStrOfThis(const Task *askingNode) const
113 YACS::BASES::AutoCppPtr<SalomeContainerTmpForHP> tmpCont(SalomeContainerTmpForHP::BuildFrom(this,askingNode));
114 return tmpCont->getDiscreminantStrOfThis(askingNode);
117 bool SalomeHPContainer::isAlreadyStarted(const Task *askingNode) const
119 const SalomeContainerMonoHelper *helper(_launchModeType.getHelperOfTaskThreadSafe(this,askingNode));
120 return helper->isAlreadyStarted(askingNode);
123 void SalomeHPContainer::start(const Task *askingNode) throw(YACS::Exception)
125 SalomeContainerMonoHelper *helper(_launchModeType.getHelperOfTaskThreadSafe(this,askingNode));
126 SalomeContainerTools::Start(_componentNames,helper,_sct,_shutdownLevel,this,askingNode);
129 void SalomeHPContainer::shutdown(int level)
131 if(level < _shutdownLevel)
134 for(std::size_t i=0;i<_launchModeType.size();i++)
136 SalomeContainerMonoHelper *helper(_launchModeType.at(i));
141 std::string SalomeHPContainer::getPlacementId(const Task *askingNode) const
143 const SalomeContainerMonoHelper *helper(0);
145 YACS::BASES::AutoLocker<Container> alckCont(const_cast<SalomeHPContainer *>(this));
146 helper=_launchModeType.getHelperOfTask(askingNode);
148 return SalomeContainerTools::GetPlacementId(helper,this,askingNode);
151 std::string SalomeHPContainer::getFullPlacementId(const Task *askingNode) const
153 const SalomeContainerMonoHelper *helper(0);
155 YACS::BASES::AutoLocker<Container> alckCont(const_cast<SalomeHPContainer *>(this));
156 helper=_launchModeType.getHelperOfTask(askingNode);
158 return SalomeContainerTools::GetFullPlacementId(helper,this,askingNode);
162 * It is not a bug here ! clone for homogeneous container is not supposed to be copied !
164 Container *SalomeHPContainer::clone() const
167 return const_cast<SalomeHPContainer*>(this);
170 Container *SalomeHPContainer::cloneAlways() const
172 return new SalomeHPContainer(*this);
175 void SalomeHPContainer::setProperty(const std::string& name,const std::string& value)
177 if(name==AOC_ENTRY)//no sense to set it ! It is always true ! ignore it !
179 else if(name==SIZE_OF_POOL_KEY)
181 std::istringstream iss(value);
186 else if(name==INITIALIZE_SCRIPT_KEY)
191 _sct.setProperty(name,value);
194 std::string SalomeHPContainer::getProperty(const std::string& name) const
198 return std::string("1");
200 else if(name==SIZE_OF_POOL_KEY)
202 std::ostringstream oss; oss << getSizeOfPool();
205 else if(name==INITIALIZE_SCRIPT_KEY)
210 return _sct.getProperty(name);
213 void SalomeHPContainer::clearProperties()
216 _sct.clearProperties();
219 void SalomeHPContainer::addComponentName(const std::string& name)
221 _componentNames.push_back(name);
224 std::map<std::string,std::string> SalomeHPContainer::getProperties() const
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;
234 std::map<std::string,std::string> SalomeHPContainer::getResourceProperties(const std::string& name) const
236 return _sct.getResourceProperties(name);
239 void SalomeHPContainer::checkCapabilityToDealWith(const ComponentInstance *inst) const throw(YACS::Exception)
241 if(inst->getKind()!=SalomeHPComponent::KIND)
242 throw Exception("SalomeHPContainer::checkCapabilityToDealWith : SalomeContainer is not able to deal with this type of ComponentInstance.");
245 std::vector<std::string> SalomeHPContainer::getKernelContainerNames() const
247 return _launchModeType.getKernelContainerNames(this);
250 int SalomeHPContainer::getNumberOfCoresPerWorker() const
252 return _sct.getNumberOfCoresPerWorker();