Skip to content
Snippets Groups Projects
Commit 0f301737 authored by Christoph Alt's avatar Christoph Alt
Browse files

added a likwid_parser

parent 9d1a1154
Branches
1 merge request!6added a likwid_parser
Pipeline #51850 failed with stages
in 56 seconds
from typing import List, Dict, Any
from cbutil.postprocessing.plain_text import json2dict
from cbutil.util import get_from_nested_dict
def get_stat_sub_dict(data: Dict[str, Any], performance_group: str) -> Dict[str, Any]:
"""
Given a dictionary of data and the performance group name, returns the sub-dictionary containing the metric STAT.
Args:
data (Dict[str, Any]): A dictionary of data from a Likwid JSON file.
performance_group (str): The name of the performance group to extract the sub-dictionary from.
Returns:
Dict[str, Any]: The sub-dictionary containing the metric STAT.
"""
return get_from_nested_dict(data, [performance_group, performance_group, 'Metric STAT'])
def extract_metric_value(metric_stat: Dict[str, Any], keys: List[str], suffix: str) -> Dict[str, Any]:
"""
Given the metric STAT sub-dictionary, a list of keys, and a suffix string, returns a dictionary with the key-suffix
pairs from the metric STAT sub-dictionary.
Args:
metric_stat (Dict[str, Any]): The sub-dictionary containing the metric STAT.
keys (List[str]): The list of keys to extract the key-suffix pairs from.
suffix (str): The suffix string to append to each key in the returned dictionary.
Returns:
Dict[str, Any]: The dictionary containing the key-suffix pairs extracted from the metric STAT sub-dictionary.
"""
return {
f'{key} {suffix}': get_from_nested_dict(metric_stat, [key, suffix])
for key in keys
}
def parse_likwid_json(file: str,
performance_group: str,
*,
sum_keys: List[str],
avg_keys: List[str] = [],
min_keys: List[str] = [],
max_keys: List[str] = []) -> Dict[str, Any]:
"""
Given the path to a Likwid JSON file, the name of the performance group, and a set of metric keys, returns a
dictionary containing the extracted metric values.
Args:
file (str): The path to the Likwid JSON file.
performance_group (str): The name of the performance group to extract the metric values from.
sum_keys (List[str]): The list of metric keys to extract the sum values for.
avg_keys (List[str], optional): The list of metric keys to extract the average values for. Defaults to [].
min_keys (List[str], optional): The list of metric keys to extract the minimum values for. Defaults to [].
max_keys (List[str], optional): The list of metric keys to extract the maximum values for. Defaults to [].
Returns:
Dict[str, Any]: A dictionary containing the extracted metric values, with keys of the form "key suffix" (e.g.
"cycles Sum", "L3 cache hit ratio Avg", etc.).
"""
parsed_data = {}
data = json2dict(file)
metric_stat = get_stat_sub_dict(data, performance_group)
for keys, suffix in [(sum_keys, 'Sum'), (avg_keys, 'Avg'), (min_keys, 'Min'), (max_keys, 'Max')]:
parsed_data.update(extract_metric_value(metric_stat, keys, suffix))
return parsed_data
This diff is collapsed.
import json
import tempfile
import os
import pytest
from cbutil.likwid_parser import parse_likwid_json
def test_parse_likwid_json():
# create temporary file with sample JSON data
with tempfile.NamedTemporaryFile(mode='w', delete=False) as tmp_file:
data = {
"Performance group": {
"Performance group":
{
"Metric STAT": {
"metric1": {
"Sum": 10,
"Avg": 5,
"Min": 2,
"Max": 8
},
"metric2": {
"Sum": 20,
"Avg": 10,
"Min": 5,
"Max": 15
}
}
}}
}
json.dump(data, tmp_file)
tmp_file.flush()
# test case 1: all keys specified
expected_output = {
"metric1 Sum": 10,
"metric2 Sum": 20,
"metric1 Avg": 5,
"metric2 Avg": 10,
"metric1 Min": 2,
"metric2 Min": 5,
"metric1 Max": 8,
"metric2 Max": 15
}
assert parse_likwid_json(tmp_file.name,
"Performance group",
sum_keys=["metric1", "metric2"],
avg_keys=["metric1", "metric2"],
min_keys=["metric1", "metric2"],
max_keys=["metric1", "metric2"]) == expected_output
# test case 2: only sum keys specified
expected_output = {
"metric1 Sum": 10,
"metric2 Sum": 20
}
assert parse_likwid_json(tmp_file.name,
"Performance group",
sum_keys=["metric1", "metric2"]) == expected_output
# cleanup temporary file
os.unlink(tmp_file.name)
def test_parse_likwid_json_fail():
with pytest.raises(FileNotFoundError):
parse_likwid_json("invalid_file.json", "Performance group", sum_keys=["metric1"])
def test_parse_likwid_json_real_data():
test_data = "tests/likwid_MEM_DP_fe2ti216bddc_skylakesp2_pardiso_80_1_10_1e-8.json"
sum_keys = ['AVX DP [MFLOP/s] STAT',
'CPI STAT',
'DP [MFLOP/s] STAT',
'Energy DRAM [J] STAT',
'Energy [J] STAT',
'Memory bandwidth [MBytes/s] STAT',
'Memory data volume [GBytes] STAT',
'Memory read bandwidth [MBytes/s] STAT',
'Memory read data volume [GBytes] STAT',
'Memory write bandwidth [MBytes/s] STAT',
'Memory write data volume [GBytes] STAT',
'Operational intensity STAT',
'Packed [MUOPS/s] STAT',
'Power DRAM [W] STAT',
'Power [W] STAT',
'Scalar [MUOPS/s] STAT']
avg_keys = ['Runtime (RDTSC) [s] STAT',
'Clock [MHz] STAT',
'DP [MFLOP/s] STAT',
]
min_keys = ['Clock [MHz] STAT',
'DP [MFLOP/s] STAT', ]
max_keys = min_keys
parse_likwid_json(test_data,
"MEM_DP",
sum_keys=sum_keys,
avg_keys=avg_keys,
min_keys=min_keys,
max_keys=max_keys)
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment