Salome HOME
Merge remote-tracking branch 'origin/master'
[modules/kernel.git] / src / Container / SALOME_ContainerManager.cxx
index 36b5df43e39e763d70a5f871f4c7644ee5d33131..81ff3e15f33070c366c18b74663340bb3b43a3f2 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2007-2015  CEA/DEN, EDF R&D, OPEN CASCADE
+// Copyright (C) 2007-2017  CEA/DEN, EDF R&D, OPEN CASCADE
 //
 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
@@ -44,6 +44,7 @@
 
 #ifdef HAVE_MPI2
 #include <mpi.h>
+#include <sys/wait.h>
 #endif
 
 #ifdef WIN32
@@ -107,40 +108,42 @@ SALOME_ContainerManager::SALOME_ContainerManager(CORBA::ORB_ptr orb, PortableSer
   urifile << GetenvThreadSafeAsString("HOME") << "/.urifile_" << getpid();
   setenv("OMPI_URI_FILE",urifile.str().c_str(),1);
   if( GetenvThreadSafe("OMPI_URI_FILE") != NULL ){
-    // get the pid of all ompi-server
-    std::set<pid_t> thepids1 = getpidofprogram("ompi-server");
-    // launch a new ompi-server
-    std::string command;
-    command = "ompi-server -r ";
-    command += GetenvThreadSafeAsString("OMPI_URI_FILE");
-    int status=SystemThreadSafe(command.c_str());
-    if(status!=0)
-      throw SALOME_Exception("Error when launching ompi-server");
-    // get the pid of all ompi-server
-    std::set<pid_t> thepids2 = getpidofprogram("ompi-server");
-    // my ompi-server is the new one
-    std::set<pid_t>::const_iterator it;
-    for(it=thepids2.begin();it!=thepids2.end();it++)
-      if(thepids1.find(*it) == thepids1.end())
-        _pid_mpiServer = *it;
-    if(_pid_mpiServer < 0)
-      throw SALOME_Exception("Error when getting ompi-server id");
+    // Linux specific code
+    pid_t pid = fork(); // spawn a child process, following code is executed in both processes
+    if ( pid == 0 ) // I'm a child, replace myself with a new ompi-server
+    {
+      std::string uriarg = GetenvThreadSafeAsString("OMPI_URI_FILE");
+      execlp( "ompi-server", "ompi-server", "-r", uriarg.c_str(), NULL );
+      throw SALOME_Exception("Error when launching ompi-server"); // execlp failed
+    }
+    else if ( pid < 0 )
+    {
+      throw SALOME_Exception("fork() failed");
+    }
+    else // I'm a parent
+    {
+      //wait(NULL); // wait(?) for a child end
+      _pid_mpiServer = pid;
+    }
   }
 #elif defined(MPICH)
   _pid_mpiServer = -1;
-  // get the pid of all hydra_nameserver
-  std::set<pid_t> thepids1 = getpidofprogram("hydra_nameserver");
-  // launch a new hydra_nameserver
-  std::string command;
-  command = "hydra_nameserver &";
-  SystemThreadSafe(command.c_str());
-  // get the pid of all hydra_nameserver
-  std::set<pid_t> thepids2 = getpidofprogram("hydra_nameserver");
-  // my hydra_nameserver is the new one
-  std::set<pid_t>::const_iterator it;
-  for(it=thepids2.begin();it!=thepids2.end();it++)
-    if(thepids1.find(*it) == thepids1.end())
-      _pid_mpiServer = *it;
+  // Linux specific code
+  pid_t pid = fork(); // spawn a child process, following code is executed in both processes
+  if ( pid == 0 ) // I'm a child, replace myself with a new hydra_nameserver
+  {
+    execlp( "hydra_nameserver", "hydra_nameserver", NULL );
+    throw SALOME_Exception("Error when launching hydra_nameserver"); // execlp failed
+  }
+  else if ( pid < 0 )
+  {
+    throw SALOME_Exception("fork() failed");
+  }
+  else // I'm a parent
+  {
+    //wait(NULL);
+    _pid_mpiServer = pid;
+  }
 #endif
 #endif
 
@@ -390,7 +393,8 @@ Engines::Container_ptr SALOME_ContainerManager::GiveContainer(const Engines::Con
           return ret;
         }
         // A mpi parallel container register on zero node in NS
-        containerNameInNS = _NS->BuildContainerNameForNS(params, GetMPIZeroNode(hostname,machFile).c_str());
+        std::string mpiZeroNode = GetMPIZeroNode(resource_selected,machFile).c_str();
+        containerNameInNS = _NS->BuildContainerNameForNS(params, mpiZeroNode.c_str());
       }
       else
         containerNameInNS = _NS->BuildContainerNameForNS(params, hostname.c_str());
