Skip to content

Commit

Permalink
implement new version of remove_parameter_value_pair
Browse files Browse the repository at this point in the history
- use simple python types as arguments
- allows to use white cards for all parameters, value-names and/or value-version
- easier internal implementation
  • Loading branch information
SimeonEhrig committed Feb 15, 2024
1 parent 3c778af commit f317777
Show file tree
Hide file tree
Showing 3 changed files with 512 additions and 30 deletions.
29 changes: 17 additions & 12 deletions bashi/globals.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,21 @@

from typing import List
import packaging.version

from bashi.types import Parameter, ValueName, ValueVersion

# parameter key names, whit special meaning
HOST_COMPILER: str = "host_compiler"
DEVICE_COMPILER: str = "device_compiler"
HOST_COMPILER: Parameter = "host_compiler"
DEVICE_COMPILER: Parameter = "device_compiler"

# name of the used compilers
GCC: str = "gcc"
CLANG: str = "clang"
NVCC: str = "nvcc"
CLANG_CUDA: str = "clang-cuda"
HIPCC: str = "hipcc"
ICPX: str = "icpx"
GCC: ValueName = "gcc"
CLANG: ValueName = "clang"
NVCC: ValueName = "nvcc"
CLANG_CUDA: ValueName = "clang-cuda"
HIPCC: ValueName = "hipcc"
ICPX: ValueName = "icpx"

COMPILERS: List[str] = [GCC, CLANG, NVCC, CLANG_CUDA, HIPCC, ICPX]
COMPILERS: List[ValueName] = [GCC, CLANG, NVCC, CLANG_CUDA, HIPCC, ICPX]

# alpaka backend names
ALPAKA_ACC_CPU_B_SEQ_T_SEQ_ENABLE: str = "alpaka_ACC_CPU_B_SEQ_T_SEQ_ENABLE"
Expand Down Expand Up @@ -47,5 +47,10 @@

OFF: str = "0.0.0"
ON: str = "1.0.0"
OFF_VER: packaging.version.Version = packaging.version.parse(OFF)
ON_VER: packaging.version.Version = packaging.version.parse(ON)
OFF_VER: ValueVersion = packaging.version.parse(OFF)
ON_VER: ValueVersion = packaging.version.parse(ON)

# values are used for remove_parameter_value_pair
ANY_PARAM: Parameter = "*"
ANY_NAME: ValueName = "*"
ANY_VERSION: str = "*"
88 changes: 87 additions & 1 deletion bashi/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import sys
import copy
from collections import OrderedDict
from typing import IO, Dict, List, Optional, Union
from typing import IO, Dict, List, Optional, Union, Callable

import packaging.version
from typeguard import typechecked
Expand All @@ -18,6 +18,7 @@
ParameterValuePair,
ParameterValueSingle,
ParameterValueTuple,
ValueName,
)
from bashi.versions import COMPILERS, VERSIONS, NVCC_GCC_MAX_VERSION, NVCC_CLANG_MAX_VERSION
from bashi.globals import * # pylint: disable=wildcard-import,unused-wildcard-import
Expand Down Expand Up @@ -263,6 +264,91 @@ def filter_function(param_val_pair: ParameterValuePair) -> bool:
return len_before != len(param_val_pairs)


@typechecked
def remove_parameter_value_pair_2( # pylint: disable=too-many-arguments
parameter_value_pairs: List[ParameterValuePair],
parameter1: Parameter = ANY_PARAM,
value_name1: ValueName = ANY_NAME,
value_version1: Union[int, float, str] = ANY_VERSION,
parameter2: Parameter = ANY_PARAM,
value_name2: ValueName = ANY_NAME,
value_version2: Union[int, float, str] = ANY_VERSION,
symmetric: bool = True,
) -> bool:
"""Removes a parameter-value-pair from a list based on the specified search criteria. A
parameter-value pair must match all specified search criteria to be removed if none of the
criteria is `ANY_*`. If a criterion is `ANY_*`, it is ignored and it is always a match.
Args:
parameter_value_pairs (List[ParameterValuePair]): list where parameter-value-pairs will be
removed
parameter1 (Parameter, optional): Name of the first parameter. Defaults to ANY_PARAM.
value_name1 (ValueName, optional): Name of the first value-name. Defaults to ANY_NAME.
value_version1 (Union[int, float, str], optional): Name of the first value-version. Defaults
to ANY_VERSION.
parameter2 (Parameter, optional): Name of the second parameter. Defaults to ANY_PARAM.
value_name2 (ValueName, optional): Name of the second value-name. Defaults to ANY_NAME.
value_version2 (Union[int, float, str], optional): Name of the second value-name. Defaults
to ANY_VERSION.
symmetric (bool, optional): If symmetric is true, it does not matter whether a group of
parameters, value-name and value-version was found in the first or second
parameter-value. If false, it is taken into account whether the search criterion was
found in the first or second parameter value. Defaults to True.
Returns:
bool: Return True, if parameter-value-pair was removed.
"""
filter_list: List[Callable[[ParameterValuePair], bool]] = []
if parameter1 != ANY_PARAM:
filter_list.append(lambda param_val: param_val.first.parameter == parameter1)

if value_name1 != ANY_NAME:
filter_list.append(lambda param_val: param_val.first.parameterValue.name == value_name1)

if value_version1 != ANY_VERSION:
parsed_value_version1 = packaging.version.parse(str(value_version1))
filter_list.append(
lambda param_val: param_val.first.parameterValue.version == parsed_value_version1
)

if parameter2 != ANY_PARAM:
filter_list.append(lambda param_val: param_val.second.parameter == parameter2)

if value_name2 != ANY_NAME:
filter_list.append(lambda param_val: param_val.second.parameterValue.name == value_name2)

if value_version2 != ANY_VERSION:
parsed_value_version2 = packaging.version.parse(str(value_version2))
filter_list.append(
lambda param_val: param_val.second.parameterValue.version == parsed_value_version2
)

def filter_func(param_value_pair: ParameterValuePair) -> bool:
return_value = True

for f in filter_list:
return_value = return_value and f(param_value_pair)

return not return_value

len_before = len(parameter_value_pairs)
parameter_value_pairs[:] = list(filter(filter_func, parameter_value_pairs))

if symmetric:
remove_parameter_value_pair_2(
parameter_value_pairs,
parameter2,
value_name2,
value_version2,
parameter1,
value_name1,
value_version1,
symmetric=False,
)

return len_before != len(parameter_value_pairs)


@typechecked
def check_parameter_value_pair_in_combination_list(
combination_list: CombinationList,
Expand Down
Loading

0 comments on commit f317777

Please sign in to comment.