10000 Understanding `CmdStanModel` Behavior with `os.fork` · Issue #780 · stan-dev/cmdstanpy · GitHub
[go: up one dir, main page]

Skip to content
Understanding CmdStanModel Behavior with os.fork #780
@brucejwittmann

Description

@brucejwittmann

I've run into a somewhat odd (and likely edge-case) situation where CmdStanModel.sample behaves unexpectedly after a call to os.fork. I'm providing pseudocode now in the hopes that there is a fairly simple explanation, but happy to make a minimum working example if need be.

Say I run something structured like the following:

import os
from cmdstanpy import CmdStanModel

def run_job():
    # Compile the model
    mod = CmdStanModel(stanfile, cpp_options={"STAN_THREADS": True})
    
    # Run sampling on a detached child process
    pid = os.fork()
    if pid == 0:
        os.setsid()
        mod.sample(data = data, chains=4)
        os._exit()
    else:
        return pid

Even though my CPU has 32 cores, this will only run two chains at once--specifically, Chains 1 and 3 run, then Chains 2 and 4 run. If, however, I remove the cpp_options={"STAN_THREADS": True} kwarg, then everything behaves as expected: all chains sample in parallel.

Is there any immediate explanation for this discrepancy in behavior?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0