11r"""
22Benchmark Utility in PyLops-MPI
3- =========================
4- This tutorial demonstrates how to use the bencmark utility of PyLops-MPI. It contains various
5- function calling pattern that may come up during the benchmarking.
3+ ===============================
4+ This tutorial demonstrates how to use the :py:func:`pylops_mpi.utils.benchmark` and
5+ :py:func:`pylops_mpi.utils.mark` utility methods in PyLops-MPI. It contains various
6+ function calling pattern that may come up during the benchmarking of a distributed code.
7+
8+ :py:func:`pylops_mpi.utils.benchmark` is a decorator used to decorate any
9+ function to measure its execution time from start to finish
10+ :py:func:`pylops_mpi.utils.mark` is a function used inside the benchmark-decorated
11+ function to provide fine-grain time measurements.
612"""
13+
14+ import sys
15+ import logging
716import numpy as np
817from mpi4py import MPI
918from pylops_mpi import DistributedArray , Partition
1625 'axis' : 1 }
1726
1827###############################################################################
19- # Let's start by import the utility
28+ # Let's start by import the utility and a simple exampple
2029from pylops_mpi .utils .benchmark import benchmark , mark
2130
22- ###############################################################################
23- # :py:func:`pylops_mpi.utils.benchmark` is a decorator used to decorate any
24- # function to measure its execution time from start to finish
25- # :py:func:`pylops_mpi.utils.mark` is a function used inside the benchmark-decorated
26- # function to provide fine-grain time measurements. Let's start with a simple example
27-
2831
2932@benchmark
3033def inner_func (par ):
@@ -38,14 +41,15 @@ def inner_func(par):
3841###############################################################################
3942# When we call :py:func:`inner_func`, we will see the result
4043# of the benchmark print to standard output. If we want to customize the
41- # function name in the printout, we can pass the parameter to the :py:func:`benchmark`
44+ # function name in the printout, we can pass the parameter `description`
45+ # to the :py:func:`benchmark`
4246# i.e., :py:func:`@benchmark(description="printout_name")`
4347
4448inner_func (par )
4549
4650###############################################################################
47- # We may want to get the finer time measurement by timing the execution time from arbitary lines
48- # of code. :py:func:`pylops_mpi.utils.mark` provides such utitlity
51+ # We may want to get the fine-grained time measurements by timing the execution
52+ # time of arbitary lines of code. :py:func:`pylops_mpi.utils.mark` provides such utitlity.
4953
5054
5155@benchmark
@@ -60,14 +64,14 @@ def inner_func_with_mark(par):
6064
6165
6266###############################################################################
63- # Now when we run, we get the detail time measurement. Noted that there is a tag
64- # [decorator] to the function name to distinguish between the start-to-end time measuredment of
65- # top-level function and those that comes from :py:func:`pylops_mpi.utils.mark`
67+ # Now when we run, we get the detailed time measurement. Note that there is a tag
68+ # [decorator] next to the function name to distinguish between the start-to-end time
69+ # measurement of the top-level function and those that comes from :py:func:`pylops_mpi.utils.mark`
6670inner_func_with_mark (par )
6771
6872###############################################################################
69- # This utility also supports the nested functions . Let's define the outer function
70- # that internally calls decorated :py:func:`inner_func_with_mark`
73+ # This utility benchmarking routines can also be nested . Let's define
74+ # an outer function that internally calls the decorated :py:func:`inner_func_with_mark`
7175
7276
7377@benchmark
@@ -83,24 +87,20 @@ def outer_func_with_mark(par):
8387
8488###############################################################################
8589# If we run :py:func:`outer_func_with_mark`, we get the time measurement nicely
86- # printout with the nested indentation to specify that nested calls.
90+ # printed out with the nested indentation to specify that nested calls.
8791outer_func_with_mark (par )
8892
8993
9094###############################################################################
9195# In some cases, we may want to write benchmark output to a text file.
9296# :py:func:`pylops_mpi.utils.benchmark` also takes the py:class:`logging.Logger`
93- # in its argument. Let's first import the logging package and construct our logger
97+ # in its argument.
98+ # Here we define a simple :py:func:`make_logger()`. We set the :py:func:`logger.propagate = False`
99+ # to isolate the logging of our benchmark from that of the rest of the code
94100
95- import sys
96- import logging
97101save_file = True
98102file_path = "benchmark.log"
99103
100- ###############################################################################
101- # Here we define a simple :py:func:`make_logger()`. We set the :py:func:`logger.propagate = False`
102- # isolate the logging of our benchmark from that of the rest of the code
103-
104104
105105def make_logger (save_file = False , file_path = '' ):
106106 logger = logging .getLogger (__name__ )
0 commit comments