diff --git a/src/ert/config/ert_config.py b/src/ert/config/ert_config.py index a1010e75961..6064f7dc802 100644 --- a/src/ert/config/ert_config.py +++ b/src/ert/config/ert_config.py @@ -25,7 +25,6 @@ from ert.plugins import ErtPluginManager from ert.substitutions import Substitutions -from ._get_num_cpu import get_num_cpu_from_data_file from .analysis_config import AnalysisConfig from .ensemble_config import EnsembleConfig from .forward_model_step import ( @@ -457,6 +456,9 @@ def from_dict(cls, config_dict) -> Self: ] ) queue_config = QueueConfig.from_dict(config_dict) + + substitutions[""] = str(queue_config.preferred_num_cpu) + except ConfigValidationError as err: errors.append(err) @@ -979,10 +981,6 @@ def _installed_forward_model_steps_from_dict( raise ConfigValidationError.from_collected(errors) return fm_steps - @property - def preferred_num_cpu(self) -> int: - return int(self.substitutions.get(f"<{ConfigKeys.NUM_CPU}>", 1)) - @property def env_pr_fm_step(self) -> dict[str, dict[str, Any]]: return self.ENV_PR_FM_STEP @@ -1084,14 +1082,6 @@ def _substitutions_from_dict(config_dict) -> Substitutions: if "" not in subst_list: subst_list[""] = config_dict.get("CONFIG_DIRECTORY", os.getcwd()) - num_cpus = config_dict.get("NUM_CPU") - if num_cpus is None and "DATA_FILE" in config_dict: - num_cpus = get_num_cpu_from_data_file(config_dict.get("DATA_FILE")) - logger.info(f"Parsed NUM_CPU={num_cpus} from DATA-file") - if num_cpus is None: - num_cpus = 1 - subst_list[""] = str(num_cpus) - for key, val in config_dict.get("DATA_KW", []): subst_list[key] = val diff --git a/src/ert/config/queue_config.py b/src/ert/config/queue_config.py index 3ac8579173a..1273f85e794 100644 --- a/src/ert/config/queue_config.py +++ b/src/ert/config/queue_config.py @@ -11,6 +11,7 @@ import pydantic from pydantic.dataclasses import dataclass +from ._get_num_cpu import get_num_cpu_from_data_file from .parsing import ( ConfigDict, ConfigKeys, @@ -260,6 +261,7 @@ class QueueConfig: queue_options_test_run: LocalQueueOptions = field(default_factory=LocalQueueOptions) stop_long_running: bool = False max_runtime: int | None = None + preferred_num_cpu: int = 1 @no_type_check @classmethod @@ -276,6 +278,19 @@ def from_dict(cls, config_dict: ConfigDict) -> QueueConfig: max_submit: int = config_dict.get(ConfigKeys.MAX_SUBMIT, 1) stop_long_running = config_dict.get(ConfigKeys.STOP_LONG_RUNNING, False) + preferred_num_cpu = 1 + if ConfigKeys.NUM_CPU in config_dict: + preferred_num_cpu = config_dict.get(ConfigKeys.NUM_CPU) + elif ConfigKeys.DATA_FILE in config_dict: + if preferred_num_cpu := get_num_cpu_from_data_file( + config_dict.get(ConfigKeys.DATA_FILE) + ): + logger.info( + f"Parsed NUM_CPU={preferred_num_cpu} from {ConfigKeys.DATA_FILE}" + ) + else: + preferred_num_cpu = 1 + raw_queue_options = config_dict.get("QUEUE_OPTION", []) grouped_queue_options = _group_queue_options_by_queue_system(raw_queue_options) _log_duplicated_queue_options(raw_queue_options) @@ -320,6 +335,7 @@ def from_dict(cls, config_dict: ConfigDict) -> QueueConfig: queue_options_test_run, stop_long_running=bool(stop_long_running), max_runtime=config_dict.get(ConfigKeys.MAX_RUNTIME), + preferred_num_cpu=preferred_num_cpu, ) def create_local_copy(self) -> QueueConfig: diff --git a/src/ert/run_models/base_run_model.py b/src/ert/run_models/base_run_model.py index bd6392647de..66d29c66161 100644 --- a/src/ert/run_models/base_run_model.py +++ b/src/ert/run_models/base_run_model.py @@ -605,7 +605,7 @@ def _build_ensemble( fm_steps=self.ert_config.forward_model_steps, max_runtime=self._queue_config.max_runtime, run_arg=run_arg, - num_cpu=self.ert_config.preferred_num_cpu, + num_cpu=self._queue_config.preferred_num_cpu, job_script=self.ert_config.queue_config.job_script, realization_memory=self.ert_config.queue_config.realization_memory, ) diff --git a/tests/ert/unit_tests/config/test_num_cpu.py b/tests/ert/unit_tests/config/test_num_cpu.py index 5bb32b19739..03862ec767b 100644 --- a/tests/ert/unit_tests/config/test_num_cpu.py +++ b/tests/ert/unit_tests/config/test_num_cpu.py @@ -11,7 +11,7 @@ def test_default_num_cpu(): ert_config = ErtConfig.from_file_contents("NUM_REALIZATIONS 1") - assert ert_config.preferred_num_cpu == 1 + assert ert_config.queue_config.preferred_num_cpu == 1 @pytest.mark.usefixtures("use_tmpdir") @@ -33,7 +33,7 @@ def test_num_cpu_from_config_preferred(): ConfigKeys.NUM_REALIZATIONS: 1, ConfigKeys.DATA_FILE: data_file, } - ).preferred_num_cpu + ).queue_config.preferred_num_cpu == config_num_cpu ) @@ -104,7 +104,7 @@ def test_num_cpu_from_data_file_used_if_config_num_cpu_not_set( ConfigKeys.NUM_REALIZATIONS: 1, ConfigKeys.DATA_FILE: data_file, } - ).preferred_num_cpu + ).queue_config.preferred_num_cpu == data_file_num_cpu )