1 // Copyright (C) 2007-2014 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)
47 #define getpid _getpid
50 #ifdef WITH_PACO_PARALLEL
54 const int SALOME_ContainerManager::TIME_OUT_TO_LAUNCH_CONT=60;
56 const char *SALOME_ContainerManager::_ContainerManagerNameInNS =
59 omni_mutex SALOME_ContainerManager::_numInstanceMutex;
61 Utils_Mutex SALOME_ContainerManager::_getenvMutex;
63 Utils_Mutex SALOME_ContainerManager::_systemMutex;
65 //=============================================================================
69 * Define a CORBA single thread policy for the server, which avoid to deal
70 * with non thread-safe usage like Change_Directory in SALOME naming service
72 //=============================================================================
74 SALOME_ContainerManager::SALOME_ContainerManager(CORBA::ORB_ptr orb, PortableServer::POA_var poa, SALOME_NamingService *ns)
77 MESSAGE("constructor");
79 _resManager = new SALOME_ResourcesManager_Client(ns);
81 PortableServer::POAManager_var pman = poa->the_POAManager();
82 _orb = CORBA::ORB::_duplicate(orb) ;
83 CORBA::PolicyList policies;
85 PortableServer::ThreadPolicy_var threadPol(poa->create_thread_policy(PortableServer::ORB_CTRL_MODEL));
86 policies[0] = PortableServer::ThreadPolicy::_duplicate(threadPol);
88 _poa = poa->create_POA("MThreadPOA",pman,policies);
90 PortableServer::ObjectId_var id = _poa->activate_object(this);
91 CORBA::Object_var obj = _poa->id_to_reference(id);
92 Engines::ContainerManager_var refContMan =
93 Engines::ContainerManager::_narrow(obj);
95 _NS->Register(refContMan,_ContainerManagerNameInNS);
96 _isAppliSalomeDefined = (GetenvThreadSafe("APPLI") != 0);
101 // the urifile name depends on pid of the process
102 std::stringstream urifile;
103 urifile << GetenvThreadSafe("HOME") << "/.urifile_" << getpid();
104 setenv("OMPI_URI_FILE",urifile.str().c_str(),1);
105 if( GetenvThreadSafe("OMPI_URI_FILE") != NULL ){
106 // get the pid of all ompi-server
107 std::set<pid_t> thepids1 = getpidofprogram("ompi-server");
108 // launch a new ompi-server
110 command = "ompi-server -r ";
111 command += GetenvThreadSafe("OMPI_URI_FILE");
112 int status=SystemThreadSafe(command.c_str());
114 throw SALOME_Exception("Error when launching ompi-server");
115 // get the pid of all ompi-server
116 std::set<pid_t> thepids2 = getpidofprogram("ompi-server");
117 // my ompi-server is the new one
118 std::set<pid_t>::const_iterator it;
119 for(it=thepids2.begin();it!=thepids2.end();it++)
120 if(thepids1.find(*it) == thepids1.end())
121 _pid_mpiServer = *it;
122 if(_pid_mpiServer < 0)
123 throw SALOME_Exception("Error when getting ompi-server id");
125 #elif defined(WITHMPICH)
127 // get the pid of all hydra_nameserver
128 std::set<pid_t> thepids1 = getpidofprogram("hydra_nameserver");
129 // launch a new hydra_nameserver
131 command = "hydra_nameserver &";
132 SystemThreadSafe(command.c_str());
133 // get the pid of all hydra_nameserver
134 std::set<pid_t> thepids2 = getpidofprogram("hydra_nameserver");
135 // my hydra_nameserver is the new one
136 std::set<pid_t>::const_iterator it;
137 for(it=thepids2.begin();it!=thepids2.end();it++)
138 if(thepids1.find(*it) == thepids1.end())
139 _pid_mpiServer = *it;
143 MESSAGE("constructor end");
146 //=============================================================================
150 //=============================================================================
152 SALOME_ContainerManager::~SALOME_ContainerManager()
154 MESSAGE("destructor");
158 if( GetenvThreadSafe("OMPI_URI_FILE") != NULL ){
159 // kill my ompi-server
160 if( kill(_pid_mpiServer,SIGTERM) != 0 )
161 throw SALOME_Exception("Error when killing ompi-server");
163 int status=SystemThreadSafe("rm -f ${OMPI_URI_FILE}");
165 throw SALOME_Exception("Error when removing urifile");
167 #elif defined(WITHMPICH)
168 // kill my hydra_nameserver
169 if(_pid_mpiServer > -1)
170 if( kill(_pid_mpiServer,SIGTERM) != 0 )
171 throw SALOME_Exception("Error when killing hydra_nameserver");
176 //=============================================================================
177 //! shutdown all the containers, then the ContainerManager servant
180 //=============================================================================
182 void SALOME_ContainerManager::Shutdown()
185 ShutdownContainers();
186 _NS->Destroy_Name(_ContainerManagerNameInNS);
187 PortableServer::ObjectId_var oid = _poa->servant_to_id(this);
188 _poa->deactivate_object(oid);
191 //=============================================================================
192 //! Loop on all the containers listed in naming service, ask shutdown on each
195 //=============================================================================
197 void SALOME_ContainerManager::ShutdownContainers()
199 MESSAGE("ShutdownContainers");
201 SALOME::Session_var session = SALOME::Session::_nil();
203 CORBA::Object_var objS = _NS->Resolve("/Kernel/Session");
204 if (!CORBA::is_nil(objS))
206 session = SALOME::Session::_narrow(objS);
207 if (!CORBA::is_nil(session))
208 pid = session->getPID();
212 isOK = _NS->Change_Directory("/Containers");
214 std::vector<std::string> vec = _NS->list_directory_recurs();
215 std::list<std::string> lstCont;
216 for(std::vector<std::string>::iterator iter = vec.begin();iter!=vec.end();iter++)
219 CORBA::Object_var obj=_NS->Resolve((*iter).c_str());
222 Engines::Container_var cont=Engines::Container::_narrow(obj);
223 if(!CORBA::is_nil(cont) && pid != cont->getPID())
224 lstCont.push_back((*iter));
226 catch(const CORBA::Exception& e)
228 // ignore this entry and continue
231 MESSAGE("Container list: ");
232 for(std::list<std::string>::iterator iter=lstCont.begin();iter!=lstCont.end();iter++){
235 for(std::list<std::string>::iterator iter=lstCont.begin();iter!=lstCont.end();iter++)
240 CORBA::Object_var obj=_NS->Resolve((*iter).c_str());
241 Engines::Container_var cont=Engines::Container::_narrow(obj);
242 if(!CORBA::is_nil(cont))
244 MESSAGE("ShutdownContainers: " << (*iter));
248 MESSAGE("ShutdownContainers: no container ref for " << (*iter));
250 catch(CORBA::SystemException& e)
252 INFOS("CORBA::SystemException ignored : " << e);
254 catch(CORBA::Exception&)
256 INFOS("CORBA::Exception ignored.");
260 INFOS("Unknown exception ignored.");
266 //=============================================================================
267 //! Give a suitable Container given constraints
269 * \param params Container Parameters required for the container
270 * \return the container or nil
272 //=============================================================================
273 Engines::Container_ptr SALOME_ContainerManager::GiveContainer(const Engines::ContainerParameters& params)
275 std::string machFile;
276 Engines::Container_ptr ret(Engines::Container::_nil());
278 // Step 0: Default mode is start
279 Engines::ContainerParameters local_params(params);
280 if (std::string(local_params.mode.in()) == "")
281 local_params.mode = CORBA::string_dup("start");
282 std::string mode = local_params.mode.in();
283 MESSAGE("[GiveContainer] starting with mode: " << mode);
285 // Step 1: Find Container for find and findorstart mode
286 if (mode == "find" || mode == "findorstart")
288 ret = FindContainer(params, params.resource_params.resList);
289 if(!CORBA::is_nil(ret))
295 MESSAGE("[GiveContainer] no container found");
305 // Step 2: Get all possibleResources from the parameters
306 // Consider only resources that can run containers
307 resourceParams resource_params = resourceParameters_CORBAtoCPP(local_params.resource_params);
308 resource_params.can_run_containers = true;
309 std::vector<std::string> possibleResources = _resManager->GetFittingResources(resource_params);
310 MESSAGE("[GiveContainer] - length of possible resources " << possibleResources.size());
311 std::vector<std::string> local_resources;
313 // Step 3: if mode is "get" keep only machines with existing containers
316 for(unsigned int i=0; i < possibleResources.size(); i++)
318 Engines::Container_ptr cont = FindContainer(params, possibleResources[i]);
321 if(!cont->_non_existent())
322 local_resources.push_back(possibleResources[i]);
324 catch(CORBA::Exception&) {}
327 // if local_resources is empty, we cannot give a container
328 if (local_resources.size() == 0)
330 MESSAGE("[GiveContainer] cannot find a container for mode get");
335 local_resources = possibleResources;
337 // Step 4: select the resource where to get/start the container
338 bool resource_available = true;
339 std::string resource_selected;
340 std::vector<std::string> resources = local_resources;
341 while (resource_available)
343 if (resources.size() == 0)
344 resource_available = false;
349 resource_selected = _resManager->Find(params.resource_params.policy.in(), resources);
350 // Remove resource_selected from vector
351 std::vector<std::string>::iterator it;
352 for (it=resources.begin() ; it < resources.end(); it++ )
353 if (*it == resource_selected)
359 catch(const SALOME_Exception &ex)
361 MESSAGE("[GiveContainer] Exception in ResourceManager find !: " << ex.what());
364 MESSAGE("[GiveContainer] Resource selected is: " << resource_selected);
366 // Step 5: Create container name
367 ParserResourcesType resource_definition = _resManager->GetResourceDefinition(resource_selected);
368 std::string hostname(resource_definition.HostName);
369 std::string containerNameInNS;
372 if ( params.nb_proc <= 0 )
375 nbproc = params.nb_proc;
378 if( GetenvThreadSafe("LIBBATCH_NODEFILE") != NULL )
379 machFile = machinesFile(nbproc);
381 catch(const SALOME_Exception & ex)
383 std::string err_msg = ex.what();
384 err_msg += params.container_name;
385 INFOS(err_msg.c_str());
388 // A mpi parallel container register on zero node in NS
389 containerNameInNS = _NS->BuildContainerNameForNS(params, GetMPIZeroNode(hostname,machFile).c_str());
392 containerNameInNS = _NS->BuildContainerNameForNS(params, hostname.c_str());
393 MESSAGE("[GiveContainer] Container name in the naming service: " << containerNameInNS);
395 // Step 6: check if the name exists in naming service
396 //if params.mode == "getorstart" or "get" use the existing container
397 //if params.mode == "start" shutdown the existing container before launching a new one with that name
399 { // critical section
400 Utils_Locker lock (&_giveContainerMutex1);
401 CORBA::Object_var obj = _NS->Resolve(containerNameInNS.c_str());
402 if (!CORBA::is_nil(obj))
406 Engines::Container_var cont=Engines::Container::_narrow(obj);
407 if(!cont->_non_existent())
409 if(std::string(params.mode.in())=="getorstart" || std::string(params.mode.in())=="get"){
410 return cont._retn(); /* the container exists and params.mode is getorstart or get use it*/
414 INFOS("[GiveContainer] A container is already registered with the name: " << containerNameInNS << ", shutdown the existing container");
415 cont->Shutdown(); // shutdown the registered container if it exists
419 catch(CORBA::Exception&)
421 INFOS("[GiveContainer] CORBA::Exception ignored when trying to get the container - we start a new one");
424 } // end critical section
425 Engines::Container_var cont = LaunchContainer(params, resource_selected, hostname, machFile, containerNameInNS);
426 if (!CORBA::is_nil(cont))
428 INFOS("[GiveContainer] container " << containerNameInNS << " launched");
433 INFOS("[GiveContainer] Failed to launch container on resource " << resource_selected);
438 // We were not able to launch the container
439 INFOS("[GiveContainer] Cannot launch the container on the following selected resources:")
440 std::vector<std::string>::iterator it;
441 for (it=local_resources.begin() ; it < local_resources.end(); it++ )
442 INFOS("[GiveContainer] " << *it)
446 Engines::Container_ptr
447 SALOME_ContainerManager::LaunchContainer(const Engines::ContainerParameters& params,
448 const std::string & resource_selected,
449 const std::string & hostname,
450 const std::string & machFile,
451 const std::string & containerNameInNS)
453 std::string user,command,logFilename,tmpFileName;
455 Engines::Container_ptr ret(Engines::Container::_nil());
456 {//start of critical section
457 Utils_Locker lock (&_giveContainerMutex1);
458 // Step 1: type of container: PaCO, Exe, Mpi or Classic
459 // Mpi already tested in step 5, specific code on BuildCommandToLaunch Local/Remote Container methods
460 // TODO -> separates Mpi from Classic/Exe
462 std::string container_exe = "SALOME_Container"; // Classic container
463 Engines::ContainerParameters local_params(params);
467 CORBA::String_var container_exe_tmp;
468 CORBA::Object_var obj = _NS->Resolve("/Kernel/ModulCatalog");
469 SALOME_ModuleCatalog::ModuleCatalog_var Catalog = SALOME_ModuleCatalog::ModuleCatalog::_narrow(obj) ;
470 if (CORBA::is_nil (Catalog))
472 INFOS("[GiveContainer] Module Catalog is not found -> cannot launch a container");
475 // Loop through component list
476 for(unsigned int i=0; i < local_params.resource_params.componentList.length(); i++)
478 const char* compoi = local_params.resource_params.componentList[i];
479 SALOME_ModuleCatalog::Acomponent_var compoInfo = Catalog->GetComponent(compoi);
480 if (CORBA::is_nil (compoInfo))
484 SALOME_ModuleCatalog::ImplType impl=compoInfo->implementation_type();
485 container_exe_tmp=compoInfo->implementation_name();
486 if(impl==SALOME_ModuleCatalog::CEXE)
490 INFOS("ContainerManager Error: you can't have 2 CEXE component in the same container" );
491 return Engines::Container::_nil();
493 MESSAGE("[GiveContainer] Exe container found !: " << container_exe_tmp);
494 container_exe = container_exe_tmp.in();
499 catch (ServiceUnreachable&)
501 INFOS("Caught exception: Naming Service Unreachable");
506 INFOS("Caught unknown exception.");
510 // Step 2: test resource
511 // Only if an application directory is set
512 if(hostname != Kernel_Utils::GetHostname() && _isAppliSalomeDefined)
514 // Preparing remote command
515 std::string command = "";
516 const ParserResourcesType resInfo(_resManager->GetResourceDefinition(resource_selected));
517 command = getCommandToRunRemoteProcess(resInfo.Protocol, resInfo.HostName, resInfo.UserName);
518 if (resInfo.AppliPath != "")
519 command += resInfo.AppliPath;
522 ASSERT(GetenvThreadSafe("APPLI"));
523 command += GetenvThreadSafe("APPLI");
525 command += "/runRemote.sh ";
526 ASSERT(GetenvThreadSafe("NSHOST"));
527 command += GetenvThreadSafe("NSHOST"); // hostname of CORBA name server
529 ASSERT(GetenvThreadSafe("NSPORT"));
530 command += GetenvThreadSafe("NSPORT"); // port of CORBA name server
531 command += " \"ls /tmp >/dev/null 2>&1\"";
533 // Launch remote command
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=GetenvThreadSafe("TEMP");
571 user = GetenvThreadSafe( "USERNAME" );
573 user = GetenvThreadSafe( "USER" );
575 char* val = GetenvThreadSafe("SALOME_TMP_DIR");
578 struct stat file_info;
579 stat(val, &file_info);
580 bool is_dir = S_ISDIR(file_info.st_mode);
581 if (is_dir)logFilename=val;
582 else std::cerr << "SALOME_TMP_DIR environment variable is not a directory use /tmp instead" << std::endl;
586 logFilename += _NS->ContainerName(params)+"_"+ resource_selected +"_"+user;
587 std::ostringstream tmp;
588 tmp << "_" << getpid();
589 logFilename += tmp.str();
590 logFilename += ".log" ;
591 command += " > " + logFilename + " 2>&1";
592 MakeTheCommandToBeLaunchedASync(command);
594 // launch container with a system call
595 status=SystemThreadSafe(command.c_str());
596 }//end of critical of section
600 INFOS("[LaunchContainer] command failed (system command status -1): " << command);
601 RmTmpFile(tmpFileName); // command file can be removed here
602 return Engines::Container::_nil();
604 else if (status == 217)
606 INFOS("[LaunchContainer] command failed (system command status 217): " << command);
607 RmTmpFile(tmpFileName); // command file can be removed here
608 return Engines::Container::_nil();
612 // Step 4: Wait for the container
613 int count(GetTimeOutToLoaunchServer());
614 INFOS("[GiveContainer] waiting " << count << " second steps container " << containerNameInNS);
615 while (CORBA::is_nil(ret) && count)
619 MESSAGE("[GiveContainer] step " << count << " Waiting for container on " << resource_selected);
620 CORBA::Object_var obj(_NS->Resolve(containerNameInNS.c_str()));
621 ret=Engines::Container::_narrow(obj);
623 if (CORBA::is_nil(ret))
625 INFOS("[GiveContainer] was not able to launch container " << containerNameInNS);
629 // Setting log file name
630 logFilename=":"+logFilename;
631 logFilename="@"+Kernel_Utils::GetHostname()+logFilename;//threadsafe
632 logFilename=user+logFilename;
633 ret->logfilename(logFilename.c_str());
634 RmTmpFile(tmpFileName); // command file can be removed here
640 //=============================================================================
641 //! Find a container given constraints (params) on a list of machines (possibleComputers)
642 //! agy : this method is ThreadSafe
646 //=============================================================================
648 Engines::Container_ptr SALOME_ContainerManager::FindContainer(const Engines::ContainerParameters& params, const Engines::ResourceList& possibleResources)
650 MESSAGE("[FindContainer] FindContainer on " << possibleResources.length() << " resources");
651 for(unsigned int i=0; i < possibleResources.length();i++)
653 Engines::Container_ptr cont = FindContainer(params, possibleResources[i].in());
654 if(!CORBA::is_nil(cont))
657 MESSAGE("[FindContainer] no container found");
658 return Engines::Container::_nil();
661 //=============================================================================
662 //! Find a container given constraints (params) on a machine (theMachine)
663 //! agy : this method is ThreadSafe
667 //=============================================================================
669 Engines::Container_ptr
670 SALOME_ContainerManager::FindContainer(const Engines::ContainerParameters& params, const std::string& resource)
672 ParserResourcesType resource_definition = _resManager->GetResourceDefinition(resource);
673 std::string hostname(resource_definition.HostName);
674 std::string containerNameInNS(_NS->BuildContainerNameForNS(params, hostname.c_str()));
675 MESSAGE("[FindContainer] Try to find a container " << containerNameInNS << " on resource " << resource);
676 CORBA::Object_var obj = _NS->Resolve(containerNameInNS.c_str());
679 if(obj->_non_existent())
680 return Engines::Container::_nil();
682 return Engines::Container::_narrow(obj);
684 catch(const CORBA::Exception& e)
686 return Engines::Container::_nil();
691 bool isPythonContainer(const char* ContainerName);
693 //=============================================================================
695 * This is no longer valid (C++ container are also python containers)
697 //=============================================================================
698 bool isPythonContainer(const char* ContainerName)
700 return false; // VSR 02/08/2013: Python containers are no more supported
702 int len = strlen(ContainerName);
705 if (strcmp(ContainerName + len - 2, "Py") == 0)
711 //=============================================================================
713 * Builds the script to be launched
715 * If SALOME Application not defined ($APPLI),
716 * see BuildTempFileToLaunchRemoteContainer()
718 * Else rely on distant configuration. Command is under the form (example):
719 * ssh user@machine distantPath/runRemote.sh hostNS portNS WORKINGDIR workingdir \
720 * SALOME_Container containerName &"
722 * - where user is ommited if not specified in CatalogResources,
723 * - where distant path is always relative to user@machine $HOME, and
724 * equal to $APPLI if not specified in CatalogResources,
725 * - where hostNS is the hostname of CORBA naming server (set by scripts to
726 * use to launch SALOME and servers in $APPLI: runAppli.sh, runRemote.sh)
727 * - where portNS is the port used by CORBA naming server (set by scripts to
728 * use to launch SALOME and servers in $APPLI: runAppli.sh, runRemote.sh)
729 * - where workingdir is the requested working directory for the container.
730 * If WORKINGDIR (and workingdir) is not present the working dir will be $HOME
732 //=============================================================================
735 SALOME_ContainerManager::BuildCommandToLaunchRemoteContainer(const std::string& resource_name, const Engines::ContainerParameters& params, const std::string& container_exe) const
737 std::string command,tmpFileName;
738 if (!_isAppliSalomeDefined)
739 command = BuildTempFileToLaunchRemoteContainer(resource_name, params, tmpFileName);
743 const ParserResourcesType resInfo(_resManager->GetResourceDefinition(resource_name));
747 if ( params.nb_proc <= 0 )
750 nbproc = params.nb_proc;
753 // "ssh -l user machine distantPath/runRemote.sh hostNS portNS WORKINGDIR workingdir \
754 // SALOME_Container containerName &"
755 command = getCommandToRunRemoteProcess(resInfo.Protocol, resInfo.HostName, resInfo.UserName);
757 if (resInfo.AppliPath != "")
758 command += resInfo.AppliPath; // path relative to user@machine $HOME
761 ASSERT(GetenvThreadSafe("APPLI"));
762 command += GetenvThreadSafe("APPLI"); // path relative to user@machine $HOME
765 command += "/runRemote.sh ";
767 ASSERT(GetenvThreadSafe("NSHOST"));
768 command += GetenvThreadSafe("NSHOST"); // hostname of CORBA name server
771 ASSERT(GetenvThreadSafe("NSPORT"));
772 command += GetenvThreadSafe("NSPORT"); // port of CORBA name server
774 std::string wdir = params.workingdir.in();
777 command += " WORKINGDIR ";
779 if(wdir == "$TEMPDIR")
781 command += wdir; // requested working directory
787 command += " mpirun -np ";
788 std::ostringstream o;
792 command += "-x PATH,LD_LIBRARY_PATH,OMNIORB_CONFIG,SALOME_trace ";
793 #elif defined(WITHOPENMPI)
794 if( GetenvThreadSafe("OMPI_URI_FILE") == NULL )
795 command += "-x PATH -x LD_LIBRARY_PATH -x OMNIORB_CONFIG -x SALOME_trace";
797 command += "-x PATH -x LD_LIBRARY_PATH -x OMNIORB_CONFIG -x SALOME_trace -ompi-server file:";
798 command += GetenvThreadSafe("OMPI_URI_FILE");
800 #elif defined(WITHMPICH)
801 command += "-nameserver " + Kernel_Utils::GetHostname();
803 command += " SALOME_MPIContainer ";
806 command += " " +container_exe+ " ";
808 command += _NS->ContainerName(params);
810 AddOmninamesParams(command);
812 MESSAGE("command =" << command);
818 //=============================================================================
820 * builds the command to be launched.
822 //=============================================================================
823 std::string SALOME_ContainerManager::BuildCommandToLaunchLocalContainer(const Engines::ContainerParameters& params, const std::string& machinesFile, const std::string& container_exe, std::string& tmpFileName) const
825 tmpFileName = BuildTemporaryFileName();
829 std::ostringstream o;
835 if ( params.nb_proc <= 0 )
838 nbproc = params.nb_proc;
842 if( GetenvThreadSafe("LIBBATCH_NODEFILE") != NULL )
843 o << "-machinefile " << machinesFile << " ";
846 o << "-x PATH,LD_LIBRARY_PATH,OMNIORB_CONFIG,SALOME_trace ";
847 #elif defined(WITHOPENMPI)
848 if( GetenvThreadSafe("OMPI_URI_FILE") == NULL )
849 o << "-x PATH -x LD_LIBRARY_PATH -x OMNIORB_CONFIG -x SALOME_trace";
852 o << "-x PATH -x LD_LIBRARY_PATH -x OMNIORB_CONFIG -x SALOME_trace -ompi-server file:";
853 o << GetenvThreadSafe("OMPI_URI_FILE");
855 #elif defined(WITHMPICH)
856 o << "-nameserver " + Kernel_Utils::GetHostname();
859 if (isPythonContainer(params.container_name))
860 o << " pyMPI SALOME_ContainerPy.py ";
862 o << " SALOME_MPIContainer ";
867 std::string wdir=params.workingdir.in();
870 // a working directory is requested
871 if(wdir == "$TEMPDIR")
873 // a new temporary directory is requested
874 std::string dir = Kernel_Utils::GetTmpDir();
876 o << "cd /d " << dir << std::endl;
878 o << "cd " << dir << ";";
884 // a permanent directory is requested use it or create it
886 o << "mkdir " + wdir << std::endl;
887 o << "cd /D " + wdir << std::endl;
889 o << "mkdir -p " << wdir << " && cd " << wdir + ";";
894 if (isPythonContainer(params.container_name))
895 o << "SALOME_ContainerPy.py ";
897 o << container_exe + " ";
901 o << _NS->ContainerName(params);
903 AddOmninamesParams(o);
905 std::ofstream command_file( tmpFileName.c_str() );
906 command_file << o.str();
907 command_file.close();
910 chmod(tmpFileName.c_str(), 0x1ED);
912 command = tmpFileName;
914 MESSAGE("Command is file ... " << command);
915 MESSAGE("Command is ... " << o.str());
920 //=============================================================================
922 * removes the generated temporary file in case of a remote launch.
923 * This method is thread safe
925 //=============================================================================
927 void SALOME_ContainerManager::RmTmpFile(std::string& tmpFileName)
929 int lenght = tmpFileName.size();
933 std::string command = "del /F ";
935 std::string command = "rm ";
938 command += tmpFileName.substr(0, lenght - 3 );
940 command += tmpFileName;
942 SystemThreadSafe(command.c_str());
943 //if dir is empty - remove it
944 std::string tmp_dir = Kernel_Utils::GetDirByPath( tmpFileName );
945 if ( Kernel_Utils::IsEmptyDir( tmp_dir ) )
948 command = "del /F " + tmp_dir;
950 command = "rmdir " + tmp_dir;
952 SystemThreadSafe(command.c_str());
957 //=============================================================================
959 * add to command all options relative to naming service.
961 //=============================================================================
963 void SALOME_ContainerManager::AddOmninamesParams(std::string& command) const
965 std::ostringstream oss;
966 AddOmninamesParams(oss);
970 //=============================================================================
972 * add to command all options relative to naming service.
974 //=============================================================================
976 void SALOME_ContainerManager::AddOmninamesParams(std::ostream& fileStream) const
978 AddOmninamesParams(fileStream,_NS);
981 //=============================================================================
983 * add to command all options relative to naming service.
985 //=============================================================================
987 void SALOME_ContainerManager::AddOmninamesParams(std::ostream& fileStream, SALOME_NamingService *ns)
989 CORBA::String_var iorstr(ns->getIORaddr());
990 fileStream << "ORBInitRef NameService=";
991 fileStream << iorstr;
994 void SALOME_ContainerManager::MakeTheCommandToBeLaunchedASync(std::string& command)
997 command = "%PYTHONBIN% -c \"import win32pm ; win32pm.spawnpid(r'" + command + "', '')\"";
1003 int SALOME_ContainerManager::GetTimeOutToLoaunchServer()
1005 int count(TIME_OUT_TO_LAUNCH_CONT);
1006 if (GetenvThreadSafe("TIMEOUT_TO_LAUNCH_CONTAINER") != 0)
1008 std::string new_count_str(GetenvThreadSafe("TIMEOUT_TO_LAUNCH_CONTAINER"));
1010 std::istringstream ss(new_count_str);
1011 if (!(ss >> new_count))
1013 INFOS("[LaunchContainer] TIMEOUT_TO_LAUNCH_CONTAINER should be an int");
1021 void SALOME_ContainerManager::SleepInSecond(int ellapseTimeInSecond)
1024 sleep( ellapseTimeInSecond ) ;
1026 int timeInMS(1000*ellapseTimeInSecond);
1031 //=============================================================================
1033 * generate a file name in /tmp directory
1035 //=============================================================================
1037 std::string SALOME_ContainerManager::BuildTemporaryFileName()
1039 //build more complex file name to support multiple salome session
1040 std::string aFileName = Kernel_Utils::GetTmpFileName();
1044 aFileName += ".bat";
1049 //=============================================================================
1051 * Builds in a temporary file the script to be launched.
1053 * Used if SALOME Application ($APPLI) is not defined.
1054 * The command is build with data from CatalogResources, in which every path
1055 * used on remote computer must be defined.
1057 //=============================================================================
1059 std::string SALOME_ContainerManager::BuildTempFileToLaunchRemoteContainer (const std::string& resource_name, const Engines::ContainerParameters& params, std::string& tmpFileName) const
1063 tmpFileName = BuildTemporaryFileName();
1064 std::ofstream tempOutputFile;
1065 tempOutputFile.open(tmpFileName.c_str(), std::ofstream::out );
1066 const ParserResourcesType resInfo(_resManager->GetResourceDefinition(resource_name));
1067 tempOutputFile << "#! /bin/sh" << std::endl;
1071 tempOutputFile << "export SALOME_trace=local" << std::endl; // mkr : 27.11.2006 : PAL13967 - Distributed supervision graphs - Problem with "SALOME_trace"
1072 //tempOutputFile << "source " << resInfo.PreReqFilePath << endl;
1078 tempOutputFile << "mpirun -np ";
1081 if ( params.nb_proc <= 0 )
1084 nbproc = params.nb_proc;
1086 std::ostringstream o;
1088 tempOutputFile << nbproc << " ";
1090 tempOutputFile << "-x PATH,LD_LIBRARY_PATH,OMNIORB_CONFIG,SALOME_trace ";
1091 #elif defined(WITHOPENMPI)
1092 if( GetenvThreadSafe("OMPI_URI_FILE") == NULL )
1093 tempOutputFile << "-x PATH -x LD_LIBRARY_PATH -x OMNIORB_CONFIG -x SALOME_trace";
1095 tempOutputFile << "-x PATH -x LD_LIBRARY_PATH -x OMNIORB_CONFIG -x SALOME_trace -ompi-server file:";
1096 tempOutputFile << GetenvThreadSafe("OMPI_URI_FILE");
1098 #elif defined(WITHMPICH)
1099 tempOutputFile << "-nameserver " + Kernel_Utils::GetHostname();
1103 tempOutputFile << GetenvThreadSafe("KERNEL_ROOT_DIR") << "/bin/salome/";
1107 if (isPythonContainer(params.container_name))
1108 tempOutputFile << " pyMPI SALOME_ContainerPy.py ";
1110 tempOutputFile << " SALOME_MPIContainer ";
1115 if (isPythonContainer(params.container_name))
1116 tempOutputFile << "SALOME_ContainerPy.py ";
1118 tempOutputFile << "SALOME_Container ";
1121 tempOutputFile << _NS->ContainerName(params) << " -";
1122 AddOmninamesParams(tempOutputFile);
1123 tempOutputFile << " &" << std::endl;
1124 tempOutputFile.flush();
1125 tempOutputFile.close();
1127 chmod(tmpFileName.c_str(), 0x1ED);
1130 // --- Build command
1132 std::string command;
1134 if (resInfo.Protocol == rsh)
1137 std::string commandRcp = "rcp ";
1138 commandRcp += tmpFileName;
1140 commandRcp += resInfo.HostName;
1142 commandRcp += tmpFileName;
1143 status = SystemThreadSafe(commandRcp.c_str());
1146 else if (resInfo.Protocol == ssh)
1149 std::string commandRcp = "scp ";
1150 commandRcp += tmpFileName;
1152 commandRcp += resInfo.HostName;
1154 commandRcp += tmpFileName;
1155 status = SystemThreadSafe(commandRcp.c_str());
1158 else if (resInfo.Protocol == srun)
1160 command = "srun -n 1 -N 1 --share --nodelist=";
1161 std::string commandRcp = "rcp ";
1162 commandRcp += tmpFileName;
1164 commandRcp += resInfo.HostName;
1166 commandRcp += tmpFileName;
1167 status = SystemThreadSafe(commandRcp.c_str());
1170 throw SALOME_Exception("Unknown protocol");
1173 throw SALOME_Exception("Error of connection on remote host");
1175 command += resInfo.HostName;
1177 command += tmpFileName;
1185 std::string SALOME_ContainerManager::GetMPIZeroNode(const std::string machine, const std::string machinesFile) const
1188 std::string zeronode;
1189 std::string command;
1190 std::string tmpFile = BuildTemporaryFileName();
1192 if( GetenvThreadSafe("LIBBATCH_NODEFILE") == NULL )
1194 if (_isAppliSalomeDefined)
1196 const ParserResourcesType resInfo(_resManager->GetResourceDefinition(machine));
1198 if (resInfo.Protocol == rsh)
1200 else if (resInfo.Protocol == ssh)
1202 else if (resInfo.Protocol == srun)
1203 command = "srun -n 1 -N 1 --share --nodelist=";
1205 throw SALOME_Exception("Unknown protocol");
1207 if (resInfo.UserName != "")
1210 command += resInfo.UserName;
1214 command += resInfo.HostName;
1217 if (resInfo.AppliPath != "")
1218 command += resInfo.AppliPath; // path relative to user@machine $HOME
1221 ASSERT(GetenvThreadSafe("APPLI"));
1222 command += GetenvThreadSafe("APPLI"); // path relative to user@machine $HOME
1225 command += "/runRemote.sh ";
1227 ASSERT(GetenvThreadSafe("NSHOST"));
1228 command += GetenvThreadSafe("NSHOST"); // hostname of CORBA name server
1231 ASSERT(GetenvThreadSafe("NSPORT"));
1232 command += GetenvThreadSafe("NSPORT"); // port of CORBA name server
1234 command += " mpirun -np 1 hostname -s > " + tmpFile;
1237 command = "mpirun -np 1 hostname -s > " + tmpFile;
1240 command = "mpirun -np 1 -machinefile " + machinesFile + " hostname -s > " + tmpFile;
1242 status = SystemThreadSafe(command.c_str());
1244 std::ifstream fp(tmpFile.c_str(),std::ios::in);
1245 while(fp >> zeronode);
1253 std::string SALOME_ContainerManager::machinesFile(const int nbproc)
1256 std::string nodesFile = GetenvThreadSafe("LIBBATCH_NODEFILE");
1257 std::string machinesFile = Kernel_Utils::GetTmpFileName();
1258 std::ifstream fpi(nodesFile.c_str(),std::ios::in);
1259 std::ofstream fpo(machinesFile.c_str(),std::ios::out);
1261 _numInstanceMutex.lock();
1263 for(int i=0;i<_nbprocUsed;i++)
1266 for(int i=0;i<nbproc;i++)
1268 fpo << tmp << std::endl;
1270 throw SALOME_Exception("You need more processors than batch session have allocated for you! Unable to launch the mpi container: ");
1272 _nbprocUsed += nbproc;
1276 _numInstanceMutex.unlock();
1278 return machinesFile;
1282 std::set<pid_t> SALOME_ContainerManager::getpidofprogram(const std::string program)
1284 std::set<pid_t> thepids;
1285 std::string tmpFile = Kernel_Utils::GetTmpFileName();
1288 cmd = "pidof " + program + " > " + tmpFile;
1289 SystemThreadSafe(cmd.c_str());
1290 std::ifstream fpi(tmpFile.c_str(),std::ios::in);
1291 while(fpi >> thepid){
1292 thepids.insert(atoi(thepid.c_str()));
1297 std::string SALOME_ContainerManager::getCommandToRunRemoteProcess(AccessProtocolType protocol,
1298 const std::string & hostname,
1299 const std::string & username)
1301 std::ostringstream command;
1308 command << "-l " << username << " ";
1310 command << hostname << " ";
1316 command << "-l " << username << " ";
1318 command << hostname << " ";
1321 // no need to redefine the user with srun, the job user is taken by default
1322 // (note: for srun, user id can be specified with " --uid=<user>")
1323 command << "srun -n 1 -N 1 --share --nodelist=" << hostname << " ";
1326 command << "pbsdsh -o -h " << hostname << " ";
1329 command << "blaunch -no-shell " << hostname << " ";
1332 throw SALOME_Exception("Unknown protocol");
1335 return command.str();
1339 SALOME_ContainerManager::checkPaCOParameters(Engines::ContainerParameters & params, std::string resource_selected)
1343 // Step 1 : check ContainerParameters
1344 // Check container_name, has to be defined
1345 if (std::string(params.container_name.in()) == "")
1347 INFOS("[checkPaCOParameters] You must define a container_name to launch a PaCO++ container");
1350 // Check parallelLib
1351 std::string parallelLib = params.parallelLib.in();
1352 if (parallelLib != "Mpi" && parallelLib != "Dummy")
1354 INFOS("[checkPaCOParameters] parallelLib is not correctly defined");
1355 INFOS("[checkPaCOParameters] you can chosse between: Mpi and Dummy");
1356 INFOS("[checkPaCOParameters] you entered: " << parallelLib);
1360 if (params.nb_proc <= 0)
1362 INFOS("[checkPaCOParameters] You must define a nb_proc > 0");
1366 // Step 2 : check resource_selected
1367 const ParserResourcesType resource_definition = _resManager->GetResourceDefinition(resource_selected);
1368 //std::string protocol = resource_definition->protocol.in();
1369 std::string username = resource_definition.UserName;
1370 std::string applipath = resource_definition.AppliPath;
1372 //if (protocol == "" || username == "" || applipath == "")
1373 if (username == "" || applipath == "")
1375 INFOS("[checkPaCOParameters] resource selected is not well defined");
1376 INFOS("[checkPaCOParameters] resource name: " << resource_definition.Name);
1377 INFOS("[checkPaCOParameters] resource hostname: " << resource_definition.HostName);
1378 INFOS("[checkPaCOParameters] resource protocol: " << resource_definition.getAccessProtocolTypeStr());
1379 INFOS("[checkPaCOParameters] resource username: " << username);
1380 INFOS("[checkPaCOParameters] resource applipath: " << applipath);
1387 char *SALOME_ContainerManager::GetenvThreadSafe(const char *name)
1388 {// getenv is not thread safe. See man 7 pthread.
1389 Utils_Locker lock (&_getenvMutex);
1390 return getenv(name);
1393 int SALOME_ContainerManager::SystemThreadSafe(const char *command)
1395 Utils_Locker lock (&_systemMutex);
1396 return system(command);
1399 #ifdef WITH_PACO_PARALLEL
1401 //=============================================================================
1403 * Start a suitable PaCO++ Parallel Container in a list of machines.
1404 * \param params Container Parameters required for the container
1405 * \return CORBA container reference.
1407 //=============================================================================
1408 Engines::Container_ptr
1409 SALOME_ContainerManager::StartPaCOPPContainer(const Engines::ContainerParameters& params_const,
1410 std::string resource_selected)
1412 CORBA::Object_var obj;
1413 PaCO::InterfaceManager_var container_proxy;
1414 Engines::Container_ptr ret = Engines::Container::_nil();
1415 Engines::ContainerParameters params(params_const);
1416 params.resource_params.name = CORBA::string_dup(resource_selected.c_str());
1418 // Step 0 : Check parameters
1419 if (!checkPaCOParameters(params, resource_selected))
1421 INFOS("[StartPaCOPPContainer] check parameters failed ! see logs...");
1425 // Step 1 : Starting a new parallel container !
1426 INFOS("[StartPaCOPPContainer] Starting a PaCO++ parallel container");
1427 INFOS("[StartPaCOPPContainer] on resource : " << resource_selected);
1429 // Step 2 : Get a MachineFile for the parallel container
1430 std::string machine_file_name = _resManager->getMachineFile(resource_selected,
1432 params.parallelLib.in());
1434 if (machine_file_name == "")
1436 INFOS("[StartPaCOPPContainer] Machine file generation failed");
1440 // Step 3 : starting parallel container proxy
1441 std::string command_proxy("");
1442 std::string proxy_machine;
1445 command_proxy = BuildCommandToLaunchPaCOProxyContainer(params, machine_file_name, proxy_machine);
1447 catch(const SALOME_Exception & ex)
1449 INFOS("[StartPaCOPPContainer] Exception in BuildCommandToLaunchPaCOContainer");
1453 obj = LaunchPaCOProxyContainer(command_proxy, params, proxy_machine);
1454 if (CORBA::is_nil(obj))
1456 INFOS("[StartPaCOPPContainer] LaunchPaCOContainer for proxy returns NIL !");
1459 container_proxy = PaCO::InterfaceManager::_narrow(obj);
1460 MESSAGE("[StartPaCOPPContainer] PaCO container proxy is launched");
1462 // Step 4 : starting parallel container nodes
1463 std::string command_nodes("");
1464 SALOME_ContainerManager::actual_launch_machine_t nodes_machines;
1467 command_nodes = BuildCommandToLaunchPaCONodeContainer(params, machine_file_name, nodes_machines, proxy_machine);
1469 catch(const SALOME_Exception & ex)
1471 INFOS("[StarPaCOPPContainer] Exception in BuildCommandToLaunchPaCONodeContainer");
1476 std::string container_generic_node_name = std::string(params.container_name.in()) + std::string("Node");
1477 bool result = LaunchPaCONodeContainer(command_nodes, params, container_generic_node_name, nodes_machines);
1480 INFOS("[StarPaCOPPContainer] LaunchPaCONodeContainer failed !");
1481 // Il faut tuer le proxy
1484 Engines::Container_var proxy = Engines::Container::_narrow(container_proxy);
1489 INFOS("[StarPaCOPPContainer] Exception catched from proxy Shutdown...");
1494 // Step 4 : connecting nodes and the proxy to actually create a parallel container
1495 for (int i = 0; i < params.nb_proc; i++)
1497 std::ostringstream tmp;
1499 std::string proc_number = tmp.str();
1500 std::string container_node_name = container_generic_node_name + proc_number;
1502 std::string theNodeMachine(nodes_machines[i]);
1503 std::string containerNameInNS = _NS->BuildContainerNameForNS(container_node_name.c_str(), theNodeMachine.c_str());
1504 obj = _NS->Resolve(containerNameInNS.c_str());
1505 if (CORBA::is_nil(obj))
1507 INFOS("[StarPaCOPPContainer] CONNECTION FAILED From Naming Service !");
1508 INFOS("[StarPaCOPPContainer] Container name is " << containerNameInNS);
1513 MESSAGE("[StarPaCOPPContainer] Deploying node : " << container_node_name);
1514 PaCO::InterfaceParallel_var node = PaCO::InterfaceParallel::_narrow(obj);
1516 MESSAGE("[StarPaCOPPContainer] node " << container_node_name << " is deployed");
1518 catch(CORBA::SystemException& e)
1520 INFOS("[StarPaCOPPContainer] Exception in deploying node : " << containerNameInNS);
1521 INFOS("CORBA::SystemException : " << e);
1524 catch(CORBA::Exception& e)
1526 INFOS("[StarPaCOPPContainer] Exception in deploying node : " << containerNameInNS);
1527 INFOS("CORBA::Exception" << e);
1532 INFOS("[StarPaCOPPContainer] Exception in deploying node : " << containerNameInNS);
1533 INFOS("Unknown exception !");
1538 // Step 5 : starting parallel container
1541 MESSAGE ("[StarPaCOPPContainer] Starting parallel object");
1542 container_proxy->start();
1543 MESSAGE ("[StarPaCOPPContainer] Parallel object is started");
1544 ret = Engines::Container::_narrow(container_proxy);
1546 catch(CORBA::SystemException& e)
1548 INFOS("Caught CORBA::SystemException. : " << e);
1550 catch(PortableServer::POA::ServantAlreadyActive&)
1552 INFOS("Caught CORBA::ServantAlreadyActiveException");
1554 catch(CORBA::Exception&)
1556 INFOS("Caught CORBA::Exception.");
1558 catch(std::exception& exc)
1560 INFOS("Caught std::exception - "<<exc.what());
1564 INFOS("Caught unknown exception.");
1570 SALOME_ContainerManager::BuildCommandToLaunchPaCOProxyContainer(const Engines::ContainerParameters& params,
1571 std::string machine_file_name,
1572 std::string & proxy_hostname)
1574 // In the proxy case, we always launch a Dummy Proxy
1575 std::string exe_name = "SALOME_ParallelContainerProxyDummy";
1576 std::string container_name = params.container_name.in();
1578 // Convert nb_proc in string
1579 std::ostringstream tmp_string;
1580 tmp_string << params.nb_proc;
1581 std::string nb_proc_str = tmp_string.str();
1583 // Get resource definition
1584 ParserResourcesType resource_definition =
1585 _resManager->GetResourceDefinition(params.resource_params.name.in());
1588 std::string hostname;
1589 std::ifstream machine_file(machine_file_name.c_str());
1590 std::getline(machine_file, hostname, ' ');
1591 size_t found = hostname.find('\n');
1592 if (found!=std::string::npos)
1593 hostname.erase(found, 1); // Remove \n
1594 proxy_hostname = hostname;
1595 MESSAGE("[BuildCommandToLaunchPaCOProxyContainer] machine file name extracted is " << hostname);
1598 bool remote_execution = false;
1599 if (hostname != std::string(Kernel_Utils::GetHostname()))
1601 MESSAGE("[BuildCommandToLaunchPaCOProxyContainer] remote machine case detected !");
1602 remote_execution = true;
1605 // Log environnement
1606 std::string log_type("");
1607 char * get_val = GetenvThreadSafe("PARALLEL_LOG");
1611 // Generating the command
1612 std::string command_begin("");
1613 std::string command_end("");
1614 std::ostringstream command;
1616 LogConfiguration(log_type, "proxy", container_name, hostname, command_begin, command_end);
1617 command << command_begin;
1619 // Adding connection command
1620 // We can only have a remote execution with
1621 // a SALOME application
1622 if (remote_execution)
1624 ASSERT(GetenvThreadSafe("NSHOST"));
1625 ASSERT(GetenvThreadSafe("NSPORT"));
1627 command << resource_definition.getAccessProtocolTypeStr();
1629 command << resource_definition.UserName;
1630 command << " " << hostname;
1631 command << " " << resource_definition.AppliPath;
1632 command << "/runRemote.sh ";
1633 command << GetenvThreadSafe("NSHOST") << " "; // hostname of CORBA name server
1634 command << GetenvThreadSafe("NSPORT") << " "; // port of CORBA name server
1637 command << exe_name;
1638 command << " " << container_name;
1639 command << " Dummy";
1640 command << " " << hostname;
1641 command << " " << nb_proc_str;
1643 AddOmninamesParams(command);
1646 command << command_end;
1647 MESSAGE("[BuildCommandToLaunchPaCOProxyContainer] Command is: " << command.str());
1649 return command.str();
1653 SALOME_ContainerManager::BuildCommandToLaunchPaCONodeContainer(const Engines::ContainerParameters& params,
1654 const std::string & machine_file_name,
1655 SALOME_ContainerManager::actual_launch_machine_t & vect_machine,
1656 const std::string & proxy_hostname)
1659 std::string exe_name = "SALOME_ParallelContainerNode";
1660 exe_name += params.parallelLib.in();
1661 std::string container_name = params.container_name.in();
1663 // Convert nb_proc in string
1664 std::ostringstream nb_proc_stream;
1665 nb_proc_stream << params.nb_proc;
1667 // Get resource definition
1668 ParserResourcesType resource_definition =
1669 _resManager->GetResourceDefinition(params.resource_params.name.in());
1671 // Log environnement
1672 std::string log_type("");
1673 char * get_val = GetenvThreadSafe("PARALLEL_LOG");
1677 // Now the command is different according to paralleLib
1678 std::ostringstream command_nodes;
1679 std::ifstream machine_file(machine_file_name.c_str());
1680 if (std::string(params.parallelLib.in()) == "Dummy")
1682 for (int i= 0; i < params.nb_proc; i++)
1685 std::string hostname;
1686 std::getline(machine_file, hostname);
1687 MESSAGE("[BuildCommandToLaunchPaCONodeContainer] machine file name extracted is " << hostname);
1690 bool remote_execution = false;
1691 if (hostname != std::string(Kernel_Utils::GetHostname()))
1693 MESSAGE("[BuildCommandToLaunchPaCONodeContainer] remote machine case detected !");
1694 remote_execution = true;
1697 // For each node we have a new command
1698 // Generating the command
1699 std::ostringstream command_node_stream;
1700 std::string command_node_begin("");
1701 std::string command_node_end("");
1702 std::ostringstream node_number;
1704 std::string container_node_name = container_name + node_number.str();
1705 LogConfiguration(log_type, "node", container_node_name, hostname, command_node_begin, command_node_end);
1707 // Adding connection command
1708 // We can only have a remote execution with
1709 // a SALOME application
1710 if (remote_execution)
1712 ASSERT(GetenvThreadSafe("NSHOST"));
1713 ASSERT(GetenvThreadSafe("NSPORT"));
1715 command_node_stream << resource_definition.getAccessProtocolTypeStr();
1716 command_node_stream << " -l ";
1717 command_node_stream << resource_definition.UserName;
1718 command_node_stream << " " << hostname;
1719 command_node_stream << " " << resource_definition.AppliPath;
1720 command_node_stream << "/runRemote.sh ";
1721 command_node_stream << GetenvThreadSafe("NSHOST") << " "; // hostname of CORBA name server
1722 command_node_stream << GetenvThreadSafe("NSPORT") << " "; // port of CORBA name server
1725 command_node_stream << exe_name;
1726 command_node_stream << " " << container_name;
1727 command_node_stream << " " << params.parallelLib.in();
1728 command_node_stream << " " << proxy_hostname;
1729 command_node_stream << " " << node_number.str();
1730 command_node_stream << " -";
1731 AddOmninamesParams(command_node_stream);
1733 command_nodes << command_node_begin << command_node_stream.str() << command_node_end;
1734 vect_machine.push_back(hostname);
1738 else if (std::string(params.parallelLib.in()) == "Mpi")
1741 std::string hostname;
1742 std::getline(machine_file, hostname, ' ');
1743 MESSAGE("[BuildCommandToLaunchPaCONodeContainer] machine file name extracted is " << hostname);
1746 bool remote_execution = false;
1747 if (hostname != std::string(Kernel_Utils::GetHostname()))
1749 MESSAGE("[BuildCommandToLaunchPaCONodeContainer] remote machine case detected !");
1750 remote_execution = true;
1753 // In case of Mpi and Remote, we copy machine_file in the applipath
1754 // scp mpi_machine_file user@machine:Path
1755 std::ostringstream command_remote_stream;
1756 std::string::size_type last = machine_file_name.find_last_of("/");
1757 if (last == std::string::npos)
1760 if (resource_definition.Protocol == rsh)
1761 command_remote_stream << "rcp ";
1763 command_remote_stream << "scp ";
1764 command_remote_stream << machine_file_name << " ";
1765 command_remote_stream << resource_definition.UserName << "@";
1766 command_remote_stream << hostname << ":" << resource_definition.AppliPath;
1767 command_remote_stream << "/" << machine_file_name.substr(last+1);
1769 int status = SystemThreadSafe(command_remote_stream.str().c_str());
1772 INFOS("copy of the MPI machine file failed ! - sorry !");
1776 // Generating the command
1777 std::string command_begin("");
1778 std::string command_end("");
1780 LogConfiguration(log_type, "nodes", container_name, hostname, command_begin, command_end);
1781 command_nodes << command_begin;
1783 // Adding connection command
1784 // We can only have a remote execution with
1785 // a SALOME application
1786 if (remote_execution)
1788 ASSERT(GetenvThreadSafe("NSHOST"));
1789 ASSERT(GetenvThreadSafe("NSPORT"));
1791 command_nodes << resource_definition.getAccessProtocolTypeStr();
1792 command_nodes << " -l ";
1793 command_nodes << resource_definition.UserName;
1794 command_nodes << " " << hostname;
1795 command_nodes << " " << resource_definition.AppliPath;
1796 command_nodes << "/runRemote.sh ";
1797 command_nodes << GetenvThreadSafe("NSHOST") << " "; // hostname of CORBA name server
1798 command_nodes << GetenvThreadSafe("NSPORT") << " "; // port of CORBA name server
1801 if (resource_definition.mpi == lam)
1803 command_nodes << "mpiexec -ssi boot ";
1804 command_nodes << "-machinefile " << machine_file_name << " ";
1805 command_nodes << "-n " << params.nb_proc;
1809 command_nodes << "mpirun -np " << params.nb_proc;
1811 command_nodes << " " << exe_name;
1812 command_nodes << " " << container_name;
1813 command_nodes << " " << params.parallelLib.in();
1814 command_nodes << " " << proxy_hostname;
1815 command_nodes << " -";
1816 AddOmninamesParams(command_nodes);
1818 // We don't put hostname, because nodes are registered in the resource of the proxy
1819 for (int i= 0; i < params.nb_proc; i++)
1820 vect_machine.push_back(proxy_hostname);
1822 command_nodes << command_end;
1824 return command_nodes.str();
1828 SALOME_ContainerManager::LogConfiguration(const std::string & log_type,
1829 const std::string & exe_type,
1830 const std::string & container_name,
1831 const std::string & hostname,
1832 std::string & begin,
1835 if(log_type == "xterm")
1837 begin = "xterm -e \"export LD_LIBRARY_PATH=$LD_LIBRARY_PATH; export PATH=$PATH;";
1840 else if(log_type == "xterm_debug")
1842 begin = "xterm -e \"export LD_LIBRARY_PATH=$LD_LIBRARY_PATH; export PATH=$PATH;";
1847 // default into a file...
1848 std::string logFilename = "/tmp/" + container_name + "_" + hostname + "_" + exe_type + "_";
1849 logFilename += std::string(GetenvThreadSafe("USER")) + ".log";
1850 end = " > " + logFilename + " 2>&1 & ";
1855 SALOME_ContainerManager::LaunchPaCOProxyContainer(const std::string& command,
1856 const Engines::ContainerParameters& params,
1857 const std::string & hostname)
1859 PaCO::InterfaceManager_ptr container_proxy = PaCO::InterfaceManager::_nil();
1861 MESSAGE("[LaunchPaCOProxyContainer] Launch command");
1862 int status = SystemThreadSafe(command.c_str());
1864 INFOS("[LaunchPaCOProxyContainer] failed : system command status -1");
1865 return container_proxy;
1867 else if (status == 217) {
1868 INFOS("[LaunchPaCOProxyContainer] failed : system command status 217");
1869 return container_proxy;
1872 int count(GetTimeOutToLoaunchServer());
1873 CORBA::Object_var obj = CORBA::Object::_nil();
1874 std::string containerNameInNS = _NS->BuildContainerNameForNS(params.container_name.in(),
1876 MESSAGE("[LaunchParallelContainer] Waiting for Parallel Container proxy : " << containerNameInNS);
1878 while (CORBA::is_nil(obj) && count)
1882 obj = _NS->Resolve(containerNameInNS.c_str());
1887 container_proxy = PaCO::InterfaceManager::_narrow(obj);
1889 catch(CORBA::SystemException& e)
1891 INFOS("[StarPaCOPPContainer] Exception in _narrow after LaunchParallelContainer for proxy !");
1892 INFOS("CORBA::SystemException : " << e);
1893 return container_proxy;
1895 catch(CORBA::Exception& e)
1897 INFOS("[StarPaCOPPContainer] Exception in _narrow after LaunchParallelContainer for proxy !");
1898 INFOS("CORBA::Exception" << e);
1899 return container_proxy;
1903 INFOS("[StarPaCOPPContainer] Exception in _narrow after LaunchParallelContainer for proxy !");
1904 INFOS("Unknown exception !");
1905 return container_proxy;
1907 if (CORBA::is_nil(container_proxy))
1909 INFOS("[StarPaCOPPContainer] PaCO::InterfaceManager::_narrow returns NIL !");
1910 return container_proxy;
1915 //=============================================================================
1916 /*! This method launches the parallel container.
1917 * It will may be placed on the ressources manager.
1919 * \param command to launch
1920 * \param container's parameters
1921 * \param name of the container
1923 * \return CORBA container reference
1925 //=============================================================================
1927 SALOME_ContainerManager::LaunchPaCONodeContainer(const std::string& command,
1928 const Engines::ContainerParameters& params,
1929 const std::string& name,
1930 SALOME_ContainerManager::actual_launch_machine_t & vect_machine)
1932 INFOS("[LaunchPaCONodeContainer] Launch command");
1933 int status = SystemThreadSafe(command.c_str());
1935 INFOS("[LaunchPaCONodeContainer] failed : system command status -1");
1938 else if (status == 217) {
1939 INFOS("[LaunchPaCONodeContainer] failed : system command status 217");
1943 INFOS("[LaunchPaCONodeContainer] Waiting for the nodes of the parallel container");
1944 // We are waiting all the nodes
1945 for (int i = 0; i < params.nb_proc; i++)
1947 CORBA::Object_var obj = CORBA::Object::_nil();
1948 std::string theMachine(vect_machine[i]);
1950 std::ostringstream tmp;
1952 std::string proc_number = tmp.str();
1953 std::string container_node_name = name + proc_number;
1954 std::string containerNameInNS = _NS->BuildContainerNameForNS((char*) container_node_name.c_str(), theMachine.c_str());
1955 INFOS("[LaunchPaCONodeContainer] Waiting for Parallel Container node " << containerNameInNS << " on " << theMachine);
1956 int count(GetTimeOutToLoaunchServer());
1957 while (CORBA::is_nil(obj) && count) {
1960 obj = _NS->Resolve(containerNameInNS.c_str());
1962 if (CORBA::is_nil(obj))
1964 INFOS("[LaunchPaCONodeContainer] Launch of node failed (or not found) !");
1973 Engines::Container_ptr
1974 SALOME_ContainerManager::StartPaCOPPContainer(const Engines::ContainerParameters& params,
1975 std::string resource_selected)
1977 Engines::Container_ptr ret = Engines::Container::_nil();
1978 INFOS("[StarPaCOPPContainer] is disabled !");
1979 INFOS("[StarPaCOPPContainer] recompile SALOME Kernel to enable PaCO++ parallel extension");
1984 SALOME_ContainerManager::BuildCommandToLaunchPaCOProxyContainer(const Engines::ContainerParameters& params,
1985 std::string machine_file_name,
1986 std::string & proxy_hostname)
1992 SALOME_ContainerManager::BuildCommandToLaunchPaCONodeContainer(const Engines::ContainerParameters& params,
1993 const std::string & machine_file_name,
1994 SALOME_ContainerManager::actual_launch_machine_t & vect_machine,
1995 const std::string & proxy_hostname)
2000 SALOME_ContainerManager::LogConfiguration(const std::string & log_type,
2001 const std::string & exe_type,
2002 const std::string & container_name,
2003 const std::string & hostname,
2004 std::string & begin,
2010 SALOME_ContainerManager::LaunchPaCOProxyContainer(const std::string& command,
2011 const Engines::ContainerParameters& params,
2012 const std::string& hostname)
2014 CORBA::Object_ptr ret = CORBA::Object::_nil();
2019 SALOME_ContainerManager::LaunchPaCONodeContainer(const std::string& command,
2020 const Engines::ContainerParameters& params,
2021 const std::string& name,
2022 SALOME_ContainerManager::actual_launch_machine_t & vect_machine)