Skip to content

Commit 95699c5

Browse files
committed
compiler: Prettier table for estimate_memory
1 parent e97d597 commit 95699c5

1 file changed

Lines changed: 30 additions & 12 deletions

File tree

devito/operator/operator.py

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -872,29 +872,47 @@ def estimate_memory(self, human_readable=True, **kwargs):
872872
# Build the arguments list for which to get the memory consumption
873873
# This is so that the estimate will factor in overrides
874874
args = self.arguments(**kwargs)
875-
mem = args.nbytes_avail_mapper
875+
mem = args.nbytes_consumed
876876

877877
if human_readable:
878-
# TODO: Fill real values in here
879-
# TODO: Format these values to have 3 digits and suitable units
878+
def fancy_units(v):
879+
"""
880+
Automatically format an integer number of bytes
881+
to KB, MB, GB, and so forth.
882+
"""
883+
width = 10
884+
units = {0: "B ", 1: "KB", 2: "MB", 3: "GB"}
885+
886+
if v == 0:
887+
return "0B".center(width)
888+
889+
key = int(np.log2(v)/10)
890+
unit = units.get(key, "TB")
891+
val = v/(2**(10*key))
892+
893+
return f"{val:.4g}{unit}".center(10)
880894

881895
headline = f"Memory consumption for operator `{self.name}`:"
882-
# Table is 28 characters wide
883-
lpad = " "*((len(headline) - 28) // 2)
896+
w = len(headline)
897+
fdisk = fancy_units(mem[disk_layer])
898+
fhost = fancy_units(mem[host_layer])
899+
fdevice = fancy_units(mem[device_layer])
900+
884901
info(
885902
"\n"
886903
f"{headline}\n"
887-
f"{lpad}┌────────┬────────┬────────\n"
888-
f"{lpad}│ Disk │ Host Device \n"
889-
f"{lpad}├────────┼────────┼────────\n"
890-
f"{lpad}│ 1 │ 2 │ 3 │\n"
891-
f"{lpad}└────────┴────────┴────────\n"
904+
f"{'┌──────────┬──────────┬──────────┐'.center(w)}\n"
905+
f"{' Disk Host Device │'.center(w)}\n"
906+
f"{'├──────────┼──────────┼──────────┤'.center(w)}\n"
907+
f"{f'│{fdisk}{fhost}{fdevice}│'.center(w)}\n"
908+
f"{'└──────────┴──────────┴──────────┘'.center(w)}\n"
892909
)
910+
911+
# TODO: add hinting if the specified operator won't fit
912+
893913
else:
894914
info(f"{self.name} {mem[disk_layer]} {mem[host_layer]} {mem[device_layer]}")
895915

896-
from IPython import embed; embed()
897-
898916
def apply(self, **kwargs):
899917
"""
900918
Execute the Operator.

0 commit comments

Comments
 (0)