Skip to content

Commit

Permalink
Show nbytes for each array and total on visualization (#344)
Browse files Browse the repository at this point in the history
  • Loading branch information
tomwhite authored Jan 8, 2024
1 parent f6a4e9f commit 41a1225
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 1 deletion.
18 changes: 17 additions & 1 deletion cubed/core/plan.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,17 @@ def max_projected_mem(
]
return max(projected_mem_values) if len(projected_mem_values) > 0 else 0

def total_nbytes(self, optimize_graph: bool = True, optimize_function=None) -> int:
"""Return the total number of bytes for all materialized arrays in this plan."""
dag = self._finalize_dag(optimize_graph, optimize_function)
nbytes = 0
for _, d in dag.nodes(data=True):
if d.get("type") == "array":
target = d["target"]
if isinstance(target, (LazyZarrArray, zarr.Array)):
nbytes += target.nbytes
return nbytes

def visualize(
self,
filename="cubed",
Expand All @@ -255,7 +266,8 @@ def visualize(
"rankdir": rankdir,
"label": (
f"num tasks: {self.num_tasks(optimize_graph, optimize_function)}\n"
f"max projected memory: {memory_repr(self.max_projected_mem(optimize_graph, optimize_function))}"
f"max projected memory: {memory_repr(self.max_projected_mem(optimize_graph, optimize_function))}\n"
f"total nbytes: {memory_repr(self.total_nbytes(optimize_graph, optimize_function))}"
),
"labelloc": "bottom",
"labeljust": "left",
Expand Down Expand Up @@ -341,11 +353,13 @@ def visualize(
elif node_type == "array":
target = d["target"]
chunkmem = memory_repr(chunk_memory(target.dtype, target.chunks))
nbytes = None

# materialized arrays are light orange, virtual arrays are white
if isinstance(target, (LazyZarrArray, zarr.Array)):
d["style"] = "filled"
d["fillcolor"] = "#ffd8b1"
nbytes = memory_repr(target.nbytes)
if n in array_display_names:
var_name = array_display_names[n]
d["label"] = f"{n} ({var_name})"
Expand All @@ -356,6 +370,8 @@ def visualize(
tooltip += f"chunks: {target.chunks}\n"
tooltip += f"dtype: {target.dtype}\n"
tooltip += f"chunk memory: {chunkmem}\n"
if nbytes is not None:
tooltip += f"nbytes: {nbytes}\n"

del d["target"]

Expand Down
1 change: 1 addition & 0 deletions cubed/storage/zarr.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ def __init__(
self.shape = template.shape
self.dtype = template.dtype
self.chunks = template.chunks
self.nbytes = template.nbytes

self.store = store
self.fill_value = fill_value
Expand Down
2 changes: 2 additions & 0 deletions cubed/tests/test_optimization.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,12 @@ def test_fusion(spec):
num_created_arrays = 3 # b, c, d (a is not created on disk)
assert d.plan.num_arrays(optimize_graph=False) == num_arrays
assert d.plan.num_tasks(optimize_graph=False) == num_created_arrays + 12
assert d.plan.total_nbytes(optimize_graph=False) == b.nbytes + c.nbytes + d.nbytes
num_arrays = 2 # a, d
num_created_arrays = 1 # d (a is not created on disk)
assert d.plan.num_arrays(optimize_graph=True) == num_arrays
assert d.plan.num_tasks(optimize_graph=True) == num_created_arrays + 4
assert d.plan.total_nbytes(optimize_graph=True) == d.nbytes

task_counter = TaskCounter()
result = d.compute(callbacks=[task_counter])
Expand Down

0 comments on commit 41a1225

Please sign in to comment.