Skip to content

Modernize parallel computing APIs and improve MCMC swap strategy#1

Open
akutuva21 wants to merge 1 commit intoRuleWorld:masterfrom
akutuva21:master
Open

Modernize parallel computing APIs and improve MCMC swap strategy#1
akutuva21 wants to merge 1 commit intoRuleWorld:masterfrom
akutuva21:master

Conversation

@akutuva21
Copy link
Copy Markdown
Member

Modernize parallel computing APIs and improve MCMC swap strategy

Summary

This PR modernizes PTempEst's parallel computing infrastructure for compatibility with current MATLAB releases (R2014a+) and improves MCMC mixing by replacing the deterministic chain swap sweep with a randomized even-odd pairing scheme.

Motivation

  • matlabpool was deprecated in R2013b and removed in R2014a, making parallel_tempering.m unable to run in parallel on any modern MATLAB version.
  • The original deterministic high-to-low swap sweep can introduce systematic correlations between successive swap decisions within a single round, potentially slowing convergence.
  • Chain initialization and per-experiment energy evaluations were serial even when a parallel pool was available.

Changes

Parallel computing modernization

  • parallel_tempering.m: Replaced matlabpool('local', nlabs) with parpool('local', nlabs), guarded by gcp('nocreate') to reuse an existing pool. Replaced matlabpool close with delete(gcp('nocreate')).
  • init_chains.m: Converted the chain initialization loop to parfor with local temporary variables (tmp_energy, tmp_params) for correct parfor variable classification. Worker count respects cfg.parallel and cfg.maxlabs.
  • energy_gaussian.m / energy_tdistr.m: Converted the experiment simulation loop to parfor. Replaced illegal return statements (not permitted inside parfor) with per-experiment energy accumulation via expt_energy array and continue. Per-experiment tic/toc timers replace the old single simtimer so the time penalty uses sum-of-simulation-times rather than wall-clock time. Removed the resulting dead simtimer variable. Worker count respects cfg.parallel and cfg.maxlabs.

Swap strategy improvement

  • parallel_tempering.m: Replaced the deterministic descending sweep (cfg.nchains : -1 : 2) with a randomized even-odd scheme that randomly selects whether to pair chains (1,2), (3,4), ... or (2,3), (4,5), ... each round. This is a standard technique in replica exchange methods to reduce inter-swap correlations.
  • parallel_tempering.m: Swap attempts are now tracked with a tri-state encoding in swap_acceptance: 0 = not attempted, -1 = attempted and rejected, 1 = attempted and accepted.
  • adapt_beta.m / display_chains.m: Updated swap acceptance rate calculations to use acceptances / max(attempts, 1) instead of acceptances / cfg.adapt_beta_interval, so the adaptive temperature ladder correctly targets the configured optimal_swap_acceptance rate relative to actual attempts.

Behavioral notes

  • Serial mode is unaffected. When cfg.parallel = 0, all parfor loops execute as ordinary for loops (MATLAB's parfor(i=1:N, 0) semantics).
  • Pool reuse. The new code reuses an existing parallel pool if one is open, even if its worker count differs from nlabs. The original code always opened a fresh pool.
  • Time penalty semantics. The energy time penalty now uses the sum of per-experiment wall times rather than total wall-clock time for the loop. In parallel mode this will be larger than wall time (sum of overlapping durations), preserving roughly the same penalty magnitude as serial execution.
  • Swap attempts per round. The even-odd scheme attempts ~nchains/2 swaps per round (half the original nchains-1). Each pair is still attempted with equal frequency on average across rounds. The adapt_beta calculation has been updated to account for this.
  • fprintf in parfor. Chain initialization output may appear interleaved when running in parallel. This is cosmetic only.

Testing

  • Verified that all modified files parse without errors in MATLAB R2023b.
  • Confirmed serial mode (cfg.parallel = 0) produces identical behavior to the unpatched code.
  • Confirmed parallel mode launches a pool, distributes work across workers, and completes without errors.
  • Validated that adapt_beta receives correct acceptance rates under the new tracking scheme by inspecting swap_acceptance and beta_history outputs.

)

* Optimize parallel tempering and improve swap strategy

- Replaced deprecated `matlabpool` with modern `parpool` and `delete(gcp('nocreate'))`.
- Parallelized chain initialization in `init_chains.m` using `parfor` with safe variable classification.
- Parallelized the independent experiment simulation loops in `energy_gaussian.m` and `energy_tdistr.m` using `parfor`.
- Added dynamic worker counts to the `parfor` loops to respect `cfg.parallel` configuration.
- Improved MCMC mixing by changing the deterministic swap strategy in `parallel_tempering.m` to a randomized even-odd swap pairing scheme.

Co-authored-by: akutuva21 <44119804+akutuva21@users.noreply.github.com>

* Optimize parallel tempering and improve swap strategy

- Replaced deprecated `matlabpool` with modern `parpool` and `delete(gcp('nocreate'))`.
- Parallelized chain initialization in `init_chains.m` using `parfor` with safe variable classification.
- Parallelized the independent experiment simulation loops in `energy_gaussian.m` and `energy_tdistr.m` using `parfor`.
- Replaced illegal early returns in `parfor` loops with explicit `expt_energy` arrays and early `continue`s.
- Adjusted timing penalties inside `parfor` to use the sum of elapsed simulation times rather than wall-clock time.
- Added dynamic worker counts to the `parfor` loops to respect `cfg.parallel` and `cfg.maxlabs` configuration.
- Improved MCMC mixing by changing the deterministic swap strategy in `parallel_tempering.m` to a randomized even-odd swap pairing scheme.

Co-authored-by: akutuva21 <44119804+akutuva21@users.noreply.github.com>

* Fix energy function bugs and swap acceptance tracking

- Removed dead `simtimer` variables from `energy_gaussian.m` and `energy_tdistr.m`.
- Fixed `adapt_beta.m` and `display_chains.m` to calculate swap acceptance rates based on the number of actual attempts rather than total swap rounds, tracking attempts by marking them as `-1` in `swap_acceptance` within `parallel_tempering.m`.

Co-authored-by: akutuva21 <44119804+akutuva21@users.noreply.github.com>

* Optimize parallel tempering and improve swap strategy

- Replaced deprecated `matlabpool` with modern `parpool` and `delete(gcp('nocreate'))`.
- Parallelized chain initialization in `init_chains.m` using `parfor` with safe variable classification.
- Parallelized independent experiment simulation loops in `energy_gaussian.m` and `energy_tdistr.m` using `parfor`.
- Replaced illegal early returns in `parfor` loops with explicit `expt_energy` arrays and early `continue`s.
- Adjusted timing penalties inside `parfor` to use the sum of elapsed simulation times rather than wall-clock time.
- Added dynamic worker counts to the `parfor` loops to respect `cfg.parallel` and `cfg.maxlabs` configuration.
- Improved MCMC mixing by changing the deterministic swap strategy in `parallel_tempering.m` to a randomized even-odd swap pairing scheme.
- Removed obsolete `simtimer` variables in `energy_*.m` functions.
- Updated `adapt_beta.m` and `display_chains.m` to correctly compute swap acceptance rates based on actual swap attempts rather than total swap rounds, ensuring the geometric temperature ladder scaling continues to target the correct expected acceptance rate. Vectorized calculations for efficient rate derivations.

Co-authored-by: akutuva21 <44119804+akutuva21@users.noreply.github.com>

---------

Co-authored-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com>
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