1 // Copyright (C) 2007-2017 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)
51 #define getpid _getpid
54 #ifdef WITH_PACO_PARALLEL
58 const int SALOME_ContainerManager::TIME_OUT_TO_LAUNCH_CONT=60;
60 const char *SALOME_ContainerManager::_ContainerManagerNameInNS =
63 omni_mutex SALOME_ContainerManager::_numInstanceMutex;
65 Utils_Mutex SALOME_ContainerManager::_getenvMutex;
67 Utils_Mutex SALOME_ContainerManager::_systemMutex;
69 //=============================================================================
73 * Define a CORBA single thread policy for the server, which avoid to deal
74 * with non thread-safe usage like Change_Directory in SALOME naming service
76 //=============================================================================
78 SALOME_ContainerManager::SALOME_ContainerManager(CORBA::ORB_ptr orb, PortableServer::POA_var poa, SALOME_NamingService *ns)
81 MESSAGE("constructor");
83 _resManager = new SALOME_ResourcesManager_Client(ns);
85 PortableServer::POAManager_var pman = poa->the_POAManager();
86 _orb = CORBA::ORB::_duplicate(orb) ;
87 CORBA::PolicyList policies;
89 PortableServer::ThreadPolicy_var threadPol(poa->create_thread_policy(PortableServer::ORB_CTRL_MODEL));
90 policies[0] = PortableServer::ThreadPolicy::_duplicate(threadPol);
92 _poa = poa->create_POA("MThreadPOA",pman,policies);
94 PortableServer::ObjectId_var id = _poa->activate_object(this);
95 CORBA::Object_var obj = _poa->id_to_reference(id);
96 Engines::ContainerManager_var refContMan =
97 Engines::ContainerManager::_narrow(obj);
99 _NS->Register(refContMan,_ContainerManagerNameInNS);
100 _isAppliSalomeDefined = (GetenvThreadSafe("APPLI") != 0);
105 // the urifile name depends on pid of the process
106 std::stringstream urifile;
107 urifile << GetenvThreadSafeAsString("HOME") << "/.urifile_" << getpid();
108 setenv("OMPI_URI_FILE",urifile.str().c_str(),1);
109 if( GetenvThreadSafe("OMPI_URI_FILE") != NULL ){
110 // get the pid of all ompi-server
111 std::set<pid_t> thepids1 = getpidofprogram("ompi-server");
112 // launch a new ompi-server
114 command = "ompi-server -r ";
115 command += GetenvThreadSafeAsString("OMPI_URI_FILE");
116 int status=SystemThreadSafe(command.c_str());
118 throw SALOME_Exception("Error when launching ompi-server");
119 // get the pid of all ompi-server
120 std::set<pid_t> thepids2 = getpidofprogram("ompi-server");
121 // my ompi-server is the new one
122 std::set<pid_t>::const_iterator it;
123 for(it=thepids2.begin();it!=thepids2.end();it++)
124 if(thepids1.find(*it) == thepids1.end())
125 _pid_mpiServer = *it;
126 if(_pid_mpiServer < 0)
127 throw SALOME_Exception("Error when getting ompi-server id");
131 // get the pid of all hydra_nameserver
132 std::set<pid_t> thepids1 = getpidofprogram("hydra_nameserver");
133 // launch a new hydra_nameserver
135 command = "hydra_nameserver &";
136 SystemThreadSafe(command.c_str());
137 // get the pid of all hydra_nameserver
138 std::set<pid_t> thepids2 = getpidofprogram("hydra_nameserver");
139 // my hydra_nameserver is the new one
140 std::set<pid_t>::const_iterator it;
141 for(it=thepids2.begin();it!=thepids2.end();it++)
142 if(thepids1.find(*it) == thepids1.end())
143 _pid_mpiServer = *it;
147 MESSAGE("constructor end");
150 //=============================================================================
154 //=============================================================================
156 SALOME_ContainerManager::~SALOME_ContainerManager()
158 MESSAGE("destructor");
162 if( GetenvThreadSafe("OMPI_URI_FILE") != NULL ){
163 // kill my ompi-server
164 if( kill(_pid_mpiServer,SIGTERM) != 0 )
165 throw SALOME_Exception("Error when killing ompi-server");
167 int status=SystemThreadSafe("rm -f ${OMPI_URI_FILE}");
169 throw SALOME_Exception("Error when removing urifile");
172 // kill my hydra_nameserver
173 if(_pid_mpiServer > -1)
174 if( kill(_pid_mpiServer,SIGTERM) != 0 )
175 throw SALOME_Exception("Error when killing hydra_nameserver");
180 //=============================================================================
181 //! shutdown all the containers, then the ContainerManager servant
184 //=============================================================================
186 void SALOME_ContainerManager::Shutdown()
189 ShutdownContainers();
190 _NS->Destroy_Name(_ContainerManagerNameInNS);
191 PortableServer::ObjectId_var oid = _poa->servant_to_id(this);
192 _poa->deactivate_object(oid);
195 //=============================================================================
196 //! Loop on all the containers listed in naming service, ask shutdown on each
199 //=============================================================================
201 void SALOME_ContainerManager::ShutdownContainers()
203 MESSAGE("ShutdownContainers");
205 SALOME::Session_var session = SALOME::Session::_nil();
207 CORBA::Object_var objS = _NS->Resolve("/Kernel/Session");
208 if (!CORBA::is_nil(objS))
210 session = SALOME::Session::_narrow(objS);
211 if (!CORBA::is_nil(session))
212 pid = session->getPID();
216 isOK = _NS->Change_Directory("/Containers");
218 std::vector<std::string> vec = _NS->list_directory_recurs();
219 std::list<std::string> lstCont;
220 for(std::vector<std::string>::iterator iter = vec.begin();iter!=vec.end();iter++)
223 CORBA::Object_var obj=_NS->Resolve((*iter).c_str());
226 Engines::Container_var cont=Engines::Container::_narrow(obj);
227 if(!CORBA::is_nil(cont) && pid != cont->getPID())
228 lstCont.push_back((*iter));
230 catch(const CORBA::Exception& e)
232 // ignore this entry and continue
235 MESSAGE("Container list: ");
236 for(std::list<std::string>::iterator iter=lstCont.begin();iter!=lstCont.end();iter++){
239 for(std::list<std::string>::iterator iter=lstCont.begin();iter!=lstCont.end();iter++)
244 CORBA::Object_var obj=_NS->Resolve((*iter).c_str());
245 Engines::Container_var cont=Engines::Container::_narrow(obj);
246 if(!CORBA::is_nil(cont))
248 MESSAGE("ShutdownContainers: " << (*iter));
252 MESSAGE("ShutdownContainers: no container ref for " << (*iter));
254 catch(CORBA::SystemException& e)
256 INFOS("CORBA::SystemException ignored : " << e);
258 catch(CORBA::Exception&)
260 INFOS("CORBA::Exception ignored.");
264 INFOS("Unknown exception ignored.");
270 //=============================================================================
271 //! Give a suitable Container given constraints
273 * \param params Container Parameters required for the container
274 * \return the container or nil
276 //=============================================================================
277 Engines::Container_ptr SALOME_ContainerManager::GiveContainer(const Engines::ContainerParameters& params)
279 std::string machFile;
280 Engines::Container_ptr ret(Engines::Container::_nil());
282 // Step 0: Default mode is start
283 Engines::ContainerParameters local_params(params);
284 if (std::string(local_params.mode.in()) == "")
285 local_params.mode = CORBA::string_dup("start");
286 std::string mode = local_params.mode.in();
287 MESSAGE("[GiveContainer] starting with mode: " << mode);
289 // Step 1: Find Container for find and findorstart mode
290 if (mode == "find" || mode == "findorstart")
292 ret = FindContainer(params, params.resource_params.resList);
293 if(!CORBA::is_nil(ret))
299 MESSAGE("[GiveContainer] no container found");
309 // Step 2: Get all possibleResources from the parameters
310 // Consider only resources that can run containers
311 resourceParams resource_params = resourceParameters_CORBAtoCPP(local_params.resource_params);
312 resource_params.can_run_containers = true;
313 std::vector<std::string> possibleResources = _resManager->GetFittingResources(resource_params);
314 MESSAGE("[GiveContainer] - length of possible resources " << possibleResources.size());
315 std::vector<std::string> local_resources;
317 // Step 3: if mode is "get" keep only machines with existing containers
320 for(unsigned int i=0; i < possibleResources.size(); i++)
322 Engines::Container_ptr cont = FindContainer(params, possibleResources[i]);
325 if(!cont->_non_existent())
326 local_resources.push_back(possibleResources[i]);
328 catch(CORBA::Exception&) {}
331 // if local_resources is empty, we cannot give a container
332 if (local_resources.size() == 0)
334 MESSAGE("[GiveContainer] cannot find a container for mode get");
339 local_resources = possibleResources;
341 // Step 4: select the resource where to get/start the container
342 bool resource_available = true;
343 std::string resource_selected;
344 std::vector<std::string> resources = local_resources;
345 while (resource_available)
347 if (resources.size() == 0)
348 resource_available = false;
353 resource_selected = _resManager->Find(params.resource_params.policy.in(), resources);
354 // Remove resource_selected from vector
355 std::vector<std::string>::iterator it;
356 for (it=resources.begin() ; it < resources.end(); it++ )
357 if (*it == resource_selected)
363 catch(const SALOME_Exception &ex)
365 MESSAGE("[GiveContainer] Exception in ResourceManager find !: " << ex.what());
368 MESSAGE("[GiveContainer] Resource selected is: " << resource_selected);
370 // Step 5: Create container name
371 ParserResourcesType resource_definition = _resManager->GetResourceDefinition(resource_selected);
372 std::string hostname(resource_definition.HostName);
373 std::string containerNameInNS;
376 if ( params.nb_proc <= 0 )
379 nbproc = params.nb_proc;
382 if( GetenvThreadSafe("LIBBATCH_NODEFILE") != NULL )
383 machFile = machinesFile(nbproc);
385 catch(const SALOME_Exception & ex)
387 std::string err_msg = ex.what();
388 err_msg += params.container_name;
389 INFOS(err_msg.c_str());
392 // A mpi parallel container register on zero node in NS
393 std::string mpiZeroNode = GetMPIZeroNode(resource_selected,machFile).c_str();
394 containerNameInNS = _NS->BuildContainerNameForNS(params, mpiZeroNode.c_str());
397 containerNameInNS = _NS->BuildContainerNameForNS(params, hostname.c_str());
398 MESSAGE("[GiveContainer] Container name in the naming service: " << containerNameInNS);
400 // Step 6: check if the name exists in naming service
401 //if params.mode == "getorstart" or "get" use the existing container
402 //if params.mode == "start" shutdown the existing container before launching a new one with that name
404 { // critical section
405 Utils_Locker lock (&_giveContainerMutex1);
406 CORBA::Object_var obj = _NS->Resolve(containerNameInNS.c_str());
407 if (!CORBA::is_nil(obj))
411 Engines::Container_var cont=Engines::Container::_narrow(obj);
412 if(!cont->_non_existent())
414 if(std::string(params.mode.in())=="getorstart" || std::string(params.mode.in())=="get"){
415 return cont._retn(); /* the container exists and params.mode is getorstart or get use it*/
419 INFOS("[GiveContainer] A container is already registered with the name: " << containerNameInNS << ", shutdown the existing container");
420 cont->Shutdown(); // shutdown the registered container if it exists
424 catch(CORBA::Exception&)
426 INFOS("[GiveContainer] CORBA::Exception ignored when trying to get the container - we start a new one");
429 } // end critical section
430 Engines::Container_var cont = LaunchContainer(params, resource_selected, hostname, machFile, containerNameInNS);
431 if (!CORBA::is_nil(cont))
433 INFOS("[GiveContainer] container " << containerNameInNS << " launched");
438 INFOS("[GiveContainer] Failed to launch container on resource " << resource_selected);
443 // We were not able to launch the container
444 INFOS("[GiveContainer] Cannot launch the container on the following selected resources:")
445 std::vector<std::string>::iterator it;
446 for (it=local_resources.begin() ; it < local_resources.end(); it++ )
447 INFOS("[GiveContainer] " << *it)
451 Engines::Container_ptr
452 SALOME_ContainerManager::LaunchContainer(const Engines::ContainerParameters& params,
453 const std::string & resource_selected,
454 const std::string & hostname,
455 const std::string & machFile,
456 const std::string & containerNameInNS)
458 std::string user,command,logFilename,tmpFileName;
460 Engines::Container_ptr ret(Engines::Container::_nil());
461 {//start of critical section
462 Utils_Locker lock (&_giveContainerMutex1);
463 // Step 1: type of container: PaCO, Exe, Mpi or Classic
464 // Mpi already tested in step 5, specific code on BuildCommandToLaunch Local/Remote Container methods
465 // TODO -> separates Mpi from Classic/Exe
467 std::string container_exe = "SALOME_Container"; // Classic container
468 Engines::ContainerParameters local_params(params);
472 CORBA::String_var container_exe_tmp;
473 CORBA::Object_var obj = _NS->Resolve("/Kernel/ModulCatalog");
474 SALOME_ModuleCatalog::ModuleCatalog_var Catalog = SALOME_ModuleCatalog::ModuleCatalog::_narrow(obj) ;
475 if (CORBA::is_nil (Catalog))
477 INFOS("[GiveContainer] Module Catalog is not found -> cannot launch a container");
480 // Loop through component list
481 for(unsigned int i=0; i < local_params.resource_params.componentList.length(); i++)
483 const char* compoi = local_params.resource_params.componentList[i];
484 SALOME_ModuleCatalog::Acomponent_var compoInfo = Catalog->GetComponent(compoi);
485 if (CORBA::is_nil (compoInfo))
489 SALOME_ModuleCatalog::ImplType impl=compoInfo->implementation_type();
490 container_exe_tmp=compoInfo->implementation_name();
491 if(impl==SALOME_ModuleCatalog::CEXE)
495 INFOS("ContainerManager Error: you can't have 2 CEXE component in the same container" );
496 return Engines::Container::_nil();
498 MESSAGE("[GiveContainer] Exe container found !: " << container_exe_tmp);
499 container_exe = container_exe_tmp.in();
504 catch (ServiceUnreachable&)
506 INFOS("Caught exception: Naming Service Unreachable");
511 INFOS("Caught unknown exception.");
515 // Step 2: test resource
516 // Only if an application directory is set
517 if(hostname != Kernel_Utils::GetHostname() && _isAppliSalomeDefined)
519 // Preparing remote command
520 std::string command = "";
521 const ParserResourcesType resInfo(_resManager->GetResourceDefinition(resource_selected));
522 command = getCommandToRunRemoteProcess(resInfo.Protocol, resInfo.HostName, resInfo.UserName);
523 if (resInfo.AppliPath != "")
524 command += resInfo.AppliPath;
527 ASSERT(GetenvThreadSafe("APPLI"));
528 command += GetenvThreadSafeAsString("APPLI");
530 command += "/runRemote.sh ";
531 ASSERT(GetenvThreadSafe("NSHOST"));
532 command += GetenvThreadSafeAsString("NSHOST"); // hostname of CORBA name server
534 ASSERT(GetenvThreadSafe("NSPORT"));
535 command += GetenvThreadSafeAsString("NSPORT"); // port of CORBA name server
536 command += " \"ls /tmp >/dev/null 2>&1\"";
538 // Launch remote command
539 int status = SystemThreadSafe(command.c_str());
542 // Error on resource - cannot launch commands
543 INFOS("[LaunchContainer] Cannot launch commands on machine " << hostname);
544 INFOS("[LaunchContainer] Command was " << command);
546 INFOS("[LaunchContainer] Command status is " << WEXITSTATUS(status));
548 return Engines::Container::_nil();
552 // Step 3: start a new container
553 // Check if a PaCO container
555 if (std::string(local_params.parallelLib.in()) != "")
557 ret = StartPaCOPPContainer(params, resource_selected);
560 // Other type of containers...
561 MESSAGE("[GiveContainer] Try to launch a new container on " << resource_selected);
562 // if a parallel container is launched in batch job, command is: "mpirun -np nbproc -machinefile nodesfile SALOME_MPIContainer"
563 if( GetenvThreadSafe("LIBBATCH_NODEFILE") != NULL && params.isMPI )
564 command = BuildCommandToLaunchLocalContainer(params, machFile, container_exe, tmpFileName);
565 // if a container is launched on localhost, command is "SALOME_Container" or "mpirun -np nbproc SALOME_MPIContainer"
566 else if(hostname == Kernel_Utils::GetHostname())
567 command = BuildCommandToLaunchLocalContainer(params, machFile, container_exe, tmpFileName);
568 // if a container is launched in remote mode, command is "ssh resource_selected SALOME_Container" or "ssh resource_selected mpirun -np nbproc SALOME_MPIContainer"
570 command = BuildCommandToLaunchRemoteContainer(resource_selected, params, container_exe);
572 //redirect stdout and stderr in a file
574 logFilename=GetenvThreadSafeAsString("TEMP");
576 user = GetenvThreadSafeAsString( "USERNAME" );
578 user = GetenvThreadSafeAsString( "USER" );
580 user = GetenvThreadSafeAsString( "LOGNAME" );
582 char* val = GetenvThreadSafe("SALOME_TMP_DIR");
585 struct stat file_info;
586 stat(val, &file_info);
587 bool is_dir = S_ISDIR(file_info.st_mode);
588 if (is_dir)logFilename=val;
589 else std::cerr << "SALOME_TMP_DIR environment variable is not a directory use /tmp instead" << std::endl;
593 logFilename += _NS->ContainerName(params)+"_"+ resource_selected +"_"+user;
594 std::ostringstream tmp;
595 tmp << "_" << getpid();
596 logFilename += tmp.str();
597 logFilename += ".log" ;
598 command += " > " + logFilename + " 2>&1";
599 MakeTheCommandToBeLaunchedASync(command);
601 // launch container with a system call
602 status=SystemThreadSafe(command.c_str());
603 }//end of critical of section
607 INFOS("[LaunchContainer] command failed (system command status -1): " << command);
608 RmTmpFile(tmpFileName); // command file can be removed here
609 return Engines::Container::_nil();
611 else if (status == 217)
613 INFOS("[LaunchContainer] command failed (system command status 217): " << command);
614 RmTmpFile(tmpFileName); // command file can be removed here
615 return Engines::Container::_nil();
619 // Step 4: Wait for the container
620 int count(GetTimeOutToLoaunchServer());
621 INFOS("[GiveContainer] waiting " << count << " second steps container " << containerNameInNS);
622 while (CORBA::is_nil(ret) && count)
626 MESSAGE("[GiveContainer] step " << count << " Waiting for container on " << resource_selected);
627 CORBA::Object_var obj(_NS->Resolve(containerNameInNS.c_str()));
628 ret=Engines::Container::_narrow(obj);
630 if (CORBA::is_nil(ret))
632 INFOS("[GiveContainer] was not able to launch container " << containerNameInNS);
636 // Setting log file name
637 logFilename=":"+logFilename;
638 logFilename="@"+Kernel_Utils::GetHostname()+logFilename;//threadsafe
639 logFilename=user+logFilename;
640 ret->logfilename(logFilename.c_str());
641 RmTmpFile(tmpFileName); // command file can be removed here
647 //=============================================================================
648 //! Find a container given constraints (params) on a list of machines (possibleComputers)
649 //! agy : this method is ThreadSafe
653 //=============================================================================
655 Engines::Container_ptr SALOME_ContainerManager::FindContainer(const Engines::ContainerParameters& params, const Engines::ResourceList& possibleResources)
657 MESSAGE("[FindContainer] FindContainer on " << possibleResources.length() << " resources");
658 for(unsigned int i=0; i < possibleResources.length();i++)
660 Engines::Container_ptr cont = FindContainer(params, possibleResources[i].in());
661 if(!CORBA::is_nil(cont))
664 MESSAGE("[FindContainer] no container found");
665 return Engines::Container::_nil();
668 //=============================================================================
669 //! Find a container given constraints (params) on a machine (theMachine)
670 //! agy : this method is ThreadSafe
674 //=============================================================================
676 Engines::Container_ptr
677 SALOME_ContainerManager::FindContainer(const Engines::ContainerParameters& params, const std::string& resource)
679 ParserResourcesType resource_definition = _resManager->GetResourceDefinition(resource);
680 std::string hostname(resource_definition.HostName);
681 std::string containerNameInNS(_NS->BuildContainerNameForNS(params, hostname.c_str()));
682 MESSAGE("[FindContainer] Try to find a container " << containerNameInNS << " on resource " << resource);
683 CORBA::Object_var obj = _NS->Resolve(containerNameInNS.c_str());
686 if(obj->_non_existent())
687 return Engines::Container::_nil();
689 return Engines::Container::_narrow(obj);
691 catch(const CORBA::Exception& e)
693 return Engines::Container::_nil();
698 bool isPythonContainer(const char* ContainerName);
700 //=============================================================================
702 * This is no longer valid (C++ container are also python containers)
704 //=============================================================================
705 bool isPythonContainer(const char* ContainerName)
707 return false; // VSR 02/08/2013: Python containers are no more supported
709 int len = strlen(ContainerName);
712 if (strcmp(ContainerName + len - 2, "Py") == 0)
718 //=============================================================================
720 * Builds the script to be launched
722 * If SALOME Application not defined ($APPLI),
723 * see BuildTempFileToLaunchRemoteContainer()
725 * Else rely on distant configuration. Command is under the form (example):
726 * ssh user@machine distantPath/runRemote.sh hostNS portNS WORKINGDIR workingdir \
727 * SALOME_Container containerName &"
729 * - where user is ommited if not specified in CatalogResources,
730 * - where distant path is always relative to user@machine $HOME, and
731 * equal to $APPLI if not specified in CatalogResources,
732 * - where hostNS is the hostname of CORBA naming server (set by scripts to
733 * use to launch SALOME and servers in $APPLI: runAppli.sh, runRemote.sh)
734 * - where portNS is the port used by CORBA naming server (set by scripts to
735 * use to launch SALOME and servers in $APPLI: runAppli.sh, runRemote.sh)
736 * - where workingdir is the requested working directory for the container.
737 * If WORKINGDIR (and workingdir) is not present the working dir will be $HOME
739 //=============================================================================
742 SALOME_ContainerManager::BuildCommandToLaunchRemoteContainer(const std::string& resource_name, const Engines::ContainerParameters& params, const std::string& container_exe) const
744 std::string command,tmpFileName;
745 if (!_isAppliSalomeDefined)
746 command = BuildTempFileToLaunchRemoteContainer(resource_name, params, tmpFileName);
750 const ParserResourcesType resInfo(_resManager->GetResourceDefinition(resource_name));
754 if ( params.nb_proc <= 0 )
757 nbproc = params.nb_proc;
760 // "ssh -l user machine distantPath/runRemote.sh hostNS portNS WORKINGDIR workingdir
761 // SALOME_Container containerName &"
762 command = getCommandToRunRemoteProcess(resInfo.Protocol, resInfo.HostName, resInfo.UserName);
764 if (resInfo.AppliPath != "")
765 command += resInfo.AppliPath; // path relative to user@machine $HOME
768 ASSERT(GetenvThreadSafe("APPLI"));
769 command += GetenvThreadSafeAsString("APPLI"); // path relative to user@machine $HOME
772 command += "/runRemote.sh ";
774 ASSERT(GetenvThreadSafe("NSHOST"));
775 command += GetenvThreadSafeAsString("NSHOST"); // hostname of CORBA name server
778 ASSERT(GetenvThreadSafe("NSPORT"));
779 command += GetenvThreadSafeAsString("NSPORT"); // port of CORBA name server
781 std::string wdir = params.workingdir.in();
784 command += " WORKINGDIR ";
786 if(wdir == "$TEMPDIR")
788 command += wdir; // requested working directory
794 command += " mpirun -np ";
795 std::ostringstream o;
799 command += "-x PATH,LD_LIBRARY_PATH,OMNIORB_CONFIG,SALOME_trace ";
800 #elif defined(OPEN_MPI)
801 if( GetenvThreadSafe("OMPI_URI_FILE") == NULL )
802 command += "-x PATH -x LD_LIBRARY_PATH -x OMNIORB_CONFIG -x SALOME_trace";
804 command += "-x PATH -x LD_LIBRARY_PATH -x OMNIORB_CONFIG -x SALOME_trace -ompi-server file:";
805 command += GetenvThreadSafeAsString("OMPI_URI_FILE");
808 command += "-nameserver " + Kernel_Utils::GetHostname();
810 command += " SALOME_MPIContainer ";
813 command += " " +container_exe+ " ";
815 command += _NS->ContainerName(params);
817 AddOmninamesParams(command);
819 MESSAGE("command =" << command);
825 //=============================================================================
827 * builds the command to be launched.
829 //=============================================================================
830 std::string SALOME_ContainerManager::BuildCommandToLaunchLocalContainer(const Engines::ContainerParameters& params, const std::string& machinesFile, const std::string& container_exe, std::string& tmpFileName) const
832 tmpFileName = BuildTemporaryFileName();
836 std::ostringstream o;
842 if ( params.nb_proc <= 0 )
845 nbproc = params.nb_proc;
849 if( GetenvThreadSafe("LIBBATCH_NODEFILE") != NULL )
850 o << "-machinefile " << machinesFile << " ";
853 o << "-x PATH,LD_LIBRARY_PATH,OMNIORB_CONFIG,SALOME_trace ";
854 #elif defined(OPEN_MPI)
855 if( GetenvThreadSafe("OMPI_URI_FILE") == NULL )
856 o << "-x PATH -x LD_LIBRARY_PATH -x OMNIORB_CONFIG -x SALOME_trace";
859 o << "-x PATH -x LD_LIBRARY_PATH -x OMNIORB_CONFIG -x SALOME_trace -ompi-server file:";
860 o << GetenvThreadSafeAsString("OMPI_URI_FILE");
863 o << "-nameserver " + Kernel_Utils::GetHostname();
866 if (isPythonContainer(params.container_name))
867 o << " pyMPI SALOME_ContainerPy.py ";
869 o << " SALOME_MPIContainer ";
874 std::string wdir=params.workingdir.in();
877 // a working directory is requested
878 if(wdir == "$TEMPDIR")
880 // a new temporary directory is requested
881 std::string dir = Kernel_Utils::GetTmpDir();
883 o << "cd /d " << dir << std::endl;
885 o << "cd " << dir << ";";
891 // a permanent directory is requested use it or create it
893 o << "mkdir " + wdir << std::endl;
894 o << "cd /D " + wdir << std::endl;
896 o << "mkdir -p " << wdir << " && cd " << wdir + ";";
901 if (isPythonContainer(params.container_name))
902 o << "SALOME_ContainerPy.py ";
904 o << container_exe + " ";
908 o << _NS->ContainerName(params);
910 AddOmninamesParams(o);
912 std::ofstream command_file( tmpFileName.c_str() );
913 command_file << o.str();
914 command_file.close();
917 chmod(tmpFileName.c_str(), 0x1ED);
919 command = tmpFileName;
921 MESSAGE("Command is file ... " << command);
922 MESSAGE("Command is ... " << o.str());
927 //=============================================================================
929 * removes the generated temporary file in case of a remote launch.
930 * This method is thread safe
932 //=============================================================================
934 void SALOME_ContainerManager::RmTmpFile(std::string& tmpFileName)
936 int lenght = tmpFileName.size();
940 std::string command = "del /F ";
942 std::string command = "rm ";
945 command += tmpFileName.substr(0, lenght - 3 );
947 command += tmpFileName;
949 SystemThreadSafe(command.c_str());
950 //if dir is empty - remove it
951 std::string tmp_dir = Kernel_Utils::GetDirByPath( tmpFileName );
952 if ( Kernel_Utils::IsEmptyDir( tmp_dir ) )
955 command = "del /F " + tmp_dir;
957 command = "rmdir " + tmp_dir;
959 SystemThreadSafe(command.c_str());
964 //=============================================================================
966 * add to command all options relative to naming service.
968 //=============================================================================
970 void SALOME_ContainerManager::AddOmninamesParams(std::string& command) const
972 std::ostringstream oss;
973 AddOmninamesParams(oss);
977 //=============================================================================
979 * add to command all options relative to naming service.
981 //=============================================================================
983 void SALOME_ContainerManager::AddOmninamesParams(std::ostream& fileStream) const
985 AddOmninamesParams(fileStream,_NS);
988 //=============================================================================
990 * add to command all options relative to naming service.
992 //=============================================================================
994 void SALOME_ContainerManager::AddOmninamesParams(std::ostream& fileStream, SALOME_NamingService *ns)
996 CORBA::String_var iorstr(ns->getIORaddr());
997 fileStream << "ORBInitRef NameService=";
998 fileStream << iorstr;
1001 void SALOME_ContainerManager::MakeTheCommandToBeLaunchedASync(std::string& command)
1004 command = "%PYTHONBIN% -c \"import subprocess ; subprocess.Popen(r'" + command + "').pid\"";
1010 int SALOME_ContainerManager::GetTimeOutToLoaunchServer()
1012 int count(TIME_OUT_TO_LAUNCH_CONT);
1013 if (GetenvThreadSafe("TIMEOUT_TO_LAUNCH_CONTAINER") != 0)
1015 std::string new_count_str(GetenvThreadSafeAsString("TIMEOUT_TO_LAUNCH_CONTAINER"));
1017 std::istringstream ss(new_count_str);
1018 if (!(ss >> new_count))
1020 INFOS("[LaunchContainer] TIMEOUT_TO_LAUNCH_CONTAINER should be an int");
1028 void SALOME_ContainerManager::SleepInSecond(int ellapseTimeInSecond)
1031 sleep( ellapseTimeInSecond ) ;
1033 int timeInMS(1000*ellapseTimeInSecond);
1038 //=============================================================================
1040 * generate a file name in /tmp directory
1042 //=============================================================================
1044 std::string SALOME_ContainerManager::BuildTemporaryFileName()
1046 //build more complex file name to support multiple salome session
1047 std::string aFileName = Kernel_Utils::GetTmpFileName();
1048 std::ostringstream str_pid;
1049 str_pid << ::getpid();
1050 aFileName = aFileName + "-" + str_pid.str();
1054 aFileName += ".bat";
1059 //=============================================================================
1061 * Builds in a temporary file the script to be launched.
1063 * Used if SALOME Application ($APPLI) is not defined.
1064 * The command is build with data from CatalogResources, in which every path
1065 * used on remote computer must be defined.
1067 //=============================================================================
1069 std::string SALOME_ContainerManager::BuildTempFileToLaunchRemoteContainer (const std::string& resource_name, const Engines::ContainerParameters& params, std::string& tmpFileName) const
1073 tmpFileName = BuildTemporaryFileName();
1074 std::ofstream tempOutputFile;
1075 tempOutputFile.open(tmpFileName.c_str(), std::ofstream::out );
1076 const ParserResourcesType resInfo(_resManager->GetResourceDefinition(resource_name));
1077 tempOutputFile << "#! /bin/sh" << std::endl;
1081 tempOutputFile << "export SALOME_trace=local" << std::endl; // mkr : 27.11.2006 : PAL13967 - Distributed supervision graphs - Problem with "SALOME_trace"
1082 //tempOutputFile << "source " << resInfo.PreReqFilePath << endl;
1088 tempOutputFile << "mpirun -np ";
1091 if ( params.nb_proc <= 0 )
1094 nbproc = params.nb_proc;
1096 std::ostringstream o;
1098 tempOutputFile << nbproc << " ";
1100 tempOutputFile << "-x PATH,LD_LIBRARY_PATH,OMNIORB_CONFIG,SALOME_trace ";
1101 #elif defined(OPEN_MPI)
1102 if( GetenvThreadSafe("OMPI_URI_FILE") == NULL )
1103 tempOutputFile << "-x PATH -x LD_LIBRARY_PATH -x OMNIORB_CONFIG -x SALOME_trace";
1105 tempOutputFile << "-x PATH -x LD_LIBRARY_PATH -x OMNIORB_CONFIG -x SALOME_trace -ompi-server file:";
1106 tempOutputFile << GetenvThreadSafeAsString("OMPI_URI_FILE");
1108 #elif defined(MPICH)
1109 tempOutputFile << "-nameserver " + Kernel_Utils::GetHostname();
1113 tempOutputFile << GetenvThreadSafeAsString("KERNEL_ROOT_DIR") << "/bin/salome/";
1117 if (isPythonContainer(params.container_name))
1118 tempOutputFile << " pyMPI SALOME_ContainerPy.py ";
1120 tempOutputFile << " SALOME_MPIContainer ";
1125 if (isPythonContainer(params.container_name))
1126 tempOutputFile << "SALOME_ContainerPy.py ";
1128 tempOutputFile << "SALOME_Container ";
1131 tempOutputFile << _NS->ContainerName(params) << " -";
1132 AddOmninamesParams(tempOutputFile);
1133 tempOutputFile << " &" << std::endl;
1134 tempOutputFile.flush();
1135 tempOutputFile.close();
1137 chmod(tmpFileName.c_str(), 0x1ED);
1140 // --- Build command
1142 std::string command;
1144 if (resInfo.Protocol == rsh)
1147 std::string commandRcp = "rcp ";
1148 commandRcp += tmpFileName;
1150 commandRcp += resInfo.HostName;
1152 commandRcp += tmpFileName;
1153 status = SystemThreadSafe(commandRcp.c_str());
1156 else if (resInfo.Protocol == ssh)
1159 std::string commandRcp = "scp ";
1160 commandRcp += tmpFileName;
1162 commandRcp += resInfo.HostName;
1164 commandRcp += tmpFileName;
1165 status = SystemThreadSafe(commandRcp.c_str());
1168 else if (resInfo.Protocol == srun)
1170 command = "srun -n 1 -N 1 --share --nodelist=";
1171 std::string commandRcp = "rcp ";
1172 commandRcp += tmpFileName;
1174 commandRcp += resInfo.HostName;
1176 commandRcp += tmpFileName;
1177 status = SystemThreadSafe(commandRcp.c_str());
1180 throw SALOME_Exception("Unknown protocol");
1183 throw SALOME_Exception("Error of connection on remote host");
1185 command += resInfo.HostName;
1187 command += tmpFileName;
1195 std::string SALOME_ContainerManager::GetMPIZeroNode(const std::string machine, const std::string machinesFile) const
1198 std::string zeronode;
1199 std::string command;
1200 std::string tmpFile = BuildTemporaryFileName();
1201 const ParserResourcesType resInfo(_resManager->GetResourceDefinition(machine));
1203 if(resInfo.Protocol == sh)
1205 return resInfo.HostName;
1208 if( GetenvThreadSafe("LIBBATCH_NODEFILE") == NULL )
1210 if (_isAppliSalomeDefined)
1213 if (resInfo.Protocol == rsh)
1215 else if (resInfo.Protocol == ssh)
1217 else if (resInfo.Protocol == srun)
1218 command = "srun -n 1 -N 1 --share --nodelist=";
1220 throw SALOME_Exception("Unknown protocol");
1222 if (resInfo.UserName != "")
1225 command += resInfo.UserName;
1229 command += resInfo.HostName;
1232 if (resInfo.AppliPath != "")
1233 command += resInfo.AppliPath; // path relative to user@machine $HOME
1236 ASSERT(GetenvThreadSafe("APPLI"));
1237 command += GetenvThreadSafeAsString("APPLI"); // path relative to user@machine $HOME
1240 command += "/runRemote.sh ";
1242 ASSERT(GetenvThreadSafe("NSHOST"));
1243 command += GetenvThreadSafeAsString("NSHOST"); // hostname of CORBA name server
1246 ASSERT(GetenvThreadSafe("NSPORT"));
1247 command += GetenvThreadSafeAsString("NSPORT"); // port of CORBA name server
1249 command += " mpirun -np 1 hostname -s > " + tmpFile;
1252 command = "mpirun -np 1 hostname -s > " + tmpFile;
1255 command = "mpirun -np 1 -machinefile " + machinesFile + " hostname -s > " + tmpFile;
1257 status = SystemThreadSafe(command.c_str());
1259 std::ifstream fp(tmpFile.c_str(),std::ios::in);
1260 while(fp >> zeronode);
1268 std::string SALOME_ContainerManager::machinesFile(const int nbproc)
1271 std::string nodesFile = GetenvThreadSafeAsString("LIBBATCH_NODEFILE");
1272 std::string machinesFile = Kernel_Utils::GetTmpFileName();
1273 std::ifstream fpi(nodesFile.c_str(),std::ios::in);
1274 std::ofstream fpo(machinesFile.c_str(),std::ios::out);
1276 _numInstanceMutex.lock();
1278 for(int i=0;i<_nbprocUsed;i++)
1281 for(int i=0;i<nbproc;i++)
1283 fpo << tmp << std::endl;
1285 throw SALOME_Exception("You need more processors than batch session have allocated for you! Unable to launch the mpi container: ");
1287 _nbprocUsed += nbproc;
1291 _numInstanceMutex.unlock();
1293 return machinesFile;
1297 std::set<pid_t> SALOME_ContainerManager::getpidofprogram(const std::string program)
1299 std::set<pid_t> thepids;
1300 std::string tmpFile = Kernel_Utils::GetTmpFileName();
1303 cmd = "pidof " + program + " > " + tmpFile;
1304 SystemThreadSafe(cmd.c_str());
1305 std::ifstream fpi(tmpFile.c_str(),std::ios::in);
1306 while(fpi >> thepid){
1307 thepids.insert(atoi(thepid.c_str()));
1312 std::string SALOME_ContainerManager::getCommandToRunRemoteProcess(AccessProtocolType protocol,
1313 const std::string & hostname,
1314 const std::string & username)
1316 std::ostringstream command;
1323 command << "-l " << username << " ";
1325 command << hostname << " ";
1331 command << "-l " << username << " ";
1333 command << hostname << " ";
1336 // no need to redefine the user with srun, the job user is taken by default
1337 // (note: for srun, user id can be specified with " --uid=<user>")
1338 command << "srun -n 1 -N 1 --share --nodelist=" << hostname << " ";
1341 command << "pbsdsh -o -h " << hostname << " ";
1344 command << "blaunch -no-shell " << hostname << " ";
1347 throw SALOME_Exception("Unknown protocol");
1350 return command.str();
1354 SALOME_ContainerManager::checkPaCOParameters(Engines::ContainerParameters & params, std::string resource_selected)
1358 // Step 1 : check ContainerParameters
1359 // Check container_name, has to be defined
1360 if (std::string(params.container_name.in()) == "")
1362 INFOS("[checkPaCOParameters] You must define a container_name to launch a PaCO++ container");
1365 // Check parallelLib
1366 std::string parallelLib = params.parallelLib.in();
1367 if (parallelLib != "Mpi" && parallelLib != "Dummy")
1369 INFOS("[checkPaCOParameters] parallelLib is not correctly defined");
1370 INFOS("[checkPaCOParameters] you can chosse between: Mpi and Dummy");
1371 INFOS("[checkPaCOParameters] you entered: " << parallelLib);
1375 if (params.nb_proc <= 0)
1377 INFOS("[checkPaCOParameters] You must define a nb_proc > 0");
1381 // Step 2 : check resource_selected
1382 const ParserResourcesType resource_definition = _resManager->GetResourceDefinition(resource_selected);
1383 //std::string protocol = resource_definition->protocol.in();
1384 std::string username = resource_definition.UserName;
1385 std::string applipath = resource_definition.AppliPath;
1387 //if (protocol == "" || username == "" || applipath == "")
1388 if (username == "" || applipath == "")
1390 INFOS("[checkPaCOParameters] resource selected is not well defined");
1391 INFOS("[checkPaCOParameters] resource name: " << resource_definition.Name);
1392 INFOS("[checkPaCOParameters] resource hostname: " << resource_definition.HostName);
1393 INFOS("[checkPaCOParameters] resource protocol: " << resource_definition.getAccessProtocolTypeStr());
1394 INFOS("[checkPaCOParameters] resource username: " << username);
1395 INFOS("[checkPaCOParameters] resource applipath: " << applipath);
1403 * :WARNING: Do not directly convert returned value to std::string
1404 * This function may return NULL if env variable is not defined.
1405 * And std::string(NULL) causes undefined behavior.
1406 * Use GetenvThreadSafeAsString to properly get a std::string.
1408 char *SALOME_ContainerManager::GetenvThreadSafe(const char *name)
1409 {// getenv is not thread safe. See man 7 pthread.
1410 Utils_Locker lock (&_getenvMutex);
1411 return getenv(name);
1415 * Return env variable as a std::string.
1416 * Return empty string if env variable is not set.
1418 std::string SALOME_ContainerManager::GetenvThreadSafeAsString(const char *name)
1420 char* var = GetenvThreadSafe(name);
1421 return var ? std::string(var) : std::string();
1424 int SALOME_ContainerManager::SystemThreadSafe(const char *command)
1426 Utils_Locker lock (&_systemMutex);
1427 return system(command);
1430 #ifdef WITH_PACO_PARALLEL
1432 //=============================================================================
1434 * Start a suitable PaCO++ Parallel Container in a list of machines.
1435 * \param params Container Parameters required for the container
1436 * \return CORBA container reference.
1438 //=============================================================================
1439 Engines::Container_ptr
1440 SALOME_ContainerManager::StartPaCOPPContainer(const Engines::ContainerParameters& params_const,
1441 std::string resource_selected)
1443 CORBA::Object_var obj;
1444 PaCO::InterfaceManager_var container_proxy;
1445 Engines::Container_ptr ret = Engines::Container::_nil();
1446 Engines::ContainerParameters params(params_const);
1447 params.resource_params.name = CORBA::string_dup(resource_selected.c_str());
1449 // Step 0 : Check parameters
1450 if (!checkPaCOParameters(params, resource_selected))
1452 INFOS("[StartPaCOPPContainer] check parameters failed ! see logs...");
1456 // Step 1 : Starting a new parallel container !
1457 INFOS("[StartPaCOPPContainer] Starting a PaCO++ parallel container");
1458 INFOS("[StartPaCOPPContainer] on resource : " << resource_selected);
1460 // Step 2 : Get a MachineFile for the parallel container
1461 std::string machine_file_name = _resManager->getMachineFile(resource_selected,
1463 params.parallelLib.in());
1465 if (machine_file_name == "")
1467 INFOS("[StartPaCOPPContainer] Machine file generation failed");
1471 // Step 3 : starting parallel container proxy
1472 std::string command_proxy("");
1473 std::string proxy_machine;
1476 command_proxy = BuildCommandToLaunchPaCOProxyContainer(params, machine_file_name, proxy_machine);
1478 catch(const SALOME_Exception & ex)
1480 INFOS("[StartPaCOPPContainer] Exception in BuildCommandToLaunchPaCOContainer");
1484 obj = LaunchPaCOProxyContainer(command_proxy, params, proxy_machine);
1485 if (CORBA::is_nil(obj))
1487 INFOS("[StartPaCOPPContainer] LaunchPaCOContainer for proxy returns NIL !");
1490 container_proxy = PaCO::InterfaceManager::_narrow(obj);
1491 MESSAGE("[StartPaCOPPContainer] PaCO container proxy is launched");
1493 // Step 4 : starting parallel container nodes
1494 std::string command_nodes("");
1495 SALOME_ContainerManager::actual_launch_machine_t nodes_machines;
1498 command_nodes = BuildCommandToLaunchPaCONodeContainer(params, machine_file_name, nodes_machines, proxy_machine);
1500 catch(const SALOME_Exception & ex)
1502 INFOS("[StarPaCOPPContainer] Exception in BuildCommandToLaunchPaCONodeContainer");
1507 std::string container_generic_node_name = std::string(params.container_name.in()) + std::string("Node");
1508 bool result = LaunchPaCONodeContainer(command_nodes, params, container_generic_node_name, nodes_machines);
1511 INFOS("[StarPaCOPPContainer] LaunchPaCONodeContainer failed !");
1512 // Il faut tuer le proxy
1515 Engines::Container_var proxy = Engines::Container::_narrow(container_proxy);
1520 INFOS("[StarPaCOPPContainer] Exception catched from proxy Shutdown...");
1525 // Step 4 : connecting nodes and the proxy to actually create a parallel container
1526 for (int i = 0; i < params.nb_proc; i++)
1528 std::ostringstream tmp;
1530 std::string proc_number = tmp.str();
1531 std::string container_node_name = container_generic_node_name + proc_number;
1533 std::string theNodeMachine(nodes_machines[i]);
1534 std::string containerNameInNS = _NS->BuildContainerNameForNS(container_node_name.c_str(), theNodeMachine.c_str());
1535 obj = _NS->Resolve(containerNameInNS.c_str());
1536 if (CORBA::is_nil(obj))
1538 INFOS("[StarPaCOPPContainer] CONNECTION FAILED From Naming Service !");
1539 INFOS("[StarPaCOPPContainer] Container name is " << containerNameInNS);
1544 MESSAGE("[StarPaCOPPContainer] Deploying node : " << container_node_name);
1545 PaCO::InterfaceParallel_var node = PaCO::InterfaceParallel::_narrow(obj);
1547 MESSAGE("[StarPaCOPPContainer] node " << container_node_name << " is deployed");
1549 catch(CORBA::SystemException& e)
1551 INFOS("[StarPaCOPPContainer] Exception in deploying node : " << containerNameInNS);
1552 INFOS("CORBA::SystemException : " << e);
1555 catch(CORBA::Exception& e)
1557 INFOS("[StarPaCOPPContainer] Exception in deploying node : " << containerNameInNS);
1558 INFOS("CORBA::Exception" << e);
1563 INFOS("[StarPaCOPPContainer] Exception in deploying node : " << containerNameInNS);
1564 INFOS("Unknown exception !");
1569 // Step 5 : starting parallel container
1572 MESSAGE ("[StarPaCOPPContainer] Starting parallel object");
1573 container_proxy->start();
1574 MESSAGE ("[StarPaCOPPContainer] Parallel object is started");
1575 ret = Engines::Container::_narrow(container_proxy);
1577 catch(CORBA::SystemException& e)
1579 INFOS("Caught CORBA::SystemException. : " << e);
1581 catch(PortableServer::POA::ServantAlreadyActive&)
1583 INFOS("Caught CORBA::ServantAlreadyActiveException");
1585 catch(CORBA::Exception&)
1587 INFOS("Caught CORBA::Exception.");
1589 catch(std::exception& exc)
1591 INFOS("Caught std::exception - "<<exc.what());
1595 INFOS("Caught unknown exception.");
1601 SALOME_ContainerManager::BuildCommandToLaunchPaCOProxyContainer(const Engines::ContainerParameters& params,
1602 std::string machine_file_name,
1603 std::string & proxy_hostname)
1605 // In the proxy case, we always launch a Dummy Proxy
1606 std::string exe_name = "SALOME_ParallelContainerProxyDummy";
1607 std::string container_name = params.container_name.in();
1609 // Convert nb_proc in string
1610 std::ostringstream tmp_string;
1611 tmp_string << params.nb_proc;
1612 std::string nb_proc_str = tmp_string.str();
1614 // Get resource definition
1615 ParserResourcesType resource_definition =
1616 _resManager->GetResourceDefinition(params.resource_params.name.in());
1619 std::string hostname;
1620 std::ifstream machine_file(machine_file_name.c_str());
1621 std::getline(machine_file, hostname, ' ');
1622 size_t found = hostname.find('\n');
1623 if (found!=std::string::npos)
1624 hostname.erase(found, 1); // Remove \n
1625 proxy_hostname = hostname;
1626 MESSAGE("[BuildCommandToLaunchPaCOProxyContainer] machine file name extracted is " << hostname);
1629 bool remote_execution = false;
1630 if (hostname != std::string(Kernel_Utils::GetHostname()))
1632 MESSAGE("[BuildCommandToLaunchPaCOProxyContainer] remote machine case detected !");
1633 remote_execution = true;
1636 // Log environnement
1637 std::string log_type("");
1638 char * get_val = GetenvThreadSafe("PARALLEL_LOG");
1642 // Generating the command
1643 std::string command_begin("");
1644 std::string command_end("");
1645 std::ostringstream command;
1647 LogConfiguration(log_type, "proxy", container_name, hostname, command_begin, command_end);
1648 command << command_begin;
1650 // Adding connection command
1651 // We can only have a remote execution with
1652 // a SALOME application
1653 if (remote_execution)
1655 ASSERT(GetenvThreadSafe("NSHOST"));
1656 ASSERT(GetenvThreadSafe("NSPORT"));
1658 command << resource_definition.getAccessProtocolTypeStr();
1660 command << resource_definition.UserName;
1661 command << " " << hostname;
1662 command << " " << resource_definition.AppliPath;
1663 command << "/runRemote.sh ";
1664 command << GetenvThreadSafeAsString("NSHOST") << " "; // hostname of CORBA name server
1665 command << GetenvThreadSafeAsString("NSPORT") << " "; // port of CORBA name server
1668 command << exe_name;
1669 command << " " << container_name;
1670 command << " Dummy";
1671 command << " " << hostname;
1672 command << " " << nb_proc_str;
1674 AddOmninamesParams(command);
1677 command << command_end;
1678 MESSAGE("[BuildCommandToLaunchPaCOProxyContainer] Command is: " << command.str());
1680 return command.str();
1684 SALOME_ContainerManager::BuildCommandToLaunchPaCONodeContainer(const Engines::ContainerParameters& params,
1685 const std::string & machine_file_name,
1686 SALOME_ContainerManager::actual_launch_machine_t & vect_machine,
1687 const std::string & proxy_hostname)
1690 std::string exe_name = "SALOME_ParallelContainerNode";
1691 exe_name += params.parallelLib.in();
1692 std::string container_name = params.container_name.in();
1694 // Convert nb_proc in string
1695 std::ostringstream nb_proc_stream;
1696 nb_proc_stream << params.nb_proc;
1698 // Get resource definition
1699 ParserResourcesType resource_definition =
1700 _resManager->GetResourceDefinition(params.resource_params.name.in());
1702 // Log environnement
1703 std::string log_type("");
1704 char * get_val = GetenvThreadSafe("PARALLEL_LOG");
1708 // Now the command is different according to paralleLib
1709 std::ostringstream command_nodes;
1710 std::ifstream machine_file(machine_file_name.c_str());
1711 if (std::string(params.parallelLib.in()) == "Dummy")
1713 for (int i= 0; i < params.nb_proc; i++)
1716 std::string hostname;
1717 std::getline(machine_file, hostname);
1718 MESSAGE("[BuildCommandToLaunchPaCONodeContainer] machine file name extracted is " << hostname);
1721 bool remote_execution = false;
1722 if (hostname != std::string(Kernel_Utils::GetHostname()))
1724 MESSAGE("[BuildCommandToLaunchPaCONodeContainer] remote machine case detected !");
1725 remote_execution = true;
1728 // For each node we have a new command
1729 // Generating the command
1730 std::ostringstream command_node_stream;
1731 std::string command_node_begin("");
1732 std::string command_node_end("");
1733 std::ostringstream node_number;
1735 std::string container_node_name = container_name + node_number.str();
1736 LogConfiguration(log_type, "node", container_node_name, hostname, command_node_begin, command_node_end);
1738 // Adding connection command
1739 // We can only have a remote execution with
1740 // a SALOME application
1741 if (remote_execution)
1743 ASSERT(GetenvThreadSafe("NSHOST"));
1744 ASSERT(GetenvThreadSafe("NSPORT"));
1746 command_node_stream << resource_definition.getAccessProtocolTypeStr();
1747 command_node_stream << " -l ";
1748 command_node_stream << resource_definition.UserName;
1749 command_node_stream << " " << hostname;
1750 command_node_stream << " " << resource_definition.AppliPath;
1751 command_node_stream << "/runRemote.sh ";
1752 command_node_stream << GetenvThreadSafeAsString("NSHOST") << " "; // hostname of CORBA name server
1753 command_node_stream << GetenvThreadSafeAsString("NSPORT") << " "; // port of CORBA name server
1756 command_node_stream << exe_name;
1757 command_node_stream << " " << container_name;
1758 command_node_stream << " " << params.parallelLib.in();
1759 command_node_stream << " " << proxy_hostname;
1760 command_node_stream << " " << node_number.str();
1761 command_node_stream << " -";
1762 AddOmninamesParams(command_node_stream);
1764 command_nodes << command_node_begin << command_node_stream.str() << command_node_end;
1765 vect_machine.push_back(hostname);
1769 else if (std::string(params.parallelLib.in()) == "Mpi")
1772 std::string hostname;
1773 std::getline(machine_file, hostname, ' ');
1774 MESSAGE("[BuildCommandToLaunchPaCONodeContainer] machine file name extracted is " << hostname);
1777 bool remote_execution = false;
1778 if (hostname != std::string(Kernel_Utils::GetHostname()))
1780 MESSAGE("[BuildCommandToLaunchPaCONodeContainer] remote machine case detected !");
1781 remote_execution = true;
1784 // In case of Mpi and Remote, we copy machine_file in the applipath
1785 // scp mpi_machine_file user@machine:Path
1786 std::ostringstream command_remote_stream;
1787 std::string::size_type last = machine_file_name.find_last_of("/");
1788 if (last == std::string::npos)
1791 if (resource_definition.Protocol == rsh)
1792 command_remote_stream << "rcp ";
1794 command_remote_stream << "scp ";
1795 command_remote_stream << machine_file_name << " ";
1796 command_remote_stream << resource_definition.UserName << "@";
1797 command_remote_stream << hostname << ":" << resource_definition.AppliPath;
1798 command_remote_stream << "/" << machine_file_name.substr(last+1);
1800 int status = SystemThreadSafe(command_remote_stream.str().c_str());
1803 INFOS("copy of the MPI machine file failed ! - sorry !");
1807 // Generating the command
1808 std::string command_begin("");
1809 std::string command_end("");
1811 LogConfiguration(log_type, "nodes", container_name, hostname, command_begin, command_end);
1812 command_nodes << command_begin;
1814 // Adding connection command
1815 // We can only have a remote execution with
1816 // a SALOME application
1817 if (remote_execution)
1819 ASSERT(GetenvThreadSafe("NSHOST"));
1820 ASSERT(GetenvThreadSafe("NSPORT"));
1822 command_nodes << resource_definition.getAccessProtocolTypeStr();
1823 command_nodes << " -l ";
1824 command_nodes << resource_definition.UserName;
1825 command_nodes << " " << hostname;
1826 command_nodes << " " << resource_definition.AppliPath;
1827 command_nodes << "/runRemote.sh ";
1828 command_nodes << GetenvThreadSafeAsString("NSHOST") << " "; // hostname of CORBA name server
1829 command_nodes << GetenvThreadSafeAsString("NSPORT") << " "; // port of CORBA name server
1832 if (resource_definition.mpi == lam)
1834 command_nodes << "mpiexec -ssi boot ";
1835 command_nodes << "-machinefile " << machine_file_name << " ";
1836 command_nodes << "-n " << params.nb_proc;
1840 command_nodes << "mpirun -np " << params.nb_proc;
1842 command_nodes << " " << exe_name;
1843 command_nodes << " " << container_name;
1844 command_nodes << " " << params.parallelLib.in();
1845 command_nodes << " " << proxy_hostname;
1846 command_nodes << " -";
1847 AddOmninamesParams(command_nodes);
1849 // We don't put hostname, because nodes are registered in the resource of the proxy
1850 for (int i= 0; i < params.nb_proc; i++)
1851 vect_machine.push_back(proxy_hostname);
1853 command_nodes << command_end;
1855 return command_nodes.str();
1859 SALOME_ContainerManager::LogConfiguration(const std::string & log_type,
1860 const std::string & exe_type,
1861 const std::string & container_name,
1862 const std::string & hostname,
1863 std::string & begin,
1866 if(log_type == "xterm")
1868 begin = "xterm -e \"export LD_LIBRARY_PATH=$LD_LIBRARY_PATH; export PATH=$PATH;";
1871 else if(log_type == "xterm_debug")
1873 begin = "xterm -e \"export LD_LIBRARY_PATH=$LD_LIBRARY_PATH; export PATH=$PATH;";
1878 // default into a file...
1879 std::string logFilename = "/tmp/" + container_name + "_" + hostname + "_" + exe_type + "_";
1880 std::string user = GetenvThreadSafeAsString("USER");
1882 user = GetenvThreadSafeAsString("LOGNAME");
1883 logFilename += user + ".log";
1884 end = " > " + logFilename + " 2>&1 & ";
1889 SALOME_ContainerManager::LaunchPaCOProxyContainer(const std::string& command,
1890 const Engines::ContainerParameters& params,
1891 const std::string & hostname)
1893 PaCO::InterfaceManager_ptr container_proxy = PaCO::InterfaceManager::_nil();
1895 MESSAGE("[LaunchPaCOProxyContainer] Launch command");
1896 int status = SystemThreadSafe(command.c_str());
1898 INFOS("[LaunchPaCOProxyContainer] failed : system command status -1");
1899 return container_proxy;
1901 else if (status == 217) {
1902 INFOS("[LaunchPaCOProxyContainer] failed : system command status 217");
1903 return container_proxy;
1906 int count(GetTimeOutToLoaunchServer());
1907 CORBA::Object_var obj = CORBA::Object::_nil();
1908 std::string containerNameInNS = _NS->BuildContainerNameForNS(params.container_name.in(),
1910 MESSAGE("[LaunchParallelContainer] Waiting for Parallel Container proxy : " << containerNameInNS);
1912 while (CORBA::is_nil(obj) && count)
1916 obj = _NS->Resolve(containerNameInNS.c_str());
1921 container_proxy = PaCO::InterfaceManager::_narrow(obj);
1923 catch(CORBA::SystemException& e)
1925 INFOS("[StarPaCOPPContainer] Exception in _narrow after LaunchParallelContainer for proxy !");
1926 INFOS("CORBA::SystemException : " << e);
1927 return container_proxy;
1929 catch(CORBA::Exception& e)
1931 INFOS("[StarPaCOPPContainer] Exception in _narrow after LaunchParallelContainer for proxy !");
1932 INFOS("CORBA::Exception" << e);
1933 return container_proxy;
1937 INFOS("[StarPaCOPPContainer] Exception in _narrow after LaunchParallelContainer for proxy !");
1938 INFOS("Unknown exception !");
1939 return container_proxy;
1941 if (CORBA::is_nil(container_proxy))
1943 INFOS("[StarPaCOPPContainer] PaCO::InterfaceManager::_narrow returns NIL !");
1944 return container_proxy;
1949 //=============================================================================
1950 /*! This method launches the parallel container.
1951 * It will may be placed on the ressources manager.
1953 * \param command to launch
1954 * \param container's parameters
1955 * \param name of the container
1957 * \return CORBA container reference
1959 //=============================================================================
1961 SALOME_ContainerManager::LaunchPaCONodeContainer(const std::string& command,
1962 const Engines::ContainerParameters& params,
1963 const std::string& name,
1964 SALOME_ContainerManager::actual_launch_machine_t & vect_machine)
1966 INFOS("[LaunchPaCONodeContainer] Launch command");
1967 int status = SystemThreadSafe(command.c_str());
1969 INFOS("[LaunchPaCONodeContainer] failed : system command status -1");
1972 else if (status == 217) {
1973 INFOS("[LaunchPaCONodeContainer] failed : system command status 217");
1977 INFOS("[LaunchPaCONodeContainer] Waiting for the nodes of the parallel container");
1978 // We are waiting all the nodes
1979 for (int i = 0; i < params.nb_proc; i++)
1981 CORBA::Object_var obj = CORBA::Object::_nil();
1982 std::string theMachine(vect_machine[i]);
1984 std::ostringstream tmp;
1986 std::string proc_number = tmp.str();
1987 std::string container_node_name = name + proc_number;
1988 std::string containerNameInNS = _NS->BuildContainerNameForNS((char*) container_node_name.c_str(), theMachine.c_str());
1989 INFOS("[LaunchPaCONodeContainer] Waiting for Parallel Container node " << containerNameInNS << " on " << theMachine);
1990 int count(GetTimeOutToLoaunchServer());
1991 while (CORBA::is_nil(obj) && count) {
1994 obj = _NS->Resolve(containerNameInNS.c_str());
1996 if (CORBA::is_nil(obj))
1998 INFOS("[LaunchPaCONodeContainer] Launch of node failed (or not found) !");
2007 Engines::Container_ptr
2008 SALOME_ContainerManager::StartPaCOPPContainer(const Engines::ContainerParameters& params,
2009 std::string resource_selected)
2011 Engines::Container_ptr ret = Engines::Container::_nil();
2012 INFOS("[StarPaCOPPContainer] is disabled !");
2013 INFOS("[StarPaCOPPContainer] recompile SALOME Kernel to enable PaCO++ parallel extension");
2018 SALOME_ContainerManager::BuildCommandToLaunchPaCOProxyContainer(const Engines::ContainerParameters& params,
2019 std::string machine_file_name,
2020 std::string & proxy_hostname)
2026 SALOME_ContainerManager::BuildCommandToLaunchPaCONodeContainer(const Engines::ContainerParameters& params,
2027 const std::string & machine_file_name,
2028 SALOME_ContainerManager::actual_launch_machine_t & vect_machine,
2029 const std::string & proxy_hostname)
2034 SALOME_ContainerManager::LogConfiguration(const std::string & log_type,
2035 const std::string & exe_type,
2036 const std::string & container_name,
2037 const std::string & hostname,
2038 std::string & begin,
2044 SALOME_ContainerManager::LaunchPaCOProxyContainer(const std::string& command,
2045 const Engines::ContainerParameters& params,
2046 const std::string& hostname)
2048 CORBA::Object_ptr ret = CORBA::Object::_nil();
2053 SALOME_ContainerManager::LaunchPaCONodeContainer(const std::string& command,
2054 const Engines::ContainerParameters& params,
2055 const std::string& name,
2056 SALOME_ContainerManager::actual_launch_machine_t & vect_machine)