1 // Copyright (C) 2007-2021 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License, or (at your option) any later version.
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // Lesser General Public License for more details.
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 #include "SALOME_ContainerManager.hxx"
24 #include "SALOME_ResourcesManager.hxx"
25 #include "SALOME_LoadRateManager.hxx"
26 #include "SALOME_NamingService.hxx"
27 #include "SALOME_ResourcesManager_Client.hxx"
28 #include "SALOME_ModuleCatalog.hh"
29 #include "Basics_Utils.hxx"
30 #include "Basics_DirUtils.hxx"
31 #include <sys/types.h>
38 #include "Utils_CorbaException.hxx"
42 #include <SALOMEconfig.h>
43 #include CORBA_CLIENT_HEADER(SALOME_Session)
52 #define getpid _getpid
55 #define S_ISREG(mode) (((mode) & S_IFMT) == S_IFREG)
60 #ifdef WITH_PACO_PARALLEL
64 const int SALOME_ContainerManager::TIME_OUT_TO_LAUNCH_CONT=60;
66 const char *SALOME_ContainerManager::_ContainerManagerNameInNS =
69 omni_mutex SALOME_ContainerManager::_numInstanceMutex;
71 Utils_Mutex SALOME_ContainerManager::_getenvMutex;
73 Utils_Mutex SALOME_ContainerManager::_systemMutex;
75 //=============================================================================
79 * Define a CORBA single thread policy for the server, which avoid to deal
80 * with non thread-safe usage like Change_Directory in SALOME naming service
82 //=============================================================================
84 SALOME_ContainerManager::SALOME_ContainerManager(CORBA::ORB_ptr orb, PortableServer::POA_var poa, SALOME_NamingService *ns)
87 MESSAGE("constructor");
89 _resManager = new SALOME_ResourcesManager_Client(ns);
91 PortableServer::POAManager_var pman = poa->the_POAManager();
92 _orb = CORBA::ORB::_duplicate(orb) ;
93 CORBA::PolicyList policies;
95 PortableServer::ThreadPolicy_var threadPol(poa->create_thread_policy(PortableServer::ORB_CTRL_MODEL));
96 policies[0] = PortableServer::ThreadPolicy::_duplicate(threadPol);
98 _poa = poa->create_POA("MThreadPOA",pman,policies);
100 PortableServer::ObjectId_var id = _poa->activate_object(this);
101 CORBA::Object_var obj = _poa->id_to_reference(id);
102 Engines::ContainerManager_var refContMan = Engines::ContainerManager::_narrow(obj);
104 _NS->Register(refContMan,_ContainerManagerNameInNS);
105 _isAppliSalomeDefined = (GetenvThreadSafe("APPLI") != 0);
110 // the urifile name depends on pid of the process
111 std::stringstream urifile;
112 urifile << GetenvThreadSafeAsString("HOME") << "/.urifile_" << getpid();
113 setenv("OMPI_URI_FILE",urifile.str().c_str(),1);
114 if( GetenvThreadSafe("OMPI_URI_FILE") != NULL ){
115 // Linux specific code
116 pid_t pid = fork(); // spawn a child process, following code is executed in both processes
117 if ( pid == 0 ) // I'm a child, replace myself with a new ompi-server
119 std::string uriarg = GetenvThreadSafeAsString("OMPI_URI_FILE");
120 execlp( "ompi-server", "ompi-server", "-r", uriarg.c_str(), NULL );
121 throw SALOME_Exception("Error when launching ompi-server"); // execlp failed
125 throw SALOME_Exception("fork() failed");
129 //wait(NULL); // wait(?) for a child end
130 _pid_mpiServer = pid;
135 // Linux specific code
136 pid_t pid = fork(); // spawn a child process, following code is executed in both processes
137 if ( pid == 0 ) // I'm a child, replace myself with a new hydra_nameserver
139 execlp( "hydra_nameserver", "hydra_nameserver", NULL );
140 throw SALOME_Exception("Error when launching hydra_nameserver"); // execlp failed
144 throw SALOME_Exception("fork() failed");
149 _pid_mpiServer = pid;
154 MESSAGE("constructor end");
157 //=============================================================================
161 //=============================================================================
163 SALOME_ContainerManager::~SALOME_ContainerManager()
165 MESSAGE("destructor");
169 if( GetenvThreadSafe("OMPI_URI_FILE") != NULL ){
170 // kill my ompi-server
171 if( kill(_pid_mpiServer,SIGTERM) != 0 )
172 throw SALOME_Exception("Error when killing ompi-server");
174 int status=SystemThreadSafe("rm -f ${OMPI_URI_FILE}");
176 throw SALOME_Exception("Error when removing urifile");
179 // kill my hydra_nameserver
180 if(_pid_mpiServer > -1)
181 if( kill(_pid_mpiServer,SIGTERM) != 0 )
182 throw SALOME_Exception("Error when killing hydra_nameserver");
187 //=============================================================================
188 //! shutdown all the containers, then the ContainerManager servant
191 //=============================================================================
193 void SALOME_ContainerManager::Shutdown()
196 ShutdownContainers();
198 _NS->Destroy_Name(_ContainerManagerNameInNS);
199 PortableServer::ObjectId_var oid = _poa->servant_to_id(this);
200 _poa->deactivate_object(oid);
203 //=============================================================================
204 //! Loop on all the containers listed in naming service, ask shutdown on each
207 //=============================================================================
209 void SALOME_ContainerManager::ShutdownContainers()
211 MESSAGE("ShutdownContainers");
214 SALOME::Session_var session = SALOME::Session::_nil();
216 CORBA::Object_var objS = _NS->Resolve("/Kernel/Session");
217 if (!CORBA::is_nil(objS))
219 session = SALOME::Session::_narrow(objS);
220 if (!CORBA::is_nil(session))
221 pid = session->getPID();
225 isOK = _NS->Change_Directory("/Containers");
227 std::vector<std::string> vec = _NS->list_directory_recurs();
228 std::list<std::string> lstCont;
229 for(std::vector<std::string>::iterator iter = vec.begin();iter!=vec.end();iter++)
232 CORBA::Object_var obj=_NS->Resolve((*iter).c_str());
235 Engines::Container_var cont=Engines::Container::_narrow(obj);
236 if(!CORBA::is_nil(cont) && pid != cont->getPID())
237 lstCont.push_back((*iter));
239 catch(const CORBA::Exception&)
241 // ignore this entry and continue
244 MESSAGE("Container list: ");
245 for(std::list<std::string>::iterator iter=lstCont.begin();iter!=lstCont.end();iter++){
248 for(std::list<std::string>::iterator iter=lstCont.begin();iter!=lstCont.end();iter++)
253 CORBA::Object_var obj=_NS->Resolve((*iter).c_str());
254 Engines::Container_var cont=Engines::Container::_narrow(obj);
255 if(!CORBA::is_nil(cont))
257 MESSAGE("ShutdownContainers: " << (*iter));
261 MESSAGE("ShutdownContainers: no container ref for " << (*iter));
263 catch(CORBA::SystemException& e)
265 INFOS("CORBA::SystemException ignored : " << e);
267 catch(CORBA::Exception&)
269 INFOS("CORBA::Exception ignored.");
273 INFOS("Unknown exception ignored.");
279 //=============================================================================
280 //! Give a suitable Container given constraints
282 * \param params Container Parameters required for the container
283 * \return the container or nil
285 //=============================================================================
286 Engines::Container_ptr SALOME_ContainerManager::GiveContainer(const Engines::ContainerParameters& params)
288 std::string machFile;
289 Engines::Container_ptr ret(Engines::Container::_nil());
291 // Step 0: Default mode is start
292 Engines::ContainerParameters local_params(params);
293 if (std::string(local_params.mode.in()) == "")
294 local_params.mode = CORBA::string_dup("start");
295 std::string mode = local_params.mode.in();
296 MESSAGE("[GiveContainer] starting with mode: " << mode);
298 // Step 1: Find Container for find and findorstart mode
299 if (mode == "find" || mode == "findorstart")
301 ret = FindContainer(params, params.resource_params.resList);
302 if(!CORBA::is_nil(ret))
308 MESSAGE("[GiveContainer] no container found");
318 // Step 2: Get all possibleResources from the parameters
319 // Consider only resources that can run containers
320 resourceParams resource_params = resourceParameters_CORBAtoCPP(local_params.resource_params);
321 resource_params.can_run_containers = true;
322 std::vector<std::string> possibleResources = _resManager->GetFittingResources(resource_params);
323 MESSAGE("[GiveContainer] - length of possible resources " << possibleResources.size());
324 std::vector<std::string> local_resources;
326 // Step 3: if mode is "get" keep only machines with existing containers
329 for(unsigned int i=0; i < possibleResources.size(); i++)
331 Engines::Container_ptr cont = FindContainer(params, possibleResources[i]);
334 if(!cont->_non_existent())
335 local_resources.push_back(possibleResources[i]);
337 catch(CORBA::Exception&) {}
340 // if local_resources is empty, we cannot give a container
341 if (local_resources.size() == 0)
343 MESSAGE("[GiveContainer] cannot find a container for mode get");
348 local_resources = possibleResources;
350 // Step 4: select the resource where to get/start the container
351 bool resource_available = true;
352 std::string resource_selected;
353 std::vector<std::string> resources = local_resources;
354 while (resource_available)
356 if (resources.size() == 0)
357 resource_available = false;
362 resource_selected = _resManager->Find(params.resource_params.policy.in(), resources);
363 // Remove resource_selected from vector
364 std::vector<std::string>::iterator it;
365 for (it=resources.begin() ; it < resources.end(); it++ )
366 if (*it == resource_selected)
372 catch(const SALOME_Exception &ex) //!< TODO: unused variable
374 MESSAGE("[GiveContainer] Exception in ResourceManager find !: " << ex.what());
377 MESSAGE("[GiveContainer] Resource selected is: " << resource_selected);
379 // Step 5: Create container name
380 ParserResourcesType resource_definition = _resManager->GetResourceDefinition(resource_selected);
381 std::string hostname(resource_definition.HostName);
382 std::string containerNameInNS;
384 int nbproc = params.nb_proc <= 0 ? 1 : params.nb_proc;
387 if( GetenvThreadSafe("LIBBATCH_NODEFILE") != NULL )
388 machFile = machinesFile(nbproc);
390 catch(const SALOME_Exception & ex)
392 std::string err_msg = ex.what();
393 err_msg += params.container_name;
394 INFOS(err_msg.c_str());
397 // A mpi parallel container register on zero node in NS
398 std::string mpiZeroNode = GetMPIZeroNode(resource_selected,machFile).c_str();
399 containerNameInNS = _NS->BuildContainerNameForNS(params, mpiZeroNode.c_str());
402 containerNameInNS = _NS->BuildContainerNameForNS(params, hostname.c_str());
403 MESSAGE("[GiveContainer] Container name in the naming service: " << containerNameInNS);
405 // Step 6: check if the name exists in naming service
406 //if params.mode == "getorstart" or "get" use the existing container
407 //if params.mode == "start" shutdown the existing container before launching a new one with that name
409 { // critical section
410 Utils_Locker lock (&_giveContainerMutex1);
411 CORBA::Object_var obj = _NS->Resolve(containerNameInNS.c_str());
412 if (!CORBA::is_nil(obj))
416 Engines::Container_var cont=Engines::Container::_narrow(obj);
417 if(!cont->_non_existent())
419 if(std::string(params.mode.in())=="getorstart" || std::string(params.mode.in())=="get"){
420 return cont._retn(); /* the container exists and params.mode is getorstart or get use it*/
424 INFOS("[GiveContainer] A container is already registered with the name: " << containerNameInNS << ", shutdown the existing container");
425 cont->Shutdown(); // shutdown the registered container if it exists
429 catch(CORBA::Exception&)
431 INFOS("[GiveContainer] CORBA::Exception ignored when trying to get the container - we start a new one");
434 } // end critical section
435 Engines::Container_var cont = LaunchContainer(params, resource_selected, hostname, machFile, containerNameInNS);
436 if (!CORBA::is_nil(cont))
438 INFOS("[GiveContainer] container " << containerNameInNS << " launched");
443 INFOS("[GiveContainer] Failed to launch container on resource " << resource_selected);
448 // We were not able to launch the container
449 INFOS("[GiveContainer] Cannot launch the container on the following selected resources:")
450 std::vector<std::string>::iterator it;
451 for (it=local_resources.begin() ; it < local_resources.end(); it++ )
452 INFOS("[GiveContainer] " << *it)
456 Engines::Container_ptr
457 SALOME_ContainerManager::LaunchContainer(const Engines::ContainerParameters& params,
458 const std::string & resource_selected,
459 const std::string & hostname,
460 const std::string & machFile,
461 const std::string & containerNameInNS)
463 std::string user,command,logFilename,tmpFileName;
465 Engines::Container_ptr ret(Engines::Container::_nil());
466 {//start of critical section
467 Utils_Locker lock (&_giveContainerMutex1);
468 // Step 1: type of container: PaCO, Exe, Mpi or Classic
469 // Mpi already tested in step 5, specific code on BuildCommandToLaunch Local/Remote Container methods
470 // TODO -> separates Mpi from Classic/Exe
472 std::string container_exe = "SALOME_Container"; // Classic container
473 Engines::ContainerParameters local_params(params);
477 CORBA::String_var container_exe_tmp;
478 CORBA::Object_var obj = _NS->Resolve("/Kernel/ModulCatalog");
479 SALOME_ModuleCatalog::ModuleCatalog_var Catalog = SALOME_ModuleCatalog::ModuleCatalog::_narrow(obj) ;
480 if (CORBA::is_nil (Catalog))
482 INFOS("[GiveContainer] Module Catalog is not found -> cannot launch a container");
485 // Loop through component list
486 for(unsigned int i=0; i < local_params.resource_params.componentList.length(); i++)
488 const char* compoi = local_params.resource_params.componentList[i];
489 SALOME_ModuleCatalog::Acomponent_var compoInfo = Catalog->GetComponent(compoi);
490 if (CORBA::is_nil (compoInfo))
494 SALOME_ModuleCatalog::ImplType impl=compoInfo->implementation_type();
495 container_exe_tmp=compoInfo->implementation_name();
496 if(impl==SALOME_ModuleCatalog::CEXE)
500 INFOS("ContainerManager Error: you can't have 2 CEXE component in the same container" );
501 return Engines::Container::_nil();
503 MESSAGE("[GiveContainer] Exe container found !: " << container_exe_tmp);
504 container_exe = container_exe_tmp.in();
509 catch (ServiceUnreachable&)
511 INFOS("Caught exception: Naming Service Unreachable");
516 INFOS("Caught unknown exception.");
520 // Step 2: test resource
521 // Only if an application directory is set
522 if(hostname != Kernel_Utils::GetHostname() && _isAppliSalomeDefined)
525 const ParserResourcesType resInfo(_resManager->GetResourceDefinition(resource_selected));
526 std::string command = getCommandToRunRemoteProcess(resInfo.Protocol, resInfo.HostName,
527 resInfo.UserName, resInfo.AppliPath);
529 // Launch remote command
530 command += " \"ls /tmp >/dev/null 2>&1\"";
531 // Anthony : command is NO MORE launched to improve dramatically time to launch containers
535 // Error on resource - cannot launch commands
536 INFOS("[LaunchContainer] Cannot launch commands on machine " << hostname);
537 INFOS("[LaunchContainer] Command was " << command);
539 INFOS("[LaunchContainer] Command status is " << WEXITSTATUS(status));
541 return Engines::Container::_nil();
545 // Step 3: start a new container
546 // Check if a PaCO container
548 if (std::string(local_params.parallelLib.in()) != "")
550 ret = StartPaCOPPContainer(params, resource_selected);
553 // Other type of containers...
554 MESSAGE("[GiveContainer] Try to launch a new container on " << resource_selected);
555 // if a parallel container is launched in batch job, command is: "mpirun -np nbproc -machinefile nodesfile SALOME_MPIContainer"
556 if( GetenvThreadSafe("LIBBATCH_NODEFILE") != NULL && params.isMPI )
557 command = BuildCommandToLaunchLocalContainer(params, machFile, container_exe, tmpFileName);
558 // if a container is launched on localhost, command is "SALOME_Container" or "mpirun -np nbproc SALOME_MPIContainer"
559 else if(hostname == Kernel_Utils::GetHostname())
560 command = BuildCommandToLaunchLocalContainer(params, machFile, container_exe, tmpFileName);
561 // if a container is launched in remote mode, command is "ssh resource_selected SALOME_Container" or "ssh resource_selected mpirun -np nbproc SALOME_MPIContainer"
563 command = BuildCommandToLaunchRemoteContainer(resource_selected, params, container_exe);
565 //redirect stdout and stderr in a file
567 logFilename=GetenvThreadSafeAsString("TEMP");
569 user = GetenvThreadSafeAsString( "USERNAME" );
571 user = GetenvThreadSafeAsString( "USER" );
573 user = GetenvThreadSafeAsString( "LOGNAME" );
575 char* val = GetenvThreadSafe("SALOME_TMP_DIR");
578 struct stat file_info;
579 stat(val, &file_info);
580 bool is_dir = S_ISDIR(file_info.st_mode);
581 if (is_dir)logFilename=val;
582 else std::cerr << "SALOME_TMP_DIR environment variable is not a directory use /tmp instead" << std::endl;
586 logFilename += _NS->ContainerName(params)+"_"+ resource_selected +"_"+user;
587 std::ostringstream tmp;
588 tmp << "_" << getpid();
589 logFilename += tmp.str();
590 logFilename += ".log" ;
591 command += " > " + logFilename + " 2>&1";
592 MakeTheCommandToBeLaunchedASync(command);
594 // launch container with a system call
595 status=SystemThreadSafe(command.c_str());
596 }//end of critical of section
600 INFOS("[LaunchContainer] command failed (system command status -1): " << command);
601 RmTmpFile(tmpFileName); // command file can be removed here
602 return Engines::Container::_nil();
604 else if (status == 217)
606 INFOS("[LaunchContainer] command failed (system command status 217): " << command);
607 RmTmpFile(tmpFileName); // command file can be removed here
608 return Engines::Container::_nil();
612 // Step 4: Wait for the container
613 int count(GetTimeOutToLoaunchServer());
614 INFOS("[GiveContainer] waiting " << count << " second steps container " << containerNameInNS);
615 while (CORBA::is_nil(ret) && count)
619 MESSAGE("[GiveContainer] step " << count << " Waiting for container on " << resource_selected);
620 CORBA::Object_var obj(_NS->Resolve(containerNameInNS.c_str()));
621 ret=Engines::Container::_narrow(obj);
623 if (CORBA::is_nil(ret))
625 INFOS("[GiveContainer] was not able to launch container " << containerNameInNS);
629 // Setting log file name
630 logFilename=":"+logFilename;
631 logFilename="@"+Kernel_Utils::GetHostname()+logFilename;//threadsafe
632 logFilename=user+logFilename;
633 ret->logfilename(logFilename.c_str());
634 RmTmpFile(tmpFileName); // command file can be removed here
640 //=============================================================================
641 //! Find a container given constraints (params) on a list of machines (possibleComputers)
642 //! agy : this method is ThreadSafe
646 //=============================================================================
648 Engines::Container_ptr SALOME_ContainerManager::FindContainer(const Engines::ContainerParameters& params, const Engines::ResourceList& possibleResources)
650 MESSAGE("[FindContainer] FindContainer on " << possibleResources.length() << " resources");
651 for(unsigned int i=0; i < possibleResources.length();i++)
653 Engines::Container_ptr cont = FindContainer(params, possibleResources[i].in());
654 if(!CORBA::is_nil(cont))
657 MESSAGE("[FindContainer] no container found");
658 return Engines::Container::_nil();
661 //=============================================================================
662 //! Find a container given constraints (params) on a machine (theMachine)
663 //! agy : this method is ThreadSafe
667 //=============================================================================
669 Engines::Container_ptr
670 SALOME_ContainerManager::FindContainer(const Engines::ContainerParameters& params, const std::string& resource)
672 ParserResourcesType resource_definition = _resManager->GetResourceDefinition(resource);
673 std::string hostname(resource_definition.HostName);
674 std::string containerNameInNS(_NS->BuildContainerNameForNS(params, hostname.c_str()));
675 MESSAGE("[FindContainer] Try to find a container " << containerNameInNS << " on resource " << resource);
676 CORBA::Object_var obj = _NS->Resolve(containerNameInNS.c_str());
679 if(obj->_non_existent())
680 return Engines::Container::_nil();
682 return Engines::Container::_narrow(obj);
684 catch(const CORBA::Exception&)
686 return Engines::Container::_nil();
691 bool isPythonContainer(const char* ContainerName);
693 //=============================================================================
695 * This is no longer valid (C++ container are also python containers)
697 //=============================================================================
698 bool isPythonContainer(const char* ContainerName)
700 return false; // VSR 02/08/2013: Python containers are no more supported
702 size_t len = strlen(ContainerName);
705 if (strcmp(ContainerName + len - 2, "Py") == 0)
711 //=============================================================================
713 * Builds the script to be launched
715 * If SALOME Application not defined ($APPLI),
716 * see BuildTempFileToLaunchRemoteContainer()
718 * Else rely on distant configuration. Command is under the form (example):
719 * ssh user@machine distantPath/runRemote.sh hostNS portNS WORKINGDIR workingdir \
720 * SALOME_Container containerName &"
722 * - where user is omitted if not specified in CatalogResources,
723 * - where distant path is always relative to user@machine $HOME, and
724 * equal to $APPLI if not specified in CatalogResources,
725 * - where hostNS is the hostname of CORBA naming server (set by scripts to
726 * use to launch SALOME and servers in $APPLI: runAppli.sh, runRemote.sh)
727 * - where portNS is the port used by CORBA naming server (set by scripts to
728 * use to launch SALOME and servers in $APPLI: runAppli.sh, runRemote.sh)
729 * - where workingdir is the requested working directory for the container.
730 * If WORKINGDIR (and workingdir) is not present the working dir will be $HOME
732 //=============================================================================
735 SALOME_ContainerManager::BuildCommandToLaunchRemoteContainer(const std::string& resource_name, const Engines::ContainerParameters& params, const std::string& container_exe) const
737 std::string command,tmpFileName;
738 if (!_isAppliSalomeDefined)
739 command = BuildTempFileToLaunchRemoteContainer(resource_name, params, tmpFileName);
742 const ParserResourcesType resInfo(_resManager->GetResourceDefinition(resource_name));
744 std::string wdir = params.workingdir.in();
746 // "ssh -l user machine distantPath/runRemote.sh hostNS portNS WORKINGDIR workingdir
747 // SALOME_Container containerName -ORBInitRef NameService=IOR:01000..."
749 // "ssh -l user machine distantLauncher remote -p hostNS -m portNS -d dir
750 // -- SALOME_Container contName -ORBInitRef NameService=IOR:01000..."
751 command = getCommandToRunRemoteProcess(resInfo.Protocol, resInfo.HostName,
752 resInfo.UserName, resInfo.AppliPath,
757 int nbproc = params.nb_proc <= 0 ? 1 : params.nb_proc;
758 command += " mpirun -np ";
759 std::ostringstream o;
763 command += "-x PATH,LD_LIBRARY_PATH,OMNIORB_CONFIG,SALOME_trace ";
764 #elif defined(OPEN_MPI)
765 if( GetenvThreadSafe("OMPI_URI_FILE") == NULL )
766 command += "-x PATH -x LD_LIBRARY_PATH -x OMNIORB_CONFIG -x SALOME_trace";
768 command += "-x PATH -x LD_LIBRARY_PATH -x OMNIORB_CONFIG -x SALOME_trace -ompi-server file:";
769 command += GetenvThreadSafeAsString("OMPI_URI_FILE");
772 command += "-nameserver " + Kernel_Utils::GetHostname();
774 command += " SALOME_MPIContainer ";
777 command += " " +container_exe+ " ";
779 command += _NS->ContainerName(params);
781 AddOmninamesParams(command);
783 MESSAGE("command =" << command);
789 //=============================================================================
791 * builds the command to be launched.
793 //=============================================================================
794 std::string SALOME_ContainerManager::BuildCommandToLaunchLocalContainer(const Engines::ContainerParameters& params, const std::string& machinesFile, const std::string& container_exe, std::string& tmpFileName) const
796 tmpFileName = BuildTemporaryFileName();
799 std::ostringstream o;
803 int nbproc = params.nb_proc <= 0 ? 1 : params.nb_proc;
809 if( GetenvThreadSafe("LIBBATCH_NODEFILE") != NULL )
810 o << "-machinefile " << machinesFile << " ";
813 o << "-x PATH,LD_LIBRARY_PATH,OMNIORB_CONFIG,SALOME_trace ";
814 #elif defined(OPEN_MPI)
815 if( GetenvThreadSafe("OMPI_URI_FILE") == NULL )
816 o << "-x PATH -x LD_LIBRARY_PATH -x OMNIORB_CONFIG -x SALOME_trace";
819 o << "-x PATH -x LD_LIBRARY_PATH -x OMNIORB_CONFIG -x SALOME_trace -ompi-server file:";
820 o << GetenvThreadSafeAsString("OMPI_URI_FILE");
823 o << "-nameserver " + Kernel_Utils::GetHostname();
826 if (isPythonContainer(params.container_name))
827 o << " pyMPI SALOME_ContainerPy.py ";
829 o << " SALOME_MPIContainer ";
834 std::string wdir=params.workingdir.in();
837 // a working directory is requested
838 if(wdir == "$TEMPDIR")
840 // a new temporary directory is requested
841 std::string dir = Kernel_Utils::GetTmpDir();
843 o << "cd /d " << dir << std::endl;
845 o << "cd " << dir << ";";
851 // a permanent directory is requested use it or create it
853 o << "mkdir " + wdir << std::endl;
854 o << "cd /D " + wdir << std::endl;
856 o << "mkdir -p " << wdir << " && cd " << wdir + ";";
861 if (isPythonContainer(params.container_name))
862 o << "SALOME_ContainerPy.py ";
864 o << container_exe + " ";
868 o << _NS->ContainerName(params);
870 AddOmninamesParams(o);
872 std::ofstream command_file( tmpFileName.c_str() );
873 command_file << o.str();
874 command_file.close();
877 chmod(tmpFileName.c_str(), 0x1ED);
879 command = tmpFileName;
881 MESSAGE("Command is file ... " << command);
882 MESSAGE("Command is ... " << o.str());
887 //=============================================================================
889 * removes the generated temporary file in case of a remote launch.
890 * This method is thread safe
892 //=============================================================================
894 void SALOME_ContainerManager::RmTmpFile(std::string& tmpFileName)
896 size_t length = tmpFileName.size();
900 std::string command = "del /F ";
902 std::string command = "rm ";
905 command += tmpFileName.substr(0, length - 3 );
907 command += tmpFileName;
909 SystemThreadSafe(command.c_str());
910 //if dir is empty - remove it
911 std::string tmp_dir = Kernel_Utils::GetDirByPath( tmpFileName );
912 if ( Kernel_Utils::IsEmptyDir( tmp_dir ) )
915 command = "del /F " + tmp_dir;
917 command = "rmdir " + tmp_dir;
919 SystemThreadSafe(command.c_str());
924 //=============================================================================
926 * add to command all options relative to naming service.
928 //=============================================================================
930 void SALOME_ContainerManager::AddOmninamesParams(std::string& command) const
932 std::ostringstream oss;
933 AddOmninamesParams(oss);
937 //=============================================================================
939 * add to command all options relative to naming service.
941 //=============================================================================
943 void SALOME_ContainerManager::AddOmninamesParams(std::ostream& fileStream) const
945 AddOmninamesParams(fileStream,_NS);
948 //=============================================================================
950 * add to command all options relative to naming service.
952 //=============================================================================
954 void SALOME_ContainerManager::AddOmninamesParams(std::ostream& fileStream, SALOME_NamingService *ns)
956 CORBA::String_var iorstr(ns->getIORaddr());
957 fileStream << "ORBInitRef NameService=";
958 fileStream << iorstr;
961 void SALOME_ContainerManager::MakeTheCommandToBeLaunchedASync(std::string& command)
964 command = "%PYTHONBIN% -c \"import subprocess ; subprocess.Popen(r'" + command + "').pid\"";
970 int SALOME_ContainerManager::GetTimeOutToLoaunchServer()
972 int count(TIME_OUT_TO_LAUNCH_CONT);
973 if (GetenvThreadSafe("TIMEOUT_TO_LAUNCH_CONTAINER") != 0)
975 std::string new_count_str(GetenvThreadSafeAsString("TIMEOUT_TO_LAUNCH_CONTAINER"));
977 std::istringstream ss(new_count_str);
978 if (!(ss >> new_count))
980 INFOS("[LaunchContainer] TIMEOUT_TO_LAUNCH_CONTAINER should be an int");
988 void SALOME_ContainerManager::SleepInSecond(int ellapseTimeInSecond)
991 sleep( ellapseTimeInSecond ) ;
993 int timeInMS(1000*ellapseTimeInSecond);
998 //=============================================================================
1000 * generate a file name in /tmp directory
1002 //=============================================================================
1004 std::string SALOME_ContainerManager::BuildTemporaryFileName()
1006 //build more complex file name to support multiple salome session
1007 std::string aFileName = Kernel_Utils::GetTmpFileName();
1008 std::ostringstream str_pid;
1009 str_pid << ::getpid();
1010 aFileName = aFileName + "-" + str_pid.str();
1014 aFileName += ".bat";
1019 //=============================================================================
1021 * Builds in a temporary file the script to be launched.
1023 * Used if SALOME Application ($APPLI) is not defined.
1024 * The command is build with data from CatalogResources, in which every path
1025 * used on remote computer must be defined.
1027 //=============================================================================
1029 std::string SALOME_ContainerManager::BuildTempFileToLaunchRemoteContainer (const std::string& resource_name, const Engines::ContainerParameters& params, std::string& tmpFileName) const
1033 tmpFileName = BuildTemporaryFileName();
1034 std::ofstream tempOutputFile;
1035 tempOutputFile.open(tmpFileName.c_str(), std::ofstream::out );
1036 const ParserResourcesType resInfo(_resManager->GetResourceDefinition(resource_name));
1037 tempOutputFile << "#! /bin/sh" << std::endl;
1041 tempOutputFile << "export SALOME_trace=local" << std::endl; // mkr : 27.11.2006 : PAL13967 - Distributed supervision graphs - Problem with "SALOME_trace"
1042 //tempOutputFile << "source " << resInfo.PreReqFilePath << endl;
1048 int nbproc = params.nb_proc <= 0 ? 1 : params.nb_proc;
1050 tempOutputFile << "mpirun -np ";
1052 tempOutputFile << nbproc << " ";
1054 tempOutputFile << "-x PATH,LD_LIBRARY_PATH,OMNIORB_CONFIG,SALOME_trace ";
1055 #elif defined(OPEN_MPI)
1056 if( GetenvThreadSafe("OMPI_URI_FILE") == NULL )
1057 tempOutputFile << "-x PATH -x LD_LIBRARY_PATH -x OMNIORB_CONFIG -x SALOME_trace";
1059 tempOutputFile << "-x PATH -x LD_LIBRARY_PATH -x OMNIORB_CONFIG -x SALOME_trace -ompi-server file:";
1060 tempOutputFile << GetenvThreadSafeAsString("OMPI_URI_FILE");
1062 #elif defined(MPICH)
1063 tempOutputFile << "-nameserver " + Kernel_Utils::GetHostname();
1067 tempOutputFile << GetenvThreadSafeAsString("KERNEL_ROOT_DIR") << "/bin/salome/";
1071 if (isPythonContainer(params.container_name))
1072 tempOutputFile << " pyMPI SALOME_ContainerPy.py ";
1074 tempOutputFile << " SALOME_MPIContainer ";
1079 if (isPythonContainer(params.container_name))
1080 tempOutputFile << "SALOME_ContainerPy.py ";
1082 tempOutputFile << "SALOME_Container ";
1085 tempOutputFile << _NS->ContainerName(params) << " -";
1086 AddOmninamesParams(tempOutputFile);
1087 tempOutputFile << " &" << std::endl;
1088 tempOutputFile.flush();
1089 tempOutputFile.close();
1091 chmod(tmpFileName.c_str(), 0x1ED);
1094 // --- Build command
1096 std::string command;
1098 if (resInfo.Protocol == rsh)
1101 std::string commandRcp = "rcp ";
1102 commandRcp += tmpFileName;
1104 commandRcp += resInfo.HostName;
1106 commandRcp += tmpFileName;
1107 status = SystemThreadSafe(commandRcp.c_str());
1110 else if (resInfo.Protocol == ssh)
1113 std::string commandRcp = "scp ";
1114 commandRcp += tmpFileName;
1116 commandRcp += resInfo.HostName;
1118 commandRcp += tmpFileName;
1119 status = SystemThreadSafe(commandRcp.c_str());
1122 else if (resInfo.Protocol == srun)
1124 command = "srun -n 1 -N 1 -s --mem-per-cpu=0 --cpu-bind=none --nodelist=";
1125 std::string commandRcp = "rcp ";
1126 commandRcp += tmpFileName;
1128 commandRcp += resInfo.HostName;
1130 commandRcp += tmpFileName;
1131 status = SystemThreadSafe(commandRcp.c_str());
1134 throw SALOME_Exception("Unknown protocol");
1137 throw SALOME_Exception("Error of connection on remote host");
1139 command += resInfo.HostName;
1141 command += tmpFileName;
1149 std::string SALOME_ContainerManager::GetMPIZeroNode(const std::string machine, const std::string machinesFile) const
1152 std::string zeronode;
1153 std::string command;
1154 std::string tmpFile = BuildTemporaryFileName();
1155 const ParserResourcesType resInfo(_resManager->GetResourceDefinition(machine));
1157 if(resInfo.Protocol == sh)
1159 return resInfo.HostName;
1162 if( GetenvThreadSafe("LIBBATCH_NODEFILE") == NULL )
1164 if (_isAppliSalomeDefined)
1166 command = getCommandToRunRemoteProcess(resInfo.Protocol, resInfo.HostName,
1167 resInfo.UserName, resInfo.AppliPath);
1168 command += " mpirun -np 1 hostname -s > " + tmpFile;
1171 command = "mpirun -np 1 hostname -s > " + tmpFile;
1174 command = "mpirun -np 1 -machinefile " + machinesFile + " hostname -s > " + tmpFile;
1176 status = SystemThreadSafe(command.c_str());
1178 std::ifstream fp(tmpFile.c_str(),std::ios::in);
1179 while(fp >> zeronode);
1187 std::string SALOME_ContainerManager::machinesFile(const int nbproc)
1190 std::string nodesFile = GetenvThreadSafeAsString("LIBBATCH_NODEFILE");
1191 std::string machinesFile = Kernel_Utils::GetTmpFileName();
1192 std::ifstream fpi(nodesFile.c_str(),std::ios::in);
1193 std::ofstream fpo(machinesFile.c_str(),std::ios::out);
1195 _numInstanceMutex.lock();
1197 for(int i=0;i<_nbprocUsed;i++)
1200 for(int i=0;i<nbproc;i++)
1202 fpo << tmp << std::endl;
1204 throw SALOME_Exception("You need more processors than batch session have allocated for you! Unable to launch the mpi container: ");
1206 _nbprocUsed += nbproc;
1210 _numInstanceMutex.unlock();
1212 return machinesFile;
1216 std::string SALOME_ContainerManager::getCommandToRunRemoteProcess(AccessProtocolType protocol,
1217 const std::string & hostname,
1218 const std::string & username,
1219 const std::string & applipath,
1220 const std::string & workdir)
1222 std::ostringstream command;
1229 command << "-l " << username << " ";
1231 command << hostname << " ";
1237 command << "-l " << username << " ";
1239 command << hostname << " ";
1242 // no need to redefine the user with srun, the job user is taken by default
1243 // (note: for srun, user id can be specified with " --uid=<user>")
1244 command << "srun -n 1 -N 1 -s --mem-per-cpu=0 --cpu-bind=none --nodelist=" << hostname << " ";
1247 command << "pbsdsh -o -h " << hostname << " ";
1250 command << "blaunch -no-shell " << hostname << " ";
1253 throw SALOME_Exception("Unknown protocol");
1256 std::string remoteapplipath;
1258 remoteapplipath = GetenvThreadSafeAsString("APPLI");
1260 remoteapplipath = applipath;
1262 ASSERT(GetenvThreadSafe("NSHOST"));
1263 ASSERT(GetenvThreadSafe("NSPORT"));
1265 // $APPLI points either to an application directory, or to a salome launcher file
1266 // we prepare the remote command according to the case
1267 struct stat statbuf;
1268 if (stat(GetenvThreadSafe("APPLI"), &statbuf) ==0 && S_ISREG(statbuf.st_mode))
1270 // if $APPLI is a regular file, we asume it's a salome Launcher
1271 // generate a command with a salome launcher
1272 command << remoteapplipath
1275 << GetenvThreadSafeAsString("NSHOST") // hostname of CORBA name server
1277 << GetenvThreadSafeAsString("NSPORT"); // port of CORBA name server
1279 command << "-d " << workdir;
1282 else // we assume it's a salome application directory
1284 // generate a command with runRemote.sh
1285 command << remoteapplipath;
1286 command << "/runRemote.sh ";
1287 command << GetenvThreadSafeAsString("NSHOST"); // hostname of CORBA name server
1289 command << GetenvThreadSafeAsString("NSPORT"); // port of CORBA name server
1292 command << " WORKINGDIR ";
1294 if(workdir == "$TEMPDIR")
1295 command << "\\$TEMPDIR";
1297 command << workdir; // requested working directory
1302 return command.str();
1306 SALOME_ContainerManager::checkPaCOParameters(Engines::ContainerParameters & params, std::string resource_selected)
1310 // Step 1 : check ContainerParameters
1311 // Check container_name, has to be defined
1312 if (std::string(params.container_name.in()) == "")
1314 INFOS("[checkPaCOParameters] You must define a container_name to launch a PaCO++ container");
1317 // Check parallelLib
1318 std::string parallelLib = params.parallelLib.in();
1319 if (parallelLib != "Mpi" && parallelLib != "Dummy")
1321 INFOS("[checkPaCOParameters] parallelLib is not correctly defined");
1322 INFOS("[checkPaCOParameters] you can chosse between: Mpi and Dummy");
1323 INFOS("[checkPaCOParameters] you entered: " << parallelLib);
1327 if (params.nb_proc <= 0)
1329 INFOS("[checkPaCOParameters] You must define a nb_proc > 0");
1333 // Step 2 : check resource_selected
1334 const ParserResourcesType resource_definition = _resManager->GetResourceDefinition(resource_selected);
1335 //std::string protocol = resource_definition->protocol.in();
1336 std::string username = resource_definition.UserName;
1337 std::string applipath = resource_definition.AppliPath;
1339 //if (protocol == "" || username == "" || applipath == "")
1340 if (username == "" || applipath == "")
1342 INFOS("[checkPaCOParameters] resource selected is not well defined");
1343 INFOS("[checkPaCOParameters] resource name: " << resource_definition.Name);
1344 INFOS("[checkPaCOParameters] resource hostname: " << resource_definition.HostName);
1345 INFOS("[checkPaCOParameters] resource protocol: " << resource_definition.getAccessProtocolTypeStr());
1346 INFOS("[checkPaCOParameters] resource username: " << username);
1347 INFOS("[checkPaCOParameters] resource applipath: " << applipath);
1355 * :WARNING: Do not directly convert returned value to std::string
1356 * This function may return NULL if env variable is not defined.
1357 * And std::string(NULL) causes undefined behavior.
1358 * Use GetenvThreadSafeAsString to properly get a std::string.
1360 char *SALOME_ContainerManager::GetenvThreadSafe(const char *name)
1361 {// getenv is not thread safe. See man 7 pthread.
1362 Utils_Locker lock (&_getenvMutex);
1363 return getenv(name);
1367 * Return env variable as a std::string.
1368 * Return empty string if env variable is not set.
1370 std::string SALOME_ContainerManager::GetenvThreadSafeAsString(const char *name)
1372 char* var = GetenvThreadSafe(name);
1373 return var ? std::string(var) : std::string();
1376 int SALOME_ContainerManager::SystemThreadSafe(const char *command)
1378 Utils_Locker lock (&_systemMutex);
1379 return system(command);
1382 long SALOME_ContainerManager::SystemWithPIDThreadSafe(const std::vector<std::string>& command)
1384 Utils_Locker lock(&_systemMutex);
1385 if(command.size()<1)
1386 throw SALOME_Exception("SystemWithPIDThreadSafe : command is expected to have a length of size 1 at least !");
1388 pid_t pid ( fork() ) ; // spawn a child process, following code is executed in both processes
1390 pid_t pid = -1; //Throw SALOME_Exception on Windows
1392 if ( pid == 0 ) // I'm a child, replace myself with a new ompi-server
1394 std::size_t sz(command.size());
1395 char **args = new char *[sz+1];
1396 for(std::size_t i=0;i<sz;i++)
1397 args[i] = strdup(command[i].c_str());
1399 execvp( command[0].c_str() , args );
1400 std::ostringstream oss;
1401 oss << "Error when launching " << command[0];
1402 throw SALOME_Exception(oss.str().c_str()); // execvp failed
1406 throw SALOME_Exception("fork() failed");
1408 else // I'm a parent
1414 #ifdef WITH_PACO_PARALLEL
1416 //=============================================================================
1418 * Start a suitable PaCO++ Parallel Container in a list of machines.
1419 * \param params Container Parameters required for the container
1420 * \return CORBA container reference.
1422 //=============================================================================
1423 Engines::Container_ptr
1424 SALOME_ContainerManager::StartPaCOPPContainer(const Engines::ContainerParameters& params_const,
1425 std::string resource_selected)
1427 CORBA::Object_var obj;
1428 PaCO::InterfaceManager_var container_proxy;
1429 Engines::Container_ptr ret = Engines::Container::_nil();
1430 Engines::ContainerParameters params(params_const);
1431 params.resource_params.name = CORBA::string_dup(resource_selected.c_str());
1433 // Step 0 : Check parameters
1434 if (!checkPaCOParameters(params, resource_selected))
1436 INFOS("[StartPaCOPPContainer] check parameters failed ! see logs...");
1440 // Step 1 : Starting a new parallel container !
1441 INFOS("[StartPaCOPPContainer] Starting a PaCO++ parallel container");
1442 INFOS("[StartPaCOPPContainer] on resource : " << resource_selected);
1444 // Step 2 : Get a MachineFile for the parallel container
1445 std::string machine_file_name = _resManager->getMachineFile(resource_selected,
1447 params.parallelLib.in());
1449 if (machine_file_name == "")
1451 INFOS("[StartPaCOPPContainer] Machine file generation failed");
1455 // Step 3 : starting parallel container proxy
1456 std::string command_proxy("");
1457 std::string proxy_machine;
1460 command_proxy = BuildCommandToLaunchPaCOProxyContainer(params, machine_file_name, proxy_machine);
1462 catch(const SALOME_Exception & ex)
1464 INFOS("[StartPaCOPPContainer] Exception in BuildCommandToLaunchPaCOContainer");
1468 obj = LaunchPaCOProxyContainer(command_proxy, params, proxy_machine);
1469 if (CORBA::is_nil(obj))
1471 INFOS("[StartPaCOPPContainer] LaunchPaCOContainer for proxy returns NIL !");
1474 container_proxy = PaCO::InterfaceManager::_narrow(obj);
1475 MESSAGE("[StartPaCOPPContainer] PaCO container proxy is launched");
1477 // Step 4 : starting parallel container nodes
1478 std::string command_nodes("");
1479 SALOME_ContainerManager::actual_launch_machine_t nodes_machines;
1482 command_nodes = BuildCommandToLaunchPaCONodeContainer(params, machine_file_name, nodes_machines, proxy_machine);
1484 catch(const SALOME_Exception & ex)
1486 INFOS("[StarPaCOPPContainer] Exception in BuildCommandToLaunchPaCONodeContainer");
1491 std::string container_generic_node_name = std::string(params.container_name.in()) + std::string("Node");
1492 bool result = LaunchPaCONodeContainer(command_nodes, params, container_generic_node_name, nodes_machines);
1495 INFOS("[StarPaCOPPContainer] LaunchPaCONodeContainer failed !");
1496 // Il faut tuer le proxy
1499 Engines::Container_var proxy = Engines::Container::_narrow(container_proxy);
1504 INFOS("[StarPaCOPPContainer] Exception caught from proxy Shutdown...");
1509 // Step 4 : connecting nodes and the proxy to actually create a parallel container
1510 for (int i = 0; i < params.nb_proc; i++)
1512 std::ostringstream tmp;
1514 std::string proc_number = tmp.str();
1515 std::string container_node_name = container_generic_node_name + proc_number;
1517 std::string theNodeMachine(nodes_machines[i]);
1518 std::string containerNameInNS = _NS->BuildContainerNameForNS(container_node_name.c_str(), theNodeMachine.c_str());
1519 obj = _NS->Resolve(containerNameInNS.c_str());
1520 if (CORBA::is_nil(obj))
1522 INFOS("[StarPaCOPPContainer] CONNECTION FAILED From Naming Service !");
1523 INFOS("[StarPaCOPPContainer] Container name is " << containerNameInNS);
1528 MESSAGE("[StarPaCOPPContainer] Deploying node : " << container_node_name);
1529 PaCO::InterfaceParallel_var node = PaCO::InterfaceParallel::_narrow(obj);
1531 MESSAGE("[StarPaCOPPContainer] node " << container_node_name << " is deployed");
1533 catch(CORBA::SystemException& e)
1535 INFOS("[StarPaCOPPContainer] Exception in deploying node : " << containerNameInNS);
1536 INFOS("CORBA::SystemException : " << e);
1539 catch(CORBA::Exception& e)
1541 INFOS("[StarPaCOPPContainer] Exception in deploying node : " << containerNameInNS);
1542 INFOS("CORBA::Exception" << e);
1547 INFOS("[StarPaCOPPContainer] Exception in deploying node : " << containerNameInNS);
1548 INFOS("Unknown exception !");
1553 // Step 5 : starting parallel container
1556 MESSAGE ("[StarPaCOPPContainer] Starting parallel object");
1557 container_proxy->start();
1558 MESSAGE ("[StarPaCOPPContainer] Parallel object is started");
1559 ret = Engines::Container::_narrow(container_proxy);
1561 catch(CORBA::SystemException& e)
1563 INFOS("Caught CORBA::SystemException. : " << e);
1565 catch(PortableServer::POA::ServantAlreadyActive&)
1567 INFOS("Caught CORBA::ServantAlreadyActiveException");
1569 catch(CORBA::Exception&)
1571 INFOS("Caught CORBA::Exception.");
1573 catch(std::exception& exc)
1575 INFOS("Caught std::exception - "<<exc.what());
1579 INFOS("Caught unknown exception.");
1585 SALOME_ContainerManager::BuildCommandToLaunchPaCOProxyContainer(const Engines::ContainerParameters& params,
1586 std::string machine_file_name,
1587 std::string & proxy_hostname)
1589 // In the proxy case, we always launch a Dummy Proxy
1590 std::string exe_name = "SALOME_ParallelContainerProxyDummy";
1591 std::string container_name = params.container_name.in();
1593 // Convert nb_proc in string
1594 std::ostringstream tmp_string;
1595 tmp_string << params.nb_proc;
1596 std::string nb_proc_str = tmp_string.str();
1598 // Get resource definition
1599 ParserResourcesType resource_definition =
1600 _resManager->GetResourceDefinition(params.resource_params.name.in());
1603 std::string hostname;
1604 std::ifstream machine_file(machine_file_name.c_str());
1605 std::getline(machine_file, hostname, ' ');
1606 size_t found = hostname.find('\n');
1607 if (found!=std::string::npos)
1608 hostname.erase(found, 1); // Remove \n
1609 proxy_hostname = hostname;
1610 MESSAGE("[BuildCommandToLaunchPaCOProxyContainer] machine file name extracted is " << hostname);
1613 bool remote_execution = false;
1614 if (hostname != std::string(Kernel_Utils::GetHostname()))
1616 MESSAGE("[BuildCommandToLaunchPaCOProxyContainer] remote machine case detected !");
1617 remote_execution = true;
1621 std::string log_type("");
1622 char * get_val = GetenvThreadSafe("PARALLEL_LOG");
1626 // Generating the command
1627 std::string command_begin("");
1628 std::string command_end("");
1629 std::ostringstream command;
1631 LogConfiguration(log_type, "proxy", container_name, hostname, command_begin, command_end);
1632 command << command_begin;
1634 // Adding connection command
1635 // We can only have a remote execution with
1636 // a SALOME application
1637 if (remote_execution)
1639 ASSERT(GetenvThreadSafe("NSHOST"));
1640 ASSERT(GetenvThreadSafe("NSPORT"));
1642 command << resource_definition.getAccessProtocolTypeStr();
1644 command << resource_definition.UserName;
1645 command << " " << hostname;
1646 command << " " << resource_definition.AppliPath;
1647 command << "/runRemote.sh ";
1648 command << GetenvThreadSafeAsString("NSHOST") << " "; // hostname of CORBA name server
1649 command << GetenvThreadSafeAsString("NSPORT") << " "; // port of CORBA name server
1652 command << exe_name;
1653 command << " " << container_name;
1654 command << " Dummy";
1655 command << " " << hostname;
1656 command << " " << nb_proc_str;
1658 AddOmninamesParams(command);
1661 command << command_end;
1662 MESSAGE("[BuildCommandToLaunchPaCOProxyContainer] Command is: " << command.str());
1664 return command.str();
1668 SALOME_ContainerManager::BuildCommandToLaunchPaCONodeContainer(const Engines::ContainerParameters& params,
1669 const std::string & machine_file_name,
1670 SALOME_ContainerManager::actual_launch_machine_t & vect_machine,
1671 const std::string & proxy_hostname)
1674 std::string exe_name = "SALOME_ParallelContainerNode";
1675 exe_name += params.parallelLib.in();
1676 std::string container_name = params.container_name.in();
1678 // Convert nb_proc in string
1679 std::ostringstream nb_proc_stream;
1680 nb_proc_stream << params.nb_proc;
1682 // Get resource definition
1683 ParserResourcesType resource_definition =
1684 _resManager->GetResourceDefinition(params.resource_params.name.in());
1687 std::string log_type("");
1688 char * get_val = GetenvThreadSafe("PARALLEL_LOG");
1692 // Now the command is different according to paralleLib
1693 std::ostringstream command_nodes;
1694 std::ifstream machine_file(machine_file_name.c_str());
1695 if (std::string(params.parallelLib.in()) == "Dummy")
1697 for (int i= 0; i < params.nb_proc; i++)
1700 std::string hostname;
1701 std::getline(machine_file, hostname);
1702 MESSAGE("[BuildCommandToLaunchPaCONodeContainer] machine file name extracted is " << hostname);
1705 bool remote_execution = false;
1706 if (hostname != std::string(Kernel_Utils::GetHostname()))
1708 MESSAGE("[BuildCommandToLaunchPaCONodeContainer] remote machine case detected !");
1709 remote_execution = true;
1712 // For each node we have a new command
1713 // Generating the command
1714 std::ostringstream command_node_stream;
1715 std::string command_node_begin("");
1716 std::string command_node_end("");
1717 std::ostringstream node_number;
1719 std::string container_node_name = container_name + node_number.str();
1720 LogConfiguration(log_type, "node", container_node_name, hostname, command_node_begin, command_node_end);
1722 // Adding connection command
1723 // We can only have a remote execution with
1724 // a SALOME application
1725 if (remote_execution)
1727 ASSERT(GetenvThreadSafe("NSHOST"));
1728 ASSERT(GetenvThreadSafe("NSPORT"));
1730 command_node_stream << resource_definition.getAccessProtocolTypeStr();
1731 command_node_stream << " -l ";
1732 command_node_stream << resource_definition.UserName;
1733 command_node_stream << " " << hostname;
1734 command_node_stream << " " << resource_definition.AppliPath;
1735 command_node_stream << "/runRemote.sh ";
1736 command_node_stream << GetenvThreadSafeAsString("NSHOST") << " "; // hostname of CORBA name server
1737 command_node_stream << GetenvThreadSafeAsString("NSPORT") << " "; // port of CORBA name server
1740 command_node_stream << exe_name;
1741 command_node_stream << " " << container_name;
1742 command_node_stream << " " << params.parallelLib.in();
1743 command_node_stream << " " << proxy_hostname;
1744 command_node_stream << " " << node_number.str();
1745 command_node_stream << " -";
1746 AddOmninamesParams(command_node_stream);
1748 command_nodes << command_node_begin << command_node_stream.str() << command_node_end;
1749 vect_machine.push_back(hostname);
1753 else if (std::string(params.parallelLib.in()) == "Mpi")
1756 std::string hostname;
1757 std::getline(machine_file, hostname, ' ');
1758 MESSAGE("[BuildCommandToLaunchPaCONodeContainer] machine file name extracted is " << hostname);
1761 bool remote_execution = false;
1762 if (hostname != std::string(Kernel_Utils::GetHostname()))
1764 MESSAGE("[BuildCommandToLaunchPaCONodeContainer] remote machine case detected !");
1765 remote_execution = true;
1768 // In case of Mpi and Remote, we copy machine_file in the applipath
1769 // scp mpi_machine_file user@machine:Path
1770 std::ostringstream command_remote_stream;
1771 std::string::size_type last = machine_file_name.find_last_of("/");
1772 if (last == std::string::npos)
1775 if (resource_definition.Protocol == rsh)
1776 command_remote_stream << "rcp ";
1778 command_remote_stream << "scp ";
1779 command_remote_stream << machine_file_name << " ";
1780 command_remote_stream << resource_definition.UserName << "@";
1781 command_remote_stream << hostname << ":" << resource_definition.AppliPath;
1782 command_remote_stream << "/" << machine_file_name.substr(last+1);
1784 int status = SystemThreadSafe(command_remote_stream.str().c_str());
1787 INFOS("copy of the MPI machine file failed ! - sorry !");
1791 // Generating the command
1792 std::string command_begin("");
1793 std::string command_end("");
1795 LogConfiguration(log_type, "nodes", container_name, hostname, command_begin, command_end);
1796 command_nodes << command_begin;
1798 // Adding connection command
1799 // We can only have a remote execution with
1800 // a SALOME application
1801 if (remote_execution)
1803 ASSERT(GetenvThreadSafe("NSHOST"));
1804 ASSERT(GetenvThreadSafe("NSPORT"));
1806 command_nodes << resource_definition.getAccessProtocolTypeStr();
1807 command_nodes << " -l ";
1808 command_nodes << resource_definition.UserName;
1809 command_nodes << " " << hostname;
1810 command_nodes << " " << resource_definition.AppliPath;
1811 command_nodes << "/runRemote.sh ";
1812 command_nodes << GetenvThreadSafeAsString("NSHOST") << " "; // hostname of CORBA name server
1813 command_nodes << GetenvThreadSafeAsString("NSPORT") << " "; // port of CORBA name server
1816 if (resource_definition.mpi == lam)
1818 command_nodes << "mpiexec -ssi boot ";
1819 command_nodes << "-machinefile " << machine_file_name << " ";
1820 command_nodes << "-n " << params.nb_proc;
1824 command_nodes << "mpirun -np " << params.nb_proc;
1826 command_nodes << " " << exe_name;
1827 command_nodes << " " << container_name;
1828 command_nodes << " " << params.parallelLib.in();
1829 command_nodes << " " << proxy_hostname;
1830 command_nodes << " -";
1831 AddOmninamesParams(command_nodes);
1833 // We don't put hostname, because nodes are registered in the resource of the proxy
1834 for (int i= 0; i < params.nb_proc; i++)
1835 vect_machine.push_back(proxy_hostname);
1837 command_nodes << command_end;
1839 return command_nodes.str();
1843 SALOME_ContainerManager::LogConfiguration(const std::string & log_type,
1844 const std::string & exe_type,
1845 const std::string & container_name,
1846 const std::string & hostname,
1847 std::string & begin,
1850 if(log_type == "xterm")
1852 begin = "xterm -e \"export LD_LIBRARY_PATH=$LD_LIBRARY_PATH; export PATH=$PATH;";
1855 else if(log_type == "xterm_debug")
1857 begin = "xterm -e \"export LD_LIBRARY_PATH=$LD_LIBRARY_PATH; export PATH=$PATH;";
1862 // default into a file...
1863 std::string logFilename = "/tmp/" + container_name + "_" + hostname + "_" + exe_type + "_";
1864 std::string user = GetenvThreadSafeAsString("USER");
1866 user = GetenvThreadSafeAsString("LOGNAME");
1867 logFilename += user + ".log";
1868 end = " > " + logFilename + " 2>&1 & ";
1873 SALOME_ContainerManager::LaunchPaCOProxyContainer(const std::string& command,
1874 const Engines::ContainerParameters& params,
1875 const std::string & hostname)
1877 PaCO::InterfaceManager_ptr container_proxy = PaCO::InterfaceManager::_nil();
1879 MESSAGE("[LaunchPaCOProxyContainer] Launch command");
1880 int status = SystemThreadSafe(command.c_str());
1882 INFOS("[LaunchPaCOProxyContainer] failed : system command status -1");
1883 return container_proxy;
1885 else if (status == 217) {
1886 INFOS("[LaunchPaCOProxyContainer] failed : system command status 217");
1887 return container_proxy;
1890 int count(GetTimeOutToLoaunchServer());
1891 CORBA::Object_var obj = CORBA::Object::_nil();
1892 std::string containerNameInNS = _NS->BuildContainerNameForNS(params.container_name.in(),
1894 MESSAGE("[LaunchParallelContainer] Waiting for Parallel Container proxy : " << containerNameInNS);
1896 while (CORBA::is_nil(obj) && count)
1900 obj = _NS->Resolve(containerNameInNS.c_str());
1905 container_proxy = PaCO::InterfaceManager::_narrow(obj);
1907 catch(CORBA::SystemException& e)
1909 INFOS("[StarPaCOPPContainer] Exception in _narrow after LaunchParallelContainer for proxy !");
1910 INFOS("CORBA::SystemException : " << e);
1911 return container_proxy;
1913 catch(CORBA::Exception& e)
1915 INFOS("[StarPaCOPPContainer] Exception in _narrow after LaunchParallelContainer for proxy !");
1916 INFOS("CORBA::Exception" << e);
1917 return container_proxy;
1921 INFOS("[StarPaCOPPContainer] Exception in _narrow after LaunchParallelContainer for proxy !");
1922 INFOS("Unknown exception !");
1923 return container_proxy;
1925 if (CORBA::is_nil(container_proxy))
1927 INFOS("[StarPaCOPPContainer] PaCO::InterfaceManager::_narrow returns NIL !");
1928 return container_proxy;
1933 //=============================================================================
1934 /*! This method launches the parallel container.
1935 * It will may be placed on the resources manager.
1937 * \param command to launch
1938 * \param container's parameters
1939 * \param name of the container
1941 * \return CORBA container reference
1943 //=============================================================================
1945 SALOME_ContainerManager::LaunchPaCONodeContainer(const std::string& command,
1946 const Engines::ContainerParameters& params,
1947 const std::string& name,
1948 SALOME_ContainerManager::actual_launch_machine_t & vect_machine)
1950 INFOS("[LaunchPaCONodeContainer] Launch command");
1951 int status = SystemThreadSafe(command.c_str());
1953 INFOS("[LaunchPaCONodeContainer] failed : system command status -1");
1956 else if (status == 217) {
1957 INFOS("[LaunchPaCONodeContainer] failed : system command status 217");
1961 INFOS("[LaunchPaCONodeContainer] Waiting for the nodes of the parallel container");
1962 // We are waiting all the nodes
1963 for (int i = 0; i < params.nb_proc; i++)
1965 CORBA::Object_var obj = CORBA::Object::_nil();
1966 std::string theMachine(vect_machine[i]);
1968 std::ostringstream tmp;
1970 std::string proc_number = tmp.str();
1971 std::string container_node_name = name + proc_number;
1972 std::string containerNameInNS = _NS->BuildContainerNameForNS((char*) container_node_name.c_str(), theMachine.c_str());
1973 INFOS("[LaunchPaCONodeContainer] Waiting for Parallel Container node " << containerNameInNS << " on " << theMachine);
1974 int count(GetTimeOutToLoaunchServer());
1975 while (CORBA::is_nil(obj) && count) {
1978 obj = _NS->Resolve(containerNameInNS.c_str());
1980 if (CORBA::is_nil(obj))
1982 INFOS("[LaunchPaCONodeContainer] Launch of node failed (or not found) !");
1991 Engines::Container_ptr
1992 SALOME_ContainerManager::StartPaCOPPContainer(const Engines::ContainerParameters& /*params*/,
1993 std::string /*resource_selected*/)
1995 Engines::Container_ptr ret = Engines::Container::_nil();
1996 INFOS("[StarPaCOPPContainer] is disabled !");
1997 INFOS("[StarPaCOPPContainer] recompile SALOME Kernel to enable PaCO++ parallel extension");
2002 SALOME_ContainerManager::BuildCommandToLaunchPaCOProxyContainer(const Engines::ContainerParameters& /*params*/,
2003 std::string /*machine_file_name*/,
2004 std::string & /*proxy_hostname*/)
2010 SALOME_ContainerManager::BuildCommandToLaunchPaCONodeContainer(const Engines::ContainerParameters& /*params*/,
2011 const std::string & /*machine_file_name*/,
2012 SALOME_ContainerManager::actual_launch_machine_t & /*vect_machine*/,
2013 const std::string & /*proxy_hostname*/)
2018 SALOME_ContainerManager::LogConfiguration(const std::string & /*log_type*/,
2019 const std::string & /*exe_type*/,
2020 const std::string & /*container_name*/,
2021 const std::string & /*hostname*/,
2022 std::string & /*begin*/,
2023 std::string & /*end*/)
2028 SALOME_ContainerManager::LaunchPaCOProxyContainer(const std::string& /*command*/,
2029 const Engines::ContainerParameters& /*params*/,
2030 const std::string& /*hostname*/)
2032 CORBA::Object_ptr ret = CORBA::Object::_nil();
2037 SALOME_ContainerManager::LaunchPaCONodeContainer(const std::string& /*command*/,
2038 const Engines::ContainerParameters& /*params*/,
2039 const std::string& /*name*/,
2040 SALOME_ContainerManager::actual_launch_machine_t & /*vect_machine*/)