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_NamingService.hxx"
25 #include "SALOME_ModuleCatalog.hh"
26 #include "Basics_Utils.hxx"
27 #include "Basics_DirUtils.hxx"
28 #include <sys/types.h>
35 #include "Utils_CorbaException.hxx"
39 #include <SALOMEconfig.h>
40 #include CORBA_CLIENT_HEADER(SALOME_Session)
44 #define getpid _getpid
47 #ifdef WITH_PACO_PARALLEL
51 #define TIME_OUT_TO_LAUNCH_CONT 60
53 const char *SALOME_ContainerManager::_ContainerManagerNameInNS =
56 omni_mutex SALOME_ContainerManager::_numInstanceMutex;
58 Utils_Mutex SALOME_ContainerManager::_getenvMutex;
60 Utils_Mutex SALOME_ContainerManager::_systemMutex;
62 //=============================================================================
66 * Define a CORBA single thread policy for the server, which avoid to deal
67 * with non thread-safe usage like Change_Directory in SALOME naming service
69 //=============================================================================
71 SALOME_ContainerManager::SALOME_ContainerManager(CORBA::ORB_ptr orb, PortableServer::POA_var poa, SALOME_ResourcesManager *rm, SALOME_NamingService *ns):_nbprocUsed(1)
73 MESSAGE("constructor");
77 PortableServer::POAManager_var pman = poa->the_POAManager();
78 _orb = CORBA::ORB::_duplicate(orb) ;
79 CORBA::PolicyList policies;
81 PortableServer::ThreadPolicy_var threadPol(poa->create_thread_policy(PortableServer::ORB_CTRL_MODEL));
82 policies[0] = PortableServer::ThreadPolicy::_duplicate(threadPol);
84 _poa = poa->create_POA("MThreadPOA",pman,policies);
86 PortableServer::ObjectId_var id = _poa->activate_object(this);
87 CORBA::Object_var obj = _poa->id_to_reference(id);
88 Engines::ContainerManager_var refContMan =
89 Engines::ContainerManager::_narrow(obj);
91 _NS->Register(refContMan,_ContainerManagerNameInNS);
92 _isAppliSalomeDefined = (GetenvThreadSafe("APPLI") != 0);
97 // the urifile name depends on pid of the process
98 std::stringstream urifile;
99 urifile << GetenvThreadSafe("HOME") << "/.urifile_" << getpid();
100 setenv("OMPI_URI_FILE",urifile.str().c_str(),1);
101 if( GetenvThreadSafe("OMPI_URI_FILE") != NULL ){
102 // get the pid of all ompi-server
103 std::set<pid_t> thepids1 = getpidofprogram("ompi-server");
104 // launch a new ompi-server
106 command = "ompi-server -r ";
107 command += GetenvThreadSafe("OMPI_URI_FILE");
108 int status=SystemThreadSafe(command.c_str());
110 throw SALOME_Exception("Error when launching ompi-server");
111 // get the pid of all ompi-server
112 std::set<pid_t> thepids2 = getpidofprogram("ompi-server");
113 // my ompi-server is the new one
114 std::set<pid_t>::const_iterator it;
115 for(it=thepids2.begin();it!=thepids2.end();it++)
116 if(thepids1.find(*it) == thepids1.end())
117 _pid_mpiServer = *it;
118 if(_pid_mpiServer < 0)
119 throw SALOME_Exception("Error when getting ompi-server id");
121 #elif defined(WITHMPICH)
123 // get the pid of all hydra_nameserver
124 std::set<pid_t> thepids1 = getpidofprogram("hydra_nameserver");
125 // launch a new hydra_nameserver
127 command = "hydra_nameserver &";
128 SystemThreadSafe(command.c_str());
129 // get the pid of all hydra_nameserver
130 std::set<pid_t> thepids2 = getpidofprogram("hydra_nameserver");
131 // my hydra_nameserver is the new one
132 std::set<pid_t>::const_iterator it;
133 for(it=thepids2.begin();it!=thepids2.end();it++)
134 if(thepids1.find(*it) == thepids1.end())
135 _pid_mpiServer = *it;
139 MESSAGE("constructor end");
142 //=============================================================================
146 //=============================================================================
148 SALOME_ContainerManager::~SALOME_ContainerManager()
150 MESSAGE("destructor");
153 if( GetenvThreadSafe("OMPI_URI_FILE") != NULL ){
154 // kill my ompi-server
155 if( kill(_pid_mpiServer,SIGTERM) != 0 )
156 throw SALOME_Exception("Error when killing ompi-server");
158 int status=SystemThreadSafe("rm -f ${OMPI_URI_FILE}");
160 throw SALOME_Exception("Error when removing urifile");
162 #elif defined(WITHMPICH)
163 // kill my hydra_nameserver
164 if(_pid_mpiServer > -1)
165 if( kill(_pid_mpiServer,SIGTERM) != 0 )
166 throw SALOME_Exception("Error when killing hydra_nameserver");
171 //=============================================================================
172 //! shutdown all the containers, then the ContainerManager servant
175 //=============================================================================
177 void SALOME_ContainerManager::Shutdown()
180 ShutdownContainers();
181 _NS->Destroy_Name(_ContainerManagerNameInNS);
182 PortableServer::ObjectId_var oid = _poa->servant_to_id(this);
183 _poa->deactivate_object(oid);
186 //=============================================================================
187 //! Loop on all the containers listed in naming service, ask shutdown on each
190 //=============================================================================
192 void SALOME_ContainerManager::ShutdownContainers()
194 MESSAGE("ShutdownContainers");
196 SALOME::Session_var session = SALOME::Session::_nil();
198 CORBA::Object_var objS = _NS->Resolve("/Kernel/Session");
199 if (!CORBA::is_nil(objS))
201 session = SALOME::Session::_narrow(objS);
202 if (!CORBA::is_nil(session))
203 pid = session->getPID();
207 isOK = _NS->Change_Directory("/Containers");
209 std::vector<std::string> vec = _NS->list_directory_recurs();
210 std::list<std::string> lstCont;
211 for(std::vector<std::string>::iterator iter = vec.begin();iter!=vec.end();iter++)
214 CORBA::Object_var obj=_NS->Resolve((*iter).c_str());
217 Engines::Container_var cont=Engines::Container::_narrow(obj);
218 if(!CORBA::is_nil(cont) && pid != cont->getPID())
219 lstCont.push_back((*iter));
221 catch(const CORBA::Exception& e)
223 // ignore this entry and continue
226 MESSAGE("Container list: ");
227 for(std::list<std::string>::iterator iter=lstCont.begin();iter!=lstCont.end();iter++){
230 for(std::list<std::string>::iterator iter=lstCont.begin();iter!=lstCont.end();iter++)
235 CORBA::Object_var obj=_NS->Resolve((*iter).c_str());
236 Engines::Container_var cont=Engines::Container::_narrow(obj);
237 if(!CORBA::is_nil(cont))
239 MESSAGE("ShutdownContainers: " << (*iter));
243 MESSAGE("ShutdownContainers: no container ref for " << (*iter));
245 catch(CORBA::SystemException& e)
247 INFOS("CORBA::SystemException ignored : " << e);
249 catch(CORBA::Exception&)
251 INFOS("CORBA::Exception ignored.");
255 INFOS("Unknown exception ignored.");
261 //=============================================================================
262 //! Give a suitable Container given constraints
264 * \param params Container Parameters required for the container
265 * \return the container or nil
267 //=============================================================================
268 Engines::Container_ptr SALOME_ContainerManager::GiveContainer(const Engines::ContainerParameters& params)
270 std::string machFile;
271 Engines::Container_ptr ret(Engines::Container::_nil());
273 // Step 0: Default mode is start
274 Engines::ContainerParameters local_params(params);
275 if (std::string(local_params.mode.in()) == "")
276 local_params.mode = CORBA::string_dup("start");
277 std::string mode = local_params.mode.in();
278 MESSAGE("[GiveContainer] starting with mode: " << mode);
280 // Step 1: Find Container for find and findorstart mode
281 if (mode == "find" || mode == "findorstart")
283 ret = FindContainer(params, params.resource_params.resList);
284 if(!CORBA::is_nil(ret))
290 MESSAGE("[GiveContainer] no container found");
300 // Step 2: Get all possibleResources from the parameters
301 // Consider only resources that can run containers
302 local_params.resource_params.can_run_containers = true;
303 Engines::ResourceList_var possibleResources = _ResManager->GetFittingResources(local_params.resource_params);
304 MESSAGE("[GiveContainer] - length of possible resources " << possibleResources->length());
305 std::vector<std::string> local_resources;
307 // Step 3: if mode is "get" keep only machines with existing containers
310 for(unsigned int i=0; i < possibleResources->length(); i++)
312 Engines::Container_ptr cont = FindContainer(params, possibleResources[i].in());
315 if(!cont->_non_existent())
316 local_resources.push_back(std::string(possibleResources[i]));
318 catch(CORBA::Exception&) {}
321 // if local_resources is empty, we cannot give a container
322 if (local_resources.size() == 0)
324 MESSAGE("[GiveContainer] cannot find a container for mode get");
329 for(unsigned int i=0; i < possibleResources->length(); i++)
330 local_resources.push_back(std::string(possibleResources[i]));
332 // Step 4: select the resource where to get/start the container
333 bool resource_available = true;
334 std::string resource_selected;
335 std::vector<std::string> resources = local_resources;
336 while (resource_available)
338 if (resources.size() == 0)
339 resource_available = false;
344 resource_selected = _ResManager->GetImpl()->Find(params.resource_params.policy.in(), resources);
345 // Remove resource_selected from vector
346 std::vector<std::string>::iterator it;
347 for (it=resources.begin() ; it < resources.end(); it++ )
348 if (*it == resource_selected)
354 catch(const SALOME_Exception &ex)
356 MESSAGE("[GiveContainer] Exception in ResourceManager find !: " << ex.what());
359 MESSAGE("[GiveContainer] Resource selected is: " << resource_selected);
361 // Step 5: Create container name
362 Engines::ResourceDefinition_var resource_definition = _ResManager->GetResourceDefinition(resource_selected.c_str());
363 std::string hostname(resource_definition->hostname.in());
364 std::string containerNameInNS;
367 if ( params.nb_proc <= 0 )
370 nbproc = params.nb_proc;
373 if( GetenvThreadSafe("LIBBATCH_NODEFILE") != NULL )
374 machFile = machinesFile(nbproc);
376 catch(const SALOME_Exception & ex)
378 std::string err_msg = ex.what();
379 err_msg += params.container_name;
380 INFOS(err_msg.c_str());
383 // A mpi parallel container register on zero node in NS
384 containerNameInNS = _NS->BuildContainerNameForNS(params, GetMPIZeroNode(hostname,machFile).c_str());
387 containerNameInNS = _NS->BuildContainerNameForNS(params, hostname.c_str());
388 MESSAGE("[GiveContainer] Container name in the naming service: " << containerNameInNS);
390 // Step 6: check if the name exists in naming service
391 //if params.mode == "getorstart" or "get" use the existing container
392 //if params.mode == "start" shutdown the existing container before launching a new one with that name
394 { // critical section
395 Utils_Locker lock (&_giveContainerMutex1);
396 CORBA::Object_var obj = _NS->Resolve(containerNameInNS.c_str());
397 if (!CORBA::is_nil(obj))
401 Engines::Container_var cont=Engines::Container::_narrow(obj);
402 if(!cont->_non_existent())
404 if(std::string(params.mode.in())=="getorstart" || std::string(params.mode.in())=="get"){
405 return cont._retn(); /* the container exists and params.mode is getorstart or get use it*/
409 INFOS("[GiveContainer] A container is already registered with the name: " << containerNameInNS << ", shutdown the existing container");
410 cont->Shutdown(); // shutdown the registered container if it exists
414 catch(CORBA::Exception&)
416 INFOS("[GiveContainer] CORBA::Exception ignored when trying to get the container - we start a new one");
419 } // end critical section
420 Engines::Container_var cont = LaunchContainer(params, resource_selected, hostname, machFile, containerNameInNS);
421 if (!CORBA::is_nil(cont))
423 INFOS("[GiveContainer] container " << containerNameInNS << " launched");
428 INFOS("[GiveContainer] Failed to launch container on resource " << resource_selected);
433 // We were not able to launch the container
434 INFOS("[GiveContainer] Cannot launch the container on the following selected resources:")
435 std::vector<std::string>::iterator it;
436 for (it=local_resources.begin() ; it < local_resources.end(); it++ )
437 INFOS("[GiveContainer] " << *it)
441 Engines::Container_ptr
442 SALOME_ContainerManager::LaunchContainer(const Engines::ContainerParameters& params,
443 const std::string & resource_selected,
444 const std::string & hostname,
445 const std::string & machFile,
446 const std::string & containerNameInNS)
448 std::string user,command,logFilename,tmpFileName;
450 Engines::Container_ptr ret(Engines::Container::_nil());
451 {//start of critical section
452 Utils_Locker lock (&_giveContainerMutex1);
453 // Step 1: type of container: PaCO, Exe, Mpi or Classic
454 // Mpi already tested in step 5, specific code on BuildCommandToLaunch Local/Remote Container methods
455 // TODO -> separates Mpi from Classic/Exe
457 std::string container_exe = "SALOME_Container"; // Classic container
458 Engines::ContainerParameters local_params(params);
462 CORBA::String_var container_exe_tmp;
463 CORBA::Object_var obj = _NS->Resolve("/Kernel/ModulCatalog");
464 SALOME_ModuleCatalog::ModuleCatalog_var Catalog = SALOME_ModuleCatalog::ModuleCatalog::_narrow(obj) ;
465 if (CORBA::is_nil (Catalog))
467 INFOS("[GiveContainer] Module Catalog is not found -> cannot launch a container");
470 // Loop through component list
471 for(unsigned int i=0; i < local_params.resource_params.componentList.length(); i++)
473 const char* compoi = local_params.resource_params.componentList[i];
474 SALOME_ModuleCatalog::Acomponent_var compoInfo = Catalog->GetComponent(compoi);
475 if (CORBA::is_nil (compoInfo))
479 SALOME_ModuleCatalog::ImplType impl=compoInfo->implementation_type();
480 container_exe_tmp=compoInfo->implementation_name();
481 if(impl==SALOME_ModuleCatalog::CEXE)
485 INFOS("ContainerManager Error: you can't have 2 CEXE component in the same container" );
486 return Engines::Container::_nil();
488 MESSAGE("[GiveContainer] Exe container found !: " << container_exe_tmp);
489 container_exe = container_exe_tmp.in();
494 catch (ServiceUnreachable&)
496 INFOS("Caught exception: Naming Service Unreachable");
501 INFOS("Caught unknown exception.");
505 // Step 2: test resource
506 // Only if an application directory is set
507 if(hostname != Kernel_Utils::GetHostname() && _isAppliSalomeDefined)
509 // Preparing remote command
510 std::string command = "";
511 const ParserResourcesType resInfo(_ResManager->GetImpl()->GetResourcesDescr(resource_selected));
512 command = getCommandToRunRemoteProcess(resInfo.Protocol, resInfo.HostName, resInfo.UserName);
513 if (resInfo.AppliPath != "")
514 command += resInfo.AppliPath;
517 ASSERT(GetenvThreadSafe("APPLI"));
518 command += GetenvThreadSafe("APPLI");
520 command += "/runRemote.sh ";
521 ASSERT(GetenvThreadSafe("NSHOST"));
522 command += GetenvThreadSafe("NSHOST"); // hostname of CORBA name server
524 ASSERT(GetenvThreadSafe("NSPORT"));
525 command += GetenvThreadSafe("NSPORT"); // port of CORBA name server
526 command += " \"ls /tmp >/dev/null 2>&1\"";
528 // Launch remote command
529 int status = SystemThreadSafe(command.c_str());
532 // Error on resource - cannot launch commands
533 INFOS("[LaunchContainer] Cannot launch commands on machine " << hostname);
534 INFOS("[LaunchContainer] Command was " << command);
536 INFOS("[LaunchContainer] Command status is " << WEXITSTATUS(status));
538 return Engines::Container::_nil();
542 // Step 3: start a new container
543 // Check if a PaCO container
545 if (std::string(local_params.parallelLib.in()) != "")
547 ret = StartPaCOPPContainer(params, resource_selected);
550 // Other type of containers...
551 MESSAGE("[GiveContainer] Try to launch a new container on " << resource_selected);
552 // if a parallel container is launched in batch job, command is: "mpirun -np nbproc -machinefile nodesfile SALOME_MPIContainer"
553 if( GetenvThreadSafe("LIBBATCH_NODEFILE") != NULL && params.isMPI )
554 command = BuildCommandToLaunchLocalContainer(params, machFile, container_exe, tmpFileName);
555 // if a container is launched on localhost, command is "SALOME_Container" or "mpirun -np nbproc SALOME_MPIContainer"
556 else if(hostname == Kernel_Utils::GetHostname())
557 command = BuildCommandToLaunchLocalContainer(params, machFile, container_exe, tmpFileName);
558 // if a container is launched in remote mode, command is "ssh resource_selected SALOME_Container" or "ssh resource_selected mpirun -np nbproc SALOME_MPIContainer"
560 command = BuildCommandToLaunchRemoteContainer(resource_selected, params, container_exe);
562 //redirect stdout and stderr in a file
564 logFilename=GetenvThreadSafe("TEMP");
566 user = GetenvThreadSafe( "USERNAME" );
568 user = GetenvThreadSafe( "USER" );
570 char* val = GetenvThreadSafe("SALOME_TMP_DIR");
573 struct stat file_info;
574 stat(val, &file_info);
575 bool is_dir = S_ISDIR(file_info.st_mode);
576 if (is_dir)logFilename=val;
577 else std::cerr << "SALOME_TMP_DIR environment variable is not a directory use /tmp instead" << std::endl;
581 logFilename += _NS->ContainerName(params)+"_"+ resource_selected +"_"+user;
582 std::ostringstream tmp;
583 tmp << "_" << getpid();
584 logFilename += tmp.str();
585 logFilename += ".log" ;
586 command += " > " + logFilename + " 2>&1";
588 command = "%PYTHONBIN% -c \"import win32pm ; win32pm.spawnpid(r'" + command + "', '')\"";
593 // launch container with a system call
594 status=SystemThreadSafe(command.c_str());
595 }//end of critical of section
599 INFOS("[LaunchContainer] command failed (system command status -1): " << command);
600 RmTmpFile(tmpFileName); // command file can be removed here
601 return Engines::Container::_nil();
603 else if (status == 217)
605 INFOS("[LaunchContainer] command failed (system command status 217): " << command);
606 RmTmpFile(tmpFileName); // command file can be removed here
607 return Engines::Container::_nil();
611 // Step 4: Wait for the container
612 int count = TIME_OUT_TO_LAUNCH_CONT;
613 if (GetenvThreadSafe("TIMEOUT_TO_LAUNCH_CONTAINER") != 0)
615 std::string new_count_str = GetenvThreadSafe("TIMEOUT_TO_LAUNCH_CONTAINER");
617 std::istringstream ss(new_count_str);
618 if (!(ss >> new_count))
620 INFOS("[LaunchContainer] TIMEOUT_TO_LAUNCH_CONTAINER should be an int");
625 INFOS("[GiveContainer] waiting " << count << " second steps container " << containerNameInNS);
626 while (CORBA::is_nil(ret) && count)
634 MESSAGE("[GiveContainer] step " << count << " Waiting for container on " << resource_selected);
635 CORBA::Object_var obj = _NS->Resolve(containerNameInNS.c_str());
636 ret=Engines::Container::_narrow(obj);
638 if (CORBA::is_nil(ret))
640 INFOS("[GiveContainer] was not able to launch container " << containerNameInNS);
644 // Setting log file name
645 logFilename=":"+logFilename;
646 logFilename="@"+Kernel_Utils::GetHostname()+logFilename;//threadsafe
647 logFilename=user+logFilename;
648 ret->logfilename(logFilename.c_str());
649 RmTmpFile(tmpFileName); // command file can be removed here
655 //=============================================================================
656 //! Find a container given constraints (params) on a list of machines (possibleComputers)
657 //! agy : this method is ThreadSafe
661 //=============================================================================
663 Engines::Container_ptr SALOME_ContainerManager::FindContainer(const Engines::ContainerParameters& params, const Engines::ResourceList& possibleResources)
665 MESSAGE("[FindContainer] FindContainer on " << possibleResources.length() << " resources");
666 for(unsigned int i=0; i < possibleResources.length();i++)
668 Engines::Container_ptr cont = FindContainer(params, possibleResources[i].in());
669 if(!CORBA::is_nil(cont))
672 MESSAGE("[FindContainer] no container found");
673 return Engines::Container::_nil();
676 //=============================================================================
677 //! Find a container given constraints (params) on a machine (theMachine)
678 //! agy : this method is ThreadSafe
682 //=============================================================================
684 Engines::Container_ptr
685 SALOME_ContainerManager::FindContainer(const Engines::ContainerParameters& params, const std::string& resource)
687 Engines::ResourceDefinition_var resource_definition = _ResManager->GetResourceDefinition(resource.c_str());
688 std::string hostname(resource_definition->hostname.in());
689 std::string containerNameInNS(_NS->BuildContainerNameForNS(params, hostname.c_str()));
690 MESSAGE("[FindContainer] Try to find a container " << containerNameInNS << " on resource " << resource);
691 CORBA::Object_var obj = _NS->Resolve(containerNameInNS.c_str());
694 if(obj->_non_existent())
695 return Engines::Container::_nil();
697 return Engines::Container::_narrow(obj);
699 catch(const CORBA::Exception& e)
701 return Engines::Container::_nil();
706 bool isPythonContainer(const char* ContainerName);
708 //=============================================================================
710 * This is no longer valid (C++ container are also python containers)
712 //=============================================================================
713 bool isPythonContainer(const char* ContainerName)
715 return false; // VSR 02/08/2013: Python containers are no more supported
717 int len = strlen(ContainerName);
720 if (strcmp(ContainerName + len - 2, "Py") == 0)
726 //=============================================================================
728 * Builds the script to be launched
730 * If SALOME Application not defined ($APPLI),
731 * see BuildTempFileToLaunchRemoteContainer()
733 * Else rely on distant configuration. Command is under the form (example):
734 * ssh user@machine distantPath/runRemote.sh hostNS portNS WORKINGDIR workingdir \
735 * SALOME_Container containerName &"
737 * - where user is ommited if not specified in CatalogResources,
738 * - where distant path is always relative to user@machine $HOME, and
739 * equal to $APPLI if not specified in CatalogResources,
740 * - where hostNS is the hostname of CORBA naming server (set by scripts to
741 * use to launch SALOME and servers in $APPLI: runAppli.sh, runRemote.sh)
742 * - where portNS is the port used by CORBA naming server (set by scripts to
743 * use to launch SALOME and servers in $APPLI: runAppli.sh, runRemote.sh)
744 * - where workingdir is the requested working directory for the container.
745 * If WORKINGDIR (and workingdir) is not present the working dir will be $HOME
747 //=============================================================================
750 SALOME_ContainerManager::BuildCommandToLaunchRemoteContainer(const std::string& resource_name, const Engines::ContainerParameters& params, const std::string& container_exe) const
752 std::string command,tmpFileName;
753 if (!_isAppliSalomeDefined)
754 command = BuildTempFileToLaunchRemoteContainer(resource_name, params, tmpFileName);
758 Engines::ResourceDefinition_var resource_definition = _ResManager->GetResourceDefinition(resource_name.c_str());
759 std::string hostname(resource_definition->hostname.in());
760 const ParserResourcesType resInfo(_ResManager->GetImpl()->GetResourcesDescr(resource_name));
764 if ( params.nb_proc <= 0 )
767 nbproc = params.nb_proc;
770 // "ssh -l user machine distantPath/runRemote.sh hostNS portNS WORKINGDIR workingdir \
771 // SALOME_Container containerName &"
772 command = getCommandToRunRemoteProcess(resInfo.Protocol, resInfo.HostName, resInfo.UserName);
774 if (resInfo.AppliPath != "")
775 command += resInfo.AppliPath; // path relative to user@machine $HOME
778 ASSERT(GetenvThreadSafe("APPLI"));
779 command += GetenvThreadSafe("APPLI"); // path relative to user@machine $HOME
782 command += "/runRemote.sh ";
784 ASSERT(GetenvThreadSafe("NSHOST"));
785 command += GetenvThreadSafe("NSHOST"); // hostname of CORBA name server
788 ASSERT(GetenvThreadSafe("NSPORT"));
789 command += GetenvThreadSafe("NSPORT"); // port of CORBA name server
791 std::string wdir = params.workingdir.in();
794 command += " WORKINGDIR ";
796 if(wdir == "$TEMPDIR")
798 command += wdir; // requested working directory
804 command += " mpirun -np ";
805 std::ostringstream o;
809 command += "-x PATH,LD_LIBRARY_PATH,OMNIORB_CONFIG,SALOME_trace ";
810 #elif defined(WITHOPENMPI)
811 if( GetenvThreadSafe("OMPI_URI_FILE") == NULL )
812 command += "-x PATH -x LD_LIBRARY_PATH -x OMNIORB_CONFIG -x SALOME_trace";
814 command += "-x PATH -x LD_LIBRARY_PATH -x OMNIORB_CONFIG -x SALOME_trace -ompi-server file:";
815 command += GetenvThreadSafe("OMPI_URI_FILE");
817 #elif defined(WITHMPICH)
818 command += "-nameserver " + Kernel_Utils::GetHostname();
820 command += " SALOME_MPIContainer ";
823 command += " " +container_exe+ " ";
825 command += _NS->ContainerName(params);
827 AddOmninamesParams(command);
829 MESSAGE("command =" << command);
835 //=============================================================================
837 * builds the command to be launched.
839 //=============================================================================
840 std::string SALOME_ContainerManager::BuildCommandToLaunchLocalContainer(const Engines::ContainerParameters& params, const std::string& machinesFile, const std::string& container_exe, std::string& tmpFileName) const
842 tmpFileName = BuildTemporaryFileName();
846 std::ostringstream o;
852 if ( params.nb_proc <= 0 )
855 nbproc = params.nb_proc;
859 if( GetenvThreadSafe("LIBBATCH_NODEFILE") != NULL )
860 o << "-machinefile " << machinesFile << " ";
863 o << "-x PATH,LD_LIBRARY_PATH,OMNIORB_CONFIG,SALOME_trace ";
864 #elif defined(WITHOPENMPI)
865 if( GetenvThreadSafe("OMPI_URI_FILE") == NULL )
866 o << "-x PATH -x LD_LIBRARY_PATH -x OMNIORB_CONFIG -x SALOME_trace";
869 o << "-x PATH -x LD_LIBRARY_PATH -x OMNIORB_CONFIG -x SALOME_trace -ompi-server file:";
870 o << GetenvThreadSafe("OMPI_URI_FILE");
872 #elif defined(WITHMPICH)
873 o << "-nameserver " + Kernel_Utils::GetHostname();
876 if (isPythonContainer(params.container_name))
877 o << " pyMPI SALOME_ContainerPy.py ";
879 o << " SALOME_MPIContainer ";
884 std::string wdir=params.workingdir.in();
887 // a working directory is requested
888 if(wdir == "$TEMPDIR")
890 // a new temporary directory is requested
891 std::string dir = Kernel_Utils::GetTmpDir();
893 o << "cd /d " << dir << std::endl;
895 o << "cd " << dir << ";";
901 // a permanent directory is requested use it or create it
903 o << "mkdir " + wdir << std::endl;
904 o << "cd /D " + wdir << std::endl;
906 o << "mkdir -p " << wdir << " && cd " << wdir + ";";
911 if (isPythonContainer(params.container_name))
912 o << "SALOME_ContainerPy.py ";
914 o << container_exe + " ";
918 o << _NS->ContainerName(params);
920 AddOmninamesParams(o);
922 std::ofstream command_file( tmpFileName.c_str() );
923 command_file << o.str();
924 command_file.close();
927 chmod(tmpFileName.c_str(), 0x1ED);
929 command = tmpFileName;
931 MESSAGE("Command is file ... " << command);
932 MESSAGE("Command is ... " << o.str());
937 //=============================================================================
939 * removes the generated temporary file in case of a remote launch.
940 * This method is thread safe
942 //=============================================================================
944 void SALOME_ContainerManager::RmTmpFile(std::string& tmpFileName)
946 int lenght = tmpFileName.size();
950 std::string command = "del /F ";
952 std::string command = "rm ";
955 command += tmpFileName.substr(0, lenght - 3 );
957 command += tmpFileName;
959 SystemThreadSafe(command.c_str());
960 //if dir is empty - remove it
961 std::string tmp_dir = Kernel_Utils::GetDirByPath( tmpFileName );
962 if ( Kernel_Utils::IsEmptyDir( tmp_dir ) )
965 command = "del /F " + tmp_dir;
967 command = "rmdir " + tmp_dir;
969 SystemThreadSafe(command.c_str());
974 //=============================================================================
976 * add to command all options relative to naming service.
978 //=============================================================================
980 void SALOME_ContainerManager::AddOmninamesParams(std::string& command) const
982 CORBA::String_var iorstr = _NS->getIORaddr();
983 command += "ORBInitRef NameService=";
987 //=============================================================================
989 * add to command all options relative to naming service.
991 //=============================================================================
993 void SALOME_ContainerManager::AddOmninamesParams(std::ofstream& fileStream) const
995 CORBA::String_var iorstr = _NS->getIORaddr();
996 fileStream << "ORBInitRef NameService=";
997 fileStream << iorstr;
1000 //=============================================================================
1002 * add to command all options relative to naming service.
1004 //=============================================================================
1006 void SALOME_ContainerManager::AddOmninamesParams(std::ostringstream& oss) const
1008 CORBA::String_var iorstr = _NS->getIORaddr();
1009 oss << "ORBInitRef NameService=";
1013 //=============================================================================
1015 * generate a file name in /tmp directory
1017 //=============================================================================
1019 std::string SALOME_ContainerManager::BuildTemporaryFileName()
1021 //build more complex file name to support multiple salome session
1022 std::string aFileName = Kernel_Utils::GetTmpFileName();
1026 aFileName += ".bat";
1031 //=============================================================================
1033 * Builds in a temporary file the script to be launched.
1035 * Used if SALOME Application ($APPLI) is not defined.
1036 * The command is build with data from CatalogResources, in which every path
1037 * used on remote computer must be defined.
1039 //=============================================================================
1041 std::string SALOME_ContainerManager::BuildTempFileToLaunchRemoteContainer (const std::string& resource_name, const Engines::ContainerParameters& params, std::string& tmpFileName) const throw(SALOME_Exception)
1045 tmpFileName = BuildTemporaryFileName();
1046 std::ofstream tempOutputFile;
1047 tempOutputFile.open(tmpFileName.c_str(), std::ofstream::out );
1048 const ParserResourcesType resInfo(_ResManager->GetImpl()->GetResourcesDescr(resource_name));
1049 tempOutputFile << "#! /bin/sh" << std::endl;
1053 tempOutputFile << "export SALOME_trace=local" << std::endl; // mkr : 27.11.2006 : PAL13967 - Distributed supervision graphs - Problem with "SALOME_trace"
1054 //tempOutputFile << "source " << resInfo.PreReqFilePath << endl;
1060 tempOutputFile << "mpirun -np ";
1063 if ( params.nb_proc <= 0 )
1066 nbproc = params.nb_proc;
1068 std::ostringstream o;
1070 tempOutputFile << nbproc << " ";
1072 tempOutputFile << "-x PATH,LD_LIBRARY_PATH,OMNIORB_CONFIG,SALOME_trace ";
1073 #elif defined(WITHOPENMPI)
1074 if( GetenvThreadSafe("OMPI_URI_FILE") == NULL )
1075 tempOutputFile << "-x PATH -x LD_LIBRARY_PATH -x OMNIORB_CONFIG -x SALOME_trace";
1077 tempOutputFile << "-x PATH -x LD_LIBRARY_PATH -x OMNIORB_CONFIG -x SALOME_trace -ompi-server file:";
1078 tempOutputFile << GetenvThreadSafe("OMPI_URI_FILE");
1080 #elif defined(WITHMPICH)
1081 tempOutputFile << "-nameserver " + Kernel_Utils::GetHostname();
1085 tempOutputFile << GetenvThreadSafe("KERNEL_ROOT_DIR") << "/bin/salome/";
1089 if (isPythonContainer(params.container_name))
1090 tempOutputFile << " pyMPI SALOME_ContainerPy.py ";
1092 tempOutputFile << " SALOME_MPIContainer ";
1097 if (isPythonContainer(params.container_name))
1098 tempOutputFile << "SALOME_ContainerPy.py ";
1100 tempOutputFile << "SALOME_Container ";
1103 tempOutputFile << _NS->ContainerName(params) << " -";
1104 AddOmninamesParams(tempOutputFile);
1105 tempOutputFile << " &" << std::endl;
1106 tempOutputFile.flush();
1107 tempOutputFile.close();
1109 chmod(tmpFileName.c_str(), 0x1ED);
1112 // --- Build command
1114 std::string command;
1116 if (resInfo.Protocol == rsh)
1119 std::string commandRcp = "rcp ";
1120 commandRcp += tmpFileName;
1122 commandRcp += resInfo.HostName;
1124 commandRcp += tmpFileName;
1125 status = SystemThreadSafe(commandRcp.c_str());
1128 else if (resInfo.Protocol == ssh)
1131 std::string commandRcp = "scp ";
1132 commandRcp += tmpFileName;
1134 commandRcp += resInfo.HostName;
1136 commandRcp += tmpFileName;
1137 status = SystemThreadSafe(commandRcp.c_str());
1140 else if (resInfo.Protocol == srun)
1142 command = "srun -n 1 -N 1 --share --nodelist=";
1143 std::string commandRcp = "rcp ";
1144 commandRcp += tmpFileName;
1146 commandRcp += resInfo.HostName;
1148 commandRcp += tmpFileName;
1149 status = SystemThreadSafe(commandRcp.c_str());
1152 throw SALOME_Exception("Unknown protocol");
1155 throw SALOME_Exception("Error of connection on remote host");
1157 command += resInfo.HostName;
1159 command += tmpFileName;
1167 std::string SALOME_ContainerManager::GetMPIZeroNode(const std::string machine, const std::string machinesFile) const
1170 std::string zeronode;
1171 std::string command;
1172 std::string tmpFile = BuildTemporaryFileName();
1174 if( GetenvThreadSafe("LIBBATCH_NODEFILE") == NULL )
1176 if (_isAppliSalomeDefined)
1178 const ParserResourcesType resInfo(_ResManager->GetImpl()->GetResourcesDescr(machine));
1180 if (resInfo.Protocol == rsh)
1182 else if (resInfo.Protocol == ssh)
1184 else if (resInfo.Protocol == srun)
1185 command = "srun -n 1 -N 1 --share --nodelist=";
1187 throw SALOME_Exception("Unknown protocol");
1189 if (resInfo.UserName != "")
1192 command += resInfo.UserName;
1196 command += resInfo.HostName;
1199 if (resInfo.AppliPath != "")
1200 command += resInfo.AppliPath; // path relative to user@machine $HOME
1203 ASSERT(GetenvThreadSafe("APPLI"));
1204 command += GetenvThreadSafe("APPLI"); // path relative to user@machine $HOME
1207 command += "/runRemote.sh ";
1209 ASSERT(GetenvThreadSafe("NSHOST"));
1210 command += GetenvThreadSafe("NSHOST"); // hostname of CORBA name server
1213 ASSERT(GetenvThreadSafe("NSPORT"));
1214 command += GetenvThreadSafe("NSPORT"); // port of CORBA name server
1216 command += " mpirun -np 1 hostname -s > " + tmpFile;
1219 command = "mpirun -np 1 hostname -s > " + tmpFile;
1222 command = "mpirun -np 1 -machinefile " + machinesFile + " hostname -s > " + tmpFile;
1224 status = SystemThreadSafe(command.c_str());
1226 std::ifstream fp(tmpFile.c_str(),std::ios::in);
1227 while(fp >> zeronode);
1235 std::string SALOME_ContainerManager::machinesFile(const int nbproc)
1238 std::string nodesFile = GetenvThreadSafe("LIBBATCH_NODEFILE");
1239 std::string machinesFile = Kernel_Utils::GetTmpFileName();
1240 std::ifstream fpi(nodesFile.c_str(),std::ios::in);
1241 std::ofstream fpo(machinesFile.c_str(),std::ios::out);
1243 _numInstanceMutex.lock();
1245 for(int i=0;i<_nbprocUsed;i++)
1248 for(int i=0;i<nbproc;i++)
1250 fpo << tmp << std::endl;
1252 throw SALOME_Exception("You need more processors than batch session have allocated for you! Unable to launch the mpi container: ");
1254 _nbprocUsed += nbproc;
1258 _numInstanceMutex.unlock();
1260 return machinesFile;
1264 std::set<pid_t> SALOME_ContainerManager::getpidofprogram(const std::string program)
1266 std::set<pid_t> thepids;
1267 std::string tmpFile = Kernel_Utils::GetTmpFileName();
1270 cmd = "pidof " + program + " > " + tmpFile;
1271 SystemThreadSafe(cmd.c_str());
1272 std::ifstream fpi(tmpFile.c_str(),std::ios::in);
1273 while(fpi >> thepid){
1274 thepids.insert(atoi(thepid.c_str()));
1279 std::string SALOME_ContainerManager::getCommandToRunRemoteProcess(AccessProtocolType protocol,
1280 const std::string & hostname,
1281 const std::string & username)
1283 std::ostringstream command;
1290 command << "-l " << username << " ";
1292 command << hostname << " ";
1298 command << "-l " << username << " ";
1300 command << hostname << " ";
1303 // no need to redefine the user with srun, the job user is taken by default
1304 // (note: for srun, user id can be specified with " --uid=<user>")
1305 command << "srun -n 1 -N 1 --share --nodelist=" << hostname << " ";
1308 command << "pbsdsh -o -h " << hostname << " ";
1311 command << "blaunch -no-shell " << hostname << " ";
1314 throw SALOME_Exception("Unknown protocol");
1317 return command.str();
1321 SALOME_ContainerManager::checkPaCOParameters(Engines::ContainerParameters & params, std::string resource_selected)
1325 // Step 1 : check ContainerParameters
1326 // Check container_name, has to be defined
1327 if (std::string(params.container_name.in()) == "")
1329 INFOS("[checkPaCOParameters] You must define a container_name to launch a PaCO++ container");
1332 // Check parallelLib
1333 std::string parallelLib = params.parallelLib.in();
1334 if (parallelLib != "Mpi" && parallelLib != "Dummy")
1336 INFOS("[checkPaCOParameters] parallelLib is not correctly defined");
1337 INFOS("[checkPaCOParameters] you can chosse between: Mpi and Dummy");
1338 INFOS("[checkPaCOParameters] you entered: " << parallelLib);
1342 if (params.nb_proc <= 0)
1344 INFOS("[checkPaCOParameters] You must define a nb_proc > 0");
1348 // Step 2 : check resource_selected
1349 Engines::ResourceDefinition_var resource_definition = _ResManager->GetResourceDefinition(resource_selected.c_str());
1350 std::string protocol = resource_definition->protocol.in();
1351 std::string username = resource_definition->username.in();
1352 std::string applipath = resource_definition->applipath.in();
1354 if (protocol == "" || username == "" || applipath == "")
1356 INFOS("[checkPaCOParameters] resource selected is not well defined");
1357 INFOS("[checkPaCOParameters] resource name: " << resource_definition->name.in());
1358 INFOS("[checkPaCOParameters] resource hostname: " << resource_definition->hostname.in());
1359 INFOS("[checkPaCOParameters] resource protocol: " << protocol);
1360 INFOS("[checkPaCOParameters] resource username: " << username);
1361 INFOS("[checkPaCOParameters] resource applipath: " << applipath);
1368 char *SALOME_ContainerManager::GetenvThreadSafe(const char *name)
1369 {// getenv is not thread safe. See man 7 pthread.
1370 Utils_Locker lock (&_getenvMutex);
1371 return getenv(name);
1374 int SALOME_ContainerManager::SystemThreadSafe(const char *command)
1376 Utils_Locker lock (&_systemMutex);
1377 return system(command);
1380 #ifdef WITH_PACO_PARALLEL
1382 //=============================================================================
1384 * Start a suitable PaCO++ Parallel Container in a list of machines.
1385 * \param params Container Parameters required for the container
1386 * \return CORBA container reference.
1388 //=============================================================================
1389 Engines::Container_ptr
1390 SALOME_ContainerManager::StartPaCOPPContainer(const Engines::ContainerParameters& params_const,
1391 std::string resource_selected)
1393 CORBA::Object_var obj;
1394 PaCO::InterfaceManager_var container_proxy;
1395 Engines::Container_ptr ret = Engines::Container::_nil();
1396 Engines::ContainerParameters params(params_const);
1397 params.resource_params.name = CORBA::string_dup(resource_selected.c_str());
1399 // Step 0 : Check parameters
1400 if (!checkPaCOParameters(params, resource_selected))
1402 INFOS("[StartPaCOPPContainer] check parameters failed ! see logs...");
1406 // Step 1 : Starting a new parallel container !
1407 INFOS("[StartPaCOPPContainer] Starting a PaCO++ parallel container");
1408 INFOS("[StartPaCOPPContainer] on resource : " << resource_selected);
1410 // Step 2 : Get a MachineFile for the parallel container
1411 std::string machine_file_name = _ResManager->getMachineFile(resource_selected,
1413 params.parallelLib.in());
1415 if (machine_file_name == "")
1417 INFOS("[StartPaCOPPContainer] Machine file generation failed");
1421 // Step 3 : starting parallel container proxy
1422 std::string command_proxy("");
1423 std::string proxy_machine;
1426 command_proxy = BuildCommandToLaunchPaCOProxyContainer(params, machine_file_name, proxy_machine);
1428 catch(const SALOME_Exception & ex)
1430 INFOS("[StartPaCOPPContainer] Exception in BuildCommandToLaunchPaCOContainer");
1434 obj = LaunchPaCOProxyContainer(command_proxy, params, proxy_machine);
1435 if (CORBA::is_nil(obj))
1437 INFOS("[StartPaCOPPContainer] LaunchPaCOContainer for proxy returns NIL !");
1440 container_proxy = PaCO::InterfaceManager::_narrow(obj);
1441 MESSAGE("[StartPaCOPPContainer] PaCO container proxy is launched");
1443 // Step 4 : starting parallel container nodes
1444 std::string command_nodes("");
1445 SALOME_ContainerManager::actual_launch_machine_t nodes_machines;
1448 command_nodes = BuildCommandToLaunchPaCONodeContainer(params, machine_file_name, nodes_machines, proxy_machine);
1450 catch(const SALOME_Exception & ex)
1452 INFOS("[StarPaCOPPContainer] Exception in BuildCommandToLaunchPaCONodeContainer");
1457 std::string container_generic_node_name = std::string(params.container_name.in()) + std::string("Node");
1458 bool result = LaunchPaCONodeContainer(command_nodes, params, container_generic_node_name, nodes_machines);
1461 INFOS("[StarPaCOPPContainer] LaunchPaCONodeContainer failed !");
1462 // Il faut tuer le proxy
1465 Engines::Container_var proxy = Engines::Container::_narrow(container_proxy);
1470 INFOS("[StarPaCOPPContainer] Exception catched from proxy Shutdown...");
1475 // Step 4 : connecting nodes and the proxy to actually create a parallel container
1476 for (int i = 0; i < params.nb_proc; i++)
1478 std::ostringstream tmp;
1480 std::string proc_number = tmp.str();
1481 std::string container_node_name = container_generic_node_name + proc_number;
1483 std::string theNodeMachine(nodes_machines[i]);
1484 std::string containerNameInNS = _NS->BuildContainerNameForNS(container_node_name.c_str(), theNodeMachine.c_str());
1485 obj = _NS->Resolve(containerNameInNS.c_str());
1486 if (CORBA::is_nil(obj))
1488 INFOS("[StarPaCOPPContainer] CONNECTION FAILED From Naming Service !");
1489 INFOS("[StarPaCOPPContainer] Container name is " << containerNameInNS);
1494 MESSAGE("[StarPaCOPPContainer] Deploying node : " << container_node_name);
1495 PaCO::InterfaceParallel_var node = PaCO::InterfaceParallel::_narrow(obj);
1497 MESSAGE("[StarPaCOPPContainer] node " << container_node_name << " is deployed");
1499 catch(CORBA::SystemException& e)
1501 INFOS("[StarPaCOPPContainer] Exception in deploying node : " << containerNameInNS);
1502 INFOS("CORBA::SystemException : " << e);
1505 catch(CORBA::Exception& e)
1507 INFOS("[StarPaCOPPContainer] Exception in deploying node : " << containerNameInNS);
1508 INFOS("CORBA::Exception" << e);
1513 INFOS("[StarPaCOPPContainer] Exception in deploying node : " << containerNameInNS);
1514 INFOS("Unknown exception !");
1519 // Step 5 : starting parallel container
1522 MESSAGE ("[StarPaCOPPContainer] Starting parallel object");
1523 container_proxy->start();
1524 MESSAGE ("[StarPaCOPPContainer] Parallel object is started");
1525 ret = Engines::Container::_narrow(container_proxy);
1527 catch(CORBA::SystemException& e)
1529 INFOS("Caught CORBA::SystemException. : " << e);
1531 catch(PortableServer::POA::ServantAlreadyActive&)
1533 INFOS("Caught CORBA::ServantAlreadyActiveException");
1535 catch(CORBA::Exception&)
1537 INFOS("Caught CORBA::Exception.");
1539 catch(std::exception& exc)
1541 INFOS("Caught std::exception - "<<exc.what());
1545 INFOS("Caught unknown exception.");
1551 SALOME_ContainerManager::BuildCommandToLaunchPaCOProxyContainer(const Engines::ContainerParameters& params,
1552 std::string machine_file_name,
1553 std::string & proxy_hostname)
1555 // In the proxy case, we always launch a Dummy Proxy
1556 std::string exe_name = "SALOME_ParallelContainerProxyDummy";
1557 std::string container_name = params.container_name.in();
1559 // Convert nb_proc in string
1560 std::ostringstream tmp_string;
1561 tmp_string << params.nb_proc;
1562 std::string nb_proc_str = tmp_string.str();
1564 // Get resource definition
1565 Engines::ResourceDefinition_var resource_definition =
1566 _ResManager->GetResourceDefinition(params.resource_params.name);
1569 std::string hostname;
1570 std::ifstream machine_file(machine_file_name.c_str());
1571 std::getline(machine_file, hostname, ' ');
1572 size_t found = hostname.find('\n');
1573 if (found!=std::string::npos)
1574 hostname.erase(found, 1); // Remove \n
1575 proxy_hostname = hostname;
1576 MESSAGE("[BuildCommandToLaunchPaCOProxyContainer] machine file name extracted is " << hostname);
1579 bool remote_execution = false;
1580 if (hostname != std::string(Kernel_Utils::GetHostname()))
1582 MESSAGE("[BuildCommandToLaunchPaCOProxyContainer] remote machine case detected !");
1583 remote_execution = true;
1586 // Log environnement
1587 std::string log_type("");
1588 char * get_val = GetenvThreadSafe("PARALLEL_LOG");
1592 // Generating the command
1593 std::string command_begin("");
1594 std::string command_end("");
1595 std::ostringstream command;
1597 LogConfiguration(log_type, "proxy", container_name, hostname, command_begin, command_end);
1598 command << command_begin;
1600 // Adding connection command
1601 // We can only have a remote execution with
1602 // a SALOME application
1603 if (remote_execution)
1605 ASSERT(GetenvThreadSafe("NSHOST"));
1606 ASSERT(GetenvThreadSafe("NSPORT"));
1608 command << resource_definition->protocol.in();
1610 command << resource_definition->username.in();
1611 command << " " << hostname;
1612 command << " " << resource_definition->applipath.in();
1613 command << "/runRemote.sh ";
1614 command << GetenvThreadSafe("NSHOST") << " "; // hostname of CORBA name server
1615 command << GetenvThreadSafe("NSPORT") << " "; // port of CORBA name server
1618 command << exe_name;
1619 command << " " << container_name;
1620 command << " Dummy";
1621 command << " " << hostname;
1622 command << " " << nb_proc_str;
1624 AddOmninamesParams(command);
1627 command << command_end;
1628 MESSAGE("[BuildCommandToLaunchPaCOProxyContainer] Command is: " << command.str());
1630 return command.str();
1634 SALOME_ContainerManager::BuildCommandToLaunchPaCONodeContainer(const Engines::ContainerParameters& params,
1635 const std::string & machine_file_name,
1636 SALOME_ContainerManager::actual_launch_machine_t & vect_machine,
1637 const std::string & proxy_hostname)
1640 std::string exe_name = "SALOME_ParallelContainerNode";
1641 exe_name += params.parallelLib.in();
1642 std::string container_name = params.container_name.in();
1644 // Convert nb_proc in string
1645 std::ostringstream nb_proc_stream;
1646 nb_proc_stream << params.nb_proc;
1648 // Get resource definition
1649 Engines::ResourceDefinition_var resource_definition =
1650 _ResManager->GetResourceDefinition(params.resource_params.name);
1652 // Log environnement
1653 std::string log_type("");
1654 char * get_val = GetenvThreadSafe("PARALLEL_LOG");
1658 // Now the command is different according to paralleLib
1659 std::ostringstream command_nodes;
1660 std::ifstream machine_file(machine_file_name.c_str());
1661 if (std::string(params.parallelLib.in()) == "Dummy")
1663 for (int i= 0; i < params.nb_proc; i++)
1666 std::string hostname;
1667 std::getline(machine_file, hostname);
1668 MESSAGE("[BuildCommandToLaunchPaCONodeContainer] machine file name extracted is " << hostname);
1671 bool remote_execution = false;
1672 if (hostname != std::string(Kernel_Utils::GetHostname()))
1674 MESSAGE("[BuildCommandToLaunchPaCONodeContainer] remote machine case detected !");
1675 remote_execution = true;
1678 // For each node we have a new command
1679 // Generating the command
1680 std::ostringstream command_node_stream;
1681 std::string command_node_begin("");
1682 std::string command_node_end("");
1683 std::ostringstream node_number;
1685 std::string container_node_name = container_name + node_number.str();
1686 LogConfiguration(log_type, "node", container_node_name, hostname, command_node_begin, command_node_end);
1688 // Adding connection command
1689 // We can only have a remote execution with
1690 // a SALOME application
1691 if (remote_execution)
1693 ASSERT(GetenvThreadSafe("NSHOST"));
1694 ASSERT(GetenvThreadSafe("NSPORT"));
1696 command_node_stream << resource_definition->protocol.in();
1697 command_node_stream << " -l ";
1698 command_node_stream << resource_definition->username.in();
1699 command_node_stream << " " << hostname;
1700 command_node_stream << " " << resource_definition->applipath.in();
1701 command_node_stream << "/runRemote.sh ";
1702 command_node_stream << GetenvThreadSafe("NSHOST") << " "; // hostname of CORBA name server
1703 command_node_stream << GetenvThreadSafe("NSPORT") << " "; // port of CORBA name server
1706 command_node_stream << exe_name;
1707 command_node_stream << " " << container_name;
1708 command_node_stream << " " << params.parallelLib.in();
1709 command_node_stream << " " << proxy_hostname;
1710 command_node_stream << " " << node_number.str();
1711 command_node_stream << " -";
1712 AddOmninamesParams(command_node_stream);
1714 command_nodes << command_node_begin << command_node_stream.str() << command_node_end;
1715 vect_machine.push_back(hostname);
1719 else if (std::string(params.parallelLib.in()) == "Mpi")
1722 std::string hostname;
1723 std::getline(machine_file, hostname, ' ');
1724 MESSAGE("[BuildCommandToLaunchPaCONodeContainer] machine file name extracted is " << hostname);
1727 bool remote_execution = false;
1728 if (hostname != std::string(Kernel_Utils::GetHostname()))
1730 MESSAGE("[BuildCommandToLaunchPaCONodeContainer] remote machine case detected !");
1731 remote_execution = true;
1734 // In case of Mpi and Remote, we copy machine_file in the applipath
1735 // scp mpi_machine_file user@machine:Path
1736 std::ostringstream command_remote_stream;
1737 std::string::size_type last = machine_file_name.find_last_of("/");
1738 if (last == std::string::npos)
1741 std::string protocol = resource_definition->protocol.in();
1742 if (protocol == "rsh")
1743 command_remote_stream << "rcp ";
1745 command_remote_stream << "scp ";
1746 command_remote_stream << machine_file_name << " ";
1747 command_remote_stream << resource_definition->username.in() << "@";
1748 command_remote_stream << hostname << ":" << resource_definition->applipath.in();
1749 command_remote_stream << "/" << machine_file_name.substr(last+1);
1751 int status = SystemThreadSafe(command_remote_stream.str().c_str());
1754 INFOS("copy of the MPI machine file failed ! - sorry !");
1758 // Generating the command
1759 std::string command_begin("");
1760 std::string command_end("");
1762 LogConfiguration(log_type, "nodes", container_name, hostname, command_begin, command_end);
1763 command_nodes << command_begin;
1765 // Adding connection command
1766 // We can only have a remote execution with
1767 // a SALOME application
1768 if (remote_execution)
1770 ASSERT(GetenvThreadSafe("NSHOST"));
1771 ASSERT(GetenvThreadSafe("NSPORT"));
1773 command_nodes << resource_definition->protocol.in();
1774 command_nodes << " -l ";
1775 command_nodes << resource_definition->username.in();
1776 command_nodes << " " << hostname;
1777 command_nodes << " " << resource_definition->applipath.in();
1778 command_nodes << "/runRemote.sh ";
1779 command_nodes << GetenvThreadSafe("NSHOST") << " "; // hostname of CORBA name server
1780 command_nodes << GetenvThreadSafe("NSPORT") << " "; // port of CORBA name server
1783 if (std::string(resource_definition->mpiImpl.in()) == "lam")
1785 command_nodes << "mpiexec -ssi boot ";
1786 command_nodes << "-machinefile " << machine_file_name << " ";
1787 command_nodes << "-n " << params.nb_proc;
1791 command_nodes << "mpirun -np " << params.nb_proc;
1793 command_nodes << " " << exe_name;
1794 command_nodes << " " << container_name;
1795 command_nodes << " " << params.parallelLib.in();
1796 command_nodes << " " << proxy_hostname;
1797 command_nodes << " -";
1798 AddOmninamesParams(command_nodes);
1800 // We don't put hostname, because nodes are registered in the resource of the proxy
1801 for (int i= 0; i < params.nb_proc; i++)
1802 vect_machine.push_back(proxy_hostname);
1804 command_nodes << command_end;
1806 return command_nodes.str();
1810 SALOME_ContainerManager::LogConfiguration(const std::string & log_type,
1811 const std::string & exe_type,
1812 const std::string & container_name,
1813 const std::string & hostname,
1814 std::string & begin,
1817 if(log_type == "xterm")
1819 begin = "xterm -e \"export LD_LIBRARY_PATH=$LD_LIBRARY_PATH; export PATH=$PATH;";
1822 else if(log_type == "xterm_debug")
1824 begin = "xterm -e \"export LD_LIBRARY_PATH=$LD_LIBRARY_PATH; export PATH=$PATH;";
1829 // default into a file...
1830 std::string logFilename = "/tmp/" + container_name + "_" + hostname + "_" + exe_type + "_";
1831 logFilename += std::string(GetenvThreadSafe("USER")) + ".log";
1832 end = " > " + logFilename + " 2>&1 & ";
1837 SALOME_ContainerManager::LaunchPaCOProxyContainer(const std::string& command,
1838 const Engines::ContainerParameters& params,
1839 const std::string & hostname)
1841 PaCO::InterfaceManager_ptr container_proxy = PaCO::InterfaceManager::_nil();
1843 MESSAGE("[LaunchPaCOProxyContainer] Launch command");
1844 int status = SystemThreadSafe(command.c_str());
1846 INFOS("[LaunchPaCOProxyContainer] failed : system command status -1");
1847 return container_proxy;
1849 else if (status == 217) {
1850 INFOS("[LaunchPaCOProxyContainer] failed : system command status 217");
1851 return container_proxy;
1854 int count = TIME_OUT_TO_LAUNCH_CONT;
1855 CORBA::Object_var obj = CORBA::Object::_nil();
1856 std::string containerNameInNS = _NS->BuildContainerNameForNS(params.container_name.in(),
1858 MESSAGE("[LaunchParallelContainer] Waiting for Parallel Container proxy : " << containerNameInNS);
1860 while (CORBA::is_nil(obj) && count)
1864 obj = _NS->Resolve(containerNameInNS.c_str());
1869 container_proxy = PaCO::InterfaceManager::_narrow(obj);
1871 catch(CORBA::SystemException& e)
1873 INFOS("[StarPaCOPPContainer] Exception in _narrow after LaunchParallelContainer for proxy !");
1874 INFOS("CORBA::SystemException : " << e);
1875 return container_proxy;
1877 catch(CORBA::Exception& e)
1879 INFOS("[StarPaCOPPContainer] Exception in _narrow after LaunchParallelContainer for proxy !");
1880 INFOS("CORBA::Exception" << e);
1881 return container_proxy;
1885 INFOS("[StarPaCOPPContainer] Exception in _narrow after LaunchParallelContainer for proxy !");
1886 INFOS("Unknown exception !");
1887 return container_proxy;
1889 if (CORBA::is_nil(container_proxy))
1891 INFOS("[StarPaCOPPContainer] PaCO::InterfaceManager::_narrow returns NIL !");
1892 return container_proxy;
1897 //=============================================================================
1898 /*! This method launches the parallel container.
1899 * It will may be placed on the ressources manager.
1901 * \param command to launch
1902 * \param container's parameters
1903 * \param name of the container
1905 * \return CORBA container reference
1907 //=============================================================================
1909 SALOME_ContainerManager::LaunchPaCONodeContainer(const std::string& command,
1910 const Engines::ContainerParameters& params,
1911 const std::string& name,
1912 SALOME_ContainerManager::actual_launch_machine_t & vect_machine)
1914 INFOS("[LaunchPaCONodeContainer] Launch command");
1915 int status = SystemThreadSafe(command.c_str());
1917 INFOS("[LaunchPaCONodeContainer] failed : system command status -1");
1920 else if (status == 217) {
1921 INFOS("[LaunchPaCONodeContainer] failed : system command status 217");
1925 INFOS("[LaunchPaCONodeContainer] Waiting for the nodes of the parallel container");
1926 // We are waiting all the nodes
1927 for (int i = 0; i < params.nb_proc; i++)
1929 CORBA::Object_var obj = CORBA::Object::_nil();
1930 std::string theMachine(vect_machine[i]);
1932 std::ostringstream tmp;
1934 std::string proc_number = tmp.str();
1935 std::string container_node_name = name + proc_number;
1936 std::string containerNameInNS = _NS->BuildContainerNameForNS((char*) container_node_name.c_str(), theMachine.c_str());
1937 INFOS("[LaunchPaCONodeContainer] Waiting for Parallel Container node " << containerNameInNS << " on " << theMachine);
1938 int count = TIME_OUT_TO_LAUNCH_CONT;
1939 while (CORBA::is_nil(obj) && count) {
1942 obj = _NS->Resolve(containerNameInNS.c_str());
1944 if (CORBA::is_nil(obj))
1946 INFOS("[LaunchPaCONodeContainer] Launch of node failed (or not found) !");
1955 Engines::Container_ptr
1956 SALOME_ContainerManager::StartPaCOPPContainer(const Engines::ContainerParameters& params,
1957 std::string resource_selected)
1959 Engines::Container_ptr ret = Engines::Container::_nil();
1960 INFOS("[StarPaCOPPContainer] is disabled !");
1961 INFOS("[StarPaCOPPContainer] recompile SALOME Kernel to enable PaCO++ parallel extension");
1966 SALOME_ContainerManager::BuildCommandToLaunchPaCOProxyContainer(const Engines::ContainerParameters& params,
1967 std::string machine_file_name,
1968 std::string & proxy_hostname)
1974 SALOME_ContainerManager::BuildCommandToLaunchPaCONodeContainer(const Engines::ContainerParameters& params,
1975 const std::string & machine_file_name,
1976 SALOME_ContainerManager::actual_launch_machine_t & vect_machine,
1977 const std::string & proxy_hostname)
1982 SALOME_ContainerManager::LogConfiguration(const std::string & log_type,
1983 const std::string & exe_type,
1984 const std::string & container_name,
1985 const std::string & hostname,
1986 std::string & begin,
1992 SALOME_ContainerManager::LaunchPaCOProxyContainer(const std::string& command,
1993 const Engines::ContainerParameters& params,
1994 const std::string& hostname)
1996 CORBA::Object_ptr ret = CORBA::Object::_nil();
2001 SALOME_ContainerManager::LaunchPaCONodeContainer(const std::string& command,
2002 const Engines::ContainerParameters& params,
2003 const std::string& name,
2004 SALOME_ContainerManager::actual_launch_machine_t & vect_machine)