Skip to content

Commit 1123c16

Browse files
refactor tabulate_profiling_data
1 parent 55537cc commit 1123c16

3 files changed

Lines changed: 64 additions & 37 deletions

File tree

‎arraycontext/impl/pytato/__init__.py‎

Lines changed: 12 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@
5959

6060
import numpy as np
6161

62-
import pytools
62+
import pytools as pytools
6363
from pytools import memoize_method
6464
from pytools.tag import Tag, ToTagSetConvertible, normalize_tags
6565

@@ -358,6 +358,8 @@ def __init__(
358358

359359
self._force_svm_arg_limit = _force_svm_arg_limit
360360

361+
# {{{ Profiling functionality
362+
361363
def _wait_and_transfer_profile_events(self) -> None:
362364
import pyopencl as cl
363365
# First, wait for completion of all events
@@ -403,43 +405,20 @@ def reset_profiling_data_for_kernel(self, kernel_name: str) -> None:
403405
"""Reset profiling data for kernel *kernel_name*."""
404406
self.profile_results.pop(kernel_name, None)
405407

406-
def tabulate_profiling_data(self) -> pytools.Table:
407-
"""Return a :class:`pytools.Table` with the profiling results."""
408+
def get_and_reset_profiling_data(self) -> dict[str, MultiCallKernelProfile]:
409+
"""Return and reset profiling data."""
408410
self._wait_and_transfer_profile_events()
409411

410-
tbl = pytools.Table()
411-
412-
# Table header
413-
tbl.add_row(("Kernel", "# Calls", "Time_sum [ns]", "Time_avg [ns]"))
414-
415-
# Precision of results
416-
g = ".4g"
417-
418-
total_calls = 0
419-
total_time = 0.0
420-
421-
for knl in self.profile_results:
422-
r = self.get_profiling_data_for_kernel(knl)
423-
424-
total_calls += r.num_calls
425-
426-
t_sum = r.time
427-
t_avg = r.time / r.num_calls
428-
if t_sum is not None:
429-
total_time += t_sum
430-
431-
time_sum = f"{t_sum:{g}}"
432-
time_avg = f"{t_avg:{g}}"
433-
434-
tbl.add_row((knl, r.num_calls, time_sum,
435-
time_avg,))
436-
437-
tbl.add_row(("", "", "", ""))
438-
tbl.add_row(("Total", total_calls, f"{total_time:{g}}", "--"))
412+
result = {
413+
kernel_name: MultiCallKernelProfile(len(times), sum(times))
414+
for kernel_name, times in self.profile_results.items()
415+
}
439416

440417
self.profile_results = {}
441418

442-
return tbl
419+
return result
420+
421+
# }}}
443422

444423
@property
445424
def _frozen_array_types(self) -> tuple[type, ...]:

‎arraycontext/impl/pytato/utils.py‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
from collections.abc import Mapping
3636
from typing import TYPE_CHECKING, Any, cast
3737

38+
import pytools
3839
from pytato.array import (
3940
AbstractResultWithNamedArrays,
4041
Array,
@@ -51,6 +52,7 @@
5152

5253
from arraycontext import ArrayContext
5354
from arraycontext.impl.pyopencl.taggable_cl_array import Axis as ClAxis
55+
from arraycontext.impl.pytato import MultiCallKernelProfile
5456

5557

5658
if TYPE_CHECKING:
@@ -221,4 +223,38 @@ def transfer_to_numpy(expr: ArrayOrNames, actx: ArrayContext) -> ArrayOrNames:
221223

222224
# }}}
223225

226+
227+
def tabulate_profiling_data(profile_results: dict[str, MultiCallKernelProfile])\
228+
-> pytools.Table:
229+
"""Return a :class:`pytools.Table` with the profiling results."""
230+
tbl = pytools.Table()
231+
232+
# Table header
233+
tbl.add_row(("Kernel", "# Calls", "Time_sum [ns]", "Time_avg [ns]"))
234+
235+
# Precision of results
236+
g = ".4g"
237+
238+
total_calls = 0
239+
total_time = 0.0
240+
241+
for kernel_name, mckp in profile_results.items():
242+
total_calls += mckp.num_calls
243+
244+
t_sum = mckp.time
245+
t_avg = mckp.time / mckp.num_calls
246+
if t_sum is not None:
247+
total_time += t_sum
248+
249+
time_sum = f"{t_sum:{g}}"
250+
time_avg = f"{t_avg:{g}}"
251+
252+
tbl.add_row((kernel_name, mckp.num_calls, time_sum,
253+
time_avg,))
254+
255+
tbl.add_row(("", "", "", ""))
256+
tbl.add_row(("Total", total_calls, f"{total_time:{g}}", "--"))
257+
258+
return tbl
259+
224260
# vim: foldmethod=marker

‎test/test_pytato_arraycontext.py‎

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626

2727
import logging
2828

29+
import numpy as np
2930
import pytest
3031

3132
from pytools.tag import Tag
@@ -289,27 +290,38 @@ def twice(x):
289290

290291
f = actx.compile(twice)
291292

293+
assert len(actx.profile_events) == 0
294+
292295
for _ in range(10):
293296
assert actx.to_numpy(f(99)) == 198
294297

298+
assert len(actx.profile_events) == 10
295299
actx._wait_and_transfer_profile_events()
300+
assert len(actx.profile_events) == 0
296301
assert len(actx.profile_results) == 1
297302
assert len(actx.profile_results["twice"]) == 10
298303

299-
print(actx.tabulate_profiling_data())
304+
from arraycontext.impl.pytato.utils import tabulate_profiling_data
305+
306+
print(tabulate_profiling_data(actx.get_and_reset_profiling_data()))
307+
assert len(actx.profile_results) == 0
300308

301309
# }}}
302310

303311
# {{{ Uncompiled/frozen test
304312

313+
assert len(actx.profile_events) == 0
314+
305315
for _ in range(10):
306-
assert actx.to_numpy(twice(99)) == 198
316+
assert np.all(actx.to_numpy(twice(actx.from_numpy(np.array([99, 99])))) == 198)
307317

318+
assert len(actx.profile_events) == 10
308319
actx._wait_and_transfer_profile_events()
320+
assert len(actx.profile_events) == 0
309321
assert len(actx.profile_results) == 1
310-
assert len(actx.profile_results["twice"]) == 10
322+
assert len(actx.profile_results["frozen_result"]) == 10
311323

312-
print(actx.tabulate_profiling_data())
324+
print(tabulate_profiling_data(actx.get_and_reset_profiling_data()))
313325

314326
# }}}
315327

0 commit comments

Comments
 (0)