| 1 |
// Author: David Alexander |
| 2 |
|
| 3 |
#include <ConsensusCore/Quiver/SseRecursor.hpp> |
| 4 |
|
| 5 |
#include <ConsensusCore/Edna/EdnaEvaluator.hpp> |
| 6 |
#include <ConsensusCore/Interval.hpp> |
| 7 |
#include <ConsensusCore/Matrix/DenseMatrix.hpp> |
| 8 |
#include <ConsensusCore/Matrix/SparseMatrix.hpp> |
| 9 |
#include <ConsensusCore/Quiver/QvEvaluator.hpp> |
| 10 |
#include <ConsensusCore/Quiver/SimpleRecursor.hpp> |
| 11 |
#include <ConsensusCore/Quiver/detail/Combiner.hpp> |
| 12 |
#include <ConsensusCore/Utils.hpp> |
| 13 |
|
| 14 |
#include <algorithm> |
| 15 |
#include <boost/tuple/tuple.hpp> |
| 16 |
#include <climits> |
| 17 |
#include <numeric> |
| 18 |
#include <utility> |
| 19 |
|
| 20 |
using std::max; |
| 21 |
using std::min; |
| 22 |
|
| 23 |
#define NEG_INF -FLT_MAX |
| 24 |
#define POS_INF FLT_MAX |
| 25 |
#define NEG_INF_4 (Zero4<lfloat>()) |
| 26 |
|
| 27 |
namespace ConsensusCore { |
| 28 |
|
| 29 |
// MSVC dones't appear to have this defined. |
| 30 |
#ifdef _MSC_VER |
| 31 |
static inline __m128 operator+(const __m128 a, const __m128 b) { return _mm_add_ps(a, b); } |
| 32 |
#endif |
| 33 |
|
| 34 |
template <typename M, typename E, typename C> |
| 35 |
void SseRecursor<M, E, C>::FillAlpha(const E& e, const M& guide, M& alpha) const |
| 36 |
{ |
| 37 |
int I = e.ReadLength(); |
| 38 |
int J = e.TemplateLength(); |
| 39 |
|
| 40 |
assert(alpha.Rows() == I + 1 && alpha.Columns() == J + 1); |
| 41 |
assert(guide.IsNull() || (guide.Rows() == alpha.Rows() && guide.Columns() == alpha.Columns())); |
| 42 |
|
| 43 |
int hintBeginRow = 0, hintEndRow = 0; |
| 44 |
|
| 45 |
for (int j = 0; j <= J; ++j) { |
| 46 |
this->RangeGuide(j, guide, alpha, &hintBeginRow, &hintEndRow); |
| 47 |
|
| 48 |
int requiredEndRow = min(I + 1, hintEndRow); |
| 49 |
|
| 50 |
float score = NEG_INF; |
| 51 |
float thresholdScore = NEG_INF; |
| 52 |
float maxScore = NEG_INF; |
| 53 |
|
| 54 |
alpha.StartEditingColumn(j, hintBeginRow, hintEndRow); |
| 55 |
|
| 56 |
int i; |
| 57 |
int beginRow = hintBeginRow, endRow; |
| 58 |
// Handle beginning rows non-SSE. Must handle row 0 this |
| 59 |
// way (if row 0 is to be filled), and must terminate with |
| 60 |
// (I - i + 1) divisible by 4, so that the SSE loop can |
| 61 |
// run safely to the end. Banding optimizations not applied |
| 62 |
// here. |
| 63 |
// |
| 64 |
// TODO(dalexander): we could also handle the first two columns this way, |
| 65 |
// and then we could remove all the conditionals from the SSE loop. |
| 66 |
// Profile first. |
| 67 |
// |
| 68 |
for (i = beginRow; (i == 0 || (I - i + 1) % 4 != 0) && i <= I; i++) { |
| 69 |
score = NEG_INF; |
| 70 |
|
| 71 |
// Start: |
| 72 |
if (i == 0 && j == 0) { |
| 73 |
score = 0.0f; |
| 74 |
} |
| 75 |
// Inc |
| 76 |
if (i > 0 && j > 0) { |
| 77 |
score = C::Combine(score, alpha(i - 1, j - 1) + e.Inc(i - 1, j - 1)); |
| 78 |
} |
| 79 |
// Merge |
| 80 |
if ((this->movesAvailable_ & MERGE) && (i > 0 && j > 1)) { |
| 81 |
score = C::Combine(score, alpha(i - 1, j - 2) + e.Merge(i - 1, j - 2)); |
| 82 |
} |
| 83 |
// Delete |
| 84 |
if (j > 0) { |
| 85 |
score = C::Combine(score, alpha(i, j - 1) + e.Del(i, j - 1)); |
| 86 |
} |
| 87 |
// Extra |
| 88 |
if (i > 0) { |
| 89 |
score = C::Combine(score, alpha(i - 1, j) + e.Extra(i - 1, j)); |
| 90 |
} |
| 91 |
alpha.Set(i, j, score); |
| 92 |
|
| 93 |
if (score > maxScore) { |
| 94 |
maxScore = score; |
| 95 |
thresholdScore = maxScore - this->bandingOptions_.ScoreDiff; |
| 96 |
} |
| 97 |
} |
| 98 |
// |
| 99 |
// Main SSE loop |
| 100 |
// |
| 101 |
assert(i > 0); |
| 102 |
for (; i <= I && (score >= thresholdScore || i < requiredEndRow); i += 4) { |
| 103 |
__m128 score4 = NEG_INF_4; |
| 104 |
// Incorporation: |
| 105 |
if (j > 0) { |
| 106 |
score4 = C::Combine4(score4, alpha.Get4(i - 1, j - 1) + e.Inc4(i - 1, j - 1)); |
| 107 |
} |
| 108 |
// Merge |
| 109 |
if ((this->movesAvailable_ & MERGE) && j >= 2) { |
| 110 |
score4 = C::Combine4(score4, alpha.Get4(i - 1, j - 2) + e.Merge4(i - 1, j - 2)); |
| 111 |
} |
| 112 |
// Deletion: |
| 113 |
if (j > 0) { |
| 114 |
score4 = C::Combine4(score4, alpha.Get4(i, j - 1) + e.Del4(i, j - 1)); |
| 115 |
} |
| 116 |
|
| 117 |
// |
| 118 |
// Extra (non-SSE cascade) |
| 119 |
// |
| 120 |
float insScores4_[4], score5_[5]; |
| 121 |
|
| 122 |
__m128 insScores4 = e.Extra4(i - 1, j); |
| 123 |
_mm_storeu_ps(insScores4_, insScores4); |
| 124 |
|
| 125 |
score5_[0] = alpha.Get(i - 1, j); |
| 126 |
_mm_storeu_ps(&score5_[1], score4); |
| 127 |
|
| 128 |
for (int ii = 1; ii < 5; ii++) { |
| 129 |
float v = C::Combine(score5_[ii], score5_[ii - 1] + insScores4_[ii - 1]); |
| 130 |
score5_[ii] = v; |
| 131 |
} |
| 132 |
score4 = _mm_loadu_ps(&score5_[1]); |
| 133 |
alpha.Set4(i, j, score4); |
| 134 |
|
| 135 |
// Update score, potentialNewMax |
| 136 |
float potentialNewMax = *std::max_element(score5_ + 1, score5_ + 5); |
| 137 |
score = *std::min_element(score5_ + 1, score5_ + 5); |
| 138 |
|
| 139 |
if (potentialNewMax > maxScore) { |
| 140 |
maxScore = potentialNewMax; |
| 141 |
thresholdScore = maxScore - this->bandingOptions_.ScoreDiff; |
| 142 |
} |
| 143 |
} |
| 144 |
|
| 145 |
endRow = i; |
| 146 |
alpha.FinishEditingColumn(j, beginRow, endRow); |
| 147 |
|
| 148 |
// Now, revise the hints to tell the caller where the mass of the |
| 149 |
// distribution really lived in this column. |
| 150 |
hintEndRow = endRow; |
| 151 |
for (i = beginRow; i < endRow && alpha(i, j) < thresholdScore; ++i) |
| 152 |
; |
| 153 |
hintBeginRow = i; |
| 154 |
} |
| 155 |
} |
| 156 |
|
| 157 |
template <typename M, typename E, typename C> |
| 158 |
void SseRecursor<M, E, C>::FillBeta(const E& e, const M& guide, M& beta) const |
| 159 |
{ |
| 160 |
int I = e.ReadLength(); |
| 161 |
int J = e.TemplateLength(); |
| 162 |
|
| 163 |
assert(beta.Rows() == I + 1 && beta.Columns() == J + 1); |
| 164 |
assert(guide.IsNull() || (guide.Rows() == beta.Rows() && guide.Columns() == beta.Columns())); |
| 165 |
|
| 166 |
int hintBeginRow = I + 1, hintEndRow = I + 1; |
| 167 |
|
| 168 |
for (int j = J; j >= 0; --j) { |
| 169 |
this->RangeGuide(j, guide, beta, &hintBeginRow, &hintEndRow); |
| 170 |
|
| 171 |
int requiredBeginRow = max(0, hintBeginRow); |
| 172 |
|
| 173 |
float score = NEG_INF; |
| 174 |
float thresholdScore = NEG_INF; |
| 175 |
float maxScore = NEG_INF; |
| 176 |
|
| 177 |
beta.StartEditingColumn(j, hintBeginRow, hintEndRow); |
| 178 |
// |
| 179 |
// See comment in FillAlpha---we are doing the same thing here. |
| 180 |
// An initial non-SSE loop, terminating when a multiple of 4 |
| 181 |
// rows remain. |
| 182 |
// |
| 183 |
int i, beginRow, endRow = hintEndRow; |
| 184 |
for (i = endRow - 1; (i == I || (i + 1) % 4 != 0) && i >= 0; i--) { |
| 185 |
score = NEG_INF; |
| 186 |
|
| 187 |
// Start: |
| 188 |
if (i == I && j == J) { |
| 189 |
score = 0.0f; |
| 190 |
} |
| 191 |
// Inc |
| 192 |
if (i < I && j < J) { |
| 193 |
score = C::Combine(score, beta(i + 1, j + 1) + e.Inc(i, j)); |
| 194 |
} |
| 195 |
// Merge |
| 196 |
if ((this->movesAvailable_ & MERGE) && j < J - 1 && i < I) { |
| 197 |
score = C::Combine(score, beta(i + 1, j + 2) + e.Merge(i, j)); |
| 198 |
} |
| 199 |
// Delete |
| 200 |
if (j < J) { |
| 201 |
score = C::Combine(score, beta(i, j + 1) + e.Del(i, j)); |
| 202 |
} |
| 203 |
// Extra |
| 204 |
if (i < I) { |
| 205 |
score = C::Combine(score, beta(i + 1, j) + e.Extra(i, j)); |
| 206 |
} |
| 207 |
|
| 208 |
beta.Set(i, j, score); |
| 209 |
|
| 210 |
if (score > maxScore) { |
| 211 |
maxScore = score; |
| 212 |
thresholdScore = maxScore - this->bandingOptions_.ScoreDiff; |
| 213 |
} |
| 214 |
} |
| 215 |
// |
| 216 |
// SSE loop |
| 217 |
// |
| 218 |
i = i - 3; |
| 219 |
for (; i >= 0 && (score >= thresholdScore || i >= requiredBeginRow); i -= 4) { |
| 220 |
__m128 score4 = NEG_INF_4; |
| 221 |
|
| 222 |
// Incorporation: |
| 223 |
if (i < I && j < J) { |
| 224 |
score4 = C::Combine4(score4, beta.Get4(i + 1, j + 1) + e.Inc4(i, j)); |
| 225 |
} |
| 226 |
// Merge |
| 227 |
if ((this->movesAvailable_ & MERGE) && j < J - 1 && i < I) { |
| 228 |
score4 = C::Combine4(score4, beta.Get4(i + 1, j + 2) + e.Merge4(i, j)); |
| 229 |
} |
| 230 |
// Deletion: |
| 231 |
if (j < J) { |
| 232 |
score4 = C::Combine4(score4, beta.Get4(i, j + 1) + e.Del4(i, j)); |
| 233 |
} |
| 234 |
|
| 235 |
// |
| 236 |
// Extra (non-SSE cascade) |
| 237 |
// |
| 238 |
float insScores4_[4], score5_[5]; |
| 239 |
|
| 240 |
__m128 insScores4 = e.Extra4(i, j); |
| 241 |
_mm_storeu_ps(insScores4_, insScores4); |
| 242 |
|
| 243 |
score5_[4] = beta.Get(i + 4, j); |
| 244 |
_mm_storeu_ps(score5_, score4); |
| 245 |
|
| 246 |
for (int ii = 3; ii >= 0; ii--) { |
| 247 |
float v = C::Combine(score5_[ii], score5_[ii + 1] + insScores4_[ii]); |
| 248 |
score5_[ii] = v; |
| 249 |
} |
| 250 |
score4 = _mm_loadu_ps(score5_); |
| 251 |
beta.Set4(i, j, score4); |
| 252 |
|
| 253 |
// Update score, potentialNewMax |
| 254 |
float potentialNewMax = *std::max_element(score5_, score5_ + 4); |
| 255 |
score = *std::min_element(score5_, score5_ + 4); |
| 256 |
|
| 257 |
if (potentialNewMax > maxScore) { |
| 258 |
maxScore = potentialNewMax; |
| 259 |
thresholdScore = maxScore - this->bandingOptions_.ScoreDiff; |
| 260 |
} |
| 261 |
} |
| 262 |
|
| 263 |
beginRow = i + 4; |
| 264 |
beta.FinishEditingColumn(j, beginRow, endRow); |
| 265 |
|
| 266 |
// Now, revise the hints to tell the caller where the mass of the |
| 267 |
// distribution really lived in this column. |
| 268 |
hintBeginRow = beginRow; |
| 269 |
for (i = endRow; i > beginRow && beta(i - 1, j) < thresholdScore; i--) |
| 270 |
; |
| 271 |
hintEndRow = i; |
| 272 |
} |
| 273 |
} |
| 274 |
|
| 275 |
template <typename M, typename E, typename C> |
| 276 |
INLINE_CALLEES float SseRecursor<M, E, C>::LinkAlphaBeta(const E& e, const M& alpha, |
| 277 |
int alphaColumn, const M& beta, |
| 278 |
int betaColumn, int absoluteColumn) const |
| 279 |
{ |
| 280 |
const int I = e.ReadLength(); |
| 281 |
|
| 282 |
assert(alphaColumn > 1 && absoluteColumn > 1); |
| 283 |
assert(absoluteColumn < e.TemplateLength()); |
| 284 |
|
| 285 |
int usedBegin, usedEnd; |
| 286 |
boost::tie(usedBegin, usedEnd) = |
| 287 |
RangeUnion(alpha.UsedRowRange(alphaColumn - 2), alpha.UsedRowRange(alphaColumn - 1), |
| 288 |
beta.UsedRowRange(betaColumn), beta.UsedRowRange(betaColumn + 1)); |
| 289 |
|
| 290 |
float v = NEG_INF; |
| 291 |
__m128 v4 = NEG_INF_4; |
| 292 |
|
| 293 |
// SSE loop |
| 294 |
int i; |
| 295 |
for (i = usedBegin; i < usedEnd - 4; i += 4) { |
| 296 |
// Incorporate |
| 297 |
v4 = C::Combine4(v4, alpha.Get4(i, alphaColumn - 1) + e.Inc4(i, absoluteColumn - 1) + |
| 298 |
beta.Get4(i + 1, betaColumn)); |
| 299 |
// Merge (2 possible ways): |
| 300 |
if (this->movesAvailable_ & MERGE) { |
| 301 |
v4 = C::Combine4(v4, alpha.Get4(i, alphaColumn - 2) + e.Merge4(i, absoluteColumn - 2) + |
| 302 |
beta.Get4(i + 1, betaColumn)); |
| 303 |
v4 = C::Combine4(v4, alpha.Get4(i, alphaColumn - 1) + e.Merge4(i, absoluteColumn - 1) + |
| 304 |
beta.Get4(i + 1, betaColumn + 1)); |
| 305 |
} |
| 306 |
// Delete |
| 307 |
v4 = C::Combine4(v4, alpha.Get4(i, alphaColumn - 1) + e.Del4(i, absoluteColumn - 1) + |
| 308 |
beta.Get4(i, betaColumn)); |
| 309 |
} |
| 310 |
// Handle the remaining rows non-SSE |
| 311 |
for (; i < usedEnd; i++) { |
| 312 |
if (i < I) { |
| 313 |
// Incorporate |
| 314 |
v = C::Combine(v, alpha(i, alphaColumn - 1) + e.Inc(i, absoluteColumn - 1) + |
| 315 |
beta(i + 1, betaColumn)); |
| 316 |
// Merge (2 possible ways): |
| 317 |
if (this->movesAvailable_ & MERGE) { |
| 318 |
v = C::Combine(v, alpha(i, alphaColumn - 2) + e.Merge(i, absoluteColumn - 2) + |
| 319 |
beta(i + 1, betaColumn)); |
| 320 |
v = C::Combine(v, alpha(i, alphaColumn - 1) + e.Merge(i, absoluteColumn - 1) + |
| 321 |
beta(i + 1, betaColumn + 1)); |
| 322 |
} |
| 323 |
} |
| 324 |
// Delete: |
| 325 |
v = C::Combine( |
| 326 |
v, alpha(i, alphaColumn - 1) + e.Del(i, absoluteColumn - 1) + beta(i, betaColumn)); |
| 327 |
} |
| 328 |
// Combine v4 and v |
| 329 |
float v_array[5]; |
| 330 |
_mm_storeu_ps(v_array, v4); |
| 331 |
v_array[4] = v; |
| 332 |
v = std::accumulate(v_array, v_array + 5, NEG_INF, C::Combine); |
| 333 |
return v; |
| 334 |
} |
| 335 |
|
| 336 |
template <typename M, typename E, typename C> |
| 337 |
INLINE_CALLEES void SseRecursor<M, E, C>::ExtendAlpha(const E& e, const M& alpha, int beginColumn, |
| 338 |
M& ext, int numExtColumns) const |
| 339 |
{ |
| 340 |
assert(numExtColumns >= 2); |
| 341 |
assert(alpha.Rows() == e.ReadLength() + 1 && ext.Rows() == e.ReadLength() + 1); |
| 342 |
|
| 343 |
// The new template may not be the same length as the old template. |
| 344 |
// Just make sure that we have anough room to fill out the extend buffer |
| 345 |
assert(beginColumn + 1 < e.TemplateLength() + 1); |
| 346 |
assert(ext.Columns() >= numExtColumns); |
| 347 |
assert(beginColumn >= 2); |
| 348 |
|
| 349 |
for (int extCol = 0; extCol < numExtColumns; extCol++) { |
| 350 |
int j = beginColumn + extCol; |
| 351 |
int beginRow, endRow; |
| 352 |
|
| 353 |
// |
| 354 |
// If this extend is contained within the column bounds of |
| 355 |
// the original alpha, we use the row range that was |
| 356 |
// previously determined. Otherwise start at alpha's last |
| 357 |
// UsedRow beginRow and go to the end. |
| 358 |
// |
| 359 |
if (j < alpha.Columns()) { |
| 360 |
boost::tie(beginRow, endRow) = alpha.UsedRowRange(j); |
| 361 |
} else { |
| 362 |
beginRow = alpha.UsedRowRange(alpha.Columns() - 1).Begin; |
| 363 |
endRow = alpha.Rows(); |
| 364 |
} |
| 365 |
|
| 366 |
ext.StartEditingColumn(extCol, beginRow, endRow); |
| 367 |
int i; |
| 368 |
// Handle the first rows non-SSE, leaving a multiple of 4 |
| 369 |
// entries to be handed off to the SSE loop. Need to always |
| 370 |
// handle at least row 0 this way, so that we don't have |
| 371 |
// to check for (i > 0) in the SSE loop. |
| 372 |
for (i = beginRow; (i == 0 || (endRow - i) % 4 != 0) && i < endRow; i++) { |
| 373 |
float prev, score = NEG_INF; |
| 374 |
if (i > 0) { |
| 375 |
// Inc |
| 376 |
prev = (extCol == 0 ? alpha(i - 1, j - 1) : ext(i - 1, extCol - 1)); |
| 377 |
score = C::Combine(score, prev + e.Inc(i - 1, j - 1)); |
| 378 |
|
| 379 |
// Extra |
| 380 |
prev = ext(i - 1, extCol); |
| 381 |
score = C::Combine(score, prev + e.Extra(i - 1, j)); |
| 382 |
|
| 383 |
// Merge |
| 384 |
if (this->movesAvailable_ & MERGE) { |
| 385 |
prev = alpha(i - 1, j - 2); |
| 386 |
score = C::Combine(score, prev + e.Merge(i - 1, j - 2)); |
| 387 |
} |
| 388 |
} |
| 389 |
// Delete |
| 390 |
prev = (extCol == 0 ? alpha(i, j - 1) : ext(i, extCol - 1)); |
| 391 |
score = C::Combine(score, prev + e.Del(i, j - 1)); |
| 392 |
ext.Set(i, extCol, score); |
| 393 |
} |
| 394 |
for (; i < endRow - 3; i += 4) { |
| 395 |
__m128 prev4, score4 = NEG_INF_4; |
| 396 |
|
| 397 |
// Incorporation: |
| 398 |
prev4 = (extCol == 0 ? alpha.Get4(i - 1, j - 1) : ext.Get4(i - 1, extCol - 1)); |
| 399 |
score4 = C::Combine4(score4, prev4 + e.Inc4(i - 1, j - 1)); |
| 400 |
|
| 401 |
// Merge |
| 402 |
if ((this->movesAvailable_ & MERGE) && j >= 2) { |
| 403 |
prev4 = alpha.Get4(i - 1, j - 2); |
| 404 |
score4 = C::Combine4(score4, prev4 + e.Merge4(i - 1, j - 2)); |
| 405 |
} |
| 406 |
|
| 407 |
// Deletion: |
| 408 |
prev4 = (extCol == 0 ? alpha.Get4(i, j - 1) : ext.Get4(i, extCol - 1)); |
| 409 |
score4 = C::Combine4(score4, prev4 + e.Del4(i, j - 1)); |
| 410 |
|
| 411 |
// Extras: |
| 412 |
float insScores4_[4], score5_[5]; |
| 413 |
|
| 414 |
__m128 insScores4 = e.Extra4(i - 1, j); |
| 415 |
_mm_storeu_ps(insScores4_, insScores4); |
| 416 |
|
| 417 |
score5_[0] = ext.Get(i - 1, extCol); |
| 418 |
_mm_storeu_ps(&score5_[1], score4); |
| 419 |
|
| 420 |
for (int ii = 1; ii < 5; ii++) { |
| 421 |
float v = C::Combine(score5_[ii], score5_[ii - 1] + insScores4_[ii - 1]); |
| 422 |
score5_[ii] = v; |
| 423 |
} |
| 424 |
score4 = _mm_loadu_ps(&score5_[1]); |
| 425 |
ext.Set4(i, extCol, score4); |
| 426 |
} |
| 427 |
assert(i == endRow); |
| 428 |
|
| 429 |
ext.FinishEditingColumn(extCol, beginRow, endRow); |
| 430 |
} |
| 431 |
} |
| 432 |
|
| 433 |
template <typename M, typename E, typename C> |
| 434 |
void SseRecursor<M, E, C>::ExtendBeta(const E& e, const M& beta, int endColumn, M& ext, |
| 435 |
int numExtColumns, int lengthDiff) const |
| 436 |
{ |
| 437 |
simpleRecursor_.ExtendBeta(e, beta, endColumn, ext, numExtColumns, lengthDiff); |
| 438 |
} |
| 439 |
|
| 440 |
template <typename M, typename E, typename C> |
| 441 |
SseRecursor<M, E, C>::SseRecursor(int movesAvailable, const BandingOptions& banding) |
| 442 |
: detail::RecursorBase<M, E, C>(movesAvailable, banding) |
| 443 |
, simpleRecursor_(movesAvailable, banding) |
| 444 |
{ |
| 445 |
} |
| 446 |
|
| 447 |
template class SseRecursor<DenseMatrix, QvEvaluator, detail::ViterbiCombiner>; |
| 448 |
template class SseRecursor<SparseMatrix, QvEvaluator, detail::ViterbiCombiner>; |
| 449 |
template class SseRecursor<SparseMatrix, QvEvaluator, detail::SumProductCombiner>; |
| 450 |
template class SseRecursor<SparseMatrix, EdnaEvaluator, detail::SumProductCombiner>; |
| 451 |
} |