#!/usr/bin/env python import os import shlex import tempfile import subprocess as sp def run(cmd, ompthreads=None, blasthreads=None, fslskipglobal=None): env = os.environ.copy() blacklist = ['OMP', 'GOTO', 'BLAS', 'FSL'] for varname in list(env.keys()): if any(b in varname for b in blacklist): env.pop(varname) if ompthreads is not None: env['OMP_NUM_THREADS'] = str(ompthreads) if blasthreads is not None: env['BLAS_NUM_THREADS'] = str(blasthreads) if fslskipglobal is not None: env['FSL_SKIP_GLOBAL'] = str(fslskipglobal) result = sp.run(shlex.split(cmd), check=True, text=True, stdout=sp.PIPE, stderr=sp.STDOUT, env=env) print(f'Called {cmd} {ompthreads} {blasthreads} {fslskipglobal}') print(f' exit code: {result.returncode}') print(f' stdout: {result.stdout.strip()}') return result.stdout.strip().split('\n')[-1] def main(): buildcmds = ['source $FSLDIR/bin/activate $FSLDIR', 'source $FSLDIR/etc/fslconf/fsl-devel.sh', 'make'] sp.run('; '.join(buildcmds), check=True, shell=True) # Default behaviour should be: OMP multi-threaded, BLAS single threaded. assert run('./test_fslStartup', 8, 8) == '8 1 8' assert run('./test_fslStartup', 4, 4) == '4 1 4' assert run('./test_fslStartup', 1, 1) == '1 1 1' # FSL_SKIP_GLOBAL=0 should be equivalent to default behaviour assert run('./test_fslStartup', 8, 8, 0) == '8 1 8' assert run('./test_fslStartup', 4, 4, 0) == '4 1 4' assert run('./test_fslStartup', 1, 1, 0) == '1 1 1' # With FSL_SKIP_GLOBAL=1, BLAS should be multi-threaded assert run('./test_fslStartup', 8, 8, 1) == '8 8 8' assert run('./test_fslStartup', 4, 4, 1) == '4 4 4' assert run('./test_fslStartup', 1, 1, 1) == '1 1 1' if __name__ == '__main__': main()