Parallel¶
Helpers shared by the samplers for optional parallel evaluation. See
Parallel evaluation for the narrative version; you rarely need
to touch these directly, since n_workers and backend on the samplers cover
the normal cases.
WorkerPool ¶
A pool held open for the lifetime of a sampler run.
Creating a process pool is expensive - on Windows each worker re-imports the calling module - so a pool rebuilt for every batch of likelihood evaluations can easily cost more than it saves. This opens one pool and reuses it for every batch, then shuts it down at the end.
Use it as a context manager::
with WorkerPool(n_workers=4, backend="auto", func=problem.log_likelihood) as pool:
values = pool.map(problem.log_likelihood, particles)
With n_workers == 1 no pool is created at all and map is a plain
list comprehension, so the serial path stays free of overhead.
Source code in mcmckit/core/parallel.py
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 | |
Methods:¶
map ¶
Apply func to every item, in order.
Falls back to serial evaluation if the pool is not open, so a sampler can call this whether or not it is inside the context manager.
Source code in mcmckit/core/parallel.py
resolve_n_workers ¶
Normalise n_workers; -1 means "one per core".
Source code in mcmckit/core/parallel.py
check_picklable ¶
Whether func survives a pickle round trip.
Process workers receive the function by pickling it. Lambdas, closures and functions defined inside another function cannot be pickled, and the error raised deep inside the executor is unhelpful, so callers check up front.
Source code in mcmckit/core/parallel.py
limit_blas_threads ¶
Pin each worker process to a single BLAS thread.
NumPy and SciPy already spread a single large dot or svd over every
core. Running N such workers in parallel therefore asks for N x cores
threads, and the resulting contention can make the parallel run slower
than the serial one. Since the parallelism here is across likelihood
evaluations, one BLAS thread per worker is the right split.
Called as the process-pool initialiser, before the worker imports NumPy.