Salome HOME
the dump state for batch of yacs file is optional
[modules/kernel.git] / src / Launcher / Launcher_Job_SALOME.cxx
1 //  Copyright (C) 2009-2010  CEA/DEN, EDF R&D
2 //
3 //  This library is free software; you can redistribute it and/or
4 //  modify it under the terms of the GNU Lesser General Public
5 //  License as published by the Free Software Foundation; either
6 //  version 2.1 of the License.
7 //
8 //  This library is distributed in the hope that it will be useful,
9 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
10 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 //  Lesser General Public License for more details.
12 //
13 //  You should have received a copy of the GNU Lesser General Public
14 //  License along with this library; if not, write to the Free Software
15 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 //  See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 // Author: AndrĂ© RIBES - EDF R&D
21 //
22 #include "Launcher_Job_SALOME.hxx"
23
24 #ifdef WNT
25 #include <io.h>
26 #define _chmod chmod
27 #endif
28
29 Launcher::Job_SALOME::Job_SALOME() {}
30
31 Launcher::Job_SALOME::~Job_SALOME() {}
32
33 void 
34 Launcher::Job_SALOME::setResourceDefinition(const ParserResourcesType & resource_definition)
35 {
36   // Check resource_definition
37   if (resource_definition.AppliPath == "")
38   {
39     std::string mess = "Resource definition must define an application path !, resource name is: " + resource_definition.Name;
40     throw LauncherException(mess);
41   }
42   Launcher::Job::setResourceDefinition(resource_definition);
43 }
44
45 void
46 Launcher::Job_SALOME::update_job()
47 {
48 #ifdef WITH_LIBBATCH
49   Batch::Parametre params = common_job_params();
50   params[Batch::EXECUTABLE] = buildSalomeScript(params);
51   _batch_job->setParametre(params);
52 #endif
53 }
54
55 #ifdef WITH_LIBBATCH
56 std::string 
57 Launcher::Job_SALOME::buildSalomeScript(Batch::Parametre params)
58 {
59   // parameters
60   std::string work_directory = params[Batch::WORKDIR].str();
61
62   std::string launch_date_port_file = _launch_date;
63   std::string launch_script = "/tmp/runSalome_" + _job_file_name + "_" + _launch_date + ".sh";
64   std::ofstream launch_script_stream;
65   launch_script_stream.open(launch_script.c_str(), std::ofstream::out);
66    
67   // Begin of script
68   launch_script_stream << "#! /bin/bash -f" << std::endl;
69   launch_script_stream << "cd " << work_directory << std::endl;
70   launch_script_stream << "export PYTHONPATH=" << work_directory << ":$PYTHONPATH" << std::endl;
71   launch_script_stream << "export PATH=" << work_directory << ":$PATH" << std::endl;
72   if (_env_file != "")
73   {
74     std::string::size_type last = _env_file.find_last_of("/");
75     launch_script_stream << "source ./" << _env_file.substr(last+1) << std::endl;
76   }
77   launch_script_stream << "export SALOME_TMP_DIR=" << work_directory << "/logs" << std::endl;
78
79   // -- Generates Catalog Resources
80   std::string resource_protocol = "ssh";
81   if (_resource_definition.ClusterInternalProtocol == rsh)
82     resource_protocol = "rsh";
83   
84   launch_script_stream << "if [ \"x$LIBBATCH_NODEFILE\" != \"x\" ]; then " << std::endl;
85   launch_script_stream << "CATALOG_FILE=" << "CatalogResources_" << _launch_date << ".xml" << std::endl;
86   launch_script_stream << "export USER_CATALOG_RESOURCES_FILE=" << "$CATALOG_FILE" << std::endl;
87   launch_script_stream << "echo '<!DOCTYPE ResourcesCatalog>'  > $CATALOG_FILE" << std::endl;
88   launch_script_stream << "echo '<resources>'                 >> $CATALOG_FILE" << std::endl;   
89   launch_script_stream << "cat $LIBBATCH_NODEFILE | sort | uniq -c | while read nbproc host"  << std::endl;
90   launch_script_stream << "do"                                                  << std::endl;
91   launch_script_stream << "echo '<machine hostname='\\\"$host\\\"                                 >> $CATALOG_FILE" << std::endl;
92   launch_script_stream << "echo '         protocol=\"" << resource_protocol               << "\"' >> $CATALOG_FILE" << std::endl;
93   launch_script_stream << "echo '         userName=\"" << _resource_definition.UserName   << "\"' >> $CATALOG_FILE" << std::endl;
94   launch_script_stream << "echo '         appliPath=\"" << _resource_definition.AppliPath << "\"' >> $CATALOG_FILE" << std::endl;
95   launch_script_stream << "echo '         mpi=\"" << _resource_definition.PrintMpiImplType() << "\"' >> $CATALOG_FILE" << std::endl;
96   launch_script_stream << "echo '         nbOfNodes='\\\"$nbproc\\\"                              >> $CATALOG_FILE" << std::endl;
97   launch_script_stream << "echo '         nbOfProcPerNode=\"1\"'                                          >> $CATALOG_FILE" << std::endl;
98   launch_script_stream << "echo '/>'                                                              >> $CATALOG_FILE" << std::endl;
99   launch_script_stream << "done"                                 << std::endl;
100   launch_script_stream << "echo '</resources>' >> $CATALOG_FILE" << std::endl;
101   launch_script_stream << "fi" << std::endl;
102
103   // Launch SALOME with an appli
104   launch_script_stream << _resource_definition.AppliPath << "/runAppli --terminal  --ns-port-log=" << launch_date_port_file <<  " > logs/salome_" << _launch_date << ".log 2>&1" << std::endl;
105   launch_script_stream << "current=0\n"
106                        << "stop=20\n" 
107                        << "while ! test -f " << _resource_definition.AppliPath << "/" << launch_date_port_file << "\n"
108                        << "do\n"
109                        << "  sleep 2\n"
110                        << "  let current=current+1\n"
111                        << "  if [ \"$current\" -eq \"$stop\" ] ; then\n"
112                        << "    echo Error Naming Service failed ! >&2\n"
113                        << "    exit\n"
114                        << "  fi\n"
115                        << "done\n"
116                        << "appli_port=`cat " << _resource_definition.AppliPath << "/" << launch_date_port_file << "`\n";
117
118   // Call real job type
119   addJobTypeSpecificScript(launch_script_stream);
120
121   // End
122   launch_script_stream << _resource_definition.AppliPath << "/runSession -p $appli_port shutdownSalome.py" << std::endl;
123   launch_script_stream << "sleep 10" << std::endl;
124
125   // Return
126   launch_script_stream.flush();
127   launch_script_stream.close();
128   chmod(launch_script.c_str(), 0x1ED);
129   return launch_script;
130 }
131 #endif
132