Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multiprocessing behavior differs across Ubuntu/MacOS #2842

Open
anilbey opened this issue Apr 18, 2024 · 6 comments
Open

Multiprocessing behavior differs across Ubuntu/MacOS #2842

anilbey opened this issue Apr 18, 2024 · 6 comments
Labels

Comments

@anilbey
Copy link
Contributor

anilbey commented Apr 18, 2024

Context

Overview of the issue

Neuron global parameters are passed to sub processes on Ubuntu.
Neuron global parameters are not passed to sub processes on MacOS.

Expected result/behavior

The behavior should not differ between operating systems.

NEURON setup

NEURON 8.2.4

Minimal working example - MWE

MWE that can be used for reproducing the issue and testing. A couple of examples:

  • python script:
import neuron
from multiprocessing.pool import Pool
import os

def f(_):
    print(f" inside the process: {neuron.h.celsius}")
    print(f" process id: {os.getpid()}")


if __name__ == '__main__':
    print("the main process")
    print(neuron.h.celsius)
    neuron.h.celsius = 37.0
    print(neuron.h.celsius)

    with Pool(5) as p:
        p.map(f, range(5))

Prints the following on Ubuntu

❯ python reproducer.py
the main process
6.3
37.0
celsius is: 37.0
 process id: 22858
celsius is: 37.0
 process id: 22859
celsius is: 37.0
 process id: 22860
celsius is: 37.0
 process id: 22861
celsius is: 37.0
 process id: 22862

Prints this on MacOS

> python reproducer.py
the main process
6.3
37.0
celsius is: 6.3
 process id: 4712
celsius is: 6.3
 process id: 4712
celsius is: 6.3
 process id: 4712
celsius is: 6.3
 process id: 4712
celsius is: 6.3
 process id: 4712

The issue applies to all global parameters.

@anilbey anilbey added the bug label Apr 18, 2024
@anilbey
Copy link
Contributor Author

anilbey commented Apr 18, 2024

@pramodk
Copy link
Member

pramodk commented Apr 22, 2024

@anilbey : could you check this #2094 (comment) ? Just from the title, without looking into details, I wonder if it's multiprocessing module itself and not neuron.

@anilbey
Copy link
Contributor Author

anilbey commented Apr 22, 2024

Yes it is the same issue. Thanks for pointing to it. It is indeed the difference of the multiprocessing module between the platforms.

However, this subtle but critical implementation difference in the behavior is very difficult for the users of NEURON to spot. When not considered, the simulation results become corrupted. Can we not make a decision between the fork mode / spawn mode at the NEURON level and hide this detail (and responsibility) from the Python users?

@pramodk
Copy link
Member

pramodk commented May 7, 2024

thanks for confirming, @anilbey!

As neuron is loaded into Python as a C/C++ library and has a global state, I don't have clarity into how such a thing could be ("easily") achieved on the NEURON side.

Tagging @ferdonline and @1uc if they have any thoughts/ideas.

@1uc
Copy link
Collaborator

1uc commented May 8, 2024

I don't immediately see a quick fix. In a library, e.g. bluepyopt, you can use get_context:
https://docs.python.org/3/library/multiprocessing.html#contexts-and-start-methods
(half way down the section).

Likely something close to:

import multiprocessing

# ...

    ctx = multiprocessing.get_context("fork")
    with ctx.Pool(5) as p:
        p.map(f, range(5))

@anilbey
Copy link
Contributor Author

anilbey commented May 8, 2024

Thanks for the information.

And also the maxtasksperchild=1, argument has to be set in the pool running NEURON to make sure one run does not cause side-effect on the others.
https://docs.python.org/3/library/multiprocessing.html#multiprocessing.pool.Pool

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants