1 // Copyright (C) 2007-2020 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 =
103 Engines::ContainerManager::_narrow(obj);
105 _NS->Register(refContMan,_ContainerManagerNameInNS);
106 _isAppliSalomeDefined = (GetenvThreadSafe("APPLI") != 0);
111 // the urifile name depends on pid of the process
112 std::stringstream urifile;
113 urifile << GetenvThreadSafeAsString("HOME") << "/.urifile_" << getpid();
114 setenv("OMPI_URI_FILE",urifile.str().c_str(),1);
115 if( GetenvThreadSafe("OMPI_URI_FILE") != NULL ){
116 // Linux specific code
117 pid_t pid = fork(); // spawn a child process, following code is executed in both processes
118 if ( pid == 0 ) // I'm a child, replace myself with a new ompi-server
120 std::string uriarg = GetenvThreadSafeAsString("OMPI_URI_FILE");
121 execlp( "ompi-server", "ompi-server", "-r", uriarg.c_str(), NULL );
122 throw SALOME_Exception("Error when launching ompi-server"); // execlp failed
126 throw SALOME_Exception("fork() failed");
130 //wait(NULL); // wait(?) for a child end
131 _pid_mpiServer = pid;
136 // Linux specific code
137 pid_t pid = fork(); // spawn a child process, following code is executed in both processes
138 if ( pid == 0 ) // I'm a child, replace myself with a new hydra_nameserver
140 execlp( "hydra_nameserver", "hydra_nameserver", NULL );
141 throw SALOME_Exception("Error when launching hydra_nameserver"); // execlp failed
145 throw SALOME_Exception("fork() failed");
150 _pid_mpiServer = pid;
155 MESSAGE("constructor end");
158 //=============================================================================
162 //=============================================================================
164 SALOME_ContainerManager::~SALOME_ContainerManager()
166 MESSAGE("destructor");
170 if( GetenvThreadSafe("OMPI_URI_FILE") != NULL ){
171 // kill my ompi-server
172 if( kill(_pid_mpiServer,SIGTERM) != 0 )
173 throw SALOME_Exception("Error when killing ompi-server");
175 int status=SystemThreadSafe("rm -f ${OMPI_URI_FILE}");
177 throw SALOME_Exception("Error when removing urifile");
180 // kill my hydra_nameserver
181 if(_pid_mpiServer > -1)
182 if( kill(_pid_mpiServer,SIGTERM) != 0 )
183 throw SALOME_Exception("Error when killing hydra_nameserver");
188 //=============================================================================
189 //! shutdown all the containers, then the ContainerManager servant
192 //=============================================================================
194 void SALOME_ContainerManager::Shutdown()
197 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");
213 SALOME::Session_var session = SALOME::Session::_nil();
215 CORBA::Object_var objS = _NS->Resolve("/Kernel/Session");
216 if (!CORBA::is_nil(objS))
218 session = SALOME::Session::_narrow(objS);
219 if (!CORBA::is_nil(session))
220 pid = session->getPID();
224 isOK = _NS->Change_Directory("/Containers");
226 std::vector<std::string> vec = _NS->list_directory_recurs();
227 std::list<std::string> lstCont;
228 for(std::vector<std::string>::iterator iter = vec.begin();iter!=vec.end();iter++)
231 CORBA::Object_var obj=_NS->Resolve((*iter).c_str());
234 Engines::Container_var cont=Engines::Container::_narrow(obj);
235 if(!CORBA::is_nil(cont) && pid != cont->getPID())
236 lstCont.push_back((*iter));
238 catch(const CORBA::Exception& e)
240 // ignore this entry and continue
243 MESSAGE("Container list: ");
244 for(std::list<std::string>::iterator iter=lstCont.begin();iter!=lstCont.end();iter++){
247 for(std::list<std::string>::iterator iter=lstCont.begin();iter!=lstCont.end();iter++)
252 CORBA::Object_var obj=_NS->Resolve((*iter).c_str());
253 Engines::Container_var cont=Engines::Container::_narrow(obj);
254 if(!CORBA::is_nil(cont))
256 MESSAGE("ShutdownContainers: " << (*iter));
260 MESSAGE("ShutdownContainers: no container ref for " << (*iter));
262 catch(CORBA::SystemException& e)
264 INFOS("CORBA::SystemException ignored : " << e);
266 catch(CORBA::Exception&)
268 INFOS("CORBA::Exception ignored.");
272 INFOS("Unknown exception ignored.");
278 //=============================================================================
279 //! Give a suitable Container given constraints
281 * \param params Container Parameters required for the container
282 * \return the container or nil
284 //=============================================================================
285 Engines::Container_ptr SALOME_ContainerManager::GiveContainer(const Engines::ContainerParameters& params)
287 std::string machFile;
288 Engines::Container_ptr ret(Engines::Container::_nil());
290 // Step 0: Default mode is start
291 Engines::ContainerParameters local_params(params);
292 if (std::string(local_params.mode.in()) == "")
293 local_params.mode = CORBA::string_dup("start");
294 std::string mode = local_params.mode.in();
295 MESSAGE("[GiveContainer] starting with mode: " << mode);
297 // Step 1: Find Container for find and findorstart mode
298 if (mode == "find" || mode == "findorstart")
300 ret = FindContainer(params, params.resource_params.resList);
301 if(!CORBA::is_nil(ret))
307 MESSAGE("[GiveContainer] no container found");
317 // Step 2: Get all possibleResources from the parameters
318 // Consider only resources that can run containers
319 resourceParams resource_params = resourceParameters_CORBAtoCPP(local_params.resource_params);
320 resource_params.can_run_containers = true;
321 std::vector<std::string> possibleResources = _resManager->GetFittingResources(resource_params);
322 MESSAGE("[GiveContainer] - length of possible resources " << possibleResources.size());
323 std::vector<std::string> local_resources;
325 // Step 3: if mode is "get" keep only machines with existing containers
328 for(unsigned int i=0; i < possibleResources.size(); i++)
330 Engines::Container_ptr cont = FindContainer(params, possibleResources[i]);
333 if(!cont->_non_existent())
334 local_resources.push_back(possibleResources[i]);
336 catch(CORBA::Exception&) {}
339 // if local_resources is empty, we cannot give a container
340 if (local_resources.size() == 0)
342 MESSAGE("[GiveContainer] cannot find a container for mode get");
347 local_resources = possibleResources;
349 // Step 4: select the resource where to get/start the container
350 bool resource_available = true;
351 std::string resource_selected;
352 std::vector<std::string> resources = local_resources;
353 while (resource_available)
355 if (resources.size() == 0)
356 resource_available = false;
361 resource_selected = _resManager->Find(params.resource_params.policy.in(), resources);
362 // Remove resource_selected from vector
363 std::vector<std::string>::iterator it;
364 for (it=resources.begin() ; it < resources.end(); it++ )
365 if (*it == resource_selected)
371 catch(const SALOME_Exception &ex)
373 MESSAGE("[GiveContainer] Exception in ResourceManager find !: " << ex.what());
376 MESSAGE("[GiveContainer] Resource selected is: " << resource_selected);
378 // Step 5: Create container name
379 ParserResourcesType resource_definition = _resManager->GetResourceDefinition(resource_selected);
380 std::string hostname(resource_definition.HostName);
381 std::string containerNameInNS;
384 if ( params.nb_proc <= 0 )
387 nbproc = params.nb_proc;
390 if( GetenvThreadSafe("LIBBATCH_NODEFILE") != NULL )
391 machFile = machinesFile(nbproc);
393 catch(const SALOME_Exception & ex)
395 std::string err_msg = ex.what();
396 err_msg += params.container_name;
397 INFOS(err_msg.c_str());
400 // A mpi parallel container register on zero node in NS
401 std::string mpiZeroNode = GetMPIZeroNode(resource_selected,machFile).c_str();
402 containerNameInNS = _NS->BuildContainerNameForNS(params, mpiZeroNode.c_str());
405 containerNameInNS = _NS->BuildContainerNameForNS(params, hostname.c_str());
406 MESSAGE("[GiveContainer] Container name in the naming service: " << containerNameInNS);
408 // Step 6: check if the name exists in naming service
409 //if params.mode == "getorstart" or "get" use the existing container
410 //if params.mode == "start" shutdown the existing container before launching a new one with that name
412 { // critical section
413 Utils_Locker lock (&_giveContainerMutex1);
414 CORBA::Object_var obj = _NS->Resolve(containerNameInNS.c_str());
415 if (!CORBA::is_nil(obj))
419 Engines::Container_var cont=Engines::Container::_narrow(obj);
420 if(!cont->_non_existent())
422 if(std::string(params.mode.in())=="getorstart" || std::string(params.mode.in())=="get"){
423 return cont._retn(); /* the container exists and params.mode is getorstart or get use it*/
427 INFOS("[GiveContainer] A container is already registered with the name: " << containerNameInNS << ", shutdown the existing container");
428 cont->Shutdown(); // shutdown the registered container if it exists
432 catch(CORBA::Exception&)
434 INFOS("[GiveContainer] CORBA::Exception ignored when trying to get the container - we start a new one");
437 } // end critical section
438 Engines::Container_var cont = LaunchContainer(params, resource_selected, hostname, machFile, containerNameInNS);
439 if (!CORBA::is_nil(cont))
441 INFOS("[GiveContainer] container " << containerNameInNS << " launched");
446 INFOS("[GiveContainer] Failed to launch container on resource " << resource_selected);
451 // We were not able to launch the container
452 INFOS("[GiveContainer] Cannot launch the container on the following selected resources:")
453 std::vector<std::string>::iterator it;
454 for (it=local_resources.begin() ; it < local_resources.end(); it++ )
455 INFOS("[GiveContainer] " << *it)
459 Engines::Container_ptr
460 SALOME_ContainerManager::LaunchContainer(const Engines::ContainerParameters& params,
461 const std::string & resource_selected,
462 const std::string & hostname,
463 const std::string & machFile,
464 const std::string & containerNameInNS)
466 std::string user,command,logFilename,tmpFileName;
468 Engines::Container_ptr ret(Engines::Container::_nil());
469 {//start of critical section
470 Utils_Locker lock (&_giveContainerMutex1);
471 // Step 1: type of container: PaCO, Exe, Mpi or Classic
472 // Mpi already tested in step 5, specific code on BuildCommandToLaunch Local/Remote Container methods
473 // TODO -> separates Mpi from Classic/Exe
475 std::string container_exe = "SALOME_Container"; // Classic container
476 Engines::ContainerParameters local_params(params);
480 CORBA::String_var container_exe_tmp;
481 CORBA::Object_var obj = _NS->Resolve("/Kernel/ModulCatalog");
482 SALOME_ModuleCatalog::ModuleCatalog_var Catalog = SALOME_ModuleCatalog::ModuleCatalog::_narrow(obj) ;
483 if (CORBA::is_nil (Catalog))
485 INFOS("[GiveContainer] Module Catalog is not found -> cannot launch a container");
488 // Loop through component list
489 for(unsigned int i=0; i < local_params.resource_params.componentList.length(); i++)
491 const char* compoi = local_params.resource_params.componentList[i];
492 SALOME_ModuleCatalog::Acomponent_var compoInfo = Catalog->GetComponent(compoi);
493 if (CORBA::is_nil (compoInfo))
497 SALOME_ModuleCatalog::ImplType impl=compoInfo->implementation_type();
498 container_exe_tmp=compoInfo->implementation_name();
499 if(impl==SALOME_ModuleCatalog::CEXE)
503 INFOS("ContainerManager Error: you can't have 2 CEXE component in the same container" );
504 return Engines::Container::_nil();
506 MESSAGE("[GiveContainer] Exe container found !: " << container_exe_tmp);
507 container_exe = container_exe_tmp.in();
512 catch (ServiceUnreachable&)
514 INFOS("Caught exception: Naming Service Unreachable");
519 INFOS("Caught unknown exception.");
523 // Step 2: test resource
524 // Only if an application directory is set
525 if(hostname != Kernel_Utils::GetHostname() && _isAppliSalomeDefined)
528 const ParserResourcesType resInfo(_resManager->GetResourceDefinition(resource_selected));
529 std::string command = getCommandToRunRemoteProcess(resInfo.Protocol, resInfo.HostName,
530 resInfo.UserName, resInfo.AppliPath);
532 // Launch remote command
533 command += " \"ls /tmp >/dev/null 2>&1\"";
534 // Anthony : command is NO MORE launched to improve dramatically time to launch containers
538 // Error on resource - cannot launch commands
539 INFOS("[LaunchContainer] Cannot launch commands on machine " << hostname);
540 INFOS("[LaunchContainer] Command was " << command);
542 INFOS("[LaunchContainer] Command status is " << WEXITSTATUS(status));
544 return Engines::Container::_nil();
548 // Step 3: start a new container
549 // Check if a PaCO container
551 if (std::string(local_params.parallelLib.in()) != "")
553 ret = StartPaCOPPContainer(params, resource_selected);
556 // Other type of containers...
557 MESSAGE("[GiveContainer] Try to launch a new container on " << resource_selected);
558 // if a parallel container is launched in batch job, command is: "mpirun -np nbproc -machinefile nodesfile SALOME_MPIContainer"
559 if( GetenvThreadSafe("LIBBATCH_NODEFILE") != NULL && params.isMPI )
560 command = BuildCommandToLaunchLocalContainer(params, machFile, container_exe, tmpFileName);
561 // if a container is launched on localhost, command is "SALOME_Container" or "mpirun -np nbproc SALOME_MPIContainer"
562 else if(hostname == Kernel_Utils::GetHostname())
563 command = BuildCommandToLaunchLocalContainer(params, machFile, container_exe, tmpFileName);
564 // if a container is launched in remote mode, command is "ssh resource_selected SALOME_Container" or "ssh resource_selected mpirun -np nbproc SALOME_MPIContainer"
566 command = BuildCommandToLaunchRemoteContainer(resource_selected, params, container_exe);
568 //redirect stdout and stderr in a file
570 logFilename=GetenvThreadSafeAsString("TEMP");
572 user = GetenvThreadSafeAsString( "USERNAME" );
574 user = GetenvThreadSafeAsString( "USER" );
576 user = GetenvThreadSafeAsString( "LOGNAME" );
578 char* val = GetenvThreadSafe("SALOME_TMP_DIR");
581 struct stat file_info;
582 stat(val, &file_info);
583 bool is_dir = S_ISDIR(file_info.st_mode);
584 if (is_dir)logFilename=val;
585 else std::cerr << "SALOME_TMP_DIR environment variable is not a directory use /tmp instead" << std::endl;
589 logFilename += _NS->ContainerName(params)+"_"+ resource_selected +"_"+user;
590 std::ostringstream tmp;
591 tmp << "_" << getpid();
592 logFilename += tmp.str();
593 logFilename += ".log" ;
594 command += " > " + logFilename + " 2>&1";
595 MakeTheCommandToBeLaunchedASync(command);
597 // launch container with a system call
598 status=SystemThreadSafe(command.c_str());
599 }//end of critical of section
603 INFOS("[LaunchContainer] command failed (system command status -1): " << command);
604 RmTmpFile(tmpFileName); // command file can be removed here
605 return Engines::Container::_nil();
607 else if (status == 217)
609 INFOS("[LaunchContainer] command failed (system command status 217): " << command);
610 RmTmpFile(tmpFileName); // command file can be removed here
611 return Engines::Container::_nil();
615 // Step 4: Wait for the container
616 int count(GetTimeOutToLoaunchServer());
617 INFOS("[GiveContainer] waiting " << count << " second steps container " << containerNameInNS);
618 while (CORBA::is_nil(ret) && count)
622 MESSAGE("[GiveContainer] step " << count << " Waiting for container on " << resource_selected);
623 CORBA::Object_var obj(_NS->Resolve(containerNameInNS.c_str()));
624 ret=Engines::Container::_narrow(obj);
626 if (CORBA::is_nil(ret))
628 INFOS("[GiveContainer] was not able to launch container " << containerNameInNS);
632 // Setting log file name
633 logFilename=":"+logFilename;
634 logFilename="@"+Kernel_Utils::GetHostname()+logFilename;//threadsafe
635 logFilename=user+logFilename;
636 ret->logfilename(logFilename.c_str());
637 RmTmpFile(tmpFileName); // command file can be removed here
643 //=============================================================================
644 //! Find a container given constraints (params) on a list of machines (possibleComputers)
645 //! agy : this method is ThreadSafe
649 //=============================================================================
651 Engines::Container_ptr SALOME_ContainerManager::FindContainer(const Engines::ContainerParameters& params, const Engines::ResourceList& possibleResources)
653 MESSAGE("[FindContainer] FindContainer on " << possibleResources.length() << " resources");
654 for(unsigned int i=0; i < possibleResources.length();i++)
656 Engines::Container_ptr cont = FindContainer(params, possibleResources[i].in());
657 if(!CORBA::is_nil(cont))
660 MESSAGE("[FindContainer] no container found");
661 return Engines::Container::_nil();
664 //=============================================================================
665 //! Find a container given constraints (params) on a machine (theMachine)
666 //! agy : this method is ThreadSafe
670 //=============================================================================
672 Engines::Container_ptr
673 SALOME_ContainerManager::FindContainer(const Engines::ContainerParameters& params, const std::string& resource)
675 ParserResourcesType resource_definition = _resManager->GetResourceDefinition(resource);
676 std::string hostname(resource_definition.HostName);
677 std::string containerNameInNS(_NS->BuildContainerNameForNS(params, hostname.c_str()));
678 MESSAGE("[FindContainer] Try to find a container " << containerNameInNS << " on resource " << resource);
679 CORBA::Object_var obj = _NS->Resolve(containerNameInNS.c_str());
682 if(obj->_non_existent())
683 return Engines::Container::_nil();
685 return Engines::Container::_narrow(obj);
687 catch(const CORBA::Exception& e)
689 return Engines::Container::_nil();
694 bool isPythonContainer(const char* ContainerName);
696 //=============================================================================
698 * This is no longer valid (C++ container are also python containers)
700 //=============================================================================
701 bool isPythonContainer(const char* ContainerName)
703 return false; // VSR 02/08/2013: Python containers are no more supported
705 int len = strlen(ContainerName);
708 if (strcmp(ContainerName + len - 2, "Py") == 0)
714 //=============================================================================
716 * Builds the script to be launched
718 * If SALOME Application not defined ($APPLI),
719 * see BuildTempFileToLaunchRemoteContainer()
721 * Else rely on distant configuration. Command is under the form (example):
722 * ssh user@machine distantPath/runRemote.sh hostNS portNS WORKINGDIR workingdir \
723 * SALOME_Container containerName &"
725 * - where user is omitted if not specified in CatalogResources,
726 * - where distant path is always relative to user@machine $HOME, and
727 * equal to $APPLI if not specified in CatalogResources,
728 * - where hostNS is the hostname of CORBA naming server (set by scripts to
729 * use to launch SALOME and servers in $APPLI: runAppli.sh, runRemote.sh)
730 * - where portNS is the port used by CORBA naming server (set by scripts to
731 * use to launch SALOME and servers in $APPLI: runAppli.sh, runRemote.sh)
732 * - where workingdir is the requested working directory for the container.
733 * If WORKINGDIR (and workingdir) is not present the working dir will be $HOME
735 //=============================================================================
738 SALOME_ContainerManager::BuildCommandToLaunchRemoteContainer(const std::string& resource_name, const Engines::ContainerParameters& params, const std::string& container_exe) const
740 std::string command,tmpFileName;
741 if (!_isAppliSalomeDefined)
742 command = BuildTempFileToLaunchRemoteContainer(resource_name, params, tmpFileName);
746 const ParserResourcesType resInfo(_resManager->GetResourceDefinition(resource_name));
750 if ( params.nb_proc <= 0 )
753 nbproc = params.nb_proc;
756 std::string wdir = params.workingdir.in();
758 // "ssh -l user machine distantPath/runRemote.sh hostNS portNS WORKINGDIR workingdir \
759 // SALOME_Container containerName -ORBInitRef NameService=IOR:01000..."
761 // "ssh -l user machine distantLauncher remote -p hostNS -m portNS -d dir
762 // -- SALOME_Container contName -ORBInitRef NameService=IOR:01000..."
763 command = getCommandToRunRemoteProcess(resInfo.Protocol, resInfo.HostName,
764 resInfo.UserName, resInfo.AppliPath,
769 command += " mpirun -np ";
770 std::ostringstream o;
774 command += "-x PATH,LD_LIBRARY_PATH,OMNIORB_CONFIG,SALOME_trace ";
775 #elif defined(OPEN_MPI)
776 if( GetenvThreadSafe("OMPI_URI_FILE") == NULL )
777 command += "-x PATH -x LD_LIBRARY_PATH -x OMNIORB_CONFIG -x SALOME_trace";
779 command += "-x PATH -x LD_LIBRARY_PATH -x OMNIORB_CONFIG -x SALOME_trace -ompi-server file:";
780 command += GetenvThreadSafeAsString("OMPI_URI_FILE");
783 command += "-nameserver " + Kernel_Utils::GetHostname();
785 command += " SALOME_MPIContainer ";
788 command += " " +container_exe+ " ";
790 command += _NS->ContainerName(params);
792 AddOmninamesParams(command);
794 MESSAGE("command =" << command);
800 //=============================================================================
802 * builds the command to be launched.
804 //=============================================================================
805 std::string SALOME_ContainerManager::BuildCommandToLaunchLocalContainer(const Engines::ContainerParameters& params, const std::string& machinesFile, const std::string& container_exe, std::string& tmpFileName) const
807 tmpFileName = BuildTemporaryFileName();
811 std::ostringstream o;
817 if ( params.nb_proc <= 0 )
820 nbproc = params.nb_proc;
824 if( GetenvThreadSafe("LIBBATCH_NODEFILE") != NULL )
825 o << "-machinefile " << machinesFile << " ";
828 o << "-x PATH,LD_LIBRARY_PATH,OMNIORB_CONFIG,SALOME_trace ";
829 #elif defined(OPEN_MPI)
830 if( GetenvThreadSafe("OMPI_URI_FILE") == NULL )
831 o << "-x PATH -x LD_LIBRARY_PATH -x OMNIORB_CONFIG -x SALOME_trace";
834 o << "-x PATH -x LD_LIBRARY_PATH -x OMNIORB_CONFIG -x SALOME_trace -ompi-server file:";
835 o << GetenvThreadSafeAsString("OMPI_URI_FILE");
838 o << "-nameserver " + Kernel_Utils::GetHostname();
841 if (isPythonContainer(params.container_name))
842 o << " pyMPI SALOME_ContainerPy.py ";
844 o << " SALOME_MPIContainer ";
849 std::string wdir=params.workingdir.in();
852 // a working directory is requested
853 if(wdir == "$TEMPDIR")
855 // a new temporary directory is requested
856 std::string dir = Kernel_Utils::GetTmpDir();
858 o << "cd /d " << dir << std::endl;
860 o << "cd " << dir << ";";
866 // a permanent directory is requested use it or create it
868 o << "mkdir " + wdir << std::endl;
869 o << "cd /D " + wdir << std::endl;
871 o << "mkdir -p " << wdir << " && cd " << wdir + ";";
876 if (isPythonContainer(params.container_name))
877 o << "SALOME_ContainerPy.py ";
879 o << container_exe + " ";
883 o << _NS->ContainerName(params);
885 AddOmninamesParams(o);
887 std::ofstream command_file( tmpFileName.c_str() );
888 command_file << o.str();
889 command_file.close();
892 chmod(tmpFileName.c_str(), 0x1ED);
894 command = tmpFileName;
896 MESSAGE("Command is file ... " << command);
897 MESSAGE("Command is ... " << o.str());
902 //=============================================================================
904 * removes the generated temporary file in case of a remote launch.
905 * This method is thread safe
907 //=============================================================================
909 void SALOME_ContainerManager::RmTmpFile(std::string& tmpFileName)
911 int length = tmpFileName.size();
915 std::string command = "del /F ";
917 std::string command = "rm ";
920 command += tmpFileName.substr(0, length - 3 );
922 command += tmpFileName;
924 SystemThreadSafe(command.c_str());
925 //if dir is empty - remove it
926 std::string tmp_dir = Kernel_Utils::GetDirByPath( tmpFileName );
927 if ( Kernel_Utils::IsEmptyDir( tmp_dir ) )
930 command = "del /F " + tmp_dir;
932 command = "rmdir " + tmp_dir;
934 SystemThreadSafe(command.c_str());
939 //=============================================================================
941 * add to command all options relative to naming service.
943 //=============================================================================
945 void SALOME_ContainerManager::AddOmninamesParams(std::string& command) const
947 std::ostringstream oss;
948 AddOmninamesParams(oss);
952 //=============================================================================
954 * add to command all options relative to naming service.
956 //=============================================================================
958 void SALOME_ContainerManager::AddOmninamesParams(std::ostream& fileStream) const
960 AddOmninamesParams(fileStream,_NS);
963 //=============================================================================
965 * add to command all options relative to naming service.
967 //=============================================================================
969 void SALOME_ContainerManager::AddOmninamesParams(std::ostream& fileStream, SALOME_NamingService *ns)
971 CORBA::String_var iorstr(ns->getIORaddr());
972 fileStream << "ORBInitRef NameService=";
973 fileStream << iorstr;
976 void SALOME_ContainerManager::MakeTheCommandToBeLaunchedASync(std::string& command)
979 command = "%PYTHONBIN% -c \"import subprocess ; subprocess.Popen(r'" + command + "').pid\"";
985 int SALOME_ContainerManager::GetTimeOutToLoaunchServer()
987 int count(TIME_OUT_TO_LAUNCH_CONT);
988 if (GetenvThreadSafe("TIMEOUT_TO_LAUNCH_CONTAINER") != 0)
990 std::string new_count_str(GetenvThreadSafeAsString("TIMEOUT_TO_LAUNCH_CONTAINER"));
992 std::istringstream ss(new_count_str);
993 if (!(ss >> new_count))
995 INFOS("[LaunchContainer] TIMEOUT_TO_LAUNCH_CONTAINER should be an int");
1003 void SALOME_ContainerManager::SleepInSecond(int ellapseTimeInSecond)
1006 sleep( ellapseTimeInSecond ) ;
1008 int timeInMS(1000*ellapseTimeInSecond);
1013 //=============================================================================
1015 * generate a file name in /tmp directory
1017 //=============================================================================
1019 std::string SALOME_ContainerManager::BuildTemporaryFileName()
1021 //build more complex file name to support multiple salome session
1022 std::string aFileName = Kernel_Utils::GetTmpFileName();
1023 std::ostringstream str_pid;
1024 str_pid << ::getpid();
1025 aFileName = aFileName + "-" + str_pid.str();
1029 aFileName += ".bat";
1034 //=============================================================================
1036 * Builds in a temporary file the script to be launched.
1038 * Used if SALOME Application ($APPLI) is not defined.
1039 * The command is build with data from CatalogResources, in which every path
1040 * used on remote computer must be defined.
1042 //=============================================================================
1044 std::string SALOME_ContainerManager::BuildTempFileToLaunchRemoteContainer (const std::string& resource_name, const Engines::ContainerParameters& params, std::string& tmpFileName) const
1048 tmpFileName = BuildTemporaryFileName();
1049 std::ofstream tempOutputFile;
1050 tempOutputFile.open(tmpFileName.c_str(), std::ofstream::out );
1051 const ParserResourcesType resInfo(_resManager->GetResourceDefinition(resource_name));
1052 tempOutputFile << "#! /bin/sh" << std::endl;
1056 tempOutputFile << "export SALOME_trace=local" << std::endl; // mkr : 27.11.2006 : PAL13967 - Distributed supervision graphs - Problem with "SALOME_trace"
1057 //tempOutputFile << "source " << resInfo.PreReqFilePath << endl;
1063 tempOutputFile << "mpirun -np ";
1066 if ( params.nb_proc <= 0 )
1069 nbproc = params.nb_proc;
1071 std::ostringstream o;
1073 tempOutputFile << nbproc << " ";
1075 tempOutputFile << "-x PATH,LD_LIBRARY_PATH,OMNIORB_CONFIG,SALOME_trace ";
1076 #elif defined(OPEN_MPI)
1077 if( GetenvThreadSafe("OMPI_URI_FILE") == NULL )
1078 tempOutputFile << "-x PATH -x LD_LIBRARY_PATH -x OMNIORB_CONFIG -x SALOME_trace";
1080 tempOutputFile << "-x PATH -x LD_LIBRARY_PATH -x OMNIORB_CONFIG -x SALOME_trace -ompi-server file:";
1081 tempOutputFile << GetenvThreadSafeAsString("OMPI_URI_FILE");
1083 #elif defined(MPICH)
1084 tempOutputFile << "-nameserver " + Kernel_Utils::GetHostname();
1088 tempOutputFile << GetenvThreadSafeAsString("KERNEL_ROOT_DIR") << "/bin/salome/";
1092 if (isPythonContainer(params.container_name))
1093 tempOutputFile << " pyMPI SALOME_ContainerPy.py ";
1095 tempOutputFile << " SALOME_MPIContainer ";
1100 if (isPythonContainer(params.container_name))
1101 tempOutputFile << "SALOME_ContainerPy.py ";
1103 tempOutputFile << "SALOME_Container ";
1106 tempOutputFile << _NS->ContainerName(params) << " -";
1107 AddOmninamesParams(tempOutputFile);
1108 tempOutputFile << " &" << std::endl;
1109 tempOutputFile.flush();
1110 tempOutputFile.close();
1112 chmod(tmpFileName.c_str(), 0x1ED);
1115 // --- Build command
1117 std::string command;
1119 if (resInfo.Protocol == rsh)
1122 std::string commandRcp = "rcp ";
1123 commandRcp += tmpFileName;
1125 commandRcp += resInfo.HostName;
1127 commandRcp += tmpFileName;
1128 status = SystemThreadSafe(commandRcp.c_str());
1131 else if (resInfo.Protocol == ssh)
1134 std::string commandRcp = "scp ";
1135 commandRcp += tmpFileName;
1137 commandRcp += resInfo.HostName;
1139 commandRcp += tmpFileName;
1140 status = SystemThreadSafe(commandRcp.c_str());
1143 else if (resInfo.Protocol == srun)
1145 command = "srun -n 1 -N 1 -s --mem-per-cpu=0 --nodelist=";
1146 std::string commandRcp = "rcp ";
1147 commandRcp += tmpFileName;
1149 commandRcp += resInfo.HostName;
1151 commandRcp += tmpFileName;
1152 status = SystemThreadSafe(commandRcp.c_str());
1155 throw SALOME_Exception("Unknown protocol");
1158 throw SALOME_Exception("Error of connection on remote host");
1160 command += resInfo.HostName;
1162 command += tmpFileName;
1170 std::string SALOME_ContainerManager::GetMPIZeroNode(const std::string machine, const std::string machinesFile) const
1173 std::string zeronode;
1174 std::string command;
1175 std::string tmpFile = BuildTemporaryFileName();
1176 const ParserResourcesType resInfo(_resManager->GetResourceDefinition(machine));
1178 if(resInfo.Protocol == sh)
1180 return resInfo.HostName;
1183 if( GetenvThreadSafe("LIBBATCH_NODEFILE") == NULL )
1185 if (_isAppliSalomeDefined)
1187 command = getCommandToRunRemoteProcess(resInfo.Protocol, resInfo.HostName,
1188 resInfo.UserName, resInfo.AppliPath);
1189 command += " mpirun -np 1 hostname -s > " + tmpFile;
1192 command = "mpirun -np 1 hostname -s > " + tmpFile;
1195 command = "mpirun -np 1 -machinefile " + machinesFile + " hostname -s > " + tmpFile;
1197 status = SystemThreadSafe(command.c_str());
1199 std::ifstream fp(tmpFile.c_str(),std::ios::in);
1200 while(fp >> zeronode);
1208 std::string SALOME_ContainerManager::machinesFile(const int nbproc)
1211 std::string nodesFile = GetenvThreadSafeAsString("LIBBATCH_NODEFILE");
1212 std::string machinesFile = Kernel_Utils::GetTmpFileName();
1213 std::ifstream fpi(nodesFile.c_str(),std::ios::in);
1214 std::ofstream fpo(machinesFile.c_str(),std::ios::out);
1216 _numInstanceMutex.lock();
1218 for(int i=0;i<_nbprocUsed;i++)
1221 for(int i=0;i<nbproc;i++)
1223 fpo << tmp << std::endl;
1225 throw SALOME_Exception("You need more processors than batch session have allocated for you! Unable to launch the mpi container: ");
1227 _nbprocUsed += nbproc;
1231 _numInstanceMutex.unlock();
1233 return machinesFile;
1237 std::string SALOME_ContainerManager::getCommandToRunRemoteProcess(AccessProtocolType protocol,
1238 const std::string & hostname,
1239 const std::string & username,
1240 const std::string & applipath,
1241 const std::string & workdir)
1243 std::ostringstream command;
1250 command << "-l " << username << " ";
1252 command << hostname << " ";
1258 command << "-l " << username << " ";
1260 command << hostname << " ";
1263 // no need to redefine the user with srun, the job user is taken by default
1264 // (note: for srun, user id can be specified with " --uid=<user>")
1265 command << "srun -n 1 -N 1 -s --mem-per-cpu=0 --nodelist=" << hostname << " ";
1268 command << "pbsdsh -o -h " << hostname << " ";
1271 command << "blaunch -no-shell " << hostname << " ";
1274 throw SALOME_Exception("Unknown protocol");
1277 std::string remoteapplipath;
1279 remoteapplipath = GetenvThreadSafeAsString("APPLI");
1281 remoteapplipath = applipath;
1283 ASSERT(GetenvThreadSafe("NSHOST"));
1284 ASSERT(GetenvThreadSafe("NSPORT"));
1286 // $APPLI points either to an application directory, or to a salome launcher file
1287 // we prepare the remote command according to the case
1288 struct stat statbuf;
1289 if (stat(GetenvThreadSafe("APPLI"), &statbuf) ==0 && S_ISREG(statbuf.st_mode))
1291 // if $APPLI is a regular file, we asume it's a salome Launcher
1292 // generate a command with a salome launcher
1293 command << remoteapplipath
1296 << GetenvThreadSafeAsString("NSHOST") // hostname of CORBA name server
1298 << GetenvThreadSafeAsString("NSPORT"); // port of CORBA name server
1300 command << "-d " << workdir;
1303 else // we assume it's a salome application directory
1305 // generate a command with runRemote.sh
1306 command << remoteapplipath;
1307 command << "/runRemote.sh ";
1308 command << GetenvThreadSafeAsString("NSHOST"); // hostname of CORBA name server
1310 command << GetenvThreadSafeAsString("NSPORT"); // port of CORBA name server
1313 command << " WORKINGDIR ";
1315 if(workdir == "$TEMPDIR")
1316 command << "\\$TEMPDIR";
1318 command << workdir; // requested working directory
1323 return command.str();
1327 SALOME_ContainerManager::checkPaCOParameters(Engines::ContainerParameters & params, std::string resource_selected)
1331 // Step 1 : check ContainerParameters
1332 // Check container_name, has to be defined
1333 if (std::string(params.container_name.in()) == "")
1335 INFOS("[checkPaCOParameters] You must define a container_name to launch a PaCO++ container");
1338 // Check parallelLib
1339 std::string parallelLib = params.parallelLib.in();
1340 if (parallelLib != "Mpi" && parallelLib != "Dummy")
1342 INFOS("[checkPaCOParameters] parallelLib is not correctly defined");
1343 INFOS("[checkPaCOParameters] you can chosse between: Mpi and Dummy");
1344 INFOS("[checkPaCOParameters] you entered: " << parallelLib);
1348 if (params.nb_proc <= 0)
1350 INFOS("[checkPaCOParameters] You must define a nb_proc > 0");
1354 // Step 2 : check resource_selected
1355 const ParserResourcesType resource_definition = _resManager->GetResourceDefinition(resource_selected);
1356 //std::string protocol = resource_definition->protocol.in();
1357 std::string username = resource_definition.UserName;
1358 std::string applipath = resource_definition.AppliPath;
1360 //if (protocol == "" || username == "" || applipath == "")
1361 if (username == "" || applipath == "")
1363 INFOS("[checkPaCOParameters] resource selected is not well defined");
1364 INFOS("[checkPaCOParameters] resource name: " << resource_definition.Name);
1365 INFOS("[checkPaCOParameters] resource hostname: " << resource_definition.HostName);
1366 INFOS("[checkPaCOParameters] resource protocol: " << resource_definition.getAccessProtocolTypeStr());
1367 INFOS("[checkPaCOParameters] resource username: " << username);
1368 INFOS("[checkPaCOParameters] resource applipath: " << applipath);
1376 * :WARNING: Do not directly convert returned value to std::string
1377 * This function may return NULL if env variable is not defined.
1378 * And std::string(NULL) causes undefined behavior.
1379 * Use GetenvThreadSafeAsString to properly get a std::string.
1381 char *SALOME_ContainerManager::GetenvThreadSafe(const char *name)
1382 {// getenv is not thread safe. See man 7 pthread.
1383 Utils_Locker lock (&_getenvMutex);
1384 return getenv(name);
1388 * Return env variable as a std::string.
1389 * Return empty string if env variable is not set.
1391 std::string SALOME_ContainerManager::GetenvThreadSafeAsString(const char *name)
1393 char* var = GetenvThreadSafe(name);
1394 return var ? std::string(var) : std::string();
1397 int SALOME_ContainerManager::SystemThreadSafe(const char *command)
1399 Utils_Locker lock (&_systemMutex);
1400 return system(command);
1403 long SALOME_ContainerManager::SystemWithPIDThreadSafe(const std::vector<std::string>& command)
1405 Utils_Locker lock(&_systemMutex);
1406 if(command.size()<1)
1407 throw SALOME_Exception("SystemWithPIDThreadSafe : command is expected to have a length of size 1 at least !");
1409 pid_t pid ( fork() ) ; // spawn a child process, following code is executed in both processes
1411 pid_t pid = -1; //Throw SALOME_Exception on Windows
1413 if ( pid == 0 ) // I'm a child, replace myself with a new ompi-server
1415 std::size_t sz(command.size());
1416 char **args = new char *[sz+1];
1417 for(std::size_t i=0;i<sz;i++)
1418 args[i] = strdup(command[i].c_str());
1420 execvp( command[0].c_str() , args );
1421 std::ostringstream oss;
1422 oss << "Error when launching " << command[0];
1423 throw SALOME_Exception(oss.str().c_str()); // execvp failed
1427 throw SALOME_Exception("fork() failed");
1429 else // I'm a parent
1435 #ifdef WITH_PACO_PARALLEL
1437 //=============================================================================
1439 * Start a suitable PaCO++ Parallel Container in a list of machines.
1440 * \param params Container Parameters required for the container
1441 * \return CORBA container reference.
1443 //=============================================================================
1444 Engines::Container_ptr
1445 SALOME_ContainerManager::StartPaCOPPContainer(const Engines::ContainerParameters& params_const,
1446 std::string resource_selected)
1448 CORBA::Object_var obj;
1449 PaCO::InterfaceManager_var container_proxy;
1450 Engines::Container_ptr ret = Engines::Container::_nil();
1451 Engines::ContainerParameters params(params_const);
1452 params.resource_params.name = CORBA::string_dup(resource_selected.c_str());
1454 // Step 0 : Check parameters
1455 if (!checkPaCOParameters(params, resource_selected))
1457 INFOS("[StartPaCOPPContainer] check parameters failed ! see logs...");
1461 // Step 1 : Starting a new parallel container !
1462 INFOS("[StartPaCOPPContainer] Starting a PaCO++ parallel container");
1463 INFOS("[StartPaCOPPContainer] on resource : " << resource_selected);
1465 // Step 2 : Get a MachineFile for the parallel container
1466 std::string machine_file_name = _resManager->getMachineFile(resource_selected,
1468 params.parallelLib.in());
1470 if (machine_file_name == "")
1472 INFOS("[StartPaCOPPContainer] Machine file generation failed");
1476 // Step 3 : starting parallel container proxy
1477 std::string command_proxy("");
1478 std::string proxy_machine;
1481 command_proxy = BuildCommandToLaunchPaCOProxyContainer(params, machine_file_name, proxy_machine);
1483 catch(const SALOME_Exception & ex)
1485 INFOS("[StartPaCOPPContainer] Exception in BuildCommandToLaunchPaCOContainer");
1489 obj = LaunchPaCOProxyContainer(command_proxy, params, proxy_machine);
1490 if (CORBA::is_nil(obj))
1492 INFOS("[StartPaCOPPContainer] LaunchPaCOContainer for proxy returns NIL !");
1495 container_proxy = PaCO::InterfaceManager::_narrow(obj);
1496 MESSAGE("[StartPaCOPPContainer] PaCO container proxy is launched");
1498 // Step 4 : starting parallel container nodes
1499 std::string command_nodes("");
1500 SALOME_ContainerManager::actual_launch_machine_t nodes_machines;
1503 command_nodes = BuildCommandToLaunchPaCONodeContainer(params, machine_file_name, nodes_machines, proxy_machine);
1505 catch(const SALOME_Exception & ex)
1507 INFOS("[StarPaCOPPContainer] Exception in BuildCommandToLaunchPaCONodeContainer");
1512 std::string container_generic_node_name = std::string(params.container_name.in()) + std::string("Node");
1513 bool result = LaunchPaCONodeContainer(command_nodes, params, container_generic_node_name, nodes_machines);
1516 INFOS("[StarPaCOPPContainer] LaunchPaCONodeContainer failed !");
1517 // Il faut tuer le proxy
1520 Engines::Container_var proxy = Engines::Container::_narrow(container_proxy);
1525 INFOS("[StarPaCOPPContainer] Exception caught from proxy Shutdown...");
1530 // Step 4 : connecting nodes and the proxy to actually create a parallel container
1531 for (int i = 0; i < params.nb_proc; i++)
1533 std::ostringstream tmp;
1535 std::string proc_number = tmp.str();
1536 std::string container_node_name = container_generic_node_name + proc_number;
1538 std::string theNodeMachine(nodes_machines[i]);
1539 std::string containerNameInNS = _NS->BuildContainerNameForNS(container_node_name.c_str(), theNodeMachine.c_str());
1540 obj = _NS->Resolve(containerNameInNS.c_str());
1541 if (CORBA::is_nil(obj))
1543 INFOS("[StarPaCOPPContainer] CONNECTION FAILED From Naming Service !");
1544 INFOS("[StarPaCOPPContainer] Container name is " << containerNameInNS);
1549 MESSAGE("[StarPaCOPPContainer] Deploying node : " << container_node_name);
1550 PaCO::InterfaceParallel_var node = PaCO::InterfaceParallel::_narrow(obj);
1552 MESSAGE("[StarPaCOPPContainer] node " << container_node_name << " is deployed");
1554 catch(CORBA::SystemException& e)
1556 INFOS("[StarPaCOPPContainer] Exception in deploying node : " << containerNameInNS);
1557 INFOS("CORBA::SystemException : " << e);
1560 catch(CORBA::Exception& e)
1562 INFOS("[StarPaCOPPContainer] Exception in deploying node : " << containerNameInNS);
1563 INFOS("CORBA::Exception" << e);
1568 INFOS("[StarPaCOPPContainer] Exception in deploying node : " << containerNameInNS);
1569 INFOS("Unknown exception !");
1574 // Step 5 : starting parallel container
1577 MESSAGE ("[StarPaCOPPContainer] Starting parallel object");
1578 container_proxy->start();
1579 MESSAGE ("[StarPaCOPPContainer] Parallel object is started");
1580 ret = Engines::Container::_narrow(container_proxy);
1582 catch(CORBA::SystemException& e)
1584 INFOS("Caught CORBA::SystemException. : " << e);
1586 catch(PortableServer::POA::ServantAlreadyActive&)
1588 INFOS("Caught CORBA::ServantAlreadyActiveException");
1590 catch(CORBA::Exception&)
1592 INFOS("Caught CORBA::Exception.");
1594 catch(std::exception& exc)
1596 INFOS("Caught std::exception - "<<exc.what());
1600 INFOS("Caught unknown exception.");
1606 SALOME_ContainerManager::BuildCommandToLaunchPaCOProxyContainer(const Engines::ContainerParameters& params,
1607 std::string machine_file_name,
1608 std::string & proxy_hostname)
1610 // In the proxy case, we always launch a Dummy Proxy
1611 std::string exe_name = "SALOME_ParallelContainerProxyDummy";
1612 std::string container_name = params.container_name.in();
1614 // Convert nb_proc in string
1615 std::ostringstream tmp_string;
1616 tmp_string << params.nb_proc;
1617 std::string nb_proc_str = tmp_string.str();
1619 // Get resource definition
1620 ParserResourcesType resource_definition =
1621 _resManager->GetResourceDefinition(params.resource_params.name.in());
1624 std::string hostname;
1625 std::ifstream machine_file(machine_file_name.c_str());
1626 std::getline(machine_file, hostname, ' ');
1627 size_t found = hostname.find('\n');
1628 if (found!=std::string::npos)
1629 hostname.erase(found, 1); // Remove \n
1630 proxy_hostname = hostname;
1631 MESSAGE("[BuildCommandToLaunchPaCOProxyContainer] machine file name extracted is " << hostname);
1634 bool remote_execution = false;
1635 if (hostname != std::string(Kernel_Utils::GetHostname()))
1637 MESSAGE("[BuildCommandToLaunchPaCOProxyContainer] remote machine case detected !");
1638 remote_execution = true;
1642 std::string log_type("");
1643 char * get_val = GetenvThreadSafe("PARALLEL_LOG");
1647 // Generating the command
1648 std::string command_begin("");
1649 std::string command_end("");
1650 std::ostringstream command;
1652 LogConfiguration(log_type, "proxy", container_name, hostname, command_begin, command_end);
1653 command << command_begin;
1655 // Adding connection command
1656 // We can only have a remote execution with
1657 // a SALOME application
1658 if (remote_execution)
1660 ASSERT(GetenvThreadSafe("NSHOST"));
1661 ASSERT(GetenvThreadSafe("NSPORT"));
1663 command << resource_definition.getAccessProtocolTypeStr();
1665 command << resource_definition.UserName;
1666 command << " " << hostname;
1667 command << " " << resource_definition.AppliPath;
1668 command << "/runRemote.sh ";
1669 command << GetenvThreadSafeAsString("NSHOST") << " "; // hostname of CORBA name server
1670 command << GetenvThreadSafeAsString("NSPORT") << " "; // port of CORBA name server
1673 command << exe_name;
1674 command << " " << container_name;
1675 command << " Dummy";
1676 command << " " << hostname;
1677 command << " " << nb_proc_str;
1679 AddOmninamesParams(command);
1682 command << command_end;
1683 MESSAGE("[BuildCommandToLaunchPaCOProxyContainer] Command is: " << command.str());
1685 return command.str();
1689 SALOME_ContainerManager::BuildCommandToLaunchPaCONodeContainer(const Engines::ContainerParameters& params,
1690 const std::string & machine_file_name,
1691 SALOME_ContainerManager::actual_launch_machine_t & vect_machine,
1692 const std::string & proxy_hostname)
1695 std::string exe_name = "SALOME_ParallelContainerNode";
1696 exe_name += params.parallelLib.in();
1697 std::string container_name = params.container_name.in();
1699 // Convert nb_proc in string
1700 std::ostringstream nb_proc_stream;
1701 nb_proc_stream << params.nb_proc;
1703 // Get resource definition
1704 ParserResourcesType resource_definition =
1705 _resManager->GetResourceDefinition(params.resource_params.name.in());
1708 std::string log_type("");
1709 char * get_val = GetenvThreadSafe("PARALLEL_LOG");
1713 // Now the command is different according to paralleLib
1714 std::ostringstream command_nodes;
1715 std::ifstream machine_file(machine_file_name.c_str());
1716 if (std::string(params.parallelLib.in()) == "Dummy")
1718 for (int i= 0; i < params.nb_proc; i++)
1721 std::string hostname;
1722 std::getline(machine_file, hostname);
1723 MESSAGE("[BuildCommandToLaunchPaCONodeContainer] machine file name extracted is " << hostname);
1726 bool remote_execution = false;
1727 if (hostname != std::string(Kernel_Utils::GetHostname()))
1729 MESSAGE("[BuildCommandToLaunchPaCONodeContainer] remote machine case detected !");
1730 remote_execution = true;
1733 // For each node we have a new command
1734 // Generating the command
1735 std::ostringstream command_node_stream;
1736 std::string command_node_begin("");
1737 std::string command_node_end("");
1738 std::ostringstream node_number;
1740 std::string container_node_name = container_name + node_number.str();
1741 LogConfiguration(log_type, "node", container_node_name, hostname, command_node_begin, command_node_end);
1743 // Adding connection command
1744 // We can only have a remote execution with
1745 // a SALOME application
1746 if (remote_execution)
1748 ASSERT(GetenvThreadSafe("NSHOST"));
1749 ASSERT(GetenvThreadSafe("NSPORT"));
1751 command_node_stream << resource_definition.getAccessProtocolTypeStr();
1752 command_node_stream << " -l ";
1753 command_node_stream << resource_definition.UserName;
1754 command_node_stream << " " << hostname;
1755 command_node_stream << " " << resource_definition.AppliPath;
1756 command_node_stream << "/runRemote.sh ";
1757 command_node_stream << GetenvThreadSafeAsString("NSHOST") << " "; // hostname of CORBA name server
1758 command_node_stream << GetenvThreadSafeAsString("NSPORT") << " "; // port of CORBA name server
1761 command_node_stream << exe_name;
1762 command_node_stream << " " << container_name;
1763 command_node_stream << " " << params.parallelLib.in();
1764 command_node_stream << " " << proxy_hostname;
1765 command_node_stream << " " << node_number.str();
1766 command_node_stream << " -";
1767 AddOmninamesParams(command_node_stream);
1769 command_nodes << command_node_begin << command_node_stream.str() << command_node_end;
1770 vect_machine.push_back(hostname);
1774 else if (std::string(params.parallelLib.in()) == "Mpi")
1777 std::string hostname;
1778 std::getline(machine_file, hostname, ' ');
1779 MESSAGE("[BuildCommandToLaunchPaCONodeContainer] machine file name extracted is " << hostname);
1782 bool remote_execution = false;
1783 if (hostname != std::string(Kernel_Utils::GetHostname()))
1785 MESSAGE("[BuildCommandToLaunchPaCONodeContainer] remote machine case detected !");
1786 remote_execution = true;
1789 // In case of Mpi and Remote, we copy machine_file in the applipath
1790 // scp mpi_machine_file user@machine:Path
1791 std::ostringstream command_remote_stream;
1792 std::string::size_type last = machine_file_name.find_last_of("/");
1793 if (last == std::string::npos)
1796 if (resource_definition.Protocol == rsh)
1797 command_remote_stream << "rcp ";
1799 command_remote_stream << "scp ";
1800 command_remote_stream << machine_file_name << " ";
1801 command_remote_stream << resource_definition.UserName << "@";
1802 command_remote_stream << hostname << ":" << resource_definition.AppliPath;
1803 command_remote_stream << "/" << machine_file_name.substr(last+1);
1805 int status = SystemThreadSafe(command_remote_stream.str().c_str());
1808 INFOS("copy of the MPI machine file failed ! - sorry !");
1812 // Generating the command
1813 std::string command_begin("");
1814 std::string command_end("");
1816 LogConfiguration(log_type, "nodes", container_name, hostname, command_begin, command_end);
1817 command_nodes << command_begin;
1819 // Adding connection command
1820 // We can only have a remote execution with
1821 // a SALOME application
1822 if (remote_execution)
1824 ASSERT(GetenvThreadSafe("NSHOST"));
1825 ASSERT(GetenvThreadSafe("NSPORT"));
1827 command_nodes << resource_definition.getAccessProtocolTypeStr();
1828 command_nodes << " -l ";
1829 command_nodes << resource_definition.UserName;
1830 command_nodes << " " << hostname;
1831 command_nodes << " " << resource_definition.AppliPath;
1832 command_nodes << "/runRemote.sh ";
1833 command_nodes << GetenvThreadSafeAsString("NSHOST") << " "; // hostname of CORBA name server
1834 command_nodes << GetenvThreadSafeAsString("NSPORT") << " "; // port of CORBA name server
1837 if (resource_definition.mpi == lam)
1839 command_nodes << "mpiexec -ssi boot ";
1840 command_nodes << "-machinefile " << machine_file_name << " ";
1841 command_nodes << "-n " << params.nb_proc;
1845 command_nodes << "mpirun -np " << params.nb_proc;
1847 command_nodes << " " << exe_name;
1848 command_nodes << " " << container_name;
1849 command_nodes << " " << params.parallelLib.in();
1850 command_nodes << " " << proxy_hostname;
1851 command_nodes << " -";
1852 AddOmninamesParams(command_nodes);
1854 // We don't put hostname, because nodes are registered in the resource of the proxy
1855 for (int i= 0; i < params.nb_proc; i++)
1856 vect_machine.push_back(proxy_hostname);
1858 command_nodes << command_end;
1860 return command_nodes.str();
1864 SALOME_ContainerManager::LogConfiguration(const std::string & log_type,
1865 const std::string & exe_type,
1866 const std::string & container_name,
1867 const std::string & hostname,
1868 std::string & begin,
1871 if(log_type == "xterm")
1873 begin = "xterm -e \"export LD_LIBRARY_PATH=$LD_LIBRARY_PATH; export PATH=$PATH;";
1876 else if(log_type == "xterm_debug")
1878 begin = "xterm -e \"export LD_LIBRARY_PATH=$LD_LIBRARY_PATH; export PATH=$PATH;";
1883 // default into a file...
1884 std::string logFilename = "/tmp/" + container_name + "_" + hostname + "_" + exe_type + "_";
1885 std::string user = GetenvThreadSafeAsString("USER");
1887 user = GetenvThreadSafeAsString("LOGNAME");
1888 logFilename += user + ".log";
1889 end = " > " + logFilename + " 2>&1 & ";
1894 SALOME_ContainerManager::LaunchPaCOProxyContainer(const std::string& command,
1895 const Engines::ContainerParameters& params,
1896 const std::string & hostname)
1898 PaCO::InterfaceManager_ptr container_proxy = PaCO::InterfaceManager::_nil();
1900 MESSAGE("[LaunchPaCOProxyContainer] Launch command");
1901 int status = SystemThreadSafe(command.c_str());
1903 INFOS("[LaunchPaCOProxyContainer] failed : system command status -1");
1904 return container_proxy;
1906 else if (status == 217) {
1907 INFOS("[LaunchPaCOProxyContainer] failed : system command status 217");
1908 return container_proxy;
1911 int count(GetTimeOutToLoaunchServer());
1912 CORBA::Object_var obj = CORBA::Object::_nil();
1913 std::string containerNameInNS = _NS->BuildContainerNameForNS(params.container_name.in(),
1915 MESSAGE("[LaunchParallelContainer] Waiting for Parallel Container proxy : " << containerNameInNS);
1917 while (CORBA::is_nil(obj) && count)
1921 obj = _NS->Resolve(containerNameInNS.c_str());
1926 container_proxy = PaCO::InterfaceManager::_narrow(obj);
1928 catch(CORBA::SystemException& e)
1930 INFOS("[StarPaCOPPContainer] Exception in _narrow after LaunchParallelContainer for proxy !");
1931 INFOS("CORBA::SystemException : " << e);
1932 return container_proxy;
1934 catch(CORBA::Exception& e)
1936 INFOS("[StarPaCOPPContainer] Exception in _narrow after LaunchParallelContainer for proxy !");
1937 INFOS("CORBA::Exception" << e);
1938 return container_proxy;
1942 INFOS("[StarPaCOPPContainer] Exception in _narrow after LaunchParallelContainer for proxy !");
1943 INFOS("Unknown exception !");
1944 return container_proxy;
1946 if (CORBA::is_nil(container_proxy))
1948 INFOS("[StarPaCOPPContainer] PaCO::InterfaceManager::_narrow returns NIL !");
1949 return container_proxy;
1954 //=============================================================================
1955 /*! This method launches the parallel container.
1956 * It will may be placed on the resources manager.
1958 * \param command to launch
1959 * \param container's parameters
1960 * \param name of the container
1962 * \return CORBA container reference
1964 //=============================================================================
1966 SALOME_ContainerManager::LaunchPaCONodeContainer(const std::string& command,
1967 const Engines::ContainerParameters& params,
1968 const std::string& name,
1969 SALOME_ContainerManager::actual_launch_machine_t & vect_machine)
1971 INFOS("[LaunchPaCONodeContainer] Launch command");
1972 int status = SystemThreadSafe(command.c_str());
1974 INFOS("[LaunchPaCONodeContainer] failed : system command status -1");
1977 else if (status == 217) {
1978 INFOS("[LaunchPaCONodeContainer] failed : system command status 217");
1982 INFOS("[LaunchPaCONodeContainer] Waiting for the nodes of the parallel container");
1983 // We are waiting all the nodes
1984 for (int i = 0; i < params.nb_proc; i++)
1986 CORBA::Object_var obj = CORBA::Object::_nil();
1987 std::string theMachine(vect_machine[i]);
1989 std::ostringstream tmp;
1991 std::string proc_number = tmp.str();
1992 std::string container_node_name = name + proc_number;
1993 std::string containerNameInNS = _NS->BuildContainerNameForNS((char*) container_node_name.c_str(), theMachine.c_str());
1994 INFOS("[LaunchPaCONodeContainer] Waiting for Parallel Container node " << containerNameInNS << " on " << theMachine);
1995 int count(GetTimeOutToLoaunchServer());
1996 while (CORBA::is_nil(obj) && count) {
1999 obj = _NS->Resolve(containerNameInNS.c_str());
2001 if (CORBA::is_nil(obj))
2003 INFOS("[LaunchPaCONodeContainer] Launch of node failed (or not found) !");
2012 Engines::Container_ptr
2013 SALOME_ContainerManager::StartPaCOPPContainer(const Engines::ContainerParameters& params,
2014 std::string resource_selected)
2016 Engines::Container_ptr ret = Engines::Container::_nil();
2017 INFOS("[StarPaCOPPContainer] is disabled !");
2018 INFOS("[StarPaCOPPContainer] recompile SALOME Kernel to enable PaCO++ parallel extension");
2023 SALOME_ContainerManager::BuildCommandToLaunchPaCOProxyContainer(const Engines::ContainerParameters& params,
2024 std::string machine_file_name,
2025 std::string & proxy_hostname)
2031 SALOME_ContainerManager::BuildCommandToLaunchPaCONodeContainer(const Engines::ContainerParameters& params,
2032 const std::string & machine_file_name,
2033 SALOME_ContainerManager::actual_launch_machine_t & vect_machine,
2034 const std::string & proxy_hostname)
2039 SALOME_ContainerManager::LogConfiguration(const std::string & log_type,
2040 const std::string & exe_type,
2041 const std::string & container_name,
2042 const std::string & hostname,
2043 std::string & begin,
2049 SALOME_ContainerManager::LaunchPaCOProxyContainer(const std::string& command,
2050 const Engines::ContainerParameters& params,
2051 const std::string& hostname)
2053 CORBA::Object_ptr ret = CORBA::Object::_nil();
2058 SALOME_ContainerManager::LaunchPaCONodeContainer(const std::string& command,
2059 const Engines::ContainerParameters& params,
2060 const std::string& name,
2061 SALOME_ContainerManager::actual_launch_machine_t & vect_machine)