MCPcopy Create free account
hub / github.com/dask/dask / optimize

Function optimize

dask/base.py:535–594  ·  view source on GitHub ↗

Optimize several dask collections at once. Returns equivalent dask collections that all share the same merged and optimized underlying graph. This can be useful if converting multiple collections to delayed objects, or to manually apply the optimizations at strategic points. No

(*args, traverse=True, **kwargs)

Source from the content-addressed store, hash-verified

533
534
535def optimize(*args, traverse=True, **kwargs):
536 """Optimize several dask collections at once.
537
538 Returns equivalent dask collections that all share the same merged and
539 optimized underlying graph. This can be useful if converting multiple
540 collections to delayed objects, or to manually apply the optimizations at
541 strategic points.
542
543 Note that in most cases you shouldn't need to call this function directly.
544
545 Warning::
546
547 This function triggers a materialization of the collections and looses
548 any annotations attached to HLG layers.
549
550 Parameters
551 ----------
552 *args : objects
553 Any number of objects. If a dask object, its graph is optimized and
554 merged with all those of all other dask objects before returning an
555 equivalent dask collection. Non-dask arguments are passed through
556 unchanged.
557 traverse : bool, optional
558 By default dask traverses builtin python collections looking for dask
559 objects passed to ``optimize``. For large collections this can be
560 expensive. If none of the arguments contain any dask objects, set
561 ``traverse=False`` to avoid doing this traversal.
562 optimizations : list of callables, optional
563 Additional optimization passes to perform.
564 **kwargs
565 Extra keyword arguments to forward to the optimization passes.
566
567 Examples
568 --------
569 >>> import dask
570 >>> import dask.array as da
571 >>> a = da.arange(10, chunks=2).sum()
572 >>> b = da.arange(10, chunks=2).mean()
573 >>> a2, b2 = dask.optimize(a, b)
574
575 >>> a2.compute() == a.compute()
576 np.True_
577 >>> b2.compute() == b.compute()
578 np.True_
579 """
580 # TODO: This API is problematic. The approach to using postpersist forces us
581 # to materialize the graph. Most low level optimizations will materialize as
582 # well
583 collections, repack = unpack_collections(*args, traverse=traverse)
584 if not collections:
585 return args
586
587 dsk = collections_to_expr(collections)
588
589 postpersists = []
590 for a in collections:
591 r, s = a.__dask_postpersist__()
592 postpersists.append(r(dsk.__dask_graph__(), *s))

Callers 2

test_optimizeFunction · 0.90
test_optimize_nestedFunction · 0.90

Calls 6

collections_to_exprFunction · 0.85
rFunction · 0.85
repackFunction · 0.85
unpack_collectionsFunction · 0.70
__dask_postpersist__Method · 0.45
__dask_graph__Method · 0.45

Tested by 2

test_optimizeFunction · 0.72
test_optimize_nestedFunction · 0.72