feat: parallelize unitig extraction and FASTA output
Replace the non-atomic `set_visited` with atomic `fetch_or` bitmask operations to enable thread-safe node claiming. Introduce a two-phase extraction pipeline where `par_for_each_chain_unitig` builds chains in parallel and `for_each_remaining_unitig` sequentially handles residual cycles and junctions. Add `is_start` and `collect_from_start` to explicitly define unitig boundaries. Wrap `BufWriter` in a `Mutex` and use an `AtomicUsize` counter to ensure thread-safe concurrent FASTA output, refactoring the write logic into a shared closure for safe multi-threaded execution.
This commit is contained in:
1 parent
249998beed
commit
d202ead385
2 files changed
+138
-17
No files matched your search
@@ -1,5 +1,7 @@
|
||||
use std::io::{self, BufWriter, Write};
|
||||
use std::path::PathBuf;
|
||||
use std::sync::Mutex;
|
||||
use std::sync::atomic::{AtomicUsize, Ordering};
|
||||
|
||||
use clap::Args;
|
||||
use obidebruinj::GraphDeBruijn;
|
||||
@@ -68,24 +70,32 @@ pub fn run(args: UnitigArgs) {
|
||||
g.compute_degrees();
|
||||
rep.push(stage.stop());
|
||||
|
||||
// ── Phase 3 : enumerate unitigs and write as FASTA ────────────────────────
|
||||
let pb = spinner("unitig");
|
||||
let stdout = io::stdout();
|
||||
let mut out = BufWriter::new(stdout.lock());
|
||||
// ── Phase 3 : enumerate unitigs and write as FASTA ───────────────────────
|
||||
let pb = spinner("unitig");
|
||||
let out = Mutex::new(BufWriter::new(io::stdout()));
|
||||
let j = AtomicUsize::new(0);
|
||||
|
||||
let stage = Stage::start("enumerate unitigs");
|
||||
for (j, unitig) in g.iter_unitig().enumerate() {
|
||||
write_unitig(&unitig, k, 0, j, &mut out).unwrap_or_else(|e| {
|
||||
let write_unitig_fn = |unitig: obikseq::unitig::Unitig| {
|
||||
let idx = j.fetch_add(1, Ordering::Relaxed);
|
||||
let mut w = out.lock().unwrap();
|
||||
write_unitig(&unitig, k, 0, idx, &mut *w).unwrap_or_else(|e| {
|
||||
eprintln!("write error: {e}");
|
||||
std::process::exit(1);
|
||||
});
|
||||
if j % 10_000 == 0 {
|
||||
pb.set_message(format!("{j} unitigs written"));
|
||||
if idx % 10_000 == 0 {
|
||||
pb.set_message(format!("{idx} unitigs written"));
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
let stage = Stage::start("chain unitigs");
|
||||
g.par_for_each_chain_unitig(&write_unitig_fn);
|
||||
rep.push(stage.stop());
|
||||
|
||||
let stage = Stage::start("remaining unitigs");
|
||||
g.for_each_remaining_unitig(&write_unitig_fn);
|
||||
pb.finish_and_clear();
|
||||
rep.push(stage.stop());
|
||||
|
||||
out.flush().expect("flush error");
|
||||
out.into_inner().unwrap().flush().expect("flush error");
|
||||
rep.print();
|
||||
}
|
||||
Reference in new issue
Block a user