<commit_before>#!/usr/bin/env python

"""
Relaunches an mpi4py program with mpiexec so that it can spawn processes.
Should be the first import in the program.
"""

import inspect
import os
import re
import subprocess
import sys

from mpi4py import MPI
import psutil

# Prevent SLURM from preventing mpiexec from starting multiple processes
# (this approach should probably be modified to take some SLURM variables into
# consideration):
env = os.environ.copy()
for k in env.keys():
    if k.startswith('SLURM'):
        del env[k]

# Get name of the file in which this module is imported:
script_name = os.path.basename(inspect.stack()[1][1])
parent_name = psutil.Process(os.getppid()).name()
if not re.search('mpirun|mpiexec', parent_name):
    try:
        subprocess.call(['mpiexec', '-np', '1',
                        sys.executable, script_name],
                        env=env,
                        stdout=sys.stdout,
                        stderr=sys.stderr,
                        stdin=sys.stdin)
    except IOError:
        raise IOError('cannot execute mpiexec')
    else:
        sys.exit(0)
<commit_msg>Don't extract basename of script in mpi_relaunch module so that the script can be relaunched even when invoked from another directory.<commit_after>#!/usr/bin/env python

"""
Relaunches an mpi4py program with mpiexec so that it can spawn processes.
Should be the first import in the program.
"""

import inspect
import os
import re
import subprocess
import sys

from mpi4py import MPI
import psutil

# Prevent SLURM from preventing mpiexec from starting multiple processes
# (this approach should probably be modified to take some SLURM variables into
# consideration):
env = os.environ.copy()
for k in env.keys():
    if k.startswith('SLURM'):
        del env[k]

# Get name of the file in which this module is imported:
script_name = inspect.stack()[1][1]
parent_name = psutil.Process(os.getppid()).name()
if not re.search('mpirun|mpiexec', parent_name):
    try:
        subprocess.call(['mpiexec', '-np', '1',
                        sys.executable, script_name],
                        env=env,
                        stdout=sys.stdout,
                        stderr=sys.stderr,
                        stdin=sys.stdin)
    except IOError:
        raise IOError('cannot execute mpiexec')
    else:
        sys.exit(0)

{'fraction_non_alphanumeric': 0.09195402298850575, 'fraction_numerical': 0.005108556832694764, 'mean_word_length': 2.6490683229813663, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5496096', 'n_tokens_mistral': 703, 'n_tokens_neox': 683, 'n_words': 234}