Skip to content

Commit

Permalink
Refactor preferred_num_cpu
Browse files Browse the repository at this point in the history
  • Loading branch information
frode-aarstad committed Jan 8, 2025
1 parent 383d645 commit 7068a62
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 17 deletions.
16 changes: 3 additions & 13 deletions src/ert/config/ert_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -457,6 +456,9 @@ def from_dict(cls, config_dict) -> Self:
]
)
queue_config = QueueConfig.from_dict(config_dict)

substitutions["<NUM_CPU>"] = str(queue_config.preferred_num_cpu)

except ConfigValidationError as err:
errors.append(err)

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -1084,14 +1082,6 @@ def _substitutions_from_dict(config_dict) -> Substitutions:
if "<CONFIG_PATH>" not in subst_list:
subst_list["<CONFIG_PATH>"] = 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["<NUM_CPU>"] = str(num_cpus)

for key, val in config_dict.get("DATA_KW", []):
subst_list[key] = val

Expand Down
16 changes: 16 additions & 0 deletions src/ert/config/queue_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion src/ert/run_models/base_run_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
Expand Down
6 changes: 3 additions & 3 deletions tests/ert/unit_tests/config/test_num_cpu.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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
)

Expand Down Expand Up @@ -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
)

Expand Down

0 comments on commit 7068a62

Please sign in to comment.