-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstack_pmpi
executable file
·166 lines (134 loc) · 6.51 KB
/
stack_pmpi
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#!/bin/env python
####################################################################################
# Copyright (c) 2016, Lawrence Livermore National Security, LLC.
# Produced at the Lawrence Livermore National Laboratory.
#
# Written by Kento Sato, [email protected]. LLNL-CODE-711378.
# All rights reserved.
#
# This file is part of StackP. For details, see https://github.com/PRUNER/StackP
# Please also see the LICENSE.TXT file for our notice and the LGPL.
#
# This program is free software; you can redistribute it and/or modify it under the
# terms of the GNU General Public License (as published by the Free Software
# Foundation) version 2.1 dated February 1999.
#
# This program is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the IMPLIED WARRANTY OF MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE. See the terms and conditions of the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License along
# with this program; if not, write to the Free Software Foundation, Inc., 59 Temple
# Place, Suite 330, Boston, MA 02111-1307 USA
####################################################################################
import sys, os
def is_mpi_function(func_name):
return func_name.startswith("MPI_")
def is_pmpi_function(func_name):
return func_name.startswith("PMPI_")
def mpi_symbol_list(option, labels, is_mpi, file):
outputs = os.popen("nm " + option + " " + file).read().split("\n")
defined_symbols = {}
for line in outputs:
vals = line.split(" ")
if len(vals) < 3: continue
count = labels.count(vals[-2])
if count == 0: continue
if is_mpi:
if not is_mpi_function(vals[-1]): continue
defined_symbols[vals[-1]] = None
else: #is_pmpi
if not is_pmpi_function(vals[-1]): continue
defined_symbols[vals[-1][1:]] = None
return defined_symbols
def defined_mpi_symbol_list(file):
lst = mpi_symbol_list("--defined-only", "TD", True, file)
return lst
def undefined_pmpi_symbol_list(file):
lst = mpi_symbol_list("--undefined-only", "U", False, file)
return lst
USAGE = "ws <output.o> <wraper1> <wraper2> ... "
if len(sys.argv) < 4:
print USAGE
output_obj = sys.argv[1]
wrap_files = sys.argv[2:]
symbol_stack = []
first_file=wrap_files[0]
module_id="_" + first_file.strip("./").split(".")[-2] + "_"
#print wrap_files, first_file, module_id
for wrap_file in wrap_files:
symbol_stack.append(
(wrap_file,
defined_mpi_symbol_list(wrap_file),
undefined_pmpi_symbol_list(wrap_file)
)
)
#print symbol_stack
for stack_id in range(len(symbol_stack)):
symbol_table = symbol_stack[stack_id]
undefined_symbols = symbol_table[2]
for undefined_symbol in undefined_symbols:
for target_stack_id in range(stack_id + 1, len(symbol_stack)):
lower_defined_symbols = symbol_stack[target_stack_id][1]
if lower_defined_symbols.has_key(undefined_symbol):
undefined_symbols[undefined_symbol] = target_stack_id
lower_defined_symbols[undefined_symbol] = target_stack_id
break
# print stack_id, target_stack_id, undefined_symbol, undefined_symbols, lower_defined_symbols
# print stack_id, target_stack_id, symbol_stack
for stack_id in range(len(symbol_stack)):
symbol_table = symbol_stack[stack_id]
defined_symbols = symbol_table[1]
for defined_symbol in defined_symbols:
for target_stack_id in range(stack_id + 1, len(symbol_stack)):
lower_defined_symbols = symbol_stack[target_stack_id][1]
# print lower_defined_symbols, defined_symbol
if lower_defined_symbols.has_key(defined_symbol):
if lower_defined_symbols[defined_symbol] == None:
lower_defined_symbols[defined_symbol] = -1
# print symbol_stack[target_stack_id][0], defined_symbol
#create tmp files
tmpobj_files = ""
pid = str(os.getpid());
for symbol_table in symbol_stack:
stack_name = symbol_table[0]
tmpobj_file = os.path.basename(stack_name) + "." + pid + ".wstmp"
tmpobj_files = tmpobj_files + " " + tmpobj_file
os.popen("cp -f " + stack_name + " ./" + tmpobj_file)
#print "======"
#for vs in symbol_stack:
# for v in vs:
# print v
#print symbol_stack
target_stack_id = 1
for symbol_table in symbol_stack:
stack_name = symbol_table[0]
objcopy_file = os.path.basename(stack_name) + ".objcopy"
tmpobj_file = os.path.basename(stack_name) + "." + pid + ".wstmp"
defined_symbols = symbol_table[1]
undefined_symbols = symbol_table[2]
fd = open(objcopy_file, "w");
for defined_symbol, defined_stack_id in defined_symbols.items():
if defined_stack_id != None:
old = defined_symbol
new = ""
if defined_stack_id == -1:
new = "UNUSED_WRAPPER_" + str(target_stack_id) + module_id + old
else:
new = "P" + str(defined_stack_id) + module_id + old
val = old + " " + new + "\n"
fd.write(val)
for undefined_symbol, undefined_stack_id in undefined_symbols.items():
if undefined_stack_id != None:
old = "P" + undefined_symbol
new = "P" + str(undefined_stack_id) + module_id + undefined_symbol
val = old + " " + new + "\n"
fd.write(val)
fd.close()
os.popen("objcopy --redefine-syms=" + objcopy_file + " ./" + tmpobj_file)
os.popen("rm -f " + objcopy_file)
target_stack_id += 1
os.popen("ld -o " + output_obj + " -r " + tmpobj_files)
os.popen("rm -f " + tmpobj_files)
# print os.popen("nm --undefined-only " + wrap_file).read()