-
Notifications
You must be signed in to change notification settings - Fork 0
/
felsenstein_faster.h
260 lines (210 loc) · 6.43 KB
/
felsenstein_faster.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
#ifndef FELSENSTEIN_FASTER_H
#define FELSENSTEIN_FASTER_H
#include <stdint.h>
#include <math.h>
#ifdef DEBUG_PRINT
#include <stdlib.h>
#include <stdio.h>
#endif
#ifdef SINGLE_PRECISION
#define c_f0 0.0f
#define log0 -50.0f
typedef float c_float_t;
#else
#define c_f0 0.0
#define log0 -2000
typedef double c_float_t;
#endif
#define N_COL 2
#ifdef DEBUG_PRINT
#include "debug_tools.h"
#endif
typedef struct SignedLogExp {
int8_t sign;
c_float_t result;
} SignedLogExp;
static inline void initialize_array_int8(int8_t* arr, int8_t value, int length) {
for(int i = 0; i < length; i++) {
arr[i] = value;
}
}
static inline void initialize_array(c_float_t* arr, c_float_t value, int length) {
for(int i = 0; i < length; i++) {
arr[i] = value;
}
}
static inline c_float_t logsumexp2(c_float_t log_x, c_float_t log_y) {
c_float_t logsumexp;
if(log_x > log_y) {
c_float_t exp_part = 1 + pow(2, log_y - log_x);
logsumexp = log_x + ((exp_part != 0) ? log2(exp_part) : log0);
} else {
c_float_t exp_part = pow(2, log_x - log_y) + 1;
logsumexp = log_y + ((exp_part != 0) ? log2(exp_part) : log0);
}
return logsumexp;
}
static inline SignedLogExp signed_logsumexp2(c_float_t log_x, int8_t sign_x, c_float_t log_y, int8_t sign_y) {
c_float_t logsumexp;
int8_t sign;
if(log_x > log_y) {
c_float_t exp_part = sign_x + sign_y * pow(2, log_y - log_x);
sign = exp_part >= 0 ? 1 : -1;
logsumexp = log_x + ((exp_part != 0) ? log2(sign*exp_part) : log0);
} else {
c_float_t exp_part = sign_x * pow(2, log_x - log_y) + sign_y;
sign = exp_part >= 0 ? 1 : -1;
logsumexp = log_y + ((exp_part != 0) ? log2(sign*exp_part) : log0);
}
SignedLogExp result = {sign, logsumexp};
return result;
}
static inline c_float_t logsumexp3(c_float_t log_x, c_float_t log_y, c_float_t log_z) {
c_float_t logsumexp;
if(log_x > log_y && log_x > log_z) {
c_float_t exp_part = 1 + pow(2, log_y - log_x) + pow(2, log_z - log_x);
logsumexp = log_x + ((exp_part != 0) ? log2(exp_part) : log0);
} else if(log_y > log_x && log_y > log_z) {
c_float_t exp_part = pow(2, log_x - log_y) + 1 + pow(2, log_z - log_y);
logsumexp = log_y + ((exp_part != 0) ? log2(exp_part) : log0);
} else {
c_float_t exp_part = pow(2, log_x - log_z) + pow(2, log_y - log_z) + 1;
logsumexp = log_z + ((exp_part != 0) ? log2(exp_part) : log0);
}
return logsumexp;
}
static inline SignedLogExp signed_logsumexp3(c_float_t log_x, int8_t sign_x, c_float_t log_y, int8_t sign_y, c_float_t log_z, int8_t sign_z) {
c_float_t logsumexp;
int8_t sign;
if(log_x > log_y && log_x > log_z) {
c_float_t exp_part = sign_x + sign_y*pow(2, log_y - log_x) + sign_z*pow(2, log_z - log_x);
sign = exp_part >= 0 ? 1 : -1;
logsumexp = log_x + ((exp_part != 0) ? log2(sign*exp_part) : log0);
} else if(log_y > log_x && log_y > log_z) {
c_float_t exp_part = sign_x*pow(2, log_x - log_y) + sign_y + sign_z*pow(2, log_z - log_y);
sign = exp_part >= 0 ? 1 : -1;
logsumexp = log_y + ((exp_part != 0) ? log2(sign*exp_part) : log0);
} else {
c_float_t exp_part = sign_x*pow(2, log_x - log_z) + sign_y*pow(2, log_y - log_z) + sign_z;
sign = exp_part >= 0 ? 1 : -1;
logsumexp = log_z + ((exp_part != 0) ? log2(sign*exp_part) : log0);
}
SignedLogExp result = {sign, logsumexp};
return result;
}
static inline c_float_t logsumexpn(c_float_t* log_vals, int n) {
if( n < 2) {
return log_vals[0];
}
c_float_t max = log_vals[0];
for(int i = 1; i < n; i++) {
max = (log_vals[i] > max) ? log_vals[i] : max;
}
c_float_t exp_sum = 0;
for(int i = 0; i < n; i++) {
exp_sum += pow(2, log_vals[i] - max);
}
return max + ((exp_sum != 0) ? log2(exp_sum) : log0);
}
static inline SignedLogExp signed_logsumexp_n(c_float_t* log_vals, int8_t* signs, int n) {
c_float_t max = log_vals[0];
for(int i = 1; i < n; i++) {
max = (log_vals[i] > max) ? log_vals[i] : max;
}
c_float_t exp_sum = 0;
for(int i = 0; i < n; i++) {
exp_sum += signs[i] * pow(2, log_vals[i] - max);
}
int8_t sign = exp_sum >= 0 ? 1 : -1;
SignedLogExp result = {sign, max + ((exp_sum != 0) ? log2(exp_sum * sign) : log0)};
return result;
}
typedef struct NodePrecomputation {
// dim: A*A [a, b]
c_float_t* Ln_ab;
// dim: 2*A*A*A [i, c, a, b]
c_float_t* dv_Ln_ab;
int8_t* dv_Ln_ab_signs;
// dim: A*A*A*A [c, d, a, b]
c_float_t* dw_Ln_ab;
int8_t* dw_Ln_ab_signs;
} NodePrecomputation;
typedef struct Node {
struct Node* left;
struct Node* right;
int seq_id;
c_float_t phi_left;
c_float_t log_1mp_left;
c_float_t log_p_left;
c_float_t phi_right;
c_float_t log_1mp_right;
c_float_t log_p_right;
NodePrecomputation* data;
} Node;
typedef struct NodeBuffer {
// dim: scalar
c_float_t Ln;
// dim: A [a]
c_float_t* Ln_ia;
// dim: A [b]
c_float_t* Ln_jb;
// dim: 2*A [i, c]
c_float_t* dv_Ln;
int8_t* dv_Ln_signs;
// dim: 2*A*A [i, c, a]
c_float_t* dv_Ln_ia;
int8_t* dv_Ln_ia_signs;
// dim: 2*A*A [i, c, b]
c_float_t* dv_Ln_jb;
int8_t* dv_Ln_jb_signs;
// dim: A*A [c, d]
c_float_t* dw_Ln;
int8_t* dw_Ln_signs;
// dim: A*A*A [c, d, a]
c_float_t* dw_Ln_ia;
int8_t* dw_Ln_ia_signs;
// dim: A*A*A [c, d, b]
c_float_t* dw_Ln_jb;
int8_t* dw_Ln_jb_signs;
} NodeBuffer;
typedef struct Buffer {
NodeBuffer* left;
NodeBuffer* right;
} Buffer;
typedef struct Constants {
int L;
c_float_t* p_ab;
c_float_t* dw_p_ab;
int8_t* dw_p_ab_signs;
c_float_t* dv_p_ab;
int8_t* dv_p_ab_signs;
c_float_t* p_ij_cond;
c_float_t* dw_p_ij_cond;
int8_t* dw_p_ij_cond_signs;
c_float_t* dv_p_ij_cond;
int8_t* dv_p_ij_cond_signs;
c_float_t* p_ji_cond;
c_float_t* dw_p_ji_cond;
int8_t* dw_p_ji_cond_signs;
c_float_t* dv_p_ji_cond;
int8_t* dv_p_ji_cond_signs;
Node* phylo_tree;
uint8_t* msa;
int A_i;
int A_j;
int A_i_p_A_j;
int AA_ij;
int i;
int j;
} Constants;
void initialize_node(Node* node, Constants* consts);
void deinitialize_node(Node* node);
void initialize_leaf(Node* leaf, Constants* consts);
void initialize_constants(Constants* consts);
void precalculate_constants(Constants* consts, c_float_t* v, c_float_t* w);
void deinitialize_constants(Constants* consts);
void initialize_buffer(NodeBuffer*, Constants* consts);
void precompute_buffer(NodeBuffer* buffer, NodePrecomputation* data, Constants* consts);
void deinitialize_buffer(NodeBuffer*);
c_float_t calculate_fx_grad(c_float_t* x, c_float_t* grad, Constants* consts, Buffer* buf);
#endif //FELSENSTEIN_FASTER_H