Skip to content

Commit

Permalink
changed pyyaml to ruamel.yaml
Browse files Browse the repository at this point in the history
  • Loading branch information
JanLJL committed Jan 11, 2024
1 parent 7120640 commit 26f6130
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 15 deletions.
5 changes: 4 additions & 1 deletion kerncraft/cachetile.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand Down Expand Up @@ -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
Expand Down
7 changes: 5 additions & 2 deletions kerncraft/kerncraft.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
10 changes: 6 additions & 4 deletions kerncraft/machinemodel.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down
9 changes: 4 additions & 5 deletions kerncraft/prefixedunit.py
Original file line number Diff line number Diff line change
@@ -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<value>[0-9]+(?:\.[0-9]+)?) (?P<prefix>[kMGTP])?(?P<unit>.*)$'))
Expand Down Expand Up @@ -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)
4 changes: 3 additions & 1 deletion kerncraft/roofline-plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from pprint import pprint

import matplotlib.pyplot as plt
from ruamel import yaml
import ruamel.yaml

from .prefixedunit import PrefixedUnit

Expand All @@ -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"
Expand Down
7 changes: 5 additions & 2 deletions tests/test_kernel.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down

0 comments on commit 26f6130

Please sign in to comment.