Skip to content

Commit b6406ed

Browse files
author
Thierry RAMORASOAVINA
committed
Ensure HOME is always set when running khiops core with OpenMPI 5+
- A specific environment is created when running khiops core. In this environment HOME is set using KHIOPS_MPI_HOME if it exists
1 parent 6aea158 commit b6406ed

File tree

2 files changed

+54
-2
lines changed

2 files changed

+54
-2
lines changed

khiops/core/internals/runner.py

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,22 @@ def _get_current_library_installer():
327327
return "unknown"
328328

329329

330+
def _build_khiops_process_environment():
331+
"""Build a specific environment used for the execution of khiops in a process
332+
333+
This environment can be modified freely without interfering
334+
with the global one.
335+
"""
336+
khiops_env = os.environ.copy()
337+
338+
# Ensure HOME is always set for OpenMPI 5+
339+
# (using KHIOPS_MPI_HOME if it exists)
340+
khiops_env["HOME"] = os.path.pathsep.join(
341+
[khiops_env.get("KHIOPS_MPI_HOME", ""), khiops_env.get("HOME", "")]
342+
)
343+
return khiops_env
344+
345+
330346
class KhiopsRunner(ABC):
331347
"""Abstract Khiops Python runner to be re-implemented"""
332348

@@ -994,8 +1010,14 @@ def _initialize_khiops_environment(self):
9941010
var_value = ""
9951011
else:
9961012
continue
1013+
1014+
# Special var to export in order
1015+
# to prepend to or to set to HOME for OpenMPI 5+
1016+
# when running khiops core
1017+
if var_name == "KHIOPS_MPI_HOME":
1018+
os.environ["KHIOPS_MPI_HOME"] = var_value
9971019
# Set paths to Khiops binaries
998-
if var_name == "KHIOPS_PATH":
1020+
elif var_name == "KHIOPS_PATH":
9991021
self.khiops_path = var_value
10001022
os.environ["KHIOPS_PATH"] = var_value
10011023
elif var_name == "KHIOPS_COCLUSTERING_PATH":
@@ -1443,6 +1465,11 @@ def raw_run(self, tool_name, command_line_args=None, use_mpi=True, trace=False):
14431465
khiops_call += f" {quote}{arg}{quote}"
14441466
print(f"Khiops execution call: {khiops_call}")
14451467

1468+
# Build custom Khiops process environment
1469+
# which makes sure HOME is defined and set
1470+
# according to khiops_env's KHIOPS_MPI_HOME
1471+
khiops_env = _build_khiops_process_environment()
1472+
14461473
# Execute the process
14471474
with subprocess.Popen(
14481475
khiops_process_args,
@@ -1452,6 +1479,7 @@ def raw_run(self, tool_name, command_line_args=None, use_mpi=True, trace=False):
14521479
encoding="utf8",
14531480
errors="replace",
14541481
universal_newlines=True,
1482+
env=khiops_env,
14551483
) as khiops_process:
14561484
stdout, stderr = khiops_process.communicate()
14571485

tests/test_khiops_integrations.py

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,10 @@
1919
import khiops.core.internals.filesystems as fs
2020
from khiops import tools
2121
from khiops.core.exceptions import KhiopsEnvironmentError
22-
from khiops.core.internals.runner import KhiopsLocalRunner
22+
from khiops.core.internals.runner import (
23+
KhiopsLocalRunner,
24+
_build_khiops_process_environment,
25+
)
2326
from khiops.extras.docker import KhiopsDockerRunner
2427
from khiops.sklearn.estimators import KhiopsClassifier
2528
from tests.test_helper import KhiopsTestHelper
@@ -282,12 +285,33 @@ def test_environment_error_on_bogus_khiops_env_script(self):
282285
output_msg = str(context.exception)
283286
self.assertEqual(output_msg, expected_msg)
284287

288+
def test_runner_environment_for_openmpi5(self):
289+
"""Test if KHIOPS_MPI_HOME is actually exported
290+
and HOME is corrected for OpenMPI 5+"""
291+
292+
# Trigger the environment initialization
293+
_ = kh.get_runner().khiops_version
294+
295+
khiops_env = _build_khiops_process_environment()
296+
297+
# Check `KHIOPS_MPI_HOME` is correctly exported for OpenMPI 5+
298+
self.assertIsNotNone(os.environ.get("KHIOPS_MPI_HOME"))
299+
300+
# Check HOME is corrected in the new process environment
301+
self.assertEqual(
302+
os.path.pathsep.join(
303+
[khiops_env.get("KHIOPS_MPI_HOME", ""), os.environ.get("HOME", "")]
304+
),
305+
khiops_env.get("HOME"),
306+
)
307+
285308
def test_runner_environment_initialization(self):
286309
"""Test that local runner initializes/ed its environment properly
287310
288311
.. note::
289312
To test a real initialization this test should be executed alone.
290313
"""
314+
291315
# Obtain the current runner
292316
runner = kh.get_runner()
293317

0 commit comments

Comments
 (0)