parallelization#6
Open
iron-lion wants to merge 2 commits into
Open
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Dear @lilit-nersisyan
Thanks for the nice tool.
I was analyzing our Ribo-seq data using your library, but it was too slow for our analyses. The original Fivepseq processes all ~20,000 transcripts sequentially at every stage of the analysis pipeline.
This PR adds multiprocessing, resulting in a roughly 10× speedup (with 8 workers) in computation time.
I tested with our own data.
Test scenario: serial (--n-workers =1) vs. parallel (--n-workers= 8)
Note: This PR was done with assistance from Claude.
Pipeline Execution Order
Process-Parallel Pattern
All five multiprocessing functions follow the same three-phase pattern.
Chunk strategy
Transcripts are divided into
n_workersequal chunks. Each chunk becomes onetask_id. Iftranscript_count < n_workers,n_workersis clamped totranscript_count.Phase 1 — Set globals before fork (copy-on-write)
get_context('fork')workers inherit these via the OS copy-on-write mechanism. The objects are never pickled or transferred; workers read pages only as needed. Setting them beforePool()construction is required — workers see the module state at fork time.Phase 2 — Pool initializer: fresh BAM handle per worker
Each worker opens its own
pysam.AlignmentFile. File descriptors cannot be shared across fork (POSIX semantics), so the parent never opens the BAM — it passes the path as a string.Phase 3 — Shared-memory accumulator (zero-copy reduction)
Each worker writes to its own
[task_id]slice — no locks, no contention. The main process sums acrossaxis=0to get the final aggregate array.Shared-Memory Lifecycle
All
SharedMemoryblocks follow this pattern to avoid leaks on exceptions:finallyguarantees cleanup even if workers crash.shm.unlink()removes the POSIX shared-memory name from/dev/shm;shm.close()releases the local mapping.Comparison
All 33 output files match exactly between serial (n_workers=1) and parallel (n_workers=8) runs.
Test env : 2,000 genes of Ribo-seq data