| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
#ifndef _KXMER_SET_ |
| 11 |
#define _KXMER_SET_ |
| 12 |
#include "defs.h" |
| 13 |
#include <tuple> |
| 14 |
#include <queue> |
| 15 |
#include "exception_aware_thread.h" |
| 16 |
|
| 17 |
using namespace std; |
| 18 |
|
| 19 |
#define KXMER_SET_SIZE 1024 |
| 20 |
|
| 21 |
#define MAX_FOR_X_3 112 |
| 22 |
|
| 23 |
template <unsigned SIZE> |
| 24 |
class CKXmerSet; |
| 25 |
|
| 26 |
template<bool> struct Range; |
| 27 |
|
| 28 |
template<unsigned SIZE, unsigned PARENT, typename = Range<true> > |
| 29 |
struct ParentFinder |
| 30 |
{ |
| 31 |
static uint32 Execute(CKXmerSet<SIZE>& ptr, const CKmer<SIZE>& kmer); |
| 32 |
}; |
| 33 |
|
| 34 |
template<unsigned SIZE, unsigned PARENT> |
| 35 |
struct ParentFinder<SIZE, PARENT, Range<(PARENT < 56)> > |
| 36 |
{ |
| 37 |
FORCE_INLINE static uint32 Execute(CKXmerSet<SIZE>& ptr, const CKmer<SIZE>& kmer) |
| 38 |
{ |
| 39 |
if (ptr.data[PARENT * 2].first < ptr.data[PARENT * 2 + 1].first) |
| 40 |
{ |
| 41 |
if (ptr.data[PARENT * 2].first < kmer) |
| 42 |
{ |
| 43 |
ptr.data[PARENT] = ptr.data[PARENT * 2]; |
| 44 |
return ParentFinder<SIZE, PARENT * 2>::Execute(ptr, kmer); |
| 45 |
} |
| 46 |
} |
| 47 |
else |
| 48 |
{ |
| 49 |
if (ptr.data[PARENT * 2 + 1].first < kmer) |
| 50 |
{ |
| 51 |
ptr.data[PARENT] = ptr.data[PARENT * 2 + 1]; |
| 52 |
return ParentFinder<SIZE, PARENT * 2 + 1>::Execute(ptr, kmer); |
| 53 |
} |
| 54 |
} |
| 55 |
return PARENT; |
| 56 |
} |
| 57 |
}; |
| 58 |
|
| 59 |
template<unsigned SIZE, unsigned PARENT> |
| 60 |
struct ParentFinder<SIZE, PARENT, Range<(PARENT == 56)> > |
| 61 |
{ |
| 62 |
FORCE_INLINE static uint32 Execute(CKXmerSet<SIZE>& ptr, const CKmer<SIZE>& kmer) |
| 63 |
{ |
| 64 |
if (ptr.data[PARENT * 2].first < kmer) |
| 65 |
{ |
| 66 |
ptr.data[PARENT] = ptr.data[PARENT * 2]; |
| 67 |
return PARENT * 2; |
| 68 |
} |
| 69 |
return PARENT; |
| 70 |
} |
| 71 |
}; |
| 72 |
|
| 73 |
template<unsigned SIZE, unsigned PARENT> |
| 74 |
struct ParentFinder<SIZE, PARENT, Range<(PARENT > 56)> > |
| 75 |
{ |
| 76 |
FORCE_INLINE static uint32 Execute(CKXmerSet<SIZE>& , const CKmer<SIZE>& ) |
| 77 |
{ |
| 78 |
return PARENT; |
| 79 |
} |
| 80 |
}; |
| 81 |
|
| 82 |
template <unsigned SIZE> |
| 83 |
class CKXmerSet |
| 84 |
{ |
| 85 |
typedef tuple<uint64, uint64, uint32> elem_desc_t; |
| 86 |
typedef pair<CKmer<SIZE>, uint32> heap_elem_t; |
| 87 |
elem_desc_t data_desc[KXMER_SET_SIZE]; |
| 88 |
|
| 89 |
public: |
| 90 |
heap_elem_t data[KXMER_SET_SIZE]; |
| 91 |
private: |
| 92 |
uint32 pos; |
| 93 |
uint32 desc_pos; |
| 94 |
CKmer<SIZE> mask; |
| 95 |
CKmer<SIZE>* buffer; |
| 96 |
|
| 97 |
inline void update_heap() |
| 98 |
{ |
| 99 |
uint32 desc_id = data[1].second; |
| 100 |
CKmer<SIZE> kmer; |
| 101 |
if (++get<0>(data_desc[desc_id]) < get<1>(data_desc[desc_id])) |
| 102 |
{ |
| 103 |
kmer.from_kxmer(buffer[get<0>(data_desc[desc_id])], get<2>(data_desc[desc_id]), mask); |
| 104 |
} |
| 105 |
else |
| 106 |
{ |
| 107 |
kmer.set(data[--pos].first); |
| 108 |
desc_id = data[pos].second; |
| 109 |
data[pos].first.fill_T(); |
| 110 |
} |
| 111 |
|
| 112 |
uint32 parent = ParentFinder<SIZE, 1>::Execute(*this, kmer); |
| 113 |
|
| 114 |
data[parent] = make_pair(kmer, desc_id); |
| 115 |
} |
| 116 |
|
| 117 |
public: |
| 118 |
CKXmerSet(uint32 kmer_len) |
| 119 |
{ |
| 120 |
pos = 1; |
| 121 |
mask.set_n_1(kmer_len * 2); |
| 122 |
desc_pos = 0; |
| 123 |
} |
| 124 |
|
| 125 |
inline void init_add(uint64 start_pos, uint64 end_pos, uint32 shr) |
| 126 |
{ |
| 127 |
data_desc[desc_pos] = make_tuple(start_pos, end_pos, shr); |
| 128 |
data[pos].first.from_kxmer(buffer[start_pos], shr, mask); |
| 129 |
data[pos].second = desc_pos; |
| 130 |
uint32 child_pos = pos++; |
| 131 |
|
| 132 |
while (child_pos > 1 && data[child_pos].first < data[child_pos / 2].first) |
| 133 |
{ |
| 134 |
swap(data[child_pos], data[child_pos / 2]); |
| 135 |
child_pos /= 2; |
| 136 |
} |
| 137 |
++desc_pos; |
| 138 |
} |
| 139 |
inline void set_buffer(CKmer<SIZE>* _buffer) |
| 140 |
{ |
| 141 |
buffer = _buffer; |
| 142 |
} |
| 143 |
inline void clear() |
| 144 |
{ |
| 145 |
pos = 1; |
| 146 |
desc_pos = 0; |
| 147 |
for (uint32 i = 0; i < KXMER_SET_SIZE; ++i) |
| 148 |
{ |
| 149 |
data[i].first.fill_T(); |
| 150 |
data[i].second = -1; |
| 151 |
} |
| 152 |
} |
| 153 |
|
| 154 |
inline bool get_min(uint64& _pos, CKmer<SIZE>& kmer) |
| 155 |
{ |
| 156 |
if (pos <= 1) |
| 157 |
return false; |
| 158 |
kmer = data[1].first; |
| 159 |
_pos = get<0>(data_desc[data[1].second]); |
| 160 |
update_heap(); |
| 161 |
|
| 162 |
return true; |
| 163 |
} |
| 164 |
}; |
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
struct SubArrayDesc |
| 169 |
{ |
| 170 |
uint64 start, end; |
| 171 |
uint32 shr; |
| 172 |
uint64 counters_sum; |
| 173 |
}; |
| 174 |
|
| 175 |
template<unsigned SIZE> |
| 176 |
class CSubArrayDescGenerator |
| 177 |
{ |
| 178 |
uint32 kmer_len; |
| 179 |
uint32 n_parts; |
| 180 |
uint32 parts_left; |
| 181 |
const vector<SubArrayDesc>& sub_array_descs; |
| 182 |
queue<vector<SubArrayDesc>> data; |
| 183 |
CKmer<SIZE>* buffer; |
| 184 |
mutable std::mutex mtx; |
| 185 |
uint64 out_start = 0; |
| 186 |
uint32 cutoff_min; |
| 187 |
uint32 rec_len; |
| 188 |
vector<uint64> cumsum; |
| 189 |
uint32* kxmer_counters; |
| 190 |
uint64 n_kxmer_counters; |
| 191 |
uint32 n_threads; |
| 192 |
public: |
| 193 |
CSubArrayDescGenerator(uint32 kmer_len, uint32 n_parts, const vector<SubArrayDesc>& sub_array_descs, CKmer<SIZE>* buffer, uint32 cutoff_min, uint32 rec_len, uint32* kxmer_counters, uint64 n_kxmer_counters, uint32 n_threads) : |
| 194 |
kmer_len(kmer_len), |
| 195 |
n_parts(n_parts), |
| 196 |
parts_left(n_parts), |
| 197 |
sub_array_descs(sub_array_descs), |
| 198 |
buffer(buffer), |
| 199 |
cutoff_min(cutoff_min), |
| 200 |
rec_len(rec_len), |
| 201 |
kxmer_counters(kxmer_counters), |
| 202 |
n_kxmer_counters(n_kxmer_counters), |
| 203 |
n_threads(n_threads) |
| 204 |
{ |
| 205 |
|
| 206 |
cumsum.resize(n_kxmer_counters / COMPACT_CUMSUM_PART_SIZE + 1); |
| 207 |
vector<thread> threads; |
| 208 |
uint64 per_thread = (n_kxmer_counters / n_threads / COMPACT_CUMSUM_PART_SIZE + 1) * COMPACT_CUMSUM_PART_SIZE; |
| 209 |
for (uint32 th_id = 0; th_id < n_threads; ++th_id) |
| 210 |
{ |
| 211 |
threads.emplace_back([&, th_id]() |
| 212 |
{ |
| 213 |
uint64 start = th_id * per_thread; |
| 214 |
uint64 end = (th_id + 1) * per_thread; |
| 215 |
if (th_id == n_threads - 1) |
| 216 |
end = n_kxmer_counters; |
| 217 |
if (end > n_kxmer_counters) |
| 218 |
end = n_kxmer_counters; |
| 219 |
uint64 sum = 0; |
| 220 |
|
| 221 |
for (uint64 i = start; i < end; ++i) |
| 222 |
{ |
| 223 |
sum += kxmer_counters[i]; |
| 224 |
cumsum[i / COMPACT_CUMSUM_PART_SIZE] = sum; |
| 225 |
} |
| 226 |
}); |
| 227 |
} |
| 228 |
for (auto& t : threads) |
| 229 |
t.join(); |
| 230 |
uint64 part_size = per_thread / COMPACT_CUMSUM_PART_SIZE; |
| 231 |
for (uint32 i = 1; i < n_threads && i*part_size - 1 < cumsum.size(); ++i) |
| 232 |
{ |
| 233 |
uint64 to_add = cumsum[i*part_size - 1]; |
| 234 |
for (uint64 j = i * part_size; j < (i + 1)*part_size&& j < cumsum.size(); ++j) |
| 235 |
cumsum[j] += to_add; |
| 236 |
} |
| 237 |
|
| 238 |
|
| 239 |
uint32 biggest_id = 0; |
| 240 |
for (uint32 i = 0; i < sub_array_descs.size(); ++i) |
| 241 |
{ |
| 242 |
if (sub_array_descs[i].end - sub_array_descs[i].start > sub_array_descs[biggest_id].end - sub_array_descs[biggest_id].start) |
| 243 |
biggest_id = i; |
| 244 |
} |
| 245 |
|
| 246 |
uint64 start_in_biggest = 0; |
| 247 |
|
| 248 |
vector<SubArrayDesc> sub_array_desc_copy(sub_array_descs.begin(), sub_array_descs.end()); |
| 249 |
|
| 250 |
CKmer<SIZE> mask; |
| 251 |
mask.set_n_1(kmer_len * 2); |
| 252 |
while (parts_left > 1) |
| 253 |
{ |
| 254 |
uint64 end_in_biggest = (sub_array_desc_copy[biggest_id].end - sub_array_desc_copy[biggest_id].start - start_in_biggest) / parts_left + sub_array_desc_copy[biggest_id].start; |
| 255 |
CKmer<SIZE> kmer; |
| 256 |
kmer.from_kxmer(buffer[end_in_biggest], sub_array_desc_copy[biggest_id].shr, mask); |
| 257 |
|
| 258 |
vector<SubArrayDesc> current; |
| 259 |
for (auto& e : sub_array_desc_copy) |
| 260 |
{ |
| 261 |
uint32 shr = e.shr; |
| 262 |
uint64 new_end = std::lower_bound(buffer + e.start , buffer + e.end, kmer, [shr, mask](const CKmer<SIZE>& k1, const CKmer<SIZE>& k2) |
| 263 |
{ |
| 264 |
CKmer<SIZE> val; |
| 265 |
val.from_kxmer(k1, shr, mask); |
| 266 |
return val < k2; |
| 267 |
}) - buffer; |
| 268 |
current.push_back({ e.start, new_end, shr, 0 }); |
| 269 |
e.start = new_end; |
| 270 |
} |
| 271 |
|
| 272 |
|
| 273 |
for (auto& elem : current) |
| 274 |
elem.counters_sum = GetCumSum(elem.end) - GetCumSum(elem.start); |
| 275 |
data.push(move(current)); |
| 276 |
--parts_left; |
| 277 |
} |
| 278 |
|
| 279 |
|
| 280 |
for (auto& elem : sub_array_desc_copy) |
| 281 |
elem.counters_sum = GetCumSum(elem.end) - GetCumSum(elem.start); |
| 282 |
data.push(move(sub_array_desc_copy)); |
| 283 |
} |
| 284 |
|
| 285 |
|
| 286 |
uint64 GetCumSum(uint64 pos) |
| 287 |
{ |
| 288 |
if (pos == 0) |
| 289 |
return 0; |
| 290 |
--pos; |
| 291 |
uint64 res = 0; |
| 292 |
if (pos / COMPACT_CUMSUM_PART_SIZE > 0) |
| 293 |
res = cumsum[pos / COMPACT_CUMSUM_PART_SIZE - 1]; |
| 294 |
for (uint64 i = pos / COMPACT_CUMSUM_PART_SIZE * COMPACT_CUMSUM_PART_SIZE; i <= pos; ++i) |
| 295 |
res += kxmer_counters[i]; |
| 296 |
return res; |
| 297 |
} |
| 298 |
|
| 299 |
bool GetNext(vector<SubArrayDesc>& desc, uint64& _out_start) |
| 300 |
{ |
| 301 |
std::lock_guard<std::mutex> lck(mtx); |
| 302 |
if (data.empty()) |
| 303 |
return false; |
| 304 |
desc = move(data.front()); |
| 305 |
data.pop(); |
| 306 |
_out_start = out_start; |
| 307 |
|
| 308 |
uint64 n_recs = 0; |
| 309 |
for (auto& e : desc) |
| 310 |
n_recs += e.counters_sum; |
| 311 |
out_start += ((n_recs) / MAX(cutoff_min, 1u)) * rec_len; |
| 312 |
|
| 313 |
return true; |
| 314 |
} |
| 315 |
}; |
| 316 |
|
| 317 |
class CLutUpdater |
| 318 |
{ |
| 319 |
uint64* lut; |
| 320 |
std::mutex mtx; |
| 321 |
public: |
| 322 |
CLutUpdater(uint64* lut) :lut(lut) |
| 323 |
{ |
| 324 |
} |
| 325 |
void UpdateLut(uint64 prefix, uint64 val) |
| 326 |
{ |
| 327 |
std::lock_guard<std::mutex> lck(mtx); |
| 328 |
lut[prefix] += val; |
| 329 |
} |
| 330 |
}; |
| 331 |
|
| 332 |
template<unsigned SIZE> |
| 333 |
class CKXmerMerger |
| 334 |
{ |
| 335 |
const vector<SubArrayDesc>& sub_array_descs; |
| 336 |
CSubArrayDescGenerator<SIZE>& sub_array_desc_generator; |
| 337 |
CLutUpdater& lut_updater; |
| 338 |
uint64 n_total = 0; |
| 339 |
uint64 n_unique = 0; |
| 340 |
uint64 n_cutoff_min = 0; |
| 341 |
uint64 n_cutoff_max = 0; |
| 342 |
|
| 343 |
CKmer<SIZE>* buffer = nullptr; |
| 344 |
uint32* kxmer_counters = nullptr; |
| 345 |
uint32 cutoff_min; |
| 346 |
uint32 cutoff_max; |
| 347 |
uint32 counter_max; |
| 348 |
uint32 kmer_len; |
| 349 |
CKXmerSet<SIZE> kxmer_set; |
| 350 |
uint64* lut; |
| 351 |
uint32 counter_size; |
| 352 |
int32 lut_prefix_len; |
| 353 |
uchar* out_buffer; |
| 354 |
bool without_output; |
| 355 |
OutputType output_type; |
| 356 |
|
| 357 |
list<pair<uint64, uint64>> packs; |
| 358 |
public: |
| 359 |
|
| 360 |
void GetStats(uint64& _n_unique, uint64& _n_cutoff_min, uint64& _n_cutoff_max, uint64& _n_total) |
| 361 |
{ |
| 362 |
_n_unique = n_unique; |
| 363 |
_n_cutoff_min = n_cutoff_min; |
| 364 |
_n_cutoff_max = n_cutoff_max; |
| 365 |
_n_total = n_total; |
| 366 |
} |
| 367 |
|
| 368 |
void Process(vector<SubArrayDesc>& desc, uint64 out_start) |
| 369 |
{ |
| 370 |
kxmer_set.clear(); |
| 371 |
kxmer_set.set_buffer(buffer); |
| 372 |
|
| 373 |
CKmer<SIZE> mask; |
| 374 |
mask.set_n_1(kmer_len * 2); |
| 375 |
|
| 376 |
uint64 last_prefix = 0; |
| 377 |
uint64 last_prefix_n_recs = 0; |
| 378 |
uint64 first_prefix = 1ull << 2 * lut_prefix_len; |
| 379 |
uint64 first_prefix_n_recs = 0; |
| 380 |
|
| 381 |
uint32 suffix_len_bits = (kmer_len - lut_prefix_len) * 2; |
| 382 |
uint64 kmer_bytes = suffix_len_bits / 8; |
| 383 |
if (lut_prefix_len == 0) |
| 384 |
kmer_bytes = (kmer_len + 3) / 4; |
| 385 |
|
| 386 |
CKmer<SIZE> candidate_min, candidate_max; |
| 387 |
for (auto &d : desc) |
| 388 |
{ |
| 389 |
if (d.end > d.start) |
| 390 |
{ |
| 391 |
kxmer_set.init_add(d.start, d.end, d.shr); |
| 392 |
candidate_min.from_kxmer(buffer[d.start], d.shr, mask); |
| 393 |
candidate_max.from_kxmer(buffer[d.end - 1], d.shr, mask); |
| 394 |
|
| 395 |
uint64 candidate_min_prefix = candidate_min.remove_suffix(suffix_len_bits); |
| 396 |
uint64 candidate_max_prefix = candidate_max.remove_suffix(suffix_len_bits); |
| 397 |
|
| 398 |
if (candidate_max_prefix > last_prefix) |
| 399 |
last_prefix = candidate_max_prefix; |
| 400 |
|
| 401 |
if (candidate_min_prefix < first_prefix) |
| 402 |
first_prefix = candidate_min_prefix; |
| 403 |
} |
| 404 |
} |
| 405 |
uint64 counter_pos = 0; |
| 406 |
|
| 407 |
CKmer<SIZE> kmer, next_kmer; |
| 408 |
kmer.clear(); |
| 409 |
next_kmer.clear(); |
| 410 |
uint32 count; |
| 411 |
|
| 412 |
|
| 413 |
uint64 out_pos = out_start; |
| 414 |
|
| 415 |
|
| 416 |
if (kxmer_set.get_min(counter_pos, kmer)) |
| 417 |
{ |
| 418 |
count = kxmer_counters[counter_pos]; |
| 419 |
|
| 420 |
|
| 421 |
while (kxmer_set.get_min(counter_pos, next_kmer)) |
| 422 |
{ |
| 423 |
if (kmer == next_kmer) |
| 424 |
count += kxmer_counters[counter_pos]; |
| 425 |
else |
| 426 |
{ |
| 427 |
n_total += count; |
| 428 |
++n_unique; |
| 429 |
if (count < cutoff_min) |
| 430 |
n_cutoff_min++; |
| 431 |
else if (count > cutoff_max) |
| 432 |
n_cutoff_max++; |
| 433 |
else |
| 434 |
{ |
| 435 |
if (count > counter_max) |
| 436 |
count = counter_max; |
| 437 |
|
| 438 |
if (!without_output) |
| 439 |
{ |
| 440 |
if (output_type == OutputType::KMC) |
| 441 |
{ |
| 442 |
uint64 prefix = kmer.remove_suffix(suffix_len_bits); |
| 443 |
if (prefix == last_prefix) |
| 444 |
++last_prefix_n_recs; |
| 445 |
else if (prefix == first_prefix) |
| 446 |
++first_prefix_n_recs; |
| 447 |
else |
| 448 |
++lut[prefix]; |
| 449 |
|
| 450 |
for (int32 j = (int32)kmer_bytes - 1; j >= 0; --j) |
| 451 |
out_buffer[out_pos++] = kmer.get_byte(j); |
| 452 |
for (int32 j = 0; j < (int32)counter_size; ++j) |
| 453 |
out_buffer[out_pos++] = (count >> (j * 8)) & 0xFF; |
| 454 |
} |
| 455 |
else if (output_type == OutputType::KFF) |
| 456 |
{ |
| 457 |
for (int32 j = (int32)kmer_bytes - 1; j >= 0; --j) |
| 458 |
out_buffer[out_pos++] = kmer.get_byte(j); |
| 459 |
|
| 460 |
for (int32 j = (int32)counter_size - 1; j >= 0; --j) |
| 461 |
out_buffer[out_pos++] = (count >> (j * 8)) & 0xFF; |
| 462 |
} |
| 463 |
else |
| 464 |
{ |
| 465 |
std::ostringstream ostr; |
| •466 |
ostr << "Error: not implemented, please contact authors showing this message" << __FILE__ << "\t" << __LINE__; |
| 467 |
CCriticalErrorHandler::Inst().HandleCriticalError(ostr.str()); |
| 468 |
} |
| 469 |
} |
| 470 |
} |
| 471 |
count = kxmer_counters[counter_pos]; |
| 472 |
kmer = next_kmer; |
| 473 |
} |
| 474 |
} |
| 475 |
|
| 476 |
++n_unique; |
| 477 |
n_total += count; |
| 478 |
if (count < cutoff_min) |
| 479 |
n_cutoff_min++; |
| 480 |
else if (count > cutoff_max) |
| 481 |
n_cutoff_max++; |
| 482 |
else |
| 483 |
{ |
| 484 |
if (count > counter_max) |
| 485 |
count = counter_max; |
| 486 |
|
| 487 |
if (!without_output) |
| 488 |
{ |
| 489 |
if (output_type == OutputType::KMC) |
| 490 |
{ |
| 491 |
uint64 prefix = kmer.remove_suffix(suffix_len_bits); |
| 492 |
if (prefix == last_prefix) |
| 493 |
++last_prefix_n_recs; |
| 494 |
else if (prefix == first_prefix) |
| 495 |
++first_prefix_n_recs; |
| 496 |
else |
| 497 |
++lut[prefix]; |
| 498 |
|
| 499 |
for (int32 j = (int32)kmer_bytes - 1; j >= 0; --j) |
| 500 |
out_buffer[out_pos++] = kmer.get_byte(j); |
| 501 |
for (int32 j = 0; j < (int32)counter_size; ++j) |
| 502 |
out_buffer[out_pos++] = (count >> (j * 8)) & 0xFF; |
| 503 |
} |
| 504 |
else if (output_type == OutputType::KFF) |
| 505 |
{ |
| 506 |
for (int32 j = (int32)kmer_bytes - 1; j >= 0; --j) |
| 507 |
out_buffer[out_pos++] = kmer.get_byte(j); |
| 508 |
|
| 509 |
for (int32 j = (int32)counter_size - 1; j >= 0; --j) |
| 510 |
out_buffer[out_pos++] = (count >> (j * 8)) & 0xFF; |
| 511 |
} |
| 512 |
else |
| 513 |
{ |
| 514 |
std::ostringstream ostr; |
| •515 |
ostr << "Error: not implemented, please contact authors showing this message" << __FILE__ << "\t" << __LINE__; |
| 516 |
CCriticalErrorHandler::Inst().HandleCriticalError(ostr.str()); |
| 517 |
} |
| 518 |
} |
| 519 |
} |
| 520 |
if (!without_output) |
| 521 |
{ |
| 522 |
if(output_type == OutputType::KMC) |
| 523 |
{ |
| 524 |
lut_updater.UpdateLut(last_prefix, last_prefix_n_recs); |
| 525 |
lut_updater.UpdateLut(first_prefix, first_prefix_n_recs); |
| 526 |
} |
| 527 |
packs.emplace_back(out_start, out_pos); |
| 528 |
} |
| 529 |
} |
| 530 |
} |
| 531 |
|
| 532 |
CKXmerMerger(const vector<SubArrayDesc>& sub_array_descs, |
| 533 |
CSubArrayDescGenerator<SIZE>& sub_array_desc_generator, |
| 534 |
CLutUpdater& lut_updater, |
| 535 |
CKmer<SIZE>* buffer, |
| 536 |
uint32* kxmer_counters, |
| 537 |
uint32 cutoff_min, |
| 538 |
uint32 cutoff_max, |
| 539 |
uint32 counter_max, |
| 540 |
uint32 kmer_len, |
| 541 |
uint64* lut, |
| 542 |
uint32 counter_size, |
| 543 |
int32 lut_prefix_len, |
| 544 |
uchar* out_buffer, |
| 545 |
bool without_output, |
| 546 |
OutputType output_type) |
| 547 |
: |
| 548 |
sub_array_descs(sub_array_descs), |
| 549 |
sub_array_desc_generator(sub_array_desc_generator), |
| 550 |
lut_updater(lut_updater), |
| 551 |
buffer(buffer), |
| 552 |
kxmer_counters(kxmer_counters), |
| 553 |
cutoff_min(cutoff_min), |
| 554 |
cutoff_max(cutoff_max), |
| 555 |
counter_max(counter_max), |
| 556 |
kmer_len(kmer_len), |
| 557 |
kxmer_set(kmer_len), |
| 558 |
lut(lut), |
| 559 |
counter_size(counter_size), |
| 560 |
lut_prefix_len(lut_prefix_len), |
| 561 |
out_buffer(out_buffer), |
| 562 |
without_output(without_output), |
| 563 |
output_type(output_type) |
| 564 |
{ |
| 565 |
|
| 566 |
} |
| 567 |
|
| 568 |
void operator()() |
| 569 |
{ |
| 570 |
vector<SubArrayDesc> desc; |
| 571 |
uint64 out_start; |
| 572 |
while (sub_array_desc_generator.GetNext(desc, out_start)) |
| 573 |
{ |
| 574 |
Process(desc, out_start); |
| 575 |
} |
| 576 |
} |
| 577 |
|
| 578 |
std::list<std::pair<uint64, uint64>>& GetPacks() |
| 579 |
{ |
| 580 |
return packs; |
| 581 |
} |
| 582 |
}; |
| 583 |
|
| 584 |
template<unsigned SIZE> |
| 585 |
class CKXmerSetMultiThreaded |
| 586 |
{ |
| 587 |
vector<SubArrayDesc> sub_array_descs; |
| 588 |
CKmer<SIZE>* buffer; |
| 589 |
uint32* kxmer_counters; |
| 590 |
uint64 n_kxmer_counters; |
| 591 |
uint32 cutoff_min; |
| 592 |
uint32 cutoff_max; |
| 593 |
uint32 counter_max; |
| 594 |
uint32 kmer_len; |
| 595 |
int32 lut_prefix_len; |
| 596 |
uint64* lut; |
| 597 |
uchar* out_buffer; |
| 598 |
uint32 n_threads = 0; |
| 599 |
|
| 600 |
uint64 n_unique = 0; |
| 601 |
uint64 n_cutoff_min = 0; |
| 602 |
uint64 n_cutoff_max = 0; |
| 603 |
uint64 n_total = 0; |
| 604 |
|
| 605 |
list<pair<uint64, uint64>> output_packs_desc; |
| 606 |
|
| 607 |
public: |
| 608 |
CKXmerSetMultiThreaded(CKmer<SIZE>* buffer, |
| 609 |
uint32* kxmer_counters, |
| 610 |
uint64 n_kxmer_counters, |
| 611 |
uint32 cutoff_min, |
| 612 |
uint32 cutoff_max, |
| 613 |
uint32 counter_max, |
| 614 |
uint32 kmer_len, |
| 615 |
int32 lut_prefix_len, |
| 616 |
uint64* lut, |
| 617 |
uchar* out_buffer, |
| 618 |
uint32 n_threads) |
| 619 |
: |
| 620 |
buffer(buffer), |
| 621 |
kxmer_counters(kxmer_counters), |
| 622 |
n_kxmer_counters(n_kxmer_counters), |
| 623 |
cutoff_min(cutoff_min), |
| 624 |
cutoff_max(cutoff_max), |
| 625 |
counter_max(counter_max), |
| 626 |
kmer_len(kmer_len), |
| 627 |
lut_prefix_len(lut_prefix_len), |
| 628 |
lut(lut), |
| 629 |
out_buffer(out_buffer), |
| 630 |
n_threads(n_threads) |
| 631 |
{ |
| 632 |
} |
| 633 |
void InitAdd(uint64 start, uint64 end, uint32 shr) |
| 634 |
{ |
| 635 |
sub_array_descs.emplace_back(SubArrayDesc{ start, end, shr, 0 }); |
| 636 |
} |
| 637 |
|
| 638 |
void Process(bool without_output, OutputType output_type) |
| 639 |
{ |
| 640 |
uint32 n_parts = 8 * n_threads; |
| 641 |
|
| 642 |
CLutUpdater lut_updater(lut); |
| 643 |
vector<CExceptionAwareThread> threads; |
| 644 |
vector<std::unique_ptr<CKXmerMerger<SIZE>>> mergers; |
| 645 |
uint32 counter_size = calc_counter_size(cutoff_max, counter_max); |
| 646 |
|
| 647 |
uint32 rec_len = (kmer_len - lut_prefix_len) / 4 + counter_size; |
| 648 |
|
| 649 |
CSubArrayDescGenerator<SIZE> sub_array_desc_generator(kmer_len, n_parts, sub_array_descs, buffer, cutoff_min, rec_len, kxmer_counters, n_kxmer_counters, n_threads); |
| 650 |
for (uint32 i = 0; i < n_threads; ++i) |
| 651 |
{ |
| 652 |
mergers.push_back(std::make_unique<CKXmerMerger<SIZE>>(sub_array_descs, sub_array_desc_generator, lut_updater, buffer, kxmer_counters, cutoff_min, |
| 653 |
cutoff_max, counter_max, kmer_len, lut, counter_size, lut_prefix_len, out_buffer, without_output, output_type)); |
| 654 |
threads.emplace_back(ref(*mergers.back())); |
| 655 |
} |
| 656 |
|
| 657 |
for (auto& t : threads) |
| 658 |
t.join(); |
| 659 |
|
| 660 |
uint64 tmp_n_unique = 0; |
| 661 |
uint64 tmp_n_cutoff_min = 0; |
| 662 |
uint64 tmp_n_cutoff_max = 0; |
| 663 |
uint64 tmp_n_total = 0; |
| 664 |
|
| 665 |
for (auto& ptr : mergers) |
| 666 |
{ |
| 667 |
ptr->GetStats(tmp_n_unique, tmp_n_cutoff_min, tmp_n_cutoff_max, tmp_n_total); |
| 668 |
output_packs_desc.splice(output_packs_desc.end(), move(ptr->GetPacks())); |
| 669 |
n_unique += tmp_n_unique; |
| 670 |
n_cutoff_min += tmp_n_cutoff_min; |
| 671 |
n_cutoff_max += tmp_n_cutoff_max; |
| 672 |
n_total += tmp_n_total; |
| 673 |
} |
| 674 |
|
| 675 |
for (auto& ptr : mergers) |
| 676 |
ptr.reset(); |
| 677 |
output_packs_desc.sort([](const pair<uint64, uint64>& e1, const pair<uint64, uint64>& e2){return e1.first < e2.first; }); |
| 678 |
} |
| 679 |
|
| 680 |
void GetStats(uint64& _n_unique, uint64& _n_cutoff_min, uint64& _n_cutoff_max, uint64& _n_total) |
| 681 |
{ |
| 682 |
_n_unique = n_unique; |
| 683 |
_n_cutoff_min = n_cutoff_min; |
| 684 |
_n_cutoff_max = n_cutoff_max; |
| 685 |
_n_total = n_total; |
| 686 |
} |
| 687 |
|
| 688 |
list<pair<uint64, uint64>>& GetOutputPacksDesc() |
| 689 |
{ |
| 690 |
return output_packs_desc; |
| 691 |
} |
| 692 |
}; |
| 693 |
|
| 694 |
#endif |
| 695 |
|
| •696 |
|