17 #include <boost/bind.hpp>
18 #include <boost/function.hpp>
19 #include <boost/lexical_cast.hpp>
21 #ifdef USEBOOSTSERIALIZE
22 #include <boost/archive/binary_oarchive.hpp>
27 #include <basic/options/option.hh>
28 #include <basic/options/keys/els.OptionKeys.gen.hh>
30 #include <utility/lua/LuaIterator.hh>
32 #include <utility/Factory.hh>
37 #include <basic/Tracer.hh>
43 static basic::Tracer
TR(
"protocols.elscripts.Master");
45 void lregister_Master( lua_State * lstate ) {
46 lregister_BaseRole( lstate );
47 luabind::module(lstate,
"protocols")
49 luabind::namespace_(
"elscripts")
51 luabind::class_<Master, BaseRole>(
"Master")
52 .def(
"make_wu_until_limit", &Master::make_wu_until_limit)
59 .def(
"interpreter", &Master::interpreter)
60 .def(
"end_traj", &Master::end_traj)
65 Master::Master(
int num_trajectories, boost::uint64_t mem_limit, boost::uint64_t reserved_mem, boost::uint64_t reserved_mem_multiplier) :
66 num_trajectories_(num_trajectories),
68 num_trajectories_finished_(0),
69 mpicounter_(10000000),
71 last_generate_initial_wu_time_( boost::posix_time::microsec_clock::universal_time() - boost::posix_time::minutes(100)),
72 BaseRole( mem_limit, reserved_mem, reserved_mem_multiplier) {
73 using namespace basic::options;
74 using namespace basic::options::OptionKeys;
77 boost::function< boost::uint64_t ()> ref_available_mem = boost::bind( &protocols::elscripts::Master::available_mem,
this );
90 lregister_Master(lstate_);
91 luabind::globals(lstate_)[
"master"] =
this;
92 luabind::globals(lstate_)[
"num_trajectories"] = num_trajectories_;
94 instantiate_inputters();
95 instantiate_inputterstream();
98 register_calculators();
99 instantiate_scorefxns();
100 instantiate_filters();
101 instantiate_movers();
102 instantiate_workunits();
106 for(
int i = 0; i < num_trajectories_; i++ ) {
113 "for i=0,num_trajectories do\n"
114 " traj_env[i] = {}\n"
115 " setmetatable( traj_env[i], { __index = _G } )\n"
116 " local _ENV = traj_env[i]\n"
122 int err = luaL_dostring ( lstate_, action.c_str() );
124 TR <<
"Setting up trajectory environments failed. Error is:" << std::endl;
125 TR << lua_tostring(lstate_, -1) << std::endl;
129 luabind::globals(lstate_)[
"trajectories"] = trajectories_;
132 void Master::interpreter() {
133 TR <<
"Switching to lua interpreter mode!" << std::endl;
137 std::cout <<
"> " << std::flush;
138 std::getline(std::cin, line);
139 if( line ==
"quit" )
break;
140 err = luaL_dostring ( lstate_, line.c_str() );
142 std::cout << lua_tostring(lstate_, -1) << std::endl;
145 TR <<
"Leaving to lua interpreter mode!" << std::endl;
149 using namespace utility::lua;
158 if( mpicounter_ >= 10000000 ) {
160 if ( ( boost::posix_time::microsec_clock::universal_time() - last_generate_initial_wu_time_) > boost::posix_time::seconds( 60 )){
164 int m = luaL_dostring ( lstate_,
"loop_every()" );
166 TR <<
"calling lua function loop_every() failed. Error is:" << std::endl;
167 TR << lua_tostring(lstate_, -1) << std::endl;
170 last_generate_initial_wu_time_ = boost::posix_time::microsec_clock::universal_time();
176 if( ! slave_comm_->inq().empty() ) {
179 if( castattempt != 0 ) {
184 luabind::globals(lstate_)[
"traj_env"][traj_idx_][
"pipemap"] = castattempt->pipemap().lock();
185 luabind::globals(lstate_)[
"traj_env"][traj_idx_][
"state"] = castattempt->state().lock();
191 " local _ENV = traj_env[)" + boost::lexical_cast<
std::string>(traj_idx_) +
"(]\n"
192 " if wu_done.)"+wuname+
"( == nil then\n"
193 " wu_done.)"+wuname+
"( = 0\n"
195 " wu_done.)"+wuname+
"( = wu_done.)"+wuname+
"( + 1\n"
196 " els_setenv(_ENV)\n"
197 " els.workunits.)"+wuname+
"DELIM(.proceed_on_master()\n"
200 int err = luaL_dostring ( lstate_, action.c_str() );
202 TR <<
"Calling lua function for workunit " << wuname <<
" proceed_on_master fxn failed. Error is:" << std::endl;
203 TR << lua_tostring(lstate_, -1) << std::endl;
206 luabind::globals(lstate_)[
"traj_env"][traj_idx_][
"pipemap"] = luabind::nil;
207 luabind::globals(lstate_)[
"traj_env"][traj_idx_][
"state"] = luabind::nil;
208 lua_gc(lstate_, LUA_GCCOLLECT, 0);
218 using namespace core::io::serialization;
227 (*pmap)[
"input"] = pipe;
230 make_wu_copied( wuname, pmap, state );
234 using namespace core::io::serialization;
240 make_wu_copied( wuname, pmap, state );
244 using namespace core::io::serialization;
249 make_wu_copied( wuname, pmap, state );
255 using namespace core::io::serialization;
258 1, traj_idx_, pmap, state, wuname
261 slave_comm_->outq().push_back( tmp );
262 if( !luabind::globals(lstate_)[
"traj_env"][traj_idx_][
"wu_made"][wuname] ) {
263 luabind::globals(lstate_)[
"traj_env"][traj_idx_][
"wu_made"][wuname] = 0;
265 luabind::globals(lstate_)[
"traj_env"][traj_idx_][
"wu_made"][wuname] = luabind::object_cast<
int>(luabind::globals(lstate_)[
"traj_env"][traj_idx_][
"wu_made"][wuname]) + 1;
268 void Master::make_wu_until_limit(
std::string const & wuname,
int num ) {
269 using namespace core::io::serialization;
270 for(
int i = 0; i < num_trajectories_; i++ ){
271 if( ! (*trajectories_)[i] )
continue;
272 if( !luabind::globals(lstate_)[
"traj_env"][i][
"wu_made"][wuname] ) {
273 luabind::globals(lstate_)[
"traj_env"][i][
"wu_made"][wuname] = 0;
275 if( luabind::object_cast<int>(luabind::globals(lstate_)[
"traj_env"][i][
"wu_made"][wuname]) < num ) {
276 if( mem_limit_ - current_mem() > 2 * reserved_mem_ ) {
279 pipe->push_back( (*trajectories_)[i] );
280 (*pmap)[
"input"] = pipe;
284 1, i, pmap, state, wuname
286 slave_comm_->outq().push_back( tmp );
287 luabind::globals(lstate_)[
"traj_env"][i][
"wu_made"][wuname] = luabind::object_cast<
int>(luabind::globals(lstate_)[
"traj_env"][i][
"wu_made"][wuname]) + 1;
295 void Master::end_traj() {
296 (*trajectories_)[traj_idx_].reset();
297 num_trajectories_finished_++;
298 TR <<
"Finished " << num_trajectories_finished_ <<
" trajectories." << std::endl;
302 "i = )" + boost::lexical_cast<
std::string>(traj_idx_) +
"DELIM(\n"
304 "setmetatable( traj_env[i], { __index = _G } )\n"
306 " local _ENV = traj_env[i]\n"
312 int err = luaL_dostring ( lstate_, action.c_str() );
314 TR <<
"Cleaning up trajectory " << traj_idx_ <<
" environment failed. Error is:" << std::endl;
315 TR << lua_tostring(lstate_, -1) << std::endl;
320 void Master::fill_trajectories() {
321 std::vector< int > needs_replace;
322 for(
int i = 0; i < num_trajectories_; i++ ){
323 if( ! (*trajectories_)[i] )
324 needs_replace.push_back( i );
327 while( needs_replace.size() != 0 && inputterstream_->has_pose() ) {
328 (*trajectories_)[ needs_replace.back() ] = inputterstream_->get_pose();
329 needs_replace.pop_back();
332 if( needs_replace.size() == num_trajectories_ ) {
333 TR <<
"Elscripts finished successfully" << std::endl;
338 void Master::update_trajectories_mem(){
339 #ifdef USEBOOSTSERIALIZE
341 boost::archive::binary_oarchive oa(s);
343 trajectories_mem_ = s.str().length();
345 TR <<
"Memory usage tracked only if compiled against boost::serialize" << std::endl;