Skip to content

feat: add multithreading/multiprocessing acceleration - #3

Closed
Zac-lzh wants to merge 2 commits into
mainfrom
feat/multithreading-acceleration
Closed

feat: add multithreading/multiprocessing acceleration#3
Zac-lzh wants to merge 2 commits into
mainfrom
feat/multithreading-acceleration

Conversation

@Zac-lzh

@Zac-lzh Zac-lzh commented Jun 8, 2026

Copy link
Copy Markdown
Collaborator

Summary

Addresses issue #1: adds CPU-based multithreading/multiprocessing acceleration for compute-intensive operations in py-hdWGCNA.

Changes

  • New parallel.py module: ProcessPoolExecutor/ThreadPoolExecutor utilities
  • Vectorized Spearman ranking: Replace Python loop with scipy.stats.rankdata(axis=1) (~1.9x speedup)
  • Parallel soft power testing: Power-value loop uses ThreadPoolExecutor (each power is independent)
  • Parallel TF network construction: Per-gene XGBoost loop uses ThreadPoolExecutor
  • Vectorized regulon scores: Per-cell ranking loop replaced with vectorized np.argsort
  • Wired n_threads parameter: Flows through construct_networkcompute_tom/compute_kme/compute_correlation_matrix
  • HDWGCNA class: Added n_threads parameter to test_soft_powers, construct_network, construct_tf_network, regulon_scores

Approach

  • Uses concurrent.futures.ThreadPoolExecutor for numpy-heavy operations (numpy releases GIL)
  • Uses concurrent.futures.ProcessPoolExecutor for CPU-bound Python code
  • BLAS threading controlled via OMP_NUM_THREADS/MKL_NUM_THREADS environment variables
  • No new external dependencies (uses stdlib concurrent.futures)
  • Visualization code unchanged

Verification

  • All 20 existing tests pass
  • Spearman vectorization produces identical results to loop version
  • n_threads parameter defaults to None (auto-detect CPU count)

Test plan

  • Run existing test suite: python -m pytest tests/ -v (20/20 pass)
  • Verify Spearman vectorization correctness
  • Verify imports work correctly
  • Test with large datasets to measure speedup

Zac-lzh added 2 commits June 9, 2026 02:01
Add CPU-based parallelization for compute-intensive operations:

- New parallel.py module with ProcessPoolExecutor/ThreadPoolExecutor utilities
- Vectorize Spearman ranking in compute_correlation_matrix (1.9x speedup)
- Parallelize test_soft_powers power-value loop using threads
- Parallelize construct_tf_network per-gene XGBoost loop using threads
- Vectorize regulon_scores per-cell ranking loop (eliminates Python loop)
- Wire n_threads parameter through construct_network to compute_tom/compute_kme
- Add n_threads parameter to HDWGCNA class wrapper methods

Addresses issue #1: multithreading support for hdWGCNA.

Changes:
- py_hdWGCNA/parallel.py (new): parallelization utilities
- py_hdWGCNA/utils.py: vectorized Spearman, n_threads for TOM/kME/correlation
- py_hdWGCNA/network.py: parallel power testing, n_threads wiring
- py_hdWGCNA/tf_network.py: parallel TF network, vectorized regulon scores
- py_hdWGCNA/hdWGCNA.py: n_threads parameter in wrapper methods
Add _kernels.py module with optimized implementations:
- Use numpy BLAS for matrix multiply (already multi-threaded SIMD)
- Use vectorized scipy.stats.rankdata for Spearman (1.7-2.1x speedup)
- Graceful fallback if numba available but use numpy BLAS path

Benchmark results (2000 genes):
- Spearman: 1.7x speedup via vectorized rankdata
- Power testing: 2.4x speedup via ThreadPoolExecutor
- TOM: numpy BLAS already optimal (adj @ adj)
- Full pipeline: 1.1-1.5x speedup

Note: The main bottleneck (adj @ adj matrix multiply) is already
handled by numpy's BLAS library (MKL/OpenBLAS) which uses multiple
threads and SIMD instructions internally.
@Zac-lzh

Zac-lzh commented Jun 9, 2026

Copy link
Copy Markdown
Collaborator Author

Closed by author. The speedup from Python-level parallelization is limited since numpy BLAS (MKL/OpenBLAS) already uses multiple threads internally for matrix multiply. Significant acceleration would require C++ extensions with a compiler or GPU (cupy).

@Zac-lzh Zac-lzh closed this Jun 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant