1 // Copyright (C) 2007-2019 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 int status = SystemThreadSafe(command.c_str());
537 // Error on resource - cannot launch commands
538 INFOS("[LaunchContainer] Cannot launch commands on machine " << hostname);
539 INFOS("[LaunchContainer] Command was " << command);
541 INFOS("[LaunchContainer] Command status is " << WEXITSTATUS(status));
543 return Engines::Container::_nil();
547 // Step 3: start a new container
548 // Check if a PaCO container
550 if (std::string(local_params.parallelLib.in()) != "")
552 ret = StartPaCOPPContainer(params, resource_selected);
555 // Other type of containers...
556 MESSAGE("[GiveContainer] Try to launch a new container on " << resource_selected);
557 // if a parallel container is launched in batch job, command is: "mpirun -np nbproc -machinefile nodesfile SALOME_MPIContainer"
558 if( GetenvThreadSafe("LIBBATCH_NODEFILE") != NULL && params.isMPI )
559 command = BuildCommandToLaunchLocalContainer(params, machFile, container_exe, tmpFileName);
560 // if a container is launched on localhost, command is "SALOME_Container" or "mpirun -np nbproc SALOME_MPIContainer"
561 else if(hostname == Kernel_Utils::GetHostname())
562 command = BuildCommandToLaunchLocalContainer(params, machFile, container_exe, tmpFileName);
563 // if a container is launched in remote mode, command is "ssh resource_selected SALOME_Container" or "ssh resource_selected mpirun -np nbproc SALOME_MPIContainer"
565 command = BuildCommandToLaunchRemoteContainer(resource_selected, params, container_exe);
567 //redirect stdout and stderr in a file
569 logFilename=GetenvThreadSafeAsString("TEMP");
571 user = GetenvThreadSafeAsString( "USERNAME" );
573 user = GetenvThreadSafeAsString( "USER" );
575 user = GetenvThreadSafeAsString( "LOGNAME" );
577 char* val = GetenvThreadSafe("SALOME_TMP_DIR");
580 struct stat file_info;
581 stat(val, &file_info);
582 bool is_dir = S_ISDIR(file_info.st_mode);
583 if (is_dir)logFilename=val;
584 else std::cerr << "SALOME_TMP_DIR environment variable is not a directory use /tmp instead" << std::endl;
588 logFilename += _NS->ContainerName(params)+"_"+ resource_selected +"_"+user;
589 std::ostringstream tmp;
590 tmp << "_" << getpid();
591 logFilename += tmp.str();
592 logFilename += ".log" ;
593 command += " > " + logFilename + " 2>&1";
594 MakeTheCommandToBeLaunchedASync(command);
596 // launch container with a system call
597 status=SystemThreadSafe(command.c_str());
598 }//end of critical of section
602 INFOS("[LaunchContainer] command failed (system command status -1): " << command);
603 RmTmpFile(tmpFileName); // command file can be removed here
604 return Engines::Container::_nil();
606 else if (status == 217)
608 INFOS("[LaunchContainer] command failed (system command status 217): " << command);
609 RmTmpFile(tmpFileName); // command file can be removed here
610 return Engines::Container::_nil();
614 // Step 4: Wait for the container
615 int count(GetTimeOutToLoaunchServer());
616 INFOS("[GiveContainer] waiting " << count << " second steps container " << containerNameInNS);
617 while (CORBA::is_nil(ret) && count)
621 MESSAGE("[GiveContainer] step " << count << " Waiting for container on " << resource_selected);
622 CORBA::Object_var obj(_NS->Resolve(containerNameInNS.c_str()));
623 ret=Engines::Container::_narrow(obj);
625 if (CORBA::is_nil(ret))
627 INFOS("[GiveContainer] was not able to launch container " << containerNameInNS);
631 // Setting log file name
632 logFilename=":"+logFilename;
633 logFilename="@"+Kernel_Utils::GetHostname()+logFilename;//threadsafe
634 logFilename=user+logFilename;
635 ret->logfilename(logFilename.c_str());
636 RmTmpFile(tmpFileName); // command file can be removed here
642 //=============================================================================
643 //! Find a container given constraints (params) on a list of machines (possibleComputers)
644 //! agy : this method is ThreadSafe
648 //=============================================================================
650 Engines::Container_ptr SALOME_ContainerManager::FindContainer(const Engines::ContainerParameters& params, const Engines::ResourceList& possibleResources)
652 MESSAGE("[FindContainer] FindContainer on " << possibleResources.length() << " resources");
653 for(unsigned int i=0; i < possibleResources.length();i++)
655 Engines::Container_ptr cont = FindContainer(params, possibleResources[i].in());
656 if(!CORBA::is_nil(cont))
659 MESSAGE("[FindContainer] no container found");
660 return Engines::Container::_nil();
663 //=============================================================================
664 //! Find a container given constraints (params) on a machine (theMachine)
665 //! agy : this method is ThreadSafe
669 //=============================================================================
671 Engines::Container_ptr
672 SALOME_ContainerManager::FindContainer(const Engines::ContainerParameters& params, const std::string& resource)
674 ParserResourcesType resource_definition = _resManager->GetResourceDefinition(resource);
675 std::string hostname(resource_definition.HostName);
676 std::string containerNameInNS(_NS->BuildContainerNameForNS(params, hostname.c_str()));
677 MESSAGE("[FindContainer] Try to find a container " << containerNameInNS << " on resource " << resource);
678 CORBA::Object_var obj = _NS->Resolve(containerNameInNS.c_str());
681 if(obj->_non_existent())
682 return Engines::Container::_nil();
684 return Engines::Container::_narrow(obj);
686 catch(const CORBA::Exception& e)
688 return Engines::Container::_nil();
693 bool isPythonContainer(const char* ContainerName);
695 //=============================================================================
697 * This is no longer valid (C++ container are also python containers)
699 //=============================================================================
700 bool isPythonContainer(const char* ContainerName)
702 return false; // VSR 02/08/2013: Python containers are no more supported
704 int len = strlen(ContainerName);
707 if (strcmp(ContainerName + len - 2, "Py") == 0)
713 //=============================================================================
715 * Builds the script to be launched
717 * If SALOME Application not defined ($APPLI),
718 * see BuildTempFileToLaunchRemoteContainer()
720 * Else rely on distant configuration. Command is under the form (example):
721 * ssh user@machine distantPath/runRemote.sh hostNS portNS WORKINGDIR workingdir \
722 * SALOME_Container containerName &"
724 * - where user is omitted if not specified in CatalogResources,
725 * - where distant path is always relative to user@machine $HOME, and
726 * equal to $APPLI if not specified in CatalogResources,
727 * - where hostNS is the hostname of CORBA naming server (set by scripts to
728 * use to launch SALOME and servers in $APPLI: runAppli.sh, runRemote.sh)
729 * - where portNS is the port used by CORBA naming server (set by scripts to
730 * use to launch SALOME and servers in $APPLI: runAppli.sh, runRemote.sh)
731 * - where workingdir is the requested working directory for the container.
732 * If WORKINGDIR (and workingdir) is not present the working dir will be $HOME
734 //=============================================================================
737 SALOME_ContainerManager::BuildCommandToLaunchRemoteContainer(const std::string& resource_name, const Engines::ContainerParameters& params, const std::string& container_exe) const
739 std::string command,tmpFileName;
740 if (!_isAppliSalomeDefined)
741 command = BuildTempFileToLaunchRemoteContainer(resource_name, params, tmpFileName);
745 const ParserResourcesType resInfo(_resManager->GetResourceDefinition(resource_name));
749 if ( params.nb_proc <= 0 )
752 nbproc = params.nb_proc;
755 std::string wdir = params.workingdir.in();
757 // "ssh -l user machine distantPath/runRemote.sh hostNS portNS WORKINGDIR workingdir \
758 // SALOME_Container containerName -ORBInitRef NameService=IOR:01000..."
760 // "ssh -l user machine distantLauncher remote -p hostNS -m portNS -d dir
761 // -- SALOME_Container contName -ORBInitRef NameService=IOR:01000..."
762 command = getCommandToRunRemoteProcess(resInfo.Protocol, resInfo.HostName,
763 resInfo.UserName, resInfo.AppliPath,
768 command += " mpirun -np ";
769 std::ostringstream o;
773 command += "-x PATH,LD_LIBRARY_PATH,OMNIORB_CONFIG,SALOME_trace ";
774 #elif defined(OPEN_MPI)
775 if( GetenvThreadSafe("OMPI_URI_FILE") == NULL )
776 command += "-x PATH -x LD_LIBRARY_PATH -x OMNIORB_CONFIG -x SALOME_trace";
778 command += "-x PATH -x LD_LIBRARY_PATH -x OMNIORB_CONFIG -x SALOME_trace -ompi-server file:";
779 command += GetenvThreadSafeAsString("OMPI_URI_FILE");
782 command += "-nameserver " + Kernel_Utils::GetHostname();
784 command += " SALOME_MPIContainer ";
787 command += " " +container_exe+ " ";
789 command += _NS->ContainerName(params);
791 AddOmninamesParams(command);
793 MESSAGE("command =" << command);
799 //=============================================================================
801 * builds the command to be launched.
803 //=============================================================================
804 std::string SALOME_ContainerManager::BuildCommandToLaunchLocalContainer(const Engines::ContainerParameters& params, const std::string& machinesFile, const std::string& container_exe, std::string& tmpFileName) const
806 tmpFileName = BuildTemporaryFileName();
810 std::ostringstream o;
816 if ( params.nb_proc <= 0 )
819 nbproc = params.nb_proc;
823 if( GetenvThreadSafe("LIBBATCH_NODEFILE") != NULL )
824 o << "-machinefile " << machinesFile << " ";
827 o << "-x PATH,LD_LIBRARY_PATH,OMNIORB_CONFIG,SALOME_trace ";
828 #elif defined(OPEN_MPI)
829 if( GetenvThreadSafe("OMPI_URI_FILE") == NULL )
830 o << "-x PATH -x LD_LIBRARY_PATH -x OMNIORB_CONFIG -x SALOME_trace";
833 o << "-x PATH -x LD_LIBRARY_PATH -x OMNIORB_CONFIG -x SALOME_trace -ompi-server file:";
834 o << GetenvThreadSafeAsString("OMPI_URI_FILE");
837 o << "-nameserver " + Kernel_Utils::GetHostname();
840 if (isPythonContainer(params.container_name))
841 o << " pyMPI SALOME_ContainerPy.py ";
843 o << " SALOME_MPIContainer ";
848 std::string wdir=params.workingdir.in();
851 // a working directory is requested
852 if(wdir == "$TEMPDIR")
854 // a new temporary directory is requested
855 std::string dir = Kernel_Utils::GetTmpDir();
857 o << "cd /d " << dir << std::endl;
859 o << "cd " << dir << ";";
865 // a permanent directory is requested use it or create it
867 o << "mkdir " + wdir << std::endl;
868 o << "cd /D " + wdir << std::endl;
870 o << "mkdir -p " << wdir << " && cd " << wdir + ";";
875 if (isPythonContainer(params.container_name))
876 o << "SALOME_ContainerPy.py ";
878 o << container_exe + " ";
882 o << _NS->ContainerName(params);
884 AddOmninamesParams(o);
886 std::ofstream command_file( tmpFileName.c_str() );
887 command_file << o.str();
888 command_file.close();
891 chmod(tmpFileName.c_str(), 0x1ED);
893 command = tmpFileName;
895 MESSAGE("Command is file ... " << command);
896 MESSAGE("Command is ... " << o.str());
901 //=============================================================================
903 * removes the generated temporary file in case of a remote launch.
904 * This method is thread safe
906 //=============================================================================
908 void SALOME_ContainerManager::RmTmpFile(std::string& tmpFileName)
910 int length = tmpFileName.size();
914 std::string command = "del /F ";
916 std::string command = "rm ";
919 command += tmpFileName.substr(0, length - 3 );
921 command += tmpFileName;
923 SystemThreadSafe(command.c_str());
924 //if dir is empty - remove it
925 std::string tmp_dir = Kernel_Utils::GetDirByPath( tmpFileName );
926 if ( Kernel_Utils::IsEmptyDir( tmp_dir ) )
929 command = "del /F " + tmp_dir;
931 command = "rmdir " + tmp_dir;
933 SystemThreadSafe(command.c_str());
938 //=============================================================================
940 * add to command all options relative to naming service.
942 //=============================================================================
944 void SALOME_ContainerManager::AddOmninamesParams(std::string& command) const
946 std::ostringstream oss;
947 AddOmninamesParams(oss);
951 //=============================================================================
953 * add to command all options relative to naming service.
955 //=============================================================================
957 void SALOME_ContainerManager::AddOmninamesParams(std::ostream& fileStream) const
959 AddOmninamesParams(fileStream,_NS);
962 //=============================================================================
964 * add to command all options relative to naming service.
966 //=============================================================================
968 void SALOME_ContainerManager::AddOmninamesParams(std::ostream& fileStream, SALOME_NamingService *ns)
970 CORBA::String_var iorstr(ns->getIORaddr());
971 fileStream << "ORBInitRef NameService=";
972 fileStream << iorstr;
975 void SALOME_ContainerManager::MakeTheCommandToBeLaunchedASync(std::string& command)
978 command = "%PYTHONBIN% -c \"import subprocess ; subprocess.Popen(r'" + command + "').pid\"";
984 int SALOME_ContainerManager::GetTimeOutToLoaunchServer()
986 int count(TIME_OUT_TO_LAUNCH_CONT);
987 if (GetenvThreadSafe("TIMEOUT_TO_LAUNCH_CONTAINER") != 0)
989 std::string new_count_str(GetenvThreadSafeAsString("TIMEOUT_TO_LAUNCH_CONTAINER"));
991 std::istringstream ss(new_count_str);
992 if (!(ss >> new_count))
994 INFOS("[LaunchContainer] TIMEOUT_TO_LAUNCH_CONTAINER should be an int");
1002 void SALOME_ContainerManager::SleepInSecond(int ellapseTimeInSecond)
1005 sleep( ellapseTimeInSecond ) ;
1007 int timeInMS(1000*ellapseTimeInSecond);
1012 //=============================================================================
1014 * generate a file name in /tmp directory
1016 //=============================================================================
1018 std::string SALOME_ContainerManager::BuildTemporaryFileName()
1020 //build more complex file name to support multiple salome session
1021 std::string aFileName = Kernel_Utils::GetTmpFileName();
1022 std::ostringstream str_pid;
1023 str_pid << ::getpid();
1024 aFileName = aFileName + "-" + str_pid.str();
1028 aFileName += ".bat";
1033 //=============================================================================
1035 * Builds in a temporary file the script to be launched.
1037 * Used if SALOME Application ($APPLI) is not defined.
1038 * The command is build with data from CatalogResources, in which every path
1039 * used on remote computer must be defined.
1041 //=============================================================================
1043 std::string SALOME_ContainerManager::BuildTempFileToLaunchRemoteContainer (const std::string& resource_name, const Engines::ContainerParameters& params, std::string& tmpFileName) const
1047 tmpFileName = BuildTemporaryFileName();
1048 std::ofstream tempOutputFile;
1049 tempOutputFile.open(tmpFileName.c_str(), std::ofstream::out );
1050 const ParserResourcesType resInfo(_resManager->GetResourceDefinition(resource_name));
1051 tempOutputFile << "#! /bin/sh" << std::endl;
1055 tempOutputFile << "export SALOME_trace=local" << std::endl; // mkr : 27.11.2006 : PAL13967 - Distributed supervision graphs - Problem with "SALOME_trace"
1056 //tempOutputFile << "source " << resInfo.PreReqFilePath << endl;
1062 tempOutputFile << "mpirun -np ";
1065 if ( params.nb_proc <= 0 )
1068 nbproc = params.nb_proc;
1070 std::ostringstream o;
1072 tempOutputFile << nbproc << " ";
1074 tempOutputFile << "-x PATH,LD_LIBRARY_PATH,OMNIORB_CONFIG,SALOME_trace ";
1075 #elif defined(OPEN_MPI)
1076 if( GetenvThreadSafe("OMPI_URI_FILE") == NULL )
1077 tempOutputFile << "-x PATH -x LD_LIBRARY_PATH -x OMNIORB_CONFIG -x SALOME_trace";
1079 tempOutputFile << "-x PATH -x LD_LIBRARY_PATH -x OMNIORB_CONFIG -x SALOME_trace -ompi-server file:";
1080 tempOutputFile << GetenvThreadSafeAsString("OMPI_URI_FILE");
1082 #elif defined(MPICH)
1083 tempOutputFile << "-nameserver " + Kernel_Utils::GetHostname();
1087 tempOutputFile << GetenvThreadSafeAsString("KERNEL_ROOT_DIR") << "/bin/salome/";
1091 if (isPythonContainer(params.container_name))
1092 tempOutputFile << " pyMPI SALOME_ContainerPy.py ";
1094 tempOutputFile << " SALOME_MPIContainer ";
1099 if (isPythonContainer(params.container_name))
1100 tempOutputFile << "SALOME_ContainerPy.py ";
1102 tempOutputFile << "SALOME_Container ";
1105 tempOutputFile << _NS->ContainerName(params) << " -";
1106 AddOmninamesParams(tempOutputFile);
1107 tempOutputFile << " &" << std::endl;
1108 tempOutputFile.flush();
1109 tempOutputFile.close();
1111 chmod(tmpFileName.c_str(), 0x1ED);
1114 // --- Build command
1116 std::string command;
1118 if (resInfo.Protocol == rsh)
1121 std::string commandRcp = "rcp ";
1122 commandRcp += tmpFileName;
1124 commandRcp += resInfo.HostName;
1126 commandRcp += tmpFileName;
1127 status = SystemThreadSafe(commandRcp.c_str());
1130 else if (resInfo.Protocol == ssh)
1133 std::string commandRcp = "scp ";
1134 commandRcp += tmpFileName;
1136 commandRcp += resInfo.HostName;
1138 commandRcp += tmpFileName;
1139 status = SystemThreadSafe(commandRcp.c_str());
1142 else if (resInfo.Protocol == srun)
1144 command = "srun -n 1 -N 1 -s --mem-per-cpu=0 --nodelist=";
1145 std::string commandRcp = "rcp ";
1146 commandRcp += tmpFileName;
1148 commandRcp += resInfo.HostName;
1150 commandRcp += tmpFileName;
1151 status = SystemThreadSafe(commandRcp.c_str());
1154 throw SALOME_Exception("Unknown protocol");
1157 throw SALOME_Exception("Error of connection on remote host");
1159 command += resInfo.HostName;
1161 command += tmpFileName;
1169 std::string SALOME_ContainerManager::GetMPIZeroNode(const std::string machine, const std::string machinesFile) const
1172 std::string zeronode;
1173 std::string command;
1174 std::string tmpFile = BuildTemporaryFileName();
1175 const ParserResourcesType resInfo(_resManager->GetResourceDefinition(machine));
1177 if(resInfo.Protocol == sh)
1179 return resInfo.HostName;
1182 if( GetenvThreadSafe("LIBBATCH_NODEFILE") == NULL )
1184 if (_isAppliSalomeDefined)
1186 command = getCommandToRunRemoteProcess(resInfo.Protocol, resInfo.HostName,
1187 resInfo.UserName, resInfo.AppliPath);
1188 command += " mpirun -np 1 hostname -s > " + tmpFile;
1191 command = "mpirun -np 1 hostname -s > " + tmpFile;
1194 command = "mpirun -np 1 -machinefile " + machinesFile + " hostname -s > " + tmpFile;
1196 status = SystemThreadSafe(command.c_str());
1198 std::ifstream fp(tmpFile.c_str(),std::ios::in);
1199 while(fp >> zeronode);
1207 std::string SALOME_ContainerManager::machinesFile(const int nbproc)
1210 std::string nodesFile = GetenvThreadSafeAsString("LIBBATCH_NODEFILE");
1211 std::string machinesFile = Kernel_Utils::GetTmpFileName();
1212 std::ifstream fpi(nodesFile.c_str(),std::ios::in);
1213 std::ofstream fpo(machinesFile.c_str(),std::ios::out);
1215 _numInstanceMutex.lock();
1217 for(int i=0;i<_nbprocUsed;i++)
1220 for(int i=0;i<nbproc;i++)
1222 fpo << tmp << std::endl;
1224 throw SALOME_Exception("You need more processors than batch session have allocated for you! Unable to launch the mpi container: ");
1226 _nbprocUsed += nbproc;
1230 _numInstanceMutex.unlock();
1232 return machinesFile;
1236 std::string SALOME_ContainerManager::getCommandToRunRemoteProcess(AccessProtocolType protocol,
1237 const std::string & hostname,
1238 const std::string & username,
1239 const std::string & applipath,
1240 const std::string & workdir)
1242 std::ostringstream command;
1249 command << "-l " << username << " ";
1251 command << hostname << " ";
1257 command << "-l " << username << " ";
1259 command << hostname << " ";
1262 // no need to redefine the user with srun, the job user is taken by default
1263 // (note: for srun, user id can be specified with " --uid=<user>")
1264 command << "srun -n 1 -N 1 --share --mem-per-cpu=0 --nodelist=" << hostname << " ";
1267 command << "pbsdsh -o -h " << hostname << " ";
1270 command << "blaunch -no-shell " << hostname << " ";
1273 throw SALOME_Exception("Unknown protocol");
1276 std::string remoteapplipath;
1278 remoteapplipath = GetenvThreadSafeAsString("APPLI");
1280 remoteapplipath = applipath;
1282 ASSERT(GetenvThreadSafe("NSHOST"));
1283 ASSERT(GetenvThreadSafe("NSPORT"));
1285 // $APPLI points either to an application directory, or to a salome launcher file
1286 // we prepare the remote command according to the case
1287 struct stat statbuf;
1288 if (stat(GetenvThreadSafe("APPLI"), &statbuf) ==0 && S_ISREG(statbuf.st_mode))
1290 // if $APPLI is a regular file, we asume it's a salome Launcher
1291 // generate a command with a salome launcher
1292 command << remoteapplipath
1295 << GetenvThreadSafeAsString("NSHOST") // hostname of CORBA name server
1297 << GetenvThreadSafeAsString("NSPORT"); // port of CORBA name server
1299 command << "-d " << workdir;
1302 else // we assume it's a salome application directory
1304 // generate a command with runRemote.sh
1305 command << remoteapplipath;
1306 command << "/runRemote.sh ";
1307 command << GetenvThreadSafeAsString("NSHOST"); // hostname of CORBA name server
1309 command << GetenvThreadSafeAsString("NSPORT"); // port of CORBA name server
1312 command << " WORKINGDIR ";
1314 if(workdir == "$TEMPDIR")
1315 command << "\\$TEMPDIR";
1317 command << workdir; // requested working directory
1322 return command.str();
1326 SALOME_ContainerManager::checkPaCOParameters(Engines::ContainerParameters & params, std::string resource_selected)
1330 // Step 1 : check ContainerParameters
1331 // Check container_name, has to be defined
1332 if (std::string(params.container_name.in()) == "")
1334 INFOS("[checkPaCOParameters] You must define a container_name to launch a PaCO++ container");
1337 // Check parallelLib
1338 std::string parallelLib = params.parallelLib.in();
1339 if (parallelLib != "Mpi" && parallelLib != "Dummy")
1341 INFOS("[checkPaCOParameters] parallelLib is not correctly defined");
1342 INFOS("[checkPaCOParameters] you can chosse between: Mpi and Dummy");
1343 INFOS("[checkPaCOParameters] you entered: " << parallelLib);
1347 if (params.nb_proc <= 0)
1349 INFOS("[checkPaCOParameters] You must define a nb_proc > 0");
1353 // Step 2 : check resource_selected
1354 const ParserResourcesType resource_definition = _resManager->GetResourceDefinition(resource_selected);
1355 //std::string protocol = resource_definition->protocol.in();
1356 std::string username = resource_definition.UserName;
1357 std::string applipath = resource_definition.AppliPath;
1359 //if (protocol == "" || username == "" || applipath == "")
1360 if (username == "" || applipath == "")
1362 INFOS("[checkPaCOParameters] resource selected is not well defined");
1363 INFOS("[checkPaCOParameters] resource name: " << resource_definition.Name);
1364 INFOS("[checkPaCOParameters] resource hostname: " << resource_definition.HostName);
1365 INFOS("[checkPaCOParameters] resource protocol: " << resource_definition.getAccessProtocolTypeStr());
1366 INFOS("[checkPaCOParameters] resource username: " << username);
1367 INFOS("[checkPaCOParameters] resource applipath: " << applipath);
1375 * :WARNING: Do not directly convert returned value to std::string
1376 * This function may return NULL if env variable is not defined.
1377 * And std::string(NULL) causes undefined behavior.
1378 * Use GetenvThreadSafeAsString to properly get a std::string.
1380 char *SALOME_ContainerManager::GetenvThreadSafe(const char *name)
1381 {// getenv is not thread safe. See man 7 pthread.
1382 Utils_Locker lock (&_getenvMutex);
1383 return getenv(name);
1387 * Return env variable as a std::string.
1388 * Return empty string if env variable is not set.
1390 std::string SALOME_ContainerManager::GetenvThreadSafeAsString(const char *name)
1392 char* var = GetenvThreadSafe(name);
1393 return var ? std::string(var) : std::string();
1396 int SALOME_ContainerManager::SystemThreadSafe(const char *command)
1398 Utils_Locker lock (&_systemMutex);
1399 return system(command);
1402 long SALOME_ContainerManager::SystemWithPIDThreadSafe(const std::vector<std::string>& command)
1404 Utils_Locker lock(&_systemMutex);
1405 if(command.size()<1)
1406 throw SALOME_Exception("SystemWithPIDThreadSafe : command is expected to have a length of size 1 at least !");
1408 pid_t pid ( fork() ) ; // spawn a child process, following code is executed in both processes
1410 pid_t pid = -1; //Throw SALOME_Exception on Windows
1412 if ( pid == 0 ) // I'm a child, replace myself with a new ompi-server
1414 std::size_t sz(command.size());
1415 char **args = new char *[sz+1];
1416 for(std::size_t i=0;i<sz;i++)
1417 args[i] = strdup(command[i].c_str());
1419 execvp( command[0].c_str() , args );
1420 std::ostringstream oss;
1421 oss << "Error when launching " << command[0];
1422 throw SALOME_Exception(oss.str().c_str()); // execvp failed
1426 throw SALOME_Exception("fork() failed");
1428 else // I'm a parent
1434 #ifdef WITH_PACO_PARALLEL
1436 //=============================================================================
1438 * Start a suitable PaCO++ Parallel Container in a list of machines.
1439 * \param params Container Parameters required for the container
1440 * \return CORBA container reference.
1442 //=============================================================================
1443 Engines::Container_ptr
1444 SALOME_ContainerManager::StartPaCOPPContainer(const Engines::ContainerParameters& params_const,
1445 std::string resource_selected)
1447 CORBA::Object_var obj;
1448 PaCO::InterfaceManager_var container_proxy;
1449 Engines::Container_ptr ret = Engines::Container::_nil();
1450 Engines::ContainerParameters params(params_const);
1451 params.resource_params.name = CORBA::string_dup(resource_selected.c_str());
1453 // Step 0 : Check parameters
1454 if (!checkPaCOParameters(params, resource_selected))
1456 INFOS("[StartPaCOPPContainer] check parameters failed ! see logs...");
1460 // Step 1 : Starting a new parallel container !
1461 INFOS("[StartPaCOPPContainer] Starting a PaCO++ parallel container");
1462 INFOS("[StartPaCOPPContainer] on resource : " << resource_selected);
1464 // Step 2 : Get a MachineFile for the parallel container
1465 std::string machine_file_name = _resManager->getMachineFile(resource_selected,
1467 params.parallelLib.in());
1469 if (machine_file_name == "")
1471 INFOS("[StartPaCOPPContainer] Machine file generation failed");
1475 // Step 3 : starting parallel container proxy
1476 std::string command_proxy("");
1477 std::string proxy_machine;
1480 command_proxy = BuildCommandToLaunchPaCOProxyContainer(params, machine_file_name, proxy_machine);
1482 catch(const SALOME_Exception & ex)
1484 INFOS("[StartPaCOPPContainer] Exception in BuildCommandToLaunchPaCOContainer");
1488 obj = LaunchPaCOProxyContainer(command_proxy, params, proxy_machine);
1489 if (CORBA::is_nil(obj))
1491 INFOS("[StartPaCOPPContainer] LaunchPaCOContainer for proxy returns NIL !");
1494 container_proxy = PaCO::InterfaceManager::_narrow(obj);
1495 MESSAGE("[StartPaCOPPContainer] PaCO container proxy is launched");
1497 // Step 4 : starting parallel container nodes
1498 std::string command_nodes("");
1499 SALOME_ContainerManager::actual_launch_machine_t nodes_machines;
1502 command_nodes = BuildCommandToLaunchPaCONodeContainer(params, machine_file_name, nodes_machines, proxy_machine);
1504 catch(const SALOME_Exception & ex)
1506 INFOS("[StarPaCOPPContainer] Exception in BuildCommandToLaunchPaCONodeContainer");
1511 std::string container_generic_node_name = std::string(params.container_name.in()) + std::string("Node");
1512 bool result = LaunchPaCONodeContainer(command_nodes, params, container_generic_node_name, nodes_machines);
1515 INFOS("[StarPaCOPPContainer] LaunchPaCONodeContainer failed !");
1516 // Il faut tuer le proxy
1519 Engines::Container_var proxy = Engines::Container::_narrow(container_proxy);
1524 INFOS("[StarPaCOPPContainer] Exception caught from proxy Shutdown...");
1529 // Step 4 : connecting nodes and the proxy to actually create a parallel container
1530 for (int i = 0; i < params.nb_proc; i++)
1532 std::ostringstream tmp;
1534 std::string proc_number = tmp.str();
1535 std::string container_node_name = container_generic_node_name + proc_number;
1537 std::string theNodeMachine(nodes_machines[i]);
1538 std::string containerNameInNS = _NS->BuildContainerNameForNS(container_node_name.c_str(), theNodeMachine.c_str());
1539 obj = _NS->Resolve(containerNameInNS.c_str());
1540 if (CORBA::is_nil(obj))
1542 INFOS("[StarPaCOPPContainer] CONNECTION FAILED From Naming Service !");
1543 INFOS("[StarPaCOPPContainer] Container name is " << containerNameInNS);
1548 MESSAGE("[StarPaCOPPContainer] Deploying node : " << container_node_name);
1549 PaCO::InterfaceParallel_var node = PaCO::InterfaceParallel::_narrow(obj);
1551 MESSAGE("[StarPaCOPPContainer] node " << container_node_name << " is deployed");
1553 catch(CORBA::SystemException& e)
1555 INFOS("[StarPaCOPPContainer] Exception in deploying node : " << containerNameInNS);
1556 INFOS("CORBA::SystemException : " << e);
1559 catch(CORBA::Exception& e)
1561 INFOS("[StarPaCOPPContainer] Exception in deploying node : " << containerNameInNS);
1562 INFOS("CORBA::Exception" << e);
1567 INFOS("[StarPaCOPPContainer] Exception in deploying node : " << containerNameInNS);
1568 INFOS("Unknown exception !");
1573 // Step 5 : starting parallel container
1576 MESSAGE ("[StarPaCOPPContainer] Starting parallel object");
1577 container_proxy->start();
1578 MESSAGE ("[StarPaCOPPContainer] Parallel object is started");
1579 ret = Engines::Container::_narrow(container_proxy);
1581 catch(CORBA::SystemException& e)
1583 INFOS("Caught CORBA::SystemException. : " << e);
1585 catch(PortableServer::POA::ServantAlreadyActive&)
1587 INFOS("Caught CORBA::ServantAlreadyActiveException");
1589 catch(CORBA::Exception&)
1591 INFOS("Caught CORBA::Exception.");
1593 catch(std::exception& exc)
1595 INFOS("Caught std::exception - "<<exc.what());
1599 INFOS("Caught unknown exception.");
1605 SALOME_ContainerManager::BuildCommandToLaunchPaCOProxyContainer(const Engines::ContainerParameters& params,
1606 std::string machine_file_name,
1607 std::string & proxy_hostname)
1609 // In the proxy case, we always launch a Dummy Proxy
1610 std::string exe_name = "SALOME_ParallelContainerProxyDummy";
1611 std::string container_name = params.container_name.in();
1613 // Convert nb_proc in string
1614 std::ostringstream tmp_string;
1615 tmp_string << params.nb_proc;
1616 std::string nb_proc_str = tmp_string.str();
1618 // Get resource definition
1619 ParserResourcesType resource_definition =
1620 _resManager->GetResourceDefinition(params.resource_params.name.in());
1623 std::string hostname;
1624 std::ifstream machine_file(machine_file_name.c_str());
1625 std::getline(machine_file, hostname, ' ');
1626 size_t found = hostname.find('\n');
1627 if (found!=std::string::npos)
1628 hostname.erase(found, 1); // Remove \n
1629 proxy_hostname = hostname;
1630 MESSAGE("[BuildCommandToLaunchPaCOProxyContainer] machine file name extracted is " << hostname);
1633 bool remote_execution = false;
1634 if (hostname != std::string(Kernel_Utils::GetHostname()))
1636 MESSAGE("[BuildCommandToLaunchPaCOProxyContainer] remote machine case detected !");
1637 remote_execution = true;
1641 std::string log_type("");
1642 char * get_val = GetenvThreadSafe("PARALLEL_LOG");
1646 // Generating the command
1647 std::string command_begin("");
1648 std::string command_end("");
1649 std::ostringstream command;
1651 LogConfiguration(log_type, "proxy", container_name, hostname, command_begin, command_end);
1652 command << command_begin;
1654 // Adding connection command
1655 // We can only have a remote execution with
1656 // a SALOME application
1657 if (remote_execution)
1659 ASSERT(GetenvThreadSafe("NSHOST"));
1660 ASSERT(GetenvThreadSafe("NSPORT"));
1662 command << resource_definition.getAccessProtocolTypeStr();
1664 command << resource_definition.UserName;
1665 command << " " << hostname;
1666 command << " " << resource_definition.AppliPath;
1667 command << "/runRemote.sh ";
1668 command << GetenvThreadSafeAsString("NSHOST") << " "; // hostname of CORBA name server
1669 command << GetenvThreadSafeAsString("NSPORT") << " "; // port of CORBA name server
1672 command << exe_name;
1673 command << " " << container_name;
1674 command << " Dummy";
1675 command << " " << hostname;
1676 command << " " << nb_proc_str;
1678 AddOmninamesParams(command);
1681 command << command_end;
1682 MESSAGE("[BuildCommandToLaunchPaCOProxyContainer] Command is: " << command.str());
1684 return command.str();
1688 SALOME_ContainerManager::BuildCommandToLaunchPaCONodeContainer(const Engines::ContainerParameters& params,
1689 const std::string & machine_file_name,
1690 SALOME_ContainerManager::actual_launch_machine_t & vect_machine,
1691 const std::string & proxy_hostname)
1694 std::string exe_name = "SALOME_ParallelContainerNode";
1695 exe_name += params.parallelLib.in();
1696 std::string container_name = params.container_name.in();
1698 // Convert nb_proc in string
1699 std::ostringstream nb_proc_stream;
1700 nb_proc_stream << params.nb_proc;
1702 // Get resource definition
1703 ParserResourcesType resource_definition =
1704 _resManager->GetResourceDefinition(params.resource_params.name.in());
1707 std::string log_type("");
1708 char * get_val = GetenvThreadSafe("PARALLEL_LOG");
1712 // Now the command is different according to paralleLib
1713 std::ostringstream command_nodes;
1714 std::ifstream machine_file(machine_file_name.c_str());
1715 if (std::string(params.parallelLib.in()) == "Dummy")
1717 for (int i= 0; i < params.nb_proc; i++)
1720 std::string hostname;
1721 std::getline(machine_file, hostname);
1722 MESSAGE("[BuildCommandToLaunchPaCONodeContainer] machine file name extracted is " << hostname);
1725 bool remote_execution = false;
1726 if (hostname != std::string(Kernel_Utils::GetHostname()))
1728 MESSAGE("[BuildCommandToLaunchPaCONodeContainer] remote machine case detected !");
1729 remote_execution = true;
1732 // For each node we have a new command
1733 // Generating the command
1734 std::ostringstream command_node_stream;
1735 std::string command_node_begin("");
1736 std::string command_node_end("");
1737 std::ostringstream node_number;
1739 std::string container_node_name = container_name + node_number.str();
1740 LogConfiguration(log_type, "node", container_node_name, hostname, command_node_begin, command_node_end);
1742 // Adding connection command
1743 // We can only have a remote execution with
1744 // a SALOME application
1745 if (remote_execution)
1747 ASSERT(GetenvThreadSafe("NSHOST"));
1748 ASSERT(GetenvThreadSafe("NSPORT"));
1750 command_node_stream << resource_definition.getAccessProtocolTypeStr();
1751 command_node_stream << " -l ";
1752 command_node_stream << resource_definition.UserName;
1753 command_node_stream << " " << hostname;
1754 command_node_stream << " " << resource_definition.AppliPath;
1755 command_node_stream << "/runRemote.sh ";
1756 command_node_stream << GetenvThreadSafeAsString("NSHOST") << " "; // hostname of CORBA name server
1757 command_node_stream << GetenvThreadSafeAsString("NSPORT") << " "; // port of CORBA name server
1760 command_node_stream << exe_name;
1761 command_node_stream << " " << container_name;
1762 command_node_stream << " " << params.parallelLib.in();
1763 command_node_stream << " " << proxy_hostname;
1764 command_node_stream << " " << node_number.str();
1765 command_node_stream << " -";
1766 AddOmninamesParams(command_node_stream);
1768 command_nodes << command_node_begin << command_node_stream.str() << command_node_end;
1769 vect_machine.push_back(hostname);
1773 else if (std::string(params.parallelLib.in()) == "Mpi")
1776 std::string hostname;
1777 std::getline(machine_file, hostname, ' ');
1778 MESSAGE("[BuildCommandToLaunchPaCONodeContainer] machine file name extracted is " << hostname);
1781 bool remote_execution = false;
1782 if (hostname != std::string(Kernel_Utils::GetHostname()))
1784 MESSAGE("[BuildCommandToLaunchPaCONodeContainer] remote machine case detected !");
1785 remote_execution = true;
1788 // In case of Mpi and Remote, we copy machine_file in the applipath
1789 // scp mpi_machine_file user@machine:Path
1790 std::ostringstream command_remote_stream;
1791 std::string::size_type last = machine_file_name.find_last_of("/");
1792 if (last == std::string::npos)
1795 if (resource_definition.Protocol == rsh)
1796 command_remote_stream << "rcp ";
1798 command_remote_stream << "scp ";
1799 command_remote_stream << machine_file_name << " ";
1800 command_remote_stream << resource_definition.UserName << "@";
1801 command_remote_stream << hostname << ":" << resource_definition.AppliPath;
1802 command_remote_stream << "/" << machine_file_name.substr(last+1);
1804 int status = SystemThreadSafe(command_remote_stream.str().c_str());
1807 INFOS("copy of the MPI machine file failed ! - sorry !");
1811 // Generating the command
1812 std::string command_begin("");
1813 std::string command_end("");
1815 LogConfiguration(log_type, "nodes", container_name, hostname, command_begin, command_end);
1816 command_nodes << command_begin;
1818 // Adding connection command
1819 // We can only have a remote execution with
1820 // a SALOME application
1821 if (remote_execution)
1823 ASSERT(GetenvThreadSafe("NSHOST"));
1824 ASSERT(GetenvThreadSafe("NSPORT"));
1826 command_nodes << resource_definition.getAccessProtocolTypeStr();
1827 command_nodes << " -l ";
1828 command_nodes << resource_definition.UserName;
1829 command_nodes << " " << hostname;
1830 command_nodes << " " << resource_definition.AppliPath;
1831 command_nodes << "/runRemote.sh ";
1832 command_nodes << GetenvThreadSafeAsString("NSHOST") << " "; // hostname of CORBA name server
1833 command_nodes << GetenvThreadSafeAsString("NSPORT") << " "; // port of CORBA name server
1836 if (resource_definition.mpi == lam)
1838 command_nodes << "mpiexec -ssi boot ";
1839 command_nodes << "-machinefile " << machine_file_name << " ";
1840 command_nodes << "-n " << params.nb_proc;
1844 command_nodes << "mpirun -np " << params.nb_proc;
1846 command_nodes << " " << exe_name;
1847 command_nodes << " " << container_name;
1848 command_nodes << " " << params.parallelLib.in();
1849 command_nodes << " " << proxy_hostname;
1850 command_nodes << " -";
1851 AddOmninamesParams(command_nodes);
1853 // We don't put hostname, because nodes are registered in the resource of the proxy
1854 for (int i= 0; i < params.nb_proc; i++)
1855 vect_machine.push_back(proxy_hostname);
1857 command_nodes << command_end;
1859 return command_nodes.str();
1863 SALOME_ContainerManager::LogConfiguration(const std::string & log_type,
1864 const std::string & exe_type,
1865 const std::string & container_name,
1866 const std::string & hostname,
1867 std::string & begin,
1870 if(log_type == "xterm")
1872 begin = "xterm -e \"export LD_LIBRARY_PATH=$LD_LIBRARY_PATH; export PATH=$PATH;";
1875 else if(log_type == "xterm_debug")
1877 begin = "xterm -e \"export LD_LIBRARY_PATH=$LD_LIBRARY_PATH; export PATH=$PATH;";
1882 // default into a file...
1883 std::string logFilename = "/tmp/" + container_name + "_" + hostname + "_" + exe_type + "_";
1884 std::string user = GetenvThreadSafeAsString("USER");
1886 user = GetenvThreadSafeAsString("LOGNAME");
1887 logFilename += user + ".log";
1888 end = " > " + logFilename + " 2>&1 & ";
1893 SALOME_ContainerManager::LaunchPaCOProxyContainer(const std::string& command,
1894 const Engines::ContainerParameters& params,
1895 const std::string & hostname)
1897 PaCO::InterfaceManager_ptr container_proxy = PaCO::InterfaceManager::_nil();
1899 MESSAGE("[LaunchPaCOProxyContainer] Launch command");
1900 int status = SystemThreadSafe(command.c_str());
1902 INFOS("[LaunchPaCOProxyContainer] failed : system command status -1");
1903 return container_proxy;
1905 else if (status == 217) {
1906 INFOS("[LaunchPaCOProxyContainer] failed : system command status 217");
1907 return container_proxy;
1910 int count(GetTimeOutToLoaunchServer());
1911 CORBA::Object_var obj = CORBA::Object::_nil();
1912 std::string containerNameInNS = _NS->BuildContainerNameForNS(params.container_name.in(),
1914 MESSAGE("[LaunchParallelContainer] Waiting for Parallel Container proxy : " << containerNameInNS);
1916 while (CORBA::is_nil(obj) && count)
1920 obj = _NS->Resolve(containerNameInNS.c_str());
1925 container_proxy = PaCO::InterfaceManager::_narrow(obj);
1927 catch(CORBA::SystemException& e)
1929 INFOS("[StarPaCOPPContainer] Exception in _narrow after LaunchParallelContainer for proxy !");
1930 INFOS("CORBA::SystemException : " << e);
1931 return container_proxy;
1933 catch(CORBA::Exception& e)
1935 INFOS("[StarPaCOPPContainer] Exception in _narrow after LaunchParallelContainer for proxy !");
1936 INFOS("CORBA::Exception" << e);
1937 return container_proxy;
1941 INFOS("[StarPaCOPPContainer] Exception in _narrow after LaunchParallelContainer for proxy !");
1942 INFOS("Unknown exception !");
1943 return container_proxy;
1945 if (CORBA::is_nil(container_proxy))
1947 INFOS("[StarPaCOPPContainer] PaCO::InterfaceManager::_narrow returns NIL !");
1948 return container_proxy;
1953 //=============================================================================
1954 /*! This method launches the parallel container.
1955 * It will may be placed on the resources manager.
1957 * \param command to launch
1958 * \param container's parameters
1959 * \param name of the container
1961 * \return CORBA container reference
1963 //=============================================================================
1965 SALOME_ContainerManager::LaunchPaCONodeContainer(const std::string& command,
1966 const Engines::ContainerParameters& params,
1967 const std::string& name,
1968 SALOME_ContainerManager::actual_launch_machine_t & vect_machine)
1970 INFOS("[LaunchPaCONodeContainer] Launch command");
1971 int status = SystemThreadSafe(command.c_str());
1973 INFOS("[LaunchPaCONodeContainer] failed : system command status -1");
1976 else if (status == 217) {
1977 INFOS("[LaunchPaCONodeContainer] failed : system command status 217");
1981 INFOS("[LaunchPaCONodeContainer] Waiting for the nodes of the parallel container");
1982 // We are waiting all the nodes
1983 for (int i = 0; i < params.nb_proc; i++)
1985 CORBA::Object_var obj = CORBA::Object::_nil();
1986 std::string theMachine(vect_machine[i]);
1988 std::ostringstream tmp;
1990 std::string proc_number = tmp.str();
1991 std::string container_node_name = name + proc_number;
1992 std::string containerNameInNS = _NS->BuildContainerNameForNS((char*) container_node_name.c_str(), theMachine.c_str());
1993 INFOS("[LaunchPaCONodeContainer] Waiting for Parallel Container node " << containerNameInNS << " on " << theMachine);
1994 int count(GetTimeOutToLoaunchServer());
1995 while (CORBA::is_nil(obj) && count) {
1998 obj = _NS->Resolve(containerNameInNS.c_str());
2000 if (CORBA::is_nil(obj))
2002 INFOS("[LaunchPaCONodeContainer] Launch of node failed (or not found) !");
2011 Engines::Container_ptr
2012 SALOME_ContainerManager::StartPaCOPPContainer(const Engines::ContainerParameters& params,
2013 std::string resource_selected)
2015 Engines::Container_ptr ret = Engines::Container::_nil();
2016 INFOS("[StarPaCOPPContainer] is disabled !");
2017 INFOS("[StarPaCOPPContainer] recompile SALOME Kernel to enable PaCO++ parallel extension");
2022 SALOME_ContainerManager::BuildCommandToLaunchPaCOProxyContainer(const Engines::ContainerParameters& params,
2023 std::string machine_file_name,
2024 std::string & proxy_hostname)
2030 SALOME_ContainerManager::BuildCommandToLaunchPaCONodeContainer(const Engines::ContainerParameters& params,
2031 const std::string & machine_file_name,
2032 SALOME_ContainerManager::actual_launch_machine_t & vect_machine,
2033 const std::string & proxy_hostname)
2038 SALOME_ContainerManager::LogConfiguration(const std::string & log_type,
2039 const std::string & exe_type,
2040 const std::string & container_name,
2041 const std::string & hostname,
2042 std::string & begin,
2048 SALOME_ContainerManager::LaunchPaCOProxyContainer(const std::string& command,
2049 const Engines::ContainerParameters& params,
2050 const std::string& hostname)
2052 CORBA::Object_ptr ret = CORBA::Object::_nil();
2057 SALOME_ContainerManager::LaunchPaCONodeContainer(const std::string& command,
2058 const Engines::ContainerParameters& params,
2059 const std::string& name,
2060 SALOME_ContainerManager::actual_launch_machine_t & vect_machine)