Rosetta 3.5
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
CheckPointer.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 /// @author Oliver Lange
11 /// @author Mike Tyka
12 
13 // Project headers
14 // This has to come before boinc_util.hh or we get this error on VC++
15 // '_read' : is not a member of 'std::basic_istream<_Elem,_Traits>'
16 #include <utility/io/izstream.hh>
17 
18 // must be here to avoid VC++ ambiguous symbol w/ ObjexxFCL::byte
19 // for boinc builds - dek
20 #ifdef BOINC
21 #include <protocols/boinc/boinc.hh>
23 #endif
24 
25 // Unit Headers
27 
28 // Package Headers
30 
31 // Project Headers
32 #include <core/pose/Pose.hh>
37 // AUTO-REMOVED #include <core/io/silent/ProteinSilentStruct.hh>
40 
41 #if defined(WIN32) || defined(BOINC)
43 #endif
44 
46 #include <numeric/random/random.hh>
47 
48 // ObjexxFCL Headers
49 // Utility headers
50 #include <utility/vector1.hh>
51 #include <utility/io/ozstream.hh>
52 #include <utility/file/file_sys_util.hh>
53 #include <basic/options/option.hh>
54 
55 #ifdef BOINC
56 #ifndef _WIN32
57 #include "pthread.h"
58 #endif
59 #endif
60 
61 
62 // C++ headers
63 
64 #include <basic/Tracer.hh>
65 using basic::T;
66 
67 // option key includes
68 
69 #include <basic/options/keys/run.OptionKeys.gen.hh>
70 
71 #include <core/io/pdb/file_data.hh>
72 
73 
74 
75 using basic::Error;
76 using basic::Warning;
77 
78 namespace protocols {
79 namespace checkpoint {
80 
81 basic::Tracer TR("protocols::checkpoint");
82 
83 using namespace core;
84 
85 
87 
88  //std::cerr << "Dumped checkpoint. " << std::endl;
89  if( !gzipped_ ){
90  utility::io::ozstream output( filename_ );
91  if ( !output ) {
92  std::cout << "Cannot write checkpoint! open failed for file: " << filename_ << std::endl;
93  utility::exit( EXIT_FAILURE, __FILE__, __LINE__);
94  }
95  output << contents_;
96  output.close();
97  } else {
98  utility::io::ozstream output( filename_ );
99  output << contents_;
100  output.close();
101  }
102 
103 }
104 
105 
106 bool pose_to_binary_silent_file( std::ostream &output, const std::string &tag, const pose::Pose &pose ){
107  using namespace io::silent;
108  SilentFileData outsfd;
110  pss->fill_struct( pose, tag );
111 
112  pss->print_header( output );
113  pss->print_scores( output );
114  pss->print_conformation( output );
115 
116  return true;
117 }
118 
119 bool pose_from_binary_silent_file( const std::string &filename, const std::string &tag, pose::Pose &pose, bool fullatom ){
120  using namespace core::chemical;
121  ResidueTypeSetCAP residue_set;
122  pose::Pose tmppose;
123  if( fullatom ) residue_set = ChemicalManager::get_instance()->residue_type_set( FA_STANDARD );
125  core::io::silent::SilentFileData sfd("", false, false, "binary" );
126  if(!sfd.read_file( filename ) ) return false;
127  for ( core::io::silent::SilentFileData::iterator iter = sfd.begin(), end = sfd.end(); iter != end; ++iter ) {
128  if( iter->decoy_tag() != tag ) continue;
129  iter->fill_pose( tmppose, *residue_set );
130  break;
131  }
132  tmppose.transfer_constraint_set( pose );
133  pose.clear();
134  pose = tmppose;
135  return true;
136 }
137 
139  type_ ( type ),
140  disabled_( false ),
141  count_checkpoint_recoveries_( 0 )
142 {
143  using namespace basic::options;
144  delete_checkpoints_ = option[ OptionKeys::run::delete_checkpoints ]();
145  if( option[ OptionKeys::run::suppress_checkpoints ]() ) disabled_ = true;
146 }
147 
148 
149 void CheckPointer::debug( const std::string &tag, const std::string &label, core::Real data1, core::Real data2, core::Real data3 ) const {
150  using namespace basic::options;
151  if( disabled_ ) return;
152  if( option[ OptionKeys::run::checkpoint ]() ||
153  option[ OptionKeys::run::checkpoint_interval ].user() ){
154 
155  TR.Info << "CHECKDEBUG: " << tag << "_" << label << " " << data1 << " " << data2 << " " << data3 << std::endl;
156  }
157 }
158 
160 {
161  for( core::Size i=0; i < file_buffer.size() ; i ++ ){
162  file_buffer[i].dump();
163  }
164  file_buffer.clear();
165 }
166 
168 {
169  core::Size total_size = 0;
170  for( core::Size i=0; i < file_buffer.size() ; i ++ ){
171  total_size += file_buffer[i].size();
172  }
173  return total_size;
174 }
175 
177  pose::Pose &pose,
178  moves::MonteCarlo* mc,
179  std::string const& current_tag,
180  std::string const& id,
181  bool /*foldtree*/
182 )
183 {
184 
185 
186  ///////////// Is checkpointing on at all ?
187 
188  if( disabled_ ) return;
189  if( !Timer::is_on() ) return;
190 
191  if( pose.total_residue() <= 0 ) return;
192 
193  /////////////
194  // set global bailout structure - this is used by the watchdog to abort when the watchdog time
195  // has been global_bailout = new pose::Pose( pose );
196 
197 
198 #ifdef BOINC
199  boinc_begin_critical_section();
200 
201  using namespace core::io::silent;
202 
203  std::stringstream bail_structure;
204  std::stringstream bail_structure_header;
206  ss->fill_struct( pose, "W_00000001" );
207  SilentFileDataOP sfd = new SilentFileData();
208 
209  // write the structure to the bailout buffer
210  sfd->write_silent_struct( *ss, bail_structure );
211 
212  // write the header to the bailout header bufffer (might be needed later if the watchdog
213  // is triggered before the first decoy is written.
214 
215  ss->print_header( bail_structure_header );
216 
217  // make sure the watchdog isn't about to use that variable!
218 #ifdef _WIN32
219 #else
220  pthread_mutex_lock(&protocols::boinc::watchdog::bailout_mutex);
221 #endif
224 #ifdef _WIN32
225 #else
226  pthread_mutex_unlock(&protocols::boinc::watchdog::bailout_mutex);
227 #endif
228 
229  boinc_end_critical_section();
230 #endif
231 
232 
233 
234 
235  ////// Create Checkpoint in buffer.
236 
237 
238  /// Create the unique checkpoint ID
239  std::string mcstr( "_MC" );
240  if ( mc == 0 ) mcstr = "";
241  std::string checkpoint_id( "chk_" + current_tag + "_" + type() + "_" + mcstr + "_" + id );
242  if( pose.is_fullatom() ) checkpoint_id += "_fa";
243 
244  /// Make a note of it.
245  checkpoint_ids_.push_back( checkpoint_id );
246 
247  /// Save Random State
248 
249  {
250  FileBuffer new_file( checkpoint_id + ".rng.state.gz", true /*gzipped*/ );
251  std::stringstream ss_stream;
252  numeric::random::RandomGenerator::saveAllStates( ss_stream );
253  new_file.set_contents( ss_stream.str() );
254  file_buffer.push_back( new_file );
255  }
256 // utility::io::ozstream ozs(checkpoint_id + ".rng.state.gz");
257 // numeric::random::RandomGenerator::saveAllStates(ozs);
258 // ozs.close();
259 
260 
261  std::string notag="";
262  if ( mc != 0 ) {
263  {
264  FileBuffer new_file( checkpoint_id + "mc_last.out" );
265  std::stringstream ss_stream;
266  pose_to_binary_silent_file( ss_stream , checkpoint_id, mc->last_accepted_pose() );
267  new_file.set_contents( ss_stream.str() );
268  file_buffer.push_back( new_file );
269  }
270 
271  {
272  FileBuffer new_file( checkpoint_id + "mc_low.out" );
273  std::stringstream ss_stream;
274  pose_to_binary_silent_file( ss_stream , checkpoint_id, mc->lowest_score_pose() );
275  new_file.set_contents( ss_stream.str() );
276  file_buffer.push_back( new_file );
277  }
278 // pose_to_binary_silent_file( checkpoint_id + "mc_last.out", checkpoint_id, mc->last_accepted_pose() );
279 // pose_to_binary_silent_file( checkpoint_id + "mc_low.out", checkpoint_id, mc->lowest_score_pose() );
280  }
281 
282  {
283  FileBuffer new_file( checkpoint_id + ".out" );
284  std::stringstream ss_stream;
285  pose_to_binary_silent_file( ss_stream , checkpoint_id, pose );
286  new_file.set_contents( ss_stream.str() );
287  file_buffer.push_back( new_file );
288  }
289 
290 
291 
292  TR << "CHECK: " << " Created virtual checkpoint: " << checkpoint_id << std::endl;
293 
294 
295  /////////////////////////////////////////////////////////////////////////////////////////
296  //// Is it time to flush the checkpoint ?
297 
298 
300  // ------ BOINC critical ------------------------------------
301  #ifdef BOINC
302  boinc_begin_critical_section();
303  #endif
304 
306 
307  // ------ endof BOINC critical (Timer::reset calls boinc_end_critical_section() )------------------------------------
308  protocols::checkpoint::Timer::reset(); // required for checkpoint
309  }else{
310 
311  // WHatever the user says - never store more than 10 checkpoints - it takes too much memory. Allow 15MB
312  // but no more. if we exceed that then dump checkpoint contents whatever.
313 
314  if( file_buffer_size() > 15000000 ){
316  }
317 
318  };
319 
320  //std::cerr << "CHECK: CheckPointerSize: " << file_buffer_size() << " N: " << file_buffer.size() << std::endl;
321 }
322 
324  pose::Pose & pose, moves::MonteCarlo* mc,
325  std::string const & current_tag,
326  std::string const & id,
327  bool fullatom,
328  bool foldtree
329 )
330 {
331  using namespace basic::options;
332  using namespace core::chemical;
333 
334  bool debug = option[ OptionKeys::run::debug ]();
335 
336  if( disabled_ ) return false;
337  if (!protocols::checkpoint::Timer::is_on()) return false; // required for checkpoint
338 
339  TR.Error << "recovering checkpoint of tag " << current_tag
340  << " with id " << id
341  << std::endl;
342 
343  std::string mcstr( "_MC" );
344  if ( mc == 0 ) mcstr = "";
345  // must be same as in checkpoint function
346  std::string checkpoint_id( "chk_" + current_tag + "_" + type() + "_" + mcstr + "_" + id );
347  if( fullatom ) checkpoint_id += "_fa";
348 
349 
350  ResidueTypeSetCAP residue_set;
351 
352 
353  // Make sure the check point exists - presence of the silent file indicates so.
354 
355  TR << "CHECKING FOR CHECKPOINT" << " Id: " << checkpoint_id << " : ";
356  if (!utility::file::file_exists( checkpoint_id + ".out" )) {
357  TR << " NOT PRESENT" << std::endl;
358  return false;
359  }
360 
361  // Make an appropriate residue set.
362  if( fullatom ) residue_set = ChemicalManager::get_instance()->residue_type_set( FA_STANDARD );
364 
365  // Restore random number state!
366  if (utility::file::file_exists( checkpoint_id + ".rng.state.gz" )) {
367  TR.Info << "Read random number state. " << std::endl;
368  utility::io::izstream izs(checkpoint_id + ".rng.state.gz");
369  numeric::random::RandomGenerator::restoreAllStates(izs);
370  izs.close();
371  } else {
372  utility_exit_with_message("Random generator state not found for checkpoint " + checkpoint_id + " even though the silent file is there. " );
373  }
374 
375  // get the structure from the binary file
376  pose_from_binary_silent_file( checkpoint_id + ".out", checkpoint_id, pose, fullatom );
377 
378  if( pose.is_fullatom() != fullatom ){
379  utility_exit_with_message("Fullatom mismatch in checkpointer.");
380  }
381 
382  if( debug ) core::io::pdb::FileData::dump_pdb( pose, checkpoint_id + ".debug.pdb", "", foldtree );
383 
384 #ifdef BOINC_GRAPHICS
385  // attach boinc graphics pose observer
386  protocols::boinc::Boinc::attach_graphics_current_pose_observer( pose );
387 #endif
388  if (utility::file::file_exists( checkpoint_id + ".mc_last.pdb" )) {
389  pose::Pose recovered_mc_last = mc->last_accepted_pose();
390  pose_from_binary_silent_file( checkpoint_id + ".out", checkpoint_id, recovered_mc_last, fullatom );
391  mc->set_last_accepted_pose( recovered_mc_last );
392  if(debug) core::io::pdb::FileData::dump_pdb( mc->last_accepted_pose(), checkpoint_id + ".mc_last.debug.pdb" , "", foldtree );
393  }
394  if (utility::file::file_exists( checkpoint_id + ".mc_low.pdb" )) {
395  pose::Pose recovered_mc_low = mc->lowest_score_pose();
396  pose_from_binary_silent_file( checkpoint_id + ".out", checkpoint_id, recovered_mc_low, fullatom );
397  mc->set_lowest_score_pose( recovered_mc_low );
398  if(debug) core::io::pdb::FileData::dump_pdb( mc->lowest_score_pose(), checkpoint_id + ".mc_low.debug.pdb" , "", foldtree );
399  }
400  checkpoint_ids_.push_back( checkpoint_id );
401  TR << " SUCCESS" << std::endl;
402 
403 #ifdef BOINC
404  std::cerr << "Continuing computation from checkpoint: " << checkpoint_id << " ... success! " << std::endl;
405 #endif
406 
408 
409  return true;
410 }
411 
413 
414  TR.Info << "Deleting checkpoints of " << type() << std::endl;
415  if( disabled_ ) return;
416  using namespace basic::options;
417  if( delete_checkpoints_ ){
418  for ( core::Size i = 0; i < checkpoint_ids_.size(); i++ ) {
419  //std::cerr << "deleting checkpoint files with id: " << checkpoint_ids_[i] << std::endl;
420  utility::file::file_delete( checkpoint_ids_[i] + ".mc_last.out" );
421  utility::file::file_delete( checkpoint_ids_[i] + ".mc_low.out" );
422  utility::file::file_delete( checkpoint_ids_[i] + ".out" );
423  utility::file::file_delete( checkpoint_ids_[i] + ".rng.state.gz" );
424  }
425  }else{
426  TR.Debug << "Checkpoint deletion disabled!" << std::endl;
427  }
428 
429  file_buffer.clear(); // also make sure no more structures get written out accidentally.
430  checkpoint_ids_.clear();
431 } // ClassicAbinitio::clear_checkpoints()
432 
433 
434 }
435 }