Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 4 additions & 0 deletions src/executorlib/executor/flux.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from executorlib.standalone.inputcheck import (
check_command_line_argument_lst,
check_init_function,
check_init_function_with_cache,
check_log_obj_size,
check_oversubscribe,
check_plot_dependency_graph,
Expand Down Expand Up @@ -580,6 +581,9 @@ def create_flux_executor(
executor_kwargs["hostname_localhost"] = hostname_localhost
executor_kwargs["log_obj_size"] = log_obj_size
check_init_function(block_allocation=block_allocation, init_function=init_function)
check_init_function_with_cache(
init_function=init_function, cache_directory=cache_directory
)
check_pmi(backend="flux_allocation", pmi=pmi_mode)
check_oversubscribe(
oversubscribe=executor_kwargs.get("openmpi_oversubscribe", False)
Expand Down
4 changes: 4 additions & 0 deletions src/executorlib/executor/single.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
check_command_line_argument_lst,
check_gpus_per_worker,
check_init_function,
check_init_function_with_cache,
check_plot_dependency_graph,
check_refresh_rate,
check_restart_limit,
Expand Down Expand Up @@ -511,6 +512,9 @@ def create_single_node_executor(
executor_kwargs["log_obj_size"] = log_obj_size

check_init_function(block_allocation=block_allocation, init_function=init_function)
check_init_function_with_cache(
init_function=init_function, cache_directory=cache_directory
)
check_gpus_per_worker(gpus_per_worker=executor_kwargs.get("gpus_per_core", 0))
check_command_line_argument_lst(
command_line_argument_lst=executor_kwargs.get("slurm_cmd_args", [])
Expand Down
4 changes: 4 additions & 0 deletions src/executorlib/executor/slurm.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from executorlib.executor.base import BaseExecutor
from executorlib.standalone.inputcheck import (
check_init_function,
check_init_function_with_cache,
check_log_obj_size,
check_plot_dependency_graph,
check_refresh_rate,
Expand Down Expand Up @@ -552,6 +553,9 @@ def create_slurm_executor(
executor_kwargs["log_obj_size"] = log_obj_size
executor_kwargs["pmi_mode"] = pmi_mode
check_init_function(block_allocation=block_allocation, init_function=init_function)
check_init_function_with_cache(
init_function=init_function, cache_directory=cache_directory
)
check_wait_on_shutdown(wait_on_shutdown=wait)
if block_allocation:
executor_kwargs["init_function"] = init_function
Expand Down
17 changes: 17 additions & 0 deletions src/executorlib/standalone/inputcheck.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,23 @@ def check_init_function(
raise ValueError("")


def check_init_function_with_cache(
init_function: Optional[Callable], cache_directory: Optional[str]
) -> None:
"""
Check if init_function and cache_directory are both set, and raise a warning if they are. The cache key is
computed from the submitted function and its arguments only, so it does not reflect the state set by the
init_function, which can lead to stale results being returned from the cache.
"""
if init_function is not None and cache_directory is not None:
warn(
"The init_function and the cache_directory are incompatible: the cache key does not include the "
"state set by the init_function, so functions which depend on this state may return stale results "
"from the cache. See https://github.com/pyiron/executorlib/issues/1084",
stacklevel=2,
)


def check_max_workers_and_cores(
max_workers: Optional[int], max_cores: Optional[int]
) -> None:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@
from pysqa import QueueAdapter

from executorlib.standalone.command_pysqa import pysqa_execute_command, pysqa_terminate
from executorlib.standalone.inputcheck import validate_number_of_cores
from executorlib.standalone.inputcheck import (
check_init_function_with_cache,
validate_number_of_cores,
)
from executorlib.standalone.interactive.spawner import (
BaseSpawner,
set_current_directory_in_environment,
Expand Down Expand Up @@ -233,6 +236,9 @@ def create_pysqa_block_allocation_scheduler(
backend: Optional[str] = None,
validator: Callable = validate_resource_dict,
):
check_init_function_with_cache(
init_function=init_function, cache_directory=cache_directory
)
if executor_kwargs is None:
executor_kwargs = {}
cores_per_worker = executor_kwargs.get("cores", 1)
Expand Down
25 changes: 25 additions & 0 deletions tests/unit/executor/test_single_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,15 @@ def sum_with_wait(a, b):
return a + b


def init_counter():
return {"counter": [0]}


def increment(counter):
counter[0] += 1
return counter[0]


class AddClass:
def __call__(self, a, b):
return a+b
Expand Down Expand Up @@ -103,7 +112,23 @@ def test_cache_error_file(self):
self.assertTrue(os.path.exists(error_out))
os.remove(error_out)

def test_cache_init_function_warning(self):
cache_directory = os.path.abspath("cache_init_function")
with self.assertWarns(UserWarning) as cm:
with SingleNodeExecutor(
max_workers=1,
block_allocation=True,
init_function=init_counter,
cache_directory=cache_directory,
) as exe:
cloudpickle_register(ind=1)
result_lst = [exe.submit(increment).result() for _ in range(3)]
self.assertIn("init_function", str(cm.warning))
# Documents the incompatibility from https://github.com/pyiron/executorlib/issues/1084
self.assertEqual(result_lst, [1, 1, 1])

def tearDown(self):
shutil.rmtree("executorlib_cache", ignore_errors=True)
shutil.rmtree("cache_error", ignore_errors=True)
shutil.rmtree("cache_duplicate", ignore_errors=True)
shutil.rmtree("cache_init_function", ignore_errors=True)
14 changes: 14 additions & 0 deletions tests/unit/standalone/test_inputcheck.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import unittest
import warnings

from executorlib.standalone.inputcheck import (
check_wait_on_shutdown,
Expand All @@ -7,6 +8,7 @@
check_oversubscribe,
check_executor,
check_init_function,
check_init_function_with_cache,
check_nested_flux_executor,
check_flux_log_files,
check_pmi,
Expand Down Expand Up @@ -50,6 +52,18 @@ def test_check_init_function(self):
with self.assertRaises(ValueError):
check_init_function(init_function=1, block_allocation=False)

def test_check_init_function_with_cache(self):
with self.assertWarns(UserWarning):
check_init_function_with_cache(
init_function=lambda: {}, cache_directory="executorlib_cache"
)

def test_check_init_function_with_cache_no_warning(self):
with warnings.catch_warnings():
warnings.simplefilter("error")
check_init_function_with_cache(init_function=None, cache_directory="executorlib_cache")
check_init_function_with_cache(init_function=lambda: {}, cache_directory=None)

def test_check_refresh_rate(self):
with self.assertRaises(ValueError):
check_refresh_rate(refresh_rate=1)
Expand Down
Loading