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 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 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 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104
|
/*
* phylokernelnonrev.h
* Kernel based on vectorizing over alignment patterns for non-reversible models
*
* Created on: Nov 4, 2016
* Author: minh
*/
#if !defined(PHYLOKERNELNONREV_H_) || !defined(PHYLOKERNELNONREV_STATE_H_)
#ifdef KERNEL_FIX_STATES
#define PHYLOKERNELNONREV_STATE_H_
#else
#define PHYLOKERNELNONREV_H_
#endif
#include "phylotree.h"
#ifdef _OPENMP
#include <omp.h>
#endif
//#include <thread>
using namespace std;
#ifdef KERNEL_FIX_STATES
template <class VectorClass, const int nstates, const bool FMA>
void PhyloTree::computeNonrevPartialLikelihoodSIMD(TraversalInfo &info, size_t ptn_lower, size_t ptn_upper, int thread_id) {
#else
template <class VectorClass, const bool FMA>
void PhyloTree::computeNonrevPartialLikelihoodGenericSIMD(TraversalInfo &info, size_t ptn_lower, size_t ptn_upper, int thread_id) {
#endif
PhyloNeighbor *dad_branch = info.dad_branch;
PhyloNode *dad = info.dad;
ASSERT(dad);
PhyloNode *node = (PhyloNode*)(dad_branch->node);
// assert(dad_branch->direction != UNDEFINED_DIRECTION);
#ifndef KERNEL_FIX_STATES
size_t nstates = aln->num_states;
#endif
if (node->isLeaf()) {
return;
}
ASSERT(node->degree() >= 3);
size_t ptn, c;
size_t orig_nptn = aln->size();
size_t max_orig_nptn = ((orig_nptn+VectorClass::size()-1)/VectorClass::size())*VectorClass::size();
size_t nptn = max_orig_nptn+model_factory->unobserved_ptns.size();
size_t ncat = site_rate->getNRate();
size_t ncat_mix = (model_factory->fused_mix_rate) ? ncat : ncat*model->getNMixtures();
size_t i, x;
size_t block = nstates * ncat_mix;
// internal node
PhyloNeighbor *left = NULL, *right = NULL; // left & right are two neighbors leading to 2 subtrees
FOR_NEIGHBOR_IT(node, dad, it) {
if (!left) left = (PhyloNeighbor*)(*it); else right = (PhyloNeighbor*)(*it);
}
// precomputed buffer to save times
double *buffer_partial_lh_ptr = buffer_partial_lh + (getBufferPartialLhSize() - 2*block*VectorClass::size()*num_threads);
double *echildren = info.echildren;
double *partial_lh_leaves = info.partial_lh_leaves;
double *eleft = echildren, *eright = echildren + block*nstates;
if ((!left->node->isLeaf() && right->node->isLeaf())) {
PhyloNeighbor *tmp = left;
left = right;
right = tmp;
double *etmp = eleft;
eleft = eright;
eright = etmp;
}
if (node->degree() > 3) {
/*--------------------- multifurcating node ------------------*/
double *vec_tip = buffer_partial_lh_ptr + (block*2)*VectorClass::size()*thread_id;
VectorClass *vtip = (VectorClass*)vec_tip;
// now for-loop computing partial_lh over all site-patterns
for (ptn = ptn_lower; ptn < ptn_upper; ptn+=VectorClass::size()) {
VectorClass *partial_lh_all = (VectorClass*)(dad_branch->partial_lh + ptn*block);
for (i = 0; i < block; i++)
partial_lh_all[i] = 1.0;
memset(&dad_branch->scale_num[ptn], 0, sizeof(UBYTE)*VectorClass::size());
double *partial_lh_leaf = partial_lh_leaves;
double *echild = echildren;
FOR_NEIGHBOR_IT(node, dad, it) {
PhyloNeighbor *child = (PhyloNeighbor*)*it;
if (child->node->isLeaf()) {
// external node
// load data for tip
for (x = 0; x < VectorClass::size(); x++) {
double *tip_child;
if (isRootLeaf(child->node))
tip_child = partial_lh_leaf;
else if (ptn+x < orig_nptn)
tip_child = partial_lh_leaf + block * (aln->at(ptn+x))[child->node->id];
else if (ptn+x < max_orig_nptn)
tip_child = partial_lh_leaf + block * aln->STATE_UNKNOWN;
else if (ptn+x < nptn)
tip_child = partial_lh_leaf + block * model_factory->unobserved_ptns[ptn+x-max_orig_nptn];
else
tip_child = partial_lh_leaf + block * aln->STATE_UNKNOWN;
double *this_vec_tip = vec_tip+x;
for (i = 0; i < block; i++) {
*this_vec_tip = tip_child[i];
this_vec_tip += VectorClass::size();
}
}
for (c = 0; c < block; c++) {
// compute real partial likelihood vector
partial_lh_all[c] *= vtip[c];
}
partial_lh_leaf += (aln->STATE_UNKNOWN+1)*block;
} else {
// internal node
VectorClass *partial_lh = partial_lh_all;
VectorClass *partial_lh_child = (VectorClass*)(child->partial_lh + ptn*block);
for (i = 0; i < VectorClass::size(); i++)
dad_branch->scale_num[ptn+i] += child->scale_num[ptn+i];
double *echild_ptr = echild;
for (c = 0; c < ncat_mix; c++) {
// compute real partial likelihood vector
for (x = 0; x < nstates; x++) {
VectorClass vchild;
// for (i = 0; i < nstates; i++) {
// vchild += echild_ptr[i] * partial_lh_child[i];
// }
#ifdef KERNEL_FIX_STATES
dotProductVec<VectorClass, double, nstates, FMA>(echild_ptr, partial_lh_child, vchild);
#else
dotProductVec<VectorClass, double, FMA>(echild_ptr, partial_lh_child, vchild, nstates);
#endif
echild_ptr += nstates;
partial_lh[x] *= vchild;
}
partial_lh += nstates;
partial_lh_child += nstates;
}
} // if
echild += block*nstates;
} // FOR_NEIGHBOR
VectorClass lh_max = partial_lh_all[0];
for (i = 1; i < block; i++)
lh_max = max(lh_max, partial_lh_all[i]);
// check if one should scale partial likelihoods
auto underflown = (lh_max < SCALING_THRESHOLD);
if (horizontal_or(underflown)) {
// now do the likelihood scaling
for (x = 0; x < VectorClass::size(); x++)
if (underflown[x]) {
double *partial_lh = dad_branch->partial_lh + (ptn*block + x);
// now do the likelihood scaling
for (i = 0; i < block; i++) {
partial_lh[i*VectorClass::size()] = ldexp(partial_lh[i*VectorClass::size()], SCALING_THRESHOLD_EXP);
}
dad_branch->scale_num[ptn+x] += 1;
}
}
} // for ptn
// end multifurcating treatment
} else if (left->node->isLeaf() && right->node->isLeaf()) {
/*--------------------- TIP-TIP (cherry) case ------------------*/
double *partial_lh_left = partial_lh_leaves;
double *partial_lh_right = partial_lh_leaves + (aln->STATE_UNKNOWN+1)*block;
double *vec_left = buffer_partial_lh_ptr + (block*2)*VectorClass::size()*thread_id;
double *vec_right = &vec_left[block*VectorClass::size()];
if (isRootLeaf(right->node)) {
// swap so that left node is the root
PhyloNeighbor *tmp = left;
left = right;
right = tmp;
double *etmp = eleft;
eleft = eright;
eright = etmp;
etmp = partial_lh_left;
partial_lh_left = partial_lh_right;
partial_lh_right = etmp;
}
// scale number must be ZERO
memset(dad_branch->scale_num + ptn_lower, 0, (ptn_upper-ptn_lower) * sizeof(UBYTE));
if (isRootLeaf(left->node)) {
for (ptn = ptn_lower; ptn < ptn_upper; ptn+=VectorClass::size()) {
double *vright = dad_branch->partial_lh + ptn*block;
VectorClass *partial_lh = (VectorClass*)vright;
// load data for tip
for (x = 0; x < VectorClass::size(); x++) {
double *tip_right;
if (ptn+x < orig_nptn)
tip_right = partial_lh_right + block * (aln->at(ptn+x))[right->node->id];
else if (ptn+x < max_orig_nptn)
tip_right = partial_lh_right + block * aln->STATE_UNKNOWN;
else if (ptn+x < nptn)
tip_right = partial_lh_right + block * model_factory->unobserved_ptns[ptn+x-max_orig_nptn];
else
tip_right = partial_lh_right + block * aln->STATE_UNKNOWN;
double *this_vec_right = vright+x;
for (i = 0; i < block; i++) {
*this_vec_right = tip_right[i];
this_vec_right += VectorClass::size();
}
}
for (i = 0; i < block; i++)
partial_lh[i] *= partial_lh_left[i];
}
} else
for (ptn = ptn_lower; ptn < ptn_upper; ptn+=VectorClass::size()) {
VectorClass *partial_lh = (VectorClass*)(dad_branch->partial_lh + ptn*block);
VectorClass *vleft = (VectorClass*)vec_left;
VectorClass *vright = (VectorClass*)vec_right;
// load data for tip
for (x = 0; x < VectorClass::size(); x++) {
double *tip_left, *tip_right;
if (ptn+x < orig_nptn) {
tip_left = partial_lh_left + block * (aln->at(ptn+x))[left->node->id];
tip_right = partial_lh_right + block * (aln->at(ptn+x))[right->node->id];
} else if (ptn+x < max_orig_nptn) {
tip_left = partial_lh_left + block * aln->STATE_UNKNOWN;
tip_right = partial_lh_right + block * aln->STATE_UNKNOWN;
} else if (ptn+x < nptn) {
tip_left = partial_lh_left + block * model_factory->unobserved_ptns[ptn+x-max_orig_nptn];
tip_right = partial_lh_right + block * model_factory->unobserved_ptns[ptn+x-max_orig_nptn];
} else {
tip_left = partial_lh_left + block * aln->STATE_UNKNOWN;
tip_right = partial_lh_right + block * aln->STATE_UNKNOWN;
}
double *this_vec_left = vec_left+x;
double *this_vec_right = vec_right+x;
for (i = 0; i < block; i++) {
*this_vec_left = tip_left[i];
*this_vec_right = tip_right[i];
this_vec_left += VectorClass::size();
this_vec_right += VectorClass::size();
}
}
for (i = 0; i < block; i++)
partial_lh[i] = vleft[i] * vright[i];
}
} else if (isRootLeaf(left->node) && !right->node->isLeaf()) {
// left is root node
/*--------------------- ROOT-INTERNAL NODE case ------------------*/
// only take scale_num from the right subtree
memcpy(dad_branch->scale_num + ptn_lower, right->scale_num + ptn_lower, (ptn_upper-ptn_lower) * sizeof(UBYTE));
double *partial_lh_left = partial_lh_leaves;
for (ptn = ptn_lower; ptn < ptn_upper; ptn+=VectorClass::size()) {
VectorClass *partial_lh = (VectorClass*)(dad_branch->partial_lh + ptn*block);
VectorClass *partial_lh_right = (VectorClass*)(right->partial_lh + ptn*block);
double *eright_ptr = eright;
double *lh_left = partial_lh_left;
for (c = 0; c < ncat_mix; c++) {
// compute real partial likelihood vector
for (x = 0; x < nstates; x++) {
VectorClass vright;
#ifdef KERNEL_FIX_STATES
dotProductVec<VectorClass, double, nstates, FMA>(eright_ptr, partial_lh_right, vright);
#else
dotProductVec<VectorClass, double, FMA>(eright_ptr, partial_lh_right, vright, nstates);
#endif
eright_ptr += nstates;
partial_lh[x] = lh_left[x]*vright;
}
partial_lh_right += nstates;
lh_left += nstates;
partial_lh += nstates;
}
}
} else if (left->node->isLeaf() && !right->node->isLeaf()) {
/*--------------------- TIP-INTERNAL NODE case ------------------*/
// only take scale_num from the right subtree
memcpy(dad_branch->scale_num + ptn_lower, right->scale_num + ptn_lower, (ptn_upper-ptn_lower) * sizeof(UBYTE));
double *partial_lh_left = partial_lh_leaves;
double *vec_left = buffer_partial_lh_ptr + (block*2)*VectorClass::size()*thread_id;
for (ptn = ptn_lower; ptn < ptn_upper; ptn+=VectorClass::size()) {
VectorClass *partial_lh = (VectorClass*)(dad_branch->partial_lh + ptn*block);
VectorClass *partial_lh_right = (VectorClass*)(right->partial_lh + ptn*block);
VectorClass *vleft = (VectorClass*)vec_left;
// load data for tip
for (x = 0; x < VectorClass::size(); x++) {
double *tip;
if (ptn+x < orig_nptn)
tip = partial_lh_left + block*(aln->at(ptn+x))[left->node->id];
else if (ptn+x < max_orig_nptn)
tip = partial_lh_left + block*aln->STATE_UNKNOWN;
else if (ptn+x < nptn)
tip = partial_lh_left + block*model_factory->unobserved_ptns[ptn+x-max_orig_nptn];
else
tip = partial_lh_left + block*aln->STATE_UNKNOWN;
double *this_vec_left = vec_left+x;
for (i = 0; i < block; i++) {
*this_vec_left = tip[i];
this_vec_left += VectorClass::size();
}
}
VectorClass lh_max = 0.0;
double *eright_ptr = eright;
for (c = 0; c < ncat_mix; c++) {
// compute real partial likelihood vector
for (x = 0; x < nstates; x++) {
VectorClass vright;
#ifdef KERNEL_FIX_STATES
dotProductVec<VectorClass, double, nstates, FMA>(eright_ptr, partial_lh_right, vright);
#else
dotProductVec<VectorClass, double, FMA>(eright_ptr, partial_lh_right, vright, nstates);
#endif
eright_ptr += nstates;
lh_max = max(lh_max, (partial_lh[x] = vleft[x]*vright));
}
vleft += nstates;
partial_lh_right += nstates;
partial_lh += nstates;
}
// check if one should scale partial likelihoods
auto underflown = (lh_max < SCALING_THRESHOLD);
if (horizontal_or(underflown)) {
// now do the likelihood scaling
for (x = 0; x < VectorClass::size(); x++)
if (underflown[x]) {
double *partial_lh = dad_branch->partial_lh + (ptn*block + x);
// now do the likelihood scaling
for (i = 0; i < block; i++) {
partial_lh[i*VectorClass::size()] = ldexp(partial_lh[i*VectorClass::size()], SCALING_THRESHOLD_EXP);
}
dad_branch->scale_num[ptn+x] += 1;
}
}
}
} else {
/*--------------------- INTERNAL-INTERNAL NODE case ------------------*/
for (ptn = ptn_lower; ptn < ptn_upper; ptn+=VectorClass::size()) {
VectorClass *partial_lh = (VectorClass*)(dad_branch->partial_lh + ptn*block);
VectorClass *partial_lh_left = (VectorClass*)(left->partial_lh + ptn*block);
VectorClass *partial_lh_right = (VectorClass*)(right->partial_lh + ptn*block);
VectorClass lh_max = 0.0;
for (i = 0; i < VectorClass::size(); i++)
dad_branch->scale_num[ptn+i] = left->scale_num[ptn+i] + right->scale_num[ptn+i];
double *eleft_ptr = eleft;
double *eright_ptr = eright;
for (c = 0; c < ncat_mix; c++) {
// compute real partial likelihood vector
for (x = 0; x < nstates; x++) {
#ifdef KERNEL_FIX_STATES
dotProductDualVec<VectorClass, double, nstates, FMA>(eleft_ptr, partial_lh_left, eright_ptr, partial_lh_right, partial_lh[x]);
#else
dotProductDualVec<VectorClass, double, FMA>(eleft_ptr, partial_lh_left, eright_ptr, partial_lh_right, partial_lh[x], nstates);
#endif
eleft_ptr += nstates;
eright_ptr += nstates;
lh_max=max(lh_max, partial_lh[x]);
}
partial_lh_left += nstates;
partial_lh_right += nstates;
partial_lh += nstates;
}
// check if one should scale partial likelihoods
auto underflown = (lh_max < SCALING_THRESHOLD);
if (horizontal_or(underflown)) {
// now do the likelihood scaling
for (x = 0; x < VectorClass::size(); x++)
if (underflown[x]) {
double *partial_lh = dad_branch->partial_lh + (ptn*block + x);
// now do the likelihood scaling
for (i = 0; i < block; i++) {
partial_lh[i*VectorClass::size()] = ldexp(partial_lh[i*VectorClass::size()], SCALING_THRESHOLD_EXP);
}
dad_branch->scale_num[ptn+x] += 1;
}
}
}
}
}
#ifdef KERNEL_FIX_STATES
template <class VectorClass, const int nstates, const bool FMA>
void PhyloTree::computeNonrevLikelihoodDervSIMD(PhyloNeighbor *dad_branch, PhyloNode *dad, double *df, double *ddf) {
#else
template <class VectorClass, const bool FMA>
void PhyloTree::computeNonrevLikelihoodDervGenericSIMD(PhyloNeighbor *dad_branch, PhyloNode *dad, double *df, double *ddf) {
#endif
// assert(rooted);
PhyloNode *node = (PhyloNode*) dad_branch->node;
PhyloNeighbor *node_branch = (PhyloNeighbor*) node->findNeighbor(dad);
if (!central_partial_lh)
initializeAllPartialLh();
if (node->isLeaf() || (dad_branch->direction == AWAYFROM_ROOT && !isRootLeaf(dad))) {
PhyloNode *tmp_node = dad;
dad = node;
node = tmp_node;
PhyloNeighbor *tmp_nei = dad_branch;
dad_branch = node_branch;
node_branch = tmp_nei;
}
#ifdef KERNEL_FIX_STATES
computeTraversalInfo<VectorClass, nstates>(node, dad, false);
#else
computeTraversalInfo<VectorClass>(node, dad, false);
#endif
#ifndef KERNEL_FIX_STATES
size_t nstates = aln->num_states;
#endif
size_t nstatesqr = nstates*nstates;
size_t ncat = site_rate->getNRate();
size_t ncat_mix = (model_factory->fused_mix_rate) ? ncat : ncat*model->getNMixtures();
size_t denom = (model_factory->fused_mix_rate) ? 1 : ncat;
size_t block = ncat_mix * nstates;
size_t ptn; // for big data size > 4GB memory required
size_t c, i;
size_t orig_nptn = aln->size();
size_t max_orig_nptn = ((orig_nptn+VectorClass::size()-1)/VectorClass::size())*VectorClass::size();
size_t nptn = max_orig_nptn+model_factory->unobserved_ptns.size();
bool isASC = model_factory->unobserved_ptns.size() > 0;
// double *trans_mat = new double[block*nstates*3];
double *trans_mat = buffer_partial_lh;
double *trans_derv1 = buffer_partial_lh + block*nstates;
double *trans_derv2 = trans_derv1 + block*nstates;
double *buffer_partial_lh_ptr = buffer_partial_lh + get_safe_upper_limit(3*block*nstates);
for (c = 0; c < ncat_mix; c++) {
size_t mycat = c%ncat;
size_t m = c/denom;
double cat_rate = site_rate->getRate(mycat);
double len = cat_rate * dad_branch->length;
double prop = site_rate->getProp(mycat) * model->getMixtureWeight(m);
double *this_trans_mat = &trans_mat[c*nstatesqr];
double *this_trans_derv1 = &trans_derv1[c*nstatesqr];
double *this_trans_derv2 = &trans_derv2[c*nstatesqr];
model->computeTransDerv(len, this_trans_mat, this_trans_derv1, this_trans_derv2, m);
double prop_rate = prop * cat_rate;
double prop_rate_2 = prop_rate * cat_rate;
for (i = 0; i < nstatesqr; i++) {
this_trans_mat[i] *= prop;
this_trans_derv1[i] *= prop_rate;
this_trans_derv2[i] *= prop_rate_2;
}
if (!rooted) {
// for unrooted tree, multiply with state_freq
double state_freq[nstates];
model->getStateFrequency(state_freq, m);
for (i = 0; i < nstates; i++) {
for (size_t x = 0; x < nstates; x++) {
this_trans_mat[x] *= state_freq[i];
this_trans_derv1[x] *= state_freq[i];
this_trans_derv2[x] *= state_freq[i];
}
this_trans_mat += nstates;
this_trans_derv1 += nstates;
this_trans_derv2 += nstates;
}
}
}
VectorClass all_df(0.0), all_ddf(0.0);
VectorClass all_prob_const(0.0), all_df_const(0.0), all_ddf_const(0.0);
vector<size_t> limits;
computeBounds<VectorClass>(num_threads, nptn, limits);
// double *buffer_partial_lh_ptr = buffer_partial_lh;
if (dad->isLeaf()) {
// make sure that we do not estimate the virtual branch length from the root
ASSERT(!isRootLeaf(dad));
// special treatment for TIP-INTERNAL NODE case
// double *partial_lh_node = new double[(aln->STATE_UNKNOWN+1)*block*3];
double *partial_lh_node = buffer_partial_lh_ptr;
double *partial_lh_derv1 = partial_lh_node + (aln->STATE_UNKNOWN+1)*block;
double *partial_lh_derv2 = partial_lh_derv1 + (aln->STATE_UNKNOWN+1)*block;
buffer_partial_lh_ptr += get_safe_upper_limit((aln->STATE_UNKNOWN+1)*block*3);
IntVector states_dad = aln->seq_states[dad->id];
states_dad.push_back(aln->STATE_UNKNOWN);
// precompute information from one tip
for (IntVector::iterator it = states_dad.begin(); it != states_dad.end(); it++) {
double *lh_node = partial_lh_node +(*it)*block;
double *lh_derv1 = partial_lh_derv1 +(*it)*block;
double *lh_derv2 = partial_lh_derv2 +(*it)*block;
double *lh_tip = tip_partial_lh + (*it)*nstates;
double *trans_mat_tmp = trans_mat;
double *trans_derv1_tmp = trans_derv1;
double *trans_derv2_tmp = trans_derv2;
for (c = 0; c < ncat_mix; c++) {
for (i = 0; i < nstates; i++) {
lh_node[i] = 0.0;
lh_derv1[i] = 0.0;
lh_derv2[i] = 0.0;
for (size_t x = 0; x < nstates; x++) {
lh_node[i] += trans_mat_tmp[x] * lh_tip[x];
lh_derv1[i] += trans_derv1_tmp[x] * lh_tip[x];
lh_derv2[i] += trans_derv2_tmp[x] * lh_tip[x];
}
trans_mat_tmp += nstates;
trans_derv1_tmp += nstates;
trans_derv2_tmp += nstates;
}
lh_node += nstates;
lh_derv1 += nstates;
lh_derv2 += nstates;
}
}
// now do the real computation
#ifdef _OPENMP
#pragma omp parallel for private(ptn, i, c) schedule(static,1) num_threads(num_threads)
#endif
for (int thread_id = 0; thread_id < num_threads; thread_id++) {
VectorClass my_df(0.0), my_ddf(0.0), vc_prob_const(0.0), vc_df_const(0.0), vc_ddf_const(0.0);
size_t ptn_lower = limits[thread_id];
size_t ptn_upper = limits[thread_id+1];
// first compute partial_lh
for (vector<TraversalInfo>::iterator it = traversal_info.begin(); it != traversal_info.end(); it++)
computePartialLikelihood(*it, ptn_lower, ptn_upper, thread_id);
double *vec_tip = buffer_partial_lh_ptr + block*3*VectorClass::size()*thread_id;
for (ptn = ptn_lower; ptn < ptn_upper; ptn+=VectorClass::size()) {
VectorClass lh_ptn, df_ptn, ddf_ptn;
VectorClass *partial_lh_dad = (VectorClass*)(dad_branch->partial_lh + ptn*block);
//load tip vector
for (i = 0; i < VectorClass::size(); i++) {
size_t state_dad;
if (ptn+i < orig_nptn)
state_dad = block * (aln->at(ptn+i))[dad->id];
else if (ptn+i < max_orig_nptn)
state_dad = block * aln->STATE_UNKNOWN;
else if (ptn+i < nptn)
state_dad = block * model_factory->unobserved_ptns[ptn+i-max_orig_nptn];
else
state_dad = block * aln->STATE_UNKNOWN;
double *lh_tip = partial_lh_node + state_dad;
double *lh_derv1 = partial_lh_derv1 + state_dad;
double *lh_derv2 = partial_lh_derv2 + state_dad;
double *this_vec_tip = vec_tip+i;
double *this_derv1 = this_vec_tip + block*VectorClass::size();
double *this_derv2 = this_derv1 + block*VectorClass::size();
for (c = 0; c < block; c++) {
*this_vec_tip = lh_tip[c];
*this_derv1 = lh_derv1[c];
*this_derv2 = lh_derv2[c];
this_vec_tip += VectorClass::size();
this_derv1 += VectorClass::size();
this_derv2 += VectorClass::size();
}
}
VectorClass *lh_node = (VectorClass*)vec_tip;
VectorClass *lh_derv1 = (VectorClass*)vec_tip + block;
VectorClass *lh_derv2 = (VectorClass*)lh_derv1 + block;
#ifdef KERNEL_FIX_STATES
dotProductTriple<VectorClass, VectorClass, nstates, FMA, false>(lh_node, lh_derv1, lh_derv2, partial_lh_dad, lh_ptn, df_ptn, ddf_ptn, block);
#else
dotProductTriple<VectorClass, VectorClass, FMA, false>(lh_node, lh_derv1, lh_derv2, partial_lh_dad, lh_ptn, df_ptn, ddf_ptn, block, nstates);
#endif
lh_ptn = (lh_ptn + VectorClass().load_a(&ptn_invar[ptn]));
if (ptn < orig_nptn) {
lh_ptn = 1.0 / lh_ptn;
VectorClass df_frac = df_ptn * lh_ptn;
VectorClass ddf_frac = ddf_ptn * lh_ptn;
VectorClass freq;
freq.load_a(&ptn_freq[ptn]);
VectorClass tmp1 = df_frac * freq;
VectorClass tmp2 = ddf_frac * freq;
my_df += tmp1;
my_ddf += nmul_add(tmp1, df_frac, tmp2);
} else {
if (ptn+VectorClass::size() > nptn) {
// cutoff the last entries if going beyond
lh_ptn.cutoff(nptn-ptn);
df_ptn.cutoff(nptn-ptn);
ddf_ptn.cutoff(nptn-ptn);
}
// bugfix 2016-01-21, prob_const can be rescaled
double *lh_ptn_ptr = (double*)&lh_ptn;
for (i = 0; i < VectorClass::size(); i++)
if (dad_branch->scale_num[ptn+i] >= 1)
lh_ptn_ptr[i] *= SCALING_THRESHOLD;
vc_prob_const += lh_ptn;
vc_df_const += df_ptn;
vc_ddf_const += ddf_ptn;
}
} // FOR ptn
#ifdef _OPENMP
#pragma omp critical
#endif
{
all_df += my_df;
all_ddf += my_ddf;
if (isASC) {
all_prob_const += vc_prob_const;
all_df_const += vc_df_const;
all_ddf_const += vc_ddf_const;
}
}
} // FOR thread_id
// delete [] partial_lh_node;
} else {
// both dad and node are internal nodes
#ifdef _OPENMP
#pragma omp parallel for private(ptn, i, c) schedule(static,1) num_threads(num_threads)
#endif
for (int thread_id = 0; thread_id < num_threads; thread_id++) {
VectorClass my_df(0.0), my_ddf(0.0), vc_prob_const(0.0), vc_df_const(0.0), vc_ddf_const(0.0);
size_t ptn_lower = limits[thread_id];
size_t ptn_upper = limits[thread_id+1];
// first compute partial_lh
for (vector<TraversalInfo>::iterator it = traversal_info.begin(); it != traversal_info.end(); it++)
computePartialLikelihood(*it, ptn_lower, ptn_upper, thread_id);
for (ptn = ptn_lower; ptn < ptn_upper; ptn+=VectorClass::size()) {
VectorClass lh_ptn(0.0), df_ptn(0.0), ddf_ptn(0.0);
VectorClass *partial_lh_dad = (VectorClass*)(dad_branch->partial_lh + ptn*block);
VectorClass *partial_lh_node = (VectorClass*)(node_branch->partial_lh + ptn*block);
double *trans_mat_tmp = trans_mat;
double *trans_derv1_tmp = trans_derv1;
double *trans_derv2_tmp = trans_derv2;
for (c = 0; c < ncat_mix; c++) {
for (i = 0; i < nstates; i++) {
VectorClass lh_state;
VectorClass lh_derv1;
VectorClass lh_derv2;
#ifdef KERNEL_FIX_STATES
dotProductTriple<VectorClass, double, nstates, FMA, false>(trans_mat_tmp, trans_derv1_tmp, trans_derv2_tmp, partial_lh_node, lh_state, lh_derv1, lh_derv2, nstates);
#else
dotProductTriple<VectorClass, double, FMA, false>(trans_mat_tmp, trans_derv1_tmp, trans_derv2_tmp, partial_lh_node, lh_state, lh_derv1, lh_derv2, nstates, nstates);
#endif
lh_ptn = mul_add(partial_lh_dad[i], lh_state, lh_ptn);
df_ptn = mul_add(partial_lh_dad[i], lh_derv1, df_ptn);
ddf_ptn = mul_add(partial_lh_dad[i], lh_derv2, ddf_ptn);
trans_mat_tmp += nstates;
trans_derv1_tmp += nstates;
trans_derv2_tmp += nstates;
}
partial_lh_node += nstates;
partial_lh_dad += nstates;
}
lh_ptn = (lh_ptn + VectorClass().load_a(&ptn_invar[ptn]));
if (ptn < orig_nptn) {
lh_ptn = 1.0 / lh_ptn;
VectorClass df_frac = df_ptn * lh_ptn;
VectorClass ddf_frac = ddf_ptn * lh_ptn;
VectorClass freq;
freq.load_a(&ptn_freq[ptn]);
VectorClass tmp1 = df_frac * freq;
VectorClass tmp2 = ddf_frac * freq;
my_df += tmp1;
my_ddf += nmul_add(tmp1, df_frac, tmp2);
} else {
if (ptn+VectorClass::size() > nptn) {
// cutoff the last entries if going beyond
lh_ptn.cutoff(nptn-ptn);
df_ptn.cutoff(nptn-ptn);
ddf_ptn.cutoff(nptn-ptn);
}
// bugfix 2016-01-21, prob_const can be rescaled
double *lh_ptn_ptr = (double*)&lh_ptn;
for (i = 0; i < VectorClass::size(); i++)
if (dad_branch->scale_num[ptn+i] >= 1)
lh_ptn_ptr[i] *= SCALING_THRESHOLD;
vc_prob_const += lh_ptn;
vc_df_const += df_ptn;
vc_ddf_const += ddf_ptn;
}
} // FOR ptn
#ifdef _OPENMP
#pragma omp critical
#endif
{
all_df += my_df;
all_ddf += my_ddf;
if (isASC) {
all_prob_const += vc_prob_const;
all_df_const += vc_df_const;
all_ddf_const += vc_ddf_const;
}
}
} // FOR thread
}
*df = horizontal_add(all_df);
*ddf = horizontal_add(all_ddf);
ASSERT(std::isfinite(*df) && "Numerical underflow for non-rev lh-derivative");
if (isASC) {
double prob_const = 0.0, df_const = 0.0, ddf_const = 0.0;
prob_const = horizontal_add(all_prob_const);
df_const = horizontal_add(all_df_const);
ddf_const = horizontal_add(all_ddf_const);
// ascertainment bias correction
prob_const = 1.0 - prob_const;
double df_frac = df_const / prob_const;
double ddf_frac = ddf_const / prob_const;
int nsites = aln->getNSite();
*df += nsites * df_frac;
*ddf += nsites *(ddf_frac + df_frac*df_frac);
}
}
#ifdef KERNEL_FIX_STATES
template <class VectorClass, const int nstates, const bool FMA>
double PhyloTree::computeNonrevLikelihoodBranchSIMD(PhyloNeighbor *dad_branch, PhyloNode *dad) {
#else
template <class VectorClass, const bool FMA>
double PhyloTree::computeNonrevLikelihoodBranchGenericSIMD(PhyloNeighbor *dad_branch, PhyloNode *dad) {
#endif
// assert(rooted);
PhyloNode *node = (PhyloNode*) dad_branch->node;
PhyloNeighbor *node_branch = (PhyloNeighbor*) node->findNeighbor(dad);
if (!central_partial_lh)
initializeAllPartialLh();
if (node->isLeaf() || (dad_branch->direction == AWAYFROM_ROOT && !isRootLeaf(dad))) {
PhyloNode *tmp_node = dad;
dad = node;
node = tmp_node;
PhyloNeighbor *tmp_nei = dad_branch;
dad_branch = node_branch;
node_branch = tmp_nei;
}
#ifdef KERNEL_FIX_STATES
computeTraversalInfo<VectorClass, nstates>(node, dad, false);
#else
computeTraversalInfo<VectorClass>(node, dad, false);
#endif
double tree_lh = 0.0;
#ifndef KERNEL_FIX_STATES
size_t nstates = aln->num_states;
#endif
size_t nstatesqr = nstates*nstates;
size_t ncat = site_rate->getNRate();
size_t ncat_mix = (model_factory->fused_mix_rate) ? ncat : ncat*model->getNMixtures();
size_t denom = (model_factory->fused_mix_rate) ? 1 : ncat;
size_t block = ncat_mix * nstates;
size_t ptn; // for big data size > 4GB memory required
size_t c, i;
size_t orig_nptn = aln->size();
size_t max_orig_nptn = ((orig_nptn+VectorClass::size()-1)/VectorClass::size())*VectorClass::size();
size_t nptn = max_orig_nptn+model_factory->unobserved_ptns.size();
bool isASC = model_factory->unobserved_ptns.size() > 0;
vector<size_t> limits;
computeBounds<VectorClass>(num_threads, nptn, limits);
// double *trans_mat = new double[block*nstates];
double *trans_mat = buffer_partial_lh;
double *buffer_partial_lh_ptr = buffer_partial_lh + block*nstates;
for (c = 0; c < ncat_mix; c++) {
size_t mycat = c%ncat;
size_t m = c/denom;
double len = site_rate->getRate(mycat) * dad_branch->length;
double prop = site_rate->getProp(mycat) * model->getMixtureWeight(m);
double *this_trans_mat = &trans_mat[c*nstatesqr];
model->computeTransMatrix(len, this_trans_mat, m);
for (i = 0; i < nstatesqr; i++)
this_trans_mat[i] *= prop;
if (!rooted) {
// if unrooted tree, multiply with frequency
double state_freq[nstates];
model->getStateFrequency(state_freq, m);
for (i = 0; i < nstates; i++) {
for (size_t x = 0; x < nstates; x++)
this_trans_mat[x] *= state_freq[i];
this_trans_mat += nstates;
}
}
}
VectorClass all_tree_lh(0.0);
VectorClass all_prob_const(0.0);
if (dad->isLeaf()) {
// special treatment for TIP-INTERNAL NODE case
// double *partial_lh_node = new double[(aln->STATE_UNKNOWN+1)*block];
double *partial_lh_node = buffer_partial_lh_ptr;
buffer_partial_lh_ptr += get_safe_upper_limit((aln->STATE_UNKNOWN+1)*block);
if (isRootLeaf(dad)) {
for (c = 0; c < ncat_mix; c++) {
double *lh_node = partial_lh_node + c*nstates;
size_t m = c/denom;
model->getStateFrequency(lh_node, m);
double prop = site_rate->getProp(c%ncat) * model->getMixtureWeight(m);
for (i = 0; i < nstates; i++)
lh_node[i] *= prop;
}
} else {
IntVector states_dad = aln->seq_states[dad->id];
states_dad.push_back(aln->STATE_UNKNOWN);
// precompute information from one tip
for (IntVector::iterator it = states_dad.begin(); it != states_dad.end(); it++) {
double *lh_node = partial_lh_node +(*it)*block;
double *lh_tip = tip_partial_lh + (*it)*nstates;
double *trans_mat_tmp = trans_mat;
for (c = 0; c < ncat_mix; c++) {
for (i = 0; i < nstates; i++) {
lh_node[i] = 0.0;
for (size_t x = 0; x < nstates; x++)
lh_node[i] += trans_mat_tmp[x] * lh_tip[x];
trans_mat_tmp += nstates;
}
lh_node += nstates;
}
}
}
// now do the real computation
#ifdef _OPENMP
#pragma omp parallel for private(ptn, i, c) schedule(static,1) num_threads(num_threads)
#endif
for (int thread_id = 0; thread_id < num_threads; thread_id++) {
VectorClass vc_tree_lh(0.0), vc_prob_const(0.0);
size_t ptn_lower = limits[thread_id];
size_t ptn_upper = limits[thread_id+1];
// first compute partial_lh
for (vector<TraversalInfo>::iterator it = traversal_info.begin(); it != traversal_info.end(); it++)
computePartialLikelihood(*it, ptn_lower, ptn_upper, thread_id);
// reset memory for _pattern_lh_cat
// memset(_pattern_lh_cat+ptn_lower*ncat_mix, 0, (ptn_upper-ptn_lower)*ncat_mix*sizeof(double));
double *vec_tip = buffer_partial_lh_ptr + block*VectorClass::size()*thread_id;
for (ptn = ptn_lower; ptn < ptn_upper; ptn+=VectorClass::size()) {
VectorClass lh_ptn;
lh_ptn.load_a(&ptn_invar[ptn]);
VectorClass *lh_cat = (VectorClass*)(_pattern_lh_cat + ptn*ncat_mix);
VectorClass *partial_lh_dad = (VectorClass*)(dad_branch->partial_lh + ptn*block);
VectorClass *lh_node = (VectorClass*)vec_tip;
//load tip vector
for (i = 0; i < VectorClass::size(); i++) {
double *lh_tip;
if (isRootLeaf(dad))
lh_tip = partial_lh_node;
else if (ptn+i < orig_nptn)
lh_tip = partial_lh_node + block*(aln->at(ptn+i))[dad->id];
else if (ptn+i < max_orig_nptn)
lh_tip = partial_lh_node + block*aln->STATE_UNKNOWN;
else if (ptn+i < nptn)
lh_tip = partial_lh_node + block*model_factory->unobserved_ptns[ptn+i-max_orig_nptn];
else
lh_tip = partial_lh_node + block*aln->STATE_UNKNOWN;
double *this_vec_tip = vec_tip+i;
for (c = 0; c < block; c++) {
*this_vec_tip = lh_tip[c];
this_vec_tip += VectorClass::size();
}
}
if (_pattern_lh_cat_state) {
// naively compute pattern_lh per category per state
VectorClass *lh_state = (VectorClass*)(_pattern_lh_cat_state + ptn*block);
for (c = 0; c < ncat_mix; c++) {
for (i=0; i < nstates; i++) {
lh_cat[c] += (lh_state[i] = lh_node[i]*partial_lh_dad[i]);
}
lh_node += nstates;
partial_lh_dad += nstates;
lh_state += nstates;
lh_ptn += lh_cat[c];
}
} else {
for (c = 0; c < ncat_mix; c++) {
#ifdef KERNEL_FIX_STATES
dotProductVec<VectorClass, VectorClass, nstates, FMA>(lh_node, partial_lh_dad, lh_cat[c]);
#else
dotProductVec<VectorClass, VectorClass, FMA>(lh_node, partial_lh_dad, lh_cat[c], nstates);
#endif
lh_node += nstates;
partial_lh_dad += nstates;
lh_ptn += lh_cat[c];
}
}
VectorClass vc_min_scale;
double* vc_min_scale_ptr = (double*)&vc_min_scale;
for (i = 0; i < VectorClass::size(); i++) {
vc_min_scale_ptr[i] = dad_branch->scale_num[ptn+i];
}
vc_min_scale *= LOG_SCALING_THRESHOLD;
// lh_ptn = abs(lh_ptn);
// assert(horizontal_and(lh_ptn > 0));
if (ptn < orig_nptn) {
lh_ptn = log(lh_ptn) + vc_min_scale;
lh_ptn.store_a(&_pattern_lh[ptn]);
vc_tree_lh = mul_add(lh_ptn, VectorClass().load_a(&ptn_freq[ptn]), vc_tree_lh);
} else {
// ascertainment bias correction
if (ptn+VectorClass::size() > nptn) {
// cutoff the last entries if going beyond
lh_ptn.cutoff(nptn-ptn);
}
// bugfix 2016-01-21, prob_const can be rescaled
if (horizontal_or(vc_min_scale != 0.0)) {
// some entries are rescaled
double *lh_ptn_dbl = (double*)&lh_ptn;
for (i = 0; i < VectorClass::size(); i++)
if (vc_min_scale_ptr[i] != 0.0)
lh_ptn_dbl[i] *= SCALING_THRESHOLD;
}
vc_prob_const += lh_ptn;
}
} // FOR ptn
#ifdef _OPENMP
#pragma omp critical
#endif
{
all_tree_lh += vc_tree_lh;
if (isASC)
all_prob_const += vc_prob_const;
}
} // FOR thread_id
} else {
// both dad and node are internal nodes
#ifdef _OPENMP
#pragma omp parallel for private(ptn, i, c) schedule(static,1) num_threads(num_threads)
#endif
for (int thread_id = 0; thread_id < num_threads; thread_id++) {
VectorClass vc_tree_lh(0.0), vc_prob_const(0.0);
size_t ptn_lower = limits[thread_id];
size_t ptn_upper = limits[thread_id+1];
// first compute partial_lh
for (vector<TraversalInfo>::iterator it = traversal_info.begin(); it != traversal_info.end(); it++)
computePartialLikelihood(*it, ptn_lower, ptn_upper, thread_id);
// reset memory for _pattern_lh_cat
memset(_pattern_lh_cat+ptn_lower*ncat_mix, 0, (ptn_upper-ptn_lower)*ncat_mix*sizeof(double));
for (ptn = ptn_lower; ptn < ptn_upper; ptn+=VectorClass::size()) {
VectorClass lh_ptn;
lh_ptn.load_a(&ptn_invar[ptn]);
VectorClass *lh_cat = (VectorClass*)(_pattern_lh_cat + ptn*ncat_mix);
VectorClass *partial_lh_dad = (VectorClass*)(dad_branch->partial_lh + ptn*block);
VectorClass *partial_lh_node = (VectorClass*)(node_branch->partial_lh + ptn*block);
double *trans_mat_tmp = trans_mat;
if (_pattern_lh_cat_state) {
VectorClass *lh_state = (VectorClass*)(_pattern_lh_cat_state + ptn*block);
for (c = 0; c < ncat_mix; c++) {
for (i = 0; i < nstates; i++) {
#ifdef KERNEL_FIX_STATES
dotProductVec<VectorClass, double, nstates, FMA>(trans_mat_tmp, partial_lh_node, lh_state[i]);
#else
dotProductVec<VectorClass, double, FMA>(trans_mat_tmp, partial_lh_node, lh_state[i], nstates);
#endif
lh_cat[c] += (lh_state[i] *= partial_lh_dad[i]);
trans_mat_tmp += nstates;
}
lh_ptn += lh_cat[c];
partial_lh_node += nstates;
partial_lh_dad += nstates;
lh_state += nstates;
}
} else {
for (c = 0; c < ncat_mix; c++) {
for (i = 0; i < nstates; i++) {
VectorClass lh_state;
#ifdef KERNEL_FIX_STATES
dotProductVec<VectorClass, double, nstates, FMA>(trans_mat_tmp, partial_lh_node, lh_state);
#else
dotProductVec<VectorClass, double, FMA>(trans_mat_tmp, partial_lh_node, lh_state, nstates);
#endif
lh_cat[c] = mul_add(partial_lh_dad[i], lh_state, lh_cat[c]);
trans_mat_tmp += nstates;
}
lh_ptn += lh_cat[c];
partial_lh_node += nstates;
partial_lh_dad += nstates;
}
}
VectorClass vc_min_scale;
double* vc_min_scale_ptr = (double*)&vc_min_scale;
for (i = 0; i < VectorClass::size(); i++) {
vc_min_scale_ptr[i] = dad_branch->scale_num[ptn+i] + node_branch->scale_num[ptn+i];
}
vc_min_scale *= LOG_SCALING_THRESHOLD;
// lh_ptn = abs(lh_ptn);
ASSERT(horizontal_and(lh_ptn > 0));
if (ptn < orig_nptn) {
lh_ptn = log(lh_ptn) + vc_min_scale;
lh_ptn.store_a(&_pattern_lh[ptn]);
vc_tree_lh = mul_add(lh_ptn, VectorClass().load_a(&ptn_freq[ptn]), vc_tree_lh);
} else {
// ascertainment bias correction
if (ptn+VectorClass::size() > nptn) {
// cutoff the last entries if going beyond
lh_ptn.cutoff(nptn-ptn);
}
// bugfix 2016-01-21, prob_const can be rescaled
if (horizontal_or(vc_min_scale != 0.0)) {
// some entries are rescaled
double *lh_ptn_dbl = (double*)&lh_ptn;
for (i = 0; i < VectorClass::size(); i++)
if (vc_min_scale_ptr[i] != 0.0)
lh_ptn_dbl[i] *= SCALING_THRESHOLD;
}
vc_prob_const += lh_ptn;
}
} // FOR ptn
#ifdef _OPENMP
#pragma omp critical
#endif
{
all_tree_lh += vc_tree_lh;
if (isASC)
all_prob_const += vc_prob_const;
}
} // FOR thread_id
}
tree_lh = horizontal_add(all_tree_lh);
if (!std::isfinite(tree_lh)) {
model->writeInfo(cout);
site_rate->writeInfo(cout);
ASSERT(0 && "Numerical underflow for non-rev lh-branch");
}
if (isASC) {
// ascertainment bias correction
double prob_const = horizontal_add(all_prob_const);
if (prob_const >= 1.0 || prob_const < 0.0) {
printTree(cout, WT_TAXON_ID + WT_BR_LEN + WT_NEWLINE);
model->writeInfo(cout);
}
ASSERT(prob_const < 1.0 && prob_const >= 0.0);
prob_const = log(1.0 - prob_const);
for (ptn = 0; ptn < orig_nptn; ptn+=VectorClass::size())
(VectorClass().load_a(&_pattern_lh[ptn])-prob_const).store_a(&_pattern_lh[ptn]);
tree_lh -= aln->getNSite()*prob_const;
ASSERT(std::isfinite(tree_lh));
}
return tree_lh;
}
#endif
|