Skip to content

Updated the LSODE integrator to be thread-safe#147

Open
yantosca wants to merge 3 commits into
devfrom
feature/lsode-thread-safe
Open

Updated the LSODE integrator to be thread-safe#147
yantosca wants to merge 3 commits into
devfrom
feature/lsode-thread-safe

Conversation

@yantosca

@yantosca yantosca commented Apr 23, 2026

Copy link
Copy Markdown
Contributor

In this PR we have modified the int/lsode.f90 integrator to be OpenMP thread-safe. This is done by removing the COMMON /DLS001/ common block declaration. All variables that used to be in the common block are now declared as local variables within routine DLSODE and are therefore reachable by other internal routines used by LSODE under its CONTAINS statement.

The refactoring was done with Claude Code AI.

Summary of Changes Applied

Location Change
Module header Replaced with thread-safety banner and attribution comment
Do_Update_* flags Added !$OMP THREADPRIVATE(...) directive after the three LOGICAL declarations
DLSODE declarations Removed old INTEGER INIT,MXSTEP,...,IOWNS + KPP_REAL ROWNS,CCMAX,... + COMMON /DLS001/ block; replaced with named local variables CONIT,CRATE,EL(13),ELCO(13,12),... covering all 218 reals and 37 integers
DINTDY Removed INTEGER IOWND,IOWNS,... + KPP_REAL ROWNS,... + COMMON /DLS001/; variables now reached via host association
DPREPJ Same removal; IER retained as a local integer
DSOLSY Same removal
DSRCOM Replaced body with documented no-op stub (COMMON /DLS001/ no longer exists)
DSTODE Removed INTEGER IOWND,IALTH,... + KPP_REAL CONIT,CRATE,EL,... + COMMON /DLS001/; all state reached via host association
LUNIT Declared with !$OMP THREADPRIVATE in function IXSAV
MESFLG Declared with !$OMP THREADPRIVATE in function IXSAV
STEPMIN Declared with !$OMP THREADPRIVATE in ROOT_Global.f90 or ROOT_Global.F90

@yantosca yantosca added this to the 3.4.0 milestone Apr 23, 2026
@yantosca yantosca self-assigned this Apr 23, 2026
@yantosca yantosca added integrators Related to numerical integrators bugfix Fixes a bug or a technical issue labels Apr 23, 2026
@yantosca
yantosca marked this pull request as draft April 23, 2026 19:47
@yantosca
yantosca force-pushed the feature/lsode-thread-safe branch 3 times, most recently from 114cb08 to 9cb4057 Compare April 23, 2026 20:26
@yantosca
yantosca force-pushed the feature/lsode-thread-safe branch from 9cb4057 to ef2651d Compare May 28, 2026 17:50
@yantosca
yantosca force-pushed the feature/lsode-thread-safe branch 2 times, most recently from 9ee9bde to 97592da Compare June 10, 2026 16:35
@yantosca
yantosca force-pushed the feature/lsode-thread-safe branch 2 times, most recently from 23945ae to 4d1c9b2 Compare June 18, 2026 14:01
@yantosca
yantosca force-pushed the feature/lsode-thread-safe branch 2 times, most recently from 73ef28b to c9bb684 Compare July 8, 2026 15:00
@yantosca
yantosca requested a review from RolfSander July 8, 2026 19:21
@yantosca
yantosca marked this pull request as ready for review July 8, 2026 19:21

@RolfSander RolfSander left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I don't know much about thread-safe coding, so I won't be able to
contribute anything useful here...

@yantosca
yantosca requested review from RolfSander and obin1 and removed request for RolfSander July 9, 2026 15:07
@yantosca

yantosca commented Jul 9, 2026

Copy link
Copy Markdown
Contributor Author

Thanks @RolfSander. We can hold off on merging until someone else reviews it.

@obin1 would you be willing take a look?

yantosca added 3 commits July 15, 2026 13:44
int/lsode.f90
- Refactored (with Claude AI) to be thread-safe.  Removed the
  COMMON /DLS001/ block and replaced with threadprivate variables.

CHANGELOG.md
- Updated accordingly

Signed-off-by: Bob Yantosca <yantosca@seas.harvard.edu>
STEPMIN is written by INTEGRATE() on every call and read back by some
integrators (e.g. the TLM) as the next call's starting step, so a
shared STEPMIN races across OpenMP threads and can leak one thread's
step size into another's. Declare it THREADPRIVATE in gen.c so all
generated _Global.f90 files pick it up (STEPMAX is write-once/read-only
and does not need this).

Also declare LUNIT/MESFLG THREADPRIVATE inside IXSAV in lsode.f90:
despite the COMMON /DLS001/ removal, these SAVE locals still have
static storage shared across all threads and calls, and XERRWD reads
them via IXSAV on every error/warning path.
docs/source/reference/known-bugs.rst
docs/source/tech_info/07_numerical_methods.rst
- Removed warnings that LSODE is not thread safe, as we
  have now fixed this.

CHANGELOG.md
- Updated accordingly

Signed-off-by: Bob Yantosca <yantosca@seas.harvard.edu>
@yantosca
yantosca force-pushed the feature/lsode-thread-safe branch from e2d7568 to 62e914b Compare July 15, 2026 17:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bugfix Fixes a bug or a technical issue integrators Related to numerical integrators

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants