Skip to content

Enable two-level nested parallelism in demeaning using OpenMP tasks and taskloops - #683

Open
tholden wants to merge 4 commits into
lrberge:masterfrom
tholden:taskloop-2level-parallel
Open

tholden wants to merge 4 commits into
lrberge:masterfrom
tholden:taskloop-2level-parallel

Conversation

@tholden

@tholden tholden commented Sep 21, 2026 •

Copy link
Copy Markdown
Contributor

NOTE: This pull request is entirely AI generated. It seems reasonable, but as always with AI, particular care should be taken before merging it.

Summary

This PR enables two-level nested parallelism in the fixed-effect demeaning routine (cpp_demean) and fuses observation-level and group-level inner passes to minimize memory bandwidth and OpenMP task barrier overhead:

  • Outer level: Parallelizes across variables using OpenMP tasks (#pragma omp task) instead of static loop partitioning.
  • Inner level: Uses #pragma omp taskloop num_tasks(omp_num_tasks(n)) for observation-level and group-level operations, plus #pragma omp parallel for for final residual subtraction.
  • Loop fusion & zero-allocation SSR checks: Fuses multi-FE gather passes into single passes over $N$ rows and computes periodic SSR convergence checks on the fly without allocating temporary $O(N)$ buffers.

Motivation & Problem

In fixest, demeaning across $V$ variables and $N$ observations was previously parallelized solely across variables using:

#pragma omp parallel for num_threads(nthreads_current) schedule(static, 1)

While this works well when $V \gg \text{nthreads}$, it suffers from two major bottlenecks:

  1. Thread starvation when $V < \text{nthreads}$: If estimating a model with only a few covariates (e.g. $V=1$ for a dependent variable, or 2-4 regressors on a 16- or 64-core machine), all remaining threads idle while only $V$ threads work.
  2. Severe load imbalance: Different variables converge at vastly different rates (some converge in 2-5 inner iterations, while others require 50+ iterations). Under static variable scheduling, cores that finish their variables sit idle at the OpenMP barrier while the slowest variable runs serially on a single core.

Design of this PR

  1. Single Parallel Region with Work Stealing:
    • In cpp_demean, #pragma omp parallel num_threads(nthreads) is opened once. Using #pragma omp master, thread 0 spawns outer tasks for variables 1 .. n_vars - 1 and directly executes variable 0 (guaranteeing that when n_vars == 1, thread 0 runs demean_acc_gnl and checks user interrupts while worker threads execute inner taskloop chunks).
    • All worker threads participate in executing these variable tasks and inner loop tasks.
  2. Inner Observation & Group Parallelism via taskloop with Bounded Grain Size:
    • Helper omp_num_tasks(n, min_grain = 32768) bounds the task count to min(4 * omp_get_num_threads(), n / min_grain) so large arrays divide into $\sim 4 \times \text{nthreads}$ tasks while medium arrays avoid excessive task creation overhead.
    • When a thread finishes its assigned variable task early, it immediately begins stealing inner chunks from the tasks of slower, still-active variables.
    • Group-level normalization (my_fe_coef[m] /= my_SW[m]) and independent per-cluster forward/backward Cholesky triangular solves for varying slopes (c = 0 .. nb_id - 1) are also parallelized via #pragma omp taskloop when nb_cluster is large.
  3. Fused Multi-FE Gather Passes (compute_sum_other_means, add_all_fe_coef_to_mu, add_2_fe_coef_to_mu):
    • Previously, in each Gauss-Seidel step for FE $q \in {0, \dots, Q-1}$, compute_fe_gnl zeroed sum_other_means (an $N$-element buffer) and then executed $Q-1$ separate add_fe_coef_to_mu passes over $N$ rows -- causing $(2(Q-1)+1)Q$ memory passes and $Q^2$ taskloop barriers per sweep.
    • FEClass::compute_sum_other_means fuses the clearing and accumulation across all $h \ne q$ into a single write-only pass over $N$ rows, reducing sum_other_means DRAM traffic by $(2Q-1)\times$ and reducing taskloop barriers from $Q^2$ to $Q$ per sweep.
    • Similarly, add_all_fe_coef_to_mu and add_2_fe_coef_to_mu fuse post-iteration mean reconstruction into a single pass over $N$ rows.
  4. Zero-Allocation SSR Convergence Checks (compute_ssr, compute_2_fe_ssr):
    • Previously, every 40 iterations in demean_acc_gnl and demean_acc_2, a fresh vector<double> mu_current(n_obs, 0) was heap-allocated (VirtualAlloc/mmap + page faults) and freed to check whether SSR had plateaued. On large datasets (e.g., $N = 5 \times 10^8$), this allocated and freed >4 GB per variable every 40 iterations.
    • compute_ssr and compute_2_fe_ssr compute the residual sum of squares directly on the fly in a single taskloop reduction(+:ssr) pass with zero heap allocations.
  5. Stateless simple_mat_with_id for Thread-Safe Varying-Slope Gather Passes:
    • simple_mat_with_id::operator() computes px0[n_total * id + i + nrow * j] directly without mutable cached state, making concurrent varying-slope reads completely thread-safe and branch-free.
  6. Parallel Residual Subtraction:
    • Final construction of X_demean and y_demean (input - output) at the end of cpp_demean is parallelized with #pragma omp parallel for.

@tholden
tholden force-pushed the taskloop-2level-parallel branch 2 times, most recently from 041a180 to aedc5c8 Compare November 5, 2026 16:48
tholden added a commit to tholden/fixest that referenced this pull request Nov 5, 2026
tholden added a commit to tholden/fixest that referenced this pull request Nov 7, 2026
@tholden
tholden force-pushed the taskloop-2level-parallel branch from c4942a8 to e3fcc47 Compare November 26, 2026 16:06

This branch has not been deployed

No deployments
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