In [1]:
from math import sqrt
from joblib import Parallel, delayed
In [3]:
%time Parallel(n_jobs=2)(delayed(sqrt)(i ** 2) for i in range(10))
Out[3]:
In [4]:
# using the threading backend
In [5]:
%time Parallel(n_jobs=2, backend="threading")(delayed(sqrt)(i ** 2) for i in range(10))
Out[5]:
Some algorithms require to make several consecutive calls to a parallel function interleaved with processing of the intermediate results. Calling Parallel several times in a loop is sub-optimal because it will create and destroy a pool of workers (threads or processes) several times which can cause a significant overhead.
For this case it is more efficient to use the context manager API of the Parallel class to re-use the same pool of workers for several calls to the Parallel object:
In [6]:
with Parallel(n_jobs=2) as parallel:
accumulator = 0.
n_iter = 0
while accumulator < 1000:
results = parallel(delayed(sqrt)(accumulator + i ** 2)for i in range(5))
accumulator += sum(results) # synchronization barrier
n_iter += 1
In [8]:
(accumulator, n_iter)
Out[8]:
In [9]:
import numpy as np
from joblib import Parallel, delayed
from joblib.pool import has_shareable_memory
Parallel(n_jobs=2, max_nbytes=1e6)(
delayed(has_shareable_memory)(np.ones(int(i)))
for i in [1e2, 1e4, 1e6])
In [12]:
import joblib
In [14]:
joblib.__version__
Out[14]:
In [ ]: