feat: add pipeline concurrency throttling and HPC build docs
Introduces a counting semaphore-based throttling mechanism to limit concurrent file I/O and pipeline processing. Replaces custom path wrappers with standardized `Throttled` types across `obikmer` and `obikpartitionner`, ensuring RAII-based resource cleanup and explicit backpressure. Additionally, documents how to redirect Cargo build artifacts to local scratch storage on HPC filesystems to prevent compilation slowdowns.
This commit is contained in:
1 parent
c6ea0c53e3
commit
175ea5bbd0
7 files changed
+149
-85
No files matched your search
+2
-63
@@ -1,9 +1,9 @@
|
||||
use std::path::PathBuf;
|
||||
use std::sync::{Arc, Condvar, Mutex};
|
||||
|
||||
use clap::Args;
|
||||
use obiread::NucPage;
|
||||
use obikseq::RoutableSuperKmer;
|
||||
use obipipeline::Throttled;
|
||||
|
||||
// ── Shared arguments ──────────────────────────────────────────────────────────
|
||||
|
||||
@@ -103,54 +103,10 @@ impl CommonArgs {
|
||||
}
|
||||
}
|
||||
|
||||
// ── Open-file throttling ──────────────────────────────────────────────────────
|
||||
|
||||
struct FileSlots {
|
||||
count: Mutex<usize>,
|
||||
condvar: Condvar,
|
||||
max: usize,
|
||||
}
|
||||
|
||||
impl FileSlots {
|
||||
fn new(max: usize) -> Self {
|
||||
Self { count: Mutex::new(0), condvar: Condvar::new(), max }
|
||||
}
|
||||
|
||||
fn acquire(&self) {
|
||||
let mut count = self.count.lock().unwrap();
|
||||
while *count >= self.max {
|
||||
count = self.condvar.wait(count).unwrap();
|
||||
}
|
||||
*count += 1;
|
||||
}
|
||||
|
||||
fn release(&self) {
|
||||
let mut count = self.count.lock().unwrap();
|
||||
*count -= 1;
|
||||
self.condvar.notify_one();
|
||||
}
|
||||
}
|
||||
|
||||
struct SlotsGuard(Arc<FileSlots>);
|
||||
|
||||
impl Drop for SlotsGuard {
|
||||
fn drop(&mut self) {
|
||||
self.0.release();
|
||||
}
|
||||
}
|
||||
|
||||
// ── Pipeline data carrier ─────────────────────────────────────────────────────
|
||||
|
||||
/// A path bundled with an opaque guard token.
|
||||
/// The guard is acquired in the source thread and dropped by the flat worker
|
||||
/// once the file is fully read, releasing the open-file slot.
|
||||
pub struct PathWithSlot {
|
||||
pub path: PathBuf,
|
||||
pub _guard: Box<dyn Send + 'static>,
|
||||
}
|
||||
|
||||
pub enum PipelineData {
|
||||
Path(PathWithSlot),
|
||||
Path(Throttled<PathBuf>),
|
||||
NucPage(NucPage),
|
||||
Batch(Vec<RoutableSuperKmer>),
|
||||
}
|
||||
@@ -158,20 +114,3 @@ pub enum PipelineData {
|
||||
unsafe impl Send for PipelineData {}
|
||||
unsafe impl Sync for PipelineData {}
|
||||
|
||||
/// Wrap a path iterator so that at most `max_open` files are open simultaneously.
|
||||
/// Acquisition happens in the caller's thread (the pipeline source thread),
|
||||
/// never inside a worker, preventing deadlocks.
|
||||
pub fn throttle_paths(
|
||||
source: impl Iterator<Item = PathBuf> + Send + 'static,
|
||||
max_open: usize,
|
||||
) -> impl Iterator<Item = PathWithSlot> + Send + 'static {
|
||||
let slots = Arc::new(FileSlots::new(max_open));
|
||||
source.map(move |path| {
|
||||
slots.acquire();
|
||||
PathWithSlot {
|
||||
path,
|
||||
_guard: Box::new(SlotsGuard(Arc::clone(&slots))),
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
@@ -1,10 +1,13 @@
|
||||
use std::io::{self, BufWriter, Write};
|
||||
use std::path::PathBuf;
|
||||
|
||||
use clap::Args;
|
||||
use obifastwrite::write_scatter;
|
||||
use obikseq::{RoutableSuperKmer, set_k, set_m};
|
||||
|
||||
use crate::cli::{CommonArgs, PipelineData, PathWithSlot, partitions_to_bits, throttle_paths};
|
||||
use obipipeline::{Throttled, throttle};
|
||||
|
||||
use crate::cli::{CommonArgs, PipelineData, partitions_to_bits};
|
||||
|
||||
#[derive(Args)]
|
||||
pub struct SuperkmerArgs {
|
||||
@@ -46,14 +49,15 @@ pub fn run(args: SuperkmerArgs) {
|
||||
set_k(k);
|
||||
set_m(m);
|
||||
|
||||
let path_source = throttle_paths(args.common.seqfile_paths(), max_open);
|
||||
let path_source = throttle(args.common.seqfile_paths(), max_open);
|
||||
|
||||
let pipe = obipipeline::make_pipe! {
|
||||
PipelineData : PathWithSlot => Vec<RoutableSuperKmer>,
|
||||
PipelineData : Throttled<PathBuf> => Vec<RoutableSuperKmer>,
|
||||
||? {
|
||||
let k = k;
|
||||
move |pw: PathWithSlot| {
|
||||
let path_str = pw.path.to_str().unwrap_or("").to_owned();
|
||||
move |pw: Throttled<PathBuf>| {
|
||||
let path_str = pw.item.to_str().unwrap_or("").to_owned();
|
||||
let _guard = pw.guard;
|
||||
obiread::open_nuc_stream(&path_str, k)
|
||||
}
|
||||
} : Path => NucPage,
|
||||
|
||||
@@ -6,16 +6,17 @@ use std::time::Instant;
|
||||
use obisys::spinner;
|
||||
use obiread::NucPage;
|
||||
use obikpartitionner::KmerPartition;
|
||||
use obipipeline::{ThrottleGuard, Throttled, throttle};
|
||||
use obisys::{Reporter, Stage};
|
||||
use tracing::info;
|
||||
|
||||
use crate::cli::{PipelineData, PathWithSlot, throttle_paths};
|
||||
use crate::cli::PipelineData;
|
||||
|
||||
// ── Iterator that keeps the slot guard alive until the file is exhausted ──────
|
||||
|
||||
struct GuardedIter {
|
||||
inner: Box<dyn Iterator<Item = NucPage> + Send>,
|
||||
_guard: Box<dyn Send + 'static>,
|
||||
_guard: ThrottleGuard,
|
||||
flat_active: Arc<AtomicU32>,
|
||||
}
|
||||
|
||||
@@ -49,7 +50,7 @@ pub fn scatter(
|
||||
use obikseq::RoutableSuperKmer;
|
||||
|
||||
// Throttle in the source thread — never in a worker — to prevent deadlock.
|
||||
let throttled = throttle_paths(path_source, max_open);
|
||||
let throttled = throttle(path_source, max_open);
|
||||
|
||||
let file_count = Arc::new(AtomicU64::new(0));
|
||||
let flat_active = Arc::new(AtomicU32::new(0));
|
||||
@@ -57,19 +58,20 @@ pub fn scatter(
|
||||
|
||||
let t = Stage::start("scatter");
|
||||
let pipe = obipipeline::make_pipe! {
|
||||
PipelineData : PathWithSlot => Vec<RoutableSuperKmer>,
|
||||
PipelineData : Throttled<PathBuf> => Vec<RoutableSuperKmer>,
|
||||
||? {
|
||||
let file_count = Arc::clone(&file_count);
|
||||
let flat_active = Arc::clone(&flat_active);
|
||||
let k = k;
|
||||
move |pw: PathWithSlot| {
|
||||
let PathWithSlot { path, _guard } = pw;
|
||||
move |pw: Throttled<PathBuf>| {
|
||||
let path = pw.item;
|
||||
let guard = pw.guard;
|
||||
let n = file_count.fetch_add(1, Ordering::Relaxed) + 1;
|
||||
info!("indexing [{}]: {}", n, path.display());
|
||||
let path_str = path.to_str().unwrap_or("").to_owned();
|
||||
flat_active.fetch_add(1, Ordering::Relaxed);
|
||||
obiread::open_nuc_stream(&path_str, k)
|
||||
.map(|iter| GuardedIter { inner: iter, _guard, flat_active: Arc::clone(&flat_active) })
|
||||
.map(|iter| GuardedIter { inner: iter, _guard: guard, flat_active: Arc::clone(&flat_active) })
|
||||
}
|
||||
} : Path => NucPage,
|
||||
| {
|
||||
|
||||
Reference in new issue
Block a user