fix: pin BLAS to one thread in PWM kernels to stop thread explosion - #37
Merged
Conversation
compute_pwm(), compute_local_pwm() and calc_seq_pwm() (and thus extract_pwm()) run a per-sequence BLAS dgemm inside an RcppParallel/TBB parallelFor over sequences. With a multi-threaded BLAS (MKL, OpenBLAS) every TBB worker spawns its own BLAS thread team, so a single call opens n_threads * n_blas_threads OS threads - e.g. 64x16 = 1025 at 16 threads on a 128-core node, and thousands at the set_parallel() default. That oversubscribes the machine and, on a cluster job with a thread/process (cgroup pids) limit, makes the call FAIL with a thread-creation error (reported via iceqream::infer_trajectory_motifs -> extract_pwm). The per-sequence dgemm is tiny and the parallelFor over sequences is the right granularity, so the inner BLAS must be serial. New helper local_serial_blas() pins the BLAS to one thread for the duration of these kernels and restores the previous count on exit (the rest of the session keeps its threaded BLAS). Verified on MKL: a 16-thread extract_pwm drops from 1025 to 17 OS threads, results identical, throughput same or slightly better. Adds RhpcBLASctl to Imports (the only portable cross-BLAS thread control). Bumps to 0.0.10. Claude-Session: https://claude.ai/code/session_01KfEW7AGsSzjhMqiY8htdnq
…ead-explosion # Conflicts: # NEWS.md
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.
Problem
extract_pwm()(andcompute_pwm()/compute_local_pwm()/calc_seq_pwm()) open thousands of OS threads and can fail outright on core-limited machines. Reported viaiceqream::infer_trajectory_motifs(), whose default path runsextract_pwm().Root cause is nested parallelism: these kernels run a small per-sequence BLAS
dgemm(PWMWorker::compute_matrix_scores) inside an RcppParallel/TBBparallelForover sequences. With a multi-threaded BLAS (MKL or OpenBLAS), each TBB worker spawns its own BLAS thread team, so one call opensn_threads × n_blas_threadsthreads:setThreadOptionsAt the
set_parallel()default (~102 threads) that's ~6500 threads for a singleextract_pwm. It oversubscribes badly and, on a cluster job with a thread/process (cgrouppids) limit, fails with a thread-creation error.(A bare RcppParallel
parallelForopens exactlyn_threads; the multiplier is entirely the inner threadeddgemm.OMP_NUM_THREADS=1at launch also collapses it - confirming the diagnosis.)Fix
New helper
local_serial_blas()pins the BLAS to one thread for the duration of these kernels and restores the previous count on exit. The per-sequencedgemmis tiny and theparallelForover sequences is the right parallelism granularity, so the inner BLAS should never be threaded here.Verification (MKL)
extract_pwm: 1025 → 17 OS threads;compute_pwm/compute_local_pwmlikewise.all.equalTRUE); throughput same or slightly better (the threaded dgemm was pure overhead at this size).testthatsuite passes.Adds
RhpcBLASctlto Imports (the portable cross-BLAS thread control -RhpcBLASctl::blas_set_num_threads). Bumps to 0.0.10.https://claude.ai/code/session_01KfEW7AGsSzjhMqiY8htdnq