Salome HOME
Merge branch 'master' into omu/workloadmanager
[modules/yacs.git] / src / engine / PlayGround.cxx
index 4836417efc06164ace4a52c99746a6ea772df452..e896f1f4d83c4377487da97e38f68eb853ef3a8e 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2006-2019  CEA/DEN, EDF R&D
+// Copyright (C) 2006-2020  CEA/DEN, EDF R&D
 //
 // This library is free software; you can redistribute it and/or
 // modify it under the terms of the GNU Lesser General Public
@@ -25,6 +25,7 @@
 #include <sstream>
 #include <iomanip>
 #include <numeric>
+#include <iostream>
 #include <algorithm>
 
 using namespace YACS::ENGINE;
@@ -68,6 +69,36 @@ std::vector<std::size_t> Resource::allocateFor(std::size_t& nbOfPlacesToTake, in
   return ret;
 }
 
+void Resource::release(std::size_t workerId, int nbCoresPerCont) const
+{
+  if(workerId >= this->getNumberOfWorkers(nbCoresPerCont))
+    throw Exception("Resource::release : invalid worker id !");
+  std::size_t pos(workerId*static_cast<std::size_t>(nbCoresPerCont));
+  for(int i = 0 ; i < nbCoresPerCont ; ++i)
+    {
+      if(!_occupied[pos + static_cast<std::size_t>(i)])
+        throw Exception("Resource::release : internal error ! A core is expected to be occupied !");
+      _occupied[pos + static_cast<std::size_t>(i)] = false;
+    }
+}
+
+std::size_t Resource::getNumberOfWorkers(int nbCoresPerCont) const
+{
+  return static_cast<std::size_t>(this->nbCores())/static_cast<std::size_t>(nbCoresPerCont);
+}
+
+void Resource::printSelf(std::ostream& oss) const
+{
+  oss << this->name() << " (" << this->nbCores() << ") : ";
+  for(auto it : this->_occupied)
+    {
+      if(it)
+        oss << "1";
+      else
+        oss << "0";
+    }
+}
+
 std::string PlayGround::printSelf() const
 {
   std::ostringstream oss;
@@ -164,7 +195,7 @@ std::vector<std::size_t> PlayGround::allocateFor(std::size_t nbOfPlacesToTake, i
 {
   std::vector<std::size_t> ret;
   std::size_t nbOfPlacesToTakeCpy(nbOfPlacesToTake),offset(0);
-  for(auto res : _data)
+  for(const auto& res : _data)
     {
       std::vector<std::size_t> contIdsInRes(res.allocateFor(nbOfPlacesToTakeCpy,nbCoresPerCont));
       std::for_each(contIdsInRes.begin(),contIdsInRes.end(),[offset](std::size_t& val) { val += offset; });
@@ -176,6 +207,30 @@ std::vector<std::size_t> PlayGround::allocateFor(std::size_t nbOfPlacesToTake, i
   return ret;
 }
 
+void PlayGround::release(std::size_t workerId, int nbCoresPerCont) const
+{
+  std::size_t offset(0);
+  for(const auto& res : _data)
+    {
+      std::size_t nbOfWorker(static_cast<std::size_t>(res.nbCores()/nbCoresPerCont));
+      std::size_t minId(offset),maxId(offset+nbOfWorker);
+      if(workerId>=minId && workerId<maxId)
+        {
+          res.release(workerId-minId,nbCoresPerCont);
+          break;
+        }
+    }
+}
+
+void PlayGround::printMe() const
+{
+  for(auto it : _data)
+  {
+    it.printSelf(std::cout);
+    std::cout << std::endl;
+  }
+}
+
 std::vector<int> PlayGround::BuildVectOfIdsFromVecBool(const std::vector<bool>& v)
 {
   std::size_t sz(std::count(v.begin(),v.end(),true)),i(0);