1111
1212# Written by David McDougall, 2022-2026
1313
14- # Disable automatic multithreading
15- import os
16- os .environ ['OPENBLAS_NUM_THREADS' ] = '1'
17- os .environ ['MKL_NUM_THREADS' ] = '1'
18- os .environ ['OMP_NUM_THREADS' ] = '1'
19-
2014from .approx import Approx1D , Approx2D , MatrixSamples
2115from .codegen import Codegen
2216from .inputs import LinearInput , LogarithmicInput
2519from pathlib import Path
2620import multiprocessing
2721import numpy as np
22+ import dill
2823import time
24+ import os
2925import sys
3026
3127__all__ = ('main' , 'LinearInput' , 'LogarithmicInput' )
3228
3329_num_threads = len (os .sched_getaffinity (0 ))
3430_thread_pool = None
35- def _initialize_thread_pool (verbose ):
31+ _derivative = None
32+ def _initialize_thread_pool (model , verbose ):
3633 global _thread_pool
3734 if verbose : print ("Worker pool:" , _num_threads , 'processes' )
3835 # Manually delete any leftover shared memory files from a previous run.
@@ -42,15 +39,27 @@ def _initialize_thread_pool(verbose):
4239 else :
4340 pass # todo
4441 multiprocessing .set_start_method ('spawn' )
45- _thread_pool = multiprocessing .Pool (_num_threads )
42+ _thread_pool = multiprocessing .Pool (
43+ _num_threads ,
44+ _initialize_worker_process ,
45+ (dill .dumps (model .derivative ),)) # Send the derivative function to every worker.
4646 return _thread_pool
4747
48+ def _initialize_worker_process (derivative_pickle ):
49+ # Recv the derivative function.
50+ global _derivative
51+ _derivative = dill .loads (derivative_pickle )
52+ # Disable automatic multithreading
53+ os .environ ['OPENBLAS_NUM_THREADS' ] = '1'
54+ os .environ ['MKL_NUM_THREADS' ] = '1'
55+ os .environ ['OMP_NUM_THREADS' ] = '1'
56+
4857def main (nmodl_filename , inputs , time_step , temperature ,
4958 error , target ,
5059 outfile = None , verbose = False ):
51- _initialize_thread_pool (verbose >= 2 )
5260 # Read and process the NMODL file.
5361 model = LTI_Model (nmodl_filename , inputs , time_step , temperature )
62+ _initialize_thread_pool (model , verbose >= 2 )
5463 if model .num_inputs == 1 : OptimizerClass = Optimize1D
5564 elif model .num_inputs == 2 : OptimizerClass = Optimize2D
5665 else : raise NotImplementedError ('too many inputs.' )
0 commit comments