UltraScan III
us_mpi_analysis.h
Go to the documentation of this file.
1 #ifndef US_MPI_ANALYSIS_H
2 #define US_MPI_ANALYSIS_H
3 
4 #include <QtCore>
5 #include <QtNetwork>
6 #include <mpi.h>
7 
8 #include "us_model.h"
9 #include "us_dmga_constr.h"
10 #include "us_pcsa_modelrec.h"
11 #include "us_dataIO.h"
12 #include "us_noise.h"
13 #include "us_simparms.h"
14 #include "us_solve_sim.h"
15 #include "us_vector.h"
16 #include "us_math2.h"
17 
18 #define SIMULATION US_SolveSim::Simulation
19 #define DATASET US_SolveSim::DataSet
20 #define DGene US_Model
21 
22 #define DbgLv(a) if(dbg_level>=a)qDebug()
23 #define DbTiming if(dbg_timing)qDebug()
24 
25 class US_MPI_Analysis : public QObject
26 {
27  Q_OBJECT
28 
29  public:
30  US_MPI_Analysis( int, QStringList& );
31 
32  public slots:
33  void start( void );
34 
35  private:
36 
38 
40  int my_rank;
41  int my_group;
47  int iterations; // Master only - Iterative
48  int max_iterations; // Master only - Iterative
49  int mc_iterations; // Monte Carlo
50  int mc_iteration; // Monte Carlo current iteration
53  int dbg_level;
54  bool dbg_timing;
55  bool glob_runid;
56  bool do_astfem;
59 
60  MPI_Comm my_communicator;
61 
62  int current_dataset; // For global fit or composite
63  int datasets_to_process; // For global fit or composite
64  int count_calc_residuals; // Simple counter
66 
69  int crossover;
70  int mutation;
71  int plague;
73  int elitism;
74  int attr_x;
75  int attr_y;
76  int attr_z;
77 
78  double mutate_sigma;
79  double p_mutate_s;
80  double p_mutate_k;
81  double p_mutate_sk;
82  double alpha;
83 
84  long int maxrss;
85  static const int min_experiment_size = 100;
86  static const double min_variance_improvement = 1.0e-100;
87 
88  QVector< int > worker_status;
89  QVector< int > worker_depth;
90  QList< int > ds_startx;
91  QList< int > ds_points;
92  QVector< double > gl_nnls_a;
93  QVector< double > gl_nnls_b;
94  int max_depth;
95  int worknext;
99 
102  double meniscus_range; // Only used by master
103  double meniscus_value; // Only used by worker
104  QVector< double > meniscus_values;
105 
106  QVector< double > concentrations;
107  QVector< double > maxods;
108  QVector< double > mc_data;
109  QVector< double > sigmas;
110  QVector< long > work_rss;
111 
112  US_DataIO::RawData* res_data; // Populated in calc_residuals
113  US_DataIO::RawData* sim_data; // Populated in calc_residuals
114  US_DataIO::RawData sim_data1; // Simulation for mc iteration 1
115  US_DataIO::RawData scaled_data; // Populated after global fit
116 
117  US_ModelRecord mrec; // Work PCSA model record
118  QVector< US_ModelRecord > mrecs; // PCSA model records (curves)
119  QStringList cm_files; // PCSA MC component models files
120  double pararry[ 36 ]; // PCSA parlims array;
121 
122  QHostAddress server;
123  quint16 port;
124  QUdpSocket* socket;
125 
126  QString requestID;
127  QString directory;
128  QString analysis_type;
129  QString cluster;
130  QString db_name;
131  QString modelGUID;
132  QString requestGUID;
133  QString analysisDate;
134 
135  QMap< QString, QString > parameters;
136  QMap< QString, QString > task_params;
137 
138  QDateTime submitTime;
139  QDateTime startTime;
140 
142  QList< DATASET* > data_sets;
143 
144  class MPI_Job
145  {
146  public:
147  static const int MASTER = 0;
148  static const int TAG3 = 3;
149 
152 
153  int solution;
154  int length;
156  int depth;
160 
162  {
163  solution = 0;
164  length = 0;
165  command = IDLE;
166  depth = 0;
167  meniscus_value = 0.0;
168  dataset_offset = 0;
169  dataset_count = 1;
170  };
171  };
172 
173  QList< QVector< US_Solute > > orig_solutes;
174  QVector< US_Solute > ljob_solutes;
175  QList< QVector< US_ZSolute > > orig_zsolutes;
176 
177  class Sa_Job
178  {
179  public:
181  QVector< US_Solute > solutes;
182  QVector< US_ZSolute > zsolutes;
183  };
184 
185  QList< Sa_Job > job_queue;
186 
187  static const double LARGE = 1.e39;
188  static const int solute_doubles = sizeof( US_Solute ) / sizeof( double );
189  static const int zsolut_doubles = sizeof( US_ZSolute ) / sizeof( double );
190  QList< QVector< US_Solute > > calculated_solutes;
191  QList< QVector< US_Solute > > ds_calc_solutes;
192  QVector< US_Solute > dset_calc_solutes;
193  QList< QVector< US_ZSolute > > calculated_zsolutes;
194 
198 
199  // 2DSA class
200  class Result
201  {
202  public:
203  int depth;
204  int worker;
205  QVector< US_Solute > solutes;
206  QVector< US_ZSolute > zsolutes;
207  };
208 
209  QList< Result > cached_results;
210 
211  // GA class variables and classes
212 
213  class Bucket
214  {
215  public:
216  double x_min;
217  double x_max;
218  double y_min;
219  double y_max;
220  double ds;
221  double dk;
222  };
223 
225  int s_grid;
226  int k_grid;
227  int p_grid;
229 
230  typedef QVector< US_Solute > Gene;
231 
234  QList< Bucket > buckets;
235  QList< Gene > genes;
236  QList< Gene > best_genes; // Size is number of processors
237  QList< SIMULATION > sim_values;
238  QMap < QString, double > fitness_map;
240  QList< DGene > dgenes;
241  QList< DGene > best_dgenes; // Size is number of workers
245  QVector< double > dgmarker;
246  QVector< US_dmGA_Constraints::Constraint > cns_flt;
247  QVector< int > lfvari;
248  double base_sig;
249  int nfloatc;
250  int ncompc;
251  int nassocc;
252  int nfvari;
255  bool in_gsm;
256 
257  class Fitness
258  {
259  public:
260  int index; // Index into genes and sim_values lists
261  double fitness;
262 
263  // Set the operators so we can sort a fitness list
264  bool operator== ( const Fitness& f )
265  {
266  return f.fitness == fitness;
267  }
268 
269  bool operator!= ( const Fitness& f )
270  {
271  return f.fitness != fitness;
272  }
273 
274  // Sort smallest to largest
275  bool operator< ( const Fitness& f ) const
276  {
277  return ( fitness < f.fitness );
278  }
279  };
280 
281  QList< Fitness > fitness;
282  QList< Fitness > best_fitness; // Size is number of processors
283 
285  {
286  public:
287  int generation; // From worker: -1 = final; From master: 1 = done
288  int size; // Number of solutes in the following vector or
289  // or genes requested for emmigration
290  double fitness; // Fitness of best result
291  };
292 
294 
295  // Methods
296 
297  void parse ( const QString& );
298  void parse_job ( QXmlStreamReader& );
299  void parse_dataset ( QXmlStreamReader&, DATASET* );
300  void parse_files ( QXmlStreamReader&, DATASET* );
301  void parse_solution( QXmlStreamReader&, DATASET* );
302  void send_udp ( const QString& );
303  void abort ( const QString&, int=-1 );
304  long int max_rss ( void );
305  QString par_key_value ( const QString, const QString );
306 
307  Gene create_solutes( double, double, double,
308  double, double, double );
309  void init_solutes ( void );
310  void fill_queue ( void );
311  void limitBucket ( Bucket& );
312 
313  // Master
314  void _2dsa_master ( void );
315  void submit ( Sa_Job&, int );
316  void submit_pcsa ( Sa_Job&, int );
317  void add_to_queue ( Sa_Job& );
318  void process_results ( int, const int* );
319  void shutdown_all ( void );
320  void write_noise ( US_Noise::NoiseType, const QVector< double>& );
321  void iterate ( void );
322  void set_meniscus ( void );
323  void set_monteCarlo ( void );
324  void write_output ( void );
325  void write_global ( void );
326  void set_gaussians ( void );
327  void global_fit ( void );
329  bool = false );
330  void stats_output ( int, int, int,
331  QDateTime, QDateTime, QDateTime );
332  void pm_2dsa_master ( void );
333  void pm_ga_master ( void );
334  void pm_dmga_master ( void );
335  int ready_worker ( void );
336  int low_working_depth ( void );
337  void cache_result ( Result& );
338  void process_solutes ( int&, int&, QVector< US_Solute >& );
339  void dset_matrices ( int, int,
340  QVector< double >&, QVector< double >& );
341  void update_outputs ( bool = false );
342  US_Model::AnalysisType model_type( const QString );
343 
344  // Worker
345  void _2dsa_worker ( void );
346 
347  void calc_residuals ( int, int, SIMULATION& );
348 
349  // GA Master
350  void ga_master ( void );
351  void ga_master_loop ( void );
352  void ga_global_fit ( void );
353  void set_gaMonteCarlo( void );
354 
355  // GA Worker
356  void ga_worker ( void );
357  void ga_worker_loop( void );
358  Gene new_gene ( void );
359  //void init_fitness ( void );
360  void mutate_s ( US_Solute&, int );
361  void mutate_k ( US_Solute&, int );
362  void mutate_gene ( Gene& );
363  void cross_gene ( Gene&, QList< Gene > );
364  int migrate_genes ( void );
365  double random_01 ( void );
366  int u_random ( int = 100 );
367  int e_random ( void );
368  double minimize ( Gene&, double );
369  double get_fitness ( const Gene& );
370  double get_fitness_v ( const US_Vector& );
371  double update_fitness( int, US_Vector& );
372  void lamm_gsm_df ( const US_Vector&, US_Vector& );
373  void align_gene ( Gene& );
374 
375  void vector_scaled_sum ( US_Vector&, US_Vector&, double,
376  US_Vector&, double = 1.0 );
377  void solutes_from_gene ( Gene&, int );
379  double, int );
381  US_Math2::SolutionData&, double, double );
382 
383  // DMGA Master
384  void dmga_master ( void );
385  void dmga_master_loop ( void );
386  void dmga_global_fit ( void );
387  void set_dmga_gaussians ( void );
388  void set_dmga_MonteCarlo( void );
389  void marker_from_dgene ( QVector< double >&, DGene& );
390  void dgene_from_marker ( QVector< double >&, DGene& );
391  void dgenes_to_marker ( QVector< double >&, QList< DGene >&,
392  const int, const int );
393  void marker_to_dgenes ( QVector< double >&, QList< DGene >&,
394  const int, const int );
395  bool store_attr_value ( double&, US_Model&,
397  bool fetch_attr_value ( double&, US_Model&,
399  void model_from_dgene ( US_Model&, DGene& dgene );
400 
401  // DMGA Worker
402  void dmga_worker ( void );
403  void dmga_worker_loop ( void );
404  DGene new_dmga_gene ( void );
405  void mutate_dgene ( DGene& );
406  void cross_dgene ( DGene&, QList< DGene > );
407  int migrate_dgenes ( void );
408  double get_fitness_dmga ( DGene& );
409  double get_fitness_v_dmga ( US_Vector&, US_Vector& );
411  double minimize_dmga ( DGene&, double );
412  QString dgene_key ( DGene& );
413  void calc_residuals_dmga( int, int, SIMULATION&, DGene& );
414 
415  // PCSA Master
416  void pcsa_master ( void );
417  void init_pcsa_solutes ( void );
418  void fill_pcsa_queue ( void );
419  void process_pcsa_results( const int, const int* );
420  void process_pcsa_solutes( Result& );
421  void write_mrecs ( void );
422  void iterate_pcsa ( void );
423  void tikreg_pcsa ( void );
424  void montecarlo_pcsa ( void );
425  void pcsa_best_model ( void );
426  void write_pcsa_aux_model( const int );
427  void filter_mrecs ( const int, QVector< US_ModelRecord >&,
428  QVector< US_ModelRecord >& );
429  void clean_mrecs ( QVector< US_ModelRecord >& );
430  double alpha_scan ( void );
431  void apply_alpha( const double, QVector< double >*, QVector< double >*,
432  const int, const int, const int, double&, double& );
433 
434 
435  // PCSA Worker
436  void pcsa_worker ( void );
437 
438  // Parallel Masters
439  void pmasters_start ( void );
440  void task_parse ( const QString& );
441  void pmasters_supervisor( void );
442  void pmasters_master ( void );
443  void pmasters_worker ( void );
444  void time_mc_iterations ( void );
445  void pm_cjobs_start ( void );
446  void pm_cjobs_supervisor( void );
447  void pm_cjobs_master ( void );
448  void pm_cjobs_worker ( void );
449  void time_datasets_left ( void );
450  void pm_2dsa_cjmast ( void );
451  void pm_ga_cjmast ( void );
452  void pm_dmga_cjmast ( void );
453  void pm_pcsa_cjmast ( void );
454 
455  // Debug
456  void dump_buckets( void );
457  void dump_genes ( int );
458  void dump_fitness( const QList< Fitness >& );
459 };
460 #endif
461