@@ -725,7 +729,7 @@ bool isPythonContainer(const char* ContainerName)
  *  ssh user@machine distantPath/runRemote.sh hostNS portNS WORKINGDIR workingdir \
  *                   SALOME_Container containerName &"
 
- *  - where user is ommited if not specified in CatalogResources,
+ *  - where user is omitted if not specified in CatalogResources,
  *  - where distant path is always relative to user@machine $HOME, and
  *    equal to $APPLI if not specified in CatalogResources,
  *  - where hostNS is the hostname of CORBA naming server (set by scripts to
@@ -756,7 +760,7 @@ SALOME_ContainerManager::BuildCommandToLaunchRemoteContainer(const std::string&
         nbproc = params.nb_proc;
     }
 
-    // "ssh -l user machine distantPath/runRemote.sh hostNS portNS WORKINGDIR workingdir \
+    // "ssh -l user machine distantPath/runRemote.sh hostNS portNS WORKINGDIR workingdir
     //  SALOME_Container containerName &"
     command = getCommandToRunRemoteProcess(resInfo.Protocol, resInfo.HostName, resInfo.UserName);
 
@@ -932,16 +936,16 @@ std::string SALOME_ContainerManager::BuildCommandToLaunchLocalContainer(const En
 
 void SALOME_ContainerManager::RmTmpFile(std::string& tmpFileName)
 {
-  int lenght = tmpFileName.size();
-  if ( lenght  > 0)
+  int length = tmpFileName.size();
+  if ( length  > 0)
     {
 #ifdef WIN32
       std::string command = "del /F ";
 #else
       std::string command = "rm ";
 #endif
-      if ( lenght > 4 )
-        command += tmpFileName.substr(0, lenght - 3 );
+      if ( length > 4 )
+        command += tmpFileName.substr(0, length - 3 );
       else
         command += tmpFileName;
       command += '*';
@@ -1044,6 +1048,9 @@ std::string SALOME_ContainerManager::BuildTemporaryFileName()
 {
   //build more complex file name to support multiple salome session
   std::string aFileName = Kernel_Utils::GetTmpFileName();
+  std::ostringstream str_pid;
+  str_pid << ::getpid();
+  aFileName = aFileName + "-" + str_pid.str();
 #ifndef WIN32
   aFileName += ".sh";
 #else
@@ -1194,12 +1201,17 @@ std::string SALOME_ContainerManager::GetMPIZeroNode(const std::string machine, c
   std::string zeronode;
   std::string command;
   std::string tmpFile = BuildTemporaryFileName();
+  const ParserResourcesType resInfo(_resManager->GetResourceDefinition(machine));
+
+  if(resInfo.Protocol == sh)
+  {
+    return resInfo.HostName;
+  }
 
   if( GetenvThreadSafe("LIBBATCH_NODEFILE") == NULL )
     {
       if (_isAppliSalomeDefined)
         {
-          const ParserResourcesType resInfo(_resManager->GetResourceDefinition(machine));
 
           if (resInfo.Protocol == rsh)
             command = "rsh ";
@@ -1285,21 +1297,6 @@ std::string SALOME_ContainerManager::machinesFile(const int nbproc)
 
 }
 
-std::set<pid_t> SALOME_ContainerManager::getpidofprogram(const std::string program)
-{
-  std::set<pid_t> thepids;
-  std::string tmpFile = Kernel_Utils::GetTmpFileName();
-  std::string cmd;
-  std::string thepid;
-  cmd = "pidof " + program + " > " + tmpFile;
-  SystemThreadSafe(cmd.c_str());
-  std::ifstream fpi(tmpFile.c_str(),std::ios::in);
-  while(fpi >> thepid){
-    thepids.insert(atoi(thepid.c_str()));
-  }
-  return thepids;
-}
-
 std::string SALOME_ContainerManager::getCommandToRunRemoteProcess(AccessProtocolType protocol,
                                                                   const std::string & hostname,
                                                                   const std::string & username)
@@ -1508,7 +1505,7 @@ SALOME_ContainerManager::StartPaCOPPContainer(const Engines::ContainerParameters
     }
     catch (...)
     {
-      INFOS("[StarPaCOPPContainer] Exception catched from proxy Shutdown...");
+      INFOS("[StarPaCOPPContainer] Exception caught from proxy Shutdown...");
     }
     return ret;
   }
@@ -1939,7 +1936,7 @@ SALOME_ContainerManager::LaunchPaCOProxyContainer(const std::string& command,
 
 //=============================================================================
 /*! This method launches the parallel container.
- *  It will may be placed on the ressources manager.
+ *  It will may be placed on the resources manager.
  *
  * \param command to launch
  * \param container's parameters
@@ -2049,4 +2046,3 @@ SALOME_ContainerManager::LaunchPaCONodeContainer(const std::string& command,
   return false;
 }
 #endif
-