trick/trick_source/sim_services/MonteCarlo/MonteCarlo_spawn_slaves.cpp

122 lines
4.6 KiB
C++
Raw Normal View History

2015-02-26 15:02:31 +00:00
#include <sstream>
#include <stdlib.h>
#include "trick/MonteCarlo.hh"
#include "trick/command_line_protos.h"
#include "trick/unix_commands.h"
#include "trick/message_proto.h"
#include "trick/message_type.h"
2015-02-26 15:02:31 +00:00
/**
* @par Detailed Design:
* This function is responsible for actually issuing the system call that
2015-02-26 15:02:31 +00:00
* will create the slave simulations on the appropriate machines.
*/
void Trick::MonteCarlo::spawn_slaves() {
/** <ul><li> For all slaves: */
for (std::vector<MonteSlave *>::size_type i = 0; i < slaves.size(); ++i) {
/** <ul><li> If the slave is in the UNINITIALZED state, then
2015-02-26 15:02:31 +00:00
* set up the command string for starting the slave.
*/
if (slaves[i]->state == MonteSlave::UNINITIALIZED) {
initialize_slave(slaves[i]) ;
}
}
}
void Trick::MonteCarlo::initialize_slave(Trick::MonteSlave* slave_to_init) {
std::stringstream ss;
if (slave_to_init->remote_shell_args.empty()) {
slave_to_init->remote_shell_args = "";
}
std::string buffer;
/** <li> If this is a custom slave dispatch: add the #custom_pre_text. */
if (custom_slave_dispatch) {
buffer = custom_pre_text;
/** <li> Otherwise add the default pre text. */
} else {
default_slave_dispatch_pre_text(slave_to_init, buffer) ;
}
if (!slave_to_init->S_main_name.compare("")) {
slave_to_init->S_main_name = "./S_main_${TRICK_HOST_CPU}.exe";
}
/** <li> Append the S_main executable and slave-specific arguments. */
ss << slave_to_init->S_main_name
<< " " << command_line_args_get_input_file()
<< " --monte_host " << machine_name
<< " --monte_sync_port " << listen_device.port
<< " --monte_data_port " << data_listen_device.port
<< " --monte_client_id " << slave_to_init->id
<< " -O " << run_directory;
buffer += ss.str();
/** <li> if this is a custom slave dispatch, append the #custom_post_text. */
if (custom_slave_dispatch) {
buffer += custom_post_text;
} else {
buffer += std::string("' &");
}
if (verbosity >= INFORMATIONAL) {
message_publish(MSG_INFO, "Monte: Spawning Slave %s:%d :\n%s\n",
slave_to_init->machine_name.c_str(), slave_to_init->id, buffer.c_str()) ;
}
/** <li> Set the slave's state to INITIALIZING. */
slave_to_init->state = MonteSlave::INITIALIZING;
/** <li> Make the system call to execute the shell. */
system(buffer.c_str());
}
void Trick::MonteCarlo::default_slave_dispatch_pre_text(Trick::MonteSlave* slave_to_init, std::string &buffer) {
/** <ul><li> If the slave is running locally, use a local shell. */
if (!localhost_as_remote &&
(equals_ignore_case(slave_to_init->machine_name, machine_name) ||
equals_ignore_case(slave_to_init->machine_name, std::string("localhost")))) {
buffer = "/bin/sh -c '";
/** <li> Otherwise, use the appropriate remote shell. */
} else {
switch (slave_to_init->remote_shell) {
case TRICK_RSH:
buffer = unix_rsh;
break;
case TRICK_USER_REMOTE_SH:
if (slave_to_init->user_remote_shell.empty()) {
slave_to_init->user_remote_shell = unix_ssh;
if (verbosity >= ERROR) {
message_publish(MSG_WARNING, "Monte: TRICK_USER_REMOTE_SH specified for Slave %s:%d, but no shell given.\n"
"Defaulting to %s.\n",
2015-02-26 15:02:31 +00:00
machine_name.c_str(), slave_to_init->id, slave_to_init->user_remote_shell.c_str()) ;
}
}
buffer = slave_to_init->user_remote_shell;
2015-02-26 15:02:31 +00:00
break;
default:
buffer = unix_ssh;
break;
}
/** <ul><li> Add the user-specified remote shell arguments and the remote machine name. </ul> */
buffer += " " + slave_to_init->remote_shell_args + " " + slave_to_init->machine_name + " '";
}
/** <li> Add the user command string to allow passing
* of environment variables and such to the slave.
2015-02-26 15:02:31 +00:00
*/
if (!user_cmd_string.empty()) {
buffer += user_cmd_string + "; ";
}
/** <li> Add the command to switch to the appropriate directory. */
if (slave_to_init->remote_shell != TRICK_USER_REMOTE_SH) {
buffer += "cd ";
if (!slave_to_init->sim_path.empty()) {
buffer += slave_to_init->sim_path;
} else {
buffer += std::string(command_line_args_get_default_dir());
}
buffer += std::string("; ");
}
}