Skip to content

Commit

Permalink
Use global config
Browse files Browse the repository at this point in the history
  • Loading branch information
ZzEeKkAa committed Apr 18, 2023
1 parent eb8650b commit 4d89433
Show file tree
Hide file tree
Showing 14 changed files with 392 additions and 379 deletions.
9 changes: 1 addition & 8 deletions dpbench/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,9 @@
#
# SPDX-License-Identifier: Apache-2.0

from .runner import (
list_available_benchmarks,
list_possible_implementations,
run_benchmark,
run_benchmarks,
)
from .runner import run_benchmark, run_benchmarks

__all__ = [
"run_benchmark",
"run_benchmarks",
"list_available_benchmarks",
"list_possible_implementations",
]
68 changes: 13 additions & 55 deletions dpbench/config/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,63 +9,21 @@
to provide a structured way to define and store benchmark data.
"""

import json
import os

from .benchmark import Benchmark
from .benchmark import Benchmark, BenchmarkImplementation
from .config import Config
from .framework import Framework
from .implementaion_postfix import Implementation


def read_configs(dirname: str = os.path.dirname(__file__)) -> Config:
"""Read all configuration files and populates those settings into Config."""
C: Config = Config([], [], [])

impl_postfix_file = os.path.join(dirname, "../configs/impl_postfix.json")
bench_info_dir = os.path.join(dirname, "../configs/bench_info")
framework_info_dir = os.path.join(dirname, "../configs/framework_info")

for bench_info_file in os.listdir(bench_info_dir):
if not bench_info_file.endswith(".json"):
continue

bench_info_file = os.path.join(bench_info_dir, bench_info_file)

with open(bench_info_file) as file:
file_contents = file.read()

bench_info = json.loads(file_contents)
benchmark = Benchmark.from_dict(bench_info.get("benchmark"))
C.benchmarks.append(benchmark)

for framework_info_file in os.listdir(framework_info_dir):
if not framework_info_file.endswith(".json"):
continue

framework_info_file = os.path.join(
framework_info_dir, framework_info_file
)

with open(framework_info_file) as file:
file_contents = file.read()

framework_info = json.loads(file_contents)
framework_dict = framework_info.get("framework")
if framework_dict:
framework = Framework.from_dict(framework_dict)
C.frameworks.append(framework)

with open(impl_postfix_file) as file:
file_contents = file.read()

implementaion_postfixes = json.loads(file_contents)
for impl in implementaion_postfixes:
implementation = Implementation.from_dict(impl)
C.implementations.append(implementation)

return C

from .implementation_postfix import Implementation
from .reader import read_configs

"""Use this variable for reading configurations"""
C: Config = read_configs()
GLOBAL: Config = read_configs()

__all__ = [
"GLOBAL",
"Benchmark",
"BenchmarkImplementation",
"Config",
"Framework",
"Implementation",
]
106 changes: 78 additions & 28 deletions dpbench/config/benchmark.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

"""Benchmark related configuration classes."""

from dataclasses import dataclass
from dataclasses import dataclass, field
from typing import Any, List

Parameters = dict[str, Any]
Expand All @@ -16,56 +16,92 @@
class Init:
"""Configuration for benchmark initialization."""

func_name: str
input_args: List[str]
output_args: List[str]
func_name: str = ""
package_path: str = ""
module_name: str = ""
input_args: List[str] = field(default_factory=list)
output_args: List[str] = field(default_factory=list)

@staticmethod
def from_dict(obj: Any) -> "Init":
"""Convert object into Init dataclass."""
_func_name = str(obj.get("func_name"))
_func_name = str(obj.get("func_name") or "")
_package_path = str(obj.get("package_path") or "")
_module_name = str(obj.get("module_name") or "")
_input_args = obj.get("input_args")
_output_args = obj.get("output_args")
return Init(_func_name, _input_args, _output_args)
return Init(
_func_name, _package_path, _module_name, _input_args, _output_args
)

def __post_init__(self):
"""Post initialization hook for dataclass. Not for direct use."""
self.func_name = self.func_name or "initialize"


@dataclass
class BenchmarkImplementation:
"""Configuration for benchmark initialization."""

postfix: str
func_name: str
module_name: str
package_path: str

@staticmethod
def from_dict(obj: Any) -> "BenchmarkImplementation":
"""Convert object into Init dataclass."""
_postfix = str(obj.get("postfix"))
_func_name = str(obj.get("func_name"))
_module_name = str(obj.get("module_name"))
_package_path = str(obj.get("package_path"))
return BenchmarkImplementation(
_postfix, _func_name, _module_name, _package_path
)


@dataclass
class Benchmark:
"""Configuration with benchmark information."""

name: str
short_name: str
relative_path: str
module_name: str
func_name: str
kind: str
domain: str
parameters: Presets
init: Init
input_args: List[str]
array_args: List[str]
output_args: List[str]
name: str = ""
short_name: str = ""
relative_path: str = ""
module_name: str = ""
package_path: str = ""
func_name: str = ""
kind: str = ""
domain: str = ""
parameters: Presets = field(default_factory=Presets)
init: Init = field(default_factory=Init)
input_args: List[str] = field(default_factory=list)
array_args: List[str] = field(default_factory=list)
output_args: List[str] = field(default_factory=list)
implementations: List[BenchmarkImplementation] = field(default_factory=list)

@staticmethod
def from_dict(obj: Any) -> "Benchmark":
"""Convert object into Benchamrk dataclass."""
_name = str(obj.get("name"))
_short_name = str(obj.get("short_name"))
_relative_path = str(obj.get("relative_path"))
_module_name = str(obj.get("module_name"))
_func_name = str(obj.get("func_name"))
_kind = str(obj.get("kind"))
_domain = str(obj.get("domain"))
_name = str(obj.get("name") or "")
_short_name = str(obj.get("short_name") or "")
_relative_path = str(obj.get("relative_path") or "")
_module_name = str(obj.get("module_name") or "")
_packge_path = str(obj.get("package_path") or "")
_func_name = str(obj.get("func_name") or "")
_kind = str(obj.get("kind") or "")
_domain = str(obj.get("domain") or "")
_parameters = Presets(obj.get("parameters"))
_init = Init.from_dict(obj.get("init"))
_input_args = obj.get("input_args")
_array_args = obj.get("input_args")
_output_args = obj.get("input_args")
_input_args = obj.get("input_args") or []
_array_args = obj.get("array_args") or []
_output_args = obj.get("output_args") or []
_implementations = obj.get("implementations") or []
return Benchmark(
_name,
_short_name,
_relative_path,
_module_name,
_packge_path,
_func_name,
_kind,
_domain,
Expand All @@ -74,4 +110,18 @@ def from_dict(obj: Any) -> "Benchmark":
_input_args,
_array_args,
_output_args,
_implementations,
)

def __post_init__(self):
"""Post initialization hook for dataclass. Not for direct use."""
if self.package_path == "":
self.package_path = f"dpbench.benchmarks.{self.module_name}"

if self.init.module_name == "":
self.init.module_name = f"{self.module_name}_initialize"

if self.init.package_path == "":
self.init.package_path = (
f"{self.package_path}.{self.init.module_name}"
)
2 changes: 1 addition & 1 deletion dpbench/config/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

from .benchmark import Benchmark
from .framework import Framework
from .implementaion_postfix import Implementation
from .implementation_postfix import Implementation


@dataclass
Expand Down
16 changes: 10 additions & 6 deletions dpbench/config/framework.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,17 @@ class Framework:
prefix: str
class_: str
arch: str
sycl_device: str

@staticmethod
def from_dict(obj: Any) -> "Framework":
"""Convert object into Framework dataclass."""
_simple_name = str(obj.get("simple_name"))
_full_name = str(obj.get("full_name"))
_prefix = str(obj.get("prefix"))
_class = str(obj.get("class"))
_arch = str(obj.get("arch"))
return Framework(_simple_name, _full_name, _prefix, _class, _arch)
_simple_name = str(obj.get("simple_name") or "")
_full_name = str(obj.get("full_name") or "")
_prefix = str(obj.get("prefix") or "")
_class = str(obj.get("class") or "")
_arch = str(obj.get("arch") or "")
_sycl_device = str(obj.get("sycl_device") or "")
return Framework(
_simple_name, _full_name, _prefix, _class, _arch, _sycl_device
)
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
class Implementation:
"""Configuration with implementation information."""

impl_postfix: str
postfix: str
description: str

@staticmethod
Expand Down
Loading

0 comments on commit 4d89433

Please sign in to comment.