From 26f61309a5fd77a16041ca269121db6322ffa64c Mon Sep 17 00:00:00 2001 From: JanLJL Date: Thu, 11 Jan 2024 14:12:38 +0100 Subject: [PATCH] changed pyyaml to ruamel.yaml --- kerncraft/cachetile.py | 5 ++++- kerncraft/kerncraft.py | 7 +++++-- kerncraft/machinemodel.py | 10 ++++++---- kerncraft/prefixedunit.py | 9 ++++----- kerncraft/roofline-plot.py | 4 +++- tests/test_kernel.py | 7 +++++-- 6 files changed, 27 insertions(+), 15 deletions(-) diff --git a/kerncraft/cachetile.py b/kerncraft/cachetile.py index dd93307..2b14086 100755 --- a/kerncraft/cachetile.py +++ b/kerncraft/cachetile.py @@ -3,11 +3,12 @@ import sys import sympy -from ruamel import yaml +import ruamel.yaml from . import models from .kernel import KernelDescription from .machinemodel import MachineModel +from .prefixedunit import PrefixedUnit def create_parser(): @@ -49,6 +50,8 @@ def run(parser, args): # process kernel description description = str(args.description_file.read()) + yaml = ruamel.yaml.YAML(typ='unsafe') + yaml.register_class(PrefixedUnit) kernel = KernelDescription(yaml.load(description)) # Add constants from define arguments diff --git a/kerncraft/kerncraft.py b/kerncraft/kerncraft.py index 54e5d33..7031c16 100755 --- a/kerncraft/kerncraft.py +++ b/kerncraft/kerncraft.py @@ -16,10 +16,11 @@ import io from collections import OrderedDict -from ruamel import yaml +import ruamel.yaml from . import models from . import __version__ +from .prefixedunit import PrefixedUnit from .kernel import KernelCode, KernelDescription, symbol_pos_int from .machinemodel import MachineModel from .pycparser_utils import clean_code @@ -327,7 +328,9 @@ def run(parser, args, output_file=sys.stdout): else: description = str(args.code_file.read()) args.code_file.close() - kernel = KernelDescription(yaml.load(description, Loader=yaml.Loader), machine=machine) + yaml = ruamel.yaml.YAML(typ='unsafe') + yaml.register_class(PrefixedUnit) + kernel = KernelDescription(yaml.load(description), machine=machine) loop_indices = set([symbol_pos_int(l['index']) for l in kernel.get_loop_stack()]) # define constants diff --git a/kerncraft/machinemodel.py b/kerncraft/machinemodel.py index 02a442b..173ae58 100755 --- a/kerncraft/machinemodel.py +++ b/kerncraft/machinemodel.py @@ -15,8 +15,7 @@ from functools import lru_cache import psutil -from ruamel import yaml -from ruamel.yaml.comments import CommentedMap +import ruamel.yaml import cachesim from sympy.parsing.sympy_parser import parse_expr @@ -156,12 +155,14 @@ def __init__(self, path_to_yaml=None, machine_yaml=None, args=None): self._path = path_to_yaml self._args = args if path_to_yaml: + yaml = ruamel.yaml.YAML(typ='unsafe') + yaml.register_class(PrefixedUnit) # Load into cache and save to self._data abspath_to_yaml = os.path.abspath(path_to_yaml) if abspath_to_yaml not in self._loaded_machine_yaml: with open(path_to_yaml, 'r') as f: # Ignore ruamel unsafe loading warning, by supplying Loader parameter - self._loaded_machine_yaml[abspath_to_yaml] = yaml.load(f, Loader=yaml.Loader) + self._loaded_machine_yaml[abspath_to_yaml] = yaml.load(f) self._data = self._loaded_machine_yaml[abspath_to_yaml] elif machine_yaml: self._data = machine_yaml @@ -612,7 +613,8 @@ def dump(self, f=None): """ Return YAML string to store machine model and store to f (if path or fp passed). """ - yaml_string = yaml.dump(self._data, Dumper=yaml.Dumper) + yaml = ruamel.yaml.YAML() + yaml_string = yaml.dump(self._data) if f is None: f = self._path diff --git a/kerncraft/prefixedunit.py b/kerncraft/prefixedunit.py index 578af3b..8268a30 100755 --- a/kerncraft/prefixedunit.py +++ b/kerncraft/prefixedunit.py @@ -1,16 +1,15 @@ #!/usr/bin/env python3 import re -from ruamel import yaml +import ruamel.yaml -class PrefixedUnit(yaml.YAMLObject): +class PrefixedUnit(ruamel.yaml.YAMLObject): PREFIXES = {'k': 1e3, 'M': 1e6, 'G': 1e9, 'T': 1e13, 'P': 1e16, 'E': 1e19, 'Z': 1e21, 'Y': 1e24, '': 1} - yaml_loader = yaml.Loader - yaml_dumper = yaml.Dumper + yaml = ruamel.yaml.YAML() yaml_tag = u'!prefixed' yaml_implicit_pattern = re.compile(re.compile( r'^(?P[0-9]+(?:\.[0-9]+)?) (?P[kMGTP])?(?P.*)$')) @@ -204,4 +203,4 @@ def __ne__(self, other): # Make this tag automatic -yaml.add_implicit_resolver(PrefixedUnit.yaml_tag, PrefixedUnit.yaml_implicit_pattern) +ruamel.yaml.add_implicit_resolver(PrefixedUnit.yaml_tag, PrefixedUnit.yaml_implicit_pattern) diff --git a/kerncraft/roofline-plot.py b/kerncraft/roofline-plot.py index a896c52..1b0351d 100755 --- a/kerncraft/roofline-plot.py +++ b/kerncraft/roofline-plot.py @@ -2,7 +2,7 @@ from pprint import pprint import matplotlib.pyplot as plt -from ruamel import yaml +import ruamel.yaml from .prefixedunit import PrefixedUnit @@ -27,6 +27,8 @@ def frange(start, stop, step=1.0): {'performance': PrefixedUnit(11175000000.0, '', 'FLOP/s'), 'bandwidth': PrefixedUnit(22.35, u'G', u'B/s'), 'arithmetic intensity': 0.5, 'bw kernel': 'triad', 'level': 'L3-MEM'}]} +yaml = ruamel.yaml.YAML(typ='unsafe') +yaml.register_class(PrefixedUnit) machine = yaml.load(open('machine-files/emmy.yaml')) max_flops = machine['clock']*sum(machine['FLOPs per cycle']['DP'].values()) max_flops.unit = "FLOP/s" diff --git a/tests/test_kernel.py b/tests/test_kernel.py index d0a36d0..c1eff24 100644 --- a/tests/test_kernel.py +++ b/tests/test_kernel.py @@ -5,19 +5,22 @@ import os import unittest -from ruamel import yaml +import ruamel.yaml from kerncraft.kernel import KernelCode, KernelDescription +from kerncraft.prefixedunit import PrefixedUnit class TestKernel(unittest.TestCase): def setUp(self): + yaml = ruamel.yaml.YAML(typ='unsafe') + yaml.register_class(PrefixedUnit) with open(self._find_file('2d-5pt.c')) as f: self.twod_code = f.read() with open(self._find_file('3d-7pt.c')) as f: self.threed_code = f.read() with open(self._find_file('2d-5pt.yml')) as f: - self.twod_description = yaml.load(f.read(), Loader=yaml.Loader) + self.twod_description = yaml.load(f) with open(self._find_file('copy-2d-linearized.c')) as f: self.twod_linear = f.read()