Commit fff3c75
authored
Refactor bulk parallelism (#221)
The use of atomic counter and termination signaling is slightly
non-obvious, and we use the same pattern in an ad-hoc way across both
simplex and tesseract mains. The goal is to re-use this also in the
python API, as an alternative to sinter.
The API looks like this:
```c++
size_t parallel_for_shots_in_order(size_t num_shots, size_t num_threads, ProcessShot&& process_shot,
ConsumeShot&& consume_shot)
```
here `process_shot` is called within a worker thread for each shot and
`consume_shot` is called on the main thread for each shot (this should
return a bool that signals termination when set to `false`).
Example usage:
```c++
std::vector<std::unique_ptr<Decoder>> decoders(args.num_threads);
std::vector<std::vector<size_t>> error_use_per_thread(
args.num_threads, std::vector<size_t>(num_error_terms));
std::vector<Result> results(shots.size());
size_t num_consumed = parallel_for_shots_in_order(
shots.size(),
args.num_threads,
// Process shot runs in parallel, potentially out of order.
[&](size_t thread_index, size_t shot_index) {
if (!decoders[thread_index]) {
decoders[thread_index] = std::make_unique<Decoder>(config);
}
auto& decoder = *decoders[thread_index];
auto& error_use = error_use_per_thread[thread_index];
results[shot_index] = decoder.decode(shots[shot_index]);
if (results[shot_index].count_for_stats) {
for (size_t ei : decoder.predicted_errors_buffer) {
++error_use[ei];
}
}
},
// Consume shot runs on the caller thread, strictly in shot order: 0, 1, 2, ...
[&](size_t shot_index) {
emit_result(results[shot_index]);
return !should_stop_early(results[shot_index]);
});
// Optional: merge per-thread scratch after all workers have joined.
std::vector<size_t> error_use_totals(num_error_terms);
for (const auto& error_use : error_use_per_thread) {
for (size_t ei = 0; ei < num_error_terms; ++ei) {
error_use_totals[ei] += error_use[ei];
}
}
```1 parent 1b453aa commit fff3c75
3 files changed
+158
-157
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
15 | 15 | | |
16 | 16 | | |
17 | 17 | | |
| 18 | + | |
18 | 19 | | |
19 | 20 | | |
20 | 21 | | |
| |||
107 | 108 | | |
108 | 109 | | |
109 | 110 | | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
110 | 114 | | |
111 | 115 | | |
112 | 116 | | |
| |||
367 | 371 | | |
368 | 372 | | |
369 | 373 | | |
370 | | - | |
| 374 | + | |
| 375 | + | |
371 | 376 | | |
372 | 377 | | |
373 | 378 | | |
| |||
416 | 421 | | |
417 | 422 | | |
418 | 423 | | |
419 | | - | |
420 | | - | |
421 | 424 | | |
422 | 425 | | |
423 | 426 | | |
424 | | - | |
425 | 427 | | |
426 | | - | |
| 428 | + | |
| 429 | + | |
| 430 | + | |
427 | 431 | | |
428 | | - | |
429 | | - | |
430 | | - | |
431 | | - | |
432 | | - | |
433 | | - | |
434 | | - | |
435 | | - | |
436 | | - | |
437 | | - | |
438 | | - | |
439 | | - | |
440 | | - | |
441 | | - | |
442 | | - | |
| 432 | + | |
| 433 | + | |
| 434 | + | |
| 435 | + | |
| 436 | + | |
| 437 | + | |
| 438 | + | |
| 439 | + | |
| 440 | + | |
| 441 | + | |
| 442 | + | |
443 | 443 | | |
444 | | - | |
| 444 | + | |
445 | 445 | | |
446 | | - | |
| 446 | + | |
447 | 447 | | |
448 | 448 | | |
449 | | - | |
| 449 | + | |
450 | 450 | | |
451 | | - | |
452 | | - | |
453 | | - | |
454 | | - | |
| 451 | + | |
| 452 | + | |
455 | 453 | | |
456 | 454 | | |
457 | 455 | | |
458 | 456 | | |
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 | | - | |
| 457 | + | |
| 458 | + | |
| 459 | + | |
| 460 | + | |
| 461 | + | |
| 462 | + | |
| 463 | + | |
| 464 | + | |
| 465 | + | |
| 466 | + | |
| 467 | + | |
| 468 | + | |
| 469 | + | |
| 470 | + | |
| 471 | + | |
| 472 | + | |
| 473 | + | |
| 474 | + | |
502 | 475 | | |
503 | | - | |
504 | | - | |
| 476 | + | |
| 477 | + | |
| 478 | + | |
| 479 | + | |
505 | 480 | | |
506 | 481 | | |
507 | | - | |
508 | | - | |
509 | | - | |
510 | 482 | | |
511 | 483 | | |
512 | 484 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
16 | 16 | | |
17 | 17 | | |
18 | 18 | | |
| 19 | + | |
19 | 20 | | |
20 | 21 | | |
21 | 22 | | |
| |||
120 | 121 | | |
121 | 122 | | |
122 | 123 | | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
123 | 127 | | |
124 | 128 | | |
125 | 129 | | |
| |||
424 | 428 | | |
425 | 429 | | |
426 | 430 | | |
427 | | - | |
| 431 | + | |
| 432 | + | |
428 | 433 | | |
429 | 434 | | |
430 | 435 | | |
| |||
475 | 480 | | |
476 | 481 | | |
477 | 482 | | |
478 | | - | |
479 | | - | |
480 | 483 | | |
481 | 484 | | |
482 | 485 | | |
483 | 486 | | |
484 | | - | |
485 | 487 | | |
486 | | - | |
| 488 | + | |
| 489 | + | |
| 490 | + | |
487 | 491 | | |
488 | | - | |
489 | | - | |
490 | | - | |
491 | | - | |
492 | | - | |
493 | | - | |
494 | | - | |
495 | | - | |
496 | | - | |
497 | | - | |
498 | | - | |
499 | | - | |
500 | | - | |
501 | | - | |
502 | | - | |
| 492 | + | |
| 493 | + | |
| 494 | + | |
| 495 | + | |
| 496 | + | |
| 497 | + | |
| 498 | + | |
| 499 | + | |
| 500 | + | |
| 501 | + | |
| 502 | + | |
| 503 | + | |
503 | 504 | | |
504 | | - | |
| 505 | + | |
505 | 506 | | |
506 | | - | |
| 507 | + | |
507 | 508 | | |
508 | 509 | | |
509 | | - | |
| 510 | + | |
510 | 511 | | |
511 | | - | |
512 | | - | |
513 | | - | |
514 | | - | |
515 | | - | |
| 512 | + | |
| 513 | + | |
| 514 | + | |
516 | 515 | | |
517 | 516 | | |
518 | 517 | | |
519 | 518 | | |
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 | | - | |
| 519 | + | |
| 520 | + | |
| 521 | + | |
| 522 | + | |
| 523 | + | |
| 524 | + | |
| 525 | + | |
| 526 | + | |
| 527 | + | |
| 528 | + | |
| 529 | + | |
| 530 | + | |
| 531 | + | |
| 532 | + | |
| 533 | + | |
| 534 | + | |
| 535 | + | |
| 536 | + | |
| 537 | + | |
| 538 | + | |
| 539 | + | |
| 540 | + | |
569 | 541 | | |
570 | | - | |
571 | | - | |
| 542 | + | |
| 543 | + | |
| 544 | + | |
| 545 | + | |
572 | 546 | | |
573 | 547 | | |
574 | | - | |
575 | | - | |
576 | | - | |
577 | 548 | | |
578 | 549 | | |
579 | 550 | | |
| |||
0 commit comments