| 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 |
#include <p7_config.h> |
| 46 |
#include <math.h> |
| 47 |
#include "hmmer.h" |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
#define p7_LOGSUM_SCALE 1000.f |
| 59 |
#define p7_LOGSUM_TBL 16000 |
| 60 |
|
| 61 |
static float flogsum_lookup[p7_LOGSUM_TBL]; |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
int |
| 80 |
p7_FLogsumInit(void) |
| 81 |
{ |
| 82 |
static int firsttime = TRUE; |
| 83 |
if (!firsttime) return eslOK; |
| 84 |
firsttime = FALSE; |
| 85 |
|
| 86 |
int i; |
| 87 |
for (i = 0; i < p7_LOGSUM_TBL; i++) |
| 88 |
flogsum_lookup[i] = log(1. + exp((double) -i / p7_LOGSUM_SCALE)); |
| 89 |
return eslOK; |
| 90 |
} |
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
float |
| 105 |
p7_FLogsum(float a, float b) |
| 106 |
{ |
| 107 |
const float max = ESL_MAX(a, b); |
| 108 |
const float min = ESL_MIN(a, b); |
| 109 |
|
| 110 |
|
| 111 |
return (min == -eslINFINITY || (max-min) >= 15.7f) ? max : max + flogsum_lookup[(int)((max-min)*p7_LOGSUM_SCALE)]; |
| 112 |
} |
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
float |
| 132 |
p7_FLogsumError(float a, float b) |
| 133 |
{ |
| 134 |
float approx = p7_FLogsum(a,b); |
| 135 |
float exact = log(exp(a) + exp(b)); |
| 136 |
return (exp(approx) - exp(exact)); |
| 137 |
} |
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
#ifdef p7LOGSUM_BENCHMARK |
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
#include <p7_config.h> |
| 160 |
|
| 161 |
#include <math.h> |
| 162 |
|
| 163 |
#include "easel.h" |
| 164 |
#include "esl_getopts.h" |
| 165 |
#include "esl_random.h" |
| 166 |
#include "esl_stopwatch.h" |
| 167 |
|
| 168 |
#include "hmmer.h" |
| 169 |
|
| 170 |
static ESL_OPTIONS options[] = { |
| 171 |
|
| 172 |
{ "-h", eslARG_NONE, NULL, NULL, NULL, NULL, NULL, NULL, "show brief help on version and usage", 0 }, |
| 173 |
{ "-n", eslARG_NONE, NULL, NULL, NULL, NULL, NULL, NULL, "naive time: A + log(1+exp(-(A-B)))", 0 }, |
| 174 |
{ "-r", eslARG_NONE, NULL, NULL, NULL, NULL, NULL, NULL, "really naive time: log(exp(A)+exp(B))", 0 }, |
| 175 |
{ "-s", eslARG_INT, "42", NULL, NULL, NULL, NULL, NULL, "set random number seed to <n>", 0 }, |
| 176 |
{ "-v", eslARG_NONE, NULL, NULL, NULL, NULL, NULL, NULL, "be verbose: show individual results", 0 }, |
| 177 |
{ "-N", eslARG_INT,"100000000",NULL,"n>0", NULL, NULL, NULL, "number of trials", 0 }, |
| 178 |
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, |
| 179 |
}; |
| 180 |
static char usage[] = "[-options]"; |
| 181 |
static char banner[] = "benchmark driver for logsum functions()"; |
| 182 |
|
| 183 |
static float |
| 184 |
naive1(float s1, float s2) |
| 185 |
{ |
| 186 |
return log(exp(s1) + exp(s2)); |
| 187 |
} |
| 188 |
|
| 189 |
static float |
| 190 |
naive2(float s1, float s2) |
| 191 |
{ |
| 192 |
if (s1 > s2) return s1 + log(1 + exp(s2-s1)); |
| 193 |
else return s2 + log(1 + exp(s1-s2)); |
| 194 |
} |
| 195 |
|
| 196 |
int |
| 197 |
main(int argc, char **argv) |
| 198 |
{ |
| 199 |
ESL_GETOPTS *go = p7_CreateDefaultApp(options, 0, argc, argv, banner, usage); |
| 200 |
ESL_RANDOMNESS *r = esl_randomness_CreateFast(esl_opt_GetInteger(go, "-s")); |
| 201 |
ESL_STOPWATCH *w = esl_stopwatch_Create(); |
| 202 |
int N = esl_opt_GetInteger(go, "-N"); |
| 203 |
int i; |
| 204 |
float *A, *B, *C; |
| 205 |
|
| 206 |
p7_FLogsumInit(); |
| 207 |
|
| 208 |
|
| 209 |
A = malloc(sizeof(float) * N); |
| 210 |
B = malloc(sizeof(float) * N); |
| 211 |
C = malloc(sizeof(float) * N); |
| 212 |
for (i = 0; i < N; i++) |
| 213 |
{ |
| 214 |
A[i] = esl_random(r) * 2000. - 1000.; |
| 215 |
B[i] = esl_random(r) * 2000. - 1000.; |
| 216 |
} |
| 217 |
|
| 218 |
|
| 219 |
esl_stopwatch_Start(w); |
| 220 |
|
| 221 |
if (esl_opt_GetBoolean(go, "-n")) |
| 222 |
{ |
| 223 |
for (i = 0; i < N; i++) |
| 224 |
C[i] = naive2(A[i], B[i]); |
| 225 |
} |
| 226 |
else if (esl_opt_GetBoolean(go, "-r")) |
| 227 |
{ |
| 228 |
for (i = 0; i < N; i++) |
| 229 |
C[i] = naive1(A[i], B[i]); |
| 230 |
} |
| 231 |
else |
| 232 |
{ |
| 233 |
for (i = 0; i < N; i++) |
| 234 |
C[i] = p7_FLogsum(A[i], B[i]); |
| 235 |
} |
| 236 |
|
| 237 |
esl_stopwatch_Stop(w); |
| 238 |
esl_stopwatch_Display(stdout, w, "# CPU time: "); |
| 239 |
|
| 240 |
esl_stopwatch_Destroy(w); |
| 241 |
esl_randomness_Destroy(r); |
| 242 |
esl_getopts_Destroy(go); |
| 243 |
return 0; |
| 244 |
} |
| 245 |
#endif |
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
#ifdef p7LOGSUM_TESTDRIVE |
| 253 |
|
| 254 |
#include "easel.h" |
| 255 |
#include "esl_getopts.h" |
| 256 |
#include "esl_random.h" |
| 257 |
|
| 258 |
static void |
| 259 |
utest_FLogsumError(ESL_GETOPTS *go, ESL_RANDOMNESS *r) |
| 260 |
{ |
| 261 |
int N = esl_opt_GetInteger(go, "-N"); |
| 262 |
float maxval = esl_opt_GetReal(go, "-S"); |
| 263 |
int be_verbose = esl_opt_GetBoolean(go, "-v"); |
| 264 |
float maxerr = 0.0; |
| 265 |
float avgerr = 0.0; |
| 266 |
int i; |
| 267 |
float a,b,result,exact,err; |
| 268 |
|
| 269 |
for (i = 0; i < N; i++) |
| 270 |
{ |
| 271 |
a = (esl_random(r) - 0.5) * maxval * 2.; |
| 272 |
b = (esl_random(r) - 0.5) * maxval * 2.; |
| 273 |
|
| 274 |
exact = log(exp(a) + exp(b)); |
| 275 |
result = p7_FLogsum(a,b); |
| 276 |
err = fabs(exact-result) / maxval; |
| 277 |
|
| 278 |
avgerr += err; |
| 279 |
maxerr = ESL_MAX(maxerr, err); |
| 280 |
|
| 281 |
if (be_verbose) |
| 282 |
printf("%8.4f %8.4f %8.4f %8.4f %8.4f\n", a, b, exact, result, err); |
| 283 |
} |
| 284 |
avgerr /= (float) N; |
| 285 |
|
| 286 |
if (be_verbose) { |
| 287 |
printf("average error = %f\n", avgerr); |
| 288 |
printf("max error = %f\n", maxerr); |
| 289 |
} |
| 290 |
|
| 291 |
if (maxerr > 0.0001) esl_fatal("maximum error of %f is too high: logsum unit test fails", maxerr); |
| 292 |
if (avgerr > 0.0001) esl_fatal("average error of %f is too high: logsum unit test fails", avgerr); |
| 293 |
} |
| 294 |
|
| 295 |
static void |
| 296 |
utest_FLogsumSpecials(void) |
| 297 |
{ |
| 298 |
char *msg = "logsum specials unit test failed"; |
| 299 |
|
| 300 |
if (p7_FLogsum(0.0, -eslINFINITY) != 0.0) esl_fatal(msg); |
| 301 |
if (p7_FLogsum(-eslINFINITY, 0.0) != 0.0) esl_fatal(msg); |
| 302 |
if (p7_FLogsum(-eslINFINITY, -eslINFINITY) != -eslINFINITY) esl_fatal(msg); |
| 303 |
} |
| 304 |
#endif |
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
#ifdef p7LOGSUM_TESTDRIVE |
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
#include <p7_config.h> |
| 316 |
|
| 317 |
#include <stdio.h> |
| 318 |
#include <math.h> |
| 319 |
|
| 320 |
#include "easel.h" |
| 321 |
#include "esl_getopts.h" |
| 322 |
#include "esl_random.h" |
| 323 |
|
| 324 |
#include "hmmer.h" |
| 325 |
|
| 326 |
static ESL_OPTIONS options[] = { |
| 327 |
|
| 328 |
{"-h", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "show help and usage", 0}, |
| 329 |
{"-N", eslARG_INT, "1000", NULL, "n>0",NULL, NULL, NULL, "number of samples", 0}, |
| 330 |
{"-S", eslARG_REAL, "20.0", NULL, "x>0",NULL, NULL, NULL, "maximum operand value", 0}, |
| 331 |
{"-s", eslARG_INT, "42", NULL,"n>=0",NULL, NULL, NULL, "random number seed", 0}, |
| 332 |
{"-v", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "show verbose output", 0}, |
| 333 |
{ 0,0,0,0,0,0,0,0,0,0}, |
| 334 |
}; |
| 335 |
static char usage[] = "[-options]"; |
| 336 |
static char banner[] = "test driver for logsum.c"; |
| 337 |
|
| 338 |
int |
| 339 |
main(int argc, char **argv) |
| 340 |
{ |
| 341 |
ESL_GETOPTS *go = p7_CreateDefaultApp(options, 0, argc, argv, banner, usage); |
| 342 |
ESL_RANDOMNESS *r = esl_randomness_CreateFast(esl_opt_GetInteger(go, "-s")); |
| 343 |
|
| 344 |
p7_FLogsumInit(); |
| 345 |
|
| 346 |
utest_FLogsumError(go, r); |
| 347 |
utest_FLogsumSpecials(); |
| 348 |
|
| 349 |
esl_randomness_Destroy(r); |
| 350 |
esl_getopts_Destroy(go); |
| 351 |
return eslOK; |
| 352 |
} |
| 353 |
#endif |
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
#ifdef p7LOGSUM_EXAMPLE |
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
#include <p7_config.h> |
| 365 |
#include "easel.h" |
| 366 |
#include "hmmer.h" |
| 367 |
|
| 368 |
int |
| 369 |
main(int argc, char **argv) |
| 370 |
{ |
| 371 |
float a = atof(argv[1]); |
| 372 |
float b = atof(argv[2]); |
| 373 |
float result; |
| 374 |
|
| 375 |
p7_FLogsumInit(); |
| 376 |
result = p7_FLogsum(a, b); |
| 377 |
printf("p7_FLogsum(%f,%f) = %f\n", a, b, result); |
| 378 |
|
| 379 |
result = log(exp(a) + exp(b)); |
| 380 |
printf("log(e^%f + e^%f) = %f\n", a, b, result); |
| 381 |
|
| 382 |
printf("Absolute error in probability: %f\n", p7_FLogsumError(a,b)); |
| 383 |
return eslOK; |
| 384 |
} |
| 385 |
#endif |
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
|