Rosetta 3.5
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Slave.cc
Go to the documentation of this file.
1 // -*- mode:c++;tab-width:2;indent-tabs-mode:t;show-trailing-whitespace:t;rm-trailing-spaces:t -*-
2 // vi: set ts=2 noet:
3 //
4 // (c) Copyright Rosetta Commons Member Institutions.
5 // (c) This file is part of the Rosetta software suite and is made available under license.
6 // (c) The Rosetta software is developed by the contributing members of the Rosetta Commons.
7 // (c) For more information, see http://www.rosettacommons.org. Questions about this can be
8 // (c) addressed to University of Washington UW TechTransfer, email: license@u.washington.edu.
9 
10 /// @file protocols/elscripts/Slave.cc
11 /// @brief the slave role of elscripts
12 /// @author Ken Jung
13 
14 #ifdef USELUA
16 
17 #include <boost/bind.hpp>
18 #include <boost/function.hpp>
19 
21 
22 #include <basic/Tracer.hh>
23 
24 namespace protocols {
25 namespace elscripts {
26 
27 void lregister_Slave( lua_State * lstate ) {
28  lregister_BaseRole( lstate );
29  luabind::module(lstate, "protocols")
30  [
31  luabind::namespace_("elscripts")
32  [
33  luabind::class_<Slave, BaseRole>("Slave")
34  ]
35  ];
36 }
37 
38 static basic::Tracer TR("protocols.elscripts.Slave");
39 
40 Slave::Slave( int master, boost::uint64_t mem_limit, boost::uint64_t reserved_mem, boost::uint64_t reserved_mem_multiplier) :
41  master_(master),
42  BaseRole( mem_limit, reserved_mem, reserved_mem_multiplier) {
43 
44  // endpoint uses this function to get role-wide memory usage
45  boost::function< boost::uint64_t ()> ref_available_mem = boost::bind( &protocols::elscripts::Slave::available_mem, this );
46 
47  master_comm_ = protocols::wum2::EndPointSP( new protocols::wum2::EndPoint( ref_available_mem ) );
48 
49  lua_init();
50  lregister_Slave(lstate_);
51  luabind::globals(lstate_)["slave"] = this;
52  luabind::globals(lstate_)["rank"] = 0;
53 
54  register_calculators();
55  instantiate_tasks();
56  instantiate_scorefxns();
57  instantiate_filters();
58  instantiate_movers();
59  instantiate_output();
60  instantiate_workunits();
61 }
62 
63 void Slave::go(){
64  using namespace utility::lua;
65 
66  // run the first wu in the queue
67  if( ! master_comm_->inq().empty() ) {
68  protocols::wum2::WorkUnitSP wu = master_comm_->inq().pop_front();
69  // try dynamic casts to WorkUnit_elscriptsState
70  if( wu ) {
71  protocols::wum2::WorkUnit_ElScriptsSP castattempt = boost::dynamic_pointer_cast<protocols::wum2::WorkUnit_ElScripts> (wu);
72  if( castattempt ) {
73  std::string wuname = castattempt->name();
74  // create temporary environment that will be thrown away after run()
75  std::string action = "DELIM(\n"
76  "tmp_run_env = {}\n"
77  "setmetatable(tmp_run_env, {__index = _G })\n"
78  ")DELIM";
79  int err = luaL_dostring ( lstate_, action.c_str() );
80  if( err == 1) {
81  TR << "Creating tmp namespace for run_on_slave() on slave failed. Error is:" << std::endl;
82  TR << lua_tostring(lstate_, -1) << std::endl;
83  std::exit(9);
84  }
85 
86  // exporting useful stuff to the tmp env
87  luabind::globals(lstate_)["tmp_run_env"]["pipemap"] = castattempt->pipemap().lock();
88  luabind::globals(lstate_)["tmp_run_env"]["state"] = castattempt->state().lock();
89  luabind::globals(lstate_)["tmp_run_env"]["traj_idx"] = castattempt->trajectory_idx();
90 
91  //calling run fxn
92  action = "(\n"
93  "els_setenv(tmp_run_env)\n"
94  "els.workunits.)"+wuname+"DELIM(.run_on_slave()\n"
95  "tmp_run_env = {} -- delete tmp_env after calling run()\n"
96  ")DELIM";
97  err = luaL_dostring ( lstate_, action.c_str() );
98  if( err == 1) {
99  TR << "Calling lua function for workunit " << wuname << " run_on_slave fxn failed. Error is:" << std::endl;
100  TR << lua_tostring(lstate_, -1) << std::endl;
101  std::exit(9);
102  }
103  lua_gc(lstate_, LUA_GCCOLLECT, 0);
104  // shallow copy! works fine for my purposes
105  protocols::wum2::WorkUnitSP result_wu( new protocols::wum2::WorkUnit_ElScripts( *castattempt ) );
106  master_comm_->outq().push_back( result_wu );
107  } else {
108  wu->run();
109  }
110  }
111  }
112 }
113 
114 
115 } //elscripts
116 } //protocols
117 #endif