]> SALOME platform Git repositories - modules/yacs.git/blob - src/runtime/SalomeHPContainerTools.cxx
Salome HOME
SALOMEHPContainer getNumberOfFreePlace and allocateFor
[modules/yacs.git] / src / runtime / SalomeHPContainerTools.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 "SalomeHPContainerTools.hxx"
21 #include "SalomeHPContainer.hxx"
22 #include "AutoLocker.hxx"
23 #include "Exception.hxx"
24
25 #include <algorithm>
26
27 using namespace YACS::ENGINE;
28
29 void SalomeHPContainerVectOfHelper::resize(std::size_t sz)
30 {
31   std::size_t oldSize(_launchModeType.size());
32   if(sz==oldSize)
33     return;
34   checkNoCurrentWork();
35   _whichOccupied.resize(sz); std::fill(_whichOccupied.begin(),_whichOccupied.end(),false);
36   _launchModeType.resize(sz);
37   for(std::size_t i=oldSize;i<sz;i++)
38       _launchModeType[i]=new SalomeContainerMonoHelper;
39   _currentlyWorking.clear();
40 }
41
42 std::size_t SalomeHPContainerVectOfHelper::getNumberOfFreePlace() const
43 {
44   return std::count(_whichOccupied.begin(),_whichOccupied.end(),false);
45 }
46
47 void SalomeHPContainerVectOfHelper::allocateFor(const std::vector<const Task *>& nodes)
48 {
49   for(std::vector<const Task *>::const_iterator it=nodes.begin();it!=nodes.end();it++)
50     {
51       if(!(*it))
52         continue;
53       if(_currentlyWorking.find(*it)!=_currentlyWorking.end())
54         throw Exception("Searching to allocate for a ServiceNode instance already declared as allocated !");
55       std::vector<bool>::iterator it2(std::find(_whichOccupied.begin(),_whichOccupied.end(),false));
56       if(it2==_whichOccupied.end())
57         throw Exception("All ressources are already occupied ! You are expected to wait for released resources !");
58       std::size_t pos(std::distance(_whichOccupied.begin(),it2));
59       _currentlyWorking[*it]=pos; _whichOccupied[pos]=true;
60     }
61 }
62
63 void SalomeHPContainerVectOfHelper::allocateForCrude(const std::vector<std::pair<const Task *,std::size_t>>& nodes)
64 {
65   for(auto it : nodes)
66     {
67       std::size_t workerId(it.second);
68       if(workerId>=size())
69         throw Exception("SalomeHPContainerVectOfHelper::allocateForCrude : Internal error ! WorkerId is greater or equal to size of HPCont !");
70       if(_whichOccupied[workerId])
71         throw Exception("SalomeHPContainerVectOfHelper::allocateForCrude : Mismatch between Playground info and HPContainer info !");
72     }
73   for(auto it : nodes)
74     {
75       const Task *task(it.first);
76       std::size_t workerId(it.second);
77       _currentlyWorking[task]=workerId;
78       _whichOccupied[workerId]=true;
79     }
80 }
81
82 void SalomeHPContainerVectOfHelper::release(const Task *node)
83 {
84   if(!node)
85     return ;
86   std::map< const Task *,std::size_t >::iterator it(_currentlyWorking.find(node));
87   if(it==_currentlyWorking.end())
88     throw Exception("Request to release a resource not declared as working !");
89   _whichOccupied[(*it).second]=false;
90   _currentlyWorking.erase(it);
91 }
92
93 std::size_t SalomeHPContainerVectOfHelper::locateTask(const Task *node) const
94 {
95   std::map< const Task *,std::size_t >::const_iterator it(_currentlyWorking.find(node));
96   if(it==_currentlyWorking.end())
97     throw Exception("current Node to be located is not marked as launched !");
98   std::size_t ret((*it).second);
99   checkPosInVec(ret);
100   return ret;
101 }
102
103 const SalomeContainerMonoHelper *SalomeHPContainerVectOfHelper::getHelperOfTaskThreadSafe(const SalomeHPContainer *cont, const Task *node) const
104 {
105   YACS::BASES::AutoLocker<Container> alck(const_cast<SalomeHPContainer *>(cont));
106   return _launchModeType[locateTask(node)];
107 }
108
109 const SalomeContainerMonoHelper *SalomeHPContainerVectOfHelper::getHelperOfTask(const Task *node) const
110 {
111   return _launchModeType[locateTask(node)];
112 }
113
114 SalomeContainerMonoHelper *SalomeHPContainerVectOfHelper::getHelperOfTaskThreadSafe(SalomeHPContainer *cont, const Task *node)
115 {
116   YACS::BASES::AutoLocker<Container> alck(cont);
117   return _launchModeType[locateTask(node)];
118 }
119
120 SalomeContainerMonoHelper *SalomeHPContainerVectOfHelper::getHelperOfTask(const Task *node)
121 {
122   return _launchModeType[locateTask(node)];
123 }
124
125 std::vector<std::string> SalomeHPContainerVectOfHelper::getKernelContainerNames(const SalomeHPContainer *cont) const
126 {
127   std::vector<std::string> ret;
128   {
129     YACS::BASES::AutoLocker<Container> alck(const_cast<SalomeHPContainer *>(cont));
130     std::size_t sz(_launchModeType.size());
131     ret.resize(sz);
132     for(std::size_t i=0;i<sz;i++)
133       {
134         ret[i]=_launchModeType[i]->getKernelContainerName();
135       }
136   }
137   return ret;
138 }
139
140 void SalomeHPContainerVectOfHelper::checkNoCurrentWork() const
141 {
142   for(std::map<const Task *,std::size_t >::const_iterator it=_currentlyWorking.begin();it!=_currentlyWorking.end();it++)
143     if((*it).first)
144       throw Exception("Something wrong a node is still declared to be using the ressource !");
145   for(std::vector< YACS::BASES::AutoRefCnt<SalomeContainerMonoHelper> >::const_iterator it=_launchModeType.begin();it!=_launchModeType.end();it++)
146     if((*it)->isAlreadyStarted(0))
147       throw Exception("Some of the containers have be started ! Please shutdown them before !");
148 }
149
150 void SalomeHPContainerVectOfHelper::checkPosInVec(std::size_t pos) const
151 {
152   if(pos<0 || pos>=_launchModeType.size())
153     throw Exception("The task has been found, but its id is not in the correct range ! resize of of container size during run ?");
154 }