Back to index

salome-kernel  6.5.0
Launcher_Job_SALOME.cxx
Go to the documentation of this file.
00001 // Copyright (C) 2009-2012  CEA/DEN, EDF R&D, OPEN CASCADE
00002 //
00003 // This library is free software; you can redistribute it and/or
00004 // modify it under the terms of the GNU Lesser General Public
00005 // License as published by the Free Software Foundation; either
00006 // version 2.1 of the License.
00007 //
00008 // This library is distributed in the hope that it will be useful,
00009 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00010 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00011 // Lesser General Public License for more details.
00012 //
00013 // You should have received a copy of the GNU Lesser General Public
00014 // License along with this library; if not, write to the Free Software
00015 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00016 //
00017 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00018 //
00019 
00020 // Author: André RIBES - EDF R&D
00021 //
00022 #include "Launcher_Job_SALOME.hxx"
00023 
00024 #ifdef WITH_LIBBATCH
00025 #include <Batch/Batch_Constants.hxx>
00026 #endif
00027 
00028 #ifdef WNT
00029 #include <io.h>
00030 #define _chmod chmod
00031 #endif
00032 
00033 Launcher::Job_SALOME::Job_SALOME() {}
00034 
00035 Launcher::Job_SALOME::~Job_SALOME() {}
00036 
00037 void 
00038 Launcher::Job_SALOME::setResourceDefinition(const ParserResourcesType & resource_definition)
00039 {
00040   // Check resource_definition
00041   if (resource_definition.AppliPath == "")
00042   {
00043     std::string mess = "Resource definition must define an application path !, resource name is: " + resource_definition.Name;
00044     throw LauncherException(mess);
00045   }
00046   Launcher::Job::setResourceDefinition(resource_definition);
00047 }
00048 
00049 void
00050 Launcher::Job_SALOME::update_job()
00051 {
00052 #ifdef WITH_LIBBATCH
00053   Batch::Parametre params = common_job_params();
00054   params[Batch::EXECUTABLE] = buildSalomeScript(params);
00055   params[Batch::EXCLUSIVE] = true;
00056   _batch_job->setParametre(params);
00057 #endif
00058 }
00059 
00060 #ifdef WITH_LIBBATCH
00061 std::string 
00062 Launcher::Job_SALOME::buildSalomeScript(Batch::Parametre params)
00063 {
00064   // parameters
00065   std::string work_directory = params[Batch::WORKDIR].str();
00066 
00067   std::string launch_script = "/tmp/runSalome_" + _job_file_name + "_" + _launch_date + ".sh";
00068   std::ofstream launch_script_stream;
00069   launch_script_stream.open(launch_script.c_str(), std::ofstream::out);
00070    
00071   // Begin of script
00072   launch_script_stream << "#!/bin/sh -f" << std::endl;
00073   launch_script_stream << "cd " << work_directory << std::endl;
00074   launch_script_stream << "export PYTHONPATH=" << work_directory << ":$PYTHONPATH" << std::endl;
00075   launch_script_stream << "export PATH=" << work_directory << ":$PATH" << std::endl;
00076   if (_env_file != "")
00077   {
00078     std::string::size_type last = _env_file.find_last_of("/");
00079     launch_script_stream << ". " << _env_file.substr(last+1) << std::endl;
00080   }
00081   launch_script_stream << "export SALOME_TMP_DIR=" << work_directory << "/logs" << std::endl;
00082 
00083   // -- Generates Catalog Resources
00084   std::string resource_protocol = ParserResourcesType::protocolToString(_resource_definition.ClusterInternalProtocol);
00085   launch_script_stream << "if [ \"x$LIBBATCH_NODEFILE\" != \"x\" ]; then " << std::endl;
00086   launch_script_stream << "CATALOG_FILE=" << "CatalogResources_" << _launch_date << ".xml" << std::endl;
00087   launch_script_stream << "export USER_CATALOG_RESOURCES_FILE=" << "$CATALOG_FILE" << std::endl;
00088   launch_script_stream << "echo '<!DOCTYPE ResourcesCatalog>'  > $CATALOG_FILE" << std::endl;
00089   launch_script_stream << "echo '<resources>'                 >> $CATALOG_FILE" << std::endl;     
00090   launch_script_stream << "cat $LIBBATCH_NODEFILE | sort | uniq -c | while read nbproc host"  << std::endl;
00091   launch_script_stream << "do"                                                  << std::endl;
00092   launch_script_stream << "echo '<machine hostname='\\\"$host\\\"                             >> $CATALOG_FILE" << std::endl;
00093   launch_script_stream << "echo '         protocol=\"" << resource_protocol               << "\"' >> $CATALOG_FILE" << std::endl;
00094   launch_script_stream << "echo '         userName=\"" << _resource_definition.UserName   << "\"' >> $CATALOG_FILE" << std::endl;
00095   launch_script_stream << "echo '         appliPath=\"" << _resource_definition.AppliPath << "\"' >> $CATALOG_FILE" << std::endl;
00096   launch_script_stream << "echo '         mpi=\"" << _resource_definition.PrintMpiImplType() << "\"' >> $CATALOG_FILE" << std::endl;
00097   launch_script_stream << "echo '    nbOfNodes='\\\"$nbproc\\\"                               >> $CATALOG_FILE" << std::endl;
00098   launch_script_stream << "echo '    nbOfProcPerNode=\"1\"'                                    >> $CATALOG_FILE" << std::endl;
00099   launch_script_stream << "echo '/>'                                                              >> $CATALOG_FILE" << std::endl;
00100   launch_script_stream << "done"                                 << std::endl;
00101   launch_script_stream << "echo '</resources>' >> $CATALOG_FILE" << std::endl;
00102   launch_script_stream << "fi" << std::endl;
00103 
00104   // Create file for ns-port-log
00105   launch_script_stream << "NS_PORT_FILE_PATH=`mktemp " << _resource_definition.AppliPath << "/USERS/nsport_XXXXXX` &&\n";
00106   launch_script_stream << "NS_PORT_FILE_NAME=`basename $NS_PORT_FILE_PATH` &&\n";
00107 
00108   // Launch SALOME with an appli
00109   launch_script_stream << _resource_definition.AppliPath << "/runAppli --terminal --ns-port-log=$NS_PORT_FILE_NAME --server-launch-mode=fork ";
00110   launch_script_stream << "> logs/salome_" << _launch_date << ".log 2>&1 &&" << std::endl;
00111   launch_script_stream << "current=0 &&\n"
00112                        << "stop=20 &&\n"
00113                        << "while ! test -s $NS_PORT_FILE_PATH\n"
00114                        << "do\n"
00115                        << "  sleep 2\n"
00116                        << "  current=$((current+1))\n"
00117                        << "  if [ \"$current\" -eq \"$stop\" ] ; then\n"
00118                        << "    echo Error Naming Service failed ! >&2\n"
00119                        << "    exit\n"
00120                        << "  fi\n"
00121                        << "done &&\n"
00122                        << "appli_port=`cat $NS_PORT_FILE_PATH` &&\n"
00123                        << "rm $NS_PORT_FILE_PATH &&\n";
00124 
00125   // Call real job type
00126   addJobTypeSpecificScript(launch_script_stream);
00127 
00128   // End
00129   launch_script_stream << _resource_definition.AppliPath << "/runSession -p $appli_port shutdownSalome.py" << std::endl;
00130   launch_script_stream << "sleep 10" << std::endl;
00131 
00132   // Return
00133   launch_script_stream.flush();
00134   launch_script_stream.close();
00135   chmod(launch_script.c_str(), 0x1ED);
00136   return launch_script;
00137 }
00138 #endif
00139