1 /*
2  * pllInternal.h
3  *
4  *  Created on: Feb 17, 2014
5  *      Author: diego
6  */
7 
8 #ifndef PLLINTERNAL_H_
9 #define PLLINTERNAL_H_
10 
11 #include "pll.h"
12 #include "genericParallelization.h"
13 #include "errcodes.h"
14 #include "lexer.h"
15 #include "parsePartition.h"
16 #include "mem_alloc.h"
17 
18 //extern int lookupWord(char *s, stringHashtable *h);
19 
20 extern void getDataTypeString(pllInstance *tr, pInfo *partitionInfo, char typeOfData[1024]);
21 extern int countTips(nodeptr p, int numsp);
22 extern unsigned int precomputed16_bitcount(unsigned int n, char *bits_in_16bits);
23 
24 extern size_t discreteRateCategories(int rateHetModel);
25 
26 extern const partitionLengths * getPartitionLengths(pInfo *p);
27 extern pllBoolean getSmoothFreqs(int dataType);
28 extern const unsigned int *getBitVector(int dataType);
29 extern int getUndetermined(int dataType);
30 extern int getStates(int dataType);
31 extern char getInverseMeaning(int dataType, unsigned char state);
32 extern double gettime ( void );
33 extern int gettimeSrand ( void );
34 extern double randum ( long *seed );
35 
36 extern void getxnode ( nodeptr p );
37 extern void hookup ( nodeptr p, nodeptr q, double *z, int numBranches);
38 extern void hookupFull ( nodeptr p, nodeptr q, double *z);
39 extern void hookupDefault ( nodeptr p, nodeptr q);
40 extern pllBoolean whitechar ( int ch );
41 extern void printLog ( pllInstance *tr);
42 extern double LnGamma ( double alpha );
43 extern double IncompleteGamma ( double x, double alpha, double ln_gamma_alpha );
44 extern double PointNormal ( double prob );
45 extern double PointChi2 ( double prob, double v );
46 extern void initModel ( pllInstance *tr, double **empiricalFrequencies, partitionList * partitions);
47 
48 extern void resetBranches ( pllInstance *tr );
49 extern void modOpt ( pllInstance *tr, partitionList *pr, double likelihoodEpsilon);
50 
51 extern void initializePartitionData(pllInstance *localTree, partitionList * localPartitions);
52 extern void initMemorySavingAndRecom(pllInstance *tr, partitionList *pr);
53 
54 extern void nodeRectifier ( pllInstance *tr );
55 extern void allocateParsimonyDataStructures(pllInstance *tr, partitionList *pr);
56 
57 extern FILE *myfopen(const char *path, const char *mode);
58 
59 extern pllBoolean initrav ( pllInstance *tr, partitionList *pr, nodeptr p );
60 extern void initravPartition ( pllInstance *tr, nodeptr p, int model );
61 extern void update ( pllInstance *tr, partitionList *pr, nodeptr p );
62 extern void smooth ( pllInstance *tr, partitionList *pr, nodeptr p );
63 extern void smoothTree ( pllInstance *tr, partitionList *pr, int maxtimes );
64 extern void localSmooth ( pllInstance *tr, partitionList *pr, nodeptr p, int maxtimes );
65 extern pllBoolean localSmoothMulti(pllInstance *tr, nodeptr p, int maxtimes, int model);
66 
67 extern void smoothRegion ( pllInstance *tr, partitionList *pr, nodeptr p, int region );
68 extern void regionalSmooth ( pllInstance *tr, partitionList *pr, nodeptr p, int maxtimes, int region );
69 extern nodeptr removeNodeBIG ( pllInstance *tr, partitionList *pr, nodeptr p, int numBranches);
70 extern nodeptr removeNodeRestoreBIG ( pllInstance *tr, partitionList *pr, nodeptr p );
71 extern pllBoolean insertBIG ( pllInstance *tr, partitionList *pr, nodeptr p, nodeptr q);
72 extern pllBoolean insertRestoreBIG ( pllInstance *tr, partitionList *pr, nodeptr p, nodeptr q );
73 extern pllBoolean testInsertBIG ( pllInstance *tr, partitionList *pr, nodeptr p, nodeptr q );
74 extern int NNI(pllInstance * tr, nodeptr p, int swap);
75 extern void addTraverseBIG ( pllInstance *tr, partitionList *pr, nodeptr p, nodeptr q, int mintrav, int maxtrav );
76 extern int rearrangeBIG ( pllInstance *tr, partitionList *pr, nodeptr p, int mintrav, int maxtrav );
77 extern void traversalOrder ( nodeptr p, int *count, nodeptr *nodeArray );
78 extern pllBoolean testInsertRestoreBIG ( pllInstance *tr, partitionList *pr, nodeptr p, nodeptr q );
79 extern void restoreTreeFast ( pllInstance *tr, partitionList *pr );
80 
81 extern void initTL ( topolRELL_LIST *rl, pllInstance *tr, int n );
82 extern void freeTL ( topolRELL_LIST *rl);
83 extern void restoreTL ( topolRELL_LIST *rl, pllInstance *tr, int n, int numBranches );
84 extern void resetTL ( topolRELL_LIST *rl );
85 extern void saveTL ( topolRELL_LIST *rl, pllInstance *tr, int index );
86 
87 extern topol  *setupTopol (int maxtips);
88 extern void saveTree (pllInstance *tr, topol *tpl, int numBranches);
89 extern pllBoolean restoreTree (topol *tpl, pllInstance *tr, partitionList *pr);
90 
91 
92 
93 
94 extern int  saveBestTree (bestlist *bt, pllInstance *tr, int numBranches);
95 extern int  recallBestTree (bestlist *bt, int rank, pllInstance *tr, partitionList *pr);
96 extern int initBestTree ( bestlist *bt, int newkeep, int numsp );
97 extern void resetBestTree ( bestlist *bt );
98 extern pllBoolean freeBestTree ( bestlist *bt );
99 
100 
101 /* extern int treeReadLen (FILE *fp, pllInstance *tr, pllBoolean readBranches, pllBoolean readNodeLabels, pllBoolean topologyOnly);
102 extern void getStartingTree (pllInstance *tr);
103 extern void treeReadTopologyString(char *treeString, pllInstance *tr);
104 extern double treeLength (pllInstance *tr, int model);*/
105 extern double evaluatePartialGeneric (pllInstance *, partitionList *pr, int i, double ki, int _model);
106 extern void newviewAncestralIterative(pllInstance *tr, partitionList *pr);
107 extern void printAncestralState(nodeptr p, pllBoolean printStates, pllBoolean printProbs, pllInstance *tr, partitionList *pr);
108 extern void makenewzGeneric(pllInstance *tr, partitionList * pr, nodeptr p, nodeptr q, double *z0, int maxiter, double *result, pllBoolean mask);
109 extern void makenewzGenericDistance(pllInstance *tr, int maxiter, double *z0, double *result, int taxon1, int taxon2);
110 extern double evaluatePartitionGeneric (pllInstance *tr, nodeptr p, int model);
111 extern void newviewPartitionGeneric (pllInstance *tr, nodeptr p, int model);
112 extern double evaluateGenericVector (pllInstance *tr, nodeptr p);
113 extern void categorizeGeneric (pllInstance *tr, nodeptr p);
114 extern double makenewzPartitionGeneric(pllInstance *tr, nodeptr p, nodeptr q, double z0, int maxiter, int model);
115 extern pllBoolean isTip(int number, int maxTips);
116 
117 /* recom functions */
118 extern void computeTraversal(pllInstance *tr, nodeptr p, pllBoolean partialTraversal, int numBranches);
119 extern void allocRecompVectorsInfo(pllInstance *tr);
120 extern void allocTraversalCounter(pllInstance *tr);
121 extern pllBoolean getxVector(recompVectors *rvec, int nodenum, int *slot, int mxtips);
122 extern pllBoolean needsRecomp(pllBoolean recompute, recompVectors *rvec, nodeptr p, int mxtips);
123 extern void unpinNode(recompVectors *v, int nodenum, int mxtips);
124 extern void protectNode(recompVectors *rvec, int nodenum, int mxtips);
125 
126 /* Handling branch lengths*/
127 extern void computeTraversalInfoStlen(nodeptr p, int maxTips, recompVectors *rvec, int *count);
128 extern void computeFullTraversalInfoStlen(nodeptr p, int maxTips, recompVectors *rvec);
129 extern void printTraversalInfo(pllInstance *tr);
130 extern void countTraversal(pllInstance *tr);
131 extern void storeExecuteMaskInTraversalDescriptor(pllInstance *tr, partitionList *pr);
132 extern void storeValuesInTraversalDescriptor(pllInstance *tr, partitionList *pr, double *value);
133 extern void makenewzIterative(pllInstance *, partitionList *pr);
134 extern void execCore(pllInstance *, partitionList *pr, volatile double *dlnLdlz, volatile double *d2lnLdlz2);
135 extern void makePermutation(int *perm, int n, pllInstance *tr);
136 extern nodeptr findAnyTip(nodeptr p, int numsp);
137 extern void putWAG(double *ext_initialRates);
138 extern  unsigned int **initBitVector(int mxtips, unsigned int *vectorLength);
139 //extern hashtable *initHashTable(unsigned int n);
140 extern void cleanupHashTable(pllHashTable * h, int state);
141 extern double convergenceCriterion(pllHashTable *h, int mxtips);
142 extern void freeBitVectors(unsigned int **v, int n);
143 //extern void freeHashTable(hashtable *h);
144 //extern stringHashtable *initStringHashTable(hashNumberType n);
145 //extern void addword(char *s, stringHashtable *h, int nodeNumber);
146 extern void initRateMatrix(pllInstance *tr, partitionList *pr);
147 extern void bitVectorInitravSpecial(unsigned int **bitVectors, nodeptr p, int numsp, unsigned int vectorLength, pllHashTable *h, int treeNumber, int function, branchInfo *bInf,
148                                     int *countBranches, int treeVectorLength, pllBoolean traverseOnly, pllBoolean computeWRF, int processID);
149 extern  unsigned int bitcount_32_bit(unsigned int i);
150 extern __inline unsigned int bitcount_64_bit(uint64_t i);
151 extern void perSiteLogLikelihoods(pllInstance *tr, partitionList *pr, double *logLikelihoods);
152 extern void updatePerSiteRates(pllInstance *tr, partitionList *pr, pllBoolean scaleRates);
153 extern void restart(pllInstance *tr, partitionList *pr);
154 
155 //extern const unsigned int mask32[32];
156 
157 /** @brief Check whether the position \a pos in bitvector \a x is a gap
158 
159     @param x
160       A bitvector represented by unsigned integers
161 
162     @param pos
163       Position to check in \a x if it is set (i.e. it is a gap)
164 
165     @return
166       Returns the value of the bit vector (\b 1 if set, \b 0 if not)
167 */
168 //#ifndef __clang__
169 //inline
170 //#endif
171 pllBoolean isGap(unsigned int *x, int pos);
172 
173 /** @brief Check whether the position \a pos in bitvector \a x is \b NOT a gap
174 
175     @param x
176       A bitvector represented by unsigned integers
177 
178     @param pos
179       Position to check in \a x if it is \b NOT set (i.e. it is \b NOT a gap)
180 
181     @return
182       Returns the value of the bit vector (\b 1 if set, \b 0 if not)
183 */
184 //#ifndef __clang__
185 //inline
186 //#endif
187 pllBoolean noGap(unsigned int *x, int pos);
188 
189 //#ifndef __clang__
190 //__inline
191 //#endif
192 //pllBoolean isGap(unsigned int *x, int pos);
193 
194 //#ifndef __clang__
195 //__inline
196 //#endif
197 //pllBoolean noGap(unsigned int *x, int pos);
198 
199 /* from utils.h */
200 linkageList* initLinkageList(int *linkList, partitionList *pr);
201 
202 #if (defined(_FINE_GRAIN_MPI) || defined(_USE_PTHREADS) )
203 /* work tags for parallel regions */
204 
205 #define PLL_THREAD_NEWVIEW                  0
206 #define PLL_THREAD_EVALUATE                 1
207 #define PLL_THREAD_MAKENEWZ                 2
208 #define PLL_THREAD_MAKENEWZ_FIRST           3
209 #define PLL_THREAD_RATE_CATS                4
210 #define PLL_THREAD_COPY_RATE_CATS           5
211 #define PLL_THREAD_COPY_INIT_MODEL          6
212 #define PLL_THREAD_INIT_PARTITION           7
213 #define PLL_THREAD_OPT_ALPHA                8
214 #define PLL_THREAD_OPT_RATE                 9
215 #define PLL_THREAD_OPT_LG4X_RATE            10
216 #define PLL_THREAD_COPY_ALPHA               11
217 #define PLL_THREAD_COPY_RATES               12
218 #define PLL_THREAD_COPY_LG4X_RATES          13
219 #define PLL_THREAD_PER_SITE_LIKELIHOODS     14
220 #define PLL_THREAD_NEWVIEW_ANCESTRAL        15
221 #define PLL_THREAD_GATHER_ANCESTRAL         16
222 #define PLL_THREAD_EXIT_GRACEFULLY          17
223 #define PLL_THREAD_EVALUATE_PER_SITE_LIKES  18
224 
225 
226 typedef struct
227 {
228   pllInstance *tr;
229 
230   partitionList *pr;
231   int threadNumber;
232 }
233   threadData;
234 extern void optRateCatPthreads(pllInstance *tr, partitionList *pr, double lower_spacing, double upper_spacing, double *lhs, int n, int tid);
235 extern void pllMasterBarrier(pllInstance *, partitionList *, int);
236 #endif
237 
238 
239 #ifdef __AVX
240 
241 extern void newviewGTRGAMMAPROT_AVX_LG4(int tipCase,
242                                         double *x1, double *x2, double *x3, double *extEV[4], double *tipVector[4],
243                                         int *ex3, unsigned char *tipX1, unsigned char *tipX2, int n,
244                                         double *left, double *right, int *wgt, int *scalerIncrement, const pllBoolean useFastScaling);
245 
246 
247 extern void newviewGTRCAT_AVX_GAPPED_SAVE(int tipCase,  double *EV,  int *cptr,
248                                    double *x1_start, double *x2_start,  double *x3_start, double *tipVector,
249                                    int *ex3, unsigned char *tipX1, unsigned char *tipX2,
250                                    int n,  double *left, double *right, int *wgt, int *scalerIncrement, const pllBoolean useFastScaling,
251                                    unsigned int *x1_gap, unsigned int *x2_gap, unsigned int *x3_gap,
252                                    double *x1_gapColumn, double *x2_gapColumn, double *x3_gapColumn, const int maxCats);
253 
254 extern void newviewGTRCATPROT_AVX_GAPPED_SAVE(int tipCase, double *extEV,
255                                        int *cptr,
256                                        double *x1, double *x2, double *x3, double *tipVector,
257                                        int *ex3, unsigned char *tipX1, unsigned char *tipX2,
258                                        int n, double *left, double *right, int *wgt, int *scalerIncrement, const pllBoolean useFastScaling,
259                                        unsigned int *x1_gap, unsigned int *x2_gap, unsigned int *x3_gap,
260                                        double *x1_gapColumn, double *x2_gapColumn, double *x3_gapColumn, const int maxCats);
261 
262 extern void  newviewGTRGAMMA_AVX_GAPPED_SAVE(int tipCase,
263                                       double *x1_start, double *x2_start, double *x3_start,
264                                       double *extEV, double *tipVector,
265                                       int *ex3, unsigned char *tipX1, unsigned char *tipX2,
266                                       const int n, double *left, double *right, int *wgt, int *scalerIncrement, const pllBoolean useFastScaling,
267                                       unsigned int *x1_gap, unsigned int *x2_gap, unsigned int *x3_gap,
268                                       double *x1_gapColumn, double *x2_gapColumn, double *x3_gapColumn
269                                       );
270 
271 extern void newviewGTRGAMMAPROT_AVX_GAPPED_SAVE(int tipCase,
272                                          double *x1_start, double *x2_start, double *x3_start, double *extEV, double *tipVector,
273                                          int *ex3, unsigned char *tipX1, unsigned char *tipX2, int n,
274                                          double *left, double *right, int *wgt, int *scalerIncrement, const pllBoolean useFastScaling,
275                                          unsigned int *x1_gap, unsigned int *x2_gap, unsigned int *x3_gap,
276                                          double *x1_gapColumn, double *x2_gapColumn, double *x3_gapColumn);
277 
278 extern void newviewGTRCAT_AVX(int tipCase,  double *EV,  int *cptr,
279     double *x1_start, double *x2_start,  double *x3_start, double *tipVector,
280     int *ex3, unsigned char *tipX1, unsigned char *tipX2,
281     int n,  double *left, double *right, int *wgt, int *scalerIncrement, const pllBoolean useFastScaling);
282 
283 
284 extern void newviewGenericCATPROT_AVX(int tipCase, double *extEV,
285     int *cptr,
286     double *x1, double *x2, double *x3, double *tipVector,
287     int *ex3, unsigned char *tipX1, unsigned char *tipX2,
288     int n, double *left, double *right, int *wgt, int *scalerIncrement, const pllBoolean useFastScaling);
289 
290 
291 extern void newviewGTRGAMMA_AVX(int tipCase,
292     double *x1_start, double *x2_start, double *x3_start,
293     double *EV, double *tipVector,
294     int *ex3, unsigned char *tipX1, unsigned char *tipX2,
295     const int n, double *left, double *right, int *wgt, int *scalerIncrement, const pllBoolean useFastScaling);
296 
297 extern void newviewGTRGAMMAPROT_AVX(int tipCase,
298                              double *x1, double *x2, double *x3, double *extEV, double *tipVector,
299                              int *ex3, unsigned char *tipX1, unsigned char *tipX2, int n,
300                              double *left, double *right, int *wgt, int *scalerIncrement, const pllBoolean useFastScaling);
301 
302 extern void newviewGTRCATPROT_AVX(int tipCase, double *extEV,
303                            int *cptr,
304                            double *x1, double *x2, double *x3, double *tipVector,
305                            int *ex3, unsigned char *tipX1, unsigned char *tipX2,
306                            int n, double *left, double *right, int *wgt, int *scalerIncrement, const pllBoolean useFastScaling);
307 
308 #endif
309 
310 extern int virtual_width( int n );
311 extern void computeAllAncestralVectors(nodeptr p, pllInstance *tr, partitionList *pr);
312 
313 #endif /* PLLINTERNAL_H_ */
314