Skip to content

Commit 1c7bf2d

Browse files
authored
Adapt to numpy NEP-18 (#65)
Together with the introduction of [NEP-18](https://numpy.org/neps/nep-0018-array-function-protocol.html) some numpy functions appeared the name `None:dot`. `None` is misleading. The solution discussed in #63 is to make the reference to numpy and the related function `__array_function__` explicit. As we are already started using co_filename, we changed to co functions as well.
1 parent 500c7a0 commit 1c7bf2d

4 files changed

Lines changed: 64 additions & 8 deletions

File tree

scorep/instrumenters/scorep_profile.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,22 +81,30 @@ def globaltrace_lt(self, frame, why, arg):
8181
code = frame.f_code
8282
modulename = frame.f_globals.get('__name__', None)
8383
if modulename is None:
84-
modulename = "None"
84+
# this is a NUMPY special situation, see NEP-18, and Score-P Issue issues #63
85+
if code.co_filename == "<__array_function__ internals>":
86+
modulename = "numpy.__array_function__"
87+
else:
88+
modulename = "unkown"
8589
if not code.co_name == "_unsetprofile" and not modulename[:6] == "scorep":
86-
file_name = frame.f_globals.get('__file__', None)
90+
file_name = code.co_filename
8791
if file_name is not None:
8892
full_file_name = os.path.abspath(file_name)
8993
else:
9094
full_file_name = "None"
91-
line_number = frame.f_lineno
95+
line_number = code.co_firstlineno
9296
self.scorep_bindings.region_begin(
9397
modulename, code.co_name, full_file_name, line_number)
9498
return
9599
elif why == 'return':
96100
code = frame.f_code
97101
modulename = frame.f_globals.get('__name__', None)
98102
if modulename is None:
99-
modulename = "None"
103+
# this is a NUMPY special situation, see NEP-18, and Score-P Issue issues #63
104+
if code.co_filename == "<__array_function__ internals>":
105+
modulename = "numpy.__array_function__"
106+
else:
107+
modulename = "unkown"
100108
if not code.co_name == "_unsetprofile" and not modulename[:6] == "scorep":
101109
self.scorep_bindings.region_end(modulename, code.co_name)
102110
else:

scorep/instrumenters/scorep_trace.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,14 +80,18 @@ def globaltrace_lt(self, frame, why, arg):
8080
code = frame.f_code
8181
modulename = frame.f_globals.get('__name__', None)
8282
if modulename is None:
83-
modulename = "None"
83+
# this is a NUMPY special situation, see NEP-18, and Score-P Issue issues #63
84+
if code.co_filename == "<__array_function__ internals>":
85+
modulename = "numpy.__array_function__"
86+
else:
87+
modulename = "unkown"
8488
if not code.co_name == "_unsettrace" and not modulename[:6] == "scorep":
85-
file_name = frame.f_globals.get('__file__', None)
89+
file_name = code.co_filename
8690
if file_name is not None:
8791
full_file_name = os.path.abspath(file_name)
8892
else:
8993
full_file_name = "None"
90-
line_number = frame.f_lineno
94+
line_number = code.co_firstlineno
9195
self.scorep_bindings.region_begin(
9296
modulename, code.co_name, full_file_name, line_number)
9397
return self.localtrace
@@ -98,7 +102,11 @@ def localtrace_trace(self, frame, why, arg):
98102
code = frame.f_code
99103
modulename = frame.f_globals.get('__name__', None)
100104
if modulename is None:
101-
modulename = "None"
105+
# this is a NUMPY special situation, see NEP-18, and Score-P Issue issues #63
106+
if code.co_filename == "<__array_function__ internals>":
107+
modulename = "numpy.__array_function__"
108+
else:
109+
modulename = "unkown"
102110
self.scorep_bindings.region_end(modulename, code.co_name)
103111
return self.localtrace
104112

test/test.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,37 @@ def test_dummy(self):
326326
env["SCOREP_EXPERIMENT_DIRECTORY"]),
327327
"Score-P directory exists for dummy test")
328328

329+
@unittest.skipIf(sys.version_info.major < 3, "not tested for python 2")
330+
def test_numpy_dot(self):
331+
env = self.env
332+
env["SCOREP_EXPERIMENT_DIRECTORY"] += "/test_numpy_dot"
333+
trace_path = env["SCOREP_EXPERIMENT_DIRECTORY"] + "/traces.otf2"
334+
335+
out = call([self.python,
336+
"-m",
337+
"scorep",
338+
"--nocompiler",
339+
"--noinstrumenter",
340+
"test_numpy_dot.py"],
341+
env=env)
342+
std_out = out[1]
343+
std_err = out[2]
344+
345+
self.assertEqual(std_out, "[[ 7 10]\n [15 22]]\n")
346+
self.assertEqual(std_err, self.expected_std_err)
347+
348+
349+
out = call(["otf2-print", trace_path])
350+
std_out = out[1]
351+
std_err = out[2]
352+
353+
self.assertEqual(std_err, "")
354+
self.assertRegex(std_out,
355+
'ENTER[ ]*[0-9 ]*[0-9 ]*Region: "numpy.__array_function__:dot"')
356+
self.assertRegex(std_out,
357+
'LEAVE[ ]*[0-9 ]*[0-9 ]*Region: "numpy.__array_function__:dot"')
358+
359+
329360
def tearDown(self):
330361
#pass
331362
shutil.rmtree(

test/test_numpy_dot.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import numpy
2+
import scorep.instrumenter
3+
4+
with scorep.instrumenter.enable():
5+
a = [[1, 2],[3, 4]]
6+
b = [[1, 2],[3, 4]]
7+
8+
c = numpy.dot(a,b)
9+
print(c)

0 commit comments

Comments
 (0)