Plan an iterative rechunking from *old_chunks* to *new_chunks*. The plan aims to minimize the rechunk graph size. Parameters ---------- itemsize: int The item size of the array threshold: int The graph growth factor under which we don't bother introducing
(
old_chunks, new_chunks, itemsize, threshold=None, block_size_limit=None
)
| 600 | |
| 601 | |
| 602 | def plan_rechunk( |
| 603 | old_chunks, new_chunks, itemsize, threshold=None, block_size_limit=None |
| 604 | ): |
| 605 | """Plan an iterative rechunking from *old_chunks* to *new_chunks*. |
| 606 | The plan aims to minimize the rechunk graph size. |
| 607 | |
| 608 | Parameters |
| 609 | ---------- |
| 610 | itemsize: int |
| 611 | The item size of the array |
| 612 | threshold: int |
| 613 | The graph growth factor under which we don't bother |
| 614 | introducing an intermediate step |
| 615 | block_size_limit: int |
| 616 | The maximum block size (in bytes) we want to produce during an |
| 617 | intermediate step |
| 618 | |
| 619 | Notes |
| 620 | ----- |
| 621 | No intermediate steps will be planned if any dimension of ``old_chunks`` |
| 622 | is unknown. |
| 623 | """ |
| 624 | threshold = threshold or config.get("array.rechunk.threshold") |
| 625 | block_size_limit = block_size_limit or config.get("array.chunk-size") |
| 626 | if isinstance(block_size_limit, str): |
| 627 | block_size_limit = parse_bytes(block_size_limit) |
| 628 | |
| 629 | has_nans = (any(math.isnan(y) for y in x) for x in old_chunks) |
| 630 | |
| 631 | if len(new_chunks) <= 1 or not all(new_chunks) or any(has_nans): |
| 632 | # Trivial array / unknown dim => no need / ability for an intermediate |
| 633 | return [new_chunks] |
| 634 | |
| 635 | # Make it a number of elements |
| 636 | block_size_limit /= itemsize |
| 637 | |
| 638 | # Fix block_size_limit if too small for either old_chunks or new_chunks |
| 639 | largest_old_block = _largest_block_size(old_chunks) |
| 640 | largest_new_block = _largest_block_size(new_chunks) |
| 641 | block_size_limit = max([block_size_limit, largest_old_block, largest_new_block]) |
| 642 | |
| 643 | # The graph size above which to optimize |
| 644 | graph_size_threshold = _graph_size_threshold(old_chunks, new_chunks, threshold) |
| 645 | |
| 646 | current_chunks = old_chunks |
| 647 | first_pass = True |
| 648 | steps = [] |
| 649 | |
| 650 | while True: |
| 651 | graph_size = estimate_graph_size(current_chunks, new_chunks) |
| 652 | if graph_size < graph_size_threshold: |
| 653 | break |
| 654 | |
| 655 | if first_pass: |
| 656 | chunks = current_chunks |
| 657 | else: |
| 658 | # We hit the block_size_limit in a previous merge pass => |
| 659 | # accept a significant increase in graph size in exchange for |