Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: cache per session #2729

Merged
merged 2 commits into from
Apr 22, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
7 changes: 0 additions & 7 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
from packaging.version import Version
import pytest

from ansys.fluent.core.data_model_cache import DataModelCache
from ansys.fluent.core.utils.fluent_version import FluentVersion

_fluent_release_version = FluentVersion.current_release().value
Expand Down Expand Up @@ -77,12 +76,6 @@ def run_before_each_test(
monkeypatch.setenv("PYFLUENT_TEST_NAME", request.node.name)


@pytest.fixture(autouse=True)
def clear_datamodel_cache():
yield
DataModelCache.rules_str_to_cache.clear()


class Helpers:
"""Can be reused to provide helper methods to tests."""

Expand Down
Loading
Loading