Rosetta 3.5
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
ArchiveBase.hh
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 // This file is part of the Rosetta software suite and is made available under license.
5 // The Rosetta software is developed by the contributing members of the Rosetta Commons consortium.
6 // (C) 199x-2009 Rosetta Commons participating institutions and developers.
7 // For more information, see http://www.rosettacommons.org/.
8 
9 /// @file protocols/jd2/MPIWorkPoolJobDistributor.hh
10 /// @brief header for MPIWorkPoolJobDistributor - intended for continuous resamplig jobs that spawn new jobs based on a pool/archive of
11 /// structures
12 /// @author Oliver Lange olange@u.washington.edu
13 
14 #ifndef INCLUDED_protocols_jd2_archive_ArchiveBase_hh
15 #define INCLUDED_protocols_jd2_archive_ArchiveBase_hh
16 
17 // Unit headers
20 
21 
22 // Package headers
24 #include <core/pose/Pose.fwd.hh>
26 // AUTO-REMOVED #include <core/io/silent/SilentFileData.hh>
27 
28 
29 // Utility headers
30 #include <core/types.hh>
31 #include <utility/pointer/ReferenceCount.hh>
32 #include <utility/exit.hh>
33 
34 // C++ headers
35 #include <string>
36 #include <list>
37 #include <deque>
38 
39 #include <utility/vector1.hh>
40 
41 
42 namespace protocols {
43 namespace jd2 {
44 namespace archive {
45 //class ArchiveManager;
46 
47 
48 
49 ///@brief Tags used to tag messeges sent by MPI functions used to decide whether a slave is requesting a new job id or
50 ///flagging as job as being a bad input
51 
52 ///@details This job distributor is meant for running jobs where the machine you are using has a large number of
53 ///processors, the number of jobs is much greater than the number of processors, or the runtimes of the individual jobs
54 ///could vary greatly. It dedicates the head node (whichever processor gets processor rank #0) to handling job requests
55 ///from the slave nodes (all nonzero ranks). Unlike the MPIWorkPartitionJobDistributor, this JD will not work at all
56 ///without MPI and the implementations of all but the interface functions have been put inside of ifdef directives.
57 ///Generally each function has a master and slave version, and the interface functions call one or the other depending
58 ///on processor rank.
60 public:
61  ///@brief Automatically generated virtual destructor for class deriving directly from ReferenceCount
62  virtual ~AbstractArchiveBase();
63  AbstractArchiveBase( ArchiveManagerAP ptr ) : manager_( ptr ), name_( "archive" ) {};
64  AbstractArchiveBase() : manager_( NULL ), name_( "archive" ) {};
65 
66  ///@brief is archive converged ?
67  virtual bool finished() const = 0;
68 
69  //this is probably obsoleted
70  // virtual bool ready_for_batch() const = 0;
71  virtual void initialize() = 0;
72 
73  ///@brief old-batches might be outdated and should not be computed anymore
74  /// return true for this query if this is the case for old_batch
75  virtual bool still_interested( jd2::archive::Batch const& /*old_batch*/ ) const { return true; };
76 
77  ///@brief create a new batch with manager().start_new_batch() and manager().finalize_batch();
78  virtual void generate_batch() = 0;
79 
80  ///@brief do some computations on archive that can be done while we are waiting
81  virtual void idle() = 0;
82 
83 
84  ///@brief read 'returned_decoys' from 'batch' into archive.
85  virtual void read_structures( core::io::silent::SilentFileData& returned_decoys, Batch const& batch ) = 0;
86 
87  ///@brief save archive to file .. you can put 'suffix' at end of dirname to save other snapshots than the 'current'
88  virtual void save_to_file( std::string suffix = "" ) = 0;
89  virtual void save_status( std::ostream& ) const = 0;
90 
91  ///@brief restore archive
92  virtual bool restore_from_file() = 0;
93 
94  virtual void init_from_decoy_set( core::io::silent::SilentFileData const& sfd ) = 0;
95 
96  ///@brief set name of archive ( used also for save_to_file and restore_from_file )
97  void set_name( std::string const& set ) { name_ = set; };
98  std::string const& name() const { return name_; };
99 
100  ///@brief access to the ArchiveManager (control of batches)
102  runtime_assert( manager_ );
103  return *manager_;
104  }
105 
106  virtual
109  }
110 protected:
111 
113  return manager_;
114  }
115 
116 private:
119 };
120 
122  //to make removal of decoys easy, this might better be a map...
123 
124 protected:
125  typedef std::list< core::io::silent::SilentStructOP > SilentStructs;
126  typedef SilentStructs::const_iterator const_decoy_iterator;
127  typedef SilentStructs::const_iterator decoy_iterator;
128 
129  //silent-struct comment identifiers
130  static std::string const TAG_IN_FILE;//( "tag_in_file" );
131  static std::string const SOURCE_FILE;//( "source_file" );
132 public:
133  ArchiveBase( ArchiveManagerAP ptr=NULL );
134  ~ArchiveBase();
135  static void register_options();
136  virtual bool finished() const { return true; };
137 
138  //obsolet ?
139  // virtual bool ready_for_batch() const { return false; };
140  virtual void initialize() {};
141 
142  virtual void generate_batch() = 0;
143 
144  ///@brief add structure to Archive.. return false if structure is rejected.
145  virtual bool add_structure( core::io::silent::SilentStructOP orig_from_batch, Batch const& );
146 
147  ///@brief how many structures should be in archive .. varies from decoys().size() in startup phase.
148  core::Size nstruct() const { return nstruct_; };
149 
150  ///@brief set target size of pool
151  void set_nstruct( core::Size set ) { nstruct_ = set; };
152 
153  ///@brief save and restore archive to file-system
154  virtual void save_to_file( std::string suffix = "" );
155  virtual bool restore_from_file();
156 
157  ///@brief save and restore status of archive to file-system
158  virtual void save_status( std::ostream& ) const;
159  virtual void restore_status( std::istream& );
160 
161  ///@brief called when nothing is happening
162  virtual void idle() {};
163 
164  ///@brief read externally provided structures from decoy_file into archive
165  virtual void init_from_decoy_set( core::io::silent::SilentFileData const& sfd );
166 
167  ///@brief SilentFileData contains the new structures belonging to this batch.
168  virtual void read_structures( core::io::silent::SilentFileData&, Batch const& batch );
169 
170  ///
171  ///---- methods to keep statistics of acceptance
172  ///
175 
176  // core::Size& proposed_since_last_batch() { return accepts_since_last_batch_; };
179  return floating_acceptance_ratio_; //will always be upper bound of true acceptance rtio
180  // statistics_valid() ? floating_acceptance_ratio_ : 1.0;
181  // return proposed_since_last_batch_ ? 1.0*accepts_since_last_batch_ / proposed_since_last_batch_ : 1.0;
182  }
183 
188  acceptance_history_.clear();
189  }
190 
194 
195  SilentStructs const& decoys() const { return decoys_; };
196  SilentStructs& decoys() { return decoys_; };
197 protected:
198  virtual void count_structure( Batch const& batch, bool accepted );
199  void count_removed_structures( core::Size n_removed );
200 
201  void set_max_nstruct( core::Size setting ) {
202  max_nstruct_ = setting;
203  }
204 
205  ///@brief call to insert structure at position given by iterator
206  void add_structure_at_position( SilentStructs::iterator iss, core::io::silent::SilentStructOP new_decoy );
207 
208 private:
209  core::Size max_nstruct_; //how many structures maximally maintained in archive
210  core::Size nstruct_; //how many structures maintained in archive
211 
213 
216 
219 
220  typedef std::deque< bool > AcceptHistoryQueue;
222 
225 
226  static bool options_registered_;
227 };
228 
229 class DebugArchive : public ArchiveBase {
230 public:
232 
233  virtual bool add_structure( core::io::silent::SilentStructOP, Batch const& );
234 
235  virtual bool finished() const { return ct_batches_ > 4; };
236  // virtual bool ready_for_batch() const { return !finished() && (ct_batches_ < 3 || decoys().size() > 200); };
237  virtual void generate_batch();
238  virtual void score( core::pose::Pose& pose ) const;
239 
240  virtual void save_status( std::ostream& ) const;
241  virtual void restore_status( std::istream& );
242 private:
246 };
247 
248 }//archive
249 }//jd2
250 }//protocols
251 
252 
253 #endif //INCLUDED_protocols_jd2_Archive_HH