-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathsubsystem.py
99 lines (76 loc) · 3.13 KB
/
subsystem.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import os
import subprocess
import re
import sys
import stat
import platform
import functools
import distutils.ccompiler
import tempfile
import shutil
import scorep.helper
def gen_subsystem_lib_name():
"""
generate the name for the subsystem lib.
"""
mpi_lib_name = "libscorep_init_subsystem-{}.so".format(
scorep.helper.get_python_version())
return mpi_lib_name
def generate_subsystem_code(config=[]):
"""
Generates the data needed to be preloaded.
"""
scorep_config = ["scorep-config"] + config + ["--user"]
(retrun_code, _, _) = scorep.helper.call(scorep_config)
if retrun_code != 0:
raise ValueError(
"given config {} is not supported".format(scorep_config))
(_, scorep_adapter_init, _) = scorep.helper.call(
scorep_config + ["--adapter-init"])
return scorep_adapter_init
def generate(scorep_config, keep_files=False):
"""
Uses the scorep_config to compile the scorep subsystem.
Returns the name of the compiled subsystem, and the path the the temp folder, where the lib is located
@param scorep_config scorep configuration to build subsystem
@param keep_files whether to keep the generated files, or not.
"""
(include, lib, lib_dir, macro,
linker_flags_tmp, cc, cxx) = scorep.helper.generate_compile_deps(scorep_config)
scorep_adapter_init = generate_subsystem_code(scorep_config)
# add -Wl,-no-as-needed to tell the compiler that we really want to link these. Actually this sould be default.
# as distutils adds extra args at the very end we need to add all the libs
# after this and skipt the libs later in the extension module
linker_flags = ["-Wl,-no-as-needed"]
linker_flags.extend(lib)
linker_flags.extend(linker_flags_tmp)
temp_dir = tempfile.mkdtemp(prefix="scorep.")
if keep_files:
sys.stderr.write("Score-P files are keept at: {}".format(temp_dir), file=sys.stderr)
with open(temp_dir + "/scorep_init.c", "w") as f:
f.write(scorep_adapter_init)
subsystem_lib_name = gen_subsystem_lib_name()
compiler = distutils.ccompiler.new_compiler()
# distutils UnixCCompiler as of python 3.7 simply doens not care about compiler, thats why we need to set compiler_so.
compiler.set_executables(compiler=cc, compiler_cxx=cxx, compiler_so=cc)
compiled_subsystem = compiler.compile(
[temp_dir + "/scorep_init.c"], output_dir=temp_dir)
compiler.link(
"scorep_init_mpi",
objects=compiled_subsystem,
output_filename=subsystem_lib_name,
output_dir=temp_dir,
library_dirs=lib_dir,
extra_postargs=linker_flags)
os.environ["SCOREP_PYTHON_BINDINGS_TEMP_DIR"] = temp_dir
return(subsystem_lib_name, temp_dir)
def clean_up(keep_files=True):
"""
deletes the files that are associated to subsystem
@param keep_files do not delete the generated files. For debugging.
"""
if keep_files:
return
else:
if ("SCOREP_PYTHON_BINDINGS_TEMP_DIR" in os.environ) and (os.environ["SCOREP_PYTHON_BINDINGS_TEMP_DIR"] != ""):
shutil.rmtree(os.environ["SCOREP_PYTHON_BINDINGS_TEMP_DIR"])