Rosetta 3.5
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
boinc.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 // (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/boinc/boinc.hh
11 /// @brief Wrappers to make BOINC work
12 /// @author David Kim (dekim@u.washington.edu)
13 
14 
15 #ifndef INCLUDED_protocols_boinc_boinc_hh
16 #define INCLUDED_protocols_boinc_boinc_hh
17 
18 // External headers: BOINC
19 #ifdef BOINC
20 #ifdef WIN32
21 #include <boinc_win.h> // WIN32 INCLUDE
22 #include <str_util.h> // WIN32 INCLUDE
23 #endif
24 #include <boinc_api.h>
25 #include <diagnostics.h>
26 #include <error_numbers.h>
27 #ifdef BOINC_GRAPHICS
28 #include <graphics2.h>
29 #include <util.h>
30 #include <shmem.h>
31 
32 // for pose serialization
33 #define SKIP_FOR_EFFICIENCY 50 //100
34 
35 #ifndef _WIN32
36 #ifdef __APPLE__
37 #include <semaphore.h>
38 #else
39 #define USE_SYSV_SEMAPHORE
40 #define SEMA_KEY_PREFIX 54321
41 #include <sys/types.h>
42 #include <sys/ipc.h>
43 #include <sys/sem.h>
44 #endif
45 #endif
46 
47 
49 #include <core/pose/Pose.fwd.hh>
50 
51 #endif // BOINC_GRAPHICS
52 #endif // BOINC
53 
54 // AUTO-REMOVED #include <protocols/boinc/watchdog.hh>
55 
56 
57 // C++ headers
58 // AUTO-REMOVED #include <string>
59 
60 #define BOINC_MAX_NO_PROGRESS_INIT_CNT 25 // maximum allowed restarts w/ no progress
61 #define BOINC_MAX_NSTRUCT 99 // maximum nstruct that the assimilator should have to handle. More than that is likely too much to sensibly upload anyway!
62 #define BOINC_DEFAULT_MAX_CPU_RUN_TIME 10800
63 #define BOINC_MAX_GFX_FPS 10.0
64 #define BOINC_MAX_GFX_CPU 10.0
65 #define BOINC_CHECKPOINT_COUNT_FILE "boinc_checkpoint_count.txt"
66 #define BOINC_INIT_COUNT_FILE "boinc_init_count.txt"
67 
68 #ifdef BOINC_GRAPHICS
69 #define BOINC_SHMEM_NAME "minirosetta"
70 #endif
71 
72 namespace protocols {
73 namespace boinc {
74 
75 
76 
77 class Boinc {
78 
79 public:
80 
81  /// @brief Access to singleton Boinc class.
82  static Boinc& instance(void);
83 
84  /// @brief Initialize BOINC. Sets up diagnostics, calls boinc_init(), etc.
85  /// Should be used at the start of main in worker app.
86  void initialize_worker( void );
87 
88  ///////////////////////////////////////////////////////////////////////////
89  ////// User project prefs
90 
91  /// @brief Get maximum graphics frames per second user preference
92  static double get_project_pref_max_gfx_fps( void );
93  /// @brief Get maximum percent cpu for graphics user preference
94  static double get_project_pref_max_gfx_cpu( void );
95  /// @brief Get maximum target cpu run time user preference
96  static int get_project_pref_max_cpu_run_time( void );
97  /// @brief Set maximum graphics frames per second. Standalone mode only.
98  static void set_project_pref_max_gfx_fps( double project_pref_max_gfx_fps );
99  /// @brief Set maximum percent cpu for graphics. Standalone mode only.
100  static void set_project_pref_max_gfx_cpu( double project_pref_max_gfx_cpu );
101  /// @brief Set maximum target cpu run time. Standalone mode only.
102  static void set_project_pref_max_cpu_run_time( int project_pref_max_cpu_run_time );
103  /// @brief Set working set size to keep track of memory usage
104  static void set_working_set_size( double working_set_size );
105  /// @brief Rereads project prefs and stores info
106  static void read_and_set_project_prefs(void);
107 
108  ///////////////////////////////////////////////////////////////////////////
109  /////// Run status - cpu time and percent complete
110 
111  /// @brief Gets the remaining cpu time which depends on the user
112  /// run time preference
113  static double get_remaining_cpu_time(void);
114  /// @brief Determines the fraction complete based on the max_cpu_run_time
115  /// preference and the total wu_cpu_run_time. Calls BOINC API
116  /// boinc_fraction_done().
117  static void update_pct_complete(void);
118 
119  /// @brief Is the task finished?
120  static bool worker_is_finished(const int & total_nstruct);
121 
122  ///////////////////////////////////////////////////////////////////////////
123  //////// Startup and shutdown
124 
125  /// @brief Start BOINC.
126  static void worker_startup( void );
127  /// @brief Stop BOINC. Does not return. Uses BOINC API boinc_finish(0).
128  static void worker_shutdown( void );
129  /// @brief Brief summary. Formatted for the R@h validator so don't change.
130  static void worker_finish_summary( const int & num_decoys, const int & attempted_decoys, const int & starting_structs );
131 
132  ///////////////////////////////////////////////////////////////////////////
133  //////// Run progress
134 
135  /// @brief Keeps track of number of checkpoints made.
136  /// Necessary for determining whether application is making progress on the client
137  static int iterate_checkpoint_count( void );
138  /// @brief Must be called after a checkpoint is made
139  static void checkpoint_completed(void);
140 
141  // for watchdog
142  static bool is_worker_running( void );
143  static void stop_running_worker( void );
144 
145  ///////////////////////////////////////////////////////////////////////////
146  //////// For graphics
147 
148 #ifdef BOINC_GRAPHICS
149 
150 /////////////////////////////////////////////////////////////////////////////
151 // WORKER APP GRAPHICS FUNCTIONS
152 
153  static void set_wu_desc( void );
154 
155  // The current pose shared memory data will get updated with the observer.
156  static const int attach_graphics_current_pose_observer( core::pose::Pose & pose );
157 
158  // Sets the native pose
159  static const int set_graphics_native_pose( core::pose::Pose & pose );
160 
161  // Update monte carlo mover trial info
162  static void update_mc_trial_info( const int & trial_cnt, const std::string & mover_type );
163 
164  // Update the current Pose buffer directly.
165  static void update_graphics_current( core::pose::Pose & pose );
166 
167 
168  // Low energy pose and energy - set by monte carlo object
169  // If PERSIST is set, later calls with DEFAULT will be ignored until RESET is used.
170  enum update_mode_enum { DEFAULT, PERSIST, RESET };
171  static void update_graphics_low_energy( core::pose::Pose & pose, core::Real low_energy, update_mode_enum update_mode = DEFAULT );
172 
173  // Last accepted pose and energy - set by monte carlo object
174  static void update_graphics_last_accepted( core::pose::Pose & pose, core::Real last_accepted_energy );
175 
176 /////////////////////////////////////////////////////////////////////////////
177 
178 
179  // shared memory
180  static void create_shared_memory( void );
181  static void attach_shared_memory( void );
182  static void update_status_shmem( void );
183  static BoincSharedMemory* get_shmem( void );
184 #endif // BOINC_GRAPHICS
185 
186  // data synchronization
187 #ifdef USE_SYSV_SEMAPHORE
188  // integer key based on the boinc client slot run directory
189  static const key_t get_sema_key( void );
190 
191  static const int destroy_semaphore( void );
192 #endif
193 
194  // name based on the boinc client slot run directory
195  static void get_sema_name( char * name);
196 
197  static int create_semaphore( void );
198  static int get_semaphore( void );
199  static int wait_semaphore( void );
200  static int trywait_semaphore( void );
201  static int unlock_semaphore( void );
202 
203  static int decoy_count() { return decoy_count_; };
204 
205 private:
206 
207  Boinc(void) {}
208 
209  static bool worker_initialized_;
210  static double fraction_done_;
211  static double cpu_time_;
212  static double working_set_size_;
214  static int checkpoint_count_;
215  static int decoy_count_;
217  static bool worker_running_;
221 
222 #ifdef BOINC_GRAPHICS
223 
224 #ifdef WIN32
225  static HANDLE sem_des_;
226 #else
227 #ifndef USE_SYSV_SEMAPHORE
228  static sem_t* sem_des_;
229 #else
230  static int sem_id_;
231 #endif
232 #endif
233 
234  static BoincSharedMemory* shmem_;
235 
236 #endif // BOINC_GRAPHICS
237 
238 };
239 
240 
241 } // namespace boinc
242 } // namespace protocols
243 
244 
245 #endif // INCLUDED_protocols_boinc_boinc_HH