Modernize parallel computing APIs and improve MCMC swap strategy#1
Open
akutuva21 wants to merge 1 commit intoRuleWorld:masterfrom
Open
Modernize parallel computing APIs and improve MCMC swap strategy#1akutuva21 wants to merge 1 commit intoRuleWorld:masterfrom
akutuva21 wants to merge 1 commit intoRuleWorld:masterfrom
Conversation
) * 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>
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.
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
matlabpoolwas deprecated in R2013b and removed in R2014a, makingparallel_tempering.munable to run in parallel on any modern MATLAB version.Changes
Parallel computing modernization
parallel_tempering.m: Replacedmatlabpool('local', nlabs)withparpool('local', nlabs), guarded bygcp('nocreate')to reuse an existing pool. Replacedmatlabpool closewithdelete(gcp('nocreate')).init_chains.m: Converted the chain initialization loop toparforwith local temporary variables (tmp_energy,tmp_params) for correctparforvariable classification. Worker count respectscfg.parallelandcfg.maxlabs.energy_gaussian.m/energy_tdistr.m: Converted the experiment simulation loop toparfor. Replaced illegalreturnstatements (not permitted insideparfor) with per-experiment energy accumulation viaexpt_energyarray andcontinue. Per-experimenttic/toctimers replace the old singlesimtimerso the time penalty uses sum-of-simulation-times rather than wall-clock time. Removed the resulting deadsimtimervariable. Worker count respectscfg.parallelandcfg.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 inswap_acceptance:0= not attempted,-1= attempted and rejected,1= attempted and accepted.adapt_beta.m/display_chains.m: Updated swap acceptance rate calculations to useacceptances / max(attempts, 1)instead ofacceptances / cfg.adapt_beta_interval, so the adaptive temperature ladder correctly targets the configuredoptimal_swap_acceptancerate relative to actual attempts.Behavioral notes
cfg.parallel = 0, allparforloops execute as ordinaryforloops (MATLAB'sparfor(i=1:N, 0)semantics).nlabs. The original code always opened a fresh pool.nchains/2swaps per round (half the originalnchains-1). Each pair is still attempted with equal frequency on average across rounds. Theadapt_betacalculation has been updated to account for this.fprintfinparfor. Chain initialization output may appear interleaved when running in parallel. This is cosmetic only.Testing
cfg.parallel = 0) produces identical behavior to the unpatched code.adapt_betareceives correct acceptance rates under the new tracking scheme by inspectingswap_acceptanceandbeta_historyoutputs.