Skip to content
This repository was archived by the owner on Feb 26, 2025. It is now read-only.

Commit 64d7fd4

Browse files
authored
Merge pull request #425 from BlueBrain/Issue424
fork subprocess and set neuron dt before ipyp process
2 parents 5a23f52 + 56265ea commit 64d7fd4

File tree

5 files changed

+40
-10
lines changed

5 files changed

+40
-10
lines changed

bluepyopt/deapext/optimisations.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,10 @@ def setup_deap(self):
199199

200200
# Register the evaluation function for the individuals
201201
# import deap_efel_eval1
202-
self.toolbox.register("evaluate", self.evaluator.evaluate_with_lists)
202+
self.toolbox.register(
203+
"evaluate",
204+
self.evaluator.set_neuron_variables_and_evaluate_with_lists
205+
)
203206

204207
# Register the mate operator
205208
self.toolbox.register(

bluepyopt/deapext/optimisationsCMA.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,10 @@ def setup_deap(self):
211211
)
212212

213213
# Register the evaluation function for the individuals
214-
self.toolbox.register("evaluate", self.evaluator.evaluate_with_lists)
214+
self.toolbox.register(
215+
"evaluate",
216+
self.evaluator.set_neuron_variables_and_evaluate_with_lists
217+
)
215218

216219
import copyreg
217220
import types

bluepyopt/ephys/evaluators.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,17 @@ def evaluate_with_lists(self, param_list=None, target='scores'):
216216

217217
return self.objective_list(obj_dict)
218218

219+
def set_neuron_variables_and_evaluate_with_lists(
220+
self, param_list=None, target='scores'
221+
):
222+
"""Set NEURON variables and run evaluation with lists.
223+
224+
Setting the NEURON variables is necessary when using ipyparallel,
225+
since the new subprocesses have pristine NEURON.
226+
"""
227+
self.sim.set_neuron_variables()
228+
return self.evaluate_with_lists(param_list=param_list, target=target)
229+
219230
def evaluate(self, param_list=None, target='scores'):
220231
"""Run evaluation with lists as input and outputs"""
221232

bluepyopt/ephys/protocols.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -245,12 +245,20 @@ def _reduce_method(meth):
245245
copyreg.pickle(types.MethodType, _reduce_method)
246246
import pebble
247247
from concurrent.futures import TimeoutError
248+
import multiprocessing
249+
250+
# Default context for python>=3.8 on macos is spawn.
251+
# Spwan context would reset NEURON properties, such as dt.
252+
multiprocessing_context = multiprocessing.get_context('fork')
248253

249254
if timeout is not None:
250255
if timeout < 0:
251256
raise ValueError("timeout should be > 0")
252-
253-
with pebble.ProcessPool(max_workers=1, max_tasks=1) as pool:
257+
with pebble.ProcessPool(
258+
max_workers=1,
259+
max_tasks=1,
260+
context=multiprocessing_context
261+
) as pool:
254262
tasks = pool.schedule(self._run_func, kwargs={
255263
'cell_model': cell_model,
256264
'param_values': param_values,

bluepyopt/ephys/simulators.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ def __init__(self, dt=None, cvode_active=True, cvode_minstep=None,
2121
"""Constructor
2222
2323
Args:
24-
dt (float): the integration time step used by neuron.
25-
cvode_active (bool): should neuron use the variable time step
24+
dt (float): the integration time step used by Neuron.
25+
cvode_active (bool): should Neuron use the variable time step
2626
integration method
2727
cvode_minstep (float): the minimum time step allowed for a cvode
2828
step. Default is 0.0.
@@ -40,16 +40,15 @@ def __init__(self, dt=None, cvode_active=True, cvode_minstep=None,
4040
self.disable_banner = platform.system() not in ['Windows', 'Darwin']
4141
self.banner_disabled = False
4242
self.mechanisms_directory = mechanisms_directory
43-
self.neuron.h.load_file('stdrun.hoc')
4443

4544
self.dt = dt if dt is not None else self.neuron.h.dt
46-
self.neuron.h.dt = self.dt
4745

48-
self.neuron.h.cvode_active(1 if cvode_active else 0)
4946
self.cvode_minstep_value = cvode_minstep
5047

5148
self.cvode_active = cvode_active
5249

50+
self.set_neuron_variables()
51+
5352
self.random123_globalindex = random123_globalindex
5453

5554
@property
@@ -90,7 +89,7 @@ def _nrn_disable_banner():
9089
# pylint: disable=R0201
9190
@property
9291
def neuron(self):
93-
"""Return neuron module"""
92+
"""Return Neuron module"""
9493

9594
if self.disable_banner and not self.banner_disabled:
9695
NrnSimulator._nrn_disable_banner()
@@ -105,6 +104,12 @@ def neuron(self):
105104

106105
return neuron
107106

107+
def set_neuron_variables(self):
108+
"""Set Neuron variables"""
109+
self.neuron.h.load_file('stdrun.hoc')
110+
self.neuron.h.dt = self.dt
111+
self.neuron.h.cvode_active(1 if self.cvode_active else 0)
112+
108113
def run(
109114
self,
110115
tstop=None,

0 commit comments

Comments
 (0)