@@ -370,20 +370,19 @@ timer.result()
370
370
## Custom parallelization using coroutines
371
371
372
372
Adaptive by itself does not implement a way of sharing partial results between function executions.
373
- Its implementation of parallel computation using executors is minimal by design.
374
- Instead the appropriate way to implement custom parallelization is by using coroutines (asynchronous functions).
373
+ Instead its implementation of parallel computation using executors is minimal by design.
374
+ The appropriate way to implement custom parallelization is by using coroutines (asynchronous functions).
375
375
376
376
We illustrate this approach by using ` dask.distributed ` for parallel computations in part because it supports asynchronous operation out-of-the-box.
377
- Let us consider a function ` f(x) ` which is composed by two parts.
378
- It has a slow part ` g ` which can be reused by multiple inputs and shared between workers.
379
- It has fast part ` h ` that will be computed for every ` x ` .
377
+ Let us consider a function ` f(x) ` which is composed by two parts:
378
+ a slow part ` g ` which can be reused by multiple inputs and shared across function evaluations and a fast part ` h ` that will be computed for every ` x ` .
380
379
381
380
``` {code-cell} ipython3
382
381
import time
383
382
384
383
def f(x):
385
384
"""
386
- Integer part of `x` will be reused
385
+ Integer part of `x` repeats and should be reused
387
386
Decimal part requires a new computation
388
387
"""
389
388
return g(int(x)) + h(x % 1)
@@ -400,7 +399,7 @@ def h(x):
400
399
return x**3
401
400
```
402
401
403
- We need to convert ` f ` into a dask graph by using ` dask.delayed ` .
402
+ In order to combine reuse of values of ` g ` with adaptive, we need to convert ` f ` into a dask graph by using ` dask.delayed ` .
404
403
405
404
``` {code-cell} ipython3
406
405
from dask import delayed
@@ -432,15 +431,15 @@ async def f_parallel(x):
432
431
return await future_f
433
432
```
434
433
435
- Finally we provide the asynchronous function to the ` learner ` and run it via ` AsyncRunner ` .
434
+ To run the adaptive evaluation we provide the asynchronous function to the ` learner ` and run it via ` AsyncRunner ` without specifying an executor .
436
435
437
436
``` {code-cell} ipython3
438
437
learner = adaptive.Learner1D(f_parallel, bounds=(-3.5, 3.5))
439
438
440
439
runner = adaptive.AsyncRunner(learner, goal=lambda l: l.loss() < 0.01, ntasks=20)
441
440
```
442
441
443
- We await for the runner to finish, and then plot the result.
442
+ Finally we await for the runner to finish, and then plot the result.
444
443
445
444
``` {code-cell} ipython3
446
445
await runner.task
0 commit comments