1 /*
2  *   ppuzzle.h
3  *
4  *
5  * Part of TREE-PUZZLE 5.2 (July 2004)
6  *
7  * (c) 2003-2004 by Heiko A. Schmidt, Korbinian Strimmer, and Arndt von Haeseler
8  * (c) 1999-2003 by Heiko A. Schmidt, Korbinian Strimmer,
9  *                  M. Vingron, and Arndt von Haeseler
10  * (c) 1995-1999 by Korbinian Strimmer and Arndt von Haeseler
11  *
12  * All parts of the source except where indicated are distributed under
13  * the GNU public licence.  See http://www.opensource.org for details.
14  *
15  * ($Id$)
16  *
17  */
18 
19 
20 #ifndef _PPUZZLE_
21 #define _PPUZZLE_
22 
23 /* #include "treesort.h" */
24 #include "puzzle.h"
25 #include "util.h"
26 #include "ml.h"
27 #include "sched.h"
28 
29 extern int PP_IamSlave;
30 extern int PP_IamMaster;
31 
32 #ifdef PARALLEL
33 #    ifdef SEQUENTIAL
34 #        undef SEQUENTIAL
35 #    endif
36 #    define SEQUENTIAL 0
37 #    undef  PARALLEL
38 #    define PARALLEL 1
39 #    include "mpi.h"
40 #else
41 #    ifdef SEQUENTIAL
42 #        undef SEQUENTIAL
43 #    endif
44 #    define SEQUENTIAL 1
45 #    define PARALLEL   0
46 #    undef PVERBOSE
47 #    undef PVERBOSE1
48 #    undef PVERBOSE2
49 #    undef PVERBOSE3
50 #endif
51 
52 /* PVERBOSE3 includes PVERBOSE2 includes PVERBOSE1 */
53 /* PVERBOSE1 is default (PVERBOSE)                 */
54 
55 #ifdef PVERBOSE
56 #    undef  PVERBOSE1
57 #    define PVERBOSE1
58 #endif
59 #ifdef PVERBOSE3
60 #    undef  PVERBOSE2
61 #    define PVERBOSE2
62 #endif
63 #ifdef PVERBOSE2
64 #    undef  PVERBOSE1
65 #    define PVERBOSE1
66 #endif
67 
68 #if PARALLEL
69                                    /* Scheduling algorithms for: */
70 #  define SCHEDALG_PARAM_EST  sgss /* Parameter estimation       */
71 #  define SCHEDALG_ML_STEP    sgss /* ML step                    */
72 #  define SCHEDALG_PUZZLING   sgss /* puzzling step              */
73 
74 #  define PP_DONE                0 /* Finished            M->S */
75 #  define PP_SIZES               1 /* Array sizes needed  M->S */
76 #  define PP_DATA                2 /* Data Arrays         M->S */
77 
78 #  define PP_ALLQUARTS           3 /* All Quartets        M->S */
79 
80 #  define PP_DOQUART             4 /* do 4Specs           M->S */
81 #  define PP_DOQUARTX2           5 /* do 4Specs + X^2     M->S */
82 #  define PP_QUART               6 /* quartet back        S->M */
83 #  define PP_QUARTX2             7 /* quartet + X^2 back  S->M */
84 
85 #  define PP_DOQUARTBLOCKSPECS   8 /* do block Specs      M->S */
86 #  define PP_DOQUARTBLOCK        9 /* do block of Quarts  M->S */
87 #  define PP_QUARTBLOCKSPECS    10 /* block Specs         S->M */
88 #  define PP_QUARTBLOCK         11 /* block of Quarts     S->M */
89 
90 #  define PP_DOPUZZLE           12 /* do Puzzling step    M->S, obsolete (HAS)*/
91 #  define PP_PUZZLE             13 /* Puzzling tree back  S->M, obsolete (HAS)*/
92 #  define PP_DOPUZZLEBLOCK      14 /* do Puzzling block   M->S */
93 #  define PP_DOPUZZLEBLOCKSPECS 15 /* do Puzzling block   M->S */
94 #  define PP_PUZZLEBLOCK        16 /* Puzzling block      S->M */
95 #  define PP_PUZZLEBLOCKSPECS   17 /* Puzzling block      S->M */
96 
97 #  define PP_STATS              18 /* Slave Statistics    S->M */
98 
99 #  define PP_WAIT               19 /* waiting for work    S->M */
100 
101 #  define PP_MLDISTANCE         20 /* start mldistance					M->S */ /*epe*/
102 #  define PP_UPDATERATES        21 /* update Rates					M->S */ /*epe*/
103 #  define PP_UPDATEFRACINV      22 /* update fracinv					M->S */ /*epe*/
104 #  define PP_UPDATEEEI          23 /* update Eval,Evec, Ievc				M->S */ /*epe*/
105 #  define PP_NOUPDATE           24 /* update nothing and start with scatter operation	M->S */ /*epe*/
106 #  define PP_FINALUPDATE        25 /* updates Rates, fracinv, EEI, Distanmat		M->S */ /*epe*/
107 
108 #  define PP_LSLENGTH           26 /* parallelization of lslength()			M->S */ /*epe*/
109 
110 #  define PP_TEST              100 /* testing                  */
111 
112 #  define PERMUTQUEUESIZE 100
113 #  define QUARTQUEUESIZE 100
114 
115    extern int      PP_IamMaster;
116    extern int      PP_IamSlave;
117    extern int      PP_Myid;
118    extern int      PP_MyMaster;
119    extern int      PP_NumProcs;
120    extern MPI_Comm PP_Comm;
121 #endif /* PARALLEL */
122 
123 extern int *permutsent,
124            *permutrecved,
125            *quartsent,
126            *quartrecved,
127            *doquartsent,
128            *doquartrecved,
129            *splitsent,
130            *splitrecved,
131            *permutsentn,
132            *permutrecvedn,
133            *quartsentn,
134            *quartrecvedn,
135            *doquartsentn,
136            *doquartrecvedn,
137            *splitsentn,
138            *splitrecvedn;
139 extern double *walltimes,
140               *cputimes;
141 extern double *fullwalltimes,
142               *fullcputimes;
143 extern double *altwalltimes,
144               *altcputimes;
145 
146 extern int PP_permutsent,
147            PP_permutrecved,
148            PP_quartsent,
149            PP_quartrecved,
150            PP_doquartsent,
151            PP_doquartrecved,
152            PP_splitsent,
153            PP_splitrecved,
154            PP_permutsentn,
155            PP_permutrecvedn,
156            PP_quartsentn,
157            PP_quartrecvedn,
158            PP_doquartsentn,
159            PP_doquartrecvedn,
160            PP_splitsentn,
161            PP_splitrecvedn;
162 
163 extern double PP_starttime,
164        PP_stoptime,
165        PP_inittime,
166        PP_paramcomptime,
167        PP_paramsendtime,
168        PP_quartcomptime,
169        PP_quartsendtime,
170        PP_puzzletime,
171        PP_treetime;
172 
173 void num2quart(uli qnum, int *a, int *b, int *c, int *d);
174 uli numquarts(int maxspc);
175 uli quart2num (int a, int b, int c, int d);
176 
177 int  slave_main(int argc, char *argv[]);
178 void PP_Init(int *argc, char **argv[]);
179 void PP_Finalize();
180 void PP_Printerror(FILE *of, int id, int err);
181 
182 void PP_RecvDoQuart(int *a,
183                     int *b,
184                     int *c,
185                     int *d,
186 		    int *usebestq,
187                     int *approx);
188 void PP_SendDoQuart(int dest,
189                     int a,
190                     int b,
191                     int c,
192                     int d,
193 		    int usebestq,
194                     int approx);
195 void PP_RecvQuart(int    *a,
196                   int    *b,
197                   int    *c,
198                   int    *d,
199                   double *d1,
200                   double *d2,
201                   double *d3,
202 		  int    *usebestq,
203                   int    *approx);
204 void PP_SendQuart(int    a,
205                   int    b,
206                   int    c,
207                   int    d,
208                   double d1,
209                   double d2,
210                   double d3,
211 		  int    usebestq,
212                   int    approx);
213 void PP_SendSizes(int    mspc, 			/* Maxspc */
214                   int    msite,			/* Maxsite */
215                   int    ncats,			/* numcats */
216                   int    nptrn,			/* Numptrn */
217                   int    rad,			/* tpmradix */
218                   int    outgr,			/* outgroup */
219                   double frconst,		/* fracconst */
220                   int    rseed,			/* randseed */
221                   int    fixedorder,		/* fixedorder_optn */
222 		  int    consensus);		/* consensus_optn */
223 
224 void PP_RecvSizes(int    *mspc, 		/* Maxspc */
225                   int    *msite,		/* Maxsite */
226                   int    *ncats,		/* numcats */
227                   int    *nptrn,		/* Numptrn */
228                   int    *rad,			/* tpmradix */
229                   int    *outgr,		/* outgroup */
230                   double *frconst,		/* fracconst */
231                   int    *rseed,		/* randseed */
232 		  int    *fixedorder,		/* fixedorder_optn */
233 		  int    *consensus);		/* consensus_optn */
234 void PP_RecvData(
235         cmatrix Seqpat,           /* cmatrix (Maxspc x Numptrn)             */
236         ivector Alias,            /* ivector (Maxsite)                      */
237         ivector Weight,           /* ivector (Numptrn)                      */
238         ivector constpat,
239         dvector Rates,            /* dvector (numcats)                      */
240         dvector Eval,             /* dvector (tpmradix)                     */
241         dvector Freqtpm,
242         dmatrix Evec,             /* dmatrix (tpmradix x tpmradix)          */
243         dmatrix Ievc,
244         dmatrix iexp,
245 /*      dmatrix Distanmat,*/ /*epe*/ /* dmatrix (Maxspc x Maxspc)              */
246         dcube   ltprobr);          /* dcube (numcats x tpmradix x tpmradix)  */
247 void PP_SendData(
248         cmatrix Seqpat,           /* cmatrix (Maxspc x Numptrn)             */
249         ivector Alias,            /* ivector (Maxsite)                      */
250         ivector Weight,           /* ivector (Numptrn)                      */
251         ivector constpat,
252         dvector Rates,            /* dvector (numcats)                      */
253         dvector Eval,             /* dvector (tpmradix)                     */
254         dvector Freqtpm,
255         dmatrix Evec,             /* dmatrix (tpmradix x tpmradix)          */
256         dmatrix Ievc,
257         dmatrix iexp,
258 /*      dmatrix Distanmat,*/ /*epe*/ /* dmatrix (Maxspc x Maxspc)              */
259         dcube   ltprobr);          /* dcube (numcats x tpmradix x tpmradix)  */
260 void PP_SendAllQuarts(unsigned long  Numquartets,
261                       unsigned char *quartetinfo);
262 void PP_RecvAllQuarts(int            taxa,
263                       unsigned long *Numquartets,
264                       unsigned char *quartetinfo);
265 
266 void PP_SendDoQuartBlock(int dest,
267                          uli firstq,
268                          uli amount,
269 		         int usebestq,
270                          int approx);
271 void PP_RecvDoQuartBlock(uli  *firstq,
272                          uli  *amount,
273                          uli **bq,
274 		         int  *usebestq,
275                          int  *approx);
276 void PP_SendQuartBlock(uli  startq,
277                        uli  numofq,
278                        unsigned char *quartetinfo,
279 		       uli  fullresqs, /* number of fully resolved quartets */
280 		       uli  partresqs, /* number of partly resolved quartets */
281 		       uli  unresqs,   /* number of unresolved quartets */
282 		       uli  missingqs, /* number of missing quartets */
283                        uli  numofbq,
284                        uli *bq,
285 		       int  usebestq,
286                        int  approx);
287 void PP_RecvQuartBlock(int  slave,
288                        uli *startq,
289                        uli *numofq,
290                        unsigned char *quartetinfo,
291 		       uli *fullresqs, /* number of fully resolved quartets */
292 		       uli *partresqs, /* number of partly resolved quartets */
293 		       uli *unresqs,   /* number of unresolved quartets */
294 		       uli *missingqs, /* number of missing quartets */
295 		       int *usebestq,
296                        int *approx);
297 
298 void PP_SendDoPermutBlock(uli puzzlings);
299 void PP_RecvDoPermutBlock(uli *taxa);
300 
301 void PP_SendSplitsBlock(int               taxa,
302                         uli               blocksize,
303                         cmatrix          *biparts,
304                         int               pstnum,
305                         treelistitemtype *pstlist);
306 
307 void PP_SendDone();
308 void PP_RecvDone();
309 
310 int PP_emptyslave();
311 void PP_putslave(int sl);
312 int PP_getslave();
313 
314 void PP_cmpd(int rank, double a, double b);
315 void PP_cmpi(int rank, int a, int b);
316 
317 /*epe*/
318 void PP_NextCoord(int* x, int* y, int chunk);
319 void PP_NextCoord_atamt(int* x, int* y, int chunk, int dimen);
320 void PP_VectorToDM(int x, int y, int chunk, double* result);
321 void PP_DMToVector(int x, int y, int chunk, double* result);
322 void PP_Update_Rates();
323 void PP_Update_fracinv();
324 void PP_Update_EEI();
325 void PP_NoUpdate();
326 void PP_Final_Update(void);
327 
328 #endif /* _PPUZZLE_ */
329