-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Controls the passing of NEURON global params to processes (#165)
* control the passing of NEURON global params to processes * a more general solution in IsolatedProcess * no need to explicitly pass neuron global args * future annotations add import
- Loading branch information
Showing
13 changed files
with
111 additions
and
39 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
"""Controlled simulations in parallel.""" | ||
|
||
from __future__ import annotations | ||
from multiprocessing.pool import Pool | ||
from bluecellulab.simulation.neuron_globals import NeuronGlobals | ||
|
||
|
||
class IsolatedProcess(Pool): | ||
"""Multiprocessing Pool that restricts a worker to run max 1 process. | ||
Use this when running isolated NEURON simulations. Running 2 NEURON | ||
simulations on a single process is to be avoided. Required global | ||
NEURON simulation parameters will automatically be passed to each | ||
worker. | ||
""" | ||
|
||
def __init__(self, processes: int | None = 1): | ||
"""Initialize the IsolatedProcess pool. | ||
Args: | ||
processes: The number of processes to use for running the simulations. | ||
If set to None, then the number returned by os.cpu_count() is used. | ||
""" | ||
neuron_global_params = NeuronGlobals.get_instance().export_params() | ||
super().__init__( | ||
processes=processes, | ||
initializer=self.init_worker, | ||
initargs=(neuron_global_params,), | ||
maxtasksperchild=1, | ||
) | ||
|
||
@staticmethod | ||
def init_worker(neuron_global_params): | ||
"""Load global parameters for the NEURON environment in each worker | ||
process.""" | ||
NeuronGlobals.get_instance().load_params(neuron_global_params) |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
from bluecellulab.simulation.parallel import IsolatedProcess | ||
|
||
|
||
def test_isolated_process(): | ||
"""Test to ensure isolated process keeps its properties.""" | ||
runner = IsolatedProcess() | ||
assert runner._processes == 1 | ||
assert runner._maxtasksperchild == 1 |
This file contains 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