Skip to content

Commit 2461ef1

Browse files
committed
dsl: Add method to dump memory estimate to JSON
1 parent ac2e109 commit 2461ef1

2 files changed

Lines changed: 41 additions & 0 deletions

File tree

devito/tools/data_structures.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from collections import OrderedDict, deque
22
from collections.abc import Callable, Iterable, MutableSet, Mapping, Set
33
from functools import reduce, cached_property
4+
import json
45

56
import numpy as np
67
from multidict import MultiDict
@@ -672,6 +673,11 @@ class MemoryEstimate(frozendict):
672673
human_readable: frozendict
673674
The mapper, albeit with human-readable memory usage (MB, GB, etc)
674675
rather than raw bytes.
676+
677+
Methods
678+
-------
679+
to_json(path)
680+
Write the memory estimate to a JSON for scheduler ingestion.
675681
"""
676682

677683
def __init__(self, *args, **kwargs):
@@ -690,6 +696,21 @@ def human_readable(self):
690696
def __repr__(self):
691697
return f'{self.__class__.__name__}({self.name}): {self.human_readable._dict}'
692698

699+
def to_json(self, path):
700+
"""
701+
Write the MemoryEstimate to JSON for ingestion by a scheduler.
702+
703+
Arguments
704+
---------
705+
path: str
706+
The path to which the memory estimate should be written.
707+
"""
708+
summary = {'name': self.name, **self._dict}
709+
json_object = json.dumps(summary, indent=4)
710+
711+
with open(path, "w") as outfile:
712+
outfile.write(json_object)
713+
693714

694715
class UnboundTuple(tuple):
695716
"""

tests/test_operator.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1+
import os
12
from itertools import permutations
23
from functools import reduce
34
from operator import mul
45
import logging
6+
import json
57

68
import numpy as np
79
import sympy
@@ -2268,3 +2270,21 @@ def test_device(self, caplog):
22682270

22692271
# Matching memory allocated both on host and device for memmap
22702272
self.parse_output(summary, check)
2273+
2274+
def test_to_json(self):
2275+
grid = Grid(shape=(101, 101))
2276+
f = Function(name='f', grid=grid, space_order=2)
2277+
op = Operator(Eq(f, 1))
2278+
summary = op.estimate_memory()
2279+
2280+
summary.to_json("memory_estimate_output.json")
2281+
2282+
with open("memory_estimate_output.json", "r") as infile:
2283+
json_object = json.load(infile)
2284+
2285+
assert json_object['name'] == summary.name
2286+
assert json_object['host'] == summary['host']
2287+
assert json_object['device'] == summary['device']
2288+
2289+
# Clean up
2290+
os.remove("memory_estimate_output.json")

0 commit comments

Comments
 (0)