Skip to content

Commit

Permalink
fix: cache per session (#2729)
Browse files Browse the repository at this point in the history
* fix: cache per session

* ci: Do not delete examples directory
  • Loading branch information
mkundu1 authored Apr 22, 2024
1 parent 8ac0d8a commit 1394d48
Show file tree
Hide file tree
Showing 7 changed files with 129 additions and 113 deletions.
36 changes: 18 additions & 18 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -30,82 +30,82 @@ unittest: unittest-dev-241

unittest-dev-222:
@echo "Running unittests"
@sudo rm -rf /home/ansys/.local/share/ansys_fluent_core/examples
@sudo rm -rf /home/ansys/.local/share/ansys_fluent_core/examples/*
@poetry run python -m pytest --fluent-version=22.2 $(PYTESTEXTRA) || poetry run python -m pytest --fluent-version=22.2 $(PYTESTRERUN)

unittest-dev-231:
@echo "Running unittests"
@sudo rm -rf /home/ansys/.local/share/ansys_fluent_core/examples
@sudo rm -rf /home/ansys/.local/share/ansys_fluent_core/examples/*
@poetry run python -m pytest --fluent-version=23.1 $(PYTESTEXTRA) || poetry run python -m pytest --fluent-version=23.1 $(PYTESTRERUN)

unittest-dev-232:
@echo "Running unittests"
@sudo rm -rf /home/ansys/.local/share/ansys_fluent_core/examples
@sudo rm -rf /home/ansys/.local/share/ansys_fluent_core/examples/*
@poetry run python -m pytest --fluent-version=23.2 $(PYTESTEXTRA) || poetry run python -m pytest --fluent-version=23.2 $(PYTESTRERUN)

unittest-dev-241:
@echo "Running unittests"
@sudo rm -rf /home/ansys/.local/share/ansys_fluent_core/examples
@sudo rm -rf /home/ansys/.local/share/ansys_fluent_core/examples/*
@poetry run python -m pytest --fluent-version=24.1 $(PYTESTEXTRA) || poetry run python -m pytest --fluent-version=24.1 $(PYTESTRERUN)

unittest-dev-242:
@echo "Running unittests"
@sudo rm -rf /home/ansys/.local/share/ansys_fluent_core/examples
@sudo rm -rf /home/ansys/.local/share/ansys_fluent_core/examples/*
@poetry run python -m pytest --fluent-version=24.2 $(PYTESTEXTRA) || poetry run python -m pytest --fluent-version=24.2 $(PYTESTRERUN)

unittest-all-222:
@echo "Running all unittests"
@sudo rm -rf /home/ansys/.local/share/ansys_fluent_core/examples
@sudo rm -rf /home/ansys/.local/share/ansys_fluent_core/examples/*
@poetry run python -m pytest --nightly --fluent-version=22.2 $(PYTESTEXTRA) || poetry run python -m pytest --nightly --fluent-version=22.2 $(PYTESTRERUN)

unittest-all-222-no-codegen:
@echo "Running all unittests"
@sudo rm -rf /home/ansys/.local/share/ansys_fluent_core/examples
@sudo rm -rf /home/ansys/.local/share/ansys_fluent_core/examples/*
@poetry run python -m pytest --nightly --fluent-version=22.2 -m "not codegen_required" $(PYTESTEXTRA) || poetry run python -m pytest --nightly --fluent-version=22.2 -m "not codegen_required" $(PYTESTRERUN)

unittest-all-231:
@echo "Running all unittests"
@sudo rm -rf /home/ansys/.local/share/ansys_fluent_core/examples
@sudo rm -rf /home/ansys/.local/share/ansys_fluent_core/examples/*
@poetry run python -m pytest --nightly --fluent-version=23.1 $(PYTESTEXTRA) || poetry run python -m pytest --nightly --fluent-version=23.1 $(PYTESTRERUN)

unittest-all-231-no-codegen:
@echo "Running all unittests"
@sudo rm -rf /home/ansys/.local/share/ansys_fluent_core/examples
@sudo rm -rf /home/ansys/.local/share/ansys_fluent_core/examples/*
@poetry run python -m pytest --nightly --fluent-version=23.1 -m "not codegen_required" $(PYTESTEXTRA) || poetry run python -m pytest --nightly --fluent-version=23.1 -m "not codegen_required" $(PYTESTRERUN)

unittest-all-232:
@echo "Running all unittests"
@sudo rm -rf /home/ansys/.local/share/ansys_fluent_core/examples
@sudo rm -rf /home/ansys/.local/share/ansys_fluent_core/examples/*
@poetry run python -m pytest --nightly --fluent-version=23.2 $(PYTESTEXTRA) || poetry run python -m pytest --nightly --fluent-version=23.2 $(PYTESTRERUN)

unittest-all-232-no-codegen:
@echo "Running all unittests"
@sudo rm -rf /home/ansys/.local/share/ansys_fluent_core/examples
@sudo rm -rf /home/ansys/.local/share/ansys_fluent_core/examples/*
@poetry run python -m pytest --nightly --fluent-version=23.2 -m "not codegen_required" $(PYTESTEXTRA) || poetry run python -m pytest --nightly --fluent-version=23.2 -m "not codegen_required" $(PYTESTRERUN)

unittest-all-241:
@echo "Running all unittests"
@sudo rm -rf /home/ansys/.local/share/ansys_fluent_core/examples
@sudo rm -rf /home/ansys/.local/share/ansys_fluent_core/examples/*
@poetry run python -m pytest --nightly --fluent-version=24.1 $(PYTESTEXTRA) || poetry run python -m pytest --nightly --fluent-version=24.1 $(PYTESTRERUN)

unittest-all-241-no-codegen:
@echo "Running all unittests"
@sudo rm -rf /home/ansys/.local/share/ansys_fluent_core/examples
@sudo rm -rf /home/ansys/.local/share/ansys_fluent_core/examples/*
@poetry run python -m pytest --nightly --fluent-version=24.1 -m "not codegen_required" $(PYTESTEXTRA) || poetry run python -m pytest --nightly --fluent-version=24.1 -m "not codegen_required" $(PYTESTRERUN)

unittest-all-242:
@echo "Running all unittests"
@sudo rm -rf /home/ansys/.local/share/ansys_fluent_core/examples
@sudo rm -rf /home/ansys/.local/share/ansys_fluent_core/examples/*
@poetry run python -m pytest --nightly --fluent-version=24.2 $(PYTESTEXTRA) || poetry run python -m pytest --nightly --fluent-version=24.2 $(PYTESTRERUN)

unittest-solvermode-242:
@echo "Running all unittests"
@sudo rm -rf /home/ansys/.local/share/ansys_fluent_core/examples
@sudo rm -rf /home/ansys/.local/share/ansys_fluent_core/examples/*
@poetry run python -m pytest --fluent-version=24.2 --solvermode $(PYTESTEXTRA) || poetry run python -m pytest --fluent-version=24.2 --solvermode $(PYTESTRERUN)

unittest-all-242-no-codegen:
@echo "Running all unittests"
@sudo rm -rf /home/ansys/.local/share/ansys_fluent_core/examples
@sudo rm -rf /home/ansys/.local/share/ansys_fluent_core/examples/*
@poetry run python -m pytest --nightly --fluent-version=24.2 -m "not codegen_required" $(PYTESTEXTRA) || poetry run python -m pytest --nightly --fluent-version=24.2 -m "not codegen_required" $(PYTESTRERUN)

api-codegen:
Expand All @@ -122,12 +122,12 @@ build-doc-source:
@sudo rm -rf doc/source/api/solver/datamodel
@sudo rm -rf doc/source/api/solver/tui
@sudo rm -rf doc/source/api/solver/_autosummary/settings
@sudo rm -rf /home/ansys/.local/share/ansys_fluent_core/examples
@sudo rm -rf /home/ansys/.local/share/ansys_fluent_core/examples/*
@xvfb-run make -C doc html

build-all-docs:
@python doc/settings_rstgen.py
@sudo rm -rf /home/ansys/.local/share/ansys_fluent_core/examples
@sudo rm -rf /home/ansys/.local/share/ansys_fluent_core/examples/*
@xvfb-run make -C doc html

compare-flobject:
Expand Down
66 changes: 33 additions & 33 deletions src/ansys/fluent/core/data_model_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,12 @@ def update(self, d: dict[str, Any], d1: dict[str, Any]):
class DataModelCache:
"""Class to manage datamodel cache."""

use_display_name = False

def __init__(self):
self.rules_str_to_cache = defaultdict(dict)
self.rules_str_to_config = {}

class Empty:
"""Class representing unassigned cached state."""

Expand All @@ -121,11 +127,7 @@ def is_unassigned(state: Any) -> bool:
"""
return state is DataModelCache.Empty

rules_str_to_cache = defaultdict(dict)
rules_str_to_config = {}

@staticmethod
def get_config(rules: str, name: str) -> Any:
def get_config(self, rules: str, name: str) -> Any:
"""Get datamodel cache configuration value.
Parameters
Expand All @@ -140,10 +142,9 @@ def get_config(rules: str, name: str) -> Any:
Any
configuration value
"""
return DataModelCache.rules_str_to_config.get(rules, {}).get(name, False)
return self.rules_str_to_config.get(rules, {}).get(name, False)

@staticmethod
def set_config(rules: str, name: str, value: Any):
def set_config(self, rules: str, name: str, value: Any):
"""Set datamodel cache configuration value.
Parameters
Expand All @@ -155,13 +156,17 @@ def set_config(rules: str, name: str, value: Any):
value : Any
configuration value
"""
if rules not in DataModelCache.rules_str_to_config:
DataModelCache.rules_str_to_config[rules] = {}
DataModelCache.rules_str_to_config[rules][name] = value
if rules not in self.rules_str_to_config:
self.rules_str_to_config[rules] = {}
self.rules_str_to_config[rules][name] = value

@staticmethod
def _update_cache_from_variant_state(
rules: str, source: Dict[str, StateType], key: str, state: Variant, updaterFn
self,
rules: str,
source: Dict[str, StateType],
key: str,
state: Variant,
updaterFn,
):
if state.HasField("bool_state"):
updaterFn(source, key, state.bool_state)
Expand All @@ -182,12 +187,12 @@ def _update_cache_from_variant_state(
elif state.HasField("variant_vector_state"):
updaterFn(source, key, [])
for item in state.variant_vector_state.item:
DataModelCache._update_cache_from_variant_state(
self._update_cache_from_variant_state(
rules, source, key, item, lambda d, k, v: d[k].append(v)
)
elif state.HasField("variant_map_state"):
internal_names_as_keys = (
DataModelCache.get_config(rules, "name_key") == NameKey.INTERNAL
self.get_config(rules, "name_key") == NameKey.INTERNAL
)
if ":" in key:
type_, iname = key.split(":", maxsplit=1)
Expand All @@ -213,14 +218,13 @@ def _update_cache_from_variant_state(
source[key] = {}
source = source[key]
for k, v in state.variant_map_state.item.items():
DataModelCache._update_cache_from_variant_state(
self._update_cache_from_variant_state(
rules, source, k, v, dict.__setitem__
)
else:
updaterFn(source, key, None)

@staticmethod
def update_cache(rules: str, state: Variant, deleted_paths: List[str]):
def update_cache(self, rules: str, state: Variant, deleted_paths: List[str]):
"""Update datamodel cache from streamed state.
Parameters
Expand All @@ -232,10 +236,8 @@ def update_cache(rules: str, state: Variant, deleted_paths: List[str]):
deleted_paths : List[str]
list of deleted paths
"""
cache = DataModelCache.rules_str_to_cache[rules]
internal_names_as_keys = (
DataModelCache.get_config(rules, "name_key") == NameKey.INTERNAL
)
cache = self.rules_str_to_cache[rules]
internal_names_as_keys = self.get_config(rules, "name_key") == NameKey.INTERNAL
for deleted_path in deleted_paths:
comps = [x for x in deleted_path.split("/") if x]
sub_cache = cache
Expand Down Expand Up @@ -264,9 +266,7 @@ def update_cache(rules: str, state: Variant, deleted_paths: List[str]):
else:
break
for k, v in state.variant_map_state.item.items():
DataModelCache._update_cache_from_variant_state(
rules, cache, k, v, dict.__setitem__
)
self._update_cache_from_variant_state(rules, cache, k, v, dict.__setitem__)

@staticmethod
def _dm_path_comp(comp):
Expand All @@ -276,8 +276,9 @@ def _dm_path_comp(comp):
def _dm_path_comp_list(obj):
return [DataModelCache._dm_path_comp(comp) for comp in obj.path]

@staticmethod
def get_state(rules: str, obj: object, name_key: Optional[NameKey] = None) -> Any:
def get_state(
self, rules: str, obj: object, name_key: Optional[NameKey] = None
) -> Any:
"""Retrieve state from datamodel cache.
Parameters
Expand All @@ -296,10 +297,10 @@ def get_state(rules: str, obj: object, name_key: Optional[NameKey] = None) -> An
state : Any
cached state
"""
name_key_in_config = DataModelCache.get_config(rules, "name_key")
name_key_in_config = self.get_config(rules, "name_key")
if name_key == None:
name_key = name_key_in_config
cache = DataModelCache.rules_str_to_cache[rules]
cache = self.rules_str_to_cache[rules]
if not len(cache):
return DataModelCache.Empty
comps = DataModelCache._dm_path_comp_list(obj)
Expand All @@ -318,8 +319,7 @@ def get_state(rules: str, obj: object, name_key: Optional[NameKey] = None) -> An
return DataModelCache.Empty
return _CacheImpl(name_key_in_config).transform(cache)

@staticmethod
def set_state(rules: str, obj: object, value: Any):
def set_state(self, rules: str, obj: object, value: Any):
"""Set datamodel cache state.
Parameters
Expand All @@ -331,8 +331,8 @@ def set_state(rules: str, obj: object, value: Any):
value : Any
state
"""
name_key_in_config = DataModelCache.get_config(rules, "name_key")
cache = DataModelCache.rules_str_to_cache[rules]
name_key_in_config = self.get_config(rules, "name_key")
cache = self.rules_str_to_cache[rules]
comps = DataModelCache._dm_path_comp_list(obj)
for i, comp in enumerate(comps):
key, next_cache = _CacheImpl(name_key_in_config).find(cache, comp, None)
Expand Down
7 changes: 4 additions & 3 deletions src/ansys/fluent/core/services/datamodel_se.py
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,7 @@ def __init__(
self.event_streaming = None
self.subscriptions = SubscriptionList()
self.file_transfer_service = file_transfer_service
self.cache = DataModelCache() if pyfluent.DATAMODEL_USE_STATE_CACHE else None

def get_attribute_value(self, rules: str, path: str, attribute: str) -> _TValue:
"""Get attribute value."""
Expand Down Expand Up @@ -856,9 +857,9 @@ def get_remote_state(self) -> Any:

def get_state(self) -> Any:
"""Get state."""
if pyfluent.DATAMODEL_USE_STATE_CACHE:
state = DataModelCache.get_state(self.rules, self, NameKey.DISPLAY)
if DataModelCache.is_unassigned(state):
if self.service.cache is not None:
state = self.service.cache.get_state(self.rules, self, NameKey.DISPLAY)
if self.service.cache.is_unassigned(state):
state = self.get_remote_state()
else:
state = self.get_remote_state()
Expand Down
41 changes: 24 additions & 17 deletions src/ansys/fluent/core/session_pure_meshing.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,14 @@ class PureMeshing(BaseSession):
in this mode.
"""

_rules = [
"workflow",
"meshing",
"MeshingUtilities",
"PartManagement",
"PMFileManagement",
]

def __init__(
self,
fluent_connection: FluentConnection,
Expand Down Expand Up @@ -61,15 +69,26 @@ def __init__(

datamodel_service_se = self._datamodel_service_se
self.datamodel_streams = {}
if pyfluent.DATAMODEL_USE_STATE_CACHE:
for _rules in rules:
if datamodel_service_se.cache is not None:
for rules in PureMeshing._rules:
datamodel_service_se.cache.set_config(
rules,
"name_key",
(
NameKey.DISPLAY
if DataModelCache.use_display_name
else NameKey.INTERNAL
),
)
stream = DatamodelStream(datamodel_service_se)
stream.register_callback(
functools.partial(DataModelCache.update_cache, rules=_rules)
functools.partial(
datamodel_service_se.cache.update_cache, rules=rules
)
)
self.datamodel_streams[_rules] = stream
self.datamodel_streams[rules] = stream
stream.start(
rules=_rules,
rules=rules,
no_commands_diff_state=pyfluent.DATAMODEL_USE_NOCOMMANDS_DIFF_STATE,
)
self._fluent_connection.register_finalizer_cb(stream.stop)
Expand Down Expand Up @@ -189,15 +208,3 @@ def transfer_mesh_to_solvers(
clean_up_mesh_file,
overwrite_previous,
)


rules = [
"workflow",
"meshing",
"MeshingUtilities",
"PartManagement",
"PMFileManagement",
]

for r in rules:
DataModelCache.set_config(r, "name_key", NameKey.INTERNAL)
8 changes: 4 additions & 4 deletions src/ansys/fluent/core/workflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
from typing import Any, Iterable, Iterator, List, Optional, Tuple, Union
import warnings

import ansys.fluent.core as pyfluent
from ansys.fluent.core.data_model_cache import DataModelCache
from ansys.fluent.core.services.datamodel_se import (
PyCallableStateObject,
PyCommand,
Expand Down Expand Up @@ -131,8 +129,10 @@ def _refresh_task_accessors(obj):


def _convert_task_list_to_display_names(workflow_root, task_list):
if pyfluent.DATAMODEL_USE_STATE_CACHE:
workflow_state = DataModelCache.get_state("workflow", workflow_root)
if workflow_root.service.cache is not None:
workflow_state = workflow_root.service.cache.get_state(
"workflow", workflow_root
)
return [workflow_state[f"TaskObject:{x}"]["_name_"] for x in task_list]
else:
_display_names = []
Expand Down
Loading

0 comments on commit 1394d48

Please sign in to comment